From 0edb6762d04a9ba1cf3196668597b8cbe3703ce2 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Sun, 15 Feb 2009 15:27:40 +0100 Subject: Renamed Application class files --- src/application.cpp | 338 +++++++++++++++++++++++++++++++++++++++++++++ src/application.h | 90 ++++++++++++ src/browserapplication.cpp | 338 --------------------------------------------- src/browserapplication.h | 90 ------------ 4 files changed, 428 insertions(+), 428 deletions(-) create mode 100644 src/application.cpp create mode 100644 src/application.h delete mode 100644 src/browserapplication.cpp delete mode 100644 src/browserapplication.h diff --git a/src/application.cpp b/src/application.cpp new file mode 100644 index 00000000..a82b095c --- /dev/null +++ b/src/application.cpp @@ -0,0 +1,338 @@ +/* ============================================================ + * + * This file is a part of the rekonq project + * + * Copyright (C) 2007-2008 Trolltech ASA. All rights reserved + * Copyright (C) 2008 by Andrea Diamantini + * + * + * This program is free software; you can redistribute it + * and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; + * either version 2, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * ============================================================ */ + + +// Local Includes +#include "browserapplication.h" + +#include "rekonq.h" + +#include "mainwindow.h" +#include "cookiejar.h" +#include "history.h" +#include "networkaccessmanager.h" +#include "mainview.h" +#include "webview.h" +#include "download.h" + +// KDE Includes +#include +#include +#include +#include +#include + +// Qt Includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +HistoryManager *BrowserApplication::s_historyManager = 0; +NetworkAccessManager *BrowserApplication::s_networkAccessManager = 0; + + + +BrowserApplication::BrowserApplication(KCmdLineArgs *args, const QString &serverName) + : KApplication() + , m_localServer(0) +{ + QLocalSocket socket; + socket.connectToServer(serverName); + if (socket.waitForConnected(500)) + { + QTextStream stream(&socket); + int n = args->count(); + if (n > 1) + stream << args->arg(n-1); + else + stream << QString(); + stream.flush(); + socket.waitForBytesWritten(); + return; + } + + KApplication::setQuitOnLastWindowClosed(true); + + m_localServer = new QLocalServer(this); + connect(m_localServer, SIGNAL(newConnection()), this, SLOT(newLocalSocketConnection())); + if (!m_localServer->listen(serverName)) + { + if (m_localServer->serverError() == QAbstractSocket::AddressInUseError + && QFile::exists(m_localServer->serverName())) + { + QFile::remove(m_localServer->serverName()); + m_localServer->listen(serverName); + } + } + + QDesktopServices::setUrlHandler(QLatin1String("http"), this, "openUrl"); + QString localSysName = QLocale::system().name(); + + KConfig config("rekonqrc"); + KConfigGroup group = config.group("sessions"); + m_lastSession = group.readEntry( QString("lastSession"), QByteArray() ); + + setWindowIcon( KIcon("rekonq") ); + + QTimer::singleShot(0, this, SLOT( postLaunch() ) ); +} + + +BrowserApplication::~BrowserApplication() +{ + qDeleteAll(m_mainWindows); + delete s_networkAccessManager; +} + + +BrowserApplication *BrowserApplication::instance() +{ + return (static_cast(QCoreApplication::instance())); +} + + +/*! + Any actions that can be delayed until the window is visible + */ +void BrowserApplication::postLaunch() +{ + QString directory = QDesktopServices::storageLocation(QDesktopServices::DataLocation); + if ( directory.isEmpty() ) + { + directory = QDir::homePath() + QLatin1String("/.") + QCoreApplication::applicationName(); + } + QWebSettings::setIconDatabasePath(directory); + + // newMainWindow() needs to be called in main() for this to happen + if (m_mainWindows.count() > 0) + { + KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); + int n = args->count(); + if (n > 1) + { + KUrl url = MainWindow::guessUrlFromString( args->arg(n-1) ); + mainWindow()->loadUrl( url ); + } + else + { + mainWindow()->slotHome(); + } + } + BrowserApplication::historyManager(); +} + + +void BrowserApplication::downloadUrl(const KUrl &srcUrl, const KUrl &destUrl) +{ + new Download( srcUrl, destUrl ); +} + + +QList BrowserApplication::mainWindows() +{ + clean(); + QList list; + for (int i = 0; i < m_mainWindows.count(); ++i) + { + list.append(m_mainWindows.at(i)); + } + return list; +} + + +void BrowserApplication::clean() +{ + // cleanup any deleted main windows first + for (int i = m_mainWindows.count() - 1; i >= 0; --i) + { + if (m_mainWindows.at(i).isNull()) + { + m_mainWindows.removeAt(i); + } + } +} + + +void BrowserApplication::saveSession() +{ + QWebSettings *globalSettings = QWebSettings::globalSettings(); + if ( globalSettings->testAttribute( QWebSettings::PrivateBrowsingEnabled ) ) + return; + + clean(); + + KConfig config("rekonqrc"); + KConfigGroup group = config.group("sessions"); + QByteArray data; + QBuffer buffer(&data); + QDataStream stream(&buffer); + buffer.open(QIODevice::ReadWrite); + + stream << m_mainWindows.count(); + for (int i = 0; i < m_mainWindows.count(); ++i) + { + stream << m_mainWindows.at(i)->saveState(); + } + + group.writeEntry( QString("lastSession"), data ); +} + + +bool BrowserApplication::canRestoreSession() const +{ + return !m_lastSession.isEmpty(); +} + + +void BrowserApplication::restoreLastSession() +{ + QList windows; + QBuffer buffer(&m_lastSession); + QDataStream stream(&buffer); + buffer.open(QIODevice::ReadOnly); + int windowCount; + stream >> windowCount; + for (int i = 0; i < windowCount; ++i) + { + QByteArray windowState; + stream >> windowState; + windows.append(windowState); + } + for (int i = 0; i < windows.count(); ++i) + { + MainWindow *newWindow = 0; + if (m_mainWindows.count() == 1 + && mainWindow()->tabWidget()->count() == 1 + && mainWindow()->currentTab()->url() == KUrl()) + { + newWindow = mainWindow(); + } + else + { + newWindow = newMainWindow(); + } + newWindow->restoreState(windows.at(i)); + } +} + + + +bool BrowserApplication::isTheOnlyBrowser() const +{ + return (m_localServer != 0); +} + + +void BrowserApplication::openUrl(const KUrl &url) +{ + mainWindow()->loadUrl(url); +} + + + +MainWindow *BrowserApplication::newMainWindow() +{ + MainWindow *browser = new MainWindow(); + m_mainWindows.prepend(browser); + browser->show(); + return browser; +} + + +MainWindow *BrowserApplication::mainWindow() +{ + clean(); + if (m_mainWindows.isEmpty()) + newMainWindow(); + return m_mainWindows[0]; +} + + +void BrowserApplication::newLocalSocketConnection() +{ + QLocalSocket *socket = m_localServer->nextPendingConnection(); + if (!socket) + return; + socket->waitForReadyRead(1000); + QTextStream stream(socket); + QString url; + stream >> url; + if (!url.isEmpty()) + { + mainWindow()->tabWidget()->newTab(); + openUrl(url); + } + delete socket; + mainWindow()->raise(); + mainWindow()->activateWindow(); +} + + + +CookieJar *BrowserApplication::cookieJar() +{ + return (CookieJar*)networkAccessManager()->cookieJar(); +} + + +NetworkAccessManager *BrowserApplication::networkAccessManager() +{ + if (!s_networkAccessManager) + { + s_networkAccessManager = new NetworkAccessManager(); + s_networkAccessManager->setCookieJar(new CookieJar); + } + return s_networkAccessManager; +} + + + +HistoryManager *BrowserApplication::historyManager() +{ + if (!s_historyManager) + { + s_historyManager = new HistoryManager(); + QWebHistoryInterface::setDefaultInterface(s_historyManager); + } + return s_historyManager; +} + + + + +KIcon BrowserApplication::icon(const KUrl &url) const +{ + KIcon icon = KIcon( QWebSettings::iconForUrl(url) ); + if (!icon.isNull()) + return icon; + if (m_defaultIcon.isNull()) + m_defaultIcon = KIcon("kde"); + return m_defaultIcon; +} + diff --git a/src/application.h b/src/application.h new file mode 100644 index 00000000..9b47bfcc --- /dev/null +++ b/src/application.h @@ -0,0 +1,90 @@ +/* ============================================================ + * + * This file is a part of the rekonq project + * + * Copyright (C) 2007-2008 Trolltech ASA. All rights reserved + * Copyright (C) 2008 by Andrea Diamantini + * + * + * This program is free software; you can redistribute it + * and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; + * either version 2, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * ============================================================ */ + + +#ifndef BROWSERAPPLICATION_H +#define BROWSERAPPLICATION_H + +// KDE Includes +#include +#include +#include +#include +#include +#include +#include + +// Qt Includes +#include + +QT_BEGIN_NAMESPACE +class QLocalServer; +QT_END_NAMESPACE + +class MainWindow; +class CookieJar; +class HistoryManager; +class NetworkAccessManager; + +class BrowserApplication : public KApplication +{ + Q_OBJECT + +public: + BrowserApplication(KCmdLineArgs*, const QString &); + ~BrowserApplication(); + static BrowserApplication *instance(); + + bool isTheOnlyBrowser() const; + MainWindow *mainWindow(); + QList mainWindows(); + KIcon icon(const KUrl &url) const; + void downloadUrl(const KUrl &srcUrl, const KUrl &destUrl); + + void saveSession(); + bool canRestoreSession() const; + + static HistoryManager *historyManager(); + static CookieJar *cookieJar(); + static NetworkAccessManager *networkAccessManager(); + +public slots: + MainWindow *newMainWindow(); + void restoreLastSession(); + +private slots: + void postLaunch(); + void openUrl(const KUrl &url); + void newLocalSocketConnection(); + +private: + void clean(); + + static HistoryManager *s_historyManager; + static NetworkAccessManager *s_networkAccessManager; + + QList > m_mainWindows; + QLocalServer *m_localServer; + QByteArray m_lastSession; + mutable KIcon m_defaultIcon; +}; + +#endif // BROWSERAPPLICATION_H + diff --git a/src/browserapplication.cpp b/src/browserapplication.cpp deleted file mode 100644 index a82b095c..00000000 --- a/src/browserapplication.cpp +++ /dev/null @@ -1,338 +0,0 @@ -/* ============================================================ - * - * This file is a part of the rekonq project - * - * Copyright (C) 2007-2008 Trolltech ASA. All rights reserved - * Copyright (C) 2008 by Andrea Diamantini - * - * - * This program is free software; you can redistribute it - * and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; - * either version 2, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the - * GNU General Public License for more details. - * - * ============================================================ */ - - -// Local Includes -#include "browserapplication.h" - -#include "rekonq.h" - -#include "mainwindow.h" -#include "cookiejar.h" -#include "history.h" -#include "networkaccessmanager.h" -#include "mainview.h" -#include "webview.h" -#include "download.h" - -// KDE Includes -#include -#include -#include -#include -#include - -// Qt Includes -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - - -HistoryManager *BrowserApplication::s_historyManager = 0; -NetworkAccessManager *BrowserApplication::s_networkAccessManager = 0; - - - -BrowserApplication::BrowserApplication(KCmdLineArgs *args, const QString &serverName) - : KApplication() - , m_localServer(0) -{ - QLocalSocket socket; - socket.connectToServer(serverName); - if (socket.waitForConnected(500)) - { - QTextStream stream(&socket); - int n = args->count(); - if (n > 1) - stream << args->arg(n-1); - else - stream << QString(); - stream.flush(); - socket.waitForBytesWritten(); - return; - } - - KApplication::setQuitOnLastWindowClosed(true); - - m_localServer = new QLocalServer(this); - connect(m_localServer, SIGNAL(newConnection()), this, SLOT(newLocalSocketConnection())); - if (!m_localServer->listen(serverName)) - { - if (m_localServer->serverError() == QAbstractSocket::AddressInUseError - && QFile::exists(m_localServer->serverName())) - { - QFile::remove(m_localServer->serverName()); - m_localServer->listen(serverName); - } - } - - QDesktopServices::setUrlHandler(QLatin1String("http"), this, "openUrl"); - QString localSysName = QLocale::system().name(); - - KConfig config("rekonqrc"); - KConfigGroup group = config.group("sessions"); - m_lastSession = group.readEntry( QString("lastSession"), QByteArray() ); - - setWindowIcon( KIcon("rekonq") ); - - QTimer::singleShot(0, this, SLOT( postLaunch() ) ); -} - - -BrowserApplication::~BrowserApplication() -{ - qDeleteAll(m_mainWindows); - delete s_networkAccessManager; -} - - -BrowserApplication *BrowserApplication::instance() -{ - return (static_cast(QCoreApplication::instance())); -} - - -/*! - Any actions that can be delayed until the window is visible - */ -void BrowserApplication::postLaunch() -{ - QString directory = QDesktopServices::storageLocation(QDesktopServices::DataLocation); - if ( directory.isEmpty() ) - { - directory = QDir::homePath() + QLatin1String("/.") + QCoreApplication::applicationName(); - } - QWebSettings::setIconDatabasePath(directory); - - // newMainWindow() needs to be called in main() for this to happen - if (m_mainWindows.count() > 0) - { - KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); - int n = args->count(); - if (n > 1) - { - KUrl url = MainWindow::guessUrlFromString( args->arg(n-1) ); - mainWindow()->loadUrl( url ); - } - else - { - mainWindow()->slotHome(); - } - } - BrowserApplication::historyManager(); -} - - -void BrowserApplication::downloadUrl(const KUrl &srcUrl, const KUrl &destUrl) -{ - new Download( srcUrl, destUrl ); -} - - -QList BrowserApplication::mainWindows() -{ - clean(); - QList list; - for (int i = 0; i < m_mainWindows.count(); ++i) - { - list.append(m_mainWindows.at(i)); - } - return list; -} - - -void BrowserApplication::clean() -{ - // cleanup any deleted main windows first - for (int i = m_mainWindows.count() - 1; i >= 0; --i) - { - if (m_mainWindows.at(i).isNull()) - { - m_mainWindows.removeAt(i); - } - } -} - - -void BrowserApplication::saveSession() -{ - QWebSettings *globalSettings = QWebSettings::globalSettings(); - if ( globalSettings->testAttribute( QWebSettings::PrivateBrowsingEnabled ) ) - return; - - clean(); - - KConfig config("rekonqrc"); - KConfigGroup group = config.group("sessions"); - QByteArray data; - QBuffer buffer(&data); - QDataStream stream(&buffer); - buffer.open(QIODevice::ReadWrite); - - stream << m_mainWindows.count(); - for (int i = 0; i < m_mainWindows.count(); ++i) - { - stream << m_mainWindows.at(i)->saveState(); - } - - group.writeEntry( QString("lastSession"), data ); -} - - -bool BrowserApplication::canRestoreSession() const -{ - return !m_lastSession.isEmpty(); -} - - -void BrowserApplication::restoreLastSession() -{ - QList windows; - QBuffer buffer(&m_lastSession); - QDataStream stream(&buffer); - buffer.open(QIODevice::ReadOnly); - int windowCount; - stream >> windowCount; - for (int i = 0; i < windowCount; ++i) - { - QByteArray windowState; - stream >> windowState; - windows.append(windowState); - } - for (int i = 0; i < windows.count(); ++i) - { - MainWindow *newWindow = 0; - if (m_mainWindows.count() == 1 - && mainWindow()->tabWidget()->count() == 1 - && mainWindow()->currentTab()->url() == KUrl()) - { - newWindow = mainWindow(); - } - else - { - newWindow = newMainWindow(); - } - newWindow->restoreState(windows.at(i)); - } -} - - - -bool BrowserApplication::isTheOnlyBrowser() const -{ - return (m_localServer != 0); -} - - -void BrowserApplication::openUrl(const KUrl &url) -{ - mainWindow()->loadUrl(url); -} - - - -MainWindow *BrowserApplication::newMainWindow() -{ - MainWindow *browser = new MainWindow(); - m_mainWindows.prepend(browser); - browser->show(); - return browser; -} - - -MainWindow *BrowserApplication::mainWindow() -{ - clean(); - if (m_mainWindows.isEmpty()) - newMainWindow(); - return m_mainWindows[0]; -} - - -void BrowserApplication::newLocalSocketConnection() -{ - QLocalSocket *socket = m_localServer->nextPendingConnection(); - if (!socket) - return; - socket->waitForReadyRead(1000); - QTextStream stream(socket); - QString url; - stream >> url; - if (!url.isEmpty()) - { - mainWindow()->tabWidget()->newTab(); - openUrl(url); - } - delete socket; - mainWindow()->raise(); - mainWindow()->activateWindow(); -} - - - -CookieJar *BrowserApplication::cookieJar() -{ - return (CookieJar*)networkAccessManager()->cookieJar(); -} - - -NetworkAccessManager *BrowserApplication::networkAccessManager() -{ - if (!s_networkAccessManager) - { - s_networkAccessManager = new NetworkAccessManager(); - s_networkAccessManager->setCookieJar(new CookieJar); - } - return s_networkAccessManager; -} - - - -HistoryManager *BrowserApplication::historyManager() -{ - if (!s_historyManager) - { - s_historyManager = new HistoryManager(); - QWebHistoryInterface::setDefaultInterface(s_historyManager); - } - return s_historyManager; -} - - - - -KIcon BrowserApplication::icon(const KUrl &url) const -{ - KIcon icon = KIcon( QWebSettings::iconForUrl(url) ); - if (!icon.isNull()) - return icon; - if (m_defaultIcon.isNull()) - m_defaultIcon = KIcon("kde"); - return m_defaultIcon; -} - diff --git a/src/browserapplication.h b/src/browserapplication.h deleted file mode 100644 index 9b47bfcc..00000000 --- a/src/browserapplication.h +++ /dev/null @@ -1,90 +0,0 @@ -/* ============================================================ - * - * This file is a part of the rekonq project - * - * Copyright (C) 2007-2008 Trolltech ASA. All rights reserved - * Copyright (C) 2008 by Andrea Diamantini - * - * - * This program is free software; you can redistribute it - * and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; - * either version 2, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the - * GNU General Public License for more details. - * - * ============================================================ */ - - -#ifndef BROWSERAPPLICATION_H -#define BROWSERAPPLICATION_H - -// KDE Includes -#include -#include -#include -#include -#include -#include -#include - -// Qt Includes -#include - -QT_BEGIN_NAMESPACE -class QLocalServer; -QT_END_NAMESPACE - -class MainWindow; -class CookieJar; -class HistoryManager; -class NetworkAccessManager; - -class BrowserApplication : public KApplication -{ - Q_OBJECT - -public: - BrowserApplication(KCmdLineArgs*, const QString &); - ~BrowserApplication(); - static BrowserApplication *instance(); - - bool isTheOnlyBrowser() const; - MainWindow *mainWindow(); - QList mainWindows(); - KIcon icon(const KUrl &url) const; - void downloadUrl(const KUrl &srcUrl, const KUrl &destUrl); - - void saveSession(); - bool canRestoreSession() const; - - static HistoryManager *historyManager(); - static CookieJar *cookieJar(); - static NetworkAccessManager *networkAccessManager(); - -public slots: - MainWindow *newMainWindow(); - void restoreLastSession(); - -private slots: - void postLaunch(); - void openUrl(const KUrl &url); - void newLocalSocketConnection(); - -private: - void clean(); - - static HistoryManager *s_historyManager; - static NetworkAccessManager *s_networkAccessManager; - - QList > m_mainWindows; - QLocalServer *m_localServer; - QByteArray m_lastSession; - mutable KIcon m_defaultIcon; -}; - -#endif // BROWSERAPPLICATION_H - -- cgit v1.2.1