1 /**************************************************************************
2 *   Copyright (C) 2005-2020 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 <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17 
18 #include "printwidget.h"
19 #if (!defined Q_OS_WIN) && (!defined Q_WS_HILDON)
20 #include "cupsprintwidget.h"
21 #endif
22 #include "printercmddialog.h"
23 #include "x2gosettings.h"
24 #include "x2gologdebug.h"
25 #include <QDir>
26 #include <QButtonGroup>
27 #ifdef Q_OS_WIN
28 #include "wapi.h"
29 #endif
30 #include <QButtonGroup>
31 
PrintWidget(QWidget * parent)32 PrintWidget::PrintWidget ( QWidget* parent )
33 		: QWidget ( parent )
34 {
35 	ui.setupUi ( this );
36 	ui.rbPrint->setChecked ( true );
37 	ui.gbView->setVisible ( false );
38 	QVBoxLayout* lay= ( QVBoxLayout* ) ui.gbPrint->layout();
39 #if (!defined Q_OS_WIN) && (!defined Q_WS_HILDON)
40 	ui.cbWinPrinter->hide();
41 	ui.lWinPrinter->hide();
42 	ui.lWinInfo->hide();
43 	pwid=new CUPSPrintWidget ( ui.gbPrint );
44 	lay->insertWidget ( 0,pwid );
45 	connect ( ui.cbPrintCmd,SIGNAL ( toggled ( bool ) ),pwid,
46 	          SLOT ( setDisabled ( bool ) ) );
47 #else
48 #ifdef Q_OS_WIN
49 	connect ( ui.cbPrintCmd,SIGNAL ( toggled ( bool ) ),ui.cbWinPrinter,
50 	          SLOT ( setDisabled ( bool ) ) );
51 	connect ( ui.cbPrintCmd,SIGNAL ( toggled ( bool ) ),ui.lWinPrinter,
52 	          SLOT ( setDisabled ( bool ) ) );
53 	connect ( ui.cbPrintCmd,SIGNAL ( toggled ( bool ) ),ui.lWinInfo,
54 	          SLOT ( setDisabled ( bool ) ) );
55 	printers=wapiGetLocalPrinters();
56 	defaultPrinter=wapiGetDefaultPrinter();
57 	ui.cbWinPrinter->insertItems ( 0,printers );
58 	int index=printers.indexOf ( defaultPrinter );
59 	if ( index!=-1 )
60 		ui.cbWinPrinter->setCurrentIndex ( index );
61 	QLabel *rtfm=new QLabel (
62 	    tr (
63 	        "Please configure your client side printing settings.<br><br>"
64 	        "If you want to print the created file, you'll need "
65 	        "an external application. Typically you can use "
66 	        "<a href=\"http://pages.cs.wisc.edu/~ghost/doc/GPL/index.htm\">"
67 	        "ghostprint</a> and "
68 	        "<a href=\"http://pages.cs.wisc.edu/~ghost/gsview/\">"
69 	        "ghostview</a><br>You can find further information "
70 	        "<a href=\"http://www.x2go.org/index.php?id=49\">here</a>." ),
71 	    ui.gbPrint );
72 	rtfm->setWordWrap ( true );
73 	rtfm->setOpenExternalLinks ( true );
74 	lay->insertWidget ( 6,rtfm );
75 #endif
76 #endif
77 	connect ( ui.pbPrintCmd,SIGNAL ( clicked() ),this,
78 	          SLOT ( slot_editPrintCmd() ) );
79 
80 	QButtonGroup* bg=new QButtonGroup();
81 	bg->addButton ( ui.rbPrint );
82 	bg->addButton ( ui.rbView );
83 	loadSettings();
84 	connect ( ui.cbShowDialog,SIGNAL ( toggled ( bool ) ),
85 	          this, SIGNAL ( dialogShowEnabled ( bool ) ) );
86 #if (defined Q_OS_WIN)
87 	ui.label->hide();
88 	ui.leOpenCmd->hide();
89 #endif
90 #ifdef Q_WS_HILDON
91 	ui.rbView->setChecked ( true );
92 	ui.rbPrint->hide();
93 	ui.rbView->hide();
94 	ui.label->hide();
95 	ui.leOpenCmd->hide();
96 #endif
97 }
98 
99 
~PrintWidget()100 PrintWidget::~PrintWidget()
101 {
102 }
103 
104 
slot_editPrintCmd()105 void PrintWidget::slot_editPrintCmd()
106 {
107 	QString printCmd=ui.lePrintCmd->text();
108 	PrinterCmdDialog dlg ( &printCmd,&printStdIn,&printPs, this );
109 	dlg.exec();
110 	ui.lePrintCmd->setText ( printCmd );
111 }
112 
loadSettings()113 void PrintWidget::loadSettings()
114 {
115 	X2goSettings st ( "printing" );
116 	bool pdfView=st.setting()->value ( "pdfview",false ).toBool();
117 	QString prcmd=
118 	    st.setting()->value ( "print/command","" ).toString();
119 #ifdef Q_OS_WIN
120 	defaultPrinter=
121 	    st.setting()->value ( "print/defaultprinter",defaultPrinter ).toString();
122 
123 	int index=printers.indexOf ( defaultPrinter );
124 	if ( index!=-1 )
125 		ui.cbWinPrinter->setCurrentIndex ( index );
126 
127 	QString ver,gspath,gsvpath;
128 	bool isGsInstalled=gsInfo ( ver,gspath );
129 	bool isGsViewInstalled=gsViewInfo ( ver,gsvpath );
130 	if ( prcmd=="" && ! ( isGsInstalled && isGsViewInstalled ) )
131 	{
132 // 		x2goDebug<<"fallback to view"<<endl;
133 // 		pdfView=true;
134 	}
135 	else if ( prcmd=="" )
136 	{
137 		prcmd=gsvpath+" -query -color";
138 	}
139 #endif
140 	ui.cbShowDialog->setChecked (
141 	    st.setting()->value ( "showdialog",true ).toBool() );
142 
143 	if ( pdfView )
144 		ui.rbView->setChecked ( true );
145 	else
146 		ui.rbPrint->setChecked ( true );
147 
148 	ui.cbPrintCmd->setChecked ( st.setting()->value ( "print/startcmd",
149 	                                       false ).toBool() );
150 #ifndef Q_OS_WIN
151 	if ( prcmd=="" )
152 		prcmd="lpr";
153 #endif
154 	ui.lePrintCmd->setText ( prcmd );
155 
156 	printStdIn= st.setting()->value ( "print/stdin",false ).toBool();
157 	printPs=st.setting()->value ( "print/ps",false ).toBool();
158 
159 #ifdef Q_OS_WIN
160 	printPs=printPs&&isGsInstalled;
161 #endif
162 
163 	if ( ( st.setting()->value ( "view/open",true ).toBool() ) )
164 		ui.rbOpen->setChecked ( true );
165 	else
166 		ui.rbSave->setChecked ( true );
167 	ui.leOpenCmd->setText (
168 	    st.setting()->value ( "view/command","xpdf" ).toString() );
169 }
170 
saveSettings()171 void PrintWidget::saveSettings()
172 {
173 	X2goSettings st ( "printing" );
174 
175 	st.setting()->setValue ( "showdialog",
176 	              QVariant ( ui.cbShowDialog->isChecked () ) );
177 	st.setting()->setValue ( "pdfview",
178 	              QVariant ( ui.rbView->isChecked () ) );
179 	st.setting()->setValue ( "print/startcmd",
180 	              QVariant ( ui.cbPrintCmd->isChecked ( ) ) );
181 	st.setting()->setValue ( "print/command",
182 	              QVariant ( ui.lePrintCmd->text () ) );
183 	st.setting()->setValue ( "print/stdin",
184 	              QVariant ( printStdIn ) );
185 	st.setting()->setValue ( "print/ps",
186 	              QVariant ( printPs ) );
187 
188 	st.setting()->setValue ( "view/open",
189 	              QVariant ( ui.rbOpen->isChecked () ) );
190 	st.setting()->setValue ( "view/command",
191 	              QVariant ( ui.leOpenCmd->text () ) );
192 #ifdef Q_OS_WIN
193 	st.setting()->setValue ( "print/defaultprinter",
194 	              QVariant ( ui.cbWinPrinter->currentText()) );
195 #endif
196 #if (!defined Q_OS_WIN) && (!defined Q_WS_HILDON)
197 	pwid->savePrinter();
198 #endif
199 }
200 
201 
202 #ifdef Q_OS_WIN
gsInfo(QString & version,QString & pdf2ps)203 bool PrintWidget::gsInfo ( QString& version, QString& pdf2ps )
204 {
205 	QSettings st ( "HKEY_LOCAL_MACHINE\\"
206 	               "SOFTWARE\\GPL Ghostscript",
207 	               QSettings::NativeFormat );
208 	version="0.0";
209 	QStringList keys=st.allKeys();
210 	for ( int i=0;i<keys.size();++i )
211 	{
212 		if ( keys[i].indexOf ( "GS_LIB" ) !=-1 )
213 		{
214 			QString v=keys[i].split ( "/" ) [0];
215 			QString libs=st.value ( keys[i],"" ).toString();
216 			libs=libs.split ( ";" ) [0];
217 			if ( QFile::exists ( libs+"\\pdf2ps.bat" ) &&
218 			        v.toFloat() >version.toFloat() )
219 			{
220 				version=v;
221 				pdf2ps=libs+"\\pdf2ps.bat";
222 			}
223 
224 		}
225 	}
226 	if ( version.toFloat() >0.0 )
227 	{
228 		return true;
229 	}
230 	return false;
231 }
232 
gsViewInfo(QString & version,QString & gsprint)233 bool PrintWidget::gsViewInfo ( QString& version, QString& gsprint )
234 {
235 	QSettings st ( "HKEY_LOCAL_MACHINE\\"
236 	               "SOFTWARE\\Ghostgum\\GSview",
237 	               QSettings::NativeFormat );
238 	version="0.0";
239 	QStringList keys=st.allKeys();
240 	for ( int i=0;i<keys.size();++i )
241 	{
242 		QString v=keys[i];
243 		QString libs=st.value ( keys[i],"" ).toString();
244 		if ( QFile::exists ( libs+"\\gsview\\gsprint.exe" ) &&
245 		        v.toFloat() >version.toFloat() )
246 		{
247 			version=v;
248 			gsprint=libs+"\\gsview\\gsprint.exe";
249 		}
250 
251 
252 	}
253 	if ( version.toFloat() >0.0 )
254 	{
255 		return true;
256 	}
257 	return false;
258 }
259 #endif //Q_OS_WIN
260