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