aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/about/aboutdialog.ui5
-rw-r--r--src/about/aboutplugin.cpp60
-rw-r--r--src/about/aboutplugin.h3
-rw-r--r--src/about/aboutplugin.ui53
-rw-r--r--src/applicationmenu.cpp18
-rw-r--r--src/applicationmenu.h1
-rw-r--r--src/browser.cpp58
-rw-r--r--src/browser.h9
-rw-r--r--src/main.cpp14
-rw-r--r--src/webengine/meson.build2
-rw-r--r--src/webengine/test/profile.cpp145
-rw-r--r--src/webengine/webprofile.cpp18
-rw-r--r--src/webengine/webprofile.h62
-rw-r--r--src/webengine/webprofilemanager.cpp4
-rw-r--r--src/webengine/webprofilemanager.h2
15 files changed, 226 insertions, 228 deletions
diff --git a/src/about/aboutdialog.ui b/src/about/aboutdialog.ui
index 5b1c12a..036474c 100644
--- a/src/about/aboutdialog.ui
+++ b/src/about/aboutdialog.ui
@@ -134,11 +134,6 @@
</item>
</layout>
</widget>
- <widget class="QWidget" name="tab">
- <attribute name="title">
- <string>Plugins</string>
- </attribute>
- </widget>
</widget>
</item>
<item>
diff --git a/src/about/aboutplugin.cpp b/src/about/aboutplugin.cpp
index 92e55bb..a011b47 100644
--- a/src/about/aboutplugin.cpp
+++ b/src/about/aboutplugin.cpp
@@ -12,57 +12,12 @@
#include <QPluginLoader>
#include <QToolButton>
-QTreeWidgetItem *createItem(const QString &key, const QJsonValue &json, QTreeWidgetItem *parent)
-{
- auto *item = new QTreeWidgetItem(parent, { key, QLatin1String("---") });
-
- switch(json.type()) {
- case QJsonValue::Bool:
- item->setText(1, json.toBool() ? QLatin1String("true") : QLatin1String("false"));
- break;
-
- case QJsonValue::Double:
- item->setText(1, QString::number(json.toDouble()));
- break;
-
- case QJsonValue::String:
- item->setText(1, json.toString());
- break;
-
- case QJsonValue::Array:
- item->setText(1, QString());
- for(const auto &v : json.toArray()) {
- createItem(QString(), v, item);
- }
- break;
-
- case QJsonValue::Object:
- item->setText(1, QString());
- for(const QString &k : json.toObject().keys()) {
- createItem(k, json.toObject()[k], item);
- }
- break;
-
- case QJsonValue::Null:
- item->setText(1, QLatin1String("null"));
- break;
-
- case QJsonValue::Undefined:
- item->setText(1, QLatin1String("undefined"));
- break;
- }
-
- return item;
-}
-
AboutPluginDialog::AboutPluginDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::AboutPluginDialog)
{
setAttribute(Qt::WA_DeleteOnClose, true);
ui->setupUi(this);
-
-
}
AboutPluginDialog::~AboutPluginDialog()
@@ -85,20 +40,17 @@ void AboutPluginDialog::add(QPluginLoader *loader)
auto *enable_btn = new QToolButton(this);
enable_btn->setCheckable(true);
enable_btn->setChecked(loader->isLoaded());
+ enable_btn->setText(loader->isLoaded() ? "loaded" : "unloaded");
ui->tableWidget->setCellWidget(index, 4, enable_btn);
- connect(enable_btn, &QToolButton::clicked, this, [ loader, enable_btn ](bool checked) {
+ connect(enable_btn, &QToolButton::clicked, this, [this, loader, enable_btn](bool checked) {
const bool success = checked ? loader->load() : loader->unload();
if(!success) {
enable_btn->setChecked(!checked);
- //ui->error->setText(loader->errorString());
+ ui->msg->setText(loader->errorString());
+ } else {
+ ui->msg->setText(checked ? "Plugin successfully loaded" : "Plugin successfully unloaded");
+ enable_btn->setText(checked ? "loaded" : "unloaded");
}
});
-
- for(const QString &key : metadata.keys()) {
- auto *i = createItem(key, metadata.value(key), nullptr);
- if(i != nullptr) {
- ui->details_treeWidget->insertTopLevelItem(0, i);
- }
- }
}
diff --git a/src/about/aboutplugin.h b/src/about/aboutplugin.h
index b01cc78..f9cd3d4 100644
--- a/src/about/aboutplugin.h
+++ b/src/about/aboutplugin.h
@@ -24,6 +24,9 @@ public:
explicit AboutPluginDialog(QWidget *parent = nullptr);
~AboutPluginDialog() override;
+signals:
+ void loadPlugin(const QString &path);
+
public slots:
void add(QPluginLoader *loader);
diff --git a/src/about/aboutplugin.ui b/src/about/aboutplugin.ui
index d4291e5..5f44886 100644
--- a/src/about/aboutplugin.ui
+++ b/src/about/aboutplugin.ui
@@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
- <width>600</width>
+ <width>900</width>
<height>420</height>
</rect>
</property>
@@ -19,6 +19,9 @@
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
+ <attribute name="verticalHeaderVisible">
+ <bool>false</bool>
+ </attribute>
<column>
<property name="text">
<string>Name</string>
@@ -52,28 +55,32 @@
</widget>
</item>
<item>
- <widget class="QTreeWidget" name="details_treeWidget">
- <column>
- <property name="text">
- <string>Key</string>
- </property>
- </column>
- <column>
- <property name="text">
- <string>Value</string>
- </property>
- </column>
- </widget>
- </item>
- <item>
- <widget class="QDialogButtonBox" name="buttonBox">
- <property name="orientation">
- <enum>Qt::Horizontal</enum>
- </property>
- <property name="standardButtons">
- <set>QDialogButtonBox::Close</set>
- </property>
- </widget>
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <item>
+ <widget class="QToolButton" name="add_toolButton">
+ <property name="text">
+ <string>Add</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLabel" name="msg">
+ <property name="text">
+ <string>text</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QDialogButtonBox" name="buttonBox">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="standardButtons">
+ <set>QDialogButtonBox::Close</set>
+ </property>
+ </widget>
+ </item>
+ </layout>
</item>
</layout>
</widget>
diff --git a/src/applicationmenu.cpp b/src/applicationmenu.cpp
index b7acd57..dc888e7 100644
--- a/src/applicationmenu.cpp
+++ b/src/applicationmenu.cpp
@@ -13,10 +13,12 @@
#include "session/sessiondialog.h"
#include "smolbote/plugininterface.hpp"
#include <QPluginLoader>
+#include <spdlog/spdlog.h>
ApplicationMenu::ApplicationMenu(Browser *app, QWidget *parent)
: QMenu(parent)
{
+ m_app = app;
setTitle(qApp->applicationName());
Configuration conf;
@@ -57,11 +59,21 @@ void ApplicationMenu::addPlugin(QPluginLoader *plugin)
auto *action = addAction(metadata.value("name").toString());
action->setShortcut(QKeySequence::fromString(metadata.value("shortcut").toString()));
- connect(action, &QAction::triggered, this, [this, plugin]() {
+ connect(action, &QAction::triggered, [this, plugin]() {
if(plugin->isLoaded()) {
- if(const auto *interface = qobject_cast<PluginInterface *>(plugin->instance())) {
- interface->createWidget(this->parentWidget())->exec();
+ const auto *interface = qobject_cast<PluginInterface *>(plugin->instance());
+ auto *dlg = interface->createWidget(this->parentWidget());
+
+ if(auto *profileDlg = dynamic_cast<ProfileDialog*>(dlg)) {
+ WebProfileManager mgr;
+ mgr.walk([profileDlg](const QString &id, WebProfile *profile, QSettings *settings) {
+ profileDlg->addProfile(id, profile->name(), profile, settings);
+ });
}
+
+ dlg->exec();
+ } else {
+ spdlog::warn("Trying to run unloaded plugin!");
}
});
insertAction(bottom_pluginSeparator, action);
diff --git a/src/applicationmenu.h b/src/applicationmenu.h
index 7e1fe47..8b558e0 100644
--- a/src/applicationmenu.h
+++ b/src/applicationmenu.h
@@ -25,6 +25,7 @@ public slots:
void addPlugin(QPluginLoader *plugin);
private:
+ Browser *m_app = nullptr;
QAction *top_pluginSeparator = nullptr, *bottom_pluginSeparator = nullptr;
};
diff --git a/src/browser.cpp b/src/browser.cpp
index 2277e54..4f1e02e 100644
--- a/src/browser.cpp
+++ b/src/browser.cpp
@@ -92,10 +92,11 @@ void Browser::aboutPlugins()
dlg->exec();
}
-void Browser::loadPlugins(const QStringList &paths, const std::function<void(const QPluginLoader *)> &callback)
+bool Browser::loadPlugin(const QString &path)
{
- if(paths.isEmpty())
- return;
+ if(path.isEmpty()) {
+ return false;
+ }
Configuration conf;
const auto state = PluginLoader::signature_state(
@@ -103,23 +104,18 @@ void Browser::loadPlugins(const QStringList &paths, const std::function<void(con
conf.value<bool>("plugins.signature.checked").value(),
conf.value<bool>("plugins.signature.enforced").value());
- for(const auto &path : paths) {
- auto *loader = new PluginLoader(path, state, this);
- const bool loaded = loader->load();
-
- callback(loader);
-
- if(!loaded) {
- delete loader;
- } else {
- auto *info = new PluginInfo(loader);
- m_plugins.append(info);
+ auto *loader = new PluginLoader(path, state, this);
+ const bool loaded = loader->load();
- for(MainWindow *window : qAsConst(m_windows)) {
- addPluginTo(info, window);
- }
- }
+ if(!loaded) {
+ delete loader;
+ return false;
}
+ auto *info = new PluginInfo(loader);
+ m_plugins.append(info);
+
+ emit pluginAdded(loader);
+ return true;
}
void Browser::setup()
@@ -205,6 +201,7 @@ void Browser::open(const QVector<Session::MainWindow> &data, bool merge)
for(const auto &windowData : data) {
auto *menu = new ApplicationMenu(this);
+ connect(this, &Browser::pluginAdded, menu, &ApplicationMenu::addPlugin);
for(auto *info : qAsConst(m_plugins)) {
menu->addPlugin(info->loader);
}
@@ -219,28 +216,3 @@ void Browser::open(const QVector<Session::MainWindow> &data, bool merge)
});
}
}
-
-void Browser::addPluginTo(PluginInfo *info, MainWindow *window)
-{
- QPluginLoader *loader = info->loader;
- auto *pluginMenu = new QMenu(loader->metaData().value("MetaData").toObject().value("name").toString());
- //window->m_menuBar->insertPlugin(pluginMenu);
- info->menus.append(pluginMenu);
-
- auto *runAction = pluginMenu->addAction(tr("Run"));
- runAction->setShortcut(QKeySequence::fromString(loader->metaData().value("MetaData").toObject().value("shortcut").toString()));
-
- connect(runAction, &QAction::triggered, window, [loader, window]() {
- if(loader->isLoaded()) {
- const auto *interface = qobject_cast<PluginInterface *>(loader->instance());
- Q_CHECK_PTR(interface);
- interface->createWidget(window)->exec();
- }
- });
-
- auto *removeAction = pluginMenu->addAction(tr("Remove"));
- connect(removeAction, &QAction::triggered, this, [this, info]() {
- m_plugins.removeOne(info);
- delete info;
- });
-}
diff --git a/src/browser.h b/src/browser.h
index 74136f1..1964267 100644
--- a/src/browser.h
+++ b/src/browser.h
@@ -31,8 +31,6 @@ public:
explicit Browser(int &argc, char *argv[], bool allowSecondary = true);
~Browser() final;
- void loadPlugins(
- const QStringList &paths, const std::function<void(const QPluginLoader *)> &callback = [](const auto) {});
void setup();
const QVector<MainWindow *> windows() const
@@ -49,12 +47,18 @@ public:
return m_downloads.get();
}
+signals:
+ void pluginAdded(QPluginLoader *);
+
public slots:
void about();
void aboutPlugins();
+
void showWidget(QWidget *widget, MainWindow *where) const;
void open(const QVector<Session::MainWindow> &data, bool merge = true);
+ bool loadPlugin(const QString &path);
+
private:
struct PluginInfo {
explicit PluginInfo(QPluginLoader *l)
@@ -71,7 +75,6 @@ private:
QPluginLoader *loader;
QVector<QMenu *> menus;
};
- void addPluginTo(PluginInfo *info, MainWindow *window);
Q_DISABLE_COPY(Browser)
diff --git a/src/main.cpp b/src/main.cpp
index ca85b65..f0c1591 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -118,13 +118,13 @@ int main(int argc, char **argv)
}
// load plugins
- app.loadPlugins(Util::files(conf.value<QString>("plugins.path").value(), { "*.so", "*.dll" }),
- [](const auto *loader) {
- if(loader->isLoaded())
- spdlog::info("Loaded plugin [{}]", qUtf8Printable(loader->fileName()));
- else
- spdlog::warn("Loading plugin [{}] failed: {}", qUtf8Printable(loader->fileName()), qUtf8Printable(loader->errorString()));
- });
+ for(const QString &path : Util::files(conf.value<QString>("plugins.path").value(), { "*.so", "*.dll" })) {
+ if(app.loadPlugin(path)) {
+ spdlog::debug("Loaded plugin [{}]", qUtf8Printable(path));
+ } else {
+ spdlog::warn("Failed loading plugin [{}]", qUtf8Printable(path));
+ }
+ }
}
const auto profile = []() {
diff --git a/src/webengine/meson.build b/src/webengine/meson.build
index db7bdc5..47e4adf 100644
--- a/src/webengine/meson.build
+++ b/src/webengine/meson.build
@@ -5,7 +5,7 @@ webengine_moc = mod_qt5.preprocess(
dep_webengine = declare_dependency(
include_directories: [ '.', smolbote_include ],
- link_with: static_library('webengine', dependencies: dep_qt5,
+ link_with: static_library('webengine', dependencies: [ dep_qt5, dep_spdlog ],
include_directories: smolbote_include,
sources: [ 'webprofile.cpp', 'urlinterceptor.cpp', 'webprofilemanager.cpp', 'webpage.cpp', 'webview.cpp', 'webviewcontextmenu.cpp', webengine_moc ])
)
diff --git a/src/webengine/test/profile.cpp b/src/webengine/test/profile.cpp
index 7351f66..ae3a4e3 100644
--- a/src/webengine/test/profile.cpp
+++ b/src/webengine/test/profile.cpp
@@ -22,98 +22,99 @@ TEST_CASE("loading profile settings")
delete settings;
}
-SCENARIO("loading individual profiles")
+SCENARIO("profile properties")
{
- GIVEN("no profile preset")
- {
- const QString search = GENERATE(as<QString>{}, "https://search.url/t=%1", "https://duckduckgo.com/?q=%1&ia=web", "aaabbbccc");
- const QUrl homepage = GENERATE(as<QUrl>{}, "https://homepage.net", "about:blank", "aaabbbccc");
- const QUrl newtab = GENERATE(as<QUrl>{}, "https://newtab.net", "about:blank", "aaabbbccc");
+ const QString search{ "about:blank" };
+ const QUrl homepage{ "about:blank" };
+ const QUrl newtab{ "about:blank" };
+ const QString id{ "id" };
+
+ REQUIRE(qEnvironmentVariableIsSet("PROFILE"));
- const QString id{ "id" };
- auto *settings = WebProfile::load(QString(), search, homepage, newtab);
- auto *profile = WebProfile::load(id, settings, true);
+ // create an empty settings object
+ const QString settings_path = GENERATE(as<QString>{}, QString(), qgetenv("PROFILE"));
+ auto *settings = WebProfile::load(settings_path, search, homepage, newtab);
+ // create the actual profile
+ auto *profile = WebProfile::load(id, settings, true);
- REQUIRE(profile != nullptr);
+ REQUIRE(profile != nullptr);
+ REQUIRE(profile->isOffTheRecord());
+
+ WHEN("id constant")
+ {
REQUIRE(profile->getId() == id);
REQUIRE(profile->property("id").toString() == id);
- REQUIRE(profile->name() == id);
- REQUIRE(profile->search() == search);
- REQUIRE(profile->homepage() == homepage);
- REQUIRE(profile->newtab() == newtab);
-
- REQUIRE(profile->isOffTheRecord());
- delete settings;
- delete profile;
}
- GIVEN("an off-the-record profile preset")
+ WHEN("changing profile name")
{
- REQUIRE(qEnvironmentVariableIsSet("PROFILE"));
-
- const QString id{ "id" };
- auto *settings = WebProfile::load(qgetenv("PROFILE"), QString(), QUrl(), QUrl());
- auto *profile = WebProfile::load(id, settings, true);
-
- REQUIRE(profile != nullptr);
- REQUIRE(profile->getId() == id);
- REQUIRE(profile->isOffTheRecord());
-
- WHEN("created")
+ const QString name = GENERATE(as<QString>{}, "a", "bb", "ccc");
+ profile->setName(name);
+ THEN("the name changes")
{
- THEN("uses default values")
- {
- REQUIRE(profile->name() == "Test Profile");
- REQUIRE(profile->search() == "https://duckduckgo.com/?q=%1&ia=web");
- REQUIRE(profile->homepage() == QUrl("about:blank"));
- REQUIRE(profile->newtab() == QUrl("about:blank"));
- }
+ REQUIRE(profile->name() == name);
+ REQUIRE(settings->value("name").toString() == name);
}
-
- WHEN("changing profile name")
+ }
+ WHEN("changing search")
+ {
+ const QString search = GENERATE(as<QString>{}, "a", "bb", "ccc");
+ profile->setSearch(search);
+ THEN("the search url changes")
{
- const QString name = GENERATE(as<QString>{}, "a", "bb", "ccc");
- profile->setName(name);
- THEN("the name changes")
- {
- REQUIRE(profile->name() == name);
- REQUIRE(settings->value("name").toString() == name);
- }
+ REQUIRE(profile->search() == search);
+ REQUIRE(settings->value("search").toString() == search);
}
- WHEN("changing search url")
+ }
+ WHEN("changing homepage url")
+ {
+ const QUrl url = GENERATE(as<QUrl>{}, "a", "bb", "ccc");
+ profile->setHomepage(url);
+ THEN("homepage changes")
{
- const QString search = GENERATE(as<QString>{}, "a", "bb", "ccc");
- profile->setSearch(search);
- THEN("the search url changes")
- {
- REQUIRE(profile->search() == search);
- REQUIRE(settings->value("search").toString() == search);
- }
+ REQUIRE(profile->homepage() == url);
+ REQUIRE(settings->value("homepage").toUrl() == url);
}
- WHEN("changing homepage")
+ }
+ WHEN("changing newtab url")
+ {
+ const QUrl url = GENERATE(as<QUrl>{}, "a", "bb", "ccc");
+ profile->setNewtab(url);
+ THEN("newtab changes")
{
- const QUrl url = GENERATE(as<QUrl>{}, "a", "bb", "ccc");
- profile->setHomepage(url);
- THEN("homepage changes")
- {
- REQUIRE(profile->homepage() == url);
- REQUIRE(settings->value("homepage").toUrl() == url);
- }
+ REQUIRE(profile->newtab() == url);
+ REQUIRE(settings->value("newtab").toUrl() == url);
}
- WHEN("changing newtab")
+ }
+
+ WHEN("changing cookies")
+ {
+ auto list = profile->cookies();
+ REQUIRE(list.isEmpty());
+ list.append(QNetworkCookie("name", "value").toRawForm());
+ profile->setCookies(list);
+ THEN("new cookie list gets applied")
{
- const QUrl url = GENERATE(as<QUrl>{}, "a", "bb", "ccc");
- profile->setNewtab(url);
- THEN("newtab changes")
- {
- REQUIRE(profile->newtab() == url);
- REQUIRE(settings->value("newtab").toUrl() == url);
- }
+ // There is no event loop, so the signals cannot fire and update the cookie list
+ //REQUIRE(list == profile->cookies());
}
+ }
- delete settings;
- delete profile;
+ WHEN("changing headers")
+ {
+ QMap<QString, QVariant> headers;
+ headers.insert("Dnt", "1");
+ headers.insert("unknown", "pair");
+
+ profile->setHeaders(headers);
+ THEN("headers change")
+ {
+ REQUIRE(profile->headers() == headers);
+ }
}
+
+ delete settings;
+ delete profile;
}
int main(int argc, char **argv)
diff --git a/src/webengine/webprofile.cpp b/src/webengine/webprofile.cpp
index f0368c9..719ab34 100644
--- a/src/webengine/webprofile.cpp
+++ b/src/webengine/webprofile.cpp
@@ -7,11 +7,12 @@
*/
#include "webprofile.h"
+#include "urlinterceptor.h"
#include <QFileInfo>
#include <QSettings>
#include <QWebEngineCookieStore>
#include <QWebEngineSettings>
-#include "urlinterceptor.h"
+#include <spdlog/spdlog.h>
static WebProfile *s_profile = nullptr;
@@ -47,7 +48,7 @@ WebProfile *WebProfile::load(const QString &id, QSettings *settings, bool isOffT
{
WebProfile *profile = nullptr;
- if(isOffTheRecord || settings->value("otr", isOffTheRecord).toBool()) {
+ if(settings->value("otr", isOffTheRecord).toBool()) {
profile = new WebProfile(id, nullptr);
} else {
profile = new WebProfile(id, id, nullptr);
@@ -89,7 +90,8 @@ WebProfile *WebProfile::load(const QString &id, QSettings *settings, bool isOffT
{
// headers
settings->beginGroup("headers");
- for(const QString &key : settings->childKeys()) {
+ const auto keys = settings->childKeys();
+ for(const QString &key : keys) {
profile->setHttpHeader(key.toLatin1(), settings->value(key).toString().toLatin1());
}
settings->endGroup();
@@ -110,11 +112,14 @@ WebProfile::WebProfile(const QString &id, QObject *parent)
{
QWebEngineProfile::setUrlRequestInterceptor(new UrlRequestInterceptor(this));
connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) {
+ spdlog::debug("[{}]: +cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
m_cookies.append(cookie);
});
connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) {
- m_cookies.removeAll(cookie);
+ spdlog::debug("[{}]: -cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
+ m_cookies.removeOne(cookie);
});
+ cookieStore()->loadAllCookies();
}
// default constructor
@@ -124,9 +129,12 @@ WebProfile::WebProfile(const QString &id, const QString &storageName, QObject *p
{
QWebEngineProfile::setUrlRequestInterceptor(new UrlRequestInterceptor(this));
connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) {
+ spdlog::debug("[{}]: +cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
m_cookies.append(cookie);
});
connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) {
- m_cookies.removeAll(cookie);
+ spdlog::debug("[{}]: -cookie {}", qUtf8Printable(m_name), qUtf8Printable(cookie.name()));
+ m_cookies.removeOne(cookie);
});
+ cookieStore()->loadAllCookies();
}
diff --git a/src/webengine/webprofile.h b/src/webengine/webprofile.h
index 180cc9d..0747638 100644
--- a/src/webengine/webprofile.h
+++ b/src/webengine/webprofile.h
@@ -11,13 +11,14 @@
#include <QMap>
#include <QNetworkCookie>
+#include <QSettings>
#include <QString>
#include <QUrl>
+#include <QVariant>
#include <QVector>
+#include <QWebEngineCookieStore>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
-#include <QVariant>
-#include <QSettings>
class UrlRequestInterceptor;
class WebProfile : public QWebEngineProfile
@@ -44,6 +45,10 @@ class WebProfile : public QWebEngineProfile
Q_PROPERTY(bool spellCheckEnabled READ isSpellCheckEnabled WRITE setSpellCheckEnabled NOTIFY propertyChanged)
+ // more custom properties
+ Q_PROPERTY(QList<QVariant> cookies READ cookies WRITE setCookies NOTIFY cookiesChanged)
+ Q_PROPERTY(QMap<QString, QVariant> headers READ headers WRITE setHeaders NOTIFY headersChanged)
+
signals:
void nameChanged(const QString &name);
void searchChanged(const QString &url);
@@ -52,6 +57,9 @@ signals:
void propertyChanged(const QString &name, const QVariant &value);
void attributeChanged(QWebEngineSettings::WebAttribute attribute, bool value);
+
+ void cookiesChanged();
+ void headersChanged();
void headerChanged(const QString &name, const QString &value);
void headerRemoved(const QString &name);
@@ -60,9 +68,9 @@ public:
[[nodiscard]] static WebProfile *load(const QString &id, QSettings *settings, bool isOffTheRecord = true);
WebProfile(const WebProfile &) = delete;
- WebProfile& operator=(const WebProfile &) = delete;
+ WebProfile &operator=(const WebProfile &) = delete;
WebProfile(WebProfile &&) = delete;
- WebProfile& operator=(WebProfile &&) = delete;
+ WebProfile &operator=(WebProfile &&) = delete;
static WebProfile *defaultProfile();
static void setDefaultProfile(WebProfile *profile);
@@ -83,9 +91,13 @@ public:
emit nameChanged(name);
}
- [[nodiscard]] QVector<QNetworkCookie> cookies() const
+ [[nodiscard]] QList<QVariant> cookies() const
{
- return qAsConst(m_cookies);
+ QList<QVariant> r;
+ for(const auto &cookie : m_cookies) {
+ r.append(cookie.toRawForm());
+ }
+ return r;
}
[[nodiscard]] QString search() const
@@ -166,7 +178,16 @@ public:
emit headerRemoved(name);
}
}
-
+ [[nodiscard]] QMap<QString, QVariant> headers() const
+ {
+ QMap<QString, QVariant> r;
+ auto it = m_headers.constBegin();
+ while(it != m_headers.constEnd()) {
+ r.insert(QString(it.key()), QVariant(it.value()));
+ ++it;
+ }
+ return r;
+ }
void setSpellCheckEnabled(bool enable)
{
QWebEngineProfile::setSpellCheckEnabled(enable);
@@ -178,6 +199,29 @@ public:
m_filters.append(interceptor);
}
+public slots:
+ void setCookies(const QList<QVariant> &cookies)
+ {
+ auto *store = cookieStore();
+ store->deleteAllCookies();
+ for(const auto &data : cookies) {
+ for(const auto &cookie : QNetworkCookie::parseCookies(data.toByteArray())) {
+ store->setCookie(cookie);
+ }
+ }
+ emit cookiesChanged();
+ }
+ void setHeaders(const QMap<QString, QVariant> &headers)
+ {
+ m_headers.clear();
+ auto it = headers.constBegin();
+ while(it != headers.constEnd()) {
+ m_headers.insert(it.key().toLatin1(), it.value().toByteArray());
+ ++it;
+ }
+ emit headersChanged();
+ }
+
protected:
// off-the-record constructor
explicit WebProfile(const QString &id, QObject *parent = nullptr);
@@ -190,8 +234,8 @@ protected:
QUrl m_homepage = QUrl("about:blank");
QUrl m_newtab = QUrl("about:blank");
- QVector<QWebEngineUrlRequestInterceptor*> m_filters;
- QVector<QNetworkCookie> m_cookies;
+ QVector<QWebEngineUrlRequestInterceptor *> m_filters;
+ QList<QNetworkCookie> m_cookies;
QMap<QByteArray, QByteArray> m_headers;
};
diff --git a/src/webengine/webprofilemanager.cpp b/src/webengine/webprofilemanager.cpp
index 785251b..5cc83f8 100644
--- a/src/webengine/webprofilemanager.cpp
+++ b/src/webengine/webprofilemanager.cpp
@@ -56,7 +56,7 @@ QStringList WebProfileManager<true>::idList() const
}
template <>
-void WebProfileManager<false>::walk(std::function<void(const QString &id, WebProfile *profile, const QSettings *settings)> f) const
+void WebProfileManager<false>::walk(std::function<void(const QString &id, WebProfile *profile, QSettings *settings)> f) const
{
for(auto iter = profiles.begin(); iter != profiles.end(); ++iter) {
f(iter.key(), iter.value().ptr, iter.value().settings);
@@ -64,7 +64,7 @@ void WebProfileManager<false>::walk(std::function<void(const QString &id, WebPro
}
template <>
-void WebProfileManager<true>::walk(std::function<void(const QString &id, WebProfile *profile, const QSettings *settings)> f) const
+void WebProfileManager<true>::walk(std::function<void(const QString &id, WebProfile *profile, QSettings *settings)> f) const
{
s_instance->walk(f);
}
diff --git a/src/webengine/webprofilemanager.h b/src/webengine/webprofilemanager.h
index 22fb31c..91dcaf8 100644
--- a/src/webengine/webprofilemanager.h
+++ b/src/webengine/webprofilemanager.h
@@ -93,7 +93,7 @@ public:
return profiles.keys();
}
- callable_when(unconsumed) void walk(std::function<void(const QString &id, WebProfile *profile, const QSettings *settings)>) const;
+ callable_when(unconsumed) void walk(std::function<void(const QString &id, WebProfile *profile, QSettings *settings)>) const;
callable_when(unconsumed) void make_global();