1 /* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2010-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 #ifndef MAINAPPLICATION_H
19 #define MAINAPPLICATION_H
20 
21 #define mApp MainApplication::instance()
22 
23 #include <QList>
24 #include <QPointer>
25 
26 #include "qtsingleapplication/qtsingleapplication.h"
27 #include "restoremanager.h"
28 #include "qzcommon.h"
29 
30 class QMenu;
31 class QWebEngineProfile;
32 class QWebEngineSettings;
33 class QNetworkAccessManager;
34 class QWebEngineDownloadItem;
35 
36 class History;
37 class AutoFill;
38 class MainMenu;
39 class Bookmarks;
40 class CookieJar;
41 class AutoSaver;
42 class PluginProxy;
43 class BrowserWindow;
44 class NetworkManager;
45 class BrowsingLibrary;
46 class DownloadManager;
47 class UserAgentManager;
48 class SearchEnginesManager;
49 class HTML5PermissionsManager;
50 class RegisterQAppAssociation;
51 class DesktopNotificationsFactory;
52 class ProxyStyle;
53 class SessionManager;
54 class ClosedWindowsManager;
55 class ProtocolHandlerManager;
56 
57 class FALKON_EXPORT MainApplication : public QtSingleApplication
58 {
59     Q_OBJECT
60 
61 public:
62     enum AfterLaunch {
63         OpenBlankPage = 0,
64         OpenHomePage = 1,
65         OpenSpeedDial = 2,
66         RestoreSession = 3,
67         SelectSession = 4
68     };
69 
70     explicit MainApplication(int &argc, char** argv);
71     ~MainApplication();
72 
73     bool isClosing() const;
74     bool isPrivate() const;
75     bool isPortable() const;
76     bool isStartingAfterCrash() const;
77 
78     int windowCount() const;
79     QList<BrowserWindow*> windows() const;
80 
81     BrowserWindow* getWindow() const;
82     BrowserWindow* createWindow(Qz::BrowserWindowType type, const QUrl &startUrl = QUrl());
83 
84     AfterLaunch afterLaunch() const;
85 
86     void openSession(BrowserWindow* window, RestoreData &restoreData);
87     bool restoreSession(BrowserWindow* window, RestoreData restoreData);
88     void destroyRestoreManager();
89     void reloadSettings();
90 
91     // Name of current Qt style
92     QString styleName() const;
93     void setProxyStyle(ProxyStyle *style);
94 
95     QByteArray wmClass() const;
96 
97     History* history();
98     Bookmarks* bookmarks();
99 
100     AutoFill* autoFill();
101     CookieJar* cookieJar();
102     PluginProxy* plugins();
103     BrowsingLibrary* browsingLibrary();
104 
105     NetworkManager* networkManager();
106     RestoreManager* restoreManager();
107     SessionManager* sessionManager();
108     DownloadManager* downloadManager();
109     UserAgentManager* userAgentManager();
110     SearchEnginesManager* searchEnginesManager();
111     ClosedWindowsManager* closedWindowsManager();
112     ProtocolHandlerManager *protocolHandlerManager();
113     HTML5PermissionsManager* html5PermissionsManager();
114     DesktopNotificationsFactory* desktopNotifications();
115     QWebEngineProfile* webProfile() const;
116     QWebEngineSettings *webSettings() const;
117 
118     QByteArray saveState() const;
119 
120     static MainApplication* instance();
121 
122     static bool isTestModeEnabled();
123     static void setTestModeEnabled(bool enabled);
124 
125 public Q_SLOTS:
126     void addNewTab(const QUrl &url = QUrl());
127     void startPrivateBrowsing(const QUrl &startUrl = QUrl());
128 
129     void reloadUserStyleSheet();
130     void restoreOverrideCursor();
131 
132     void changeOccurred();
133     void quitApplication();
134 
135 Q_SIGNALS:
136     void settingsReloaded();
137     void activeWindowChanged(BrowserWindow* window);
138 
139 private Q_SLOTS:
140     void postLaunch();
141 
142     void saveSettings();
143 
144     void messageReceived(const QString &message);
145     void windowDestroyed(QObject* window);
146     void onFocusChanged();
147     void runDeferredPostLaunchActions();
148 
149     void downloadRequested(QWebEngineDownloadItem *download);
150 
151 private:
152     enum PostLaunchAction {
153         OpenDownloadManager,
154         OpenNewTab,
155         ToggleFullScreen
156     };
157 
158     void loadSettings();
159     void loadTheme(const QString &name);
160 
161     void setupUserScripts();
162     void setUserStyleSheet(const QString &filePath);
163 
164     void checkDefaultWebBrowser();
165     void checkOptimizeDatabase();
166 
167     bool m_isPrivate;
168     bool m_isPortable;
169     bool m_isClosing;
170     bool m_isStartingAfterCrash;
171 
172     History* m_history;
173     Bookmarks* m_bookmarks;
174 
175     AutoFill* m_autoFill;
176     CookieJar* m_cookieJar;
177     PluginProxy* m_plugins;
178     BrowsingLibrary* m_browsingLibrary;
179 
180     NetworkManager* m_networkManager;
181     RestoreManager* m_restoreManager;
182     SessionManager* m_sessionManager;
183     DownloadManager* m_downloadManager;
184     UserAgentManager* m_userAgentManager;
185     SearchEnginesManager* m_searchEnginesManager;
186     ClosedWindowsManager* m_closedWindowsManager;
187     ProtocolHandlerManager *m_protocolHandlerManager;
188     HTML5PermissionsManager* m_html5PermissionsManager;
189     DesktopNotificationsFactory* m_desktopNotifications;
190     QWebEngineProfile* m_webProfile;
191 
192     AutoSaver* m_autoSaver;
193     ProxyStyle *m_proxyStyle = nullptr;
194 
195     QByteArray m_wmClass;
196 
197     QList<BrowserWindow*> m_windows;
198     QPointer<BrowserWindow> m_lastActiveWindow;
199 
200     QList<PostLaunchAction> m_postLaunchActions;
201 
202     void createJumpList();
203     void initPulseSupport();
204 
205 #if defined(Q_OS_WIN) && !defined(Q_OS_OS2)
206 public:
207     RegisterQAppAssociation* associationManager();
208 
209 private:
210     RegisterQAppAssociation* m_registerQAppAssociation;
211 #endif
212 
213 #ifdef Q_OS_MACOS
214 public:
215     bool event(QEvent* e);
216 #endif
217 };
218 
219 #endif // MAINAPPLICATION_H
220