Falkon Develop
Cross-platform Qt-based web browser
restoremanager.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 Franz Fellner <alpine.art.de@googlemail.com>
4* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
5*
6* This program is free software: you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation, either version 3 of the License, or
9* (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program. If not, see <http://www.gnu.org/licenses/>.
18* ============================================================ */
19#include "restoremanager.h"
20#include "recoveryjsobject.h"
21#include "datapaths.h"
22
23#include <QFile>
24
25static const int restoreDataVersion = 2;
26
28{
29 for (const BrowserWindow::SavedWindow &window : std::as_const(windows)) {
30 if (!window.isValid()) {
31 return false;
32 }
33 }
34 return !windows.isEmpty();
35}
36
38{
39 windows.clear();
40 crashedSession.clear();
41 closedWindows.clear();
42}
43
44QDataStream &operator<<(QDataStream &stream, const RestoreData &data)
45{
46 stream << static_cast<int>(data.windows.count());
47 for (const BrowserWindow::SavedWindow &window : std::as_const(data.windows)) {
48 stream << window;
49 }
50
51 stream << restoreDataVersion;
52 stream << data.crashedSession;
53 stream << data.closedWindows;
54
55 return stream;
56}
57
58QDataStream &operator>>(QDataStream &stream, RestoreData &data)
59{
60 int windowCount;
61 stream >> windowCount;
62 data.windows.reserve(windowCount);
63
64 for (int i = 0; i < windowCount; ++i) {
66 stream >> window;
67 data.windows.append(window);
68 }
69
70 int version;
71 stream >> version;
72
73 if (version >= 1) {
74 stream >> data.crashedSession;
75 }
76
77 if (version >= 2) {
78 stream >> data.closedWindows;
79 }
80
81 return stream;
82}
83
85 : m_recoveryObject(new RecoveryJsObject(this))
86{
87 createFromFile(file);
88}
89
91{
92 delete m_recoveryObject;
93}
94
96{
97 return m_data;
98}
99
101{
102 QByteArray crashedSession = m_data.crashedSession;
103 m_data.clear();
104 QDataStream stream(&crashedSession, QIODevice::ReadOnly);
105 stream >> m_data;
106}
107
109{
110 return m_data.isValid();
111}
112
114{
115 m_recoveryObject->setPage(page);
116 return m_recoveryObject;
117}
118
119static void loadCurrentVersion(QDataStream &stream, RestoreData &data)
120{
121 stream >> data;
122}
123
124static void loadVersion3(QDataStream &stream, RestoreData &data)
125{
126 int windowCount;
127 stream >> windowCount;
128 data.windows.reserve(windowCount);
129
130 for (int i = 0; i < windowCount; ++i) {
131 QByteArray tabsState;
132 QByteArray windowState;
133
134 stream >> tabsState;
135 stream >> windowState;
136
138
139#ifdef QZ_WS_X11
140 QDataStream stream1(&windowState, QIODevice::ReadOnly);
141 stream1 >> window.windowState;
142 stream1 >> window.virtualDesktop;
143#else
144 window.windowState = windowState;
145#endif
146
147 int tabsCount = -1;
148 QDataStream stream2(&tabsState, QIODevice::ReadOnly);
149 stream2 >> tabsCount;
150 window.tabs.reserve(tabsCount);
151 for (int i = 0; i < tabsCount; ++i) {
153 stream2 >> tab;
154 window.tabs.append(tab);
155 }
156 stream2 >> window.currentTab;
157
158 data.windows.append(window);
159 }
160}
161
162// static
163bool RestoreManager::validateFile(const QString &file)
164{
165 RestoreData data;
166 createFromFile(file, data);
167 return data.isValid();
168}
169
170// static
171void RestoreManager::createFromFile(const QString &file, RestoreData &data)
172{
173 if (!QFile::exists(file)) {
174 return;
175 }
176
177 QFile recoveryFile(file);
178 if (!recoveryFile.open(QIODevice::ReadOnly)) {
179 return;
180 }
181
182 QDataStream stream(&recoveryFile);
183
184 int version;
185 stream >> version;
186
187 if (version == Qz::sessionVersion) {
188 loadCurrentVersion(stream, data);
189 } else if (version == 0x0003 || version == (0x0003 | 0x050000)) {
190 loadVersion3(stream, data);
191 } else {
192 qWarning() << "Unsupported session file version" << version;
193 }
194}
195
196void RestoreManager::createFromFile(const QString &file)
197{
198 createFromFile(file, m_data);
199}
void setPage(WebPage *page)
RestoreManager(const QString &file)
static bool validateFile(const QString &file)
RestoreData restoreData() const
bool isValid() const
QObject * recoveryObject(WebPage *page)
static void createFromFile(const QString &file, RestoreData &data)
virtual ~RestoreManager()
const int sessionVersion
Definition: qzcommon.cpp:23
i
Definition: i18n.py:23
QDataStream & operator<<(QDataStream &stream, const RestoreData &data)
QDataStream & operator>>(QDataStream &stream, RestoreData &data)
QVector< WebTab::SavedTab > tabs
Definition: browserwindow.h:68
QVector< BrowserWindow::SavedWindow > windows
QByteArray crashedSession
bool isValid() const
QByteArray closedWindows