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 **************************************************************************/
24 #include "urpoconnection.h"
26 #include <stdexcept> // Thowing execption for start()
27 #include <cstdlib> // For random numbers generating temp file name
29 PrintJob::PrintJob(UrpoConnection* connection) :
38 delete currentProcess_;
42 void PrintJob::freeProcess()
46 // Delete current process when event loop
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();
54 // disvalidate pointer
59 void PrintJob::startJob()
61 // See API documentation on header...
62 throw( new std::logic_error("PrintJob::starJob() NOT for use!") );
66 void PrintJob::printFile(const QString& path, const QString& printOptions)
68 // Get information about file
69 QFileInfo fileInfo(path);
70 if( fileInfo.exists() == false)
73 sendDebugMessage( tr("File %1 not exists.").arg(path));
74 fail( tr("File not found") );
78 // new process for use
80 currentProcess_= newProcess();
83 // Copy temperary file to temperary directory
84 pathToPrint_ = "/var/tmp/urpo-";
86 // Securing unique of temperary files, add random
87 // number between 0 and ffffff
88 pathToPrint_.append( QString::number( rand() % 0xffffff, 16));
90 // In server path, replace space with _
91 pathToPrint_.append( fileInfo.fileName().replace(QChar(' '),QChar('_')));
93 printOptions_ = printOptions;
96 QString command = "scp ";
97 command.append( getConnection()->getKeyOption());
98 command.append(fileInfo.absoluteFilePath().replace(QString(" "),QString("\\ ")) );
100 command.append( getConnection()->getHostString() );
102 command.append(pathToPrint_);
105 connect( currentProcess_, SIGNAL(finished(bool)), this, SLOT(copyReady(bool)));
107 currentProcess_->start(command);
110 void PrintJob::copyReady(bool success)
114 if( success == false)
116 fail( tr("Failed copying file"));
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_);
128 command.append(pathToPrint_);
131 connect( currentProcess_, SIGNAL(finished(bool)), this, SLOT(printReady(bool)));
132 currentProcess_->start(command);
136 void PrintJob::printReady(bool success)
138 // Print step is ready
141 if( success == false )
143 fail( tr("Print failed"));
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_);
156 connect( currentProcess_, SIGNAL(finished(bool)),this,SLOT(deleteReady(bool)));
157 currentProcess_->start(command);
160 void PrintJob::deleteReady(bool success)
164 if( success == false )
166 fail(tr("Failed deleting file"));
175 void PrintJob::cancelJob()
177 // Cancelled by user.
178 if( currentProcess_ )
180 currentProcess_->terminate();