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  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(const QString& host = QString(), int port = 0,
87  QString path = QString(), const QString& storageGroup = QString());
88 
89  QString GetMasterHostPrefix(const QString &storageGroup = QString(),
90  const QString &path = QString());
91  QString GetMasterHostName(void);
92  QString GetHostName(void);
93  QString GetFilePrefix(void);
94 
95  bool IsConnectedToMaster(void);
96  void SetAsBackend(bool backend);
97  bool IsBackend(void) const;
98  void SetAsFrontend(bool frontend);
99  bool IsFrontend(void) const;
100  bool IsFrontendOnly(void);
101  bool IsMasterHost(void);
103  bool IsMasterHost(const QString &host);
104  bool IsMasterBackend(void);
105  bool BackendIsRunning(void);
106 
107  bool IsThisBackend(const QString &addr);
108  bool IsThisHost(const QString &addr);
109  bool IsThisHost(const QString &addr, const QString &host);
110 
111  void BlockShutdown(void);
112  void AllowShutdown(void);
113  bool IsBlockingClient(void) const;
114 
115  void SetWOLAllowed(bool allow);
116  bool IsWOLAllowed() const;
117 
118  bool SendReceiveStringList(QStringList &strlist, bool quickTimeout = false,
119  bool block = true);
120  void SendMessage(const QString &message);
121  void SendEvent(const MythEvent &event);
122  void SendSystemEvent(const QString &msg);
123  void SendHostSystemEvent(const QString &msg, const QString &hostname,
124  const QString &args);
125 
126  void SetGUIObject(QObject *gui);
127  QObject *GetGUIObject(void);
128  QObject *GetGUIContext(void);
129  bool HasGUI(void) const;
130  bool IsUIThread(void);
131 
132  MythDB *GetDB(void);
133  MDBManager *GetDBManager(void);
134  MythScheduler *GetScheduler(void);
135 
136  bool IsDatabaseIgnored(void) const;
138  { return GetDB()->GetDatabaseParams(); }
139 
140  void SaveSetting(const QString &key, int newValue);
141  void SaveSetting(const QString &key, const QString &newValue);
142  QString GetSetting(const QString &key, const QString &defaultval = "");
143  bool SaveSettingOnHost(const QString &key, const QString &newValue,
144  const QString &host);
145  void SaveBoolSetting(const QString &key, bool newValue)
146  { SaveSetting(key, static_cast<int>(newValue)); }
147 
148  // Convenience setting query methods
149  bool GetBoolSetting(const QString &key, bool defaultval = false);
150  int GetNumSetting(const QString &key, int defaultval = 0);
151  int GetBoolSetting(const QString &key, int defaultval) = delete;
152  bool GetNumSetting(const QString &key, bool defaultvalue) = delete;
153  double GetFloatSetting(const QString &key, double defaultval = 0.0);
154  void GetResolutionSetting(const QString &type, int &width, int &height,
155  double& forced_aspect, double &refresh_rate,
156  int index=-1);
157  void GetResolutionSetting(const QString &type, int &width, int &height,
158  int index=-1);
159 
160  QString GetSettingOnHost(const QString &key, const QString &host,
161  const QString &defaultval = "");
162  bool GetBoolSettingOnHost(const QString &key, const QString &host,
163  bool defaultval = 0);
164  int GetNumSettingOnHost(const QString &key, const QString &host,
165  int defaultval = 0);
166  int GetBoolSettingOnHost(const QString &key, const QString &host,
167  int defaultval) = delete;
168  bool GetNumSettingOnHost(const QString &key, const QString &host,
169  bool defaultval = 0) = delete;
170  double GetFloatSettingOnHost(const QString &key, const QString &host,
171  double defaultval = 0.0);
172 
173  QString GetBackendServerIP(void);
174  QString GetBackendServerIP(const QString &host);
175  QString GetBackendServerIP4(void);
176  QString GetBackendServerIP4(const QString &host);
177  QString GetBackendServerIP6(void);
178  QString GetBackendServerIP6(const QString &host);
179  QString GetMasterServerIP(void);
180  int GetMasterServerPort(void);
181  int GetMasterServerStatusPort(void);
182  int GetBackendServerPort(void);
183  int GetBackendServerPort(const QString &host);
184  int GetBackendStatusPort(void);
185  int GetBackendStatusPort(const QString &host);
186 
187  bool GetScopeForAddress(QHostAddress &addr) const;
188  void SetScopeForAddress(const QHostAddress &addr);
189  void SetScopeForAddress(const QHostAddress &addr, int scope);
190  enum ResolveType { ResolveAny = -1, ResolveIPv4 = 0, ResolveIPv6 = 1 };
191  QString resolveSettingAddress(const QString &name,
192  const QString &host = QString(),
193  ResolveType type = ResolveAny,
194  bool keepscope = false);
195  QString resolveAddress(const QString &host,
196  ResolveType = ResolveAny,
197  bool keepscope = false) const;
198  bool CheckSubnet(const QAbstractSocket *socket);
199  bool CheckSubnet(const QHostAddress &peer);
200 
201  void ClearSettingsCache(const QString &myKey = QString(""));
202  void ActivateSettingsCache(bool activate = true);
203  void OverrideSettingForSession(const QString &key, const QString &value);
204  void ClearOverrideSettingForSession(const QString &key);
205 
206  void dispatch(const MythEvent &event);
207 
208  void InitLocale(void);
209  void ReInitLocale(void);
210  MythLocale *GetLocale(void) const;
211  QLocale GetQLocale(void);
212  void SaveLocaleDefaults(void);
213  QString GetLanguage(void);
214  QString GetLanguageAndVariant(void);
215  void ResetLanguage(void);
216  void ResetSockets(void);
217 
218  void RegisterForPlayback(QObject *sender, const char *method);
219  void UnregisterForPlayback(QObject *sender);
220  void WantingPlayback(QObject *sender);
221  bool InWantingPlayback(void);
222  void TVInWantingPlayback(bool b);
223 
224  MythSessionManager *GetSessionManager(void);
225 
226  // Plugin related methods
227  bool TestPluginVersion(const QString &name, const QString &libversion,
228  const QString &pluginversion);
229 
230  void SetPluginManager(MythPluginManager *pmanager);
231  MythPluginManager *GetPluginManager(void);
232 
233  // Set when QEventLoop has been stopped and about to exit application
234  void SetExiting(bool exiting = true);
235  bool IsExiting(void);
236 
237  void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
238  void UnregisterFileForWrite(const QString &file);
239  bool IsRegisteredFileForWrite(const QString &file);
240 
241  // signal related methods
242  void WaitUntilSignals(const char *signal1, ...);
243  void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
244  void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
245  void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position); }
246  void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
247  void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
248  void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
249  void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
250 
251 
252  signals:
253  void TVPlaybackStarted(void);
256  void TVPlaybackStopped(void);
257  void TVPlaybackSought(qint64 position);
258  void TVPlaybackPaused(void);
259  void TVPlaybackUnpaused(void);
260  void TVPlaybackAborted(void);
261  void TVPlaybackAboutToStart(void);
262  void TVPlaybackPlaying(void);
263 
264  private:
265  Q_DISABLE_COPY(MythCoreContext)
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:329
#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)
const char * name
Definition: ParseText.cpp:328
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 &)