MythTV  master
mythcorecontext.h
Go to the documentation of this file.
1 #ifndef MYTHCORECONTEXT_H_
2 #define MYTHCORECONTEXT_H_
3 
4 #include <QObject>
5 #include <QString>
6 #include <QHostAddress>
7 
8 #include "mythdb.h"
9 #include "mythbaseexp.h"
10 #include "mythobservable.h"
11 #include "mythsocket_cb.h"
12 #include "mythlogging.h"
13 #include "mythlocale.h"
14 #include "mythsession.h"
15 
16 #define MYTH_APPNAME_MYTHBACKEND "mythbackend"
17 #define MYTH_APPNAME_MYTHJOBQUEUE "mythjobqueue"
18 #define MYTH_APPNAME_MYTHFRONTEND "mythfrontend"
19 #define MYTH_APPNAME_MYTHTV_SETUP "mythtv-setup"
20 #define MYTH_APPNAME_MYTHFILLDATABASE "mythfilldatabase"
21 #define MYTH_APPNAME_MYTHCOMMFLAG "mythcommflag"
22 #define MYTH_APPNAME_MYTHCCEXTRACTOR "mythccextractor"
23 #define MYTH_APPNAME_MYTHPREVIEWGEN "mythpreviewgen"
24 #define MYTH_APPNAME_MYTHTRANSCODE "mythtranscode"
25 #define MYTH_APPNAME_MYTHWELCOME "mythwelcome"
26 #define MYTH_APPNAME_MYTHSHUTDOWN "mythshutdown"
27 #define MYTH_APPNAME_MYTHLCDSERVER "mythlcdserver"
28 #define MYTH_APPNAME_MYTHAVTEST "mythavtest"
29 #define MYTH_APPNAME_MYTHMEDIASERVER "mythmediaserver"
30 #define MYTH_APPNAME_MYTHMETADATALOOKUP "mythmetadatalookup"
31 #define MYTH_APPNAME_MYTHUTIL "mythutil"
32 #define MYTH_APPNAME_MYTHSCREENWIZARD "mythscreenwizard"
33 #define MYTH_APPNAME_MYTHFFPROBE "mythffprobe"
34 #define MYTH_APPNAME_MYTHEXTERNRECORDER "mythexternrecorder"
35 
36 class MDBManager;
38 class MythSocket;
39 class MythScheduler;
40 class MythPluginManager;
41 
52 class MBASE_PUBLIC MythCoreContext : public QObject, public MythObservable, public MythSocketCBs
53 {
54  Q_OBJECT
55  public:
56  MythCoreContext(const QString &binversion, QObject *guiContext);
57  ~MythCoreContext() override;
58 
59  bool Init(void);
60 
61  void SetLocalHostname(const QString &hostname);
62  void SetServerSocket(MythSocket *serverSock);
63  void SetEventSocket(MythSocket *eventSock);
64  void SetScheduler(MythScheduler *sched);
65 
66  bool SafeConnectToMasterServer(bool blockingClient = true,
67  bool openEventSocket = true);
68  bool ConnectToMasterServer(bool blockingClient = true,
69  bool openEventSocket = true);
70 
71  MythSocket *ConnectCommandSocket(const QString &hostname, int port,
72  const QString &announcement,
73  bool *proto_mismatch = nullptr,
74  int maxConnTry = -1,
75  int setup_timeout = -1);
76 
77  MythSocket *ConnectEventSocket(const QString &hostname, int port);
78 
79  bool SetupCommandSocket(MythSocket *serverSock, const QString &announcement,
80  uint timeout_in_ms, bool &proto_mismatch);
81 
82  bool CheckProtoVersion(MythSocket *socket,
83  uint timeout_ms = kMythSocketLongTimeout,
84  bool error_dialog_desired = false);
85 
86  static QString GenMythURL(const QString& host = QString(), int port = 0,
87  QString path = QString(),
88  const QString& storageGroup = QString());
89 
90  QString GetMasterHostPrefix(const QString &storageGroup = QString(),
91  const QString &path = QString());
92  QString GetMasterHostName(void);
93  QString GetHostName(void);
94  QString GetFilePrefix(void);
95 
96  bool IsConnectedToMaster(void);
97  void SetAsBackend(bool backend);
98  bool IsBackend(void) const;
99  void SetAsFrontend(bool frontend);
100  bool IsFrontend(void) const;
101  bool IsFrontendOnly(void);
102  bool IsMasterHost(void);
104  bool IsMasterHost(const QString &host);
105  bool IsMasterBackend(void);
106  static bool BackendIsRunning(void);
107 
108  bool IsThisBackend(const QString &addr);
109  bool IsThisHost(const QString &addr);
110  bool IsThisHost(const QString &addr, const QString &host);
111 
112  void BlockShutdown(void);
113  void AllowShutdown(void);
114  bool IsBlockingClient(void) const;
115 
116  void SetWOLAllowed(bool allow);
117  bool IsWOLAllowed() const;
118 
119  bool SendReceiveStringList(QStringList &strlist, bool quickTimeout = false,
120  bool block = true);
121  void SendMessage(const QString &message);
122  void SendEvent(const MythEvent &event);
123  void SendSystemEvent(const QString &msg);
124  void SendHostSystemEvent(const QString &msg, const QString &hostname,
125  const QString &args);
126 
127  void SetGUIObject(QObject *gui);
128  QObject *GetGUIObject(void);
129  QObject *GetGUIContext(void);
130  bool HasGUI(void) const;
131  bool IsUIThread(void);
132 
133  MythDB *GetDB(void);
134  MDBManager *GetDBManager(void);
135  MythScheduler *GetScheduler(void);
136 
137  bool IsDatabaseIgnored(void) const;
139  { return GetDB()->GetDatabaseParams(); }
140 
141  void SaveSetting(const QString &key, int newValue);
142  void SaveSetting(const QString &key, const QString &newValue);
143  QString GetSetting(const QString &key, const QString &defaultval = "");
144  bool SaveSettingOnHost(const QString &key, const QString &newValue,
145  const QString &host);
146  void SaveBoolSetting(const QString &key, bool newValue)
147  { SaveSetting(key, static_cast<int>(newValue)); }
148 
149  // Convenience setting query methods
150  bool GetBoolSetting(const QString &key, bool defaultval = false);
151  int GetNumSetting(const QString &key, int defaultval = 0);
152  int GetBoolSetting(const QString &key, int defaultval) = delete;
153  bool GetNumSetting(const QString &key, bool defaultvalue) = delete;
154  double GetFloatSetting(const QString &key, double defaultval = 0.0);
155  void GetResolutionSetting(const QString &type, int &width, int &height,
156  double& forced_aspect, double &refresh_rate,
157  int index=-1);
158  void GetResolutionSetting(const QString &type, int &width, int &height,
159  int index=-1);
160 
161  QString GetSettingOnHost(const QString &key, const QString &host,
162  const QString &defaultval = "");
163  bool GetBoolSettingOnHost(const QString &key, const QString &host,
164  bool defaultval = false);
165  int GetNumSettingOnHost(const QString &key, const QString &host,
166  int defaultval = 0);
167  int GetBoolSettingOnHost(const QString &key, const QString &host,
168  int defaultval) = delete;
169  bool GetNumSettingOnHost(const QString &key, const QString &host,
170  bool defaultval = false) = delete;
171  double GetFloatSettingOnHost(const QString &key, const QString &host,
172  double defaultval = 0.0);
173 
174  QString GetBackendServerIP(void);
175  QString GetBackendServerIP(const QString &host);
176  QString GetBackendServerIP4(void);
177  QString GetBackendServerIP4(const QString &host);
178  QString GetBackendServerIP6(void);
179  QString GetBackendServerIP6(const QString &host);
180  QString GetMasterServerIP(void);
181  static int GetMasterServerPort(void);
182  int GetMasterServerStatusPort(void);
183  int GetBackendServerPort(void);
184  int GetBackendServerPort(const QString &host);
185  int GetBackendStatusPort(void);
186  int GetBackendStatusPort(const QString &host);
187 
188  bool GetScopeForAddress(QHostAddress &addr) const;
189  void SetScopeForAddress(const QHostAddress &addr);
190  void SetScopeForAddress(const QHostAddress &addr, int scope);
191  enum ResolveType { ResolveAny = -1, ResolveIPv4 = 0, ResolveIPv6 = 1 };
192  QString resolveSettingAddress(const QString &name,
193  const QString &host = QString(),
194  ResolveType type = ResolveAny,
195  bool keepscope = false);
196  static QString resolveAddress(const QString &host,
197  ResolveType type = ResolveAny,
198  bool keepscope = false) ;
199  bool CheckSubnet(const QAbstractSocket *socket);
200  bool CheckSubnet(const QHostAddress &peer);
201 
202  void ClearSettingsCache(const QString &myKey = QString(""));
203  void ActivateSettingsCache(bool activate = true);
204  void OverrideSettingForSession(const QString &key, const QString &value);
205  void ClearOverrideSettingForSession(const QString &key);
206 
207  void dispatch(const MythEvent &event);
208 
209  void InitLocale(void);
210  void ReInitLocale(void);
211  MythLocale *GetLocale(void) const;
212  QLocale GetQLocale(void);
213  void SaveLocaleDefaults(void);
214  QString GetLanguage(void);
215  QString GetLanguageAndVariant(void);
216  void ResetLanguage(void);
217  void ResetSockets(void);
218 
219  void RegisterForPlayback(QObject *sender, const char *method);
220  void UnregisterForPlayback(QObject *sender);
221  void WantingPlayback(QObject *sender);
222  bool InWantingPlayback(void);
223  void TVInWantingPlayback(bool b);
224 
225  MythSessionManager *GetSessionManager(void);
226 
227  // Plugin related methods
228  static bool TestPluginVersion(const QString &name, const QString &libversion,
229  const QString &pluginversion);
230 
231  void SetPluginManager(MythPluginManager *pmanager);
232  MythPluginManager *GetPluginManager(void);
233 
234  // Set when QEventLoop has been stopped and about to exit application
235  void SetExiting(bool exiting = true);
236  bool IsExiting(void);
237 
238  void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
239  void UnregisterFileForWrite(const QString &file);
240  bool IsRegisteredFileForWrite(const QString &file);
241 
242  // signal related methods
243  void WaitUntilSignals(const char *signal1, ...);
244  void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
245  void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
246  void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position); }
247  void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
248  void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
249  void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
250  void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
251 
252 
253  signals:
254  void TVPlaybackStarted(void);
257  void TVPlaybackStopped(void);
258  void TVPlaybackSought(qint64 position);
259  void TVPlaybackPaused(void);
260  void TVPlaybackUnpaused(void);
261  void TVPlaybackAborted(void);
262  void TVPlaybackAboutToStart(void);
263  void TVPlaybackPlaying(void);
264 
265  private:
266  Q_DISABLE_COPY(MythCoreContext)
267  MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
268 
269  void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
270  void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
271  void connectionClosed(MythSocket *sock) override; // MythSocketCBs
272  void readyRead(MythSocket *sock) override; // MythSocketCBs
273 };
274 
277 
279 extern MBASE_PUBLIC QMutex *avcodeclock;
280 
281 #endif
282 
283 /* vim: set expandtab tabstop=4 shiftwidth=4: */
This is an generic interface to a program scheduler.
Definition: mythscheduler.h:20
static int SendEvent(const MythUtilCommandLineParser &cmdline)
DatabaseParams GetDatabaseParams(void) const
Definition: mythdb.cpp:199
void connectionFailed(MythSocket *sock) override
virtual void connectionClosed(MythSocket *)=0
Definition: mythdb.h:14
#define kMythSocketLongTimeout
Definition: mythsocket_cb.h:8
static int SendMessage(const MythUtilCommandLineParser &cmdline)
void emitTVPlaybackUnpaused(void)
Superclass for making an object have a set of listeners.
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MBASE_PUBLIC MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
We use digest authentication because it protects the password over unprotected networks.
Definition: mythsession.h:98
This class is used as a container for messages.
Definition: mythevent.h:16
void emitTVPlaybackPlaying(void)
void emitTVPlaybackStopped(void)
void dispatch(const MythEvent &event)
Dispatch an event to all listeners.
static const uint16_t * d
void emitTVPlaybackPaused(void)
void emitTVPlaybackStarted(void)
string hostname
Definition: caa.py:17
void emitTVPlaybackSought(qint64 position)
unsigned int uint
Definition: compat.h:140
static int SendSystemEvent(const MythUtilCommandLineParser &cmdline)
MBASE_PUBLIC QMutex * avcodeclock
This global variable is used to makes certain calls to avlib threadsafe.
Structure containing the basic Database parameters.
Definition: mythdbparams.h:9
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:53
void emitTVPlaybackAborted(void)
virtual void readyRead(MythSocket *)=0
DatabaseParams GetDatabaseParams(void)
static TransMythUICheckBoxSetting * IsMasterBackend()
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:26
Scheduler * sched
void SaveBoolSetting(const QString &key, bool newValue)
void connected(MythSocket *sock) override
This class contains the runtime context for MythTV.
static int ClearSettingsCache(const MythUtilCommandLineParser &)