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