Password authentication (stage 1 - without xosso-terminal)
[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         PasswdRunning /*! Running after password sent */ = 11
92     };
93
94     enum UrpoError {
95         NoError         /*! No errors happends */       = 0,
96         ProcessError    /*! Error running process */    = 1,
97         ConnectionError /*! Error connecting host */    = 2,
98         AuthError       /*! Authentication failed */    = 3,
99         Timeout         /*! Timed out */                = 4,
100         Cancelled       /*! User cancelled operation */ = 5
101     };
102
103     /*! Get output of command
104       @return Output of command, list of QStrings
105       */
106     QStringList getOutput();
107
108     /*! Run command
109
110       Start process running command. When process finish
111       (successed/failed), finished() signal will be emitted
112
113       @param command Command to execute
114       */
115     void start(const QString& command);
116
117     /*! Error of process
118
119       If process failed, get error code
120       @return Error code
121       */
122     UrpoError getError() const { return error_; }
123     /*! Error of process (in string)
124
125       If process failed, return error string readable by user
126
127       @return Error string
128
129       */
130     QString getErrorString() const;
131
132     /*! Process status
133
134       @return Status (Ready, Running, Successed, Failed)
135       */
136     UrpoStatus getStatus() const { return status_; }
137
138
139     /*! Send debug message
140
141       @param message Message to debug monitor
142
143       If debug monitor has been connected, send message to debug monitor
144
145       */
146     void sendDebugMessage(QString message) { emit debugMessage(message); }
147
148     /*! Set timeout
149
150       Process time out, if running command last more than timeout
151
152
153       @param msecs Timeout in msecs
154       */
155     void setTimeout(int msecs) { timeout_=msecs; }
156
157     /*! Return timeout
158
159       @return Timeout in msecs */
160     int getTimeout() { return timeout_; }
161
162 signals:
163     /*! Process finished (successed of failed)
164
165       @param success True if process successed, false if failed
166
167       Emitted when process finished.
168       Output of process can be reader throught getOutput() */
169     void finished(bool success);
170     /** Send debug messages
171
172       UrpoConnection can set up a debug monitor, a QObject receiving
173       debugMsg signals. Debug messages contains information about
174       process output, errors etc.
175
176       @param message Message send to debug monitor */
177     void debugMessage(QString message);
178
179 public slots:
180     /*! Cancel process
181
182       Terminate process. Emit finished(false) signal and set Cancelled error */
183     void terminate();
184
185     /*! SSH client has finished */
186     void processFinished(int exitCode,QProcess::ExitStatus exitStatus);
187     /*! Timeout during connecting */
188     void timeout();
189
190
191 protected:
192
193
194 private:
195
196     static int const DEFAULTTIMEOUT = 15000;
197
198     void fail(UrpoError error);
199
200     QStringList output_;
201
202     QProcess qprocess_;
203
204     UrpoStatus status_;
205     UrpoError error_;
206     int timeout_;   /*! Timeout in msecs */
207     QString storedPassword_; /* Password to store */
208 };
209
210 #endif // URPOPROCESS_H