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 void SaveDurSetting(const QString &key, T newValue)
136 requires (std::chrono::__is_duration<T>::value)
137 { SaveSetting(key, static_cast<int>(newValue.count())); }
138
139 bool SaveSettingOnHost(const QString &key, const QString &newValue,
140 const QString &host);
141 void SaveBoolSetting(const QString &key, bool newValue)
142 { SaveSetting(key, static_cast<int>(newValue)); }
143
144 // Convenience setting query methods
145 bool GetBoolSetting(const QString &key, bool defaultval = false);
146 int GetNumSetting(const QString &key, int defaultval = 0);
147 template <typename T>
148 T GetDurSetting(const QString &key, T defaultval = T::zero())
149 requires (std::chrono::__is_duration<T>::value)
150 { return T(GetNumSetting(key, static_cast<int>(defaultval.count()))); }
151 int GetBoolSetting(const QString &key, int defaultval) = delete;
152 bool GetNumSetting(const QString &key, bool defaultvalue) = delete;
153 double GetFloatSetting(const QString &key, double defaultval = 0.0);
154 void GetResolutionSetting(const QString &type, int &width, int &height,
155 double& forced_aspect, double &refresh_rate,
156 int index=-1);
157 void GetResolutionSetting(const QString &type, int &width, int &height,
158 int index=-1);
159
160 QString GetSettingOnHost(const QString &key, const QString &host,
161 const QString &defaultval = "");
162 bool GetBoolSettingOnHost(const QString &key, const QString &host,
163 bool defaultval = false);
164 int GetNumSettingOnHost(const QString &key, const QString &host,
165 int defaultval = 0);
166 int GetBoolSettingOnHost(const QString &key, const QString &host,
167 int defaultval) = delete;
168 bool GetNumSettingOnHost(const QString &key, const QString &host,
169 bool defaultval = false) = delete;
170 double GetFloatSettingOnHost(const QString &key, const QString &host,
171 double defaultval = 0.0);
172
173 QString GetBackendServerIP(void);
174 QString GetBackendServerIP(const QString &host);
175 QString GetBackendServerIP4(void);
176 QString GetBackendServerIP4(const QString &host);
177 QString GetBackendServerIP6(void);
178 QString GetBackendServerIP6(const QString &host);
179 QString GetMasterServerIP(void);
180 static int GetMasterServerPort(void);
181 int GetMasterServerStatusPort(void);
182 int GetBackendServerPort(void);
183 int GetBackendServerPort(const QString &host);
184 static void ClearBackendServerPortCache();
185 int GetBackendStatusPort(void);
186 int GetBackendStatusPort(const QString &host);
187 static QHash<QString,int> s_serverPortCache;
188
189 bool GetScopeForAddress(QHostAddress &addr) const;
190 void SetScopeForAddress(const QHostAddress &addr);
191 void SetScopeForAddress(const QHostAddress &addr, int scope);
192 enum ResolveType : std::int8_t { ResolveAny = -1, ResolveIPv4 = 0, ResolveIPv6 = 1 };
193 QString resolveSettingAddress(const QString &name,
194 const QString &host = QString(),
195 ResolveType type = ResolveAny,
196 bool keepscope = false);
197 static QString resolveAddress(const QString &host,
198 ResolveType type = ResolveAny,
199 bool keepscope = false) ;
200 bool CheckSubnet(const QAbstractSocket *socket);
201 bool CheckSubnet(const QHostAddress &peer);
202 bool IsLocalSubnet(const QHostAddress &peer, bool log);
203
204 void ClearSettingsCache(const QString &myKey = QString(""));
205 void ActivateSettingsCache(bool activate = true);
206 void OverrideSettingForSession(const QString &key, const QString &value);
207 void ClearOverrideSettingForSession(const QString &key);
208
209 void dispatch(const MythEvent &event);
210
211 void InitPower(bool Create = true);
212 void InitLocale(void);
213 void ReInitLocale(void);
214 MythLocale *GetLocale(void) const;
215 QLocale GetQLocale(void);
216 void SaveLocaleDefaults(void);
217 QString GetLanguage(void);
218 QString GetLanguageAndVariant(void);
219 void ResetLanguage(void);
220 QString GetAudioLanguage(void);
221 QString GetAudioLanguageAndVariant(void);
222 void ResetAudioLanguage(void);
223 void ResetSockets(void);
224
225 using PlaybackStartCb = void (QObject::*)(void);
226
233 void RegisterForPlayback(QObject *sender, PlaybackStartCb method);
234
235 template <class OBJ, typename SLOT>
236 void RegisterForPlayback(OBJ *sender, SLOT method)
237 requires (std::is_member_function_pointer_v<SLOT>)
238 {
239 RegisterForPlayback(qobject_cast<QObject*>(sender),
240 static_cast<PlaybackStartCb>(method));
241 }
242
243 void UnregisterForPlayback(QObject *sender);
244 void WantingPlayback(QObject *sender);
245 bool InWantingPlayback(void);
246 void TVInWantingPlayback(bool b);
247
248 MythSessionManager *GetSessionManager(void);
249
250 // Plugin related methods
251 static bool TestPluginVersion(const QString &name, const QString &libversion,
252 const QString &pluginversion);
253
254 void SetPluginManager(MythPluginManager *pmanager);
255 MythPluginManager *GetPluginManager(void);
256
257 // Set when QEventLoop has been stopped and about to exit application
258 void SetExiting(bool exiting = true);
259 bool IsExiting(void);
260
261 void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
262 void UnregisterFileForWrite(const QString &file);
263 bool IsRegisteredFileForWrite(const QString &file);
264
265 // Test Harness help
266 void setTestIntSettings(QMap<QString,int>& overrides);
267 void setTestFloatSettings(QMap<QString,double>& overrides);
268 void setTestStringSettings(QMap<QString,QString>& overrides);
269
270 // signal related methods
271 void WaitUntilSignals(std::vector<CoreWaitInfo> & sigs) const;
272 void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
273 void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
274 void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position);
275 emit TVPlaybackSought();}
276 void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
277 void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
278 void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
279 void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
280
281
282 signals:
287 void TVPlaybackSought(qint64 position);
294
295 private:
296 Q_DISABLE_COPY_MOVE(MythCoreContext)
297 MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
298
299 void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
300 void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
301 void connectionClosed(MythSocket *sock) override; // MythSocketCBs
302 void readyRead(MythSocket *sock) override; // MythSocketCBs
303
304 QMap<QString,int> m_testOverrideInts;
305 QMap<QString,double> m_testOverrideFloats;
306 QMap<QString,QString> m_testOverrideStrings;
307
308 private:
309 bool m_dvbv3 {false};
310 public:
311 void SetDVBv3(bool dvbv3) { m_dvbv3 = dvbv3; }
312 bool GetDVBv3(void) const { return m_dvbv3; }
313};
314
317
318#endif
319
320/* 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