QtService  1.1.0
A platform independent library to easily create system services and use some of their features
terminal.h
1 #ifndef QTSERVICE_TERMINAL_H
2 #define QTSERVICE_TERMINAL_H
3 
4 #include <QtCore/qiodevice.h>
5 #include <QtCore/qscopedpointer.h>
6 
7 #include "QtService/qtservice_global.h"
8 #include "QtService/service.h"
9 
10 namespace QtService {
11 
12 class TerminalPrivate;
13 class TerminalAwaitablePrivate;
15 class Q_SERVICE_EXPORT Terminal : public QIODevice
16 {
17  Q_OBJECT
18 
20  Q_PROPERTY(QtService::Service::TerminalMode terminalMode READ terminalMode CONSTANT)
22  Q_PROPERTY(QStringList command READ command CONSTANT)
24  Q_PROPERTY(bool autoDelete READ isAutoDelete WRITE setAutoDelete NOTIFY autoDeleteChanged)
25 
26 public:
28  class Q_SERVICE_EXPORT Awaitable
29  {
30  public:
32  enum SpecialReads : qint64 {
33  ReadLine = 0,
34  ReadSingle = 1
35  };
36 
38  Awaitable(Terminal *terminal, qint64 readCnt = ReadSingle);
40  Awaitable(Awaitable &&other) noexcept;
42  Awaitable &operator=(Awaitable &&other) noexcept;
43  ~Awaitable();
44 
46  using type = QByteArray;
48  void prepare(std::function<void()> resume);
50  type result();
51 
52  private:
54  };
55 
57  explicit Terminal(TerminalPrivate *d_ptr, QObject *parent = nullptr);
58  ~Terminal() override;
59 
61  bool isSequential() const override;
63  void close() override;
65  bool atEnd() const override;
67  qint64 bytesAvailable() const override;
69  qint64 bytesToWrite() const override;
71  bool canReadLine() const override;
73  bool waitForReadyRead(int msecs) override;
75  bool waitForBytesWritten(int msecs) override;
76 
78  Service::TerminalMode terminalMode() const;
80  QStringList command() const;
82  bool isAutoDelete() const;
83 
84  //awaitables
86  Awaitable awaitChar();
88  Awaitable awaitChars(qint64 num);
90  Awaitable awaitLine();
91 
92 public Q_SLOTS:
94  void disconnectTerminal();
95 
97  void requestChar();
99  void requestChars(qint64 num);
101  void requestLine();
102 
104  void writeLine(const QByteArray &line, bool flush = true);
106  void flush();
107 
109  void setAutoDelete(bool autoDelete);
110 
111 Q_SIGNALS:
113  void terminalDisconnected();
115  void terminalError(int errorCode);
116 
118  void autoDeleteChanged(bool autoDelete);
119 
120 protected:
122  qint64 readData(char *data, qint64 maxlen) override;
124  qint64 readLineData(char *data, qint64 maxlen) override;
126  qint64 writeData(const char *data, qint64 len) override;
127 
128 private:
129  TerminalPrivate *d;
130 
131  bool open(OpenMode mode) override;
132 };
133 
134 }
135 
136 #endif // QTSERVICE_TERMINAL_H
TerminalMode
The modes a terminal can be in.
Definition: service.h:79
The primary namespace of the QtService library.
A helper class to be used with QtCoroutines to await io from a coroutine.
Definition: terminal.h:28
SpecialReads
Special read modes.
Definition: terminal.h:32
Represents a connection to a console terminal connected to the service.
Definition: terminal.h:15