From 0492a063806b6d63e4f378908b809de104a24820 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 25 Apr 2020 22:09:13 +0300 Subject: Update ProfileEditor plugin ProfileEditor: - add tests - disable read-only settings on otr profiles Add WebProfile::setHeaders and WebProfile::setCookies --- include/smolbote/plugininterface.hpp | 18 +- meson.build | 3 +- plugins/ProfileEditor/ProfileEditor.json | 5 +- .../ProfileEditor/forms/newhttpheaderdialog.cpp | 32 -- plugins/ProfileEditor/forms/newhttpheaderdialog.h | 33 -- plugins/ProfileEditor/forms/newhttpheaderdialog.ui | 84 ---- plugins/ProfileEditor/forms/newprofiledialog.cpp | 32 -- plugins/ProfileEditor/forms/newprofiledialog.h | 33 -- plugins/ProfileEditor/forms/newprofiledialog.ui | 81 ---- .../ProfileEditor/forms/profilemanagerdialog.cpp | 30 +- plugins/ProfileEditor/forms/profilemanagerdialog.h | 18 +- .../ProfileEditor/forms/profilemanagerdialog.ui | 24 +- plugins/ProfileEditor/forms/profileview.cpp | 153 ++---- plugins/ProfileEditor/forms/profileview.h | 12 +- plugins/ProfileEditor/forms/profileview.ui | 535 ++++++++++----------- plugins/ProfileEditor/forms/settingstable.cpp | 90 ++++ plugins/ProfileEditor/forms/settingstable.h | 31 ++ plugins/ProfileEditor/meson.build | 17 +- plugins/ProfileEditor/profileeditorplugin.cpp | 36 -- plugins/ProfileEditor/profileeditorplugin.h | 7 +- plugins/ProfileEditor/test/main.cpp | 22 + src/about/aboutdialog.ui | 5 - src/about/aboutplugin.cpp | 60 +-- src/about/aboutplugin.h | 3 + src/about/aboutplugin.ui | 53 +- src/applicationmenu.cpp | 18 +- src/applicationmenu.h | 1 + src/browser.cpp | 58 +-- src/browser.h | 9 +- src/main.cpp | 14 +- src/webengine/meson.build | 2 +- src/webengine/test/profile.cpp | 145 +++--- src/webengine/webprofile.cpp | 18 +- src/webengine/webprofile.h | 62 ++- src/webengine/webprofilemanager.cpp | 4 +- src/webengine/webprofilemanager.h | 2 +- 36 files changed, 757 insertions(+), 993 deletions(-) delete mode 100644 plugins/ProfileEditor/forms/newhttpheaderdialog.cpp delete mode 100644 plugins/ProfileEditor/forms/newhttpheaderdialog.h delete mode 100644 plugins/ProfileEditor/forms/newhttpheaderdialog.ui delete mode 100644 plugins/ProfileEditor/forms/newprofiledialog.cpp delete mode 100644 plugins/ProfileEditor/forms/newprofiledialog.h delete mode 100644 plugins/ProfileEditor/forms/newprofiledialog.ui create mode 100644 plugins/ProfileEditor/forms/settingstable.cpp create mode 100644 plugins/ProfileEditor/forms/settingstable.h create mode 100644 plugins/ProfileEditor/test/main.cpp diff --git a/include/smolbote/plugininterface.hpp b/include/smolbote/plugininterface.hpp index 5fa4530..328cba5 100644 --- a/include/smolbote/plugininterface.hpp +++ b/include/smolbote/plugininterface.hpp @@ -9,14 +9,28 @@ #ifndef SMOLBOTE_PLUGIN_INTERFACE_HPP #define SMOLBOTE_PLUGIN_INTERFACE_HPP +#include #include +#include + +class QWebEngineProfile; +class ProfileDialog : public QDialog +{ +public: + ProfileDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) + : QDialog(parent, f) + { + } + virtual ~ProfileDialog() = default; + virtual int addProfile(const QString &id, const QString name, QWebEngineProfile *profile, QSettings *settings) = 0; + virtual void showProfile(int index) = 0; +}; -class QDialog; class PluginInterface { public: virtual ~PluginInterface() = default; - virtual QDialog *createWidget(QWidget *parent = nullptr) const = 0; + virtual QDialog *createWidget(QWidget *parent) const = 0; }; #define PluginInterfaceIid "net.iserlohn-fortress.smolbote.PluginInterface" diff --git a/meson.build b/meson.build index 3cf7105..c180449 100644 --- a/meson.build +++ b/meson.build @@ -91,8 +91,7 @@ subdir('lang') subdir('doc') subdir('tools') -#subdir('plugins/ConfigurationEditor') -#subdir('plugins/ProfileEditor') +subdir('plugins/ProfileEditor') subdir('test/firefox-bookmarks-json-parser') diff --git a/plugins/ProfileEditor/ProfileEditor.json b/plugins/ProfileEditor/ProfileEditor.json index 8381e9a..afa0a1f 100644 --- a/plugins/ProfileEditor/ProfileEditor.json +++ b/plugins/ProfileEditor/ProfileEditor.json @@ -1,5 +1,6 @@ { "name": "Profile Editor", - "author": "Aqua-sama", - "shortcut": "Ctrl+Shift+E" + "author": "aqua@", + "shortcut": "Ctrl+Shift+E", + "license": "GPL3" } diff --git a/plugins/ProfileEditor/forms/newhttpheaderdialog.cpp b/plugins/ProfileEditor/forms/newhttpheaderdialog.cpp deleted file mode 100644 index 3978c4e..0000000 --- a/plugins/ProfileEditor/forms/newhttpheaderdialog.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "newhttpheaderdialog.h" -#include "ui_newhttpheaderdialog.h" - -NewHttpHeaderDialog::NewHttpHeaderDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::NewHttpHeaderDialog) -{ - ui->setupUi(this); -} - -NewHttpHeaderDialog::~NewHttpHeaderDialog() -{ - delete ui; -} - -QString NewHttpHeaderDialog::header() const -{ - return ui->header->text(); -} - -QString NewHttpHeaderDialog::value() const -{ - return ui->value->text(); -} diff --git a/plugins/ProfileEditor/forms/newhttpheaderdialog.h b/plugins/ProfileEditor/forms/newhttpheaderdialog.h deleted file mode 100644 index 53a2a80..0000000 --- a/plugins/ProfileEditor/forms/newhttpheaderdialog.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef NEWHTTPHEADERDIALOG_H -#define NEWHTTPHEADERDIALOG_H - -#include - -namespace Ui { -class NewHttpHeaderDialog; -} - -class NewHttpHeaderDialog : public QDialog -{ - Q_OBJECT - -public: - explicit NewHttpHeaderDialog(QWidget *parent = nullptr); - ~NewHttpHeaderDialog(); - - QString header() const; - QString value() const; - -private: - Ui::NewHttpHeaderDialog *ui; -}; - -#endif // NEWHTTPHEADERDIALOG_H diff --git a/plugins/ProfileEditor/forms/newhttpheaderdialog.ui b/plugins/ProfileEditor/forms/newhttpheaderdialog.ui deleted file mode 100644 index a457ba6..0000000 --- a/plugins/ProfileEditor/forms/newhttpheaderdialog.ui +++ /dev/null @@ -1,84 +0,0 @@ - - - NewHttpHeaderDialog - - - - 0 - 0 - 320 - 108 - - - - Dialog - - - - - - Header - - - - - - - - - - Value - - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - buttonBox - accepted() - NewHttpHeaderDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - NewHttpHeaderDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/plugins/ProfileEditor/forms/newprofiledialog.cpp b/plugins/ProfileEditor/forms/newprofiledialog.cpp deleted file mode 100644 index 3ed61d8..0000000 --- a/plugins/ProfileEditor/forms/newprofiledialog.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "newprofiledialog.h" -#include "ui_newprofiledialog.h" - -NewProfileDialog::NewProfileDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::NewProfileDialog) -{ - ui->setupUi(this); -} - -NewProfileDialog::~NewProfileDialog() -{ - delete ui; -} - -const QString NewProfileDialog::getId() const -{ - return ui->id_lineEdit->text(); -} - -bool NewProfileDialog::getOtr() const -{ - return ui->offTheRecord->isChecked(); -} diff --git a/plugins/ProfileEditor/forms/newprofiledialog.h b/plugins/ProfileEditor/forms/newprofiledialog.h deleted file mode 100644 index dd9aa4e..0000000 --- a/plugins/ProfileEditor/forms/newprofiledialog.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef NEWPROFILEDIALOG_H -#define NEWPROFILEDIALOG_H - -#include - -namespace Ui { -class NewProfileDialog; -} - -class NewProfileDialog : public QDialog -{ - Q_OBJECT - -public: - explicit NewProfileDialog(QWidget *parent = nullptr); - ~NewProfileDialog(); - - const QString getId() const; - bool getOtr() const; - -private: - Ui::NewProfileDialog *ui; -}; - -#endif // NEWPROFILEDIALOG_H diff --git a/plugins/ProfileEditor/forms/newprofiledialog.ui b/plugins/ProfileEditor/forms/newprofiledialog.ui deleted file mode 100644 index 8b4888d..0000000 --- a/plugins/ProfileEditor/forms/newprofiledialog.ui +++ /dev/null @@ -1,81 +0,0 @@ - - - NewProfileDialog - - - - 0 - 0 - 320 - 112 - - - - New Profile - - - - - - ID - - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - Off the record - - - - - - - - - buttonBox - accepted() - NewProfileDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - NewProfileDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/plugins/ProfileEditor/forms/profilemanagerdialog.cpp b/plugins/ProfileEditor/forms/profilemanagerdialog.cpp index f5a17b1..6cbc806 100644 --- a/plugins/ProfileEditor/forms/profilemanagerdialog.cpp +++ b/plugins/ProfileEditor/forms/profilemanagerdialog.cpp @@ -10,15 +10,14 @@ #include "profileview.h" #include #include +#include "ui_profilemanagerdialog.h" ProfileManagerDialog::ProfileManagerDialog(QWidget *parent) - : QDialog(parent) + : ProfileDialog(parent) , ui(new Ui::ProfileManagerDialog) { ui->setupUi(this); - - connect(ui->listWidget, &QListWidget::itemPressed, this, &ProfileManagerDialog::showProfile); - showProfile(nullptr); + connect(ui->listWidget, &QListWidget::itemPressed, this, &ProfileManagerDialog::showItem); } ProfileManagerDialog::~ProfileManagerDialog() @@ -26,17 +25,26 @@ ProfileManagerDialog::~ProfileManagerDialog() delete ui; } -void ProfileManagerDialog::addProfile(const QString &id, Profile *profile) +int ProfileManagerDialog::addProfile(const QString &id, const QString name, QWebEngineProfile *profile, QSettings *settings) { Q_CHECK_PTR(profile); auto *item = new QListWidgetItem(id, ui->listWidget); - auto pointer = QPointer(profile); - item->setData(Qt::UserRole, QVariant::fromValue(pointer)); + item->setData(Qt::UserRole, name); + item->setData(Qt::UserRole+1, QVariant::fromValue(QPointer(profile))); + item->setData(Qt::UserRole+2, QVariant::fromValue(QPointer(settings))); + + return ui->listWidget->count() - 1; +} + +void ProfileManagerDialog::showProfile(int index) +{ + auto *item = ui->listWidget->item(index); + showItem(item); } -void ProfileManagerDialog::showProfile(QListWidgetItem *item) +void ProfileManagerDialog::showItem(QListWidgetItem *item) { // clear out groupbox layout QLayoutItem *i; @@ -51,8 +59,10 @@ void ProfileManagerDialog::showProfile(QListWidgetItem *item) } ui->groupBox->setVisible(true); - auto profile = item->data(Qt::UserRole).value>(); - auto *v = new ProfileView(profile.data(), this); + const auto name = item->data(Qt::UserRole).value(); + auto profile = item->data(Qt::UserRole+1).value>(); + auto settings = item->data(Qt::UserRole+2).value>(); + auto *v = new ProfileView(name, profile.data(), settings.data(), this); ui->groupBox->layout()->addWidget(v); v->adjustSize(); } diff --git a/plugins/ProfileEditor/forms/profilemanagerdialog.h b/plugins/ProfileEditor/forms/profilemanagerdialog.h index fe91948..8dd307e 100644 --- a/plugins/ProfileEditor/forms/profilemanagerdialog.h +++ b/plugins/ProfileEditor/forms/profilemanagerdialog.h @@ -11,12 +11,15 @@ #include #include -#include -#include "ui_profilemanagerdialog.h" +#include +#include + +namespace Ui { + class ProfileManagerDialog; +} -class WebProfile; class QListWidgetItem; -class ProfileManagerDialog : public QDialog +class ProfileManagerDialog : public ProfileDialog { Q_OBJECT @@ -27,10 +30,9 @@ public: ~ProfileManagerDialog() override; public slots: - void addProfile(const QString &id, Profile *profile); - -private slots: - void showProfile(QListWidgetItem *item); + int addProfile(const QString &id, const QString name, QWebEngineProfile *profile, QSettings *settings) override; + void showProfile(int index) override; + void showItem(QListWidgetItem *item); private: Ui::ProfileManagerDialog *ui; diff --git a/plugins/ProfileEditor/forms/profilemanagerdialog.ui b/plugins/ProfileEditor/forms/profilemanagerdialog.ui index f41b1b0..b715b86 100644 --- a/plugins/ProfileEditor/forms/profilemanagerdialog.ui +++ b/plugins/ProfileEditor/forms/profilemanagerdialog.ui @@ -6,8 +6,8 @@ 0 0 - 400 - 300 + 918 + 600 @@ -34,6 +34,12 @@ 0 + + + 640 + 0 + + @@ -41,20 +47,6 @@ - - - - New - - - - - - - Delete - - - diff --git a/plugins/ProfileEditor/forms/profileview.cpp b/plugins/ProfileEditor/forms/profileview.cpp index 9a19cfc..992364c 100644 --- a/plugins/ProfileEditor/forms/profileview.cpp +++ b/plugins/ProfileEditor/forms/profileview.cpp @@ -1,29 +1,26 @@ /* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * location: https://neueland.iserlohn-fortress.net/cgit/smolbote * * SPDX-License-Identifier: GPL-3.0 */ #include "profileview.h" #include "ui_profileview.h" -#include -#include -#include #include -#include "newhttpheaderdialog.h" +#include +#include -inline void connectSetting(QCheckBox *checkBox, Profile *profile, QWebEngineSettings::WebAttribute attr) +inline void connectSetting(QCheckBox *checkBox, QWebEngineProfile *profile, QWebEngineSettings::WebAttribute attr) { checkBox->setChecked(profile->settings()->testAttribute(attr)); QObject::connect(checkBox, &QCheckBox::clicked, [profile, attr](bool checked) { profile->settings()->setAttribute(attr, checked); - emit profile->attributeChanged(attr, checked); }); } -ProfileView::ProfileView(Profile *profile, QWidget *parent) +ProfileView::ProfileView(const QString &name, QWebEngineProfile *profile, QSettings *settings, QWidget *parent) : QWidget(parent) , ui(new Ui::ProfileView) { @@ -32,52 +29,37 @@ ProfileView::ProfileView(Profile *profile, QWidget *parent) ui->setupUi(this); // general tab - ui->name->setText(profile->name()); - connect(ui->name, &QLineEdit::editingFinished, profile, [=]() { - profile->setName(ui->name->text()); - }); - + ui->name->setText(name); ui->offTheRecord->setChecked(profile->isOffTheRecord()); - ui->homepage->setText(profile->homepage().toString()); - connect(ui->homepage, &QLineEdit::editingFinished, profile, [=]() { - profile->setHomepage(QUrl::fromUserInput(ui->homepage->text())); - }); - - ui->newtab->setText(profile->newtab().toString()); - connect(ui->newtab, &QLineEdit::editingFinished, profile, [=]() { - profile->setNewtab(QUrl::fromUserInput(ui->newtab->text())); - }); - - ui->search->setText(profile->search()); - connect(ui->search, &QLineEdit::editingFinished, profile, [=]() { - profile->setSearch(ui->search->text()); - }); + ui->settings_table->connect(settings); // http tab ui->userAgent->setPlainText(m_profile->httpUserAgent()); - connect(ui->userAgent, &QPlainTextEdit::textChanged, profile, [=]() { + connect(ui->userAgent, &QPlainTextEdit::textChanged, profile, [this, profile]() { profile->setHttpUserAgent(ui->userAgent->toPlainText()); }); ui->acceptLanguage->setPlainText(m_profile->httpAcceptLanguage()); - connect(ui->acceptLanguage, &QPlainTextEdit::textChanged, profile, [=]() { + connect(ui->acceptLanguage, &QPlainTextEdit::textChanged, profile, [this, profile]() { profile->setHttpAcceptLanguage(ui->acceptLanguage->toPlainText()); }); + ui->headers_table->connect(settings, "headers"); + ui->cacheType->setCurrentIndex(m_profile->httpCacheType()); - connect(ui->cacheType, QOverload::of(&QComboBox::currentIndexChanged), profile, [=](int index) { - profile->setHttpCacheType(index); + connect(ui->cacheType, QOverload::of(&QComboBox::currentIndexChanged), profile, [profile](int index) { + profile->setHttpCacheType(static_cast(index)); }); ui->cacheSize->setText(QString::number(m_profile->httpCacheMaximumSize())); - connect(ui->cacheSize, &QLineEdit::textChanged, profile, [=](const QString &text) { + connect(ui->cacheSize, &QLineEdit::textChanged, profile, [profile](const QString &text) { profile->setHttpCacheMaximumSize(text.toInt()); }); ui->cookiePolicy->setCurrentIndex(m_profile->persistentCookiesPolicy()); - connect(ui->cookiePolicy, QOverload::of(&QComboBox::currentIndexChanged), profile, [=](int index) { - profile->setPersistentCookiesPolicy(index); + connect(ui->cookiePolicy, QOverload::of(&QComboBox::currentIndexChanged), profile, [profile](int index) { + profile->setPersistentCookiesPolicy(static_cast(index)); }); connect(ui->clearCache_pushButton, &QPushButton::clicked, profile, &QWebEngineProfile::clearHttpCache); @@ -86,28 +68,13 @@ ProfileView::ProfileView(Profile *profile, QWidget *parent) ui->storagePath_lineEdit->setText(m_profile->persistentStoragePath()); ui->cachePath_lineEdit->setText(m_profile->cachePath()); - // headers tab - for(auto i = m_profile->headers().constBegin(); i != m_profile->headers().constEnd(); ++i) { - //ui->httpHeaders->addItem(); - headerChanged(i.key(), i.value()); + if(profile->isOffTheRecord()) { + ui->cacheType->setEnabled(false); + ui->cacheSize->setEnabled(false); + ui->storagePath_lineEdit->setEnabled(false); + ui->cachePath_lineEdit->setEnabled(false); + ui->cookiePolicy->setEnabled(false); } - connect(m_profile, &Profile::headerChanged, this, &ProfileView::headerChanged); - connect(m_profile, &Profile::headerRemoved, this, &ProfileView::headerRemoved); - connect(ui->headers_insert, &QPushButton::clicked, m_profile, [this]() { - auto *dlg = new NewHttpHeaderDialog(this); - if(dlg->exec() == QDialog::Accepted) { - m_profile->setHttpHeader(dlg->header(), dlg->value()); - } - delete dlg; - }); - connect(ui->headers_delete, &QPushButton::clicked, m_profile, [this]() { - for(auto &list : ui->httpHeaders->selectedRanges()) { - for(int i = list.bottomRow(); i >= list.topRow(); --i) { - m_profile->removeHttpHeader(ui->httpHeaders->item(i, 0)->text()); - } - } - }); - // settings tab connectSetting(ui->autoloadImages, m_profile, QWebEngineSettings::AutoLoadImages); connectSetting(ui->autoloadIcons, m_profile, QWebEngineSettings::AutoLoadIconsForPage); @@ -144,62 +111,39 @@ ProfileView::ProfileView(Profile *profile, QWidget *parent) connectSetting(ui->printElementBackgrounds, m_profile, QWebEngineSettings::PrintElementBackgrounds); // cookies tab - loadCookies(profile->cookieStore()); - for(const auto &c : profile->cookies()) { - cookieAdded(c); + for(const auto &data : profile->property("cookies").toList()) { + for(const auto &cookie : QNetworkCookie::parseCookies(data.toByteArray())) { + cookieAdded(cookie); + } } -} -ProfileView::~ProfileView() -{ - delete ui; -} + connect(profile->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, &ProfileView::cookieAdded); + connect(profile->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, &ProfileView::cookieRemoved); -void ProfileView::loadCookies(QWebEngineCookieStore *store) -{ - // - connect(store, &QWebEngineCookieStore::cookieAdded, this, &ProfileView::cookieAdded); - connect(store, &QWebEngineCookieStore::cookieRemoved, this, &ProfileView::cookieRemoved); + connect(ui->cookies_deleteSession, &QPushButton::clicked, profile->cookieStore(), &QWebEngineCookieStore::deleteSessionCookies); + connect(ui->cookies_deleteAll, &QPushButton::clicked, profile->cookieStore(), &QWebEngineCookieStore::deleteAllCookies); - connect(ui->cookies_reload, &QPushButton::clicked, store, [=]() { - ui->cookies->clearContents(); - ui->cookies->setRowCount(0); - store->loadAllCookies(); - }); - - connect(ui->cookies_delete, &QPushButton::clicked, store, [=]() { + connect(ui->cookies_delete, &QPushButton::clicked, [this, profile]() { for(auto &list : ui->cookies->selectedRanges()) { for(int i = list.bottomRow(); i >= list.topRow(); --i) { auto cookie = ui->cookies->item(i, 0)->data(Qt::UserRole).value(); - store->deleteCookie(cookie); + profile->cookieStore()->deleteCookie(cookie); } } }); - connect(ui->cookies_deleteSession, &QPushButton::clicked, store, &QWebEngineCookieStore::deleteSessionCookies); - connect(ui->cookies_deleteAll, &QPushButton::clicked, store, &QWebEngineCookieStore::deleteAllCookies); -} -void ProfileView::headerChanged(const QString &name, const QString &value) -{ - const auto items = ui->httpHeaders->findItems(name, Qt::MatchExactly); - if(!items.isEmpty()) { - QTableWidgetItem *valueItem = ui->httpHeaders->item(items.constFirst()->row(), 1); - valueItem->setText(value); - } else { - // new header - const int index = ui->httpHeaders->rowCount(); - ui->httpHeaders->setRowCount(index + 1); - ui->httpHeaders->setItem(index, 0, new QTableWidgetItem(name)); - ui->httpHeaders->setItem(index, 1, new QTableWidgetItem(value)); - } + connect(ui->cookies_reload, &QPushButton::clicked, [this, profile]() { + ui->cookies->clearContents(); + ui->cookies->setRowCount(0); + qDebug("loadAllCookies called!"); + profile->cookieStore()->loadAllCookies(); + }); + } -void ProfileView::headerRemoved(const QString& name) +ProfileView::~ProfileView() { - const auto items = ui->httpHeaders->findItems(name, Qt::MatchExactly); - if(!items.isEmpty()) { - ui->httpHeaders->removeRow(items.constFirst()->row()); - } + delete ui; } void ProfileView::cookieAdded(const QNetworkCookie &cookie) @@ -212,21 +156,24 @@ void ProfileView::cookieAdded(const QNetworkCookie &cookie) ui->cookies->setItem(index, 0, item); ui->cookies->setItem(index, 1, new QTableWidgetItem(cookie.domain())); ui->cookies->setItem(index, 2, new QTableWidgetItem(cookie.path())); - if(cookie.isSessionCookie()) + if(cookie.isSessionCookie()) { ui->cookies->setItem(index, 3, new QTableWidgetItem(tr("session"))); - else + } else { ui->cookies->setItem(index, 3, new QTableWidgetItem(cookie.expirationDate().toString(Qt::RFC2822Date))); + } } void ProfileView::cookieRemoved(const QNetworkCookie &cookie) { + qDebug("cookieRemoved called"); + for(int i = 0; i < ui->cookies->rowCount(); ++i) { auto *item = ui->cookies->item(i, 0); - if(item->data(Qt::UserRole).value() == cookie) { - //qDebug("removing cookie on row %i", i); + if(item->data(Qt::UserRole).value().hasSameIdentifier(cookie)) { + qDebug("removing cookie on row %i", i); ui->cookies->removeRow(i); - break; + return; } + qDebug("cookie on row %i doesn't have the same identifier", i); } } - diff --git a/plugins/ProfileEditor/forms/profileview.h b/plugins/ProfileEditor/forms/profileview.h index 7f3e738..9b4506a 100644 --- a/plugins/ProfileEditor/forms/profileview.h +++ b/plugins/ProfileEditor/forms/profileview.h @@ -12,13 +12,14 @@ #include #include #include +#include +#include namespace Ui { class ProfileView; } -class Profile; class QWebEngineCookieStore; class QCheckBox; @@ -27,21 +28,16 @@ class ProfileView : public QWidget Q_OBJECT public: - explicit ProfileView(Profile *profile, QWidget *parent = nullptr); + explicit ProfileView(const QString &name, QWebEngineProfile *profile, QSettings *settings, QWidget *parent = nullptr); ~ProfileView() override; private slots: - void loadCookies(QWebEngineCookieStore *store); - - void headerChanged(const QString &name, const QString &value); - void headerRemoved(const QString &name); - void cookieAdded(const QNetworkCookie &cookie); void cookieRemoved(const QNetworkCookie &cookie); private: Ui::ProfileView *ui; - Profile *m_profile; + QWebEngineProfile *m_profile; }; #endif // PROFILEDIALOG_H diff --git a/plugins/ProfileEditor/forms/profileview.ui b/plugins/ProfileEditor/forms/profileview.ui index 630e53d..46e0a68 100644 --- a/plugins/ProfileEditor/forms/profileview.ui +++ b/plugins/ProfileEditor/forms/profileview.ui @@ -7,7 +7,7 @@ 0 0 640 - 504 + 510 @@ -19,7 +19,7 @@ Profile - + @@ -29,76 +29,58 @@ General - - - - - Name - - - - - - - - - - false - - - Off-the-record - - - - - - - false - - + + + + + + + + + + + + + + false + + + Off-the-record + + + + - - - + + + Configuration - - - - - - Homepage - - - - - - - New tab page - - - - - - - Search - - - - - - - - - - - - - - - - Qt::Horizontal + + true + + + + + true + + + false + + + + Key + + + + + Value + + + + + @@ -125,176 +107,150 @@ - - - - Cache Type - + + + + + + + true + + + false + + + + Key + + + + + Value + + - + - Cache Size + Headers - - + + + + + Storage + + + + + + Cache Type + + - - + + - - Memory Cache - + + + + Memory Cache + + + + + Disk Cache + + + + + Disabled + + + - - Disk Cache - + + + Qt::Horizontal + + + + 40 + 20 + + + - - Disabled - + + + Clear Cache + + - - - - + - - + + - Storage Path + Cache Size - - - - false - - + + - + Cache Path - + false - - + + - Clear Cache + Storage Path - - - - Cookies + + + + false - - - - - No Persistent Cookies - - - - - Allow Persistent Cookies - - - - - Force Persistent Cookies - - - - - + Clear History - - - - - Headers - - - - - - 200 - - - 100 - - - true - - - false - - - - Name - - - - - Value - - - - - - - - - - Insert - - - - - - - Delete - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - + + + + Qt::Vertical + + + + 20 + 40 + + + @@ -313,7 +269,7 @@ 0 0 - 584 + 273 797 @@ -559,94 +515,128 @@ Cookies - + - - - QAbstractScrollArea::AdjustToContents - - - QAbstractItemView::NoEditTriggers - - - QAbstractItemView::SelectRows - - - false - - - false - - - true - - - false - - - - Name - - - - - Domain - - - - - Path - - - - - Expires - - - - - - - - + + + - Reload + Cookies - - - - Delete - + + + + + No Persistent Cookies + + + + + Allow Persistent Cookies + + + + + Force Persistent Cookies + + + + + + - - - Delete Session + + + QAbstractScrollArea::AdjustToContents - - - - - - Delete All + + QAbstractItemView::NoEditTriggers + + + QAbstractItemView::SelectRows + + false + + + false + + + true + + + false + + + + Name + + + + + Domain + + + + + Path + + + + + Expires + + - - - Qt::Vertical - - - - 20 - 40 - - - + + + + + Reload + + + + + + + Delete + + + + + + + Delete Session + + + + + + + Delete All + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + @@ -656,6 +646,13 @@ + + + SettingsTable + QTableWidget +
forms/settingstable.h
+
+
diff --git a/plugins/ProfileEditor/forms/settingstable.cpp b/plugins/ProfileEditor/forms/settingstable.cpp new file mode 100644 index 0000000..6cfe144 --- /dev/null +++ b/plugins/ProfileEditor/forms/settingstable.cpp @@ -0,0 +1,90 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/cgit/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "settingstable.h" +#include +#include +#include + +SettingsTable::SettingsTable(QWidget *parent) + : QTableWidget(parent) +{ + QObject::connect(this, &QTableWidget::currentCellChanged, [this](int currentRow, int currentColumn, int /*previousRow*/, int /*previousColumn*/) { + if(currentColumn == 0) { + const auto key = item(currentRow, currentColumn)->text(); + m_selectedKey = key; + } + }); + + addBtn = new QToolButton(this); + addBtn->setText("Add"); + removeBtn = new QToolButton(this); + removeBtn->setText("Remove"); +} + +void SettingsTable::connect(QSettings *settings, const QString §ion) +{ + settings->beginGroup(section); + if(settings != nullptr) { + const auto keys = settings->childKeys(); + setRowCount(keys.count()); + for(int i = 0; i < keys.count(); ++i) { + setItem(i, 0, new QTableWidgetItem(keys[i])); + setItem(i, 1, new QTableWidgetItem(settings->value(keys[i]).toString())); + } + } + settings->endGroup(); + + QObject::connect(addBtn, &QToolButton::clicked, [this]() { + const auto row = rowCount(); + setRowCount(row + 1); + setItem(row, 0, new QTableWidgetItem("key")); + setItem(row, 1, new QTableWidgetItem("value")); + }); + + QObject::connect(removeBtn, &QToolButton::clicked, [this, settings, section]() { + settings->beginGroup(section); + + const int row = currentRow(); + const auto key = item(row, 0)->text(); + + removeRow(row); + qDebug("remove key: %s", qUtf8Printable(key)); + settings->remove(key); + + settings->endGroup(); + }); + + QObject::connect(this, &QTableWidget::cellChanged, [this, settings, section](int row, int column) { + // no value has been created yet + if(item(row, 1) == nullptr) { + return; + } + + settings->beginGroup(section); + + if(column == 0) { + qDebug("remove old key: %s", qUtf8Printable(m_selectedKey)); + settings->remove(m_selectedKey); + } + + const auto key = item(row, 0)->text(); + const auto val = item(row, 1)->text(); + qDebug("set: [%s]=[%s]", qUtf8Printable(key), qUtf8Printable(val)); + settings->setValue(key, val); + + settings->endGroup(); + }); +} + +void SettingsTable::resizeEvent(QResizeEvent *event) +{ + QTableWidget::resizeEvent(event); + addBtn->move(width() - addBtn->width() - removeBtn->width(), 0); + removeBtn->move(width() - removeBtn->width(), 0); +} diff --git a/plugins/ProfileEditor/forms/settingstable.h b/plugins/ProfileEditor/forms/settingstable.h new file mode 100644 index 0000000..80e76f0 --- /dev/null +++ b/plugins/ProfileEditor/forms/settingstable.h @@ -0,0 +1,31 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/cgit/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SETTINGS_TREE +#define SETTINGS_TREE + +#include + +class QSettings; +class QToolButton; +class SettingsTable : public QTableWidget +{ +public: + SettingsTable(QWidget *parent = nullptr); + + void connect(QSettings *settings, const QString §ion = QString()); + +protected: + void resizeEvent(QResizeEvent *event); + +private: + QString m_selectedKey; + QToolButton *addBtn = nullptr, *removeBtn = nullptr; +}; + +#endif // SETTINGS_TREE diff --git a/plugins/ProfileEditor/meson.build b/plugins/ProfileEditor/meson.build index caa3a75..47a1c17 100644 --- a/plugins/ProfileEditor/meson.build +++ b/plugins/ProfileEditor/meson.build @@ -1,15 +1,14 @@ ProfileEditorPlugin_moc = mod_qt5.preprocess( - include_directories: include, - moc_headers: ['profileeditorplugin.h', 'forms/profilemanagerdialog.h', 'forms/profileview.h', 'forms/newprofiledialog.h', 'forms/newhttpheaderdialog.h'], - ui_files: ['forms/profilemanagerdialog.ui', 'forms/profileview.ui', 'forms/newprofiledialog.ui', 'forms/newhttpheaderdialog.ui'], - dependencies: dep_qt5 + include_directories: plugininterfaces_include, + moc_headers: [ 'profileeditorplugin.h', 'forms/profilemanagerdialog.h', 'forms/profileview.h' ], + ui_files: [ 'forms/profilemanagerdialog.ui', 'forms/profileview.ui' ], ) -ProfileEditorPlugin_lib = shared_library('ProfileEditorPlugin', - [interfaces_moc, ProfileEditorPlugin_moc, 'profileeditorplugin.cpp', 'forms/profilemanagerdialog.cpp', 'forms/profileview.cpp', 'forms/newprofiledialog.cpp', 'forms/newhttpheaderdialog.cpp'], - dependencies: dep_qt5, - include_directories: include, - install: true, install_dir: join_paths(get_option('libdir'), 'smolbote/plugins') +ProfileEditorPlugin = shared_library('ProfileEditorPlugin', + [ ProfileEditorPlugin_moc, 'profileeditorplugin.cpp', 'forms/profilemanagerdialog.cpp', 'forms/profileview.cpp', 'forms/settingstable.cpp' ], + include_directories: plugininterfaces_include, + dependencies: [ dep_qt5 ], + install: true, install_dir: join_paths(get_option('libdir'), 'smolbote/plugins') ) test('run', executable('ProfileEditor', 'test/main.cpp', diff --git a/plugins/ProfileEditor/profileeditorplugin.cpp b/plugins/ProfileEditor/profileeditorplugin.cpp index d480392..6d1fc6a 100644 --- a/plugins/ProfileEditor/profileeditorplugin.cpp +++ b/plugins/ProfileEditor/profileeditorplugin.cpp @@ -8,48 +8,12 @@ #include "profileeditorplugin.h" #include "forms/profilemanagerdialog.h" -#include "forms/newprofiledialog.h" - -QHash> ProfileEditorPlugin::commands() -{ - QHash> hash; - - hash.insert("edit-profiles-ui", [this]() -> int { - auto *dialog = createWidget(nullptr); - return dialog->exec(); - }); - return hash; -} QDialog *ProfileEditorPlugin::createWidget(QWidget *parent) const { - auto *app = browser(); - Q_CHECK_PTR(app); auto *widget = new ProfileManagerDialog(parent); widget->setAttribute(Qt::WA_DeleteOnClose, true); - for(const auto &pair : app->profileList()) { - widget->addProfile(pair.first, pair.second); - } - - connect(widget->ui->new_pushButton, &QPushButton::clicked, widget, [=]() { - auto *newProfileDlg = new NewProfileDialog(widget); - if(newProfileDlg->exec() == QDialog::Accepted) { - const auto pair = app->loadProfile(newProfileDlg->getId(), newProfileDlg->getOtr()); - widget->addProfile(pair.first, pair.second); - } - delete newProfileDlg; - }); - - connect(widget->ui->delete_pushButton, &QPushButton::clicked, widget, [=]() { - auto *item = widget->ui->listWidget->currentItem(); - if(item != nullptr) { - // there is a selected widget - const QString id = item->text(); - widget->showProfile(nullptr); - app->removeProfile(id); - } - }); return widget; } diff --git a/plugins/ProfileEditor/profileeditorplugin.h b/plugins/ProfileEditor/profileeditorplugin.h index a673436..13f6d90 100644 --- a/plugins/ProfileEditor/profileeditorplugin.h +++ b/plugins/ProfileEditor/profileeditorplugin.h @@ -9,7 +9,7 @@ #ifndef PROFILEEDITOR_PLUGIN_H #define PROFILEEDITOR_PLUGIN_H -#include +#include class QDialog; class ProfileEditorPlugin : public QObject, public PluginInterface @@ -19,10 +19,7 @@ class ProfileEditorPlugin : public QObject, public PluginInterface Q_INTERFACES(PluginInterface) public: - // PluginInterface - QHash> commands() override; - - // ProfileInterface + ~ProfileEditorPlugin() override = default; QDialog *createWidget(QWidget *parent) const override; }; diff --git a/plugins/ProfileEditor/test/main.cpp b/plugins/ProfileEditor/test/main.cpp new file mode 100644 index 0000000..40b563c --- /dev/null +++ b/plugins/ProfileEditor/test/main.cpp @@ -0,0 +1,22 @@ +#include +#include "profileeditorplugin.h" +#include +#include +#include + +int main(int argc, char** argv) +{ + QApplication app(argc, argv); + ProfileEditorPlugin plugin; + + auto *dlg = dynamic_cast(plugin.createWidget(nullptr)); + const auto i = dlg->addProfile("id", "testing", new QWebEngineProfile, new QSettings(QString(), QSettings::IniFormat, &app)); + dlg->show(); + + if(qEnvironmentVariableIsSet("autoclose")) { + dlg->showProfile(i); + QTimer::singleShot(200, dlg, &QDialog::accept); + } + + return app.exec(); +} 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 @@
- - - Plugins - - 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 #include -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 @@ 0 0 - 600 + 900 420 @@ -19,6 +19,9 @@ true + + false + Name @@ -52,28 +55,32 @@ - - - - Key - - - - - Value - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Close - - + + + + + Add + + + + + + + text + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + 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 +#include 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(plugin->instance())) { - interface->createWidget(this->parentWidget())->exec(); + const auto *interface = qobject_cast(plugin->instance()); + auto *dlg = interface->createWidget(this->parentWidget()); + + if(auto *profileDlg = dynamic_cast(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 &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("plugins.signature.checked").value(), conf.value("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 &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 &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(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 &callback = [](const auto) {}); void setup(); const QVector 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 &data, bool merge = true); + bool loadPlugin(const QString &path); + private: struct PluginInfo { explicit PluginInfo(QPluginLoader *l) @@ -71,7 +75,6 @@ private: QPluginLoader *loader; QVector 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("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("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{}, "https://search.url/t=%1", "https://duckduckgo.com/?q=%1&ia=web", "aaabbbccc"); - const QUrl homepage = GENERATE(as{}, "https://homepage.net", "about:blank", "aaabbbccc"); - const QUrl newtab = GENERATE(as{}, "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(), 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{}, "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{}, "a", "bb", "ccc"); + profile->setSearch(search); + THEN("the search url changes") { - const QString name = GENERATE(as{}, "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{}, "a", "bb", "ccc"); + profile->setHomepage(url); + THEN("homepage changes") { - const QString search = GENERATE(as{}, "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{}, "a", "bb", "ccc"); + profile->setNewtab(url); + THEN("newtab changes") { - const QUrl url = GENERATE(as{}, "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{}, "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 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 #include #include #include -#include "urlinterceptor.h" +#include 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 #include +#include #include #include +#include #include +#include #include #include -#include -#include 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 cookies READ cookies WRITE setCookies NOTIFY cookiesChanged) + Q_PROPERTY(QMap 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 cookies() const + [[nodiscard]] QList cookies() const { - return qAsConst(m_cookies); + QList 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 headers() const + { + QMap 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 &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 &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 m_filters; - QVector m_cookies; + QVector m_filters; + QList m_cookies; QMap 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::idList() const } template <> -void WebProfileManager::walk(std::function f) const +void WebProfileManager::walk(std::function 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::walk(std::function -void WebProfileManager::walk(std::function f) const +void WebProfileManager::walk(std::function 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) const; + callable_when(unconsumed) void walk(std::function) const; callable_when(unconsumed) void make_global(); -- cgit v1.2.1