MythTV  master
mythsystemlegacy.h
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * Class MythSystemLegacy
3  *
4  * Copyright (C) Gavin Hurlbut 2012
5  * Copyright (C) Issac Richards 2008
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef _MYTHSYSTEMLEGACY_H_
23 #define _MYTHSYSTEMLEGACY_H_
24 
25 #include "mythbaseexp.h"
26 
27 // Note: The FIXME were added 2013-05-18, as part of a multi-pass
28 // cleanup of MythSystemLegacy.
29 //
30 // MythSystemLegacy exists because the Qt QProcess has caused us numerous
31 // headaches, most importantly:
32 // http://code.mythtv.org/trac/ticket/7135
33 // https://bugreports.qt-project.org/browse/QTBUG-5990
34 // QProcess also demands that the thread in which it is used
35 // and created run a Qt event loop, which MythSystemLegacy does not
36 // require. A number of MythTV threads do not run an event loop
37 // so this requirement is a bit onerous.
38 //
39 // MythSystemLegacy has grown a bit fat around the middle as functionality
40 // has been added and as a core functionality class is due for a code
41 // review and some cleanup.
42 
43 // C headers
44 #include <cstdint>
45 #include <ctime>
46 
47 #ifdef __cplusplus
48 
49 #include "mythsystem.h" // included for MythSystemFlag and MythSignal
51 
52 #include <QStringList>
53 #include <QSemaphore>
54 #include <QBuffer>
55 #include <QObject>
56 #include <QString>
57 #include <QMap> // FIXME: This shouldn't be needed, Setting_t is not public
58 
59 // FIXME: _t is not how we name types in MythTV...
60 typedef QMap<QString, bool> Setting_t;
61 
63 
64 // FIXME: Does MythSystemLegacy really need to inherit from QObject?
65 // we can probably create a private class that inherits
66 // from QObject to avoid exposing lots of thread-unsafe
67 // methods & complicated life-time management.
69 class MBASE_PUBLIC MythSystemLegacy : public QObject
70 {
71  Q_OBJECT;
72 
73  public:
74  explicit MythSystemLegacy(QObject * = nullptr);
75  MythSystemLegacy(const QString &, uint, QObject * = nullptr);
76  MythSystemLegacy(const QString &, const QStringList &, uint,
77  QObject * = nullptr);
78  ~MythSystemLegacy(void);
79 
80  // FIXME: We should not allow a MythSystemLegacy to be reused for a new command.
81  void SetCommand(const QString &, uint);
82  // FIXME: We should not allow a MythSystemLegacy to be reused for a new command.
83  void SetCommand(const QString &, const QStringList &, uint);
84  // FIXME: This should only be in the constructor
85  void SetDirectory(const QString &);
86  // FIXME: This should only be in the constructor
87  bool SetNice(int nice);
88  // FIXME: This should only be in the constructor
89  bool SetIOPrio(int prio);
90 
91  // FIXME: Forks, should be called Start() for consistency with MThread.
92  // FIXME: Do we need this call at all?
93  void Run(time_t timeout = 0);
94  // FIXME: This should just return a bool telling us if we hit the timeout.
95  uint Wait(time_t timeout = 0);
96 
97  int Write(const QByteArray&);
98  QByteArray Read(int size);
99  QByteArray ReadErr(int size);
100  // FIXME: We should not return a reference here
101  QByteArray& ReadAll();
102  // FIXME: We should not return a reference here
103  QByteArray& ReadAllErr();
104 
105  // FIXME: Can Term be wrapped into Signal?
106  void Term(bool force = false);
107  void Signal(MythSignal);
108 
109  // FIXME: Should be IsBackground() + documented
110  bool isBackground(void) { return GetSetting("RunInBackground"); }
111  // FIXME: Should be IsAutoCleanupProcess() + documented
112  bool doAutoCleanup(void) { return GetSetting("AutoCleanup"); }
113  // FIXME: No idea what this is querying but should be StudlyCase
114  // and should be documented
115  bool onlyLowExitVal(void) { return GetSetting("OnlyLowExitVal"); }
116  // FIXME: Why is this public?
117  void HandlePreRun(void);
118  // FIXME: Why is this public?
119  void HandlePostRun(void);
120 
121  // FIXME: Rename "GetExitStatus" and document that this does not
122  // block until an exit status exists.
123  // FIXME: Document what this actually returns.
124  uint GetStatus(void) { return m_status; }
125  // FIXME Make private.
126  void SetStatus(uint status) { m_status = status; }
127 
128  // FIXME: We should not return a reference here, add a Set if necessary
129  // Do we even need this function? Should it then be better named?
130  QString& GetLogCmd(void) { return m_logcmd; }
131  // FIXME: We should not return a reference here
132  QString& GetDirectory(void) { return m_directory; }
133 
134  // FIXME: Eliminate or make private, we don't allow any settings
135  // that can not be enumerated.
136  bool GetSetting(const char *setting)
137  { return m_settings.value(QString(setting)); }
138 
139  // FIXME: We should not return a reference here
140  QString& GetCommand(void) { return m_command; }
141  // FIXME: Eliminate. We should not allow setting the command
142  // after construcion.
143  void SetCommand(QString &cmd) { m_command = cmd; }
144 
145  // FIXME: We should not return a reference here
146  // FIXME: Rename "GetArguments"
147  QStringList &GetArgs(void) { return m_args; }
148  // FIXME: Eliminate. We should not allow setting the arguements
149  // after construcion.
150  void SetArgs(QStringList &args) { m_args = args; }
151 
152  int GetNice(void) { return m_nice; }
153  int GetIOPrio(void) { return m_ioprio; }
154 
155  // FIXME: We should not return a pointer to a QBuffer
156  QBuffer *GetBuffer(int index) { return &m_stdbuff[index]; }
157 
158  // FIXME: We should not make locking public
159  void Unlock(void) { m_semReady.release(1); }
160 
162 
163  // FIXME: This should be an independent function living elsewhere
164  static QString ShellEscape(const QString &in);
165 
166  // FIXME: Do we really need to expose Qt signals?
167  // If so why are they lower case?
168  signals:
169  void started(void);
170  void finished(void);
171  void error(uint status);
172  void readDataReady(int fd);
173 
174  private:
175  Q_DISABLE_COPY(MythSystemLegacy)
176  void initializePrivate(void);
177  MythSystemLegacyPrivate *d {nullptr}; // FIXME we generally call this m_priv in MythTV
178 
179  protected:
180  void ProcessFlags(uint flags);
181 
182  // FIXME if we already have a private helper, why all this?
184  QSemaphore m_semReady;
185 
186  QString m_command;
187  QString m_logcmd;
188  QStringList m_args;
189  QString m_directory;
190 
191  int m_nice {0};
192  int m_ioprio {0};
193 
195  QBuffer m_stdbuff[3];
196 };
197 
198 MBASE_PUBLIC uint myth_system(const QString &command,
199  uint flags = kMSNone,
200  uint timeout = 0);
201 #endif // __cplusplus
202 
203 #ifdef __cplusplus
204 extern "C"
205 #endif // __cplusplus
206 MBASE_PUBLIC uint myth_system_c(char *command, uint flags, uint timeout);
207 
208 #endif // _MYTHSYSTEMLEGACY_H_
209 /*
210  * vim:ts=4:sw=4:ai:et:si:sts=4
211  */
bool onlyLowExitVal(void)
MythSystemMask MythSystemFlag
bool isBackground(void)
QBuffer * GetBuffer(int index)
static void error(const char *str,...)
Definition: vbi.c:42
void SetArgs(QStringList &args)
QStringList & GetArgs(void)
unsigned int uint
Definition: compat.h:140
QString & GetLogCmd(void)
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MythSystemMask
Definition: mythsystem.h:32
void SetStatus(uint status)
static const uint16_t * d
void readDataReady(int fd)
#define nice(x)
Definition: compat.h:195
void MBASE_PUBLIC ShutdownMythSystemLegacy(void)
bool doAutoCleanup(void)
MBASE_PUBLIC uint myth_system(const QString &command, uint flags=kMSNone, uint timeout=0)
QString & GetDirectory(void)
MythSignal
Definition: mythsystem.h:54
bool GetSetting(const char *setting)
void SetCommand(QString &cmd)
QMap< QString, bool > Setting_t
MBASE_PUBLIC uint myth_system_c(char *command, uint flags, uint timeout)
QString & GetCommand(void)