1
/**************************************************************************
2
* Copyright (C) 2005-2015 by Oleksandr Shneyder *
3
* o.shneyder@phoca-gmbh.de *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
16
***************************************************************************/
18
#include "x2goclientconfig.h"
19
#include "printprocess.h"
20
#include "x2gologdebug.h"
22
#include "x2gosettings.h"
24
#include "printdialog.h"
25
#if (!defined Q_OS_WIN) && (!defined Q_WS_HILDON)
26
#include "cupsprint.h"
28
#include "printwidget.h"
31
#include <QMessageBox>
32
#include <QFileDialog>
33
#include "x2gologdebug.h"
38
PrintProcess::PrintProcess ( QString pdf, QString title, QObject *parent ) :
41
x2goDebug<<"Starting print process."<<endl;
44
parentWidg= ( QWidget* ) parent;
45
if ( !loadSettings() )
47
QFile::remove ( pdfFile );
52
x2goDebug<<"Opening PDF file: "<<pdfFile<<" ("<<pdfTitle<<")"<<endl;
53
QTimer::singleShot ( 100, this, SLOT ( openPdf() ) );
57
x2goDebug<<"Printing PDF file: "<<pdfFile<<" ("<<pdfTitle<<")"<<endl;
58
QTimer::singleShot ( 100, this, SLOT ( print() ) );
63
PrintProcess::~PrintProcess()
65
x2goDebug<<"Closing print process."<<endl;
69
void PrintProcess::slot_processFinished ( int exitCode,
70
QProcess::ExitStatus exitStatus )
72
disconnect ( this,SIGNAL ( finished ( int , QProcess::ExitStatus ) ),
73
this,SLOT ( slot_processFinished (
74
int,QProcess::ExitStatus ) ) );
75
QFile::remove ( pdfFile );
76
if ( exitCode==0 && exitStatus==QProcess::NormalExit )
80
if ( !QProcess::startDetached (
81
printCmd+" \""+psFile+"\"" ) )
82
slot_error ( QProcess::FailedToStart );
86
QProcess* proc=new QProcess;
87
proc->setStandardInputFile ( psFile );
88
connect ( proc,SIGNAL (
89
error ( QProcess::ProcessError ) ),
92
QProcess::ProcessError ) ) );
93
proc->start ( printCmd );
97
slot_pdf2psError ( QProcess::Crashed );
101
bool PrintProcess::loadSettings()
103
X2goSettings st ( "printing" );
105
if ( st.setting()->value ( "showdialog",true ).toBool() )
108
if ( dlg.exec() ==QDialog::Rejected )
112
viewPdf=st.setting()->value ( "pdfview",false ).toBool();
113
customPrintCmd=st.setting()->value ( "print/startcmd",false ).toBool();
114
printCmd=st.setting()->value ( "print/command","lpr" ).toString();
115
printStdIn= st.setting()->value ( "print/stdin",false ).toBool();
116
printPs=st.setting()->value ( "print/ps",false ).toBool();
118
pdfOpen= st.setting()->value ( "view/open",true ).toBool();
121
pdfOpenCmd=st.setting()->value ( "view/command","xpdf" ).toString();
124
st.setting()->value ( "print/defaultprinter",
125
wapiGetDefaultPrinter() ).toString();
128
pdfOpenCmd="run-standalone.sh dbus-send --print-reply"
129
" --dest=com.nokia.osso_pdfviewer "
130
"/com/nokia/osso_pdfviewer "
131
"com.nokia.osso_pdfviewer.mime_open string:file://";
139
void PrintProcess::openPdf()
141
x2goDebug<<"opening/saving PDF..."<<endl;
146
QString cmd=pdfOpenCmd+" \""+pdfFile+"\"";
148
QString cmd=pdfOpenCmd+"\""+pdfFile+"\"";
150
x2goDebug<<"Using PDF viewer command: "<<cmd<<endl;
152
if ( ! QProcess::startDetached ( cmd ) )
153
slot_error ( QProcess::FailedToStart );
156
wapiShellExecute ( "open",
157
wapiShortFileName ( pdfFile ),
159
wapiShortFileName ( QDir::homePath() ) );
164
QString homePath=QDir::homePath();
166
homePath +="/"+pdfTitle+".pdf";
168
homePath +="\\x2goprint.pdf";
170
QString fileName = QFileDialog::getSaveFileName ( 0,
173
tr ( "PDF Document (*.pdf)" ) );
174
if ( fileName.length() >0 )
175
QFile::rename ( pdfFile,fileName );
180
void PrintProcess::print()
183
if ( !customPrintCmd )
187
prn.setCurrentPrinter ( prn.getDefaultUserPrinter() );
188
prn.print ( pdfFile, pdfTitle );
190
x2goDebug<<"printing to "<<winX2goPrinter<<endl;
191
wapiShellExecute ( "printto",
192
wapiShortFileName ( pdfFile ),
194
wapiShortFileName ( QDir::homePath() ) );
204
if ( !QProcess::startDetached (
206
" \""+pdfFile+"\"" ) )
207
slot_error ( QProcess::FailedToStart );
212
QProcess* proc=new QProcess;
213
proc->setStandardInputFile ( pdfFile );
214
connect ( proc,SIGNAL (
216
QProcess::ProcessError ) ),
219
QProcess::ProcessError ) ) );
220
proc->start ( printCmd );
227
psFile.replace ( "pdf","ps" );
228
args<<pdfFile<<psFile;
229
QProcess* proc=new QProcess;
230
connect ( proc,SIGNAL (
232
QProcess::ExitStatus ) ),
234
slot_processFinished (
236
QProcess::ExitStatus ) ) );
237
connect ( proc,SIGNAL (
238
error ( QProcess::ProcessError ) ),
241
QProcess::ProcessError ) ) );
243
proc->start ( "pdf2ps",args );
246
PrintWidget::gsInfo ( ver,pdf2ps );
248
wdir.replace ( "pdf2ps.bat","" );
249
proc->setWorkingDirectory ( wdir );
250
QStringList env=QProcess::systemEnvironment();
251
env.replaceInStrings ( QRegExp ( "^PATH=(.*)",
252
Qt::CaseInsensitive ),
254
wdir.replace ( "\\lib\\","\\bin\\" );
255
env.replaceInStrings ( QRegExp ( "^PATH=(.*)",
256
Qt::CaseInsensitive ),
258
proc->setEnvironment ( env );
259
proc->start ( pdf2ps,args );
265
void PrintProcess::slot_error ( QProcess::ProcessError )
267
QString message=tr ( "Failed to execute command:\n" );
269
message+=" "+pdfOpenCmd+ " " +pdfFile;
282
QMessageBox::critical ( 0l, tr ( "Printing error" ),
287
void PrintProcess::slot_pdf2psError ( QProcess::ProcessError )
289
QMessageBox::critical ( 0l, tr ( "Printing error" ),
290
tr ( "Failed to execute command:\n" ) +
291
"pdf2ps "+pdfFile+" "+psFile );