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