aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-06-24 17:27:44 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-06-24 17:27:44 +0200
commite81526f15e8c1e9c82b27009dbe512f78cf1dc0f (patch)
treeea9a140347b6077d0521f85da4f9d881185fbb90
parentSplit shortcuts into new tab (diff)
downloadsmolbote-e81526f15e8c1e9c82b27009dbe512f78cf1dc0f.tar.xz
Add Session class
-rw-r--r--lib/web/webprofile.cpp9
-rw-r--r--lib/web/webprofile.h11
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/browser.cpp4
-rw-r--r--src/mainwindow/mainwindow.cpp37
-rw-r--r--src/mainwindow/mainwindow.h1
-rw-r--r--src/mainwindow/subwindow.cpp19
-rw-r--r--src/session.cpp30
-rw-r--r--src/session.h29
9 files changed, 113 insertions, 29 deletions
diff --git a/lib/web/webprofile.cpp b/lib/web/webprofile.cpp
index 1124261..1613e52 100644
--- a/lib/web/webprofile.cpp
+++ b/lib/web/webprofile.cpp
@@ -13,8 +13,9 @@
#include <QWebEngineSettings>
WebProfile *WebProfile::profile = nullptr;
+QMap<QString, WebProfile *> WebProfile::profiles;
-WebProfile* loadProfile(const QString &name, const QHash<QString, QString> &defaults, const QString &path, QObject *parent)
+WebProfile* WebProfile::loadProfile(const QString &name, const QHash<QString, QString> &defaults, const QString &path, QObject *parent)
{
WebProfile *profile = nullptr;
#ifdef QT_DEBUG
@@ -36,6 +37,7 @@ WebProfile* loadProfile(const QString &name, const QHash<QString, QString> &defa
}
Q_CHECK_PTR(profile);
+ profiles.insert(name, profile);
profile->setSearch(config.value("search", defaults.value("profile.search")).toString());
profile->setHomepage(config.value("homepage", defaults.value("profile.homepage")).toUrl());
@@ -73,6 +75,10 @@ WebProfile* loadProfile(const QString &name, const QHash<QString, QString> &defa
WebProfile::WebProfile(const QString &name, const QString &configPath, QObject *parent)
: QWebEngineProfile(parent)
{
+ if(configPath.isEmpty())
+ m_id = configPath;
+ else
+ m_id = QFileInfo(configPath).baseName();
m_name = name;
m_configPath = configPath;
@@ -91,6 +97,7 @@ WebProfile::WebProfile(const QString &name, const QString &configPath, QObject *
WebProfile::WebProfile(const QString &storageName, const QString &name, const QString &configPath, QObject *parent)
: QWebEngineProfile(storageName, parent)
{
+ m_id = QFileInfo(configPath).baseName();
m_name = name;
m_configPath = configPath;
diff --git a/lib/web/webprofile.h b/lib/web/webprofile.h
index 279e6bf..d41625c 100644
--- a/lib/web/webprofile.h
+++ b/lib/web/webprofile.h
@@ -15,6 +15,7 @@
#include <QWebEngineProfile>
#include <QVector>
#include <QNetworkCookie>
+#include <QMap>
class WebProfile : public QWebEngineProfile
{
@@ -53,6 +54,13 @@ public:
return WebProfile::profile;
}
+ static WebProfile* loadProfile(const QString &name, const QHash<QString, QString> &defaults, const QString &path = QString(), QObject *parent = nullptr);
+
+ const QString id() const
+ {
+ return profiles.key(const_cast<WebProfile*>(this), "");
+ }
+
QString name() const
{
return m_name;
@@ -104,7 +112,9 @@ signals:
private:
static WebProfile *profile;
+ static QMap<QString, WebProfile *> profiles;
+ QString m_id;
QString m_configPath;
QString m_name;
QString m_search = QString("about:blank");
@@ -114,7 +124,6 @@ private:
QVector<QNetworkCookie> m_cookies;
};
-WebProfile* loadProfile(const QString &name, const QHash<QString, QString> &defaults, const QString &path = QString(), QObject *parent = nullptr);
//WebProfile *saveProfile(WebProfile *profile, const QString &path);
#endif // SMOLBOTE_WEBENGINEPROFILE_H
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 5f4aed1..796f8e6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -13,6 +13,8 @@ add_executable(poi
singleapplication.h
browser.cpp
browser.h
+ session.cpp
+ session.h
../data/resources.qrc
# main window
diff --git a/src/browser.cpp b/src/browser.cpp
index e266024..d5a3d3e 100644
--- a/src/browser.cpp
+++ b/src/browser.cpp
@@ -94,7 +94,7 @@ void Browser::setup(const QString &defaultProfile)
for(const QFileInfo &f : entries) {
auto name = f.baseName();
- auto *profile = loadProfile(name, defaults, f.absoluteFilePath(), this);
+ auto *profile = WebProfile::loadProfile(name, defaults, f.absoluteFilePath(), this);
m_profiles.insert(name, profile);
connect(profile, &WebProfile::destroyed, this, [=]() {
m_profiles.remove(name);
@@ -106,7 +106,7 @@ void Browser::setup(const QString &defaultProfile)
// set default profile
if(!m_profiles.contains(defaultProfile)) {
// if this profile has not been added, it doesn't have a path
- auto *profile = loadProfile(defaultProfile, defaults, QString(), this);
+ auto *profile = WebProfile::loadProfile(defaultProfile, defaults, QString(), this);
m_profiles.insert(defaultProfile, profile);
connect(profile, &WebProfile::destroyed, this, [=]() {
m_profiles.remove(defaultProfile);
diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp
index 27279c1..0932aee 100644
--- a/src/mainwindow/mainwindow.cpp
+++ b/src/mainwindow/mainwindow.cpp
@@ -29,6 +29,10 @@
#include <QUrl>
#include <configuration/configuration.h>
+#ifdef QT_DEBUG
+#include "session.h"
+#endif
+
#ifdef PLASMA_BLUR
#include <KWindowEffects>
#endif
@@ -183,6 +187,16 @@ void MainWindow::createMenuBar()
// tools menu
toolsMenu = menuBar()->addMenu(tr("Tools"));
+
+ // debug menu
+#ifdef QT_DEBUG
+ auto *debugMenu = menuBar()->addMenu(tr("Debug"));
+
+ debugMenu->addAction(tr("Print window session"), [this]() {
+ auto json = Session::toJson(this);
+ qDebug("session data >>>\n%s\n<<<", qUtf8Printable(json.toJson()));
+ });
+#endif
}
void MainWindow::addAction(ActionLocation where, QAction *action)
@@ -236,6 +250,18 @@ void MainWindow::createTab(const QUrl &url)
}
}
+const QVector<SubWindow *> MainWindow::subWindows() const
+{
+ QVector<SubWindow *> list;
+ for(auto *w : mdiArea->subWindowList()) {
+ auto *subwindow = qobject_cast<SubWindow *>(w);
+ if(subwindow != nullptr)
+ list.append(subwindow);
+ }
+
+ return list;
+}
+
SubWindow *MainWindow::currentSubWindow() const
{
return qobject_cast<SubWindow *>(mdiArea->currentSubWindow());
@@ -247,16 +273,7 @@ SubWindow *MainWindow::createSubWindow(const QString &url)
mdiArea->addSubWindow(w);
w->showMaximized();
w->setFocus();
-
- QJsonObject session;
- session.insert("profile", "");
- QJsonArray urls;
- if(!url.isEmpty())
- urls.append(url);
- session.insert("tabs", urls);
-
- w->restoreSession(session);
-
+ w->addTab(url);
return w;
}
diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h
index 7974806..9cb65d6 100644
--- a/src/mainwindow/mainwindow.h
+++ b/src/mainwindow/mainwindow.h
@@ -42,6 +42,7 @@ public:
void addDockWidget(Qt::DockWidgetArea area, QWidget *widget);
void removeDockWidget(QWidget *widget);
+ const QVector<SubWindow *> subWindows() const;
SubWindow *currentSubWindow() const;
public slots:
diff --git a/src/mainwindow/subwindow.cpp b/src/mainwindow/subwindow.cpp
index 2b9cd48..cbfe46a 100644
--- a/src/mainwindow/subwindow.cpp
+++ b/src/mainwindow/subwindow.cpp
@@ -52,19 +52,6 @@ SubWindow::SubWindow(const QHash<QString, QString> &config, QWidget *parent, Qt:
profileName_action->setText(tr("Profile: %1").arg(name));
});
}
-
-#ifdef QT_DEBUG
- menu->addSeparator();
- menu->addAction(tr("Debug menu"))->setEnabled(false);
- auto *saveSession_action = menu->addAction(tr("Save session"));
- menu->addAction(tr("Load session"))->setEnabled(false);
- setSystemMenu(menu);
-
- connect(saveSession_action, &QAction::triggered, [this]() {
- QJsonDocument doc(session());
- qDebug("%s", qUtf8Printable(doc.toJson()));
- });
-#endif
}
// new tab button
@@ -162,13 +149,15 @@ void SubWindow::setCurrentTab(int index)
QJsonObject SubWindow::session() const
{
QJsonObject obj;
- obj.insert("profile", QJsonValue(""));
+ obj.insert("profile", profile->id());
QJsonArray tabs;
for(int i = 0; i < tabWidget->count(); ++i) {
auto *view = qobject_cast<WebView *>(tabWidget->widget(i));
if(view) {
- tabs.append(view->url().toString());
+ QJsonObject tab;
+ tab.insert(view->url().toString(), view->profile()->id());
+ tabs.append(tab);
}
}
obj.insert("tabs", tabs);
diff --git a/src/session.cpp b/src/session.cpp
new file mode 100644
index 0000000..8e4f9a8
--- /dev/null
+++ b/src/session.cpp
@@ -0,0 +1,30 @@
+/*
+ * 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/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "session.h"
+#include "mainwindow/mainwindow.h"
+#include "mainwindow/subwindow.h"
+#include <QJsonObject>
+#include <QJsonArray>
+
+Session::Session(QObject *parent) : QObject(parent)
+{
+}
+
+QJsonDocument Session::toJson(MainWindow *window)
+{
+ QJsonObject session;
+
+ QJsonArray subwindows;
+ for(const SubWindow *subwindow : window->subWindows()) {
+ subwindows.append(subwindow->session());
+ }
+ session.insert("subwindows", subwindows);
+
+ return QJsonDocument(session);
+}
diff --git a/src/session.h b/src/session.h
new file mode 100644
index 0000000..bb4f842
--- /dev/null
+++ b/src/session.h
@@ -0,0 +1,29 @@
+/*
+ * 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/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#ifndef SMOLBOTE_SESSION_H
+#define SMOLBOTE_SESSION_H
+
+#include <QObject>
+#include <QJsonDocument>
+
+class MainWindow;
+class Session : public QObject
+{
+ Q_OBJECT
+public:
+ explicit Session(QObject *parent = nullptr);
+
+ static QJsonDocument toJson(MainWindow *window);
+
+signals:
+
+public slots:
+};
+
+#endif // SMOLBOTE_SESSION_H