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 <QObject>
7 #include <QString>
8 #include <QHostAddress>
9 
10 #include "mythdb.h"
11 #include "mythbaseexp.h"
12 #include "mythobservable.h"
13 #include "mythsocket_cb.h"
14 #include "mythlogging.h"
15 #include "mythlocale.h"
16 #include "mythsession.h"
17 
18 #define MYTH_APPNAME_MYTHBACKEND "mythbackend"
19 #define MYTH_APPNAME_MYTHJOBQUEUE "mythjobqueue"
20 #define MYTH_APPNAME_MYTHFRONTEND "mythfrontend"
21 #define MYTH_APPNAME_MYTHTV_SETUP "mythtv-setup"
22 #define MYTH_APPNAME_MYTHFILLDATABASE "mythfilldatabase"
23 #define MYTH_APPNAME_MYTHCOMMFLAG "mythcommflag"
24 #define MYTH_APPNAME_MYTHCCEXTRACTOR "mythccextractor"
25 #define MYTH_APPNAME_MYTHPREVIEWGEN "mythpreviewgen"
26 #define MYTH_APPNAME_MYTHTRANSCODE "mythtranscode"
27 #define MYTH_APPNAME_MYTHWELCOME "mythwelcome"
28 #define MYTH_APPNAME_MYTHSHUTDOWN "mythshutdown"
29 #define MYTH_APPNAME_MYTHLCDSERVER "mythlcdserver"
30 #define MYTH_APPNAME_MYTHAVTEST "mythavtest"
31 #define MYTH_APPNAME_MYTHMEDIASERVER "mythmediaserver"
32 #define MYTH_APPNAME_MYTHMETADATALOOKUP "mythmetadatalookup"
33 #define MYTH_APPNAME_MYTHUTIL "mythutil"
34 #define MYTH_APPNAME_MYTHSCREENWIZARD "mythscreenwizard"
35 #define MYTH_APPNAME_MYTHFFPROBE "mythffprobe"
36 #define MYTH_APPNAME_MYTHEXTERNRECORDER "mythexternrecorder"
37 
38 class MDBManager;
40 class MythSocket;
41 class MythScheduler;
42 class MythPluginManager;
43 
54 class MBASE_PUBLIC MythCoreContext : public QObject, public MythObservable, public MythSocketCBs
55 {
56  Q_OBJECT
57  public:
58  MythCoreContext(const QString &binversion, QObject *guiContext);
59  ~MythCoreContext() override;
60 
61  bool Init(void);
62 
63  void SetLocalHostname(const QString &hostname);
64  void SetServerSocket(MythSocket *serverSock);
65  void SetEventSocket(MythSocket *eventSock);
66  void SetScheduler(MythScheduler *sched);
67 
68  bool SafeConnectToMasterServer(bool blockingClient = true,
69  bool openEventSocket = true);
70  bool ConnectToMasterServer(bool blockingClient = true,
71  bool openEventSocket = true);
72 
73  MythSocket *ConnectCommandSocket(const QString &hostname, int port,
74  const QString &announcement,
75  bool *proto_mismatch = nullptr,
76  int maxConnTry = -1,
77  int setup_timeout = -1);
78 
79  MythSocket *ConnectEventSocket(const QString &hostname, int port);
80 
81  bool SetupCommandSocket(MythSocket *serverSock, const QString &announcement,
82  uint timeout_in_ms, bool &proto_mismatch);
83 
84  bool CheckProtoVersion(MythSocket *socket,
85  uint timeout_ms = kMythSocketLongTimeout,
86  bool error_dialog_desired = false);
87 
88  static QString GenMythURL(const QString& host = QString(), int port = 0,
89  QString path = QString(),
90  const QString& storageGroup = QString());
91 
92  QString GetMasterHostPrefix(const QString &storageGroup = QString(),
93  const QString &path = QString());
94  QString GetMasterHostName(void);
95  QString GetHostName(void);
96  QString GetFilePrefix(void);
97 
98  bool IsConnectedToMaster(void);
99  void SetAsBackend(bool backend);
100  bool IsBackend(void) const;
101  void SetAsFrontend(bool frontend);
102  bool IsFrontend(void) const;
103  bool IsFrontendOnly(void);
104  bool IsMasterHost(void);
106  bool IsMasterHost(const QString &host);
107  bool IsMasterBackend(void);
108  static bool BackendIsRunning(void);
109 
110  bool IsThisBackend(const QString &addr);
111  bool IsThisHost(const QString &addr);
112  bool IsThisHost(const QString &addr, const QString &host);
113 
114  void BlockShutdown(void);
115  void AllowShutdown(void);
116  bool IsBlockingClient(void) const;
117 
118  void SetWOLAllowed(bool allow);
119  bool IsWOLAllowed() const;
120 
121  bool SendReceiveStringList(QStringList &strlist, bool quickTimeout = false,
122  bool block = true);
123  void SendMessage(const QString &message);
124  void SendEvent(const MythEvent &event);
125  void SendSystemEvent(const QString &msg);
126  void SendHostSystemEvent(const QString &msg, const QString &hostname,
127  const QString &args);
128 
129  void SetGUIObject(QObject *gui);
130  QObject *GetGUIObject(void);
131  QObject *GetGUIContext(void);
132  bool HasGUI(void) const;
133  bool IsUIThread(void);
134 
135  MythDB *GetDB(void);
136  MDBManager *GetDBManager(void);
137  MythScheduler *GetScheduler(void);
138 
139  bool IsDatabaseIgnored(void) const;
141  { return GetDB()->GetDatabaseParams(); }
142 
143  void SaveSetting(const QString &key, int newValue);
144  void SaveSetting(const QString &key, const QString &newValue);
145  QString GetSetting(const QString &key, const QString &defaultval = "");
146  bool SaveSettingOnHost(const QString &key, const QString &newValue,
147  const QString &host);
148  void SaveBoolSetting(const QString &key, bool newValue)
149  { SaveSetting(key, static_cast<int>(newValue)); }
150 
151  // Convenience setting query methods
152  bool GetBoolSetting(const QString &key, bool defaultval = false);
153  int GetNumSetting(const QString &key, int defaultval = 0);
154  int GetBoolSetting(const QString &key, int defaultval) = delete;
155  bool GetNumSetting(const QString &key, bool defaultvalue) = delete;
156  double GetFloatSetting(const QString &key, double defaultval = 0.0);
157  void GetResolutionSetting(const QString &type, int &width, int &height,
158  double& forced_aspect, double &refresh_rate,
159  int index=-1);
160  void GetResolutionSetting(const QString &type, int &width, int &height,
161  int index=-1);
162 
163  QString GetSettingOnHost(const QString &key, const QString &host,
164  const QString &defaultval = "");
165  bool GetBoolSettingOnHost(const QString &key, const QString &host,
166  bool defaultval = false);
167  int GetNumSettingOnHost(const QString &key, const QString &host,
168  int defaultval = 0);
169  int GetBoolSettingOnHost(const QString &key, const QString &host,
170  int defaultval) = delete;
171  bool GetNumSettingOnHost(const QString &key, const QString &host,
172  bool defaultval = false) = delete;
173  double GetFloatSettingOnHost(const QString &key, const QString &host,
174  double defaultval = 0.0);
175 
176  QString GetBackendServerIP(void);
177  QString GetBackendServerIP(const QString &host);
178  QString GetBackendServerIP4(void);
179  QString GetBackendServerIP4(const QString &host);
180  QString GetBackendServerIP6(void);
181  QString GetBackendServerIP6(const QString &host);
182  QString GetMasterServerIP(void);
183  static int GetMasterServerPort(void);
184  int GetMasterServerStatusPort(void);
185  int GetBackendServerPort(void);
186  int GetBackendServerPort(const QString &host);
187  int GetBackendStatusPort(void);
188  int GetBackendStatusPort(const QString &host);
189 
190  bool GetScopeForAddress(QHostAddress &addr) const;
191  void SetScopeForAddress(const QHostAddress &addr);
192  void SetScopeForAddress(const QHostAddress &addr, int scope);
193  enum ResolveType { ResolveAny = -1, ResolveIPv4 = 0, ResolveIPv6 = 1 };
194  QString resolveSettingAddress(const QString &name,
195  const QString &host = QString(),
196  ResolveType type = ResolveAny,
197  bool keepscope = false);
198  static QString resolveAddress(const QString &host,
199  ResolveType type = ResolveAny,
200  bool keepscope = false) ;
201  bool CheckSubnet(const QAbstractSocket *socket);
202  bool CheckSubnet(const QHostAddress &peer);
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  void ResetSockets(void);
221 
222  void RegisterForPlayback(QObject *sender, const char *method);
223  void UnregisterForPlayback(QObject *sender);
224  void WantingPlayback(QObject *sender);
225  bool InWantingPlayback(void);
226  void TVInWantingPlayback(bool b);
227 
228  MythSessionManager *GetSessionManager(void);
229 
230  // Plugin related methods
231  static bool TestPluginVersion(const QString &name, const QString &libversion,
232  const QString &pluginversion);
233 
234  void SetPluginManager(MythPluginManager *pmanager);
235  MythPluginManager *GetPluginManager(void);
236 
237  // Set when QEventLoop has been stopped and about to exit application
238  void SetExiting(bool exiting = true);
239  bool IsExiting(void);
240 
241  void RegisterFileForWrite(const QString &file, uint64_t size = 0LL);
242  void UnregisterFileForWrite(const QString &file);
243  bool IsRegisteredFileForWrite(const QString &file);
244 
245  // Test Harness help
246  void setTestIntSettings(QMap<QString,int>& overrides);
247  void setTestFloatSettings(QMap<QString,double>& overrides);
248  void setTestStringSettings(QMap<QString,QString>& overrides);
249 
250  // signal related methods
251  void WaitUntilSignals(std::vector<const char *> & sigs);
252  void emitTVPlaybackStarted(void) { emit TVPlaybackStarted(); }
253  void emitTVPlaybackStopped(void) { emit TVPlaybackStopped(); }
254  void emitTVPlaybackSought(qint64 position) { emit TVPlaybackSought(position); }
255  void emitTVPlaybackPaused(void) { emit TVPlaybackPaused(); }
256  void emitTVPlaybackUnpaused(void) { emit TVPlaybackUnpaused(); }
257  void emitTVPlaybackAborted(void) { emit TVPlaybackAborted(); }
258  void emitTVPlaybackPlaying(void) { emit TVPlaybackPlaying(); }
259 
260 
261  signals:
262  void TVPlaybackStarted(void);
265  void TVPlaybackStopped(void);
266  void TVPlaybackSought(qint64 position);
267  void TVPlaybackPaused(void);
268  void TVPlaybackUnpaused(void);
269  void TVPlaybackAborted(void);
271  void TVPlaybackPlaying(void);
272 
273  private:
274  Q_DISABLE_COPY(MythCoreContext)
275  MythCoreContextPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
276 
277  void connected(MythSocket *sock) override { (void)sock; } //MythSocketCBs
278  void connectionFailed(MythSocket *sock) override { (void)sock; } //MythSocketCBs
279  void connectionClosed(MythSocket *sock) override; // MythSocketCBs
280  void readyRead(MythSocket *sock) override; // MythSocketCBs
281 
282  QMap<QString,int> m_testOverrideInts {};
283  QMap<QString,double> m_testOverrideFloats {};
284  QMap<QString,QString> m_testOverrideStrings {};
285 };
286 
289 
290 #endif
291 
292 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythCoreContext::emitTVPlaybackUnpaused
void emitTVPlaybackUnpaused(void)
Definition: mythcorecontext.h:256
build_compdb.args
args
Definition: build_compdb.py:11
MythPluginManager
Definition: mythplugin.h:63
MythScheduler
This is an generic interface to a program scheduler.
Definition: mythscheduler.h:21
mythdb.h
MythCoreContext::GetBoolSettingOnHost
int GetBoolSettingOnHost(const QString &key, const QString &host, int defaultval)=delete
MythLocale
Definition: mythlocale.h:13
MythCoreContext::emitTVPlaybackStopped
void emitTVPlaybackStopped(void)
Definition: mythcorecontext.h:253
DatabaseParams
Structure containing the basic Database parameters.
Definition: mythdbparams.h:10
mythbaseexp.h
MythObservable
Superclass for making an object have a set of listeners.
Definition: mythobservable.h:12
MythCoreContext::emitTVPlaybackPlaying
void emitTVPlaybackPlaying(void)
Definition: mythcorecontext.h:258
d
static const uint16_t * d
Definition: iso6937tables.cpp:1025
MDBManager
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:54
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:17
MythCoreContext::emitTVPlaybackStarted
void emitTVPlaybackStarted(void)
Definition: mythcorecontext.h:252
MythCoreContext::GetDatabaseParams
DatabaseParams GetDatabaseParams(void)
Definition: mythcorecontext.h:140
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::emitTVPlaybackSought
void emitTVPlaybackSought(qint64 position)
Definition: mythcorecontext.h:254
SendSystemEvent
static int SendSystemEvent(const MythUtilCommandLineParser &cmdline)
Definition: backendutils.cpp:58
gCoreContext
MBASE_PUBLIC MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
MythCoreContext::GetBoolSetting
int GetBoolSetting(const QString &key, int defaultval)=delete
SendEvent
static int SendEvent(const MythUtilCommandLineParser &cmdline)
Definition: backendutils.cpp:53
MythSocket
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:27
MythCoreContext::SetEventSocket
void SetEventSocket(MythSocket *eventSock)
SendMessage
static int SendMessage(const MythUtilCommandLineParser &cmdline)
Definition: messageutils.cpp:55
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MythSocketCBs::readyRead
virtual void readyRead(MythSocket *)=0
mythlogging.h
MythCoreContext::connected
void connected(MythSocket *sock) override
Definition: mythcorecontext.h:277
MythDB
Definition: mythdb.h:15
MythCoreContext::TVPlaybackPaused
void TVPlaybackPaused(void)
MythCoreContext::TVPlaybackPlaying
void TVPlaybackPlaying(void)
MythCoreContext::GetNumSetting
bool GetNumSetting(const QString &key, bool defaultvalue)=delete
MythCoreContext::connectionFailed
void connectionFailed(MythSocket *sock) override
Definition: mythcorecontext.h:278
mythlocale.h
MythSocketCBs
Definition: mythsocket_cb.h:12
GetSetting
t<< doc.toString(4);f.close();LOG(VB_JOBQUEUE, LOG_INFO, "Copying video file");bool res=copyFile(filename, saveDirectory+title+"/"+baseName);if(!res) return 0;if(QFile::exists(filename+".png")) { LOG(VB_JOBQUEUE, LOG_INFO, "Copying preview image");res=copyFile(filename+".png", saveDirectory+title+"/"+baseName+".png");if(!res) return 0;} LOG(VB_JOBQUEUE, LOG_INFO, "Item Archived OK");return 1;}int NativeArchive::exportVideo(QDomElement &itemNode, const QString &saveDirectory){ QString dbVersion=gCoreContext-> GetSetting("DBSchemaVer", "")
MythCoreContext::TVPlaybackAboutToStart
void TVPlaybackAboutToStart(void)
uint
unsigned int uint
Definition: compat.h:140
MythCoreContext::TVPlaybackAborted
void TVPlaybackAborted(void)
MythCoreContext::GetNumSettingOnHost
bool GetNumSettingOnHost(const QString &key, const QString &host, bool defaultval=false)=delete
sched
Scheduler * sched
MythCoreContext
This class contains the runtime context for MythTV.
Definition: mythcorecontext.h:55
MythCoreContext::TVPlaybackStarted
void TVPlaybackStarted(void)
MythSocketCBs::connectionClosed
virtual void connectionClosed(MythSocket *)=0
MythCoreContext::TVPlaybackSought
void TVPlaybackSought(qint64 position)
MythCoreContext::emitTVPlaybackPaused
void emitTVPlaybackPaused(void)
Definition: mythcorecontext.h:255
MythCoreContext::TVPlaybackUnpaused
void TVPlaybackUnpaused(void)
ClearSettingsCache
static int ClearSettingsCache(const MythUtilCommandLineParser &)
Definition: backendutils.cpp:39
MythCoreContext::ResolveType
ResolveType
Definition: mythcorecontext.h:193
musicbrainzngs.caa.hostname
string hostname
Definition: caa.py:17
MythCoreContext::TVPlaybackStopped
void TVPlaybackStopped(void)
MythCoreContext::SetServerSocket
void SetServerSocket(MythSocket *serverSock)
kMythSocketLongTimeout
#define kMythSocketLongTimeout
Definition: mythsocket_cb.h:8
IsMasterBackend
static TransMythUICheckBoxSetting * IsMasterBackend()
Definition: backendsettings.cpp:12
MythCoreContextPrivate
Definition: mythcorecontext.cpp:60
MythCoreContext::SaveBoolSetting
void SaveBoolSetting(const QString &key, bool newValue)
Definition: mythcorecontext.h:148
mythsession.h
mythobservable.h
MythCoreContext::emitTVPlaybackAborted
void emitTVPlaybackAborted(void)
Definition: mythcorecontext.h:257
MythSessionManager
Definition: mythsession.h:99