Falkon Develop
Cross-platform Qt-based web browser
closedwindowsmanager.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 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* ============================================================ */
19#include "mainapplication.h"
20#include "tabbedwebview.h"
21#include "qztools.h"
22
23#include <QAction>
24
26 : QObject(parent)
27{
28}
29
31{
32 return !m_closedWindows.isEmpty();
33}
34
35QVector<ClosedWindowsManager::Window> ClosedWindowsManager::closedWindows() const
36{
37 return m_closedWindows;
38}
39
41{
42 if (mApp->isPrivate() || mApp->windowCount() == 1 || !window->weView()) {
43 return;
44 }
45
46 Window closedWindow;
47 closedWindow.icon = window->weView()->icon();
48 closedWindow.title = window->weView()->title();
49 closedWindow.windowState = BrowserWindow::SavedWindow(window);
50 m_closedWindows.prepend(closedWindow);
51}
52
54{
55 Window window;
56 if (!m_closedWindows.isEmpty()) {
57 window = m_closedWindows.takeFirst();
58 }
59 return window;
60}
61
63{
64 Window window;
65 if (QzTools::containsIndex(m_closedWindows, index)) {
66 window = m_closedWindows.takeAt(index);
67 }
68 return window;
69}
70
72{
73 Window window;
74 auto *act = qobject_cast<QAction*>(sender());
75 if (act) {
76 window = takeClosedWindowAt(act->data().toInt());
77 } else {
78 window = takeLastClosedWindow();
79 }
80
81 if (!window.isValid()) {
82 return;
83 }
84
85 mApp->createWindow(Qz::BW_OtherRestoredWindow)->restoreWindow(window.windowState);
86}
87
89{
90 const int count = m_closedWindows.count();
91 for (int i = 0; i < count; ++i) {
93 }
94}
95
97{
98 m_closedWindows.clear();
99}
100
101static const int closedWindowsVersion = 1;
102
104{
105 QByteArray data;
106 QDataStream stream(&data, QIODevice::WriteOnly);
107
108 stream << closedWindowsVersion;
109
110 // Only save last 3 windows
111 const int windowCount = qBound(0, m_closedWindows.count(), 3);
112 stream << windowCount;
113
114 for (int i = 0; i < windowCount; ++i) {
115 stream << m_closedWindows.at(i).windowState;
116 }
117
118 return data;
119}
120
122{
123 QDataStream stream(state);
124
125 int version;
126 stream >> version;
127
128 if (version < 1) {
129 return;
130 }
131
132 m_closedWindows.clear();
133
134 int windowCount;
135 stream >> windowCount;
136 m_closedWindows.reserve(windowCount);
137
138 for (int i = 0; i < windowCount; ++i) {
139 Window window;
140 stream >> window.windowState;
141 if (!window.isValid()) {
142 continue;
143 }
144 window.icon = window.windowState.tabs.at(0).icon;
145 window.title = window.windowState.tabs.at(0).title;
146 m_closedWindows.append(window);
147 }
148}
TabbedWebView * weView() const
ClosedWindowsManager(QObject *parent=nullptr)
Window takeClosedWindowAt(int index)
bool isClosedWindowAvailable() const
QVector< Window > closedWindows() const
QByteArray saveState() const
void saveWindow(BrowserWindow *window)
void restoreState(const QByteArray &state)
static bool containsIndex(const T &container, int index)
Definition: qztools.h:95
QIcon icon(bool allowNull=false) const
Definition: webview.cpp:90
QString title(bool allowEmpty=false) const
Definition: webview.cpp:107
#define mApp
@ BW_OtherRestoredWindow
Definition: qzcommon.h:66
i
Definition: i18n.py:23
State state
QVector< WebTab::SavedTab > tabs
Definition: browserwindow.h:68
BrowserWindow::SavedWindow windowState