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