1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-01 03:40:16 +02:00

Update to newer kdsingleappguard

This commit is contained in:
Leo Franchi
2012-05-20 01:06:06 -04:00
parent 7326212671
commit b052486e2e
5 changed files with 1076 additions and 387 deletions

View File

@@ -680,28 +680,29 @@ TomahawkApp::loadUrl( const QString& url )
void void
TomahawkApp::instanceStarted( KDSingleApplicationGuard::Instance instance ) TomahawkApp::instanceStarted( KDSingleApplicationGuard::Instance instance )
{ {
tDebug( LOGINFO ) << "Instance started!" << instance.pid << instance.arguments; tDebug( LOGINFO ) << "Instance started!" << instance.pid() << instance.arguments();
const QStringList arguments = instance.arguments();
if ( instance.arguments.size() < 2 ) if ( arguments.size() < 2 )
return; return;
QString arg1 = instance.arguments[ 1 ]; QString arg1 = arguments[ 1 ];
if ( loadUrl( arg1 ) ) if ( loadUrl( arg1 ) )
{ {
activate(); activate();
return; return;
} }
if ( instance.arguments.contains( "--next" ) ) if ( arguments.contains( "--next" ) )
AudioEngine::instance()->next(); AudioEngine::instance()->next();
else if ( instance.arguments.contains( "--prev" ) ) else if ( arguments.contains( "--prev" ) )
AudioEngine::instance()->previous(); AudioEngine::instance()->previous();
else if ( instance.arguments.contains( "--playpause" ) ) else if ( arguments.contains( "--playpause" ) )
AudioEngine::instance()->playPause(); AudioEngine::instance()->playPause();
else if ( instance.arguments.contains( "--play" ) ) else if ( arguments.contains( "--play" ) )
AudioEngine::instance()->play(); AudioEngine::instance()->play();
else if ( instance.arguments.contains( "--pause" ) ) else if ( arguments.contains( "--pause" ) )
AudioEngine::instance()->pause(); AudioEngine::instance()->pause();
else if ( instance.arguments.contains( "--stop" ) ) else if ( arguments.contains( "--stop" ) )
AudioEngine::instance()->stop(); AudioEngine::instance()->stop();
} }

View File

