Added program's help files to www pages and a ling to the english help file to the...
[urpo] / src / urpoprocess.h
1 /**************************************************************************
2
3     URPO
4
5     Unix Remote Printing Operation
6     Copyright (c) Arto Hyvättinen 2010
7
8     This file is part of URPO.
9
10     URPO is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation, either version 3 of the License, or
13     (at your option) any later version.
14
15     URPO is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20
21 **************************************************************************/
22
23 #ifndef URPOPROCESS_H
24 #define URPOPROCESS_H
25
26 #include <QObject>
27 #include <QStringList>
28 #include <QProcess>
29 class UrpoConnection;
30
31 /*! Process running ssh/scp command
32
33   @author Arto Hyvättinen
34   @version 0
35   @date 2010-06-11 - 2010-06-12
36
37   Run ssh/scp etc. command.
38
39   Read setting using UrpoConnect settings information object.
40   Support debug monitoring using debugMessage-signals, monitor defined in settings object
41
42   First, connect finished()-signal. Then, run command.
43   Command is run asynchronosly.
44
45   @code
46   ...
47   // Get list of available printers
48
49     QString command = "ssh ";
50     command.append(getConnection()->getKeyOption());
51     command.append(getConnection()->getHostString());
52     command.append(" env LANG=en lpstat -p");
53
54     process = new UrpoProcess( connect );
55
56   connect( process, SIGNAL(finished(bool)), this, SLOT( readOutput(bool) ) );
57   process.start(command);
58
59   ...
60   ::readOutput(bool success)
61   {
62     if( success )
63     {
64         foreach(QString line, process->getOutput() )
65         cout << line << "\n";
66     }
67     else
68         cout << "Error " << process->getError();
69   }
70   @endcode
71
72   @see UrpoConnect
73
74   */
75 class UrpoProcess : public QObject
76 {
77     Q_OBJECT
78 public:
79     /*!
80       @param connection Pointer to connection settings information
81       */
82     explicit UrpoProcess(QObject* parent = 0);
83
84
85     enum UrpoStatus {
86         Ready       /*! Ready for connecting */         = 0,
87         Running     /*! Command running */              = 1,
88         Successed   /*! Command successed */            = 2,
89         Failed      /*! Command failed */               = 3
90     };
91
92     enum UrpoError {
93         NoError         /*! No errors happends */       = 0,
94         ProcessError    /*! Error running process */    = 1,
95         ConnectionError /*! Error connecting host */    = 2,
96         AuthError       /*! Authentication failed */    = 3,
97         Timeout         /*! Timed out */                = 4,
98         Cancelled       /*! User cancelled operation */ = 5
99     };
100
101     /*! Get output of command
102       @return Output of command, list of QStrings
103       */
104     QStringList getOutput();
105
106     /*! Run command
107
108       Start process running command. When process finish
109       (successed/failed), finished() signal will be emitted
110
111       @param command Command to execute
112       */
113     void start(const QString& command);
114
115     /*! Error of process
116
117       If process failed, get error code
118       @return Error code
119       */
120     UrpoError getError() const { return error_; }
121     /*! Error of process (in string)
122
123       If process failed, return error string readable by user
124
125       @return Error string
126
127       */
128     QString getErrorString() const;
129
130     /*! Process status
131
132       @return Status (Ready, Running, Successed, Failed)
133       */
134     UrpoStatus getStatus() const { return status_; }
135
136
137     /*! Send debug message
138
139       @param message Message to debug monitor
140
141       If debug monitor has been connected, send message to debug monitor
142
143       */
144     void sendDebugMessage(QString message) { emit debugMessage(message); }
145
146     /*! Set timeout
147
148       Process time out, if running command last more than timeout
149
150
151       @param msecs Timeout in msecs
152       */
153     void setTimeout(int msecs) { timeout_=msecs; }
154
155     /*! Return timeout
156
157       @return Timeout in msecs */
158     int getTimeout() { return timeout_; }
159
160 signals:
161     /*! Process finished (successed of failed)
162
163       @param success True if process successed, false if failed
164
165       Emitted when process finished.
166       Output of process can be reader throught getOutput() */
167     void finished(bool success);
168     /** Send debug messages
169
170       UrpoConnection can set up a debug monitor, a QObject receiving
171       debugMsg signals. Debug messages contains information about
172       process output, errors etc.
173
174       @param message Message send to debug monitor */
175     void debugMessage(QString message);
176
177 public slots:
178     /*! Cancel process
179
180       Terminate process. Emit finished(false) signal and set Cancelled error */
181     void terminate();
182
183     /*! SSH client has finished */
184     void processFinished(int exitCode,QProcess::ExitStatus exitStatus);
185     /*! Timeout during connecting */
186     void timeout();
187
188
189 protected:
190
191
192 private:
193
194     static int const DEFAULTTIMEOUT = 15000;
195
196     void fail(UrpoError error);
197
198     QStringList output_;
199
200     QProcess qprocess_;
201
202     UrpoStatus status_;
203     UrpoError error_;
204     int timeout_;   /*! Timeout in msecs */
205 };
206
207 #endif // URPOPROCESS_H