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