From 13d97d714688398aaa3437a52a42ae345e907bc2 Mon Sep 17 00:00:00 2001 From: "craig.p.drummond" Date: Tue, 30 Oct 2012 20:47:15 +0000 Subject: [PATCH] For Linux Qt builds, use dbus to determine single app status. --- CMakeLists.txt | 8 +++--- ChangeLog | 1 + application.cpp | 55 ++++++++++++++++++++++++++++------------ application.h | 18 ++++++++++--- dbus/org.kde.cantata.xml | 3 +++ gui/mainwindow.cpp | 19 +++++--------- gui/mainwindow.h | 7 +---- 7 files changed, 70 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a676f0d9..f4fce1aa5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -458,9 +458,11 @@ ELSE( ENABLE_KDE_SUPPORT ) ADD_DEFINITIONS( -DQT_NO_STL -DQT_NO_CAST_TO_ASCII -Wall -Wextra ) SET( XDG_APPS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/share/applications" ) - add_subdirectory(qtsingleapplication) - TARGET_LINK_LIBRARIES(cantata qtsingleapplication) - include_directories(${CMAKE_BINARY_DIR}/qtsingleapplication) + if (WIN32) + add_subdirectory(qtsingleapplication) + TARGET_LINK_LIBRARIES(cantata qtsingleapplication) + include_directories(${CMAKE_BINARY_DIR}/qtsingleapplication) + endif (WIN32) ENDIF( ENABLE_KDE_SUPPORT ) if (MTP_FOUND AND NOT WIN32) diff --git a/ChangeLog b/ChangeLog index c121de454..474cc6fc8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -80,6 +80,7 @@ 46. Korean translation - thanks to Min Ho Park. 47. Fix detection of audio codecs for ffmpeg 1.0. 48. Remove libmaia usage. +49. For Linux Qt builds, use dbus to determine single app status. 0.8.3.1 ------- diff --git a/application.cpp b/application.cpp index 5a2ae66eb..31c25caf3 100644 --- a/application.cpp +++ b/application.cpp @@ -29,7 +29,6 @@ #include #else #include -#include #ifdef Q_OS_WIN #include #include @@ -90,9 +89,9 @@ int Application::newInstance() { #ifdef TAGLIB_FOUND KCmdLineArgs *args(KCmdLineArgs::parsedArgs()); - QList urls; + QStringList urls; for (int i = 0; i < args->count(); ++i) { - urls.append(QUrl(args->url(i))); + urls.append(args->url(i)); } if (!urls.isEmpty()) { w->load(urls); @@ -109,7 +108,7 @@ void Application::mwDestroyed(QObject *obj) } } -#else // ENABLE_KDE_SUPPORT +#elif Q_OS_WIN Application::Application(int &argc, char **argv) : QtSingleApplication(argc, argv) { @@ -117,16 +116,9 @@ Application::Application(int &argc, char **argv) connect(this, SIGNAL(messageReceived(const QString &)), SLOT(message(const QString &))); #endif - #ifdef Q_OS_WIN connect(this, SIGNAL(reconnect()), MPDConnection::self(), SLOT(reconnect())); - #endif } -Application::~Application() -{ -} - -#ifdef Q_OS_WIN static void setupIconTheme() { // Check that we have certain icons in the selected icon theme. If not, and oxygen is installed, then @@ -164,7 +156,6 @@ bool Application::winEventFilter(MSG *msg, long *result) } return QCoreApplication::winEventFilter(msg, result); } -#endif bool Application::start() { @@ -180,9 +171,7 @@ bool Application::start() return false; } - #ifdef Q_OS_WIN setupIconTheme(); - #endif Icons::init(); return true; } @@ -216,9 +205,9 @@ void Application::load(const QStringList &files) return; } - QList urls; + QStringList urls; foreach (const QString &f, files) { - urls.append(QUrl(f)); + urls.append(f); } if (!urls.isEmpty()) { MainWindow *mw=qobject_cast(activationWindow()); @@ -229,4 +218,38 @@ void Application::load(const QStringList &files) } #endif // TAGLIB_FOUND +#else // Q_OS_WIN +#include +#include + +Application::Application(int &argc, char **argv) + : QApplication(argc, argv) +{ +} + +bool Application::start() +{ + if(QDBusConnection::sessionBus().registerService("org.kde.cantata")) { + Icons::init(); + return true; + } + loadFiles(); + return false; +} + +void Application::loadFiles() +{ + #ifdef TAGLIB_FOUND + QStringList args(arguments()); + if (args.count()>1) { + args.takeAt(0); + QDBusMessage m = QDBusMessage::createMethodCall("org.kde.cantata", "/cantata", "", "load"); + QList a; + a.append(args); + m.setArguments(a); + QDBusConnection::sessionBus().send(m); + } + #endif +} + #endif diff --git a/application.h b/application.h index a978bd00f..b837b6e2c 100644 --- a/application.h +++ b/application.h @@ -47,7 +47,7 @@ private Q_SLOTS: private: MainWindow *w; }; -#else +#elif defined Q_OS_WIN #include "qtsingleapplication/qtsingleapplication.h" class Application : public QtSingleApplication { @@ -55,11 +55,9 @@ class Application : public QtSingleApplication public: Application(int &argc, char **argv); - virtual ~Application(); + virtual ~Application() { } - #ifdef Q_OS_WIN bool winEventFilter(MSG *msg, long *result); - #endif bool start(); #if defined TAGLIB_FOUND void loadFiles(); @@ -76,6 +74,18 @@ private Q_SLOTS: Q_SIGNALS: void reconnect(); }; +#else +#include +class Application : public QApplication +{ +public: + Application(int &argc, char **argv); + virtual ~Application() { } + + bool start(); + void loadFiles(); + void setActivationWindow(QWidget *) { } +}; #endif #endif diff --git a/dbus/org.kde.cantata.xml b/dbus/org.kde.cantata.xml index b1bc43440..5ac095639 100644 --- a/dbus/org.kde.cantata.xml +++ b/dbus/org.kde.cantata.xml @@ -14,5 +14,8 @@ + + + diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index c5c1a4eb0..7c50bc7cf 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -255,14 +255,6 @@ MainWindow::MainWindow(QWidget *parent) #ifndef Q_OS_WIN new CantataAdaptor(this); - #ifndef ENABLE_KDE_SUPPORT - // We need to register for this interface, so that dynamic helper can send messages... - if (!QDBusConnection::sessionBus().registerService("org.kde.cantata")) { - // Couldn't register service, so probalby KDE version is running!!! - ::exit(0); - return; - } - #endif QDBusConnection::sessionBus().registerObject("/cantata", this); #endif setMinimumHeight(256); @@ -922,16 +914,17 @@ void MainWindow::initSizes() coverWidget->setMaximumSize(cwSize, cwSize); } -#ifdef TAGLIB_FOUND -void MainWindow::load(const QList &urls) +void MainWindow::load(const QStringList &urls) { + #ifdef TAGLIB_FOUND QStringList useable; bool haveHttp=HttpServer::self()->isAlive(); bool alwaysUseHttp=haveHttp && Settings::self()->alwaysUseHttp(); bool mpdLocal=MPDConnection::self()->getDetails().isLocal(); bool allowLocal=haveHttp || mpdLocal; - foreach (QUrl u, urls) { + foreach (const QString &path, urls) { + QUrl u(path); if (QLatin1String("http")==u.scheme()) { useable.append(u.toString()); } else if (allowLocal && (u.scheme().isEmpty() || QLatin1String("file")==u.scheme())) { @@ -945,8 +938,10 @@ void MainWindow::load(const QList &urls) if (useable.count()) { playQueueModel.addItems(useable, playQueueModel.rowCount(), false, 0); } + #else + Q_UNUSED(urls) + #endif } -#endif void MainWindow::playbackButtonsMenu() { diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 6a6288ad4..82189f2ca 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -171,10 +171,6 @@ public: return volume; } - #ifdef TAGLIB_FOUND - void load(const QList &urls); - #endif - int currentTrackPosition() const; QString coverFile() const; @@ -212,8 +208,7 @@ public Q_SLOTS: void showPage(const QString &page, bool focusSearch); void dynamicStatus(const QString &message); void restoreWindow(); - -public Q_SLOTS: + void load(const QStringList &urls); #ifdef ENABLE_KDE_SUPPORT void configureShortcuts(); void saveShortcuts();