aboutsummaryrefslogtreecommitdiff
path: root/src/session
diff options
context:
space:
mode:
Diffstat (limited to 'src/session')
-rw-r--r--src/session/session.cpp95
-rw-r--r--src/session/session.h12
-rw-r--r--src/session/sessiondialog.cpp2
3 files changed, 98 insertions, 11 deletions
diff --git a/src/session/session.cpp b/src/session/session.cpp
index d728d55..0853c70 100644
--- a/src/session/session.cpp
+++ b/src/session/session.cpp
@@ -7,15 +7,17 @@
*/
#include "session.h"
+#include "../webengine/webview.h"
+#include "browser.h"
#include "mainwindow/mainwindow.h"
+#include "profilemanager.h"
#include "subwindow/subwindow.h"
-#include <QJsonObject>
-#include <QJsonArray>
#include "webengine/webview.h"
-#include "profilemanager.h"
-#include "browser.h"
-#include "../webengine/webview.h"
+#include <QJsonArray>
+#include <QJsonObject>
#include <QWebEngineHistory>
+#include <memory>
+#include "configuration.h"
QJsonObject Session::session(QVector<MainWindow *> windows)
{
@@ -81,6 +83,58 @@ QJsonObject Session::window(const QString &profile, const QStringList &urls)
return session;
}
+//////
+
+QJsonObject Session::_session(const QVector<MainWindow *> windows)
+{
+ QJsonObject sessionData;
+
+ {
+ QJsonArray windowList;
+ for(const auto *window : windows) {
+ windowList.append(Session::_window(window));
+ }
+ sessionData.insert("windows", windowList);
+ }
+
+ return sessionData;
+}
+
+QJsonObject Session::_window(const MainWindow *window)
+{
+ QJsonObject windowData;
+
+ {
+ QJsonArray subwindows;
+ for(const auto *subwindow : window->subWindows()) {
+ subwindows.append(Session::_subwindow(subwindow));
+ }
+ windowData.insert("subwindows", subwindows);
+ }
+
+ return windowData;
+}
+
+QJsonObject Session::_subwindow(const SubWindow *subwindow)
+{
+ auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager();
+ Q_CHECK_PTR(profileManager);
+
+ QJsonObject subwindowData;
+
+ subwindowData.insert("profile", profileManager->id(subwindow->profile()));
+
+ {
+ QJsonArray tabs;
+ for(int i = 0; i < subwindow->tabCount(); ++i) {
+ tabs.append(Session::view(subwindow->view(i)));
+ }
+ subwindowData.insert("tabs", tabs);
+ }
+
+ return subwindowData;
+}
+
QJsonObject Session::view(const WebView *view)
{
auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager();
@@ -91,10 +145,10 @@ QJsonObject Session::view(const WebView *view)
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()));
+
+ // store history: compress, toBase64 (to make printable), toQString (to store in json)
+ viewData.insert("history", QString(qCompress(historyData).toBase64()));
return viewData;
}
@@ -108,7 +162,30 @@ void Session::restoreView(WebView *view, const QJsonObject &data)
if(profile)
view->setProfile(profile);
- QByteArray historyData = QByteArray::fromBase64(data["history"].toString().toLatin1());
+ // restore history: toLatin1 (turn into bytearray), fromBase64, uncompress
+ QByteArray historyData = qUncompress(QByteArray::fromBase64(data["history"].toString().toLatin1()));
QDataStream historyStream(&historyData, QIODevice::ReadOnly);
historyStream >> *view->history();
}
+
+void Session::restoreSession(const QJsonObject &sessionData)
+{
+ auto *browser = dynamic_cast<Browser *>(qApp);
+ Q_CHECK_PTR(browser);
+ auto *profileManager = browser->getProfileManager();
+ Q_CHECK_PTR(profileManager);
+
+ for(const auto windowData : sessionData["windows"].toArray()) {
+ auto *window = browser->createWindow();
+
+ for(const auto subwindowData : windowData.toObject()["subwindows"].toArray()) {
+ auto *subwindow = window->createSubWindow(browser->getConfiguration(), WebProfile::defaultProfile());
+ subwindow->setProfile(profileManager->profile(subwindowData.toObject()["profile"].toString()));
+
+ for(const auto tabData : subwindowData.toObject()["tabs"].toArray()) {
+ auto *view = subwindow->view(subwindow->addTab());
+ Session::restoreView(view, tabData.toObject());
+ }
+ }
+ }
+}
diff --git a/src/session/session.h b/src/session/session.h
index 222c7ef..720b711 100644
--- a/src/session/session.h
+++ b/src/session/session.h
@@ -12,6 +12,7 @@
#include <QJsonDocument>
class MainWindow;
+class SubWindow;
class WebView;
namespace Session {
@@ -19,8 +20,17 @@ QJsonObject session(QVector<MainWindow *> windows);
QJsonObject window(const MainWindow *window);
QJsonObject window(const QString &profile, const QStringList &urls);
+// TODO:
+QJsonObject _session(const QVector<MainWindow *> windows);
+QJsonObject _window(const MainWindow *window);
+QJsonObject _subwindow(const SubWindow *subwindow);
+
+
QJsonObject view(const WebView *view);
void restoreView(WebView *view, const QJsonObject &data);
-}
+
+void restoreSession(const QJsonObject &sessionData);
+
+} // namespace Session
#endif // SMOLBOTE_SESSION_H
diff --git a/src/session/sessiondialog.cpp b/src/session/sessiondialog.cpp
index e31a42f..5fd65c3 100644
--- a/src/session/sessiondialog.cpp
+++ b/src/session/sessiondialog.cpp
@@ -81,7 +81,7 @@ void SessionDialog::openSession(const QString &filename)
if(json.open(QIODevice::ReadOnly | QIODevice::Text)) {
auto *browser = qobject_cast<Browser *>(qApp);
auto doc = QJsonDocument::fromJson(json.readAll());
- browser->createSession(doc.object());
+ Session::restoreSession(doc.object());
json.close();
}
}