From 62c3898d8e18a872700948c46a61592b315e79de Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 22 Nov 2019 19:34:09 +0200 Subject: WebProfile refactoring - Remove WebProfileManager::Profile::value - Make WebProfile constructors protected, and WebProfileManager friend class --- src/cli/cli.cpp | 92 ------------------------------------- src/cli/meson.build | 4 -- src/webengine/webprofile.cpp | 13 +++++- src/webengine/webprofile.h | 28 +++++------ src/webengine/webprofilemanager.cpp | 21 +++++---- src/webengine/webprofilemanager.h | 15 ------ test/cli/cli.cpp | 92 +++++++++++++++++++++++++++++++++++++ test/cli/meson.build | 4 ++ 8 files changed, 130 insertions(+), 139 deletions(-) delete mode 100644 src/cli/cli.cpp delete mode 100644 src/cli/meson.build create mode 100644 test/cli/cli.cpp create mode 100644 test/cli/meson.build diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp deleted file mode 100644 index 322c365..0000000 --- a/src/cli/cli.cpp +++ /dev/null @@ -1,92 +0,0 @@ -#include -#include -#include -#include - -static int numargs=0; - -/* Return the number of arguments of the application command line */ -static PyObject* emb_numargs(PyObject *self, PyObject *args) -{ - if(!PyArg_ParseTuple(args, ":numargs")) - return NULL; - - numargs = 19; - return PyLong_FromLong(numargs); -} - -static PyMethodDef EmbMethods[] = { - /* ml_name Name of the method - * ml_meth pointer to C implementation - * ml_flags flag bits indicating how it should be called - * ml_doc docstring - */ - {"numargs", emb_numargs, METH_VARARGS, "Return the number of arguments received by the process."}, - {NULL, NULL, 0, NULL} -}; - -static PyModuleDef EmbModule = { - /* m_base */ PyModuleDef_HEAD_INIT, // base module, always HEAD_INIT - /* m_name */ "emb", // module name - /* m_doc */ NULL, // Docstring for the module; usually a docstring variable created with PyDoc_STRVAR() - /* m_size */ -1, // - /* m_methods */ EmbMethods, // A pointer to a table of module-level functions - /* m_slots */ NULL, // An array of slot definitions for multi-phase initialization - /* traverse */ NULL, // A traversal function to call during GC traversal of the module object - /* clear */ NULL, // A clear function to call during GC clearing of the module object - /* free */ NULL // A function to call during deallocation of the module object -}; - -static PyObject* PyInit_emb(void) -{ - return PyModule_Create(&EmbModule); -} - -int main(int argc, char** argv) -{ - printf("cli test application\n"); - - wchar_t *program = Py_DecodeLocale(argv[0], NULL); - if (program == NULL) { - fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); - exit(1); - } - - // inform the interpreter about paths to run-time libraries - Py_SetProgramName(program); /* optional but recommended */ - - printf("import emb: %i\n", PyImport_AppendInittab("emb", &PyInit_emb)); - - // Initialize the python interpreter - Py_Initialize(); - - PyRun_SimpleString("print('Python interpreter ready')\n"); - - const char* prompt = "poi> "; - - while(true) { - char *cmd = linenoise(prompt); - - if(cmd == nullptr || *cmd == '\0') { - printf("breaking out of repl\n"); - free(cmd); - break; - } - - //printf("echo(%i):'%s'\n", strlen(cmd), cmd); - PyRun_SimpleString(cmd); - linenoiseHistoryAdd(cmd); - free(cmd); - } - - // finalize the interpreter - if (Py_FinalizeEx() < 0) { - exit(120); - } - - // - PyMem_RawFree(program); - - return 0; -} - diff --git a/src/cli/meson.build b/src/cli/meson.build deleted file mode 100644 index 298e1db..0000000 --- a/src/cli/meson.build +++ /dev/null @@ -1,4 +0,0 @@ -cli_demo = executable('cli', install: false, dependencies: [ optional_deps ], - sources: [ 'cli.cpp' ] -) - diff --git a/src/webengine/webprofile.cpp b/src/webengine/webprofile.cpp index 843b78e..2cea409 100644 --- a/src/webengine/webprofile.cpp +++ b/src/webengine/webprofile.cpp @@ -12,7 +12,18 @@ #include #include -WebProfile *WebProfile::profile = nullptr; +static WebProfile *s_profile = nullptr; + +void WebProfile::setDefaultProfile(WebProfile *profile) +{ + Q_CHECK_PTR(profile); + s_profile = profile; +} +WebProfile *WebProfile::defaultProfile() +{ + Q_CHECK_PTR(s_profile); + return s_profile; +} WebProfile::WebProfile(const QString &name, QObject *parent) : Profile(parent) diff --git a/src/webengine/webprofile.h b/src/webengine/webprofile.h index 37e3419..1ec2b88 100644 --- a/src/webengine/webprofile.h +++ b/src/webengine/webprofile.h @@ -19,29 +19,19 @@ #include #include +class WebProfileManager; class WebProfile : public Profile { + friend class WebProfileManager; + Q_OBJECT public: - // off-the-record constructor - explicit WebProfile(const QString &name, QObject *parent = nullptr); - // default constructor - explicit WebProfile(const QString &storageName, const QString &name, QObject *parent = nullptr); + static WebProfile *defaultProfile(); + static void setDefaultProfile(WebProfile *profile); ~WebProfile() = default; - static void setDefaultProfile(WebProfile *profile) - { - Q_CHECK_PTR(profile); - WebProfile::profile = profile; - } - static WebProfile *defaultProfile() - { - Q_CHECK_PTR(WebProfile::profile); - return WebProfile::profile; - } - const QString name() const; void setName(const QString &name); @@ -79,9 +69,13 @@ public: void setSpellCheckEnabled(bool enable); -private: - static WebProfile *profile; +protected: + // off-the-record constructor + explicit WebProfile(const QString &name, QObject *parent = nullptr); + // default constructor + explicit WebProfile(const QString &storageName, const QString &name, QObject *parent = nullptr); +private: QString m_name; QString m_search = QString("about:blank"); QUrl m_homepage = QUrl("about:blank"); diff --git a/src/webengine/webprofilemanager.cpp b/src/webengine/webprofilemanager.cpp index 3f3d5ba..2fe6222 100644 --- a/src/webengine/webprofilemanager.cpp +++ b/src/webengine/webprofilemanager.cpp @@ -7,10 +7,10 @@ */ #include "webprofilemanager.h" +#include "configuration.h" #include "webprofile.h" #include #include -#include "configuration.h" WebProfileManager::WebProfileManager(QObject *parent) : QObject(parent) @@ -48,32 +48,34 @@ WebProfile *WebProfileManager::profile(const QString &id, const QString &path, b if(!path.isEmpty()) profile.settings = new QSettings(path, QSettings::IniFormat); + else + profile.settings = new QSettings; // QWebEngineCore cleans up profiles automatically, so no need to set parent profile.ptr = [id, isOffTheRecord, profile]() { - if(profile.value("otr", isOffTheRecord).toBool()) - return new WebProfile(/* name */ profile.value("name", id).toString(), /* parent */ nullptr); + if(profile.settings->value("otr", isOffTheRecord).toBool()) + return new WebProfile(/* name */ profile.settings->value("name", id).toString(), /* parent */ nullptr); else - return new WebProfile(/* storageName */ id, /* name */ profile.value("name", id).toString(), /* parent */ nullptr); + return new WebProfile(/* storageName */ id, /* name */ profile.settings->value("name", id).toString(), /* parent */ nullptr); }(); - if(profile.settings != nullptr) - profile.settings->setParent(profile.ptr); + + profile.settings->setParent(profile.ptr); connect(profile.ptr, &WebProfile::nameChanged, profile.settings, [profile](const QString &name) { profile.settings->setValue("name", name); }); - profile.ptr->setSearch(profile.value("search", conf.value("profile.search").value()).toString()); + profile.ptr->setSearch(profile.settings->value("search", conf.value("profile.search").value()).toString()); connect(profile.ptr, &WebProfile::searchChanged, profile.settings, [profile](const QString &url) { profile.settings->setValue("search", url); }); - profile.ptr->setHomepage(profile.value("homepage", conf.value("profile.homepage").value()).toUrl()); + profile.ptr->setHomepage(profile.settings->value("homepage", conf.value("profile.homepage").value()).toUrl()); connect(profile.ptr, &WebProfile::homepageChanged, profile.settings, [profile](const QUrl &url) { profile.settings->setValue("homepage", url); }); - profile.ptr->setNewtab(profile.value("newtab", conf.value("profile.newtab").value()).toUrl()); + profile.ptr->setNewtab(profile.settings->value("newtab", conf.value("profile.newtab").value()).toUrl()); connect(profile.ptr, &WebProfile::newtabChanged, profile.settings, [profile](const QUrl &url) { profile.settings->setValue("newtab", url); }); @@ -146,4 +148,3 @@ void WebProfileManager::profileMenu(QMenu *menu, const std::functionaddAction(action); } } - diff --git a/src/webengine/webprofilemanager.h b/src/webengine/webprofilemanager.h index 3e82936..2d9cd29 100644 --- a/src/webengine/webprofilemanager.h +++ b/src/webengine/webprofilemanager.h @@ -18,13 +18,6 @@ #include #include -struct ProfileDefault_t -{ - QString search; - QString homepage; - QString newtab; -}; - class WebProfileManager : public QObject { Q_OBJECT @@ -68,14 +61,6 @@ private: WebProfile *ptr = nullptr; QSettings *settings = nullptr; bool selfDestruct = false; - - QVariant value(const QString &key, const QVariant &defaultValue) const - { - if(settings == nullptr) - return defaultValue; - else - return settings->value(key, defaultValue); - } }; QMap profiles; diff --git a/test/cli/cli.cpp b/test/cli/cli.cpp new file mode 100644 index 0000000..322c365 --- /dev/null +++ b/test/cli/cli.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +static int numargs=0; + +/* Return the number of arguments of the application command line */ +static PyObject* emb_numargs(PyObject *self, PyObject *args) +{ + if(!PyArg_ParseTuple(args, ":numargs")) + return NULL; + + numargs = 19; + return PyLong_FromLong(numargs); +} + +static PyMethodDef EmbMethods[] = { + /* ml_name Name of the method + * ml_meth pointer to C implementation + * ml_flags flag bits indicating how it should be called + * ml_doc docstring + */ + {"numargs", emb_numargs, METH_VARARGS, "Return the number of arguments received by the process."}, + {NULL, NULL, 0, NULL} +}; + +static PyModuleDef EmbModule = { + /* m_base */ PyModuleDef_HEAD_INIT, // base module, always HEAD_INIT + /* m_name */ "emb", // module name + /* m_doc */ NULL, // Docstring for the module; usually a docstring variable created with PyDoc_STRVAR() + /* m_size */ -1, // + /* m_methods */ EmbMethods, // A pointer to a table of module-level functions + /* m_slots */ NULL, // An array of slot definitions for multi-phase initialization + /* traverse */ NULL, // A traversal function to call during GC traversal of the module object + /* clear */ NULL, // A clear function to call during GC clearing of the module object + /* free */ NULL // A function to call during deallocation of the module object +}; + +static PyObject* PyInit_emb(void) +{ + return PyModule_Create(&EmbModule); +} + +int main(int argc, char** argv) +{ + printf("cli test application\n"); + + wchar_t *program = Py_DecodeLocale(argv[0], NULL); + if (program == NULL) { + fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); + exit(1); + } + + // inform the interpreter about paths to run-time libraries + Py_SetProgramName(program); /* optional but recommended */ + + printf("import emb: %i\n", PyImport_AppendInittab("emb", &PyInit_emb)); + + // Initialize the python interpreter + Py_Initialize(); + + PyRun_SimpleString("print('Python interpreter ready')\n"); + + const char* prompt = "poi> "; + + while(true) { + char *cmd = linenoise(prompt); + + if(cmd == nullptr || *cmd == '\0') { + printf("breaking out of repl\n"); + free(cmd); + break; + } + + //printf("echo(%i):'%s'\n", strlen(cmd), cmd); + PyRun_SimpleString(cmd); + linenoiseHistoryAdd(cmd); + free(cmd); + } + + // finalize the interpreter + if (Py_FinalizeEx() < 0) { + exit(120); + } + + // + PyMem_RawFree(program); + + return 0; +} + diff --git a/test/cli/meson.build b/test/cli/meson.build new file mode 100644 index 0000000..298e1db --- /dev/null +++ b/test/cli/meson.build @@ -0,0 +1,4 @@ +cli_demo = executable('cli', install: false, dependencies: [ optional_deps ], + sources: [ 'cli.cpp' ] +) + -- cgit v1.2.1