Updated the web pages
[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         fail(Timeout);  // FAILS !!!
65 }
66
67 void UrpoProcess::start(const QString &command)
68 {
69     sendDebugMessage(QString("[Running command %1 ]").arg(command));
70
71     // Process finished handling
72     connect( &qprocess_, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)));
73
74     // Timer for timeout
75     // use UrpoConnection's default timeout settings
76     QTimer::singleShot( getTimeout(), this, SLOT(timeout()) );
77
78     // Start process
79     status_ = Running;
80
81     qprocess_.start(command);
82
83 }
84
85 void UrpoProcess::processFinished(int exitCode,QProcess::ExitStatus exitStatus)
86 {
87     if( exitCode)
88     {
89         sendDebugMessage( QString( tr("Process exited with code %1") ).arg(exitCode));
90         // Get data from ssh server
91         QByteArray newData = qprocess_.readAllStandardError();
92         sendDebugMessage( QString(newData) );
93
94         fail(ConnectionError);
95     }
96     else if( exitStatus == QProcess::CrashExit)
97         // Process failed
98         fail(ProcessError);
99     else
100     {
101         // Read output
102         QByteArray bytes = qprocess_.readAllStandardOutput();
103         QString string(bytes);
104         sendDebugMessage(string);
105         // Split to lines and store to output_
106         output_=string.split("\n");
107         status_=Successed;
108         emit finished(true);
109
110
111     }
112 }
113
114 QString UrpoProcess::getErrorString() const
115 {
116     switch( getError() )
117     {
118     case NoError:
119             return QString();
120     case ProcessError:
121             return tr("External program calling error");
122     case ConnectionError:
123             return tr("Connection error");
124     case AuthError:
125             return tr("Authentication error");
126     case Timeout:
127             return tr("Time out");
128     case Cancelled:
129             return tr("User cancelled");
130     }
131     return QString();
132 }
133
134