1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-21 03:06:34 +01:00

RestoreManager: Make RestoreData into a struct with stream operators

This makes it possible to extend it while keeping backwards compatibility.
This commit is contained in:
David Rosca 2018-01-03 11:39:49 +01:00
parent 150c576661
commit 482f446712
5 changed files with 72 additions and 27 deletions

View File

@ -429,12 +429,12 @@ void MainApplication::openSession(BrowserWindow* window, RestoreData &restoreDat
if (window->tabWidget()->normalTabsCount() > 1) { if (window->tabWidget()->normalTabsCount() > 1) {
// This can only happen when recovering crashed session! // This can only happen when recovering crashed session!
// Don't restore tabs in current window as user already opened some new tabs. // Don't restore tabs in current window as user already opened some new tabs.
createWindow(Qz::BW_OtherRestoredWindow)->restoreWindow(restoreData.takeAt(0)); createWindow(Qz::BW_OtherRestoredWindow)->restoreWindow(restoreData.windows.takeAt(0));
} else { } else {
window->restoreWindow(restoreData.takeAt(0)); window->restoreWindow(restoreData.windows.takeAt(0));
} }
foreach (const BrowserWindow::SavedWindow &data, restoreData) { foreach (const BrowserWindow::SavedWindow &data, restoreData.windows) {
BrowserWindow* window = createWindow(Qz::BW_OtherRestoredWindow); BrowserWindow* window = createWindow(Qz::BW_OtherRestoredWindow);
window->restoreWindow(data); window->restoreWindow(data);
} }
@ -444,7 +444,7 @@ void MainApplication::openSession(BrowserWindow* window, RestoreData &restoreDat
bool MainApplication::restoreSession(BrowserWindow* window, RestoreData restoreData) bool MainApplication::restoreSession(BrowserWindow* window, RestoreData restoreData)
{ {
if (m_isPrivate || restoreData.isEmpty()) { if (m_isPrivate || !restoreData.isValid()) {
return false; return false;
} }
@ -724,15 +724,17 @@ void MainApplication::postLaunch()
QByteArray MainApplication::saveState() const QByteArray MainApplication::saveState() const
{ {
RestoreData restoreData;
restoreData.windows.reserve(m_windows.count());
for (BrowserWindow *window : qAsConst(m_windows)) {
restoreData.windows.append(BrowserWindow::SavedWindow(window));
}
QByteArray data; QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly); QDataStream stream(&data, QIODevice::WriteOnly);
stream << Qz::sessionVersion; stream << Qz::sessionVersion;
stream << m_windows.count(); stream << restoreData;
for (BrowserWindow *window : qAsConst(m_windows)) {
stream << BrowserWindow::SavedWindow(window);
}
return data; return data;
} }

View File

@ -45,7 +45,7 @@ QJsonArray RecoveryJsObject::restoreData() const
QJsonArray out; QJsonArray out;
int i = 0; int i = 0;
Q_FOREACH (const BrowserWindow::SavedWindow &w, m_manager->restoreData()) { Q_FOREACH (const BrowserWindow::SavedWindow &w, m_manager->restoreData().windows) {
int j = 0; int j = 0;
QJsonArray tabs; QJsonArray tabs;
Q_FOREACH (const WebTab::SavedTab &t, w.tabs) { Q_FOREACH (const WebTab::SavedTab &t, w.tabs) {
@ -93,17 +93,17 @@ void RecoveryJsObject::restoreSession(const QStringList &excludeWin, const QStri
int win = excludeWin.at(i).toInt(); int win = excludeWin.at(i).toInt();
int tab = excludeTab.at(i).toInt(); int tab = excludeTab.at(i).toInt();
if (!QzTools::containsIndex(data, win) || !QzTools::containsIndex(data.at(win).tabs, tab)) if (!QzTools::containsIndex(data.windows, win) || !QzTools::containsIndex(data.windows.at(win).tabs, tab))
continue; continue;
BrowserWindow::SavedWindow &wd = data[win]; BrowserWindow::SavedWindow &wd = data.windows[win];
wd.tabs.remove(tab); wd.tabs.remove(tab);
if (wd.currentTab >= tab) if (wd.currentTab >= tab)
--wd.currentTab; --wd.currentTab;
if (wd.tabs.isEmpty()) { if (wd.tabs.isEmpty()) {
data.remove(win); data.windows.remove(win);
continue; continue;
} }

View File

@ -22,6 +22,48 @@
#include <QFile> #include <QFile>
static const int restoreDataVersion = 0;
bool RestoreData::isValid() const
{
return !windows.isEmpty();
}
void RestoreData::clear()
{
windows.clear();
}
QDataStream &operator<<(QDataStream &stream, const RestoreData &data)
{
stream << data.windows.count();
for (const BrowserWindow::SavedWindow &window : qAsConst(data.windows)) {
stream << window;
}
stream << restoreDataVersion;
return stream;
}
QDataStream &operator>>(QDataStream &stream, RestoreData &data)
{
int windowCount;
stream >> windowCount;
data.windows.reserve(windowCount);
for (int i = 0; i < windowCount; ++i) {
BrowserWindow::SavedWindow window;
stream >> window;
data.windows.append(window);
}
int version;
stream >> version;
return stream;
}
RestoreManager::RestoreManager(const QString &file) RestoreManager::RestoreManager(const QString &file)
: m_recoveryObject(new RecoveryJsObject(this)) : m_recoveryObject(new RecoveryJsObject(this))
{ {
@ -40,7 +82,7 @@ RestoreData RestoreManager::restoreData() const
bool RestoreManager::isValid() const bool RestoreManager::isValid() const
{ {
return !m_data.isEmpty(); return m_data.isValid();
} }
QObject *RestoreManager::recoveryObject(WebPage *page) QObject *RestoreManager::recoveryObject(WebPage *page)
@ -51,22 +93,14 @@ QObject *RestoreManager::recoveryObject(WebPage *page)
static void loadCurrentVersion(QDataStream &stream, RestoreData &data) static void loadCurrentVersion(QDataStream &stream, RestoreData &data)
{ {
int windowCount; stream >> data;
stream >> windowCount;
data.reserve(windowCount);
for (int i = 0; i < windowCount; ++i) {
BrowserWindow::SavedWindow window;
stream >> window;
data.append(window);
}
} }
static void loadVersion3(QDataStream &stream, RestoreData &data) static void loadVersion3(QDataStream &stream, RestoreData &data)
{ {
int windowCount; int windowCount;
stream >> windowCount; stream >> windowCount;
data.reserve(windowCount); data.windows.reserve(windowCount);
for (int i = 0; i < windowCount; ++i) { for (int i = 0; i < windowCount; ++i) {
QByteArray tabsState; QByteArray tabsState;
@ -96,7 +130,7 @@ static void loadVersion3(QDataStream &stream, RestoreData &data)
} }
stream2 >> window.currentTab; stream2 >> window.currentTab;
data.append(window); data.windows.append(window);
} }
} }

View File

@ -25,7 +25,16 @@
class WebPage; class WebPage;
class RecoveryJsObject; class RecoveryJsObject;
using RestoreData = QVector<BrowserWindow::SavedWindow>; struct QUPZILLA_EXPORT RestoreData
{
QVector<BrowserWindow::SavedWindow> windows;
bool isValid() const;
void clear();
friend QUPZILLA_EXPORT QDataStream &operator<<(QDataStream &stream, const RestoreData &data);
friend QUPZILLA_EXPORT QDataStream &operator>>(QDataStream &stream, RestoreData &data);
};
class FALKON_EXPORT RestoreManager class FALKON_EXPORT RestoreManager
{ {

View File

@ -90,7 +90,7 @@ void SessionManager::openSession(QString sessionFilePath, SessionFlags flags)
RestoreData sessionData; RestoreData sessionData;
RestoreManager::createFromFile(sessionFilePath, sessionData); RestoreManager::createFromFile(sessionFilePath, sessionData);
if (sessionData.isEmpty()) if (!sessionData.isValid())
return; return;
BrowserWindow* window = mApp->getWindow(); BrowserWindow* window = mApp->getWindow();