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  static void ClearBackendServerPortCache();
206  int GetBackendStatusPort(void);
207  int GetBackendStatusPort(const QString &host);
208  static QHash<QString,int> s_serverPortCache;
209 
210  bool GetScopeForAddress(QHostAddress &addr) const;
211  void SetScopeForAddress(const QHostAddress &addr);
212  void SetScopeForAddress(const QHostAddress &addr, int scope);
213  enum ResolveType { ResolveAny = -1, ResolveIPv4 = 0, ResolveIPv6 = 1 };
214  QString resolveSettingAddress(const QString &name,
215  const QString &host = QString(),
216  ResolveType type = ResolveAny,
217  bool keepscope = false);
218  static QString resolveAddress(const QString &host,
219  ResolveType type = ResolveAny,
220  bool keepscope = false) ;
221  bool CheckSubnet(const QAbstractSocket *socket);
222  bool CheckSubnet(const QHostAddress &peer);
223 
224  void ClearSettingsCache(const QString &myKey = QString(""));
225  void ActivateSettingsCache(bool activate = true);
226  void OverrideSettingForSession(const QString &key, const QString &value);
227  void ClearOverrideSettingForSession(const QString &key);
228 
229  void dispatch(const MythEvent &event);
230 
231  void InitPower(bool Create = true);
232  void InitLocale(void);
233  void ReInitLocale(void);
234  MythLocale *GetLocale(void) const;
235  QLocale GetQLocale(void);
236  void SaveLocaleDefaults(void);
237  QString GetLanguage(void);
238  QString GetLanguageAndVariant(void);
239  void ResetLanguage(void);
240  void ResetSockets(void);
241 
242  using PlaybackStartCb = void (QObject::*)(void);
243 
250  void RegisterForPlayback(QObject *sender, PlaybackStartCb method);
251 
252  template <class OBJ, typename SLOT>
253  typename std::enable_if_t<std::is_member_function_pointer_v<SLOT>, void>
254  RegisterForPlayback(OBJ *sender, SLOT method)
255  {
256  RegisterForPlayback(qobject_cast<QObject*>(sender),
257  static_cast<PlaybackStartCb>(method));
258  }
259 
260  void UnregisterForPlayback(QObject *sender);
261  void WantingPlayback(QObject *sender);
262  bool InWantingPlayback(void);
263  void TVInWantingPlayback(bool b);
264 
265  MythSessionManager *GetSessionManager(void);
266 
267  // Plugin related methods
268  static bool TestPluginVersion(const QString &name, const QString &libversion,
269  const QString &pluginversion);
270 
271  void SetPluginManager(MythPluginManager *pmanager);
272  MythPluginManager *GetPluginManager(void);
273 
274  // Set when QEventLoop has been stopped and about to exit application
275  void SetExiting(bool exiting = true);
276  bool IsExiting(void);
277 
278  void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
279  void UnregisterFileForWrite(const QString &file);
280  bool IsRegisteredFileForWrite(const QString &file);
281 
282  // Test Harness help
283  void setTestIntSettings(QMap<QString,int>& overrides);
284  void setTestFloatSettings(QMap<QString,double>& overrides);
285  void setTestStringSettings(QMap<QString,QString>& overrides);
286 
287  // signal related methods
288  void WaitUntilSignals(std::vector<CoreWaitInfo> & sigs) const;
289  void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
290  void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
291  void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position);
292  emit TVPlaybackSought();}
293  void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
294  void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
295  void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
296  void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
297 
298 
299  signals:
300  void TVPlaybackStarted(void);
303  void TVPlaybackStopped(void);
304  void TVPlaybackSought(qint64 position);
305  void TVPlaybackSought(void);
306  void TVPlaybackPaused(void);
307  void TVPlaybackUnpaused(void);
308  void TVPlaybackAborted(void);
309  void TVPlaybackAboutToStart(void);
310  void TVPlaybackPlaying(void);
311 
312  private:
313  Q_DISABLE_COPY(MythCoreContext)
314  MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
315 
316  void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
317  void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
318  void connectionClosed(MythSocket *sock) override; // MythSocketCBs
319  void readyRead(MythSocket *sock) override; // MythSocketCBs
320 
321  QMap<QString,int> m_testOverrideInts {};
322  QMap<QString,double> m_testOverrideFloats {};
323  QMap<QString,QString> m_testOverrideStrings {};
324 
325  private:
326  bool m_dvbv3 {false};
327  public:
328  void SetDVBv3(bool dvbv3) { m_dvbv3 = dvbv3; }
329  bool GetDVBv3(void) const { return m_dvbv3; }
330 };
331 
334 
335 #endif
336 
337 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythCoreContext::emitTVPlaybackUnpaused
void emitTVPlaybackUnpaused(void)
Definition: mythcorecontext.h:294
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:290
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:296
MDBManager
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:52
MythCoreContext::s_serverPortCache
static QHash< QString, int > s_serverPortCache
Definition: mythcorecontext.h:208
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
MythCoreContext::emitTVPlaybackStarted
void emitTVPlaybackStarted(void)
Definition: mythcorecontext.h:289
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:291
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: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:316
MythCoreContext::SetDVBv3
void SetDVBv3(bool dvbv3)
Definition: mythcorecontext.h:328
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:317
mythlocale.h
MythSocketCBs
Definition: mythsocket_cb.h:13
MythCoreContext::GetDVBv3
bool GetDVBv3(void) const
Definition: mythcorecontext.h:329
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:293
ClearSettingsCache
static int ClearSettingsCache(const MythUtilCommandLineParser &)
Definition: backendutils.cpp:39
MythCoreContext::ResolveType
ResolveType
Definition: mythcorecontext.h:213
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:254
MythCoreContext::PlaybackStartCb
void(QObject::*)(void) PlaybackStartCb
Definition: mythcorecontext.h:242
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:295
MythSessionManager
Definition: mythsession.h:98