~x2go/x2go/x2goclient_master

« back to all changes in this revision

Viewing changes to printprocess.cpp

  • Committer: Mihai Moldovan
  • Date: 2015-03-04 20:15:47 UTC
  • Revision ID: git-v1:b7398771a7abd84ddcff407063edb95dd0a205d3
general: move *.cpp and *.h files to src/ and *.ts files to src/i18n/.

Show diffs side-by-side

added added

removed removed

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