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