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  std::chrono::milliseconds setup_timeout = -1ms);
86 
87  MythSocket *ConnectEventSocket(const QString &hostname, int port);
88 
89  bool SetupCommandSocket(MythSocket *serverSock, const QString &announcement,
90  std::chrono::milliseconds timeout, bool &proto_mismatch);
91 
92  bool CheckProtoVersion(MythSocket *socket,
93  std::chrono::milliseconds timeout = 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  // No conversion between duration ratios. Just extract the number.
155  template <typename T>
156  typename std::enable_if_t<std::chrono::__is_duration<T>::value, void>
157  SaveDurSetting(const QString &key, T newValue)
158  { SaveSetting(key, static_cast<int>(newValue.count())); }
159 
160  bool SaveSettingOnHost(const QString &key, const QString &newValue,
161  const QString &host);
162  void SaveBoolSetting(const QString &key, bool newValue)
163  { SaveSetting(key, static_cast<int>(newValue)); }
164 
165  // Convenience setting query methods
166  bool GetBoolSetting(const QString &key, bool defaultval = false);
167  int GetNumSetting(const QString &key, int defaultval = 0);
168  template <typename T>
169  typename std::enable_if_t<std::chrono::__is_duration<T>::value, T>
170  GetDurSetting(const QString &key, T defaultval = T::zero())
171  { return T(GetNumSetting(key, static_cast<int>(defaultval.count()))); }
172  int GetBoolSetting(const QString &key, int defaultval) = delete;
173  bool GetNumSetting(const QString &key, bool defaultvalue) = delete;
174  double GetFloatSetting(const QString &key, double defaultval = 0.0);
175  void GetResolutionSetting(const QString &type, int &width, int &height,
176  double& forced_aspect, double &refresh_rate,
177  int index=-1);
178  void GetResolutionSetting(const QString &type, int &width, int &height,
179  int index=-1);
180 
181  QString GetSettingOnHost(const QString &key, const QString &host,
182  const QString &defaultval = "");
183  bool GetBoolSettingOnHost(const QString &key, const QString &host,
184  bool defaultval = false);
185  int GetNumSettingOnHost(const QString &key, const QString &host,
186  int defaultval = 0);
187  int GetBoolSettingOnHost(const QString &key, const QString &host,
188  int defaultval) = delete;
189  bool GetNumSettingOnHost(const QString &key, const QString &host,
190  bool defaultval = false) = delete;
191  double GetFloatSettingOnHost(const QString &key, const QString &host,
192  double defaultval = 0.0);
193 
194  QString GetBackendServerIP(void);
195  QString GetBackendServerIP(const QString &host);
196  QString GetBackendServerIP4(void);
197  QString GetBackendServerIP4(const QString &host);
198  QString GetBackendServerIP6(void);
199  QString GetBackendServerIP6(const QString &host);
200  QString GetMasterServerIP(void);
201  static int GetMasterServerPort(void);
202  int GetMasterServerStatusPort(void);
203  int GetBackendServerPort(void);
204  int GetBackendServerPort(const QString &host);
205  int GetBackendStatusPort(void);
206  int GetBackendStatusPort(const QString &host);
207 
208  bool GetScopeForAddress(QHostAddress &addr) const;
209  void SetScopeForAddress(const QHostAddress &addr);
210  void SetScopeForAddress(const QHostAddress &addr, int scope);
211  enum ResolveType { ResolveAny = -1, ResolveIPv4 = 0, ResolveIPv6 = 1 };
212  QString resolveSettingAddress(const QString &name,
213  const QString &host = QString(),
214  ResolveType type = ResolveAny,
215  bool keepscope = false);
216  static QString resolveAddress(const QString &host,
217  ResolveType type = ResolveAny,
218  bool keepscope = false) ;
219  bool CheckSubnet(const QAbstractSocket *socket);
220  bool CheckSubnet(const QHostAddress &peer);
221 
222  void ClearSettingsCache(const QString &myKey = QString(""));
223  void ActivateSettingsCache(bool activate = true);
224  void OverrideSettingForSession(const QString &key, const QString &value);
225  void ClearOverrideSettingForSession(const QString &key);
226 
227  void dispatch(const MythEvent &event);
228 
229  void InitPower(bool Create = true);
230  void InitLocale(void);
231  void ReInitLocale(void);
232  MythLocale *GetLocale(void) const;
233  QLocale GetQLocale(void);
234  void SaveLocaleDefaults(void);
235  QString GetLanguage(void);
236  QString GetLanguageAndVariant(void);
237  void ResetLanguage(void);
238  void ResetSockets(void);
239 
240  using PlaybackStartCb = void (QObject::*)(void);
241 
248  void RegisterForPlayback(QObject *sender, PlaybackStartCb method);
249 
250  template <class OBJ, typename SLOT>
251  typename std::enable_if_t<std::is_member_function_pointer_v<SLOT>, void>
252  RegisterForPlayback(OBJ *sender, SLOT method)
253  {
254  RegisterForPlayback(qobject_cast<QObject*>(sender),
255  static_cast<PlaybackStartCb>(method));
256  }
257 
258  void UnregisterForPlayback(QObject *sender);
259  void WantingPlayback(QObject *sender);
260  bool InWantingPlayback(void);
261  void TVInWantingPlayback(bool b);
262 
263  MythSessionManager *GetSessionManager(void);
264 
265  // Plugin related methods
266  static bool TestPluginVersion(const QString &name, const QString &libversion,
267  const QString &pluginversion);
268 
269  void SetPluginManager(MythPluginManager *pmanager);
270  MythPluginManager *GetPluginManager(void);
271 
272  // Set when QEventLoop has been stopped and about to exit application
273  void SetExiting(bool exiting = true);
274  bool IsExiting(void);
275 
276  void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
277  void UnregisterFileForWrite(const QString &file);
278  bool IsRegisteredFileForWrite(const QString &file);
279 
280  // Test Harness help
281  void setTestIntSettings(QMap<QString,int>& overrides);
282  void setTestFloatSettings(QMap<QString,double>& overrides);
283  void setTestStringSettings(QMap<QString,QString>& overrides);
284 
285  // signal related methods
286  void WaitUntilSignals(std::vector<CoreWaitInfo> & sigs) const;
287  void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
288  void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
289  void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position);
290  emit TVPlaybackSought();}
291  void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
292  void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
293  void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
294  void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
295 
296 
297  signals:
298  void TVPlaybackStarted(void);
301  void TVPlaybackStopped(void);
302  void TVPlaybackSought(qint64 position);
303  void TVPlaybackSought(void);
304  void TVPlaybackPaused(void);
305  void TVPlaybackUnpaused(void);
306  void TVPlaybackAborted(void);
307  void TVPlaybackAboutToStart(void);
308  void TVPlaybackPlaying(void);
309 
310  private:
311  Q_DISABLE_COPY(MythCoreContext)
312  MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
313 
314  void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
315  void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
316  void connectionClosed(MythSocket *sock) override; // MythSocketCBs
317  void readyRead(MythSocket *sock) override; // MythSocketCBs
318 
319  QMap<QString,int> m_testOverrideInts {};
320  QMap<QString,double> m_testOverrideFloats {};
321  QMap<QString,QString> m_testOverrideStrings {};
322 
323  private:
324  bool m_dvbv3 {false};
325  public:
326  void SetDVBv3(bool dvbv3) { m_dvbv3 = dvbv3; }
327  bool GetDVBv3(void) const { return m_dvbv3; }
328 };
329 
332 
333 #endif
334 
335 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythCoreContext::emitTVPlaybackUnpaused
void emitTVPlaybackUnpaused(void)
Definition: mythcorecontext.h:292
build_compdb.args
args
Definition: build_compdb.py:11
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
MythPluginManager
Definition: mythplugin.h:62
MythScheduler
This is an generic interface to a program scheduler.
Definition: mythscheduler.h:20
mythdb.h
MythLocale
Definition: mythlocale.h:12
MythCoreContext::emitTVPlaybackStopped
void emitTVPlaybackStopped(void)
Definition: mythcorecontext.h:288
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:11
MythCoreContext::emitTVPlaybackPlaying
void emitTVPlaybackPlaying(void)
Definition: mythcorecontext.h:294
MDBManager
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:52
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
MythCoreContext::emitTVPlaybackStarted
void emitTVPlaybackStarted(void)
Definition: mythcorecontext.h:287
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::SaveDurSetting
std::enable_if_t< std::chrono::__is_duration< T >::value, void > SaveDurSetting(const QString &key, T newValue)
Definition: mythcorecontext.h:157
MythCoreContext::emitTVPlaybackSought
void emitTVPlaybackSought(qint64 position)
Definition: mythcorecontext.h:289
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:60
SendEvent
static int SendEvent(const MythUtilCommandLineParser &cmdline)
Definition: backendutils.cpp:53
MythSocket
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:26
kMythSocketLongTimeout
static constexpr std::chrono::milliseconds kMythSocketLongTimeout
Definition: mythsocket_cb.h:10
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:314
MythCoreContext::SetDVBv3
void SetDVBv3(bool dvbv3)
Definition: mythcorecontext.h:326
MythCoreContext::GetDurSetting
std::enable_if_t< std::chrono::__is_duration< T >::value, T > GetDurSetting(const QString &key, T defaultval=T::zero())
Definition: mythcorecontext.h:170
MythCoreContext::connectionFailed
void connectionFailed(MythSocket *sock) override
Definition: mythcorecontext.h:315
mythlocale.h
MythSocketCBs
Definition: mythsocket_cb.h:13
MythCoreContext::GetDVBv3
bool GetDVBv3(void) const
Definition: mythcorecontext.h:327
sched
Scheduler * sched
CoreWaitInfo
Definition: mythcorecontext.h:47
MythCoreContext
This class contains the runtime context for MythTV.
Definition: mythcorecontext.h:62
MythSocketCBs::connectionClosed
virtual void connectionClosed(MythSocket *)=0
MythCoreContext::emitTVPlaybackPaused
void emitTVPlaybackPaused(void)
Definition: mythcorecontext.h:291
ClearSettingsCache
static int ClearSettingsCache(const MythUtilCommandLineParser &)
Definition: backendutils.cpp:39
MythCoreContext::ResolveType
ResolveType
Definition: mythcorecontext.h:211
musicbrainzngs.caa.hostname
string hostname
Definition: caa.py:17
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
CoreWaitInfo::name
const char * name
Definition: mythcorecontext.h:48
IsMasterBackend
static TransMythUICheckBoxSetting * IsMasterBackend()
Definition: backendsettings.cpp:12
MythCoreContextPrivate
Definition: mythcorecontext.cpp:62
MythCoreContext::RegisterForPlayback
std::enable_if_t< std::is_member_function_pointer_v< SLOT >, void > RegisterForPlayback(OBJ *sender, SLOT method)
Definition: mythcorecontext.h:252
MythCoreContext::PlaybackStartCb
void(QObject::*)(void) PlaybackStartCb
Definition: mythcorecontext.h:240
MythCoreContext::SaveBoolSetting
void SaveBoolSetting(const QString &key, bool newValue)
Definition: mythcorecontext.h:162
mythsession.h
mythobservable.h
MythCoreContext::emitTVPlaybackAborted
void emitTVPlaybackAborted(void)
Definition: mythcorecontext.h:293
MythSessionManager
Definition: mythsession.h:98