Split Application class up for KDE, Qt, Windows, and Mac builds

This commit is contained in:
craig.p.drummond
2014-06-03 15:49:31 +00:00
committed by craig.p.drummond
parent 1422518bcc
commit 406e1afde1
13 changed files with 602 additions and 342 deletions

View File

@@ -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)

View File

@@ -23,35 +23,13 @@
#include "application.h"
#include "settings.h"
#ifdef ENABLE_KDE_SUPPORT
#include <KDE/KCmdLineArgs>
#include <KDE/KStartupInfo>
#include "initialsettingswizard.h"
#include "support/thread.h"
#include "mpd/song.h"
#else
#include <QIcon>
#ifdef Q_OS_WIN
#include <QDir>
#include <windows.h>
#endif
#ifdef Q_OS_MAC
#include <QDir>
#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<MSG *>(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<MainWindow *>(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<MainWindow *>(activationWindow());
if (mw) {
mw->load(urls);
}
}
}
#else // Q_OS_WIN || Q_OS_MAC
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDir>
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<QVariant> a;
a.append(args);
m.setArguments(a);
QDBusConnection::sessionBus().send(m);
}
}
#endif

View File

@@ -26,77 +26,15 @@
#include "config.h"
#include <qglobal.h>
#ifdef ENABLE_KDE_SUPPORT
#include <KDE/KUniqueApplication>
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 <QAbstractNativeEventFilter>
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 <QApplication>
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

98
gui/application_kde.cpp Normal file
View File

@@ -0,0 +1,98 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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 <KDE/KCmdLineArgs>
#include <KDE/KStartupInfo>
#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;
}
}

51
gui/application_kde.h Normal file
View File

@@ -0,0 +1,51 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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 <qglobal.h>
#include <KDE/KUniqueApplication>
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

33
gui/application_mac.cpp Normal file
View File

@@ -0,0 +1,33 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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 <QIcon>
Application::Application(int &argc, char **argv)
: SingleApplication(argc, argv)
{
QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, true);
QIcon::setThemeName(QLatin1String("oxygen"));
}

37
gui/application_mac.h Normal file
View File

@@ -0,0 +1,37 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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

87
gui/application_qt.cpp Normal file
View File

@@ -0,0 +1,87 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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 <QDBusConnection>
#include <QDBusMessage>
#include <QDir>
#include <QIcon>
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<QVariant> a;
a.append(args);
m.setArguments(a);
QDBusConnection::sessionBus().send(m);
}
}

39
gui/application_qt.h Normal file
View File

@@ -0,0 +1,39 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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 <QApplication>
class Application : public QApplication
{
public:
static void initObjects();
Application(int &argc, char **argv);
virtual ~Application() { }
bool start();
void loadFiles();
};
#endif

55
gui/application_win.cpp Normal file
View File

@@ -0,0 +1,55 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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 <QIcon>
#include <windows.h>
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<MSG *>(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

47
gui/application_win.h Normal file
View File

@@ -0,0 +1,47 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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 <QAbstractNativeEventFilter>
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

88
gui/singleapplication.cpp Normal file
View File

@@ -0,0 +1,88 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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<MainWindow *>(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<MainWindow *>(activationWindow());
if (mw) {
mw->load(urls);
}
}
}

50
gui/singleapplication.h Normal file
View File

@@ -0,0 +1,50 @@
/*
* Cantata
*
* Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* 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