From 8c30d0d5f0ab93c44b6957040fd0a2b3b7749faa Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 12 Jan 2017 19:57:00 +0100 Subject: Profile config loading and saving --- src/forms/profiledialog.cpp | 4 ++++ src/forms/profiledialog.ui | 28 ++++++++++++++++++++++++++++ src/mainwindow.cpp | 17 +++++++++++++++-- src/mainwindow.h | 5 +++-- src/qt-simplebrowser.pro | 6 ++++-- src/webengine/webengineprofile.cpp | 31 +++++++++++++++++++++++++++++++ src/webengine/webengineprofile.h | 20 ++++++++++++++++++++ test/config.ini | 1 + 8 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 src/webengine/webengineprofile.cpp create mode 100644 src/webengine/webengineprofile.h diff --git a/src/forms/profiledialog.cpp b/src/forms/profiledialog.cpp index 14279c4..b485419 100644 --- a/src/forms/profiledialog.cpp +++ b/src/forms/profiledialog.cpp @@ -1,6 +1,8 @@ #include "profiledialog.h" #include "ui_profiledialog.h" +#include + ProfileDialog::ProfileDialog(QWebEngineProfile *profile, QWidget *parent) : QDialog(parent), ui(new Ui::ProfileDialog) @@ -9,6 +11,8 @@ ProfileDialog::ProfileDialog(QWebEngineProfile *profile, QWidget *parent) : ui->setupUi(this); ui->userAgent_lineEdit->setText(_profile->httpUserAgent()); + ui->storagePath_lineEdit->setText(_profile->persistentStoragePath()); + ui->cachePath_lineEdit->setText(_profile->cachePath()); connect(this, SIGNAL(accepted()), this, SLOT(saveProfile())); } diff --git a/src/forms/profiledialog.ui b/src/forms/profiledialog.ui index 160743b..097d9cb 100644 --- a/src/forms/profiledialog.ui +++ b/src/forms/profiledialog.ui @@ -26,6 +26,34 @@ + + + + Storage Path + + + + + + + false + + + + + + + Cache Path + + + + + + + false + + + diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index fef3bcc..1f27652 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -20,7 +20,16 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : browserInstance = instance; Settings settings; - profile = QWebEngineProfile::defaultProfile(); + // Create the profile + profileName = settings.value("defaults/profile").toString(); + if(profileName.isEmpty()) { + qDebug("Creating off-the-record profile"); + profileName = tr("Off the record"); + profile = new WebEngineProfile(this); + } else { + qDebug("Using profile: %s", qUtf8Printable(profileName)); + profile = new WebEngineProfile(profileName, this); + } ui->setupUi(this); resize(settings.value("window/width", 800).toInt(), settings.value("window/height", 600).toInt()); @@ -36,11 +45,13 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : // Profile menu QMenuBar *rightBar = new QMenuBar(ui->menuBar); - profileMenu = new QMenu(tr("Profile: ") + profile->storageName()); + profileMenu = new QMenu(tr("Profile: ") + profileName); rightBar->addMenu(profileMenu); ui->menuBar->setCornerWidget(rightBar); profileMenu->addAction(tr("Edit profile"), this, SLOT(createProfileDialog())); profileMenu->addAction(tr("Load profile")); + profileMenu->addAction(tr("Settings")); + profileMenu->addAction(tr("Cookies")); this->addToolBar(Qt::TopToolBarArea, navigationToolBar); this->addToolBarBreak(Qt::TopToolBarArea); @@ -66,6 +77,8 @@ MainWindow::~MainWindow() void MainWindow::createNewTab(const QUrl &url) { QWebEngineView *view = new QWebEngineView(0); + QWebEnginePage *page = new QWebEnginePage(profile, view); + view->setPage(page); view->load(url); tabBar->addTab(view); } diff --git a/src/mainwindow.h b/src/mainwindow.h index a5709e9..35d7a1d 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include "webengine/webengineprofile.h" #include #include "widgets/webviewtabbar.h" @@ -39,7 +39,8 @@ private slots: private: Browser *browserInstance; - QWebEngineProfile *profile; + QString profileName; + WebEngineProfile *profile; // ui Ui::MainWindow *ui; diff --git a/src/qt-simplebrowser.pro b/src/qt-simplebrowser.pro index 57886d3..b4c98aa 100644 --- a/src/qt-simplebrowser.pro +++ b/src/qt-simplebrowser.pro @@ -16,13 +16,15 @@ SOURCES += main.cpp \ browser.cpp \ widgets/webviewtabbar.cpp \ settings.cpp \ - forms/profiledialog.cpp + forms/profiledialog.cpp \ + webengine/webengineprofile.cpp HEADERS += mainwindow.h \ browser.h \ widgets/webviewtabbar.h \ settings.h \ - forms/profiledialog.h + forms/profiledialog.h \ + webengine/webengineprofile.h FORMS += mainwindow.ui \ forms/profiledialog.ui diff --git a/src/webengine/webengineprofile.cpp b/src/webengine/webengineprofile.cpp new file mode 100644 index 0000000..1eb1112 --- /dev/null +++ b/src/webengine/webengineprofile.cpp @@ -0,0 +1,31 @@ +#include "webengineprofile.h" +#include + +WebEngineProfile::WebEngineProfile(QObject *parent) : + QWebEngineProfile(parent) +{ + // Off-the-record constructor +} + +WebEngineProfile::WebEngineProfile(const QString &storageName, QObject *parent) : + QWebEngineProfile(storageName, parent) +{ + qDebug("Reading WebEngineProfile..."); + QSettings config(persistentStoragePath() + "/profile.ini", QSettings::IniFormat); + + config.beginGroup("http"); + setHttpUserAgent(config.value("userAgent").toString()); + config.endGroup(); +} + +WebEngineProfile::~WebEngineProfile() +{ + if(!this->isOffTheRecord()) { + // save settings + QSettings config(persistentStoragePath() + "/profile.ini", QSettings::IniFormat); + config.beginGroup("http"); + config.setValue("userAgent", httpUserAgent()); + config.endGroup(); + config.sync(); + } +} diff --git a/src/webengine/webengineprofile.h b/src/webengine/webengineprofile.h new file mode 100644 index 0000000..ca2fd08 --- /dev/null +++ b/src/webengine/webengineprofile.h @@ -0,0 +1,20 @@ +#ifndef WEBENGINEPROFILE_H +#define WEBENGINEPROFILE_H + +#include + +class WebEngineProfile : public QWebEngineProfile +{ + Q_OBJECT +public: + WebEngineProfile(QObject *parent = Q_NULLPTR); + WebEngineProfile(const QString &storageName, QObject *parent = Q_NULLPTR); + + ~WebEngineProfile(); + +signals: + +public slots: +}; + +#endif // WEBENGINEPROFILE_H diff --git a/test/config.ini b/test/config.ini index 0c40a5f..e8087b4 100644 --- a/test/config.ini +++ b/test/config.ini @@ -1,5 +1,6 @@ [defaults] url=https://duckduckgo.com +profile=Default [window] title=" -- QtWebEngine" -- cgit v1.2.1