1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
//
// C++ Implementation: printprocess
//
// Description:
//
//
// Author: Oleksandr Shneyder <oleksandr.shneyder@obviously-nice.de>, (C) 2009
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "x2goclientconfig.h"
#include "printprocess.h"
#include "x2gologdebug.h"
#include <QFile>
#include "x2gosettings.h"
#include <QDir>
#include "printdialog.h"
#if (!defined Q_OS_WIN) && (!defined Q_WS_HILDON)
#include "cupsprint.h"
#else
#include "printwidget.h"
#endif
#include <QProcess>
#include <QMessageBox>
#include <QFileDialog>
#include "x2gologdebug.h"
#ifdef Q_OS_WIN
#include "wapi.h"
#endif
#include <QTimer>
PrintProcess::PrintProcess ( QString pdf, QString title, QObject *parent ) :
QObject ( parent )
{
pdfFile=pdf;
pdfTitle=title;
parentWidg= ( QWidget* ) parent;
if ( !loadSettings() )
{
QFile::remove ( pdfFile );
return;
}
if ( viewPdf )
QTimer::singleShot ( 100, this, SLOT ( openPdf() ) );
else
QTimer::singleShot ( 100, this, SLOT ( print() ) );
}
PrintProcess::~PrintProcess()
{
}
void PrintProcess::slot_processFinished ( int exitCode,
QProcess::ExitStatus exitStatus )
{
disconnect ( this,SIGNAL ( finished ( int , QProcess::ExitStatus ) ),
this,SLOT ( slot_processFinished (
int,QProcess::ExitStatus ) ) );
QFile::remove ( pdfFile );
if ( exitCode==0 && exitStatus==QProcess::NormalExit )
{
if ( !printStdIn )
{
if ( !QProcess::startDetached (
printCmd+" \""+psFile+"\"" ) )
slot_error ( QProcess::FailedToStart );
}
else
{
QProcess* proc=new QProcess;
proc->setStandardInputFile ( psFile );
connect ( proc,SIGNAL (
error ( QProcess::ProcessError ) ),
this,SLOT (
slot_error (
QProcess::ProcessError ) ) );
proc->start ( printCmd );
}
}
else
slot_pdf2psError ( QProcess::Crashed );
}
bool PrintProcess::loadSettings()
{
X2goSettings st ( "printing" );
if ( st.setting()->value ( "showdialog",true ).toBool() )
{
PrintDialog dlg;
if ( dlg.exec() ==QDialog::Rejected )
return false;
}
viewPdf=st.setting()->value ( "pdfview",false ).toBool();
customPrintCmd=st.setting()->value ( "print/startcmd",false ).toBool();
printCmd=st.setting()->value ( "print/command","lpr" ).toString();
printStdIn= st.setting()->value ( "print/stdin",false ).toBool();
printPs=st.setting()->value ( "print/ps",false ).toBool();
pdfOpen= st.setting()->value ( "view/open",true ).toBool();
#ifndef Q_OS_WIN
pdfOpenCmd=st.setting()->value ( "view/command","xpdf" ).toString();
#else
winX2goPrinter=
st.setting()->value ( "print/defaultprinter",
wapiGetDefaultPrinter() ).toString();
#endif
#ifdef Q_WS_HILDON
pdfOpenCmd="run-standalone.sh dbus-send --print-reply"
" --dest=com.nokia.osso_pdfviewer "
"/com/nokia/osso_pdfviewer "
"com.nokia.osso_pdfviewer.mime_open string:file://";
viewPdf=true;
#endif
return true;
}
void PrintProcess::openPdf()
{
if ( pdfOpen )
{
#ifndef Q_OS_WIN
#ifndef Q_WS_HILDON
QString cmd=pdfOpenCmd+" \""+pdfFile+"\"";
#else
QString cmd=pdfOpenCmd+"\""+pdfFile+"\"";
#endif
x2goDebug<<cmd;
if ( ! QProcess::startDetached ( cmd ) )
slot_error ( QProcess::FailedToStart );
#else
wapiShellExecute ( "open",
wapiShortFileName ( pdfFile ),
QString::null,
wapiShortFileName ( QDir::homePath() ) );
#endif
}
else
{
QString homePath=QDir::homePath();
#ifndef Q_OS_WIN
homePath +="/"+pdfTitle+".pdf";
#else
homePath +="\\x2goprint.pdf";
#endif
QString fileName = QFileDialog::getSaveFileName ( 0,
tr ( "Save File" ),
homePath,
tr ( "PDF Document (*.pdf)" ) );
if ( fileName.length() >0 )
QFile::rename ( pdfFile,fileName );
}
}
void PrintProcess::print()
{
#ifndef Q_WS_HILDON
if ( !customPrintCmd )
{
#ifndef Q_OS_WIN
CUPSPrint prn;
prn.setCurrentPrinter ( prn.getDefaultUserPrinter() );
prn.print ( pdfFile, pdfTitle );
#else
x2goDebug<<"printing to "<<winX2goPrinter<<endl;
wapiShellExecute ( "printto",
wapiShortFileName ( pdfFile ),
winX2goPrinter,
wapiShortFileName ( QDir::homePath() ) );
#endif
}
else
#endif //Q_WS_HILDON
{
if ( !printPs )
{
if ( !printStdIn )
{
if ( !QProcess::startDetached (
printCmd+
" \""+pdfFile+"\"" ) )
slot_error ( QProcess::FailedToStart );
}
else
{
QProcess* proc=new QProcess;
proc->setStandardInputFile ( pdfFile );
connect ( proc,SIGNAL (
error (
QProcess::ProcessError ) ),
this,SLOT (
slot_error (
QProcess::ProcessError ) ) );
proc->start ( printCmd );
}
}
else
{
QStringList args;
psFile=pdfFile;
psFile.replace ( "pdf","ps" );
args<<pdfFile<<psFile;
QProcess* proc=new QProcess;
connect ( proc,SIGNAL (
finished ( int ,
QProcess::ExitStatus ) ),
this,SLOT (
slot_processFinished (
int,
QProcess::ExitStatus ) ) );
connect ( proc,SIGNAL (
error ( QProcess::ProcessError ) ),
this,SLOT (
slot_pdf2psError (
QProcess::ProcessError ) ) );
#ifndef Q_OS_WIN
proc->start ( "pdf2ps",args );
#else
QString pdf2ps,ver;
PrintWidget::gsInfo ( ver,pdf2ps );
QString wdir=pdf2ps;
wdir.replace ( "pdf2ps.bat","" );
proc->setWorkingDirectory ( wdir );
QStringList env=QProcess::systemEnvironment();
env.replaceInStrings ( QRegExp ( "^PATH=(.*)",
Qt::CaseInsensitive ),
"PATH=\\1;"+wdir );
wdir.replace ( "\\lib\\","\\bin\\" );
env.replaceInStrings ( QRegExp ( "^PATH=(.*)",
Qt::CaseInsensitive ),
"PATH=\\1;"+wdir );
proc->setEnvironment ( env );
proc->start ( pdf2ps,args );
#endif
}
}
}
void PrintProcess::slot_error ( QProcess::ProcessError )
{
QString message=tr ( "Failed to execute command:\n" );
if ( viewPdf )
message+=" "+pdfOpenCmd+ " " +pdfFile;
else
{
message+=printCmd;
if ( !printStdIn )
{
message+=" ";
if ( printPs )
message+=psFile;
else
message+=pdfFile;
}
}
QMessageBox::critical ( 0l, tr ( "Printing error" ),
message );
}
void PrintProcess::slot_pdf2psError ( QProcess::ProcessError )
{
QMessageBox::critical ( 0l, tr ( "Printing error" ),
tr ( "Failed to execute command:\n" ) +
"pdf2ps "+pdfFile+" "+psFile );
}
|