Added icon
[urpo] / src / printjob.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 "printjob.h"
24 #include "urpoconnection.h"
25 #include <QFileInfo>
26 #include <stdexcept> // Thowing execption for start()
27 #include <cstdlib>  // For random numbers generating temp file name
28
29 PrintJob::PrintJob(UrpoConnection* connection) :
30     UrpoJob(connection)
31 {
32     currentProcess_ = 0;
33 }
34
35 PrintJob::~PrintJob()
36 {
37     if( currentProcess_ )
38         delete currentProcess_;
39 }
40
41
42 void PrintJob::freeProcess()
43 {
44     if( currentProcess_)
45     {
46         // Delete current process when event loop
47         //
48         // If we are processing a signal from this process
49         // (process is finished), we can't delete process
50         // (actually, we can delete, but we get null pointer
51         // and segmentation fault !)
52         currentProcess_->deleteLater();
53
54         // disvalidate pointer
55         currentProcess_ = 0;
56     }
57 }
58
59 void PrintJob::startJob()
60 {
61     // See API documentation on header...
62     throw( new std::logic_error("PrintJob::starJob() NOT for use!") );
63 }
64
65
66 void PrintJob::printFile(const QString& path, const QString& printOptions)
67 {
68     // Get information about file
69     QFileInfo fileInfo(path);
70     if( fileInfo.exists() == false)
71     {
72         // File not found!
73         sendDebugMessage( tr("File %1 not exists.").arg(path));
74         fail( tr("File not found") );
75         return;
76     }
77
78     // new process for use
79     freeProcess();
80     currentProcess_= newProcess();
81
82
83     // Copy temperary file to temperary directory
84     pathToPrint_ = "/var/tmp/urpo-";
85
86     // Securing unique of temperary files, add random
87     // number between 0 and ffffff
88     pathToPrint_.append( QString::number( rand() % 0xffffff, 16));
89
90     // In server path, replace space with _
91     pathToPrint_.append( fileInfo.fileName().replace(QChar(' '),QChar('_')));
92
93     printOptions_ = printOptions;
94
95     // Copy command
96     QString command = "scp ";
97     command.append( getConnection()->getKeyOption());
98     command.append(fileInfo.absoluteFilePath().replace(QString(" "),QString("\\ ")) );
99     command.append(" ");
100     command.append( getConnection()->getHostString() );
101     command.append(":");
102     command.append(pathToPrint_);
103
104     // Next Copy Ready
105     connect( currentProcess_, SIGNAL(finished(bool)), this, SLOT(copyReady(bool)));
106
107     currentProcess_->start(command);
108 }
109
110 void PrintJob::copyReady(bool success)
111 {
112     freeProcess();
113
114     if( success == false)
115     {
116         fail( tr("Failed copying file"));
117         return;
118     }
119
120     // Running remote printing command
121     currentProcess_ = newProcess();
122     QString command = " ssh ";
123     command.append( getConnection()->getKeyOption()) ;
124     command.append(getConnection()->getHostString());
125     command.append(" lp ");
126     command.append(printOptions_);
127     command.append(" ");
128     command.append(pathToPrint_);
129
130     // Next print ready
131     connect( currentProcess_, SIGNAL(finished(bool)), this, SLOT(printReady(bool)));
132     currentProcess_->start(command);
133
134 }
135
136 void PrintJob::printReady(bool success)
137 {
138     // Print step is ready
139     freeProcess();
140
141     if( success == false )
142     {
143         fail( tr("Print failed"));
144         return;
145     }
146
147     // delete temperary file
148     currentProcess_ = newProcess();
149     QString command = " ssh ";
150     command.append( getConnection()->getKeyOption()) ;
151     command.append(getConnection()->getHostString());
152     command.append(" rm ");
153     command.append(pathToPrint_);
154
155     // Next delete ready
156     connect( currentProcess_, SIGNAL(finished(bool)),this,SLOT(deleteReady(bool)));
157     currentProcess_->start(command);
158 }
159
160 void PrintJob::deleteReady(bool success)
161 {
162     // All done!
163     freeProcess();
164     if( success == false )
165     {
166         fail(tr("Failed deleting file"));
167     }
168     else
169     {
170         // Successed !!!
171         finish( Successed );
172     }
173 }
174
175 void PrintJob::cancelJob()
176 {
177     // Cancelled by user.
178     if( currentProcess_ )
179     {
180         currentProcess_->terminate();
181         freeProcess();
182     }
183     finish( Cancelled );
184 }
185