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