@@ -33,7 +33,7 @@ const void * KDLockedSharedMemoryPointerBase::get() const {
} }
size_t KDLockedSharedMemoryPointerBase::byteSize() const { size_t KDLockedSharedMemoryPointerBase::byteSize() const {
return mem->size(); return mem ? mem->size() : 0;
} }
/*! /*!

View File

@@ -2,24 +2,29 @@
#define __KDTOOLSCORE_KDSINGLEAPPLICATIONGUARD_H__ #define __KDTOOLSCORE_KDSINGLEAPPLICATIONGUARD_H__
#include <QtCore/QObject> #include <QtCore/QObject>
#if QT_VERSION >= 0x040400 || defined(DOXYGEN_RUN)
#ifndef QT_NO_SHAREDMEMORY
#include <QtCore/QStringList> #include <QtCore/QStringList>
#include <QtCore/QMetaType>
#include "pimpl_ptr.h" #include "pimpl_ptr.h"
#include "DllMacro.h" #include "DllMacro.h"
class QCoreApplication; #include <algorithm>
#ifndef Q_WS_WIN template <typename T> class QVector;
void SIGINT_handler( int sig ); class QCoreApplication;
#endif
class DLLEXPORT KDSingleApplicationGuard : public QObject class DLLEXPORT KDSingleApplicationGuard : public QObject
{ {
Q_OBJECT Q_OBJECT
#ifndef Q_WS_WIN Q_ENUMS( Policy )
friend void ::SIGINT_handler( int ); Q_PROPERTY( bool operational READ isOperational )
#endif Q_PROPERTY( bool exitRequested READ isExitRequested )
Q_PROPERTY( bool primaryInstance READ isPrimaryInstance NOTIFY becamePrimaryInstance )
Q_PROPERTY( Policy policy READ policy WRITE setPolicy NOTIFY policyChanged )
public: public:
enum Policy enum Policy
{ {
@@ -27,49 +32,112 @@ public:
AutoKillOtherInstances = 1 AutoKillOtherInstances = 1
}; };
Q_PROPERTY( bool primaryInstance READ isPrimaryInstance NOTIFY becamePrimaryInstance ) explicit KDSingleApplicationGuard( QObject * parent=0 );
Q_PROPERTY( Policy policy READ policy WRITE setPolicy NOTIFY policyChanged ) explicit KDSingleApplicationGuard( Policy policy, QObject * parent=0 );
explicit KDSingleApplicationGuard( const QStringList & arguments, QObject * parent=0 );
explicit KDSingleApplicationGuard( QCoreApplication* parent, Policy policy = AutoKillOtherInstances ); explicit KDSingleApplicationGuard( const QStringList & arguments, Policy policy, QObject * parent=0 );
~KDSingleApplicationGuard(); ~KDSingleApplicationGuard();
bool isOperational() const;
bool isExitRequested() const;
bool isPrimaryInstance() const; bool isPrimaryInstance() const;
Policy policy() const; Policy policy() const;
void setPolicy( Policy policy ); void setPolicy( Policy policy );
struct Instance class Instance;
{
Instance( const QStringList& arguments = QStringList(), qint64 pid = -1 );
QStringList arguments; QVector<Instance> instances() const;
qint64 pid;
};
QList< Instance > instances() const;
Q_SIGNALS: Q_SIGNALS:
void instanceStarted( KDSingleApplicationGuard::Instance instance ); void instanceStarted( const KDSingleApplicationGuard::Instance & instance );
void instanceExited( KDSingleApplicationGuard::Instance instance ); void instanceExited( const KDSingleApplicationGuard::Instance & instance );
void exitRequested();
void raiseRequested();
void becamePrimaryInstance(); void becamePrimaryInstance();
void policyChanged(); void becameSecondaryInstance();
void policyChanged( KDSingleApplicationGuard::Policy policy );
public Q_SLOTS: public Q_SLOTS:
void shutdownOtherInstances(); void shutdownOtherInstances();
void killOtherInstances(); void killOtherInstances();
protected: protected:
void timerEvent( QTimerEvent* event ); /*! \reimp */ bool event( QEvent * event );
private: private:
#ifndef Q_WS_WIN
static void SIGINT_handler( int );
#endif
private:
friend struct ProcessInfo;
class Private; class Private;
kdtools::pimpl_ptr< Private > d; kdtools::pimpl_ptr< Private > d;
}; };
#if QT_VERSION < 0x040400 class DLLEXPORT KDSingleApplicationGuard::Instance {
#ifdef Q_CC_GNU friend class ::KDSingleApplicationGuard;
#warning "Can't use KDSingleApplicationGuard with Qt versions prior to 4.4" friend class ::KDSingleApplicationGuard::Private;
#endif Instance( const QStringList &, bool, qint64 );
#endif public:
Instance();
Instance( const Instance & other );
~Instance();
#endif void swap( Instance & other ) {
std::swap( d, other.d );
}
Instance & operator=( Instance other ) {
swap( other );
return *this;
}
bool isNull() const { return !d; }
bool isValid() const;
bool areArgumentsTruncated() const;
const QStringList & arguments() const;
qint64 pid() const;
void shutdown();
void kill();
void raise();
private:
class Private;
Private * d;
};
namespace std {
template <>
inline void swap( KDSingleApplicationGuard::Instance & lhs,
KDSingleApplicationGuard::Instance & rhs )
{
lhs.swap( rhs );
}
} // namespace std
QT_BEGIN_NAMESPACE
template <>
inline void qSwap( KDSingleApplicationGuard::Instance & lhs,
KDSingleApplicationGuard::Instance & rhs )
{
lhs.swap( rhs );
}
Q_DECLARE_METATYPE( KDSingleApplicationGuard::Instance )
Q_DECLARE_TYPEINFO( KDSingleApplicationGuard::Instance, Q_MOVABLE_TYPE );
QT_END_NAMESPACE
#endif // QT_NO_SHAREDMEMORY
#endif // QT_VERSION >= 0x040400 || defined(DOXYGEN_RUN)
#endif /* __KDTOOLSCORE_KDSINGLEAPPLICATIONGUARD_H__ */

View File

@@ -152,7 +152,7 @@ main( int argc, char *argv[] )
#endif #endif
#endif #endif
KDSingleApplicationGuard guard( &a, KDSingleApplicationGuard::AutoKillOtherInstances ); KDSingleApplicationGuard guard( KDSingleApplicationGuard::AutoKillOtherInstances );
QObject::connect( &guard, SIGNAL( instanceStarted( KDSingleApplicationGuard::Instance ) ), &a, SLOT( instanceStarted( KDSingleApplicationGuard::Instance ) ) ); QObject::connect( &guard, SIGNAL( instanceStarted( KDSingleApplicationGuard::Instance ) ), &a, SLOT( instanceStarted( KDSingleApplicationGuard::Instance ) ) );
if ( guard.isPrimaryInstance() ) if ( guard.isPrimaryInstance() )