From 696743557de6a6b82303618d8f60f08f8978e650 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 26 Nov 2018 00:58:19 +0100 Subject: Add Session::view and Session::restoreView --- src/session/session.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ src/session/session.h | 26 +++++++++++ 2 files changed, 140 insertions(+) create mode 100644 src/session/session.cpp create mode 100644 src/session/session.h (limited to 'src/session') 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 +#include +#include "webengine/webview.h" +#include "profilemanager.h" +#include "browser.h" +#include "../webengine/webview.h" +#include + +QJsonObject Session::session(QVector 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(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(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(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 + +class MainWindow; +class WebView; + +namespace Session { +QJsonObject session(QVector 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 -- cgit v1.2.1