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 
201  void ClearSettingsCache(const QString &myKey = QString(""));
202  void ActivateSettingsCache(bool activate = true);
203  void OverrideSettingForSession(const QString &key, const QString &value);
204  void ClearOverrideSettingForSession(const QString &key);
205 
206  void dispatch(const MythEvent &event);
207 
208  void InitPower(bool Create = true);
209  void InitLocale(void);
210  void ReInitLocale(void);
211  MythLocale *GetLocale(void) const;
212  QLocale GetQLocale(void);
213  void SaveLocaleDefaults(void);
214  QString GetLanguage(void);
215  QString GetLanguageAndVariant(void);
216  void ResetLanguage(void);
217  QString GetAudioLanguage(void);
218  QString GetAudioLanguageAndVariant(void);
219  void ResetAudioLanguage(void);
220  void ResetSockets(void);
221 
222  using PlaybackStartCb = void (QObject::*)(void);
223 
230  void RegisterForPlayback(QObject *sender, PlaybackStartCb method);
231 
232  template <class OBJ, typename SLOT>
233  typename std::enable_if_t<std::is_member_function_pointer_v<SLOT>, void>
234  RegisterForPlayback(OBJ *sender, SLOT method)
235  {
236  RegisterForPlayback(qobject_cast<QObject*>(sender),
237  static_cast<PlaybackStartCb>(method));
238  }
239 
240  void UnregisterForPlayback(QObject *sender);
241  void WantingPlayback(QObject *sender);
242  bool InWantingPlayback(void);
243  void TVInWantingPlayback(bool b);
244 
245  MythSessionManager *GetSessionManager(void);
246 
247  // Plugin related methods
248  static bool TestPluginVersion(const QString &name, const QString &libversion,
249  const QString &pluginversion);
250 
251  void SetPluginManager(MythPluginManager *pmanager);
252  MythPluginManager *GetPluginManager(void);
253 
254  // Set when QEventLoop has been stopped and about to exit application
255  void SetExiting(bool exiting = true);
256  bool IsExiting(void);
257 
258  void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
259  void UnregisterFileForWrite(const QString &file);
260  bool IsRegisteredFileForWrite(const QString &file);
261 
262  // Test Harness help
263  void setTestIntSettings(QMap<QString,int>& overrides);
264  void setTestFloatSettings(QMap<QString,double>& overrides);
265  void setTestStringSettings(QMap<QString,QString>& overrides);
266 
267  // signal related methods
268  void WaitUntilSignals(std::vector<CoreWaitInfo> & sigs) const;
269  void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
270  void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
271  void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position);
272  emit TVPlaybackSought();}
273  void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
274  void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
275  void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
276  void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
277 
278 
279  signals:
280  void TVPlaybackStarted(void);
283  void TVPlaybackStopped(void);
284  void TVPlaybackSought(qint64 position);
285  void TVPlaybackSought(void);
286  void TVPlaybackPaused(void);
287  void TVPlaybackUnpaused(void);
288  void TVPlaybackAborted(void);
289  void TVPlaybackAboutToStart(void);
290  void TVPlaybackPlaying(void);
291 
292  private:
293  Q_DISABLE_COPY_MOVE(MythCoreContext)
294  MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
295 
296  void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
297  void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
298  void connectionClosed(MythSocket *sock) override; // MythSocketCBs
299  void readyRead(MythSocket *sock) override; // MythSocketCBs
300 
301  QMap<QString,int> m_testOverrideInts;
302  QMap<QString,double> m_testOverrideFloats;
303  QMap<QString,QString> m_testOverrideStrings;
304 
305  private:
306  bool m_dvbv3 {false};
307  public:
308  void SetDVBv3(bool dvbv3) { m_dvbv3 = dvbv3; }
309  bool GetDVBv3(void) const { return m_dvbv3; }
310 };
311 
314 
315 #endif
316 
317 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythCoreContext::emitTVPlaybackUnpaused
void emitTVPlaybackUnpaused(void)
Definition: mythcorecontext.h:274
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:270
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:276
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:269
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:271
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:296
MythCoreContext::SetDVBv3
void SetDVBv3(bool dvbv3)
Definition: mythcorecontext.h:308
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:297
MythCoreContext::m_testOverrideStrings
QMap< QString, QString > m_testOverrideStrings
Definition: mythcorecontext.h:303
mythlocale.h
MythSocketCBs
Definition: mythsocket_cb.h:13
MythCoreContext::GetDVBv3
bool GetDVBv3(void) const
Definition: mythcorecontext.h:309
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:302
MythSocketCBs::connectionClosed
virtual void connectionClosed(MythSocket *)=0
MythCoreContext::emitTVPlaybackPaused
void emitTVPlaybackPaused(void)
Definition: mythcorecontext.h:273
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
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:234
MythCoreContext::m_testOverrideInts
QMap< QString, int > m_testOverrideInts
Definition: mythcorecontext.h:301
MythCoreContext::PlaybackStartCb
void(QObject::*)(void) PlaybackStartCb
Definition: mythcorecontext.h:222
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:275
MythSessionManager
Definition: mythsession.h:104