From 4150695c5fff0504cf19ad74b3f185bd67397497 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Mon, 16 Feb 2009 00:06:28 +0100 Subject: new singleton Application class. Try 1.. --- src/application.cpp | 296 ++++++++++++++++++++-------------------------------- src/application.h | 42 ++++---- src/main.cpp | 16 +-- src/mainwindow.cpp | 34 +----- src/webview.cpp | 22 ++-- 5 files changed, 156 insertions(+), 254 deletions(-) (limited to 'src') diff --git a/src/application.cpp b/src/application.cpp index 40addcac..d6679b97 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -18,11 +18,14 @@  * ============================================================ */ -// Local Includes +// Self Includes #include "application.h" +#include "application.moc" +// Auto Includes #include "rekonq.h" +// Local Includes #include "mainwindow.h" #include "cookiejar.h" #include "history.h" @@ -56,46 +59,48 @@ HistoryManager *Application::s_historyManager = 0; NetworkAccessManager *Application::s_networkAccessManager = 0; - -Application::Application(KCmdLineArgs *args, const QString &serverName) - : KApplication() - , m_localServer(0) +Application::Application() + : KUniqueApplication() +// , 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() ); + m_mainWindow = new MainWindow(); + m_mainWindow->show(); + +// 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") ); @@ -105,147 +110,85 @@ Application::Application(KCmdLineArgs *args, const QString &serverName) Application::~Application() { - qDeleteAll(m_mainWindows); delete s_networkAccessManager; } -Application *Application::instance() -{ - return (static_cast(QCoreApplication::instance())); -} - - -/*! - Any actions that can be delayed until the window is visible - */ -void Application::postLaunch() +int Application::newInstance() { - 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::setCwd(QDir::currentPath().toUtf8()); + KCmdLineArgs* args = KCmdLineArgs::parsedArgs(); + static bool first = true; + if (args->count() > 0) { - KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); - int n = args->count(); - if (n > 1) + for (int i = 0; i < args->count(); ++i) { - KUrl url = MainWindow::guessUrlFromString( args->arg(n-1) ); + KUrl url = MainWindow::guessUrlFromString( args->arg(i) ); + newTab(); mainWindow()->loadUrl( url ); } - else + } + else + { + if( !first || !isSessionRestored()) { + newTab(); mainWindow()->slotHome(); } } - Application::historyManager(); -} - - -void Application::downloadUrl(const KUrl &srcUrl, const KUrl &destUrl) -{ - new Download( srcUrl, destUrl ); -} - - -QList Application::mainWindows() -{ - clean(); - QList list; - for (int i = 0; i < m_mainWindows.count(); ++i) - { - list.append(m_mainWindows.at(i)); - } - return list; + first = false; + args->clear(); + return 0; } -void Application::clean() +Application *Application::instance() { - // 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); - } - } + return (static_cast(QCoreApplication::instance())); } -void Application::saveSession() +void Application::postLaunch() { - 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) + // set Icon Database Path to store "favicons" associated with web sites + QString directory = QDesktopServices::storageLocation(QDesktopServices::DataLocation); + if ( directory.isEmpty() ) { - stream << m_mainWindows.at(i)->saveState(); + directory = QDir::homePath() + QLatin1String("/.") + QCoreApplication::applicationName(); } - - group.writeEntry( QString("lastSession"), data ); -} - + QWebSettings::setIconDatabasePath(directory); -bool Application::canRestoreSession() const -{ - return !m_lastSession.isEmpty(); + Application::historyManager(); } -void Application::restoreLastSession() +void Application::downloadUrl(const KUrl &srcUrl, const KUrl &destUrl) { - 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)); - } + new Download( srcUrl, destUrl ); } - -bool Application::isTheOnlyBrowser() const -{ - return (m_localServer != 0); -} +// void Application::saveSession() +// { +// QWebSettings *globalSettings = QWebSettings::globalSettings(); +// if ( globalSettings->testAttribute( QWebSettings::PrivateBrowsingEnabled ) ) +// return; +// +// 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 ); +// } void Application::openUrl(const KUrl &url) @@ -254,43 +197,36 @@ void Application::openUrl(const KUrl &url) } - -MainWindow *Application::newMainWindow() +MainWindow *Application::mainWindow() { - MainWindow *browser = new MainWindow(); - m_mainWindows.prepend(browser); - browser->show(); - return browser; + return m_mainWindow; } -MainWindow *Application::mainWindow() +WebView *Application::newTab() { - clean(); - if (m_mainWindows.isEmpty()) - newMainWindow(); - return m_mainWindows[0]; + return m_mainWindow->tabWidget()->newTab(); } -void Application::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(); -} +// void Application::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(); +// } @@ -323,8 +259,6 @@ HistoryManager *Application::historyManager() } - - KIcon Application::icon(const KUrl &url) const { KIcon icon = KIcon( QWebSettings::iconForUrl(url) ); diff --git a/src/application.h b/src/application.h index 32d810f0..30f1cc5b 100644 --- a/src/application.h +++ b/src/application.h @@ -22,7 +22,7 @@ #define REKONQ_APPLICATION_H // KDE Includes -#include +#include #include #include #include @@ -38,53 +38,53 @@ class QLocalServer; QT_END_NAMESPACE class MainWindow; +class WebView; class CookieJar; class HistoryManager; class NetworkAccessManager; -/* - * - */ -class Application : public KApplication +/** + * + */ +class Application : public KUniqueApplication { Q_OBJECT public: - Application(KCmdLineArgs*, const QString &); + Application(); ~Application(); + int newInstance(); static Application *instance(); - bool isTheOnlyBrowser() const; MainWindow *mainWindow(); - QList mainWindows(); + WebView* newTab(); + KIcon icon(const KUrl &url) const; - void downloadUrl(const KUrl &srcUrl, const KUrl &destUrl); - void saveSession(); - bool canRestoreSession() const; + /** + * This method lets you to download a file from a source remote url + * to a local destination url. + */ + void downloadUrl(const KUrl &srcUrl, const KUrl &destUrl); static HistoryManager *historyManager(); static CookieJar *cookieJar(); static NetworkAccessManager *networkAccessManager(); -public slots: - MainWindow *newMainWindow(); - void restoreLastSession(); - private slots: + + /** + * Any actions that can be delayed until the window is visible + */ void postLaunch(); void openUrl(const KUrl &url); - void newLocalSocketConnection(); +// void newLocalSocketConnection(); private: - void clean(); - static HistoryManager *s_historyManager; static NetworkAccessManager *s_networkAccessManager; - QList > m_mainWindows; - QLocalServer *m_localServer; - QByteArray m_lastSession; + MainWindow* m_mainWindow; mutable KIcon m_defaultIcon; }; diff --git a/src/main.cpp b/src/main.cpp index 9a779783..a97bb0d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,11 +23,12 @@ #include #include +#include static const char description[] = I18N_NOOP("KDE Browser Webkit Based"); -static const char version[] = "0.0.3"; +static const char version[] = "0.0.4"; int main(int argc, char **argv) { @@ -53,13 +54,14 @@ int main(int argc, char **argv) KCmdLineOptions options; options.add( "+URL" , ki18n("Location to open") ); KCmdLineArgs::addCmdLineOptions( options ); + Application::addCmdLineOptions(); - KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); - - Application app(args, "rekonq"); - if (!app.isTheOnlyBrowser()) - return 0; - app.newMainWindow(); + if (!Application::start()) + { + kWarning() << "rekonq is already running!"; + return 0; + } + Application app; return app.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index ee028abb..91ee8028 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -74,9 +74,6 @@ MainWindow::MainWindow() // updating rekonq configuration slotUpdateConf(); - // creating a new tab - m_view->newTab(); - // creating a centralWidget containing m_view and the hidden findbar QWidget *centralWidget = new QWidget; QVBoxLayout *layout = new QVBoxLayout; @@ -444,9 +441,8 @@ void MainWindow::slotUpdateWindowTitle(const QString &title) void MainWindow::slotFileNew() { - Application::instance()->newMainWindow(); - MainWindow *mw = Application::instance()->mainWindow(); - mw->slotHome(); + Application::instance()->newTab(); + slotHome(); } @@ -520,13 +516,9 @@ void MainWindow::slotPrivateBrowsing() { settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, false); - QList windows = Application::instance()->mainWindows(); - for (int i = 0; i < windows.count(); ++i) - { - MainWindow *window = windows.at(i); - window->m_lastSearch = QString::null; - window->tabWidget()->clear(); - } + MainWindow* win = Application::instance()->mainWindow(); + win->m_lastSearch = QString::null; + win->tabWidget()->clear(); } } @@ -726,22 +718,6 @@ void MainWindow::slotAboutToShowBackMenu() } -// void MainWindow::slotShowWindow() -// { -// if (KAction *action = qobject_cast(sender())) -// { -// QVariant v = action->data(); -// if (v.canConvert()) -// { -// int offset = qvariant_cast(v); -// QList windows = Application::instance()->mainWindows(); -// windows.at(offset)->activateWindow(); -// windows.at(offset)->currentTab()->setFocus(); -// } -// } -// } - - void MainWindow::slotOpenActionUrl(QAction *action) { int offset = action->data().toInt(); diff --git a/src/webview.cpp b/src/webview.cpp index ab8ef5cd..850ff54b 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -76,21 +76,8 @@ bool WebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &r if ( type == QWebPage::NavigationTypeLinkClicked && (m_keyboardModifiers & Qt::ControlModifier || m_pressedButtons == Qt::MidButton) ) { - bool newWindow = (m_keyboardModifiers & Qt::AltModifier); - WebView *webView; - if (newWindow) - { - Application::instance()->newMainWindow(); - MainWindow *newMainWindow = Application::instance()->mainWindow(); - webView = newMainWindow->currentTab(); - newMainWindow->raise(); - newMainWindow->activateWindow(); - webView->setFocus(); - } - else - { - webView = mainWindow()->tabWidget()->newTab( true ); - } + WebView *webView = Application::instance()->newTab(); + webView->setFocus(); webView->load(request); m_keyboardModifiers = Qt::NoModifier; m_pressedButtons = Qt::NoButton; @@ -109,13 +96,16 @@ QWebPage *WebPage::createWindow(QWebPage::WebWindowType type) { Q_UNUSED(type); if (m_keyboardModifiers & Qt::ControlModifier || m_pressedButtons == Qt::MidButton) + { m_openInNewTab = true; + } + if (m_openInNewTab) { m_openInNewTab = false; return mainWindow()->tabWidget()->newTab()->page(); } - Application::instance()->newMainWindow(); + MainWindow *mainWindow = Application::instance()->mainWindow(); return mainWindow->currentTab()->page(); } -- cgit v1.2.1