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 static constexpr const char * MYTH_APPNAME_MYTHBACKEND { "mythbackend" };
20 static constexpr const char * MYTH_APPNAME_MYTHJOBQUEUE { "mythjobqueue" };
21 static constexpr const char * MYTH_APPNAME_MYTHFRONTEND { "mythfrontend" };
22 static constexpr const char * MYTH_APPNAME_MYTHTV_SETUP { "mythtv-setup" };
23 static constexpr const char * MYTH_APPNAME_MYTHFILLDATABASE { "mythfilldatabase" };
24 static constexpr const char * MYTH_APPNAME_MYTHCOMMFLAG { "mythcommflag" };
25 static constexpr const char * MYTH_APPNAME_MYTHCCEXTRACTOR { "mythccextractor" };
26 static constexpr const char * MYTH_APPNAME_MYTHPREVIEWGEN { "mythpreviewgen" };
27 static constexpr const char * MYTH_APPNAME_MYTHTRANSCODE { "mythtranscode" };
28 static constexpr const char * MYTH_APPNAME_MYTHWELCOME { "mythwelcome" };
29 static constexpr const char * MYTH_APPNAME_MYTHSHUTDOWN { "mythshutdown" };
30 static constexpr const char * MYTH_APPNAME_MYTHLCDSERVER { "mythlcdserver" };
31 static constexpr const char * MYTH_APPNAME_MYTHAVTEST { "mythavtest" };
32 static constexpr const char * MYTH_APPNAME_MYTHMEDIASERVER { "mythmediaserver" };
33 static constexpr const char * MYTH_APPNAME_MYTHMETADATALOOKUP { "mythmetadatalookup" };
34 static constexpr const char * MYTH_APPNAME_MYTHUTIL { "mythutil" };
35 static constexpr const char * MYTH_APPNAME_MYTHSCREENWIZARD { "mythscreenwizard" };
36 static constexpr const char * MYTH_APPNAME_MYTHFFPROBE { "mythffprobe" };
37 static constexpr const char * 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;
148 
149  void SaveSetting(const QString &key, int newValue);
150  void SaveSetting(const QString &key, const QString &newValue);
151  QString GetSetting(const QString &key, const QString &defaultval = "");
152  // No conversion between duration ratios. Just extract the number.
153  template <typename T>
154  typename std::enable_if_t<std::chrono::__is_duration<T>::value, void>
155  SaveDurSetting(const QString &key, T newValue)
156  { SaveSetting(key, static_cast<int>(newValue.count())); }
157 
158  bool SaveSettingOnHost(const QString &key, const QString &newValue,
159  const QString &host);
160  void SaveBoolSetting(const QString &key, bool newValue)
161  { SaveSetting(key, static_cast<int>(newValue)); }
162 
163  // Convenience setting query methods
164  bool GetBoolSetting(const QString &key, bool defaultval = false);
165  int GetNumSetting(const QString &key, int defaultval = 0);
166  template <typename T>
167  typename std::enable_if_t<std::chrono::__is_duration<T>::value, T>
168  GetDurSetting(const QString &key, T defaultval = T::zero())
169  { return T(GetNumSetting(key, static_cast<int>(defaultval.count()))); }
170  int GetBoolSetting(const QString &key, int defaultval) = delete;
171  bool GetNumSetting(const QString &key, bool defaultvalue) = delete;
172  double GetFloatSetting(const QString &key, double defaultval = 0.0);
173  void GetResolutionSetting(const QString &type, int &width, int &height,
174  double& forced_aspect, double &refresh_rate,
175  int index=-1);
176  void GetResolutionSetting(const QString &type, int &width, int &height,
177  int index=-1);
178 
179  QString GetSettingOnHost(const QString &key, const QString &host,
180  const QString &defaultval = "");
181  bool GetBoolSettingOnHost(const QString &key, const QString &host,
182  bool defaultval = false);
183  int GetNumSettingOnHost(const QString &key, const QString &host,
184  int defaultval = 0);
185  int GetBoolSettingOnHost(const QString &key, const QString &host,
186  int defaultval) = delete;
187  bool GetNumSettingOnHost(const QString &key, const QString &host,
188  bool defaultval = false) = delete;
189  double GetFloatSettingOnHost(const QString &key, const QString &host,
190  double defaultval = 0.0);
191 
192  QString GetBackendServerIP(void);
193  QString GetBackendServerIP(const QString &host);
194  QString GetBackendServerIP4(void);
195  QString GetBackendServerIP4(const QString &host);
196  QString GetBackendServerIP6(void);
197  QString GetBackendServerIP6(const QString &host);
198  QString GetMasterServerIP(void);
199  static int GetMasterServerPort(void);
200  int GetMasterServerStatusPort(void);
201  int GetBackendServerPort(void);
202  int GetBackendServerPort(const QString &host);
203  static void ClearBackendServerPortCache();
204  int GetBackendStatusPort(void);
205  int GetBackendStatusPort(const QString &host);
206  static QHash<QString,int> s_serverPortCache;
207 
208  bool GetScopeForAddress(QHostAddress &addr) const;
209  void SetScopeForAddress(const QHostAddress &addr);
210  void SetScopeForAddress(const QHostAddress &addr, int scope);
211  enum ResolveType : std::int8_t { 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  QString GetAudioLanguage(void);
239  QString GetAudioLanguageAndVariant(void);
240  void ResetAudioLanguage(void);
241  void ResetSockets(void);
242 
243  using PlaybackStartCb = void (QObject::*)(void);
244 
251  void RegisterForPlayback(QObject *sender, PlaybackStartCb method);
252 
253  template <class OBJ, typename SLOT>
254  typename std::enable_if_t<std::is_member_function_pointer_v<SLOT>, void>
255  RegisterForPlayback(OBJ *sender, SLOT method)
256  {
257  RegisterForPlayback(qobject_cast<QObject*>(sender),
258  static_cast<PlaybackStartCb>(method));
259  }
260 
261  void UnregisterForPlayback(QObject *sender);
262  void WantingPlayback(QObject *sender);
263  bool InWantingPlayback(void);
264  void TVInWantingPlayback(bool b);
265 
266  MythSessionManager *GetSessionManager(void);
267 
268  // Plugin related methods
269  static bool TestPluginVersion(const QString &name, const QString &libversion,
270  const QString &pluginversion);
271 
272  void SetPluginManager(MythPluginManager *pmanager);
273  MythPluginManager *GetPluginManager(void);
274 
275  // Set when QEventLoop has been stopped and about to exit application
276  void SetExiting(bool exiting = true);
277  bool IsExiting(void);
278 
279  void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
280  void UnregisterFileForWrite(const QString &file);
281  bool IsRegisteredFileForWrite(const QString &file);
282 
283  // Test Harness help
284  void setTestIntSettings(QMap<QString,int>& overrides);
285  void setTestFloatSettings(QMap<QString,double>& overrides);
286  void setTestStringSettings(QMap<QString,QString>& overrides);
287 
288  // signal related methods
289  void WaitUntilSignals(std::vector<CoreWaitInfo> & sigs) const;
290  void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
291  void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
292  void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position);
293  emit TVPlaybackSought();}
294  void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
295  void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
296  void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
297  void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
298 
299 
300  signals:
301  void TVPlaybackStarted(void);
304  void TVPlaybackStopped(void);
305  void TVPlaybackSought(qint64 position);
306  void TVPlaybackSought(void);
307  void TVPlaybackPaused(void);
308  void TVPlaybackUnpaused(void);
309  void TVPlaybackAborted(void);
310  void TVPlaybackAboutToStart(void);
311  void TVPlaybackPlaying(void);
312 
313  private:
314  Q_DISABLE_COPY_MOVE(MythCoreContext)
315  MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
316 
317  void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
318  void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
319  void connectionClosed(MythSocket *sock) override; // MythSocketCBs
320  void readyRead(MythSocket *sock) override; // MythSocketCBs
321 
322  QMap<QString,int> m_testOverrideInts;
323  QMap<QString,double> m_testOverrideFloats;
324  QMap<QString,QString> m_testOverrideStrings;
325 
326  private:
327  bool m_dvbv3 {false};
328  public:
329  void SetDVBv3(bool dvbv3) { m_dvbv3 = dvbv3; }
330  bool GetDVBv3(void) const { return m_dvbv3; }
331 };
332 
335 
336 #endif
337 
338 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythCoreContext::emitTVPlaybackUnpaused
void emitTVPlaybackUnpaused(void)
Definition: mythcorecontext.h:295
build_compdb.args
args
Definition: build_compdb.py:11
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:102
MythPluginManager
Definition: mythplugin.h:62
MYTH_APPNAME_MYTHLCDSERVER
static constexpr const char * MYTH_APPNAME_MYTHLCDSERVER
Definition: mythcorecontext.h:30
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:291
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:297
MDBManager
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:54
MythCoreContext::s_serverPortCache
static QHash< QString, int > s_serverPortCache
Definition: mythcorecontext.h:206
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:290
MYTH_APPNAME_MYTHFFPROBE
static constexpr const char * MYTH_APPNAME_MYTHFFPROBE
Definition: mythcorecontext.h:36
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:155
MythCoreContext::emitTVPlaybackSought
void emitTVPlaybackSought(qint64 position)
Definition: mythcorecontext.h:292
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:27
SendEvent
static int SendEvent(const MythUtilCommandLineParser &cmdline)
Definition: backendutils.cpp:47
MYTH_APPNAME_MYTHCCEXTRACTOR
static constexpr const char * MYTH_APPNAME_MYTHCCEXTRACTOR
Definition: mythcorecontext.h:25
MYTH_APPNAME_MYTHTV_SETUP
static constexpr const char * MYTH_APPNAME_MYTHTV_SETUP
Definition: mythcorecontext.h:22
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: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:317
MYTH_APPNAME_MYTHBACKEND
static constexpr const char * MYTH_APPNAME_MYTHBACKEND
Definition: mythcorecontext.h:19
MYTH_APPNAME_MYTHMEDIASERVER
static constexpr const char * MYTH_APPNAME_MYTHMEDIASERVER
Definition: mythcorecontext.h:32
MYTH_APPNAME_MYTHCOMMFLAG
static constexpr const char * MYTH_APPNAME_MYTHCOMMFLAG
Definition: mythcorecontext.h:24
MythCoreContext::SetDVBv3
void SetDVBv3(bool dvbv3)
Definition: mythcorecontext.h:329
MythCoreContext::GetDurSetting
std::enable_if_t< std::chrono::__is_duration< T >::value, T > GetDurSetting(const QString &key, T defaultval=T::zero())
Definition: mythcorecontext.h:168
MYTH_APPNAME_MYTHSHUTDOWN
static constexpr const char * MYTH_APPNAME_MYTHSHUTDOWN
Definition: mythcorecontext.h:29
MythCoreContext::connectionFailed
void connectionFailed(MythSocket *sock) override
Definition: mythcorecontext.h:318
MythCoreContext::m_testOverrideStrings
QMap< QString, QString > m_testOverrideStrings
Definition: mythcorecontext.h:324
mythlocale.h
MythSocketCBs
Definition: mythsocket_cb.h:13
MythCoreContext::GetDVBv3
bool GetDVBv3(void) const
Definition: mythcorecontext.h:330
MYTH_APPNAME_MYTHEXTERNRECORDER
static constexpr const char * MYTH_APPNAME_MYTHEXTERNRECORDER
Definition: mythcorecontext.h:37
MYTH_APPNAME_MYTHAVTEST
static constexpr const char * MYTH_APPNAME_MYTHAVTEST
Definition: mythcorecontext.h:31
MYTH_APPNAME_MYTHJOBQUEUE
static constexpr const char * MYTH_APPNAME_MYTHJOBQUEUE
Definition: mythcorecontext.h:20
CoreWaitInfo
Definition: mythcorecontext.h:47
MYTH_APPNAME_MYTHFRONTEND
static constexpr const char * MYTH_APPNAME_MYTHFRONTEND
Definition: mythcorecontext.h:21
MythCoreContext
This class contains the runtime context for MythTV.
Definition: mythcorecontext.h:62
MYTH_APPNAME_MYTHFILLDATABASE
static constexpr const char * MYTH_APPNAME_MYTHFILLDATABASE
Definition: mythcorecontext.h:23
MythCoreContext::m_testOverrideFloats
QMap< QString, double > m_testOverrideFloats
Definition: mythcorecontext.h:323
MythSocketCBs::connectionClosed
virtual void connectionClosed(MythSocket *)=0
MYTH_APPNAME_MYTHSCREENWIZARD
static constexpr const char * MYTH_APPNAME_MYTHSCREENWIZARD
Definition: mythcorecontext.h:35
MythCoreContext::emitTVPlaybackPaused
void emitTVPlaybackPaused(void)
Definition: mythcorecontext.h:294
ClearSettingsCache
static int ClearSettingsCache(const MythUtilCommandLineParser &)
Definition: backendutils.cpp:33
MYTH_APPNAME_MYTHUTIL
static constexpr const char * MYTH_APPNAME_MYTHUTIL
Definition: mythcorecontext.h:34
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
MYTH_APPNAME_MYTHMETADATALOOKUP
static constexpr const char * MYTH_APPNAME_MYTHMETADATALOOKUP
Definition: mythcorecontext.h:33
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:26
MythCoreContext::RegisterForPlayback
std::enable_if_t< std::is_member_function_pointer_v< SLOT >, void > RegisterForPlayback(OBJ *sender, SLOT method)
Definition: mythcorecontext.h:255
MythCoreContext::m_testOverrideInts
QMap< QString, int > m_testOverrideInts
Definition: mythcorecontext.h:322
MYTH_APPNAME_MYTHWELCOME
static constexpr const char * MYTH_APPNAME_MYTHWELCOME
Definition: mythcorecontext.h:28
MythCoreContext::PlaybackStartCb
void(QObject::*)(void) PlaybackStartCb
Definition: mythcorecontext.h:243
MythCoreContext::SaveBoolSetting
void SaveBoolSetting(const QString &key, bool newValue)
Definition: mythcorecontext.h:160
MythCoreContext::ResolveType
ResolveType
Definition: mythcorecontext.h:211
mythsession.h
mythobservable.h
MythCoreContext::emitTVPlaybackAborted
void emitTVPlaybackAborted(void)
Definition: mythcorecontext.h:296
MythSessionManager
Definition: mythsession.h:104