From a34f7ad76fe02227cc59143d91dabb891c2e3605 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Wed, 13 Mar 2013 17:50:00 +0100 Subject: Support Activities Open links in new tab only if there is a window in the current activity/on the current desktop This code has been written by Jonathan Verner and reviewed (a bit) by me. Hope everyone will be happy now... BUG: 316322 REVIEWED-BY: adjam --- src/application.cpp | 130 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 93 insertions(+), 37 deletions(-) (limited to 'src/application.cpp') diff --git a/src/application.cpp b/src/application.cpp index dfcfc59a..002f19d1 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -69,6 +69,11 @@ #include +#include +#ifdef HAVE_KACTIVITIES +#include +#endif + // Qt Includes #include #include @@ -79,6 +84,9 @@ Application::Application() : KUniqueApplication() { +#ifdef HAVE_KACTIVITIES + m_activityConsumer = new KActivities::Consumer(); +#endif } @@ -89,6 +97,10 @@ Application::~Application() ReKonfig::setRecoverOnCrash(0); saveConfiguration(); +#ifdef HAVE_KACTIVITIES + delete m_activityConsumer; +#endif + kDebug() << "Bye bye (k)baby..."; } @@ -355,63 +367,55 @@ void Application::saveConfiguration() const } -RekonqWindow *Application::rekonqWindow() +// -------------------------------------------------------------------------------------------------------------------- + + +RekonqWindow *Application::rekonqWindow(const QString & activityID) { RekonqWindow *active = qobject_cast(QApplication::activeWindow()); if (!active) { - if (m_rekonqWindows.isEmpty()) - return 0; + RekonqWindowList wList = m_rekonqWindows; - Q_FOREACH(const QWeakPointer &pointer, m_rekonqWindows) +#ifdef HAVE_KACTIVITIES + wList = tabsForActivity(activityID); +#endif + + if (wList.isEmpty()) + return 0; + + Q_FOREACH(const QWeakPointer &pointer, wList) { if (KWindowInfo(pointer.data()->effectiveWinId(), NET::WMDesktop, 0).isOnCurrentDesktop()) return pointer.data(); } - return m_rekonqWindows.at(0).data(); + return wList.at(0).data(); } return active; } -void Application::loadUrl(const KUrl& url, const Rekonq::OpenType& type) +RekonqWindowList Application::tabsForActivity(const QString & activityID) { - if (url.isEmpty()) - return; - - if (!url.isValid()) - { - KMessageBox::error(0, i18n("Malformed URL:\n%1", url.url(KUrl::RemoveTrailingSlash))); - return; - } - - Rekonq::OpenType newType = type; - // Don't open useless tabs or windows for actions in about: pages - if (url.url().contains("about:") && url.url().contains("/")) - newType = Rekonq::CurrentTab; +#ifdef HAVE_KACTIVITIES + QString id = activityID; + if ( id.isEmpty() ) + id = m_activityConsumer->currentActivity(); + return m_activityRekonqWindowsMap[id]; +#else + return m_rekonqWindows; +#endif +} - RekonqWindow *w = 0; - if (newType == Rekonq::NewPrivateWindow) - { - w = newWindow(true, true); - newType = Rekonq::CurrentTab; - } - else if (newType == Rekonq::NewWindow - || ((newType == Rekonq::NewTab || newType == Rekonq::NewFocusedTab) && rekonqWindowList().count() == 0)) - { - w = newWindow(); - newType = Rekonq::CurrentTab; - } - else - { - w = rekonqWindow(); - } - w->loadUrl(url, newType); +bool Application::haveWindowsForActivity(const QString & activityID) +{ + return (!tabsForActivity(activityID).isEmpty()); } + RekonqWindow *Application::newWindow(bool withTab, bool PrivateBrowsingMode) { RekonqWindow *w = new RekonqWindow(withTab, PrivateBrowsingMode); @@ -424,6 +428,12 @@ RekonqWindow *Application::newWindow(bool withTab, bool PrivateBrowsingMode) w->installEventFilter(this); m_rekonqWindows.prepend(w); + +#ifdef HAVE_KACTIVITIES + QString currentActivity = m_activityConsumer->currentActivity(); + m_activityRekonqWindowsMap[currentActivity].prepend(w); +#endif + w->show(); return w; @@ -455,6 +465,9 @@ WebAppList Application::webAppList() } +// ------------------------------------------------------------------------------------------------------------------ + + bool Application::eventFilter(QObject* watched, QEvent* event) { // Track which window was activated most recently to prefer it on window choosing @@ -481,8 +494,14 @@ bool Application::eventFilter(QObject* watched, QEvent* event) { RekonqWindow *window = qobject_cast(watched); if (window) + { m_rekonqWindows.removeOne(window); - +#ifdef HAVE_KACTIVITIES + QString currentActivity = m_activityConsumer->currentActivity(); + m_activityRekonqWindowsMap[currentActivity].removeOne(window); +#endif + } + WebTab *webApp = qobject_cast(watched); m_webApps.removeOne(webApp); @@ -494,6 +513,43 @@ bool Application::eventFilter(QObject* watched, QEvent* event) } +void Application::loadUrl(const KUrl& url, const Rekonq::OpenType& type) +{ + if (url.isEmpty()) + return; + + if (!url.isValid()) + { + KMessageBox::error(0, i18n("Malformed URL:\n%1", url.url(KUrl::RemoveTrailingSlash))); + return; + } + + Rekonq::OpenType newType = type; + // Don't open useless tabs or windows for actions in about: pages + if (url.url().contains("about:") && url.url().contains("/")) + newType = Rekonq::CurrentTab; + + RekonqWindow *w = 0; + if (newType == Rekonq::NewPrivateWindow) + { + w = newWindow(true, true); + newType = Rekonq::CurrentTab; + } + else if (newType == Rekonq::NewWindow + || ((newType == Rekonq::NewTab || newType == Rekonq::NewFocusedTab) && !haveWindowsForActivity())) + { + w = newWindow(); + newType = Rekonq::CurrentTab; + } + else + { + w = rekonqWindow(); + } + + w->loadUrl(url, newType); +} + + void Application::updateConfiguration() { // ============== Tabs ================== -- cgit v1.2.1