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 *eventHandler);
57  virtual ~MythCoreContext();
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  QString GenMythURL(QString host = QString(), QString port = QString(),
87  QString path = QString(), QString storageGroup = QString());
88 
89  QString GenMythURL(QString host = QString(), int port = 0,
90  QString path = QString(), QString storageGroup = QString());
91 
92  QString GetMasterHostPrefix(const QString &storageGroup = QString(),
93  const QString &path = QString());
94  QString GetMasterHostName(void);
95  QString GetHostName(void);
96  QString GetFilePrefix(void);
97 
98  bool IsConnectedToMaster(void);
99  void SetAsBackend(bool backend);
100  bool IsBackend(void) const;
101  void SetAsFrontend(bool frontend);
102  bool IsFrontend(void) const;
103  bool IsFrontendOnly(void);
104  bool IsMasterHost(void);
106  bool IsMasterHost(const QString &host);
107  bool IsMasterBackend(void);
108  bool BackendIsRunning(void);
109 
110  bool IsThisBackend(const QString &addr);
111  bool IsThisHost(const QString &addr);
112  bool IsThisHost(const QString &addr, const QString &host);
113 
114  void BlockShutdown(void);
115  void AllowShutdown(void);
116  bool IsBlockingClient(void) const;
117 
118  void SetWOLAllowed(bool allow);
119  bool IsWOLAllowed() const;
120 
121  bool SendReceiveStringList(QStringList &strlist, bool quickTimeout = false,
122  bool block = true);
123  void SendMessage(const QString &message);
124  void SendEvent(const MythEvent &event);
125  void SendSystemEvent(const QString &msg);
126  void SendHostSystemEvent(const QString &msg, const QString &hostname,
127  const QString &args);
128 
129  void SetGUIObject(QObject *gui);
130  QObject *GetGUIObject(void);
131  QObject *GetGUIContext(void);
132  bool HasGUI(void) const;
133  bool IsUIThread(void);
134 
135  MythDB *GetDB(void);
136  MDBManager *GetDBManager(void);
137  MythScheduler *GetScheduler(void);
138 
139  bool IsDatabaseIgnored(void) const;
141  { return GetDB()->GetDatabaseParams(); }
142 
143  void SaveSetting(const QString &key, int newValue);
144  void SaveSetting(const QString &key, const QString &newValue);
145  QString GetSetting(const QString &key, const QString &defaultval = "");
146  bool SaveSettingOnHost(const QString &key, const QString &newValue,
147  const QString &host);
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 &refreshrate,
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 = 0);
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 = 0) = 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  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  QString resolveAddress(const QString &host,
197  ResolveType = ResolveAny,
198  bool keepscope = false) const;
199  bool CheckSubnet(const QAbstractSocket *socket);
200  bool CheckSubnet(const QHostAddress &addr);
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  const 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  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:
267 
268  void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
269  void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
270  void connectionClosed(MythSocket *sock) override; // MythSocketCBs
271  void readyRead(MythSocket *sock) override; // MythSocketCBs
272 };
273 
276 
278 extern MBASE_PUBLIC QMutex *avcodeclock;
279 
280 #endif
281 
282 /* 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)
unsigned int uint
Definition: compat.h:140
void emitTVPlaybackUnpaused(void)
Superclass for making an object have a set of listeners.
unsigned char b
Definition: ParseText.cpp:340
#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:15
void emitTVPlaybackPlaying(void)
void emitTVPlaybackStopped(void)
void dispatch(const MythEvent &event)
Dispatch an event to all listeners.
void emitTVPlaybackPaused(void)
void emitTVPlaybackStarted(void)
string hostname
Definition: caa.py:17
void emitTVPlaybackSought(qint64 position)
const char * name
Definition: ParseText.cpp:339
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
MythCoreContextPrivate * d
DatabaseParams GetDatabaseParams(void)
static TransMythUICheckBoxSetting * IsMasterBackend()
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:26
Scheduler * sched
void connected(MythSocket *sock) override
This class contains the runtime context for MythTV.
static int ClearSettingsCache(const MythUtilCommandLineParser &)