1 /**************************************************************************
5 Unix Remote Printing Operation
6 Copyright (c) Arto Hyvättinen 2010
8 This file is part of URPO.
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.
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.
21 **************************************************************************/
23 #include "urpoprocess.h"
24 #include "urpoconnection.h"
26 #include <QInputDialog>
30 int const UrpoProcess::DEFAULTTIMEOUT;
32 UrpoProcess::UrpoProcess(QObject* parent) :
37 setTimeout( DEFAULTTIMEOUT );
40 void UrpoProcess::terminate()
45 QStringList UrpoProcess::getOutput()
50 void UrpoProcess::fail(UrpoError error)
54 qprocess_.terminate();
56 sendDebugMessage( getErrorString() );
60 void UrpoProcess::timeout()
62 // If process is still running, it means timeout!
63 if( status_ == Running)
66 QByteArray bytes = qprocess_.readAllStandardOutput();
67 QString string(bytes);
68 sendDebugMessage(string);
70 if(string.contains("'s password:") || string.contains("passphrase:"))
72 // Try to ask password
73 if( storedPassword_.isEmpty())
75 if( string.contains("'s password:") )
76 storedPassword_ = QInputDialog::getText( 0, tr("Server ask password"), tr("Password:"), QLineEdit::PasswordEchoOnEdit );
78 storedPassword_ = QInputDialog::getText( 0, tr("Server ask passphrase"), tr("Passphrase:"), QLineEdit::PasswordEchoOnEdit );
80 qprocess_.write(storedPassword_.toAscii());
81 qprocess_.write("\n");
83 QTimer::singleShot( getTimeout(), this, SLOT(timeout()) );
84 status_ = PasswdRunning;
89 else if( status_ == PasswdRunning )
90 fail(Timeout); // FAILS !!!
93 void UrpoProcess::start(const QString &command)
95 sendDebugMessage(QString("[Running command %1 ]").arg(command));
97 // Process finished handling
98 connect( &qprocess_, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)));
101 // use UrpoConnection's default timeout settings
102 QTimer::singleShot( getTimeout(), this, SLOT(timeout()) );
107 qprocess_.start(command);
111 void UrpoProcess::processFinished(int exitCode,QProcess::ExitStatus exitStatus)
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) );
120 fail(ConnectionError);
122 else if( exitStatus == QProcess::CrashExit)
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");
140 QString UrpoProcess::getErrorString() const
147 return tr("External program calling error");
148 case ConnectionError:
149 return tr("Connection error");
151 return tr("Authentication error");
153 return tr("Time out");
155 return tr("User cancelled");