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 class Configuration;
20 
21 #define MYTH_APPNAME_MYTHBACKEND "mythbackend"
22 #define MYTH_APPNAME_MYTHJOBQUEUE "mythjobqueue"
23 #define MYTH_APPNAME_MYTHFRONTEND "mythfrontend"
24 #define MYTH_APPNAME_MYTHTV_SETUP "mythtv-setup"
25 #define MYTH_APPNAME_MYTHFILLDATABASE "mythfilldatabase"
26 #define MYTH_APPNAME_MYTHCOMMFLAG "mythcommflag"
27 #define MYTH_APPNAME_MYTHCCEXTRACTOR "mythccextractor"
28 #define MYTH_APPNAME_MYTHPREVIEWGEN "mythpreviewgen"
29 #define MYTH_APPNAME_MYTHTRANSCODE "mythtranscode"
30 #define MYTH_APPNAME_MYTHWELCOME "mythwelcome"
31 #define MYTH_APPNAME_MYTHSHUTDOWN "mythshutdown"
32 #define MYTH_APPNAME_MYTHLCDSERVER "mythlcdserver"
33 #define MYTH_APPNAME_MYTHAVTEST "mythavtest"
34 #define MYTH_APPNAME_MYTHMEDIASERVER "mythmediaserver"
35 #define MYTH_APPNAME_MYTHMETADATALOOKUP "mythmetadatalookup"
36 #define MYTH_APPNAME_MYTHUTIL "mythutil"
37 #define MYTH_APPNAME_MYTHSCREENWIZARD "mythscreenwizard"
38 #define MYTH_APPNAME_MYTHFFPROBE "mythffprobe"
39 #define MYTH_APPNAME_MYTHEXTERNRECORDER "mythexternrecorder"
40 
41 class MDBManager;
43 class MythSocket;
44 class MythScheduler;
45 class MythPluginManager;
46 
47 class MythCoreContext;
48 using CoreWaitSigFn = void (MythCoreContext::*)(void);
49 struct CoreWaitInfo {
50  const char *name;
52 };
53 
64 class MBASE_PUBLIC MythCoreContext : public QObject, public MythObservable, public MythSocketCBs
65 {
66  Q_OBJECT
67  public:
68  MythCoreContext(const QString &binversion, QObject *guiContext);
69  ~MythCoreContext() override;
70 
71  bool Init(void);
72 
73  void SetLocalHostname(const QString &hostname);
74  void SetServerSocket(MythSocket *serverSock);
75  void SetEventSocket(MythSocket *eventSock);
76  void SetScheduler(MythScheduler *sched);
77 
78  bool SafeConnectToMasterServer(bool blockingClient = true,
79  bool openEventSocket = true);
80  bool ConnectToMasterServer(bool blockingClient = true,
81  bool openEventSocket = true);
82 
83  MythSocket *ConnectCommandSocket(const QString &hostname, int port,
84  const QString &announcement,
85  bool *proto_mismatch = nullptr,
86  int maxConnTry = -1,
87  std::chrono::milliseconds setup_timeout = -1ms);
88 
89  MythSocket *ConnectEventSocket(const QString &hostname, int port);
90 
91  bool SetupCommandSocket(MythSocket *serverSock, const QString &announcement,
92  std::chrono::milliseconds timeout, bool &proto_mismatch);
93 
94  bool CheckProtoVersion(MythSocket *socket,
95  std::chrono::milliseconds timeout = kMythSocketLongTimeout,
96  bool error_dialog_desired = false);
97 
98  static QString GenMythURL(const QString& host = QString(), int port = 0,
99  QString path = QString(),
100  const QString& storageGroup = QString());
101 
102  QString GetMasterHostPrefix(const QString &storageGroup = QString(),
103  const QString &path = QString());
104  QString GetMasterHostName(void);
105  QString GetHostName(void);
106  QString GetFilePrefix(void);
107 
108  bool IsConnectedToMaster(void);
109  void SetAsBackend(bool backend);
110  bool IsBackend(void) const;
111  void SetAsFrontend(bool frontend);
112  bool IsFrontend(void) const;
113  bool IsFrontendOnly(void);
114  bool IsMasterHost(void);
116  bool IsMasterHost(const QString &host);
117  bool IsMasterBackend(void);
118  static bool BackendIsRunning(void);
119 
120  bool IsThisBackend(const QString &addr);
121  bool IsThisHost(const QString &addr);
122  bool IsThisHost(const QString &addr, const QString &host);
123 
124  void BlockShutdown(void);
125  void AllowShutdown(void);
126  bool IsBlockingClient(void) const;
127 
128  void SetWOLAllowed(bool allow);
129  bool IsWOLAllowed() const;
130 
131  bool SendReceiveStringList(QStringList &strlist, bool quickTimeout = false,
132  bool block = true);
133  void SendMessage(const QString &message);
134  void SendEvent(const MythEvent &event);
135  void SendSystemEvent(const QString &msg);
136  void SendHostSystemEvent(const QString &msg, const QString &hostname,
137  const QString &args);
138 
139  void SetGUIObject(QObject *gui);
140  QObject *GetGUIObject(void);
141  QObject *GetGUIContext(void);
142  bool HasGUI(void) const;
143  bool IsUIThread(void);
144 
145  MythDB *GetDB(void);
146  MDBManager *GetDBManager(void);
147  MythScheduler *GetScheduler(void);
148 
149  bool IsDatabaseIgnored(void) const;
151  { return GetDB()->GetDatabaseParams(); }
152 
153  void SaveSetting(const QString &key, int newValue);
154  void SaveSetting(const QString &key, const QString &newValue);
155  QString GetSetting(const QString &key, const QString &defaultval = "");
156  // No conversion between duration ratios. Just extract the number.
157  template <typename T>
158  typename std::enable_if_t<std::chrono::__is_duration<T>::value, void>
159  SaveDurSetting(const QString &key, T newValue)
160  { SaveSetting(key, static_cast<int>(newValue.count())); }
161 
162  bool SaveSettingOnHost(const QString &key, const QString &newValue,
163  const QString &host);
164  void SaveBoolSetting(const QString &key, bool newValue)
165  { SaveSetting(key, static_cast<int>(newValue)); }
166 
167  // Convenience setting query methods
168  bool GetBoolSetting(const QString &key, bool defaultval = false);
169  int GetNumSetting(const QString &key, int defaultval = 0);
170  template <typename T>
171  typename std::enable_if_t<std::chrono::__is_duration<T>::value, T>
172  GetDurSetting(const QString &key, T defaultval = T::zero())
173  { return T(GetNumSetting(key, static_cast<int>(defaultval.count()))); }
174  int GetBoolSetting(const QString &key, int defaultval) = delete;
175  bool GetNumSetting(const QString &key, bool defaultvalue) = delete;
176  double GetFloatSetting(const QString &key, double defaultval = 0.0);
177  void GetResolutionSetting(const QString &type, int &width, int &height,
178  double& forced_aspect, double &refresh_rate,
179  int index=-1);
180  void GetResolutionSetting(const QString &type, int &width, int &height,
181  int index=-1);
182 
183  QString GetSettingOnHost(const QString &key, const QString &host,
184  const QString &defaultval = "");
185  bool GetBoolSettingOnHost(const QString &key, const QString &host,
186  bool defaultval = false);
187  int GetNumSettingOnHost(const QString &key, const QString &host,
188  int defaultval = 0);
189  int GetBoolSettingOnHost(const QString &key, const QString &host,
190  int defaultval) = delete;
191  bool GetNumSettingOnHost(const QString &key, const QString &host,
192  bool defaultval = false) = delete;
193  double GetFloatSettingOnHost(const QString &key, const QString &host,
194  double defaultval = 0.0);
195 
196  QString GetBackendServerIP(void);
197  QString GetBackendServerIP(const QString &host);
198  QString GetBackendServerIP4(void);
199  QString GetBackendServerIP4(const QString &host);
200  QString GetBackendServerIP6(void);
201  QString GetBackendServerIP6(const QString &host);
202  QString GetMasterServerIP(void);
203  static int GetMasterServerPort(void);
204  int GetMasterServerStatusPort(void);
205  int GetBackendServerPort(void);
206  int GetBackendServerPort(const QString &host);
207  static void ClearBackendServerPortCache();
208  int GetBackendStatusPort(void);
209  int GetBackendStatusPort(const QString &host);
210  static QHash<QString,int> s_serverPortCache;
211 
212  bool GetScopeForAddress(QHostAddress &addr) const;
213  void SetScopeForAddress(const QHostAddress &addr);
214  void SetScopeForAddress(const QHostAddress &addr, int scope);
215  enum ResolveType { ResolveAny = -1, ResolveIPv4 = 0, ResolveIPv6 = 1 };
216  QString resolveSettingAddress(const QString &name,
217  const QString &host = QString(),
218  ResolveType type = ResolveAny,
219  bool keepscope = false);
220  static QString resolveAddress(const QString &host,
221  ResolveType type = ResolveAny,
222  bool keepscope = false) ;
223  bool CheckSubnet(const QAbstractSocket *socket);
224  bool CheckSubnet(const QHostAddress &peer);
225 
226  void ClearSettingsCache(const QString &myKey = QString(""));
227  void ActivateSettingsCache(bool activate = true);
228  void OverrideSettingForSession(const QString &key, const QString &value);
229  void ClearOverrideSettingForSession(const QString &key);
230 
231  void dispatch(const MythEvent &event);
232 
233  void InitPower(bool Create = true);
234  void InitLocale(void);
235  void ReInitLocale(void);
236  MythLocale *GetLocale(void) const;
237  QLocale GetQLocale(void);
238  void SaveLocaleDefaults(void);
239  QString GetLanguage(void);
240  QString GetLanguageAndVariant(void);
241  void ResetLanguage(void);
242  void ResetSockets(void);
243 
244  static void SetConfiguration( Configuration *pConfig );
245  static Configuration* GetConfiguration();
247 
248  using PlaybackStartCb = void (QObject::*)(void);
249 
256  void RegisterForPlayback(QObject *sender, PlaybackStartCb method);
257 
258  template <class OBJ, typename SLOT>
259  typename std::enable_if_t<std::is_member_function_pointer_v<SLOT>, void>
260  RegisterForPlayback(OBJ *sender, SLOT method)
261  {
262  RegisterForPlayback(qobject_cast<QObject*>(sender),
263  static_cast<PlaybackStartCb>(method));
264  }
265 
266  void UnregisterForPlayback(QObject *sender);
267  void WantingPlayback(QObject *sender);
268  bool InWantingPlayback(void);
269  void TVInWantingPlayback(bool b);
270 
271  MythSessionManager *GetSessionManager(void);
272 
273  // Plugin related methods
274  static bool TestPluginVersion(const QString &name, const QString &libversion,
275  const QString &pluginversion);
276 
277  void SetPluginManager(MythPluginManager *pmanager);
278  MythPluginManager *GetPluginManager(void);
279 
280  // Set when QEventLoop has been stopped and about to exit application
281  void SetExiting(bool exiting = true);
282  bool IsExiting(void);
283 
284  void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
285  void UnregisterFileForWrite(const QString &file);
286  bool IsRegisteredFileForWrite(const QString &file);
287 
288  // Test Harness help
289  void setTestIntSettings(QMap<QString,int>& overrides);
290  void setTestFloatSettings(QMap<QString,double>& overrides);
291  void setTestStringSettings(QMap<QString,QString>& overrides);
292 
293  // signal related methods
294  void WaitUntilSignals(std::vector<CoreWaitInfo> & sigs) const;
295  void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
296  void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
297  void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position);
298  emit TVPlaybackSought();}
299  void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
300  void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
301  void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
302  void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
303 
304 
305  signals:
306  void TVPlaybackStarted(void);
309  void TVPlaybackStopped(void);
310  void TVPlaybackSought(qint64 position);
311  void TVPlaybackSought(void);
312  void TVPlaybackPaused(void);
313  void TVPlaybackUnpaused(void);
314  void TVPlaybackAborted(void);
315  void TVPlaybackAboutToStart(void);
316  void TVPlaybackPlaying(void);
317 
318  private:
319  Q_DISABLE_COPY(MythCoreContext)
320  MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
321 
322  void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
323  void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
324  void connectionClosed(MythSocket *sock) override; // MythSocketCBs
325  void readyRead(MythSocket *sock) override; // MythSocketCBs
326 
327  QMap<QString,int> m_testOverrideInts {};
328  QMap<QString,double> m_testOverrideFloats {};
329  QMap<QString,QString> m_testOverrideStrings {};
330 
331  private:
332  bool m_dvbv3 {false};
333  public:
334  void SetDVBv3(bool dvbv3) { m_dvbv3 = dvbv3; }
335  bool GetDVBv3(void) const { return m_dvbv3; }
336 };
337 
340 
341 #endif
342 
343 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythCoreContext::emitTVPlaybackUnpaused
void emitTVPlaybackUnpaused(void)
Definition: mythcorecontext.h:300
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:296
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:302
MDBManager
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:53
MythCoreContext::s_serverPortCache
static QHash< QString, int > s_serverPortCache
Definition: mythcorecontext.h:210
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
sched
Scheduler * sched
MythCoreContext::emitTVPlaybackStarted
void emitTVPlaybackStarted(void)
Definition: mythcorecontext.h:295
MythCoreContext::GetDatabaseParams
DatabaseParams GetDatabaseParams(void)
Definition: mythcorecontext.h:150
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:159
MythCoreContext::emitTVPlaybackSought
void emitTVPlaybackSought(qint64 position)
Definition: mythcorecontext.h:297
SendSystemEvent
static int SendSystemEvent(const MythUtilCommandLineParser &cmdline)
Definition: backendutils.cpp:52
gCoreContext
MBASE_PUBLIC MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:61
SendEvent
static int SendEvent(const MythUtilCommandLineParser &cmdline)
Definition: backendutils.cpp:47
MythSocket
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:25
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:48
MythSocketCBs::readyRead
virtual void readyRead(MythSocket *)=0
mythlogging.h
CoreWaitInfo::fn
CoreWaitSigFn fn
Definition: mythcorecontext.h:51
MythCoreContext::connected
void connected(MythSocket *sock) override
Definition: mythcorecontext.h:322
MythCoreContext::SetDVBv3
void SetDVBv3(bool dvbv3)
Definition: mythcorecontext.h:334
MythCoreContext::GetDurSetting
std::enable_if_t< std::chrono::__is_duration< T >::value, T > GetDurSetting(const QString &key, T defaultval=T::zero())
Definition: mythcorecontext.h:172
MythCoreContext::connectionFailed
void connectionFailed(MythSocket *sock) override
Definition: mythcorecontext.h:323
mythlocale.h
MythSocketCBs
Definition: mythsocket_cb.h:13
MythCoreContext::GetDVBv3
bool GetDVBv3(void) const
Definition: mythcorecontext.h:335
CoreWaitInfo
Definition: mythcorecontext.h:49
MythCoreContext
This class contains the runtime context for MythTV.
Definition: mythcorecontext.h:64
Configuration
Definition: configuration.h:21
MythSocketCBs::connectionClosed
virtual void connectionClosed(MythSocket *)=0
MythCoreContext::emitTVPlaybackPaused
void emitTVPlaybackPaused(void)
Definition: mythcorecontext.h:299
ClearSettingsCache
static int ClearSettingsCache(const MythUtilCommandLineParser &)
Definition: backendutils.cpp:33
MythCoreContext::ResolveType
ResolveType
Definition: mythcorecontext.h:215
MythCoreContext::g_pConfig
static Configuration * g_pConfig
Definition: mythcorecontext.h:246
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:50
IsMasterBackend
static TransMythUICheckBoxSetting * IsMasterBackend()
Definition: backendsettings.cpp:16
MythCoreContextPrivate
Definition: mythcorecontext.cpp:64
MythCoreContext::RegisterForPlayback
std::enable_if_t< std::is_member_function_pointer_v< SLOT >, void > RegisterForPlayback(OBJ *sender, SLOT method)
Definition: mythcorecontext.h:260
MythCoreContext::PlaybackStartCb
void(QObject::*)(void) PlaybackStartCb
Definition: mythcorecontext.h:248
MythCoreContext::SaveBoolSetting
void SaveBoolSetting(const QString &key, bool newValue)
Definition: mythcorecontext.h:164
mythsession.h
mythobservable.h
MythCoreContext::emitTVPlaybackAborted
void emitTVPlaybackAborted(void)
Definition: mythcorecontext.h:301
MythSessionManager
Definition: mythsession.h:98