From 406e1afde1fb8bfcd165d5dc0512ba277da91b2d Mon Sep 17 00:00:00 2001 From: "craig.p.drummond" Date: Tue, 3 Jun 2014 15:49:31 +0000 Subject: [PATCH] Split Application class up for KDE, Qt, Windows, and Mac builds --- CMakeLists.txt | 12 +- gui/application.cpp | 271 -------------------------------------- gui/application.h | 76 +---------- gui/application_kde.cpp | 98 ++++++++++++++ gui/application_kde.h | 51 +++++++ gui/application_mac.cpp | 33 +++++ gui/application_mac.h | 37 ++++++ gui/application_qt.cpp | 87 ++++++++++++ gui/application_qt.h | 39 ++++++ gui/application_win.cpp | 55 ++++++++ gui/application_win.h | 47 +++++++ gui/singleapplication.cpp | 88 +++++++++++++ gui/singleapplication.h | 50 +++++++ 13 files changed, 602 insertions(+), 342 deletions(-) create mode 100644 gui/application_kde.cpp create mode 100644 gui/application_kde.h create mode 100644 gui/application_mac.cpp create mode 100644 gui/application_mac.h create mode 100644 gui/application_qt.cpp create mode 100644 gui/application_qt.h create mode 100644 gui/application_win.cpp create mode 100644 gui/application_win.h create mode 100644 gui/singleapplication.cpp create mode 100644 gui/singleapplication.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ed63f910..8b0768bbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -549,7 +549,7 @@ if (ENABLE_KDE) add_definitions(${KDE4_DEFINITIONS}) qt4_add_resources(CANTATA_RC_SRCS ${CANTATA_RCS}) kde4_add_ui_files(CANTATA_UI_HDRS ${CANTATA_UIS}) - set(CANTATA_SRCS ${CANTATA_SRCS} gui/plurals_kde.cpp) + set(CANTATA_SRCS ${CANTATA_SRCS} gui/plurals_kde.cpp gui/application_kde.cpp) kde4_add_executable(cantata ${CANTATA_SRCS} ${CANTATA_MOC_SRCS} ${CANTATA_RC_SRCS} ${CANTATA_UI_HDRS}) target_link_libraries(cantata ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} ${KDE4_SOLID_LIBS} ${KDE4_KIO_LIBRARY}) if (NOT WIN32 AND NOT APPLE AND TAGLIB_FOUND AND ENABLE_REMOTE_DEVICES) @@ -565,8 +565,16 @@ elseif (ENABLE_UBUNTU) else (ENABLE_KDE) set(CANTATA_SRCS ${CANTATA_SRCS} gui/plurals_qt.cpp) if (WIN32 OR APPLE) - set(CANTATA_MOC_HDRS ${CANTATA_MOC_HDRS} gui/application.h) + set(CANTATA_MOC_HDRS ${CANTATA_MOC_HDRS} gui/singleapplication.h) + set(CANTATA_SRCS ${CANTATA_SRCS} gui/singleapplication.cpp) endif (WIN32 OR APPLE) + if (WIN32) + set(CANTATA_SRCS ${CANTATA_SRCS} gui/application_win.cpp) + elseif (APPLE) + set(CANTATA_SRCS ${CANTATA_SRCS} gui/application_mac.cpp) + else (WIN32) + set(CANTATA_SRCS ${CANTATA_SRCS} gui/application_qt.cpp) + endif (WIN32) if (WIN32) add_definitions(-DWIN32) add_subdirectory(windows) diff --git a/gui/application.cpp b/gui/application.cpp index 3cdc52234..5acf277c5 100644 --- a/gui/application.cpp +++ b/gui/application.cpp @@ -23,35 +23,13 @@ #include "application.h" #include "settings.h" -#ifdef ENABLE_KDE_SUPPORT -#include -#include -#include "initialsettingswizard.h" -#include "support/thread.h" -#include "mpd/song.h" -#else -#include -#ifdef Q_OS_WIN -#include -#include -#endif -#ifdef Q_OS_MAC -#include -#endif -#endif -#include "support/icon.h" -#include "widgets/icons.h" #include "support/utils.h" -#include "config.h" -#include "mainwindow.h" -#include "mpd/mpdconnection.h" #include "mpd/mpdstats.h" #include "mpd/mpdstatus.h" #include "support/thread.h" #ifdef ENABLE_EXTERNAL_TAGS #include "tags/taghelperiface.h" #endif -#include "support/gtkstyle.h" #include "scrobbling/scrobbler.h" void Application::initObjects() @@ -70,252 +48,3 @@ void Application::initObjects() Utils::setTouchFriendly(Settings::self()->touchFriendly()); } -#ifdef ENABLE_KDE_SUPPORT -#ifdef Q_WS_X11 -Application::Application(Display *display, Qt::HANDLE visual, Qt::HANDLE colormap) - : KUniqueApplication(display, visual, colormap) - , w(0) -{ -} -#endif - -Application::Application() - : KUniqueApplication() - , w(0) -{ -} - -Application::~Application() { - if (w) { - disconnect(w, SIGNAL(destroyed(QObject *)), this, SLOT(mwDestroyed(QObject *))); - delete w; - w=0; - } -} - -int Application::newInstance() { - static bool in=false; - if (in) { - return 0; - } else { - in=true; - } - - if (w) { - w->restoreWindow(); - } else { - initObjects(); - if (Settings::self()->firstRun()) { - InitialSettingsWizard wz; - if (QDialog::Rejected==wz.exec()) { - QApplication::exit(0); - return 0; - } - } - w=new MainWindow(); - connect(w, SIGNAL(destroyed(QObject *)), this, SLOT(mwDestroyed(QObject *))); - if (!Settings::self()->startHidden()) { - w->show(); - } - } - - KCmdLineArgs *args(KCmdLineArgs::parsedArgs()); - QStringList urls; - for (int i = 0; i < args->count(); ++i) { - urls.append(args->arg(i)); - } - if (!urls.isEmpty()) { - w->load(urls); - } - KStartupInfo::appStarted(startupId()); - in=false; - return 0; -} - -void Application::mwDestroyed(QObject *obj) -{ - if (obj==w) { - w=0; - } -} - -#elif defined Q_OS_WIN || defined Q_OS_MAC -Application::Application(int &argc, char **argv) - : QtSingleApplication(argc, argv) -{ - connect(this, SIGNAL(messageReceived(const QString &)), SLOT(message(const QString &))); - - #if defined Q_OS_WIN - connect(this, SIGNAL(reconnect()), MPDConnection::self(), SLOT(reconnect())); - #if QT_VERSION >= 0x050000 - installNativeEventFilter(this); - #endif // QT_VERSION >= 0x050000 - #endif // Q_OS_WIN - - #ifdef Q_OS_MAC - QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, true); - #endif -} - -static void setupIconTheme() -{ - #ifdef Q_OS_MAC - //QIcon::setThemeSearchPaths(QStringList(QCoreApplication::applicationDirPath() + "/../Resources/icons")); - QIcon::setThemeName(QLatin1String("oxygen")); - #else - QString cfgTheme=Settings::self()->iconTheme(); - if (!cfgTheme.isEmpty()) { - QIcon::setThemeName(cfgTheme); - } - - QString theme=Icon::themeName().toLower(); - if (QLatin1String("oxygen")!=theme && !QIcon::hasThemeIcon("document-save-as")) { - QStringList paths=QIcon::themeSearchPaths(); - - foreach (const QString &p, paths) { - if (QDir(p+QLatin1String("/oxygen")).exists()) { - QIcon::setThemeName(QLatin1String("oxygen")); - return; - } - } - } - #endif -} - -#if defined Q_OS_WIN -#if QT_VERSION >= 0x050000 -bool Application::nativeEventFilter(const QByteArray &, void *message, long *result) -{ - MSG *msg = static_cast(message); - if (msg && WM_POWERBROADCAST==msg->message && PBT_APMRESUMEAUTOMATIC==msg->wParam) { - emit reconnect(); - } - return false; -} -#else // QT_VERSION >= 0x050000 -bool Application::winEventFilter(MSG *msg, long *result) -{ - if (msg && WM_POWERBROADCAST==msg->message && PBT_APMRESUMEAUTOMATIC==msg->wParam) { - emit reconnect(); - } - return QCoreApplication::winEventFilter(msg, result); -} -#endif // QT_VERSION >= 0x050000 -#endif // Q_OS_WIN - -bool Application::start() -{ - if (isRunning()) { - QStringList args(arguments()); - if (args.count()>1) { - args.takeAt(0); - sendMessage(args.join("\n")); - } else { - sendMessage(QString()); - } - return false; - } - - setupIconTheme(); - return true; -} - -void Application::message(const QString &msg) -{ - if (!msg.isEmpty()) { - load(msg.split("\n")); - } - MainWindow *mw=qobject_cast(activationWindow()); - if (mw) { - mw->restoreWindow(); - } -} - -void Application::loadFiles() -{ - QStringList args(arguments()); - if (args.count()>1) { - args.takeAt(0); - load(args); - } -} - -void Application::load(const QStringList &files) -{ - if (files.isEmpty()) { - return; - } - - QStringList urls; - foreach (const QString &f, files) { - urls.append(f); - } - if (!urls.isEmpty()) { - MainWindow *mw=qobject_cast(activationWindow()); - if (mw) { - mw->load(urls); - } - } -} - -#else // Q_OS_WIN || Q_OS_MAC -#include -#include -#include - -Application::Application(int &argc, char **argv) - : QApplication(argc, argv) -{ -} - -bool Application::start() -{ - if (QDBusConnection::sessionBus().registerService(CANTATA_REV_URL)) { - setupIconTheme(); - return true; - } - loadFiles(); - // ...and activate window! - QDBusConnection::sessionBus().send(QDBusMessage::createMethodCall("com.googlecode.cantata", "/org/mpris/MediaPlayer2", "", "Raise")); - return false; -} - -void Application::setupIconTheme() -{ - QString cfgTheme=Settings::self()->iconTheme(); - if (cfgTheme.isEmpty() && GtkStyle::isActive()) { - cfgTheme=GtkStyle::iconTheme(); - } - if (!cfgTheme.isEmpty()) { - QIcon::setThemeName(cfgTheme); - } - - // BUG:130 Some non-DE environments (IceWM, etc) seem to set theme to HiColor, and this has missing - // icons. So check for a themed icon, if the current theme does not have this - then see if oxygen - // or gnome icon themes are installed, and set theme to one of those. - if (!QIcon::hasThemeIcon("document-save-as")) { - QStringList themes=QStringList() << QLatin1String("oxygen") << QLatin1String("gnome"); - foreach (const QString &theme, themes) { - QString dir(QLatin1String("/usr/share/icons/")+theme); - if (QDir(dir).exists()) { - QIcon::setThemeName(theme); - return; - } - } - } -} - -void Application::loadFiles() -{ - QStringList args(arguments()); - if (args.count()>1) { - args.takeAt(0); - QDBusMessage m = QDBusMessage::createMethodCall("com.googlecode.cantata", "/cantata", "", "load"); - QList a; - a.append(args); - m.setArguments(a); - QDBusConnection::sessionBus().send(m); - } -} - -#endif diff --git a/gui/application.h b/gui/application.h index 217b24e2e..ca11b042f 100644 --- a/gui/application.h +++ b/gui/application.h @@ -26,77 +26,15 @@ #include "config.h" #include + #ifdef ENABLE_KDE_SUPPORT -#include -class MainWindow; -class Application : public KUniqueApplication -{ - Q_OBJECT - -public: - static void initObjects(); - #ifdef Q_WS_X11 - Application(Display *display, Qt::HANDLE visual, Qt::HANDLE colormap); - #endif - Application(); - ~Application(); - - int newInstance(); - -private Q_SLOTS: - void mwDestroyed(QObject *obj); - -private: - MainWindow *w; -}; -#elif defined Q_OS_WIN || defined WIN32 || defined Q_OS_MAC || defined __APPLE__ // moc does not seem to see Q_OS_WIN/Q_OS_MAC,, but will see WIN32/__APPLE__ :-( -#include "qtsingleapplication/qtsingleapplication.h" -#if (defined Q_OS_WIN || defined WIN32) && QT_VERSION >= 0x050000 -#include -class Application : public QtSingleApplication, public QAbstractNativeEventFilter + #include "application_kde.h" +#elif defined Q_OS_WIN + #include "application_win.h" +#elif defined Q_OS_MAC + #include "application_mac.h" #else -class Application : public QtSingleApplication -#endif -{ - Q_OBJECT - -public: - static void initObjects(); - Application(int &argc, char **argv); - virtual ~Application() { } - - #if defined Q_OS_WIN || defined WIN32 - #if QT_VERSION >= 0x050000 - bool nativeEventFilter(const QByteArray &, void *message, long *result); - #else - bool winEventFilter(MSG *msg, long *result); - #endif - #endif - bool start(); - void loadFiles(); - -private: - void load(const QStringList &files); - -private Q_SLOTS: - void message(const QString &m); - -Q_SIGNALS: - void reconnect(); -}; -#else -#include -class Application : public QApplication -{ -public: - static void initObjects(); - Application(int &argc, char **argv); - virtual ~Application() { } - - bool start(); - void loadFiles(); - void setupIconTheme(); -}; + #include "application_qt.h" #endif #endif diff --git a/gui/application_kde.cpp b/gui/application_kde.cpp new file mode 100644 index 000000000..ed208ed1c --- /dev/null +++ b/gui/application_kde.cpp @@ -0,0 +1,98 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "application_kde.h" +#include "initialsettingswizard.h" +#include "mainwindow.h" +#include "settings.h" +#include +#include + +#ifdef Q_WS_X11 +Application::Application(Display *display, Qt::HANDLE visual, Qt::HANDLE colormap) + : KUniqueApplication(display, visual, colormap) + , w(0) +{ +} +#endif + +Application::Application() + : KUniqueApplication() + , w(0) +{ +} + +Application::~Application() { + if (w) { + disconnect(w, SIGNAL(destroyed(QObject *)), this, SLOT(mwDestroyed(QObject *))); + delete w; + w=0; + } +} + +int Application::newInstance() { + static bool in=false; + if (in) { + return 0; + } else { + in=true; + } + + if (w) { + w->restoreWindow(); + } else { + initObjects(); + if (Settings::self()->firstRun()) { + InitialSettingsWizard wz; + if (QDialog::Rejected==wz.exec()) { + QApplication::exit(0); + return 0; + } + } + w=new MainWindow(); + connect(w, SIGNAL(destroyed(QObject *)), this, SLOT(mwDestroyed(QObject *))); + if (!Settings::self()->startHidden()) { + w->show(); + } + } + + KCmdLineArgs *args(KCmdLineArgs::parsedArgs()); + QStringList urls; + for (int i = 0; i < args->count(); ++i) { + urls.append(args->arg(i)); + } + if (!urls.isEmpty()) { + w->load(urls); + } + KStartupInfo::appStarted(startupId()); + in=false; + return 0; +} + +void Application::mwDestroyed(QObject *obj) +{ + if (obj==w) { + w=0; + } +} + diff --git a/gui/application_kde.h b/gui/application_kde.h new file mode 100644 index 000000000..321b00ba9 --- /dev/null +++ b/gui/application_kde.h @@ -0,0 +1,51 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef APPLICATION_KDE_H +#define APPLICATION_KDE_H + +#include +#include +class MainWindow; +class Application : public KUniqueApplication +{ + Q_OBJECT + +public: + static void initObjects(); + #ifdef Q_WS_X11 + Application(Display *display, Qt::HANDLE visual, Qt::HANDLE colormap); + #endif + Application(); + ~Application(); + + int newInstance(); + +private Q_SLOTS: + void mwDestroyed(QObject *obj); + +private: + MainWindow *w; +}; + +#endif diff --git a/gui/application_mac.cpp b/gui/application_mac.cpp new file mode 100644 index 000000000..66416b924 --- /dev/null +++ b/gui/application_mac.cpp @@ -0,0 +1,33 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "application_mac.h" +#include + +Application::Application(int &argc, char **argv) + : SingleApplication(argc, argv) +{ + QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, true); + QIcon::setThemeName(QLatin1String("oxygen")); +} + diff --git a/gui/application_mac.h b/gui/application_mac.h new file mode 100644 index 000000000..f3e1e67eb --- /dev/null +++ b/gui/application_mac.h @@ -0,0 +1,37 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef APPLICATION_MAC_H +#define APPLICATION_MAC_H + +#include "singleapplication.h" + +class Application : public SingleApplication +{ +public: + static void initObjects(); + Application(int &argc, char **argv); + virtual ~Application() { }; +}; + +#endif diff --git a/gui/application_qt.cpp b/gui/application_qt.cpp new file mode 100644 index 000000000..fef0a5b5c --- /dev/null +++ b/gui/application_qt.cpp @@ -0,0 +1,87 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "application_qt.h" +#include "settings.h" +#include "support/gtkstyle.h" +#include "config.h" +#include +#include +#include +#include + +static void setupIconTheme() +{ + QString cfgTheme=Settings::self()->iconTheme(); + if (cfgTheme.isEmpty() && GtkStyle::isActive()) { + cfgTheme=GtkStyle::iconTheme(); + } + if (!cfgTheme.isEmpty()) { + QIcon::setThemeName(cfgTheme); + } + + // BUG:130 Some non-DE environments (IceWM, etc) seem to set theme to HiColor, and this has missing + // icons. So check for a themed icon, if the current theme does not have this - then see if oxygen + // or gnome icon themes are installed, and set theme to one of those. + if (!QIcon::hasThemeIcon("document-save-as")) { + QStringList themes=QStringList() << QLatin1String("oxygen") << QLatin1String("gnome"); + foreach (const QString &theme, themes) { + QString dir(QLatin1String("/usr/share/icons/")+theme); + if (QDir(dir).exists()) { + QIcon::setThemeName(theme); + return; + } + } + } +} + +Application::Application(int &argc, char **argv) + : QApplication(argc, argv) +{ +} + +bool Application::start() +{ + if (QDBusConnection::sessionBus().registerService(CANTATA_REV_URL)) { + setupIconTheme(); + return true; + } + loadFiles(); + // ...and activate window! + QDBusConnection::sessionBus().send(QDBusMessage::createMethodCall("com.googlecode.cantata", "/org/mpris/MediaPlayer2", "", "Raise")); + return false; +} + +void Application::loadFiles() +{ + QStringList args(arguments()); + if (args.count()>1) { + args.takeAt(0); + QDBusMessage m = QDBusMessage::createMethodCall("com.googlecode.cantata", "/cantata", "", "load"); + QList a; + a.append(args); + m.setArguments(a); + QDBusConnection::sessionBus().send(m); + } +} + diff --git a/gui/application_qt.h b/gui/application_qt.h new file mode 100644 index 000000000..9ce596d31 --- /dev/null +++ b/gui/application_qt.h @@ -0,0 +1,39 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef APPLICATION_QT_H +#define APPLICATION_QT_H + +#include +class Application : public QApplication +{ +public: + static void initObjects(); + Application(int &argc, char **argv); + virtual ~Application() { } + + bool start(); + void loadFiles(); +}; + +#endif diff --git a/gui/application_win.cpp b/gui/application_win.cpp new file mode 100644 index 000000000..c367c4c98 --- /dev/null +++ b/gui/application_win.cpp @@ -0,0 +1,55 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "application_win.h" +#include +#include + +Application::Application(int &argc, char **argv) + : SingleApplication(argc, argv) +{ + #if QT_VERSION >= 0x050000 + installNativeEventFilter(this); + #endif + QIcon::setThemeName(QLatin1String("oxygen")); +} + +#if QT_VERSION >= 0x050000 +bool Application::nativeEventFilter(const QByteArray &, void *message, long *result) +{ + MSG *msg = static_cast(message); + if (msg && WM_POWERBROADCAST==msg->message && PBT_APMRESUMEAUTOMATIC==msg->wParam) { + emit reconnect(); + } + return false; +} +#else +bool Application::winEventFilter(MSG *msg, long *result) +{ + if (msg && WM_POWERBROADCAST==msg->message && PBT_APMRESUMEAUTOMATIC==msg->wParam) { + emit reconnect(); + } + return QCoreApplication::winEventFilter(msg, result); +} +#endif + diff --git a/gui/application_win.h b/gui/application_win.h new file mode 100644 index 000000000..832157f60 --- /dev/null +++ b/gui/application_win.h @@ -0,0 +1,47 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef APPLICATION_WIN_H +#define APPLICATION_WIN_H + +#include "singleapplication.h" + +#if QT_VERSION >= 0x050000 +#include +class Application : public SingleApplication, public QAbstractNativeEventFilter +#else +class Application : public SingleApplication +#endif +{ +public: + static void initObjects(); + Application(int &argc, char **argv); + virtual ~Application() { } + #if QT_VERSION >= 0x050000 + bool nativeEventFilter(const QByteArray &, void *message, long *result); + #else + bool winEventFilter(MSG *msg, long *result); + #endif +}; + +#endif diff --git a/gui/singleapplication.cpp b/gui/singleapplication.cpp new file mode 100644 index 000000000..1130f2647 --- /dev/null +++ b/gui/singleapplication.cpp @@ -0,0 +1,88 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "singleapplication.h" +#include "mainwindow.h" +#include "mpd/mpdconnection.h" + +SingleApplication::SingleApplication(int &argc, char **argv) + : QtSingleApplication(argc, argv) +{ + connect(this, SIGNAL(messageReceived(const QString &)), SLOT(message(const QString &))); + connect(this, SIGNAL(reconnect()), MPDConnection::self(), SLOT(reconnect())); +} + +bool SingleApplication::start() +{ + if (isRunning()) { + QStringList args(arguments()); + if (args.count()>1) { + args.takeAt(0); + sendMessage(args.join("\n")); + } else { + sendMessage(QString()); + } + return false; + } + + return true; +} + +void SingleApplication::message(const QString &msg) +{ + if (!msg.isEmpty()) { + load(msg.split("\n")); + } + MainWindow *mw=qobject_cast(activationWindow()); + if (mw) { + mw->restoreWindow(); + } +} + +void SingleApplication::loadFiles() +{ + QStringList args(arguments()); + if (args.count()>1) { + args.takeAt(0); + load(args); + } +} + +void SingleApplication::load(const QStringList &files) +{ + if (files.isEmpty()) { + return; + } + + QStringList urls; + foreach (const QString &f, files) { + urls.append(f); + } + if (!urls.isEmpty()) { + MainWindow *mw=qobject_cast(activationWindow()); + if (mw) { + mw->load(urls); + } + } +} + diff --git a/gui/singleapplication.h b/gui/singleapplication.h new file mode 100644 index 000000000..9f6782a59 --- /dev/null +++ b/gui/singleapplication.h @@ -0,0 +1,50 @@ +/* + * Cantata + * + * Copyright (c) 2011-2014 Craig Drummond + * + * ---- + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef SINGLE_APPLICATION_H +#define SINGLE_APPLICATION_H + +#include "qtsingleapplication/qtsingleapplication.h" + +class SingleApplication : public QtSingleApplication +{ + Q_OBJECT + +public: + SingleApplication(int &argc, char **argv); + virtual ~SingleApplication() { } + + bool start(); + void loadFiles(); + +private: + void load(const QStringList &files); + +private Q_SLOTS: + void message(const QString &m); + +Q_SIGNALS: + void reconnect(); +}; + +#endif