PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mythtv/libs/libmythbase/mythcorecontext.h

http://github.com/MythTV/mythtv
C Header | 281 lines | 218 code | 44 blank | 19 comment | 0 complexity | 4734a01cb0af2079dc424346790b1fd8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, MIT, GPL-2.0, LGPL-2.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. #ifndef MYTHCORECONTEXT_H_
  2. #define MYTHCORECONTEXT_H_
  3. #include <QObject>
  4. #include <QString>
  5. #include <QHostAddress>
  6. #include "mythdb.h"
  7. #include "mythbaseexp.h"
  8. #include "mythobservable.h"
  9. #include "mythsocket_cb.h"
  10. #include "mythlogging.h"
  11. #include "mythlocale.h"
  12. #include "mythsession.h"
  13. #define MYTH_APPNAME_MYTHBACKEND "mythbackend"
  14. #define MYTH_APPNAME_MYTHJOBQUEUE "mythjobqueue"
  15. #define MYTH_APPNAME_MYTHFRONTEND "mythfrontend"
  16. #define MYTH_APPNAME_MYTHTV_SETUP "mythtv-setup"
  17. #define MYTH_APPNAME_MYTHFILLDATABASE "mythfilldatabase"
  18. #define MYTH_APPNAME_MYTHCOMMFLAG "mythcommflag"
  19. #define MYTH_APPNAME_MYTHCCEXTRACTOR "mythccextractor"
  20. #define MYTH_APPNAME_MYTHPREVIEWGEN "mythpreviewgen"
  21. #define MYTH_APPNAME_MYTHTRANSCODE "mythtranscode"
  22. #define MYTH_APPNAME_MYTHWELCOME "mythwelcome"
  23. #define MYTH_APPNAME_MYTHSHUTDOWN "mythshutdown"
  24. #define MYTH_APPNAME_MYTHLCDSERVER "mythlcdserver"
  25. #define MYTH_APPNAME_MYTHAVTEST "mythavtest"
  26. #define MYTH_APPNAME_MYTHMEDIASERVER "mythmediaserver"
  27. #define MYTH_APPNAME_MYTHMETADATALOOKUP "mythmetadatalookup"
  28. #define MYTH_APPNAME_MYTHUTIL "mythutil"
  29. #define MYTH_APPNAME_MYTHSCREENWIZARD "mythscreenwizard"
  30. #define MYTH_APPNAME_MYTHFFPROBE "mythffprobe"
  31. #define MYTH_APPNAME_MYTHEXTERNRECORDER "mythexternrecorder"
  32. class MDBManager;
  33. class MythCoreContextPrivate;
  34. class MythSocket;
  35. class MythScheduler;
  36. class MythPluginManager;
  37. /** \class MythCoreContext
  38. * \brief This class contains the runtime context for MythTV.
  39. *
  40. * This class can be used to query for and set global and host
  41. * settings, and is used to communicate between the frontends
  42. * and backends. It also contains helper functions for theming
  43. * and for getting system defaults, parsing the command line, etc.
  44. * It also contains support for database error printing, and
  45. * database message logging.
  46. */
  47. class MBASE_PUBLIC MythCoreContext : public QObject, public MythObservable, public MythSocketCBs
  48. {
  49. Q_OBJECT
  50. public:
  51. MythCoreContext(const QString &binversion, QObject *guiContext);
  52. ~MythCoreContext() override;
  53. bool Init(void);
  54. void SetLocalHostname(const QString &hostname);
  55. void SetServerSocket(MythSocket *serverSock);
  56. void SetEventSocket(MythSocket *eventSock);
  57. void SetScheduler(MythScheduler *sched);
  58. bool SafeConnectToMasterServer(bool blockingClient = true,
  59. bool openEventSocket = true);
  60. bool ConnectToMasterServer(bool blockingClient = true,
  61. bool openEventSocket = true);
  62. MythSocket *ConnectCommandSocket(const QString &hostname, int port,
  63. const QString &announcement,
  64. bool *proto_mismatch = nullptr,
  65. int maxConnTry = -1,
  66. int setup_timeout = -1);
  67. MythSocket *ConnectEventSocket(const QString &hostname, int port);
  68. bool SetupCommandSocket(MythSocket *serverSock, const QString &announcement,
  69. uint timeout_in_ms, bool &proto_mismatch);
  70. bool CheckProtoVersion(MythSocket *socket,
  71. uint timeout_ms = kMythSocketLongTimeout,
  72. bool error_dialog_desired = false);
  73. static QString GenMythURL(const QString& host = QString(), int port = 0,
  74. QString path = QString(),
  75. const QString& storageGroup = QString());
  76. QString GetMasterHostPrefix(const QString &storageGroup = QString(),
  77. const QString &path = QString());
  78. QString GetMasterHostName(void);
  79. QString GetHostName(void);
  80. QString GetFilePrefix(void);
  81. bool IsConnectedToMaster(void);
  82. void SetAsBackend(bool backend);
  83. bool IsBackend(void) const; ///< is this process a backend process
  84. void SetAsFrontend(bool frontend);
  85. bool IsFrontend(void) const; ///< is this process a frontend process
  86. bool IsFrontendOnly(void); ///< is there a frontend, but no backend,
  87. ///< running on this host
  88. bool IsMasterHost(void); ///< is this the same host as the master
  89. bool IsMasterHost(const QString &host); ///< is specified host the master
  90. bool IsMasterBackend(void); ///< is this the actual MBE process
  91. static bool BackendIsRunning(void); ///< a backend process is running on this host
  92. bool IsThisBackend(const QString &addr); ///< is this address mapped to this backend host
  93. bool IsThisHost(const QString &addr); ///< is this address mapped to this host
  94. bool IsThisHost(const QString &addr, const QString &host);
  95. void BlockShutdown(void);
  96. void AllowShutdown(void);
  97. bool IsBlockingClient(void) const; ///< is this client blocking shutdown
  98. void SetWOLAllowed(bool allow);
  99. bool IsWOLAllowed() const;
  100. bool SendReceiveStringList(QStringList &strlist, bool quickTimeout = false,
  101. bool block = true);
  102. void SendMessage(const QString &message);
  103. void SendEvent(const MythEvent &event);
  104. void SendSystemEvent(const QString &msg);
  105. void SendHostSystemEvent(const QString &msg, const QString &hostname,
  106. const QString &args);
  107. void SetGUIObject(QObject *gui);
  108. QObject *GetGUIObject(void);
  109. QObject *GetGUIContext(void);
  110. bool HasGUI(void) const;
  111. bool IsUIThread(void);
  112. MythDB *GetDB(void);
  113. MDBManager *GetDBManager(void);
  114. MythScheduler *GetScheduler(void);
  115. bool IsDatabaseIgnored(void) const;
  116. DatabaseParams GetDatabaseParams(void)
  117. { return GetDB()->GetDatabaseParams(); }
  118. void SaveSetting(const QString &key, int newValue);
  119. void SaveSetting(const QString &key, const QString &newValue);
  120. QString GetSetting(const QString &key, const QString &defaultval = "");
  121. bool SaveSettingOnHost(const QString &key, const QString &newValue,
  122. const QString &host);
  123. void SaveBoolSetting(const QString &key, bool newValue)
  124. { SaveSetting(key, static_cast<int>(newValue)); }
  125. // Convenience setting query methods
  126. bool GetBoolSetting(const QString &key, bool defaultval = false);
  127. int GetNumSetting(const QString &key, int defaultval = 0);
  128. int GetBoolSetting(const QString &key, int defaultval) = delete;
  129. bool GetNumSetting(const QString &key, bool defaultvalue) = delete;
  130. double GetFloatSetting(const QString &key, double defaultval = 0.0);
  131. void GetResolutionSetting(const QString &type, int &width, int &height,
  132. double& forced_aspect, double &refresh_rate,
  133. int index=-1);
  134. void GetResolutionSetting(const QString &type, int &width, int &height,
  135. int index=-1);
  136. QString GetSettingOnHost(const QString &key, const QString &host,
  137. const QString &defaultval = "");
  138. bool GetBoolSettingOnHost(const QString &key, const QString &host,
  139. bool defaultval = false);
  140. int GetNumSettingOnHost(const QString &key, const QString &host,
  141. int defaultval = 0);
  142. int GetBoolSettingOnHost(const QString &key, const QString &host,
  143. int defaultval) = delete;
  144. bool GetNumSettingOnHost(const QString &key, const QString &host,
  145. bool defaultval = false) = delete;
  146. double GetFloatSettingOnHost(const QString &key, const QString &host,
  147. double defaultval = 0.0);
  148. QString GetBackendServerIP(void);
  149. QString GetBackendServerIP(const QString &host);
  150. QString GetBackendServerIP4(void);
  151. QString GetBackendServerIP4(const QString &host);
  152. QString GetBackendServerIP6(void);
  153. QString GetBackendServerIP6(const QString &host);
  154. QString GetMasterServerIP(void);
  155. static int GetMasterServerPort(void);
  156. int GetMasterServerStatusPort(void);
  157. int GetBackendServerPort(void);
  158. int GetBackendServerPort(const QString &host);
  159. int GetBackendStatusPort(void);
  160. int GetBackendStatusPort(const QString &host);
  161. bool GetScopeForAddress(QHostAddress &addr) const;
  162. void SetScopeForAddress(const QHostAddress &addr);
  163. void SetScopeForAddress(const QHostAddress &addr, int scope);
  164. enum ResolveType { ResolveAny = -1, ResolveIPv4 = 0, ResolveIPv6 = 1 };
  165. QString resolveSettingAddress(const QString &name,
  166. const QString &host = QString(),
  167. ResolveType type = ResolveAny,
  168. bool keepscope = false);
  169. static QString resolveAddress(const QString &host,
  170. ResolveType type = ResolveAny,
  171. bool keepscope = false) ;
  172. bool CheckSubnet(const QAbstractSocket *socket);
  173. bool CheckSubnet(const QHostAddress &peer);
  174. void ClearSettingsCache(const QString &myKey = QString(""));
  175. void ActivateSettingsCache(bool activate = true);
  176. void OverrideSettingForSession(const QString &key, const QString &value);
  177. void ClearOverrideSettingForSession(const QString &key);
  178. void dispatch(const MythEvent &event);
  179. void InitPower(bool Create = true);
  180. void InitLocale(void);
  181. void ReInitLocale(void);
  182. MythLocale *GetLocale(void) const;
  183. QLocale GetQLocale(void);
  184. void SaveLocaleDefaults(void);
  185. QString GetLanguage(void);
  186. QString GetLanguageAndVariant(void);
  187. void ResetLanguage(void);
  188. void ResetSockets(void);
  189. void RegisterForPlayback(QObject *sender, const char *method);
  190. void UnregisterForPlayback(QObject *sender);
  191. void WantingPlayback(QObject *sender);
  192. bool InWantingPlayback(void);
  193. void TVInWantingPlayback(bool b);
  194. MythSessionManager *GetSessionManager(void);
  195. // Plugin related methods
  196. static bool TestPluginVersion(const QString &name, const QString &libversion,
  197. const QString &pluginversion);
  198. void SetPluginManager(MythPluginManager *pmanager);
  199. MythPluginManager *GetPluginManager(void);
  200. // Set when QEventLoop has been stopped and about to exit application
  201. void SetExiting(bool exiting = true);
  202. bool IsExiting(void);
  203. void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
  204. void UnregisterFileForWrite(const QString &file);
  205. bool IsRegisteredFileForWrite(const QString &file);
  206. // signal related methods
  207. void WaitUntilSignals(const char *signal1, ...);
  208. void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
  209. void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
  210. void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position); }
  211. void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
  212. void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
  213. void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
  214. void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
  215. signals:
  216. void TVPlaybackStarted(void);
  217. //// TVPlaybackStopped signal should be used in combination with
  218. //// InWantingPlayback() and treat it accordingly
  219. void TVPlaybackStopped(void);
  220. void TVPlaybackSought(qint64 position);
  221. void TVPlaybackPaused(void);
  222. void TVPlaybackUnpaused(void);
  223. void TVPlaybackAborted(void);
  224. void TVPlaybackAboutToStart(void);
  225. void TVPlaybackPlaying(void);
  226. private:
  227. Q_DISABLE_COPY(MythCoreContext)
  228. MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
  229. void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
  230. void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
  231. void connectionClosed(MythSocket *sock) override; // MythSocketCBs
  232. void readyRead(MythSocket *sock) override; // MythSocketCBs
  233. };
  234. /// This global variable contains the MythCoreContext instance for the app
  235. extern MBASE_PUBLIC MythCoreContext *gCoreContext;
  236. #endif
  237. /* vim: set expandtab tabstop=4 shiftwidth=4: */