From 5feb5d606a272639653ee89e2831cf7b9a3ec4b8 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Thu, 8 Oct 2015 12:54:45 +0200 Subject: [PATCH] ProfileManager: Fix losing pinned tabs when upgrading to 1.8.7 --- src/lib/app/profilemanager.cpp | 134 +++++++++++++++++++++++++++++++++ src/lib/app/profilemanager.h | 1 + 2 files changed, 135 insertions(+) diff --git a/src/lib/app/profilemanager.cpp b/src/lib/app/profilemanager.cpp index d2fed5700..6cc10a7c9 100644 --- a/src/lib/app/profilemanager.cpp +++ b/src/lib/app/profilemanager.cpp @@ -216,6 +216,8 @@ void ProfileManager::updateProfile(const QString ¤t, const QString &profil // 1.8.x bugfix releases if (prof >= Updater::Version("1.8.0") && prof < Updater::Version("1.9.0")) { + if (prof == Updater::Version("1.8.6")) + update186(); return; } @@ -354,3 +356,135 @@ void ProfileManager::update160() { // Nothing to upgrade } + +static QVector pinnedWebTabs(const QString &file) +{ + QVector out; + + QFile f(file); + f.open(QIODevice::ReadOnly); + QByteArray sd = f.readAll(); + f.close(); + + QDataStream stream(&sd, QIODevice::ReadOnly); + if (stream.atEnd()) + return out; + + int version; + stream >> version; + + QStringList pinnedTabs; + stream >> pinnedTabs; + QList tabHistory; + stream >> tabHistory; + + for (int i = 0; i < pinnedTabs.count(); ++i) { + WebTab::SavedTab tab; + tab.url = QUrl(pinnedTabs.at(i)); + tab.history = tabHistory.at(i); + tab.isPinned = true; + out.append(tab); + } + + return out; +} + +static QVector restoreData(const QString &file) +{ + QVector out; + + QFile recoveryFile(file); + recoveryFile.open(QIODevice::ReadOnly); + + QDataStream stream(&recoveryFile); + if (stream.atEnd()) + return out; + + int version; + stream >> version; + + int windowCount; + stream >> windowCount; + + for (int win = 0; win < windowCount; ++win) { + QByteArray tabState; + QByteArray windowState; + stream >> tabState; + stream >> windowState; + + RestoreManager::WindowData wd; + wd.windowState = windowState; + + QDataStream tabStream(tabState); + if (tabStream.atEnd()) { + continue; + } + + QVector tabs; + int tabListCount = 0; + tabStream >> tabListCount; + for (int i = 0; i < tabListCount; ++i) { + WebTab::SavedTab tab; + tabStream >> tab; + tabs.append(tab); + } + wd.tabsState = tabs; + + int currentTab; + tabStream >> currentTab; + wd.currentTab = currentTab; + + out.append(wd); + } + + return out; +} + +static void saveRestoreData(const QVector &data, const QString &file) +{ + QByteArray sessionData; + QDataStream stream(&sessionData, QIODevice::WriteOnly); + + stream << Qz::sessionVersion; + stream << data.count(); + + foreach (const RestoreManager::WindowData w, data) { + QByteArray tabData; + QDataStream tabStream(&tabData, QIODevice::WriteOnly); + tabStream << w.tabsState.count(); + foreach (const WebTab::SavedTab &tab, w.tabsState) { + tabStream << tab; + } + tabStream << w.currentTab; + + stream << tabData; + stream << w.windowState; + } + + QFile f(file); + f.open(QIODevice::WriteOnly); + f.write(sessionData); + f.close(); +} + +void ProfileManager::update186() +{ + std::cout << "QupZilla: Upgrading profile version from 1.8.6..." << std::endl; + + const QString pinnedTabsPath = DataPaths::currentProfilePath() + QL1S("/pinnedtabs.dat"); + const QString sessionPath = DataPaths::currentProfilePath() + QL1S("/session.dat"); + + if (!QFile::exists(pinnedTabsPath) || !QFile::exists(sessionPath)) + return; + + const QVector pinnedTabs = pinnedWebTabs(pinnedTabsPath); + if (pinnedTabs.isEmpty()) + return; + + QVector data = restoreData(sessionPath); + if (data.isEmpty() || data.at(0).tabsState.isEmpty()) + return; + + data[0].tabsState = pinnedTabs + data[0].tabsState; + saveRestoreData(data, sessionPath); +} diff --git a/src/lib/app/profilemanager.h b/src/lib/app/profilemanager.h index 2594c2674..7fef9c097 100644 --- a/src/lib/app/profilemanager.h +++ b/src/lib/app/profilemanager.h @@ -60,6 +60,7 @@ class ProfileManager void update130(); void update140(); void update160(); + void update186(); bool m_databaseConnected; };