00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "printjob.h"
00024 #include "urpoconnection.h"
00025 #include <QFileInfo>
00026 #include <stdexcept>
00027 #include <cstdlib>
00028
00029 PrintJob::PrintJob(UrpoConnection* connection) :
00030 UrpoJob(connection)
00031 {
00032 currentProcess_ = 0;
00033 }
00034
00035 PrintJob::~PrintJob()
00036 {
00037 if( currentProcess_ )
00038 delete currentProcess_;
00039 }
00040
00041
00042 void PrintJob::freeProcess()
00043 {
00044 if( currentProcess_)
00045 {
00046
00047
00048
00049
00050
00051
00052 currentProcess_->deleteLater();
00053
00054
00055 currentProcess_ = 0;
00056 }
00057 }
00058
00059 void PrintJob::startJob()
00060 {
00061
00062 throw( new std::logic_error("PrintJob::starJob() NOT for use!") );
00063 }
00064
00065
00066 void PrintJob::printFile(const QString& path, const QString& printOptions)
00067 {
00068
00069 QFileInfo fileInfo(path);
00070 if( fileInfo.exists() == false)
00071 {
00072
00073 sendDebugMessage( tr("File %1 not exists.").arg(path));
00074 fail( tr("File not found") );
00075 return;
00076 }
00077
00078
00079 freeProcess();
00080 currentProcess_= newProcess();
00081
00082
00083
00084 pathToPrint_ = "/var/tmp/urpo-";
00085
00086
00087
00088 pathToPrint_.append( QString::number( rand() % 0xffffff, 16));
00089
00090
00091 pathToPrint_.append( fileInfo.fileName().replace(QChar(' '),QChar('_')));
00092
00093 printOptions_ = printOptions;
00094
00095
00096 QString command = "scp ";
00097 command.append( getConnection()->getKeyOption());
00098 command.append(fileInfo.absoluteFilePath().replace(QString(" "),QString("\\ ")) );
00099 command.append(" ");
00100 command.append( getConnection()->getHostString() );
00101 command.append(":");
00102 command.append(pathToPrint_);
00103
00104
00105 connect( currentProcess_, SIGNAL(finished(bool)), this, SLOT(copyReady(bool)));
00106
00107 currentProcess_->start(command);
00108 }
00109
00110 void PrintJob::copyReady(bool success)
00111 {
00112 freeProcess();
00113
00114 if( success == false)
00115 {
00116 fail( tr("Failed copying file"));
00117 return;
00118 }
00119
00120
00121 currentProcess_ = newProcess();
00122 QString command = " ssh ";
00123 command.append( getConnection()->getKeyOption()) ;
00124 command.append(getConnection()->getHostString());
00125 command.append(" lp ");
00126 command.append(printOptions_);
00127 command.append(" ");
00128 command.append(pathToPrint_);
00129
00130
00131 connect( currentProcess_, SIGNAL(finished(bool)), this, SLOT(printReady(bool)));
00132 currentProcess_->start(command);
00133
00134 }
00135
00136 void PrintJob::printReady(bool success)
00137 {
00138
00139 freeProcess();
00140
00141 if( success == false )
00142 {
00143 fail( tr("Print failed"));
00144 return;
00145 }
00146
00147
00148 currentProcess_ = newProcess();
00149 QString command = " ssh ";
00150 command.append( getConnection()->getKeyOption()) ;
00151 command.append(getConnection()->getHostString());
00152 command.append(" rm ");
00153 command.append(pathToPrint_);
00154
00155
00156 connect( currentProcess_, SIGNAL(finished(bool)),this,SLOT(deleteReady(bool)));
00157 currentProcess_->start(command);
00158 }
00159
00160 void PrintJob::deleteReady(bool success)
00161 {
00162
00163 freeProcess();
00164 if( success == false )
00165 {
00166 fail(tr("Failed deleting file"));
00167 }
00168 else
00169 {
00170
00171 finish( Successed );
00172 }
00173 }
00174
00175 void PrintJob::cancelJob()
00176 {
00177
00178 if( currentProcess_ )
00179 {
00180 currentProcess_->terminate();
00181 freeProcess();
00182 }
00183 finish( Cancelled );
00184 }
00185