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