Password authentication (stage 1 - without xosso-terminal)
[urpo] / src / urpoprocess.cpp
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 #include "urpoprocess.h"
24 #include "urpoconnection.h"
25 #include <QTimer>
26 #include <QInputDialog>
27 #include <QLineEdit>
28
29
30 int const UrpoProcess::DEFAULTTIMEOUT;
31
32 UrpoProcess::UrpoProcess(QObject* parent) :
33     QObject(parent)
34 {
35     status_ = Ready;
36     error_ = NoError;
37     setTimeout( DEFAULTTIMEOUT );
38 }
39
40 void UrpoProcess::terminate()
41 {
42     fail(Cancelled);
43 }
44
45 QStringList UrpoProcess::getOutput()
46 {
47     return output_;
48 }
49
50 void UrpoProcess::fail(UrpoError error)
51 {
52     status_=Failed;
53     error_=error;
54     qprocess_.terminate();
55     emit finished(false);
56     sendDebugMessage( getErrorString() );
57 }
58
59
60 void UrpoProcess::timeout()
61 {
62     // If process is still running, it means timeout!
63     if( status_ == Running)
64     {
65         // Read output
66         QByteArray bytes = qprocess_.readAllStandardOutput();
67         QString string(bytes);
68         sendDebugMessage(string);
69
70         if(string.contains("'s password:") || string.contains("passphrase:"))
71         {
72             // Try to ask password
73             if( storedPassword_.isEmpty())
74             {
75                 if( string.contains("'s password:") )
76                     storedPassword_ = QInputDialog::getText( 0, tr("Server ask password"), tr("Password:"), QLineEdit::PasswordEchoOnEdit );
77                 else
78                     storedPassword_ = QInputDialog::getText( 0, tr("Server ask passphrase"), tr("Passphrase:"), QLineEdit::PasswordEchoOnEdit );
79             }
80             qprocess_.write(storedPassword_.toAscii());
81             qprocess_.write("\n");
82
83             QTimer::singleShot( getTimeout(), this, SLOT(timeout()) );
84             status_ = PasswdRunning;
85         }
86         else
87             fail( Timeout );
88     }
89     else if( status_ == PasswdRunning )
90         fail(Timeout);  // FAILS !!!
91 }
92
93 void UrpoProcess::start(const QString &command)
94 {
95     sendDebugMessage(QString("[Running command %1 ]").arg(command));
96
97     // Process finished handling
98     connect( &qprocess_, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)));
99
100     // Timer for timeout
101     // use UrpoConnection's default timeout settings
102     QTimer::singleShot( getTimeout(), this, SLOT(timeout()) );
103
104     // Start process
105     status_ = Running;
106
107     qprocess_.start(command);
108
109 }
110
111 void UrpoProcess::processFinished(int exitCode,QProcess::ExitStatus exitStatus)
112 {
113     if( exitCode)
114     {
115         sendDebugMessage( QString( tr("Process exited with code %1") ).arg(exitCode));
116         // Get data from ssh server
117         QByteArray newData = qprocess_.readAllStandardError();
118         sendDebugMessage( QString(newData) );
119
120         fail(ConnectionError);
121     }
122     else if( exitStatus == QProcess::CrashExit)
123         // Process failed
124         fail(ProcessError);
125     else
126     {
127         // Read output
128         QByteArray bytes = qprocess_.readAllStandardOutput();
129         QString string(bytes);
130         sendDebugMessage(string);
131         // Split to lines and store to output_
132         output_=string.split("\n");
133         status_=Successed;
134         emit finished(true);
135
136
137     }
138 }
139
140 QString UrpoProcess::getErrorString() const
141 {
142     switch( getError() )
143     {
144     case NoError:
145             return QString();
146     case ProcessError:
147             return tr("External program calling error");
148     case ConnectionError:
149             return tr("Connection error");
150     case AuthError:
151             return tr("Authentication error");
152     case Timeout:
153             return tr("Time out");
154     case Cancelled:
155             return tr("User cancelled");
156     }
157     return QString();
158 }
159
160