diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-11-26 00:58:19 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-11-26 00:58:19 +0100 |
commit | 696743557de6a6b82303618d8f60f08f8978e650 (patch) | |
tree | 98aeb14150260921e0130c0879d59a65f4a45741 /src/session | |
parent | Add Session Dialog (diff) | |
download | smolbote-696743557de6a6b82303618d8f60f08f8978e650.tar.xz |
Add Session::view and Session::restoreView
Diffstat (limited to 'src/session')
-rw-r--r-- | src/session/session.cpp | 114 | ||||
-rw-r--r-- | src/session/session.h | 26 |
2 files changed, 140 insertions, 0 deletions
diff --git a/src/session/session.cpp b/src/session/session.cpp new file mode 100644 index 0000000..d728d55 --- /dev/null +++ b/src/session/session.cpp @@ -0,0 +1,114 @@ +/* + * 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 "session.h" +#include "mainwindow/mainwindow.h" +#include "subwindow/subwindow.h" +#include <QJsonObject> +#include <QJsonArray> +#include "webengine/webview.h" +#include "profilemanager.h" +#include "browser.h" +#include "../webengine/webview.h" +#include <QWebEngineHistory> + +QJsonObject Session::session(QVector<MainWindow *> windows) +{ + QJsonObject obj; + + QJsonArray windowsArray; + for(const MainWindow *window : windows) { + windowsArray.append(Session::window(window)); + } + obj.insert("windows", windowsArray); + + return obj; +} + +QJsonObject Session::window(const MainWindow *window) +{ + QJsonObject obj; + auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager(); + + QJsonArray subwindows; + for(const SubWindow *subwindow : window->subWindows()) { + QJsonObject window; + window.insert("profile", profileManager->id(subwindow->profile())); + + QJsonArray tabs; + for(int i = 0; i < subwindow->tabCount(); ++i) { + QJsonObject tab; + tab.insert("url", subwindow->view(i)->url().toString()); + tab.insert("profile", profileManager->id(subwindow->view(i)->profile())); + tabs.append(tab); + } + window.insert("tabs", tabs); + + subwindows.append(window); + } + obj.insert("subwindows", subwindows); + + return obj; +} + +QJsonObject Session::window(const QString &profile, const QStringList &urls) +{ + QJsonObject session; + + QJsonArray subwindows; + { + QJsonObject window; + window.insert("profile", profile); + + QJsonArray tabs; + for(const auto &url : urls) { + QJsonObject tab; + tab.insert("url", url); + tab.insert("profile", profile); + tabs.append(tab); + } + window.insert("tabs", tabs); + + subwindows.append(window); + } + session.insert("subwindows", subwindows); + + return session; +} + +QJsonObject Session::view(const WebView *view) +{ + auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager(); + Q_CHECK_PTR(profileManager); + + QByteArray historyData; + QDataStream historyStream(&historyData, QIODevice::WriteOnly); + historyStream << *view->history(); + + QJsonObject viewData; + viewData.insert("url", view->url().toString()); + viewData.insert("profile", profileManager->id(view->profile())); + //viewData.insert("history", QString(qCompress(historyData).toBase64())); + viewData.insert("history", QString(historyData.toBase64())); + + return viewData; +} + +void Session::restoreView(WebView *view, const QJsonObject &data) +{ + auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager(); + Q_CHECK_PTR(profileManager); + + auto *profile = profileManager->profile(data["profile"].toString()); + if(profile) + view->setProfile(profile); + + QByteArray historyData = QByteArray::fromBase64(data["history"].toString().toLatin1()); + QDataStream historyStream(&historyData, QIODevice::ReadOnly); + historyStream >> *view->history(); +} diff --git a/src/session/session.h b/src/session/session.h new file mode 100644 index 0000000..222c7ef --- /dev/null +++ b/src/session/session.h @@ -0,0 +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 + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_SESSION_H +#define SMOLBOTE_SESSION_H + +#include <QJsonDocument> + +class MainWindow; +class WebView; + +namespace Session { +QJsonObject session(QVector<MainWindow *> windows); +QJsonObject window(const MainWindow *window); +QJsonObject window(const QString &profile, const QStringList &urls); + +QJsonObject view(const WebView *view); +void restoreView(WebView *view, const QJsonObject &data); +} + +#endif // SMOLBOTE_SESSION_H |