Falkon Develop
Cross-platform Qt-based web browser
recoveryjsobject.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18
19#include "recoveryjsobject.h"
20#include "mainapplication.h"
21#include "webpage.h"
22#include "tabbedwebview.h"
23#include "browserwindow.h"
24#include "qztools.h"
25#include "iconprovider.h"
26
27#include <QJsonObject>
28
30 : QObject()
31 , m_manager(manager)
32 , m_page(nullptr)
33{
34}
35
37{
38 Q_ASSERT(page);
39
40 m_page = page;
41}
42
44{
45 QJsonArray out;
46
47 int i = 0;
48 const auto windows = m_manager->restoreData().windows;
49 for (const BrowserWindow::SavedWindow &w : windows) {
50 int j = 0;
51 QJsonArray tabs;
52 const auto windowTabs = w.tabs;
53 for (const WebTab::SavedTab &t : windowTabs) {
54 const QIcon icon = t.icon.isNull() ? IconProvider::emptyWebIcon() : t.icon;
55 QJsonObject tab;
56 tab[QSL("tab")] = j;
57 tab[QSL("icon")] = QzTools::pixmapToDataUrl(icon.pixmap(16)).toString();
58 tab[QSL("title")] = t.title;
59 tab[QSL("url")] = t.url.toString();
60 tab[QSL("pinned")] = t.isPinned;
61 tab[QSL("current")] = w.currentTab == j;
62 tabs.append(tab);
63 j++;
64 }
65
66 QJsonObject window;
67 window[QSL("window")] = i++;
68 window[QSL("tabs")] = tabs;
69 out.append(window);
70 }
71
72 return out;
73}
74
76{
77 auto *view = qobject_cast<TabbedWebView*>(m_page->view());
78 if (!view) {
79 return;
80 }
81
82 if (view->browserWindow()->tabCount() > 1) {
83 view->closeView();
84 } else {
85 auto *oldWindow = view->browserWindow();
86 mApp->createWindow(Qz::BW_NewWindow);
87 oldWindow->close();
88 }
89
90 mApp->restoreManager()->clearRestoreData();
91 mApp->destroyRestoreManager();
92}
93
94void RecoveryJsObject::restoreSession(const QStringList &excludeWin, const QStringList &excludeTab)
95{
96 Q_ASSERT(excludeWin.size() == excludeTab.size());
97
98 // This assumes that excludeWin and excludeTab are sorted in descending order
99
100 RestoreData data = m_manager->restoreData();
101
102 for (int i = 0; i < excludeWin.size(); ++i) {
103 int win = excludeWin.at(i).toInt();
104 int tab = excludeTab.at(i).toInt();
105
106 if (!QzTools::containsIndex(data.windows, win) || !QzTools::containsIndex(data.windows.at(win).tabs, tab))
107 continue;
108
109 BrowserWindow::SavedWindow &wd = data.windows[win];
110
111 wd.tabs.remove(tab);
112 if (wd.currentTab >= tab)
113 --wd.currentTab;
114
115 if (wd.tabs.isEmpty()) {
116 data.windows.remove(win);
117 continue;
118 }
119
120 if (wd.currentTab < 0)
121 wd.currentTab = wd.tabs.size() - 1;
122 }
123
124 if (mApp->restoreSession(nullptr, data)) {
125 closeTab();
126 } else {
128 }
129}
130
131void RecoveryJsObject::closeTab()
132{
133 auto *view = qobject_cast<TabbedWebView*>(m_page->view());
134 if (!view) {
135 return;
136 }
137
138 if (view->browserWindow()->tabCount() > 1) {
139 view->closeView();
140 } else {
141 view->browserWindow()->close();
142 }
143}
static QIcon emptyWebIcon()
static bool containsIndex(const T &container, int index)
Definition: qztools.h:95
static QUrl pixmapToDataUrl(const QPixmap &pix)
Definition: qztools.cpp:83
void setPage(WebPage *page)
void restoreSession(const QStringList &excludeWin, const QStringList &excludeTab)
QJsonArray restoreData
RecoveryJsObject(RestoreManager *manager)
RestoreData restoreData() const
WebView * view() const
Definition: webpage.cpp:140
#define mApp
@ BW_NewWindow
Definition: qzcommon.h:67
t
Definition: i18n.py:27
i
Definition: i18n.py:23
#define QSL(x)
Definition: qzcommon.h:40
QVector< WebTab::SavedTab > tabs
Definition: browserwindow.h:68
QVector< BrowserWindow::SavedWindow > windows