Mercurial > projects > qtd
comparison demos/browser/browserapplication.d @ 77:0654fc9bac95
more porting
author | mandel |
---|---|
date | Sun, 24 May 2009 13:46:32 +0000 |
parents | 37caa90ce503 |
children | 85c59c4e5f19 |
comparison
equal
deleted
inserted
replaced
76:454e4b4beb59 | 77:0654fc9bac95 |
---|---|
78 | 78 |
79 class BrowserApplication : public QApplication | 79 class BrowserApplication : public QApplication |
80 { | 80 { |
81 public: | 81 public: |
82 | 82 |
83 this(char[] args) | 83 this(string[] args) |
84 { | 84 { |
85 super(args); | 85 super(args); |
86 m_localServer = 0; | 86 m_localServer = null; |
87 QCoreApplication.setOrganizationName(QLatin1String("Trolltech")); | 87 QCoreApplication.setOrganizationName(QLatin1String("Trolltech")); |
88 QCoreApplication.setApplicationName(QLatin1String("demobrowser")); | 88 QCoreApplication.setApplicationName(QLatin1String("demobrowser")); |
89 QCoreApplication.setApplicationVersion(QLatin1String("0.1")); | 89 QCoreApplication.setApplicationVersion(QLatin1String("0.1")); |
90 version(Q_WS_QWS) | 90 version(Q_WS_QWS) |
91 { | 91 { |
92 // Use a different server name for QWS so we can run an X11 | 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 | 93 // browser and a QWS browser in parallel on the same machine for |
94 // debugging | 94 // debugging |
95 QString serverName = QCoreApplication.applicationName() + QLatin1String("_qws"); | 95 string serverName = QCoreApplication.applicationName() + QLatin1String("_qws"); |
96 } else { | 96 } else { |
97 QString serverName = QCoreApplication.applicationName(); | 97 string serverName = QCoreApplication.applicationName(); |
98 } | 98 } |
99 QLocalSocket socket; | 99 auto socket = new QLocalSocket; |
100 socket.connectToServer(serverName); | 100 socket.connectToServer(serverName); |
101 if (socket.waitForConnected(500)) { | 101 if (socket.waitForConnected(500)) { |
102 auto stream = new QTextStream(&socket); | 102 auto stream = new QTextStream(&socket); |
103 QStringList args = QCoreApplication.arguments(); | 103 string[] args = QCoreApplication.arguments(); |
104 if (args.count() > 1) | 104 if (args.count() > 1) |
105 stream << args.last(); | 105 stream << args.last(); |
106 else | 106 else |
107 stream << QString(); | 107 stream << ""; |
108 stream.flush(); | 108 stream.flush(); |
109 socket.waitForBytesWritten(); | 109 socket.waitForBytesWritten(); |
110 return; | 110 return; |
111 } | 111 } |
112 | 112 |
132 "This system does not support OpenSSL. SSL websites will not be available."); | 132 "This system does not support OpenSSL. SSL websites will not be available."); |
133 } | 133 } |
134 } | 134 } |
135 | 135 |
136 QDesktopServices.setUrlHandler(QLatin1String("http"), this, "openUrl"); | 136 QDesktopServices.setUrlHandler(QLatin1String("http"), this, "openUrl"); |
137 QString localSysName = QLocale.system().name(); | 137 string localSysName = QLocale.system().name(); |
138 | 138 |
139 installTranslator(QLatin1String("qt_") + localSysName); | 139 installTranslator(QLatin1String("qt_") + localSysName); |
140 | 140 |
141 QSettings settings; | 141 QSettings settings; |
142 settings.beginGroup(QLatin1String("sessions")); | 142 settings.beginGroup(QLatin1String("sessions")); |
151 } | 151 } |
152 | 152 |
153 ~this() | 153 ~this() |
154 { | 154 { |
155 delete s_downloadManager; | 155 delete s_downloadManager; |
156 for (int i = 0; i < m_mainWindows.size(); ++i) { | 156 for (int i = 0; i < m_mainWindows.length; ++i) { |
157 BrowserMainWindow window = m_mainWindows.at(i); | 157 BrowserMainWindow window = m_mainWindows[i]; |
158 delete window; | 158 delete window; |
159 } | 159 } |
160 delete s_networkAccessManager; | 160 delete s_networkAccessManager; |
161 delete s_bookmarksManager; | 161 delete s_bookmarksManager; |
162 } | 162 } |
170 { | 170 { |
171 QSettings settings; | 171 QSettings settings; |
172 settings.beginGroup(QLatin1String("websettings")); | 172 settings.beginGroup(QLatin1String("websettings")); |
173 | 173 |
174 QWebSettings defaultSettings = QWebSettings.globalSettings(); | 174 QWebSettings defaultSettings = QWebSettings.globalSettings(); |
175 QString standardFontFamily = defaultSettings.fontFamily(QWebSettings.StandardFont); | 175 string standardFontFamily = defaultSettings.fontFamily(QWebSettings.StandardFont); |
176 int standardFontSize = defaultSettings.fontSize(QWebSettings.DefaultFontSize); | 176 int standardFontSize = defaultSettings.fontSize(QWebSettings.DefaultFontSize); |
177 QFont standardFont = QFont(standardFontFamily, standardFontSize); | 177 QFont standardFont = QFont(standardFontFamily, standardFontSize); |
178 standardFont = qVariantValue!(QFont)(settings.value(QLatin1String("standardFont"), standardFont)); | 178 standardFont = qVariantValue!(QFont)(settings.value(QLatin1String("standardFont"), standardFont)); |
179 defaultSettings.setFontFamily(QWebSettings.StandardFont, standardFont.family()); | 179 defaultSettings.setFontFamily(QWebSettings.StandardFont, standardFont.family()); |
180 defaultSettings.setFontSize(QWebSettings.DefaultFontSize, standardFont.pointSize()); | 180 defaultSettings.setFontSize(QWebSettings.DefaultFontSize, standardFont.pointSize()); |
181 | 181 |
182 QString fixedFontFamily = defaultSettings.fontFamily(QWebSettings.FixedFont); | 182 string fixedFontFamily = defaultSettings.fontFamily(QWebSettings.FixedFont); |
183 int fixedFontSize = defaultSettings.fontSize(QWebSettings.DefaultFixedFontSize); | 183 int fixedFontSize = defaultSettings.fontSize(QWebSettings.DefaultFixedFontSize); |
184 QFont fixedFont = QFont(fixedFontFamily, fixedFontSize); | 184 QFont fixedFont = QFont(fixedFontFamily, fixedFontSize); |
185 fixedFont = qVariantValue!(QFont)(settings.value(QLatin1String("fixedFont"), fixedFont)); | 185 fixedFont = qVariantValue!(QFont)(settings.value(QLatin1String("fixedFont"), fixedFont)); |
186 defaultSettings.setFontFamily(QWebSettings.FixedFont, fixedFont.family()); | 186 defaultSettings.setFontFamily(QWebSettings.FixedFont, fixedFont.family()); |
187 defaultSettings.setFontSize(QWebSettings.DefaultFixedFontSize, fixedFont.pointSize()); | 187 defaultSettings.setFontSize(QWebSettings.DefaultFixedFontSize, fixedFont.pointSize()); |
195 settings.endGroup(); | 195 settings.endGroup(); |
196 } | 196 } |
197 | 197 |
198 bool isTheOnlyBrowser() | 198 bool isTheOnlyBrowser() |
199 { | 199 { |
200 return (m_localServer != 0); | 200 return (m_localServer != null); |
201 } | 201 } |
202 | 202 |
203 BrowserMainWindow mainWindow() | 203 BrowserMainWindow mainWindow() |
204 { | 204 { |
205 clean(); | 205 clean(); |
210 | 210 |
211 BrowserMainWindow[] mainWindows() | 211 BrowserMainWindow[] mainWindows() |
212 { | 212 { |
213 clean(); | 213 clean(); |
214 BrowserMainWindow[] list; | 214 BrowserMainWindow[] list; |
215 for (int i = 0; i < m_mainWindows.count(); ++i) | 215 for (int i = 0; i < m_mainWindows.length; ++i) |
216 list ~= m_mainWindows.at(i); | 216 list ~= m_mainWindows[i]; |
217 return list; | 217 return list; |
218 } | 218 } |
219 | 219 |
220 QIcon icon(QUrl url) | 220 QIcon icon(QUrl url) |
221 { | 221 { |
241 QByteArray data; | 241 QByteArray data; |
242 auto buffer = new QBuffer(&data); | 242 auto buffer = new QBuffer(&data); |
243 auto stream = new QDataStream(&buffer); | 243 auto stream = new QDataStream(&buffer); |
244 buffer.open(QIODevice.ReadWrite); | 244 buffer.open(QIODevice.ReadWrite); |
245 | 245 |
246 stream << m_mainWindows.count(); | 246 stream << m_mainWindows.length; |
247 for (int i = 0; i < m_mainWindows.count(); ++i) | 247 for (int i = 0; i < m_mainWindows.length; ++i) |
248 stream << m_mainWindows.at(i).saveState(); | 248 stream << m_mainWindows[i].saveState(); |
249 settings.setValue(QLatin1String("lastSession"), data); | 249 settings.setValue(QLatin1String("lastSession"), data); |
250 settings.endGroup(); | 250 settings.endGroup(); |
251 } | 251 } |
252 | 252 |
253 bool canRestoreSession() | 253 bool canRestoreSession() |
352 && mainWindow().currentTab().url() == QUrl()) { | 352 && mainWindow().currentTab().url() == QUrl()) { |
353 newWindow = mainWindow(); | 353 newWindow = mainWindow(); |
354 } else { | 354 } else { |
355 newWindow = newMainWindow(); | 355 newWindow = newMainWindow(); |
356 } | 356 } |
357 newWindow.restoreState(windows.at(i)); | 357 newWindow.restoreState(windows[i]); |
358 } | 358 } |
359 } | 359 } |
360 | 360 |
361 | 361 |
362 version(Q_WS_MAC) | 362 version(Q_WS_MAC) |
365 | 365 |
366 void quitBrowser() | 366 void quitBrowser() |
367 { | 367 { |
368 clean(); | 368 clean(); |
369 int tabCount = 0; | 369 int tabCount = 0; |
370 for (int i = 0; i < m_mainWindows.count(); ++i) { | 370 for (int i = 0; i < m_mainWindows.length; ++i) { |
371 tabCount =+ m_mainWindows.at(i).tabWidget().count(); | 371 tabCount =+ m_mainWindows[i].tabWidget().count(); |
372 } | 372 } |
373 | 373 |
374 if (tabCount > 1) { | 374 if (tabCount > 1) { |
375 int ret = QMessageBox.warning(mainWindow(), QString(), | 375 int ret = QMessageBox.warning(mainWindow(), null, |
376 tr("There are %1 windows and %2 tabs open\n" | 376 tr("There are %1 windows and %2 tabs open\n" |
377 "Do you want to quit anyway?").arg(m_mainWindows.count()).arg(tabCount), | 377 "Do you want to quit anyway?").arg(m_mainWindows.count()).arg(tabCount), |
378 QMessageBox.Yes | QMessageBox.No, | 378 QMessageBox.Yes | QMessageBox.No, |
379 QMessageBox.No); | 379 QMessageBox.No); |
380 if (ret == QMessageBox.No) | 380 if (ret == QMessageBox.No) |
399 /*! | 399 /*! |
400 Any actions that can be delayed until the window is visible | 400 Any actions that can be delayed until the window is visible |
401 */ | 401 */ |
402 void postLaunch() | 402 void postLaunch() |
403 { | 403 { |
404 QString directory = QDesktopServices.storageLocation(QDesktopServices.DataLocation); | 404 string directory = QDesktopServices.storageLocation(QDesktopServices.DataLocation); |
405 if (directory.isEmpty()) | 405 if (directory.isEmpty()) |
406 directory = QDir.homePath() ~ QLatin1String("/.") ~ QCoreApplication.applicationName(); | 406 directory = QDir.homePath() ~ QLatin1String("/.") ~ QCoreApplication.applicationName(); |
407 QWebSettings.setIconDatabasePath(directory); | 407 QWebSettings.setIconDatabasePath(directory); |
408 | 408 |
409 setWindowIcon(QIcon(QLatin1String(":browser.svg"))); | 409 setWindowIcon(QIcon(QLatin1String(":browser.svg"))); |
410 | 410 |
411 loadSettings(); | 411 loadSettings(); |
412 | 412 |
413 // newMainWindow() needs to be called in main() for this to happen | 413 // newMainWindow() needs to be called in main() for this to happen |
414 if (m_mainWindows.count() > 0) { | 414 if (m_mainWindows.count() > 0) { |
415 QStringList args = QCoreApplication.arguments(); | 415 string[] args = QCoreApplication.arguments(); |
416 if (args.count() > 1) | 416 if (args.count() > 1) |
417 mainWindow().loadPage(args.last()); | 417 mainWindow().loadPage(args.last()); |
418 else | 418 else |
419 mainWindow().slotHome(); | 419 mainWindow().slotHome(); |
420 } | 420 } |
431 QLocalSocket socket = m_localServer.nextPendingConnection(); | 431 QLocalSocket socket = m_localServer.nextPendingConnection(); |
432 if (!socket) | 432 if (!socket) |
433 return; | 433 return; |
434 socket.waitForReadyRead(1000); | 434 socket.waitForReadyRead(1000); |
435 QTextStream stream(socket); | 435 QTextStream stream(socket); |
436 QString url; | 436 string url; |
437 stream >> url; | 437 stream >> url; |
438 if (!url.isEmpty()) { | 438 if (!url.isEmpty()) { |
439 QSettings settings; | 439 QSettings settings; |
440 settings.beginGroup(QLatin1String("general")); | 440 settings.beginGroup(QLatin1String("general")); |
441 int openLinksIn = settings.value(QLatin1String("openLinksIn"), 0).toInt(); | 441 int openLinksIn = settings.value(QLatin1String("openLinksIn"), 0).toInt(); |
454 private: | 454 private: |
455 | 455 |
456 void clean() | 456 void clean() |
457 { | 457 { |
458 // cleanup any deleted main windows first | 458 // cleanup any deleted main windows first |
459 for (int i = m_mainWindows.count() - 1; i >= 0; --i) | 459 for (int i = m_mainWindows.length - 1; i >= 0; --i) |
460 if (m_mainWindows.at(i).isNull()) | 460 if (m_mainWindows[i].isNull()) |
461 m_mainWindows.removeAt(i); | 461 m_mainWindows.removeAt(i); |
462 } | 462 } |
463 | 463 |
464 void installTranslator(QString name) | 464 void installTranslator(string name) |
465 { | 465 { |
466 QTranslator translator = new QTranslator(this); | 466 QTranslator translator = new QTranslator(this); |
467 translator.load(name, QLibraryInfo.location(QLibraryInfo.TranslationsPath)); | 467 translator.load(name, QLibraryInfo.location(QLibraryInfo.TranslationsPath)); |
468 QApplication.installTranslator(translator); | 468 QApplication.installTranslator(translator); |
469 } | 469 } |