Added icon
[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
27
28 int const UrpoProcess::DEFAULTTIMEOUT;
29
30 UrpoProcess::UrpoProcess(QObject* parent) :
31     QObject(parent)
32 {
33     status_ = Ready;
34     error_ = NoError;
35     setTimeout( DEFAULTTIMEOUT );
36 }
37
38 void UrpoProcess::terminate()
39 {
40     fail(Cancelled);
41 }
42
43 QStringList UrpoProcess::getOutput()
44 {
45     return output_;
46 }
47
48 void UrpoProcess::fail(UrpoError error)
49 {
50     status_=Failed;
51     error_=error;
52     qprocess_.terminate();
53     emit finished(false);
54     sendDebugMessage( getErrorString() );
55 }
56
57
58 void UrpoProcess::timeout()
59 {
60     // If process is still running, it means timeout!
61     if( status_ == Running)
62         fail(Timeout);
63 }
64
65 void UrpoProcess::start(const QString &command)
66 {
67     sendDebugMessage(QString("[Running command %1 ]").arg(command));
68
69     // Process finished handling
70     connect( &qprocess_, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)));
71
72     // Timer for timeout
73     // use UrpoConnection's default timeout settings
74     QTimer::singleShot( getTimeout(), this, SLOT(timeout()) );
75
76     // Start process
77     status_ = Running;
78
79     qprocess_.start(command);
80
81 }
82
83 void UrpoProcess::processFinished(int exitCode,QProcess::ExitStatus exitStatus)
84 {
85     if( exitCode)
86     {
87         sendDebugMessage( QString( tr("Process exited with code %1") ).arg(exitCode));
88         // Get data from ssh server
89         QByteArray newData = qprocess_.readAllStandardError();
90         sendDebugMessage( QString(newData) );
91
92         fail(ConnectionError);
93     }
94     else if( exitStatus == QProcess::CrashExit)
95         // Process failed
96         fail(ProcessError);
97     else
98     {
99         // Read output
100         QByteArray bytes = qprocess_.readAllStandardOutput();
101         QString string(bytes);
102         sendDebugMessage(string);
103         // Split to lines and store to output_
104         output_=string.split("\n");
105         status_=Successed;
106         emit finished(true);
107     }
108 }
109
110 QString UrpoProcess::getErrorString() const
111 {
112     switch( getError() )
113     {
114     case NoError:
115             return QString();
116     case ProcessError:
117             return tr("External program calling error");
118     case ConnectionError:
119             return tr("Connection error");
120     case AuthError:
121             return tr("Authentication error");
122     case Timeout:
123             return tr("Time out");
124     case Cancelled:
125             return tr("User cancelled");
126     }
127     return QString();
128 }
129
130