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