1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

BrowserWindow: Add SavedWindow struct and use it for saving/restoring

This commit is contained in:
David Rosca 2018-01-02 12:56:10 +01:00
parent 16fcae4202
commit b3bd08c5e4
9 changed files with 168 additions and 170 deletions

View File

@ -51,7 +51,6 @@
#include "navigationcontainer.h"
#include "settings.h"
#include "qzsettings.h"
#include "webtab.h"
#include "speeddial.h"
#include "menubar.h"
#include "bookmarkstools.h"
@ -86,6 +85,109 @@
#include <xcb/xcb_atom.h>
#endif
BrowserWindow::SavedWindow::SavedWindow()
{
}
BrowserWindow::SavedWindow::SavedWindow(BrowserWindow *window)
{
windowState = window->isFullScreen() ? QByteArray() : window->saveState();
windowGeometry = window->saveGeometry();
#ifdef QZ_WS_X11
virtualDesktop = window->getCurrentVirtualDesktop();
#endif
const int tabsCount = window->tabWidget()->count();
tabs.reserve(tabsCount);
for (int i = 0; i < tabsCount; ++i) {
TabbedWebView *webView = window->weView(i);
if (!webView) {
continue;
}
WebTab* webTab = webView->webTab();
if (!webTab) {
continue;
}
WebTab::SavedTab tab(webTab);
if (!tab.isValid()) {
continue;
}
if (webTab->isCurrentTab()) {
currentTab = tabs.size();
}
tabs.append(tab);
}
}
bool BrowserWindow::SavedWindow::isValid() const
{
return currentTab > -1;
}
void BrowserWindow::SavedWindow::clear()
{
windowState.clear();
windowGeometry.clear();
virtualDesktop = -1;
currentTab = -1;
tabs.clear();
}
QDataStream &operator<<(QDataStream &stream, const BrowserWindow::SavedWindow &window)
{
QByteArray tabsState;
QByteArray windowState;
#ifdef QZ_WS_X11
QDataStream stream1(&windowState, QIODevice::WriteOnly);
stream1 << window.windowState;
stream1 << window.virtualDesktop;
#else
windowState = window.windowState;
#endif
QDataStream stream2(&tabsState, QIODevice::WriteOnly);
stream2 << window.tabs.count();
for (const WebTab::SavedTab &tab : qAsConst(window.tabs)) {
stream2 << tab;
}
stream2 << window.currentTab;
stream << tabsState;
stream << windowState;
return stream;
}
QDataStream &operator>>(QDataStream &stream, BrowserWindow::SavedWindow &window)
{
QByteArray tabsState;
QByteArray windowState;
stream >> tabsState;
stream >> windowState;
#ifdef QZ_WS_X11
QDataStream stream1(&windowState, QIODevice::ReadOnly);
stream1 >> window.windowState;
stream1 >> window.virtualDesktop;
#else
window.windowState = windowState;
#endif
int tabsCount = -1;
QDataStream stream2(&tabsState, QIODevice::ReadOnly);
stream2 >> tabsCount;
window.tabs.reserve(tabsCount);
for (int i = 0; i < tabsCount; ++i) {
WebTab::SavedTab tab;
stream2 >> tab;
window.tabs.append(tab);
}
stream2 >> window.currentTab;
return stream;
}
BrowserWindow::BrowserWindow(Qz::BrowserWindowType type, const QUrl &startUrl)
: QMainWindow(0)
, m_startUrl(startUrl)
@ -886,11 +988,15 @@ void BrowserWindow::addDeleteOnCloseWidget(QWidget* widget)
}
}
void BrowserWindow::restoreWindowState(const RestoreManager::WindowData &d)
void BrowserWindow::restoreWindow(const SavedWindow &window)
{
restoreState(d.windowState);
restoreState(window.windowState);
restoreGeometry(window.windowGeometry);
#ifdef QZ_WS_X11
moveToVirtualDesktop(window.virtualDesktop);
#endif
show(); // Window has to be visible before adding QWebEngineView's
m_tabWidget->restoreState(d.tabsState, d.currentTab);
m_tabWidget->restoreState(window.tabs, window.currentTab);
updateStartupFocus();
}
@ -1426,39 +1532,6 @@ void BrowserWindow::closeTab()
}
}
QByteArray BrowserWindow::saveState(int version) const
{
#ifdef QZ_WS_X11
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << QMainWindow::saveState(version);
stream << getCurrentVirtualDesktop();
return data;
#else
return QMainWindow::saveState(version);
#endif
}
bool BrowserWindow::restoreState(const QByteArray &state, int version)
{
#ifdef QZ_WS_X11
QByteArray windowState;
int desktopId = -1;
QDataStream stream(state);
stream >> windowState;
stream >> desktopId;
moveToVirtualDesktop(desktopId);
return QMainWindow::restoreState(windowState, version);
#else
return QMainWindow::restoreState(state, version);
#endif
}
#ifdef QZ_WS_X11
int BrowserWindow::getCurrentVirtualDesktop() const
{

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -22,7 +22,7 @@
#include <QPointer>
#include <QUrl>
#include "restoremanager.h"
#include "webtab.h"
#include "qzcommon.h"
class QLabel;
@ -41,8 +41,8 @@ class BookmarksMenu;
class BookmarksToolbar;
class AutoFill;
class MainApplication;
class WebTab;
class WebView;
class WebPage;
class AdBlockIcon;
class SideBar;
class SideBarManager;
@ -58,13 +58,30 @@ class FALKON_EXPORT BrowserWindow : public QMainWindow
Q_OBJECT
public:
struct SavedWindow {
QByteArray windowState;
QByteArray windowGeometry;
int virtualDesktop = -1;
int currentTab = -1;
QVector<WebTab::SavedTab> tabs;
SavedWindow();
SavedWindow(BrowserWindow *window);
bool isValid() const;
void clear();
friend QUPZILLA_EXPORT QDataStream &operator<<(QDataStream &stream, const SavedWindow &window);
friend QUPZILLA_EXPORT QDataStream &operator>>(QDataStream &stream, SavedWindow &window);
};
explicit BrowserWindow(Qz::BrowserWindowType type, const QUrl &url = QUrl());
~BrowserWindow();
void setStartTab(WebTab* tab);
void setStartPage(WebPage* page);
void restoreWindowState(const RestoreManager::WindowData &d);
void restoreWindow(const SavedWindow &window);
void saveSideBarWidth();
bool fullScreenNavigationVisible() const;
@ -85,9 +102,6 @@ public:
SideBar* addSideBar();
QByteArray saveState(int version = 0) const;
bool restoreState(const QByteArray &state, int version = 0);
TabbedWebView* weView() const;
TabbedWebView* weView(int index) const;

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -423,28 +423,15 @@ void MainApplication::openSession(BrowserWindow* window, RestoreData &restoreDat
if (window->tabWidget()->normalTabsCount() > 1) {
// This can only happen when recovering crashed session!
//
// Don't restore tabs in current window as user already opened
// some new tabs.
BrowserWindow* newWin = createWindow(Qz::BW_OtherRestoredWindow);
newWin->restoreWindowState(restoreData.at(0));
restoreData.remove(0);
}
else {
// QTabWidget::count() - count of tabs is not updated after closing
// recovery tab ...
// update: it seems with ComboTabBar QTabWidget::count() is updated,
// we add pinnedTabCounts to currentTab!
int tabCount = window->tabWidget()->pinnedTabsCount();
RestoreManager::WindowData data = restoreData.at(0);
data.currentTab += tabCount;
restoreData.remove(0);
window->restoreWindowState(data);
// Don't restore tabs in current window as user already opened some new tabs.
createWindow(Qz::BW_OtherRestoredWindow)->restoreWindow(restoreData.takeAt(0));
} else {
window->restoreWindow(restoreData.takeAt(0));
}
foreach (const RestoreManager::WindowData &data, restoreData) {
foreach (const BrowserWindow::SavedWindow &data, restoreData) {
BrowserWindow* window = createWindow(Qz::BW_OtherRestoredWindow);
window->restoreWindowState(data);
window->restoreWindow(data);
}
restoreOverrideCursor();
@ -725,14 +712,8 @@ QByteArray MainApplication::saveState() const
stream << Qz::sessionVersion;
stream << m_windows.count();
foreach (BrowserWindow* w, m_windows) {
stream << w->tabWidget()->saveState();
if (w->isFullScreen()) {
stream << QByteArray();
}
else {
stream << w->saveState();
}
for (BrowserWindow *window : qAsConst(m_windows)) {
stream << BrowserWindow::SavedWindow(window);
}
return data;

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2015-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -45,10 +45,10 @@ QJsonArray RecoveryJsObject::restoreData() const
QJsonArray out;
int i = 0;
Q_FOREACH (const RestoreManager::WindowData &w, m_manager->restoreData()) {
Q_FOREACH (const BrowserWindow::SavedWindow &w, m_manager->restoreData()) {
int j = 0;
QJsonArray tabs;
Q_FOREACH (const WebTab::SavedTab &t, w.tabsState) {
Q_FOREACH (const WebTab::SavedTab &t, w.tabs) {
const QIcon icon = t.icon.isNull() ? IconProvider::emptyWebIcon() : t.icon;
QJsonObject tab;
tab[QSL("tab")] = j;
@ -93,29 +93,29 @@ void RecoveryJsObject::restoreSession(const QStringList &excludeWin, const QStri
int win = excludeWin.at(i).toInt();
int tab = excludeTab.at(i).toInt();
if (!QzTools::containsIndex(data, win) || !QzTools::containsIndex(data.at(win).tabsState, tab))
if (!QzTools::containsIndex(data, win) || !QzTools::containsIndex(data.at(win).tabs, tab))
continue;
RestoreManager::WindowData &wd = data[win];
BrowserWindow::SavedWindow &wd = data[win];
wd.tabsState.remove(tab);
wd.tabs.remove(tab);
if (wd.currentTab >= tab)
--wd.currentTab;
if (wd.tabsState.isEmpty()) {
if (wd.tabs.isEmpty()) {
data.remove(win);
continue;
}
if (wd.currentTab < 0)
wd.currentTab = wd.tabsState.size() - 1;
wd.currentTab = wd.tabs.size() - 1;
}
BrowserWindow *window = getBrowserWindow();
if (!window)
return;
if (!mApp->restoreSession(window , data))
if (!mApp->restoreSession(window, data))
startNewSession();
}

View File

@ -1,7 +1,7 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2014 Franz Fellner <alpine.art.de@googlemail.com>
* David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -49,7 +49,7 @@ QObject *RestoreManager::recoveryObject(WebPage *page)
return m_recoveryObject;
}
void RestoreManager::createFromFile(const QString &file, QVector<WindowData> &data)
void RestoreManager::createFromFile(const QString &file, RestoreData &data)
{
if (!QFile::exists(file)) {
return;
@ -68,40 +68,12 @@ void RestoreManager::createFromFile(const QString &file, QVector<WindowData> &da
int windowCount;
stream >> windowCount;
data.reserve(windowCount);
for (int win = 0; win < windowCount; ++win) {
QByteArray tabState;
QByteArray windowState;
stream >> tabState;
stream >> windowState;
WindowData wd;
wd.windowState = windowState;
QDataStream tabStream(tabState);
if (tabStream.atEnd()) {
continue;
}
QVector<WebTab::SavedTab> tabs;
int tabListCount = 0;
tabStream >> tabListCount;
for (int i = 0; i < tabListCount; ++i) {
WebTab::SavedTab tab;
tabStream >> tab;
tabs.append(tab);
}
if (tabs.count() == 0)
continue;
wd.tabsState = tabs;
int currentTab;
tabStream >> currentTab;
wd.currentTab = currentTab;
data.append(wd);
for (int i = 0; i < windowCount; ++i) {
BrowserWindow::SavedWindow window;
stream >> window;
data.append(window);
}
}

View File

@ -1,7 +1,7 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2014 Franz Fellner <alpine.art.de@googlemail.com>
* David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -19,40 +19,32 @@
#ifndef RESTOREMANAGER_H
#define RESTOREMANAGER_H
#include "webtab.h"
#include "qzcommon.h"
#include "browserwindow.h"
class WebPage;
class RecoveryJsObject;
using RestoreData = QVector<BrowserWindow::SavedWindow>;
class FALKON_EXPORT RestoreManager
{
public:
struct WindowData {
int currentTab;
QByteArray windowState;
QVector<WebTab::SavedTab> tabsState;
};
explicit RestoreManager(const QString &file);
virtual ~RestoreManager();
bool isValid() const;
QVector<RestoreManager::WindowData> restoreData() const;
RestoreData restoreData() const;
QObject *recoveryObject(WebPage *page);
static void createFromFile(const QString &file, QVector<RestoreManager::WindowData> &data);
static void createFromFile(const QString &file, RestoreData &data);
private:
void createFromFile(const QString &file);
RecoveryJsObject *m_recoveryObject;
QVector<RestoreManager::WindowData> m_data;
RestoreData m_data;
};
typedef QVector<RestoreManager::WindowData> RestoreData;
// Hint to QVector to use std::realloc on item moving
Q_DECLARE_TYPEINFO(RestoreManager::WindowData, Q_MOVABLE_TYPE);
#endif // RESTOREMANAGER_H

View File

@ -1,6 +1,7 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2017 Razi Alavizadeh <s.r.alavizadeh@gmail.com>
* Copyright (C) 2017 Razi Alavizadeh <s.r.alavizadeh@gmail.com>
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -86,7 +87,7 @@ void SessionManager::openSession(QString sessionFilePath, SessionFlags flags)
return;
}
QVector<RestoreManager::WindowData> sessionData;
RestoreData sessionData;
RestoreManager::createFromFile(sessionFilePath, sessionData);
if (sessionData.isEmpty())
@ -278,7 +279,7 @@ void SessionManager::fillSessionsMetaDataListIfNeeded()
for (int i = 0; i < sessionFiles.size(); ++i) {
const QFileInfo &fileInfo = sessionFiles.at(i);
QVector<RestoreManager::WindowData> data;
RestoreData data;
RestoreManager::createFromFile(fileInfo.absoluteFilePath(), data);
if (data.isEmpty())

View File

@ -765,40 +765,6 @@ QList<WebTab*> TabWidget::allTabs(bool withPinned)
return allTabs;
}
QByteArray TabWidget::saveState()
{
int currentTabIndex = 0;
QVector<WebTab::SavedTab> tabList;
for (int i = 0; i < count(); ++i) {
WebTab* webTab = weTab(i);
if (!webTab)
continue;
WebTab::SavedTab tab(webTab);
if (!tab.isValid())
continue;
tabList.append(tab);
if (webTab->isCurrentTab())
currentTabIndex = tabList.size() - 1;
}
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << tabList.count();
foreach (const WebTab::SavedTab &tab, tabList) {
stream << tab;
}
stream << currentTabIndex;
return data;
}
bool TabWidget::restoreState(const QVector<WebTab::SavedTab> &tabs, int currentTab)
{
for (int i = 0; i < tabs.size(); ++i) {

View File

@ -69,7 +69,6 @@ public:
explicit TabWidget(BrowserWindow* mainclass, QWidget* parent = 0);
~TabWidget();
QByteArray saveState();
bool restoreState(const QVector<WebTab::SavedTab> &tabs, int currentTab);
void closeRecoveryTab();