/
SessionsManager.h
287 lines (242 loc) · 7.74 KB
/
SessionsManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**************************************************************************
* Otter Browser: Web browser controlled by the user, not vice-versa.
* Copyright (C) 2013 - 2019 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
* Copyright (C) 2014 Piotr Wójcik <chocimier@tlen.pl>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**************************************************************************/
#ifndef OTTER_SESSIONSMANAGER_H
#define OTTER_SESSIONSMANAGER_H
#include "SettingsManager.h"
#include "ToolBarsManager.h"
#include "Utils.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QDateTime>
#include <QtCore/QRect>
namespace Otter
{
struct ToolBarState final
{
enum ToolBarVisibility
{
UnspecifiedVisibilityToolBar = 0,
AlwaysVisibleToolBar,
AlwaysHiddenToolBar
};
Qt::ToolBarArea location = Qt::NoToolBarArea;
int identifier = -1;
int row = -1;
ToolBarVisibility normalVisibility = UnspecifiedVisibilityToolBar;
ToolBarVisibility fullScreenVisibility = UnspecifiedVisibilityToolBar;
explicit ToolBarState()
{
}
explicit ToolBarState(int identifierValue, const ToolBarsManager::ToolBarDefinition &definition) : identifier(identifierValue)
{
normalVisibility = ((definition.normalVisibility != ToolBarsManager::AlwaysHiddenToolBar) ? AlwaysVisibleToolBar : AlwaysHiddenToolBar);
fullScreenVisibility = ((definition.fullScreenVisibility != ToolBarsManager::AlwaysHiddenToolBar) ? AlwaysVisibleToolBar : AlwaysHiddenToolBar);
}
void setVisibility(ToolBarsManager::ToolBarsMode mode, ToolBarVisibility visibility)
{
switch (mode)
{
case ToolBarsManager::NormalMode:
normalVisibility = visibility;
break;
case ToolBarsManager::FullScreenMode:
fullScreenVisibility = visibility;
break;
default:
break;
}
}
ToolBarVisibility getVisibility(ToolBarsManager::ToolBarsMode mode) const
{
return ((mode == ToolBarsManager::FullScreenMode) ? fullScreenVisibility : normalVisibility);
}
bool isValid() const
{
return (identifier >= 0);
}
};
class MainWindow;
class SessionModel;
class Session final : public QObject
{
public:
struct Window final
{
struct History final
{
struct Entry final
{
QString url;
QString title;
QIcon icon;
QDateTime time;
QPoint position;
int zoom = SettingsManager::getOption(SettingsManager::Content_DefaultZoomOption).toInt();
QString getTitle() const
{
if (title.isEmpty())
{
if (url == QLatin1String("about:start") && SettingsManager::getOption(SettingsManager::StartPage_EnableStartPageOption).toBool())
{
return QCoreApplication::translate("Otter::SessionsManager", "Start Page");
}
return QCoreApplication::translate("Otter::SessionsManager", "(Unknown)");
}
return title;
}
};
QVector<Entry> entries;
int index = -1;
bool isEmpty() const
{
return (entries.isEmpty() || (entries.count() == 1 && Utils::isUrlEmpty(QUrl(entries.first().url))));
}
};
struct State final
{
QRect geometry;
Qt::WindowState state = ((SettingsManager::getOption(SettingsManager::Interface_NewTabOpeningActionOption).toString() == QLatin1String("maximizeTab")) ? Qt::WindowMaximized : Qt::WindowNoState);
};
History history;
State state;
QHash<int, QVariant> options;
int parentGroup = 0;
bool isAlwaysOnTop = false;
bool isPinned = false;
QString getUrl() const
{
if (history.index >= 0 && history.index < history.entries.count())
{
return history.entries.at(history.index).url;
}
return {};
}
QString getTitle() const
{
if (history.index >= 0 && history.index < history.entries.count())
{
return history.entries.at(history.index).getTitle();
}
return QCoreApplication::translate("main", "(Untitled)");
}
int getZoom() const
{
if (history.index >= 0 && history.index < history.entries.count())
{
return history.entries.at(history.index).zoom;
}
return SettingsManager::getOption(SettingsManager::Content_DefaultZoomOption).toInt();
}
};
struct MainWindow final
{
QMap<QString, QVector<int> > splitters;
QVector<Window> windows;
QVector<ToolBarState> toolBars;
QByteArray geometry;
int index = -1;
bool hasToolBarsState = false;
};
explicit Session(QObject *parent = nullptr);
};
struct SessionInformation final
{
QString path;
QString title;
QVector<Session::MainWindow> windows;
int index = -1;
bool isClean = true;
bool isValid() const
{
return !windows.isEmpty();
}
};
struct ClosedWindow final
{
Session::Window window;
QIcon icon;
quint64 nextWindow = 0;
quint64 previousWindow = 0;
bool isPrivate = false;
};
class SessionsManager final : public QObject
{
Q_OBJECT
public:
enum OpenHint
{
DefaultOpen = 0,
PrivateOpen = 1,
CurrentTabOpen = 2,
NewTabOpen = 4,
NewWindowOpen = 8,
BackgroundOpen = 16,
EndOpen = 32
};
Q_DECLARE_FLAGS(OpenHints, OpenHint)
static void createInstance(const QString &profilePath, const QString &cachePath, bool isPrivate = false, bool isReadOnly = false);
static void clearClosedWindows();
static void storeClosedWindow(MainWindow *mainWindow);
static void markSessionAsModified();
static void removeStoredUrl(const QString &url);
static SessionsManager* getInstance();
static SessionModel* getModel();
static QString getCurrentSession();
static QString getCachePath();
static QString getProfilePath();
static QString getReadableDataPath(const QString &path, bool forceBundled = false);
static QString getWritableDataPath(const QString &path);
static QString getSessionPath(const QString &path, bool isBound = false);
static SessionInformation getSession(const QString &path);
static QStringList getClosedWindows();
static QStringList getSessions();
static SessionsManager::OpenHints calculateOpenHints(OpenHints hints, Qt::MouseButton button, Qt::KeyboardModifiers modifiers);
static SessionsManager::OpenHints calculateOpenHints(OpenHints hints = DefaultOpen, Qt::MouseButton button = Qt::LeftButton);
static SessionsManager::OpenHints calculateOpenHints(const QVariantMap ¶meters, bool ignoreModifiers = false);
static bool restoreClosedWindow(int index = 0);
static bool restoreSession(const SessionInformation &session, MainWindow *mainWindow = nullptr, bool isPrivate = false);
static bool saveSession(const QString &path = {}, const QString &title = {}, MainWindow *mainWindow = nullptr, bool isClean = true);
static bool saveSession(const SessionInformation &session);
static bool deleteSession(const QString &path = {});
static bool isPrivate();
static bool isReadOnly();
static bool hasUrl(const QUrl &url, bool activate = false);
protected:
explicit SessionsManager(QObject *parent);
void timerEvent(QTimerEvent *event) override;
void scheduleSave();
private:
int m_saveTimer;
static SessionsManager *m_instance;
static SessionModel *m_model;
static QString m_sessionPath;
static QString m_sessionTitle;
static QString m_cachePath;
static QString m_profilePath;
static QVector<Session::MainWindow> m_closedWindows;
static bool m_isDirty;
static bool m_isPrivate;
static bool m_isReadOnly;
signals:
void closedWindowsChanged();
void requestedRemoveStoredUrl(const QString &url);
};
}
Q_DECLARE_OPERATORS_FOR_FLAGS(Otter::SessionsManager::OpenHints)
#endif