45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41
|
74
|
42 module browserapplication;
|
|
43
|
|
44
|
|
45 import qt.core.QBuffer;
|
|
46 import qt.core.QDir;
|
|
47 import qt.core.QLibraryInfo;
|
|
48 import qt.core.QSettings;
|
|
49 import qt.core.QTextStream;
|
|
50 import qt.core.QTranslator;
|
|
51 import qt.core.QUrl;
|
|
52 import qt.core.QPointer;
|
45
|
53
|
74
|
54 import qt.gui.QApplication;
|
|
55 import qt.gui.QIcon;
|
|
56 import qt.gui.QDesktopServices;
|
|
57 import qt.gui.QFileOpenEvent;
|
|
58 import qt.gui.QMessageBox;
|
45
|
59
|
74
|
60 import qt.network.QLocalServer;
|
|
61 import qt.network.QLocalSocket;
|
|
62 import qt.network.QNetworkProxy;
|
|
63 import qt.network.QSslSocket;
|
45
|
64
|
74
|
65 import QtWebKit.QWebSettings;
|
|
66
|
|
67 import qt.core.QDebug;
|
45
|
68
|
|
69 import bookmarks;
|
|
70 import browsermainwindow;
|
|
71 import cookiejar;
|
|
72 import downloadmanager;
|
|
73 import history;
|
|
74 import networkaccessmanager;
|
|
75 import tabwidget;
|
|
76 import webview;
|
|
77
|
|
78
|
|
79 class BrowserApplication : public QApplication
|
|
80 {
|
74
|
81 public:
|
|
82
|
|
83 this(char[] args)
|
|
84 {
|
|
85 super(args);
|
|
86 m_localServer = 0;
|
|
87 QCoreApplication.setOrganizationName(QLatin1String("Trolltech"));
|
|
88 QCoreApplication.setApplicationName(QLatin1String("demobrowser"));
|
|
89 QCoreApplication.setApplicationVersion(QLatin1String("0.1"));
|
|
90 version(Q_WS_QWS)
|
|
91 {
|
|
92 // Use a different server name for QWS so we can run an X11
|
|
93 // browser and a QWS browser in parallel on the same machine for
|
|
94 // debugging
|
|
95 QString serverName = QCoreApplication.applicationName() + QLatin1String("_qws");
|
|
96 } else {
|
|
97 QString serverName = QCoreApplication.applicationName();
|
|
98 }
|
|
99 QLocalSocket socket;
|
|
100 socket.connectToServer(serverName);
|
|
101 if (socket.waitForConnected(500)) {
|
|
102 auto stream = new QTextStream(&socket);
|
|
103 QStringList args = QCoreApplication.arguments();
|
|
104 if (args.count() > 1)
|
|
105 stream << args.last();
|
|
106 else
|
|
107 stream << QString();
|
|
108 stream.flush();
|
|
109 socket.waitForBytesWritten();
|
|
110 return;
|
|
111 }
|
|
112
|
|
113 version(Q_WS_MAC) {
|
|
114 QApplication.setQuitOnLastWindowClosed(false);
|
|
115 } else {
|
|
116 QApplication.setQuitOnLastWindowClosed(true);
|
|
117 }
|
|
118
|
|
119 m_localServer = new QLocalServer(this);
|
|
120 m_localServer.newConnection.connect(&this.newLocalSocketConnection);
|
|
121 if (!m_localServer.listen(serverName)) {
|
|
122 if (m_localServer.serverError() == QAbstractSocket.AddressInUseError
|
|
123 && QFile.exists(m_localServer.serverName())) {
|
|
124 QFile.remove(m_localServer.serverName());
|
|
125 m_localServer.listen(serverName);
|
|
126 }
|
|
127 }
|
|
128
|
|
129 version(QT_NO_OPENSSL) {} else {
|
|
130 if (!QSslSocket.supportsSsl()) {
|
|
131 QMessageBox.information(0, "Demo Browser",
|
|
132 "This system does not support OpenSSL. SSL websites will not be available.");
|
|
133 }
|
|
134 }
|
|
135
|
|
136 QDesktopServices.setUrlHandler(QLatin1String("http"), this, "openUrl");
|
|
137 QString localSysName = QLocale.system().name();
|
45
|
138
|
74
|
139 installTranslator(QLatin1String("qt_") + localSysName);
|
|
140
|
|
141 QSettings settings;
|
|
142 settings.beginGroup(QLatin1String("sessions"));
|
|
143 m_lastSession = settings.value(QLatin1String("lastSession")).toByteArray();
|
|
144 settings.endGroup();
|
|
145
|
|
146 version(Q_WS_MAC) {
|
|
147 this.lastWindowClosed.connect(&this.lastWindowClosed);
|
|
148 }
|
|
149
|
|
150 QTimer.singleShot(0, this, SLOT(postLaunch()));
|
|
151 }
|
|
152
|
|
153 ~this()
|
|
154 {
|
|
155 delete s_downloadManager;
|
|
156 for (int i = 0; i < m_mainWindows.size(); ++i) {
|
|
157 BrowserMainWindow window = m_mainWindows.at(i);
|
|
158 delete window;
|
|
159 }
|
|
160 delete s_networkAccessManager;
|
|
161 delete s_bookmarksManager;
|
|
162 }
|
|
163
|
|
164 static BrowserApplication instance()
|
|
165 {
|
|
166 return cast(BrowserApplication) QCoreApplication.instance();
|
|
167 }
|
|
168
|
|
169 void loadSettings()
|
|
170 {
|
|
171 QSettings settings;
|
|
172 settings.beginGroup(QLatin1String("websettings"));
|
|
173
|
|
174 QWebSettings defaultSettings = QWebSettings.globalSettings();
|
|
175 QString standardFontFamily = defaultSettings.fontFamily(QWebSettings.StandardFont);
|
|
176 int standardFontSize = defaultSettings.fontSize(QWebSettings.DefaultFontSize);
|
|
177 QFont standardFont = QFont(standardFontFamily, standardFontSize);
|
|
178 standardFont = qVariantValue!(QFont)(settings.value(QLatin1String("standardFont"), standardFont));
|
|
179 defaultSettings.setFontFamily(QWebSettings.StandardFont, standardFont.family());
|
|
180 defaultSettings.setFontSize(QWebSettings.DefaultFontSize, standardFont.pointSize());
|
|
181
|
|
182 QString fixedFontFamily = defaultSettings.fontFamily(QWebSettings.FixedFont);
|
|
183 int fixedFontSize = defaultSettings.fontSize(QWebSettings.DefaultFixedFontSize);
|
|
184 QFont fixedFont = QFont(fixedFontFamily, fixedFontSize);
|
|
185 fixedFont = qVariantValue!(QFont)(settings.value(QLatin1String("fixedFont"), fixedFont));
|
|
186 defaultSettings.setFontFamily(QWebSettings.FixedFont, fixedFont.family());
|
|
187 defaultSettings.setFontSize(QWebSettings.DefaultFixedFontSize, fixedFont.pointSize());
|
|
188
|
|
189 defaultSettings.setAttribute(QWebSettings.JavascriptEnabled, settings.value(QLatin1String("enableJavascript"), true).toBool());
|
|
190 defaultSettings.setAttribute(QWebSettings.PluginsEnabled, settings.value(QLatin1String("enablePlugins"), true).toBool());
|
|
191
|
|
192 QUrl url = settings.value(QLatin1String("userStyleSheet")).toUrl();
|
|
193 defaultSettings.setUserStyleSheetUrl(url);
|
|
194
|
|
195 settings.endGroup();
|
|
196 }
|
|
197
|
|
198 bool isTheOnlyBrowser()
|
45
|
199 {
|
74
|
200 return (m_localServer != 0);
|
|
201 }
|
|
202
|
|
203 BrowserMainWindow mainWindow()
|
|
204 {
|
|
205 clean();
|
|
206 if (m_mainWindows.isEmpty())
|
|
207 newMainWindow();
|
|
208 return m_mainWindows[0];
|
|
209 }
|
|
210
|
|
211 BrowserMainWindow[] mainWindows()
|
|
212 {
|
|
213 clean();
|
|
214 BrowserMainWindow[] list;
|
|
215 for (int i = 0; i < m_mainWindows.count(); ++i)
|
|
216 list ~= m_mainWindows.at(i);
|
|
217 return list;
|
|
218 }
|
|
219
|
|
220 QIcon icon(QUrl url)
|
|
221 {
|
|
222 QIcon icon = QWebSettings.iconForUrl(url);
|
|
223 if (!icon.isNull())
|
|
224 return icon.pixmap(16, 16);
|
|
225 if (m_defaultIcon.isNull())
|
|
226 m_defaultIcon = QIcon(QLatin1String(":defaulticon.png"));
|
|
227 return m_defaultIcon.pixmap(16, 16);
|
|
228 }
|
|
229
|
|
230 void saveSession()
|
|
231 {
|
|
232 QWebSettings globalSettings = QWebSettings.globalSettings();
|
|
233 if (globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled))
|
|
234 return;
|
|
235
|
|
236 clean();
|
|
237
|
|
238 QSettings settings;
|
|
239 settings.beginGroup(QLatin1String("sessions"));
|
|
240
|
|
241 QByteArray data;
|
|
242 auto buffer = new QBuffer(&data);
|
|
243 auto stream = new QDataStream(&buffer);
|
|
244 buffer.open(QIODevice.ReadWrite);
|
|
245
|
|
246 stream << m_mainWindows.count();
|
|
247 for (int i = 0; i < m_mainWindows.count(); ++i)
|
|
248 stream << m_mainWindows.at(i).saveState();
|
|
249 settings.setValue(QLatin1String("lastSession"), data);
|
|
250 settings.endGroup();
|
|
251 }
|
|
252
|
|
253 bool canRestoreSession()
|
|
254 {
|
|
255 return !m_lastSession.isEmpty();
|
|
256 }
|
45
|
257
|
74
|
258 static HistoryManager historyManager()
|
|
259 {
|
|
260 if (!s_historyManager) {
|
|
261 s_historyManager = new HistoryManager();
|
|
262 QWebHistoryInterface.setDefaultInterface(s_historyManager);
|
|
263 }
|
|
264 return s_historyManager;
|
|
265 }
|
|
266
|
|
267 static CookieJar cookieJar()
|
|
268 {
|
|
269 return cast(CookieJar) networkAccessManager().cookieJar();
|
|
270 }
|
|
271
|
|
272 static DownloadManager downloadManager()
|
|
273 {
|
|
274 if (!s_downloadManager) {
|
|
275 s_downloadManager = new DownloadManager();
|
|
276 }
|
|
277 return s_downloadManager;
|
|
278 }
|
|
279
|
|
280 static NetworkAccessManager networkAccessManager()
|
|
281 {
|
|
282 if (!s_networkAccessManager) {
|
|
283 s_networkAccessManager = new NetworkAccessManager();
|
|
284 s_networkAccessManager.setCookieJar(new CookieJar);
|
|
285 }
|
|
286 return s_networkAccessManager;
|
|
287 }
|
|
288
|
45
|
289
|
74
|
290 static BookmarksManager bookmarksManager()
|
|
291 {
|
|
292 if (!s_bookmarksManager) {
|
|
293 s_bookmarksManager = new BookmarksManager;
|
|
294 }
|
|
295 return s_bookmarksManager;
|
|
296 }
|
|
297
|
45
|
298
|
74
|
299 version(Q_WS_MAC)
|
|
300 {
|
|
301 bool event(QEvent event)
|
|
302 {
|
|
303 switch (event.type()) {
|
|
304 case QEvent.ApplicationActivate: {
|
|
305 clean();
|
|
306 if (!m_mainWindows.isEmpty()) {
|
|
307 BrowserMainWindow mw = mainWindow();
|
|
308 if (mw && !mw.isMinimized()) {
|
|
309 mainWindow().show();
|
|
310 }
|
|
311 return true;
|
|
312 }
|
|
313 }
|
|
314 case QEvent.FileOpen:
|
|
315 if (!m_mainWindows.isEmpty()) {
|
|
316 mainWindow().loadPage(cast(QFileOpenEvent) event.file());
|
|
317 return true;
|
|
318 }
|
|
319 default:
|
|
320 break;
|
|
321 }
|
|
322 return QApplication.event(event);
|
|
323 }
|
45
|
324 }
|
|
325
|
74
|
326 public:
|
45
|
327
|
74
|
328 BrowserMainWindow newMainWindow()
|
|
329 {
|
|
330 BrowserMainWindow browser = new BrowserMainWindow();
|
|
331 m_mainWindows.prepend(browser);
|
|
332 browser.show();
|
|
333 return browser;
|
|
334 }
|
45
|
335
|
74
|
336 void restoreLastSession()
|
|
337 {
|
|
338 QByteArray[] windows;
|
|
339 auto buffer = new QBuffer(&m_lastSession);
|
|
340 auto stream = new QDataStream(&buffer);
|
|
341 buffer.open(QIODevice.ReadOnly);
|
|
342 int windowCount;
|
|
343 stream >> windowCount;
|
|
344 for (int i = 0; i < windowCount; ++i) {
|
|
345 QByteArray windowState;
|
|
346 stream >> windowState;
|
|
347 windows ~= windowState;
|
|
348 }
|
|
349 for (int i = 0; i < windows.count(); ++i) {
|
|
350 BrowserMainWindow newWindow = 0;
|
|
351 if (m_mainWindows.count() == 1 && mainWindow().tabWidget().count() == 1
|
|
352 && mainWindow().currentTab().url() == QUrl()) {
|
|
353 newWindow = mainWindow();
|
|
354 } else {
|
|
355 newWindow = newMainWindow();
|
|
356 }
|
|
357 newWindow.restoreState(windows.at(i));
|
|
358 }
|
|
359 }
|
45
|
360
|
|
361
|
74
|
362 version(Q_WS_MAC)
|
45
|
363 {
|
74
|
364 import qt.gui.QMessageBox;
|
|
365
|
|
366 void quitBrowser()
|
|
367 {
|
|
368 clean();
|
|
369 int tabCount = 0;
|
|
370 for (int i = 0; i < m_mainWindows.count(); ++i) {
|
|
371 tabCount =+ m_mainWindows.at(i).tabWidget().count();
|
|
372 }
|
45
|
373
|
74
|
374 if (tabCount > 1) {
|
|
375 int ret = QMessageBox.warning(mainWindow(), QString(),
|
|
376 tr("There are %1 windows and %2 tabs open\n"
|
|
377 "Do you want to quit anyway?").arg(m_mainWindows.count()).arg(tabCount),
|
|
378 QMessageBox.Yes | QMessageBox.No,
|
|
379 QMessageBox.No);
|
|
380 if (ret == QMessageBox.No)
|
|
381 return;
|
|
382 }
|
45
|
383
|
74
|
384 exit(0);
|
|
385 }
|
|
386
|
|
387 void lastWindowClosed()
|
|
388 {
|
|
389 clean();
|
|
390 BrowserMainWindow mw = new BrowserMainWindow;
|
|
391 mw.slotHome();
|
|
392 m_mainWindows.prepend(mw);
|
|
393 }
|
45
|
394 }
|
|
395
|
|
396
|
74
|
397 private:
|
45
|
398
|
74
|
399 /*!
|
|
400 Any actions that can be delayed until the window is visible
|
|
401 */
|
|
402 void postLaunch()
|
|
403 {
|
|
404 QString directory = QDesktopServices.storageLocation(QDesktopServices.DataLocation);
|
|
405 if (directory.isEmpty())
|
|
406 directory = QDir.homePath() ~ QLatin1String("/.") ~ QCoreApplication.applicationName();
|
|
407 QWebSettings.setIconDatabasePath(directory);
|
45
|
408
|
74
|
409 setWindowIcon(QIcon(QLatin1String(":browser.svg")));
|
45
|
410
|
74
|
411 loadSettings();
|
45
|
412
|
74
|
413 // newMainWindow() needs to be called in main() for this to happen
|
|
414 if (m_mainWindows.count() > 0) {
|
|
415 QStringList args = QCoreApplication.arguments();
|
|
416 if (args.count() > 1)
|
|
417 mainWindow().loadPage(args.last());
|
|
418 else
|
|
419 mainWindow().slotHome();
|
|
420 }
|
|
421 BrowserApplication.historyManager();
|
|
422 }
|
45
|
423
|
74
|
424 void openUrl( QUrl url)
|
|
425 {
|
|
426 mainWindow().loadPage(url.toString());
|
|
427 }
|
45
|
428
|
74
|
429 void newLocalSocketConnection()
|
|
430 {
|
|
431 QLocalSocket socket = m_localServer.nextPendingConnection();
|
|
432 if (!socket)
|
|
433 return;
|
|
434 socket.waitForReadyRead(1000);
|
|
435 QTextStream stream(socket);
|
|
436 QString url;
|
|
437 stream >> url;
|
|
438 if (!url.isEmpty()) {
|
|
439 QSettings settings;
|
|
440 settings.beginGroup(QLatin1String("general"));
|
|
441 int openLinksIn = settings.value(QLatin1String("openLinksIn"), 0).toInt();
|
|
442 settings.endGroup();
|
|
443 if (openLinksIn == 1)
|
|
444 newMainWindow();
|
|
445 else
|
|
446 mainWindow().tabWidget().newTab();
|
|
447 openUrl(url);
|
|
448 }
|
|
449 delete socket;
|
|
450 mainWindow().raise();
|
|
451 mainWindow().activateWindow();
|
|
452 }
|
45
|
453
|
|
454 private:
|
74
|
455
|
|
456 void clean()
|
|
457 {
|
|
458 // cleanup any deleted main windows first
|
|
459 for (int i = m_mainWindows.count() - 1; i >= 0; --i)
|
|
460 if (m_mainWindows.at(i).isNull())
|
|
461 m_mainWindows.removeAt(i);
|
|
462 }
|
45
|
463
|
74
|
464 void installTranslator(QString name)
|
|
465 {
|
|
466 QTranslator translator = new QTranslator(this);
|
|
467 translator.load(name, QLibraryInfo.location(QLibraryInfo.TranslationsPath));
|
|
468 QApplication.installTranslator(translator);
|
|
469 }
|
|
470
|
|
471 static HistoryManager s_historyManager;
|
|
472 static DownloadManager s_downloadManager;
|
|
473 static NetworkAccessManager s_networkAccessManager;
|
|
474 static BookmarksManager s_bookmarksManager;
|
|
475
|
|
476 QPointer!(BrowserMainWindow)[] m_mainWindows;
|
|
477 QLocalServer m_localServer;
|
|
478 QByteArray m_lastSession;
|
|
479 QIcon m_defaultIcon;
|
45
|
480 }
|