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