1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
/*
* 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://library.iserlohn-fortress.net/aqua/smolbote
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "session_json.hpp"
#include <QJsonArray>
#include <QJsonDocument>
[[nodiscard]] QVector<Session::MainWindow> toWindowList(const QString &profile, const QStringList &urls)
{
Session::SubWindow main_;
main_.profile = profile;
for(const auto &url : urls) {
main_.tabs.append({ profile, url, QByteArray() });
}
Session::MainWindow main;
main.subwindows.append(main_);
return { main };
}
[[nodiscard]] QJsonObject toJson(const QVector<Session::MainWindow> &windows)
{
QJsonObject root;
QJsonArray windows_;
{
for(const auto &window : windows) {
QJsonObject window_;
QJsonArray subwindows;
for(const auto &subwindow : window.subwindows) {
QJsonObject subwindow_json;
subwindow_json.insert("profile", subwindow.profile);
QJsonArray tabs;
for(const auto &tab : subwindow.tabs) {
QJsonObject tab_;
tab_.insert("profile", tab.profile);
tab_.insert("url", tab.url);
tab_.insert("history", QString::fromLatin1(tab.history.toBase64()));
tabs.append(tab_);
}
subwindow_json.insert("tabs", tabs);
subwindows.append(subwindow_json);
}
window_.insert("subwindows", subwindows);
windows_.append(window_);
}
}
root.insert("windows", windows_);
return root;
}
JsonSession::JsonSession(const QByteArray &data)
: root(QJsonDocument::fromJson(data).object())
{
}
JsonSession::JsonSession(const QVector<MainWindow> &windows)
: root(toJson(windows))
{
}
JsonSession::JsonSession(const QString &profile, const QStringList &urls)
: root(toJson(toWindowList(profile, urls)))
{
}
QByteArray JsonSession::serialize() const
{
QJsonDocument doc(root);
return doc.toJson(QJsonDocument::Compact);
}
QVector<Session::MainWindow> JsonSession::get() const
{
QVector<Session::MainWindow> windows;
for(const auto &windowData : root["windows"].toArray()) {
Session::MainWindow window;
for(const auto &subwindowData : windowData.toObject()["subwindows"].toArray()) {
Session::SubWindow subwindow;
subwindow.profile = subwindowData.toObject()["profile"].toString();
for(const auto &tabData : subwindowData.toObject()["tabs"].toArray()) {
Session::WebView tab{
tabData.toObject()["profile"].toString(),
tabData.toObject()["url"].toString(),
QByteArray::fromBase64(tabData.toObject()["history"].toString().toLatin1())
};
subwindow.tabs.append(tab);
}
window.subwindows.append(subwindow);
}
windows.append(window);
}
return windows;
}
|