/src/libtomahawk/thirdparty/kdsingleapplicationguard/kdsingleapplicationguard.h

http://github.com/tomahawk-player/tomahawk · C Header · 141 lines · 106 code · 35 blank · 0 comment · 0 complexity · 13f9364e087115a6c37d253857992aaf MD5 · raw file

  1. #ifndef __KDTOOLSCORE_KDSINGLEAPPLICATIONGUARD_H__
  2. #define __KDTOOLSCORE_KDSINGLEAPPLICATIONGUARD_H__
  3. #include <QtCore/QObject>
  4. #ifndef QT_NO_SHAREDMEMORY
  5. #include <QtCore/QStringList>
  6. #include <QtCore/QMetaType>
  7. #include "pimpl_ptr.h"
  8. #include "DllMacro.h"
  9. #include <algorithm>
  10. template <typename T> class QVector;
  11. class QCoreApplication;
  12. class DLLEXPORT KDSingleApplicationGuard : public QObject
  13. {
  14. Q_OBJECT
  15. Q_ENUMS( Policy )
  16. Q_PROPERTY( bool operational READ isOperational )
  17. Q_PROPERTY( bool exitRequested READ isExitRequested )
  18. Q_PROPERTY( bool primaryInstance READ isPrimaryInstance NOTIFY becamePrimaryInstance )
  19. Q_PROPERTY( Policy policy READ policy WRITE setPolicy NOTIFY policyChanged )
  20. public:
  21. enum Policy
  22. {
  23. NoPolicy = 0,
  24. AutoKillOtherInstances = 1
  25. };
  26. explicit KDSingleApplicationGuard( QObject * parent=0 );
  27. explicit KDSingleApplicationGuard( Policy policy, QObject * parent=0 );
  28. explicit KDSingleApplicationGuard( const QStringList & arguments, QObject * parent=0 );
  29. explicit KDSingleApplicationGuard( const QStringList & arguments, Policy policy, QObject * parent=0 );
  30. ~KDSingleApplicationGuard();
  31. bool isOperational() const;
  32. bool isExitRequested() const;
  33. bool isPrimaryInstance() const;
  34. Policy policy() const;
  35. void setPolicy( Policy policy );
  36. class Instance;
  37. QVector<Instance> instances() const;
  38. Q_SIGNALS:
  39. void instanceStarted( const KDSingleApplicationGuard::Instance & instance );
  40. void instanceExited( const KDSingleApplicationGuard::Instance & instance );
  41. void exitRequested();
  42. void raiseRequested();
  43. void becamePrimaryInstance();
  44. void becameSecondaryInstance();
  45. void policyChanged( KDSingleApplicationGuard::Policy policy );
  46. public Q_SLOTS:
  47. void shutdownOtherInstances();
  48. void killOtherInstances();
  49. protected:
  50. /*! \reimp */ bool event( QEvent * event );
  51. private:
  52. #ifndef Q_WS_WIN
  53. static void SIGINT_handler( int );
  54. #endif
  55. private:
  56. friend struct ProcessInfo;
  57. class Private;
  58. kdtools::pimpl_ptr< Private > d;
  59. };
  60. class DLLEXPORT KDSingleApplicationGuard::Instance {
  61. friend class ::KDSingleApplicationGuard;
  62. friend class ::KDSingleApplicationGuard::Private;
  63. Instance( const QStringList &, bool, qint64 );
  64. public:
  65. Instance();
  66. Instance( const Instance & other );
  67. ~Instance();
  68. void swap( Instance & other ) {
  69. std::swap( d, other.d );
  70. }
  71. Instance & operator=( Instance other ) {
  72. swap( other );
  73. return *this;
  74. }
  75. bool isNull() const { return !d; }
  76. bool isValid() const;
  77. bool areArgumentsTruncated() const;
  78. const QStringList & arguments() const;
  79. qint64 pid() const;
  80. void shutdown();
  81. void kill();
  82. void raise();
  83. private:
  84. class Private;
  85. Private * d;
  86. };
  87. namespace std {
  88. template <>
  89. inline void swap( KDSingleApplicationGuard::Instance & lhs,
  90. KDSingleApplicationGuard::Instance & rhs )
  91. {
  92. lhs.swap( rhs );
  93. }
  94. } // namespace std
  95. QT_BEGIN_NAMESPACE
  96. template <>
  97. inline void qSwap( KDSingleApplicationGuard::Instance & lhs,
  98. KDSingleApplicationGuard::Instance & rhs )
  99. {
  100. lhs.swap( rhs );
  101. }
  102. Q_DECLARE_METATYPE( KDSingleApplicationGuard::Instance )
  103. Q_DECLARE_TYPEINFO( KDSingleApplicationGuard::Instance, Q_MOVABLE_TYPE );
  104. QT_END_NAMESPACE
  105. #endif // QT_NO_SHAREDMEMORY
  106. #endif /* __KDTOOLSCORE_KDSINGLEAPPLICATIONGUARD_H__ */