1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <tools/config.hxx>
22 #include <vcl/msgbox.hxx>
23 #include <vcl/svapp.hxx>
24 #include <rtsetup.hrc>
26 #include <padialog.hxx>
28 #include <prtsetup.hxx>
31 using namespace padmin
;
33 using ::rtl::OUString
;
35 #define PRINTER_PERSISTENCE_GROUP "KnownPrinterCommands"
36 #define FAX_PERSISTENCE_GROUP "KnownFaxCommands"
37 #define PDF_PERSISTENCE_GROUP "KnowPdfCommands"
38 #define MAX_COMMANDS 50
40 void CommandStore::getSystemPrintCommands( ::std::list
< String
>& rCommands
)
42 static ::std::list
< OUString
> aSysCommands
;
43 static bool bOnce
= false;
47 PrinterInfoManager::get().getSystemPrintCommands( aSysCommands
);
50 ::std::list
< OUString
>::const_iterator it
;
51 for( it
= aSysCommands
.begin(); it
!= aSysCommands
.end(); ++it
)
52 rCommands
.push_back( *it
);
55 void CommandStore::getSystemPdfCommands( ::std::list
< String
>& rCommands
)
57 static bool bOnce
= false;
58 static ::std::list
< String
> aSysCommands
;
66 rtl_TextEncoding aEncoding
= osl_getThreadTextEncoding();
68 pPipe
= popen( "which gs 2>/dev/null", "r" );
71 if (fgets( pBuffer
, sizeof( pBuffer
), pPipe
) != NULL
)
73 int nLen
= strlen( pBuffer
);
74 if( pBuffer
[nLen
-1] == '\n' ) // strip newline
76 aCommand
= rtl::OUString(pBuffer
, nLen
, aEncoding
);
77 if( ( ( aCommand
.GetChar( 0 ) == '/' )
78 || ( aCommand
.GetChar( 0 ) == '.' && aCommand
.GetChar( 1 ) == '/' )
79 || ( aCommand
.GetChar( 0 ) == '.' && aCommand
.GetChar( 1 ) == '.' && aCommand
.GetChar( 2 ) == '/' ) )
81 && aCommand
.GetChar( nLen
-2 ) == 'g'
82 && aCommand
.GetChar( nLen
-1 ) == 's' )
84 aCommand
.AppendAscii( " -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=\"(OUTFILE)\" -" );
85 aSysCommands
.push_back( aCommand
);
91 pPipe
= popen( "which distill 2>/dev/null", "r" );
94 if (fgets( pBuffer
, sizeof( pBuffer
), pPipe
) != NULL
)
96 int nLen
= strlen( pBuffer
);
97 if( pBuffer
[nLen
-1] == '\n' ) // strip newline
99 aCommand
= rtl::OUString(pBuffer
, nLen
, aEncoding
);
100 if( ( ( aCommand
.GetChar( 0 ) == '/' )
101 || ( aCommand
.GetChar( 0 ) == '.' && aCommand
.GetChar( 1 ) == '/' )
102 || ( aCommand
.GetChar( 0 ) == '.' && aCommand
.GetChar( 1 ) == '.' && aCommand
.GetChar( 2 ) == '/' ) )
104 && aCommand
.Copy( nLen
- 8 ).EqualsAscii( "/distill" ) )
106 aCommand
.AppendAscii( " (TMP) ; mv `echo (TMP) | sed s/\\.ps\\$/.pdf/` \"(OUTFILE)\"" );
107 aSysCommands
.push_back( aCommand
);
113 ::std::list
< String
>::const_iterator it
;
114 for( it
= aSysCommands
.begin(); it
!= aSysCommands
.end(); ++it
)
115 rCommands
.push_back( *it
);
120 void CommandStore::getStoredCommands( const char* pGroup
, ::std::list
< String
>& rCommands
)
122 Config
& rConfig( getPadminRC() );
123 rConfig
.SetGroup( pGroup
);
124 sal_Int32 nKeys
= rConfig
.GetKeyCount();
125 ::std::list
< String
>::const_iterator it
;
128 String
aCommand( rConfig
.ReadKey(rtl::OString::valueOf(nKeys
), RTL_TEXTENCODING_UTF8
) );
131 for( it
= rCommands
.begin(); it
!= rCommands
.end() && *it
!= aCommand
; ++it
)
133 if( it
== rCommands
.end() )
134 rCommands
.push_back( aCommand
);
139 void CommandStore::setCommands(
141 const ::std::list
< String
>& rCommands
,
142 const ::std::list
< String
>& rSysCommands
145 Config
& rConfig( getPadminRC() );
146 rConfig
.DeleteGroup( pGroup
);
147 rConfig
.SetGroup( pGroup
);
148 ::std::list
< String
>::const_iterator it
, loop
;
149 ::std::list
< String
> aWriteList
;
151 sal_Int32 nWritten
= 0;
152 for( it
= rCommands
.begin(); it
!= rCommands
.end(); ++it
)
156 for( loop
= rSysCommands
.begin(); loop
!= rSysCommands
.end() && *loop
!= *it
; ++loop
)
158 if( loop
== rSysCommands
.end() )
160 aWriteList
.push_back( *it
);
165 while( nWritten
> MAX_COMMANDS
)
167 aWriteList
.pop_front();
170 for( nWritten
= 0, it
= aWriteList
.begin(); it
!= aWriteList
.end(); ++it
, ++nWritten
)
171 rConfig
.WriteKey( rtl::OString::valueOf(nWritten
), rtl::OUStringToOString(*it
, RTL_TEXTENCODING_UTF8
) );
175 void CommandStore::getPrintCommands( ::std::list
< String
>& rCommands
)
178 getSystemPrintCommands( rCommands
);
179 getStoredCommands( PRINTER_PERSISTENCE_GROUP
, rCommands
);
182 void CommandStore::getPdfCommands( ::std::list
< String
>& rCommands
)
185 getSystemPdfCommands( rCommands
);
186 getStoredCommands( PDF_PERSISTENCE_GROUP
, rCommands
);
189 void CommandStore::getFaxCommands( ::std::list
< String
>& rCommands
)
192 getStoredCommands( FAX_PERSISTENCE_GROUP
, rCommands
);
195 void CommandStore::setPrintCommands( const ::std::list
< String
>& rCommands
)
197 ::std::list
< String
> aSysCmds
;
198 getSystemPrintCommands( aSysCmds
);
199 setCommands( PRINTER_PERSISTENCE_GROUP
, rCommands
, aSysCmds
);
202 void CommandStore::setPdfCommands( const ::std::list
< String
>& rCommands
)
204 ::std::list
< String
> aSysCmds
;
205 getSystemPdfCommands( aSysCmds
);
206 setCommands( PDF_PERSISTENCE_GROUP
, rCommands
, aSysCmds
);
209 void CommandStore::setFaxCommands( const ::std::list
< String
>& rCommands
)
211 ::std::list
< String
> aSysCmds
;
212 setCommands( FAX_PERSISTENCE_GROUP
, rCommands
, aSysCmds
);
216 RTSCommandPage::RTSCommandPage( RTSDialog
* pParent
) :
217 TabPage( &pParent
->m_aTabControl
, PaResId( RID_RTS_COMMANDPAGE
) ),
218 m_pParent( pParent
),
219 m_aCommandsCB( this, PaResId( RID_RTS_CMD_CB_COMMANDS
) ),
220 m_aExternalCB( this, PaResId( RID_RTS_CMD_CB_EXTERNAL
) ),
221 m_aQuickFT( this, PaResId( RID_RTS_CMD_FT_QUICKCMD
) ),
222 m_aQuickCB( this, PaResId( RIT_RTS_CMD_CB_QUICKCMD
) ),
223 m_aCommandTitle( this, PaResId( RID_RTS_CMD_FL_INSTALL
) ),
224 m_aPrinterName( this, PaResId( RID_RTS_CMD_TXT_PRTNAME
) ),
225 m_aConnectedTo( this, PaResId( RID_RTS_CMD_TXT_CONNECT
) ),
226 m_aPrinterFL( this, PaResId( RID_RTS_CMD_FL_DEFAULT
) ),
227 m_aConfigureText( this, PaResId( RID_RTS_CMD_TXT_CONFIGURE
) ),
228 m_aConfigureBox( this, PaResId( RID_RTS_CMD_LB_CONFIGURE
) ),
229 m_aPdfDirectoryText( this, PaResId( RID_RTS_CMD_TXT_PDFDIR
) ),
230 m_aPdfDirectoryButton( this, PaResId( RID_RTS_CMD_BTN_PDFDIR
) ),
231 m_aPdfDirectoryEdit( this, PaResId( RID_RTS_CMD_EDT_PDFDIR
) ),
232 m_aFaxSwallowBox( this, PaResId( RID_RTS_CMD_BOX_SWALLOWFAXNO
) ),
233 m_aHelpButton( this, PaResId( RID_RTS_CMD_BTN_HELP
) ),
234 m_aRemovePB( this, PaResId( RID_RTS_CMD_BTN_REMOVE
) ),
235 m_aFaxHelp( PaResId( RID_RTS_CMD_STR_FAXHELP
) ),
236 m_aPrinterHelp( PaResId( RID_RTS_CMD_STR_PRINTERHELP
) ),
237 m_aPdfHelp( PaResId( RID_RTS_CMD_STR_PDFHELP
) )
239 // configuring as printer is only sensible in default print system
240 PrinterInfoManager
& rMgr( PrinterInfoManager::get() );
241 if( rMgr
.getType() == PrinterInfoManager::Default
|| rMgr
.isCUPSDisabled() )
242 m_nPrinterEntry
= m_aConfigureBox
.InsertEntry( String( PaResId( RID_RTS_CMD_STR_CONFIGURE_PRINTER
) ) );
244 m_nPrinterEntry
= ~0;
245 m_nFaxEntry
= m_aConfigureBox
.InsertEntry( String( PaResId( RID_RTS_CMD_STR_CONFIGURE_FAX
) ) );
246 m_nPdfEntry
= m_aConfigureBox
.InsertEntry( String( PaResId( RID_RTS_CMD_STR_CONFIGURE_PDF
) ) );
250 CommandStore::getPrintCommands( m_aPrinterCommands
);
251 CommandStore::getFaxCommands( m_aFaxCommands
);
252 CommandStore::getPdfCommands( m_aPdfCommands
);
254 m_aPrinterName
.SetText( m_pParent
->m_aPrinter
);
256 m_aCommandsCB
.SetDoubleClickHdl( LINK( this, RTSCommandPage
, DoubleClickHdl
) );
257 m_aCommandsCB
.SetSelectHdl( LINK( this, RTSCommandPage
, SelectHdl
) );
258 m_aCommandsCB
.SetModifyHdl( LINK( this, RTSCommandPage
, ModifyHdl
) );
259 m_aConfigureBox
.SetSelectHdl( LINK( this, RTSCommandPage
, SelectHdl
) );
260 m_aHelpButton
.SetClickHdl( LINK( this, RTSCommandPage
, ClickBtnHdl
) );
261 m_aRemovePB
.SetClickHdl( LINK( this, RTSCommandPage
, ClickBtnHdl
) );
262 m_aPdfDirectoryButton
.SetClickHdl( LINK( this, RTSCommandPage
, ClickBtnHdl
) );
263 m_aExternalCB
.SetToggleHdl( LINK( this, RTSCommandPage
, ClickBtnHdl
) );
265 m_aPdfDirectoryButton
.Show( sal_False
);
266 m_aPdfDirectoryEdit
.Show( sal_False
);
267 m_aPdfDirectoryText
.Show( sal_False
);
268 m_aFaxSwallowBox
.Show( sal_False
);
269 m_aCommandsCB
.SetText( m_pParent
->m_aJobData
.m_aCommand
);
270 m_aQuickCB
.SetText( m_pParent
->m_aJobData
.m_aQuickCommand
);
274 m_aConfigureBox
.SelectEntryPos( m_nPrinterEntry
);
275 sal_Int32 nIndex
= 0;
276 while( nIndex
!= -1 )
278 OUString
aToken( m_pParent
->m_aJobData
.m_aFeatures
.getToken( 0, ',', nIndex
) );
279 if( ! aToken
.compareToAscii( "fax", 3 ) )
282 m_aFaxSwallowBox
.Show( sal_True
);
284 m_aFaxSwallowBox
.Check( ! aToken
.getToken( 1, '=', nPos
).compareToAscii( "swallow", 7 ) ? sal_True
: sal_False
);
285 m_aConfigureBox
.SelectEntryPos( m_nFaxEntry
);
287 else if( ! aToken
.compareToAscii( "pdf=", 4 ) )
291 m_aPdfDirectoryEdit
.SetText( aToken
.getToken( 1, '=', nPos
) );
292 m_aPdfDirectoryEdit
.Show( sal_True
);
293 m_aPdfDirectoryButton
.Show( sal_True
);
294 m_aPdfDirectoryText
.Show( sal_True
);
295 m_aConfigureBox
.SelectEntryPos( m_nPdfEntry
);
297 else if( ! aToken
.compareToAscii( "external_dialog" ) )
299 m_aExternalCB
.Check();
300 m_bWasExternalDialog
= true;
304 m_aQuickCB
.Enable( m_aExternalCB
.IsChecked() );
306 String
aString( m_aConnectedTo
.GetText() );
307 aString
+= String( m_pParent
->m_aJobData
.m_aCommand
);
308 m_aConnectedTo
.SetText( aString
);
313 RTSCommandPage::~RTSCommandPage()
317 void RTSCommandPage::save()
319 String aCommand
,aQuickCommand
;
320 bool bHaveFax
= m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
? true : false;
321 bool bHavePdf
= m_aConfigureBox
.GetSelectEntryPos() == m_nPdfEntry
? true : false;
322 ::std::list
< String
>::iterator it
;
325 sal_Int32 nIndex
= 0;
327 bool bOldFaxSwallow
= false;
328 bool bFaxSwallow
= m_aFaxSwallowBox
.IsChecked() ? true : false;
329 bool bExternalDialog
= m_aExternalCB
.IsChecked() ? true : false;
331 while( nIndex
!= -1 )
333 OUString
aToken( m_pParent
->m_aJobData
.m_aFeatures
.getToken( 0, ',', nIndex
) );
334 if( aToken
.compareToAscii( "fax", 3 ) &&
335 aToken
.compareToAscii( "pdf", 3 ) &&
336 aToken
.compareToAscii( "external_dialog" )
339 if( !aToken
.isEmpty() )
341 if( aFeatures
.Len() )
343 aFeatures
+= String( aToken
);
346 else if( ! aToken
.compareToAscii( "pdf=", 4 ) )
349 aOldPdfPath
= aToken
.getToken( 1, '=', nPos
);
351 else if( ! aToken
.compareToAscii( "fax=", 4 ) )
354 bOldFaxSwallow
= aToken
.getToken( 1, '=', nPos
).compareToAscii( "swallow", 7 ) ? false : true;
357 ::std::list
< String
>* pList
= &m_aPrinterCommands
;
358 if( bExternalDialog
)
360 if( aFeatures
.Len() )
362 aFeatures
.AppendAscii( "external_dialog" );
366 if( aFeatures
.Len() )
368 aFeatures
.AppendAscii( "fax=" );
370 aFeatures
.AppendAscii( "swallow" );
371 pList
= &m_aFaxCommands
;
375 if( aFeatures
.Len() )
377 aFeatures
.AppendAscii( "pdf=" );
378 aFeatures
.Append( m_aPdfDirectoryEdit
.GetText() );
379 pList
= &m_aPdfCommands
;
381 aCommand
= m_aCommandsCB
.GetText();
382 aQuickCommand
= m_aQuickCB
.GetText();
383 for( it
= pList
->begin(); it
!= pList
->end() && *it
!= aCommand
; ++it
)
385 if( it
== pList
->end() )
386 pList
->push_back( aCommand
);
388 if( aCommand
!= String( m_pParent
->m_aJobData
.m_aCommand
) ||
389 aQuickCommand
!= String( m_pParent
->m_aJobData
.m_aQuickCommand
) ||
390 ( m_bWasFax
&& ! bHaveFax
) ||
391 ( ! m_bWasFax
&& bHaveFax
) ||
392 ( m_bWasPdf
&& ! bHavePdf
) ||
393 ( ! m_bWasPdf
&& bHavePdf
) ||
394 ( bHavePdf
&& aOldPdfPath
!= m_aPdfDirectoryEdit
.GetText() ) ||
395 ( bHaveFax
&& bFaxSwallow
!= bOldFaxSwallow
) ||
396 ( m_bWasExternalDialog
&& ! bExternalDialog
) ||
397 ( ! m_bWasExternalDialog
&& bExternalDialog
)
400 m_pParent
->m_aJobData
.m_aCommand
= aCommand
;
401 m_pParent
->m_aJobData
.m_aQuickCommand
= aQuickCommand
;
402 m_pParent
->m_aJobData
.m_aFeatures
= aFeatures
;
404 PrinterInfoManager::get().changePrinterInfo( m_pParent
->m_aPrinter
, m_pParent
->m_aJobData
);
406 CommandStore::setPrintCommands( m_aPrinterCommands
);
407 CommandStore::setFaxCommands( m_aFaxCommands
);
408 CommandStore::setPdfCommands( m_aPdfCommands
);
412 IMPL_LINK( RTSCommandPage
, SelectHdl
, Control
*, pBox
)
414 if( pBox
== &m_aConfigureBox
)
416 sal_Bool bEnable
= m_aConfigureBox
.GetSelectEntryPos() == m_nPdfEntry
? sal_True
: sal_False
;
417 m_aPdfDirectoryButton
.Show( bEnable
);
418 m_aPdfDirectoryEdit
.Show( bEnable
);
419 m_aPdfDirectoryText
.Show( bEnable
);
420 bEnable
= m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
? sal_True
: sal_False
;
421 m_aFaxSwallowBox
.Show( bEnable
);
424 else if( pBox
== &m_aCommandsCB
)
426 m_aRemovePB
.Enable( sal_True
);
432 IMPL_LINK( RTSCommandPage
, ClickBtnHdl
, Button
*, pButton
)
434 if( pButton
== & m_aPdfDirectoryButton
)
436 String
aPath( m_aPdfDirectoryEdit
.GetText() );
437 if( chooseDirectory( aPath
) )
438 m_aPdfDirectoryEdit
.SetText( aPath
);
440 else if( pButton
== &m_aRemovePB
)
442 String
aEntry( m_aCommandsCB
.GetText() );
443 ::std::list
< String
>* pList
;
444 if( m_aConfigureBox
.GetSelectEntryPos() == m_nPrinterEntry
)
445 pList
= &m_aPrinterCommands
;
446 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
)
447 pList
= &m_aFaxCommands
;
449 pList
= &m_aPdfCommands
;
451 pList
->remove( aEntry
);
452 m_aCommandsCB
.RemoveEntry( aEntry
);
453 m_aQuickCB
.RemoveEntry( aEntry
);
455 else if( pButton
== &m_aHelpButton
)
458 if( m_aConfigureBox
.GetSelectEntryPos() == m_nPrinterEntry
)
459 aHelpText
= m_aPrinterHelp
;
460 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
)
461 aHelpText
= m_aFaxHelp
;
462 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nPdfEntry
)
463 aHelpText
= m_aPdfHelp
;
465 InfoBox
aBox( this, aHelpText
);
468 else if( pButton
== &m_aExternalCB
)
470 m_aQuickCB
.Enable( m_aExternalCB
.IsChecked() );
475 IMPL_LINK( RTSCommandPage
, DoubleClickHdl
, ComboBox
*, pComboBox
)
477 if( pComboBox
== &m_aCommandsCB
)
482 IMPL_LINK( RTSCommandPage
, ModifyHdl
, Edit
*, pEdit
)
484 if( pEdit
== &m_aCommandsCB
)
485 m_aRemovePB
.Enable( m_aCommandsCB
.GetEntryPos( m_aCommandsCB
.GetText() ) != LISTBOX_ENTRY_NOTFOUND
);
490 void RTSCommandPage::UpdateCommands()
492 m_aCommandsCB
.Clear();
493 ::std::list
< String
>::iterator it
;
494 if( m_aConfigureBox
.GetSelectEntryPos() == m_nPrinterEntry
)
496 for( it
= m_aPrinterCommands
.begin(); it
!= m_aPrinterCommands
.end(); ++it
)
498 m_aCommandsCB
.InsertEntry( *it
);
499 m_aQuickCB
.InsertEntry( *it
);
502 m_aCommandsCB
.SetText( m_pParent
->m_aJobData
.m_aCommand
);
504 m_aCommandsCB
.SetText( String() );
506 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
)
508 for( it
= m_aFaxCommands
.begin(); it
!= m_aFaxCommands
.end(); ++it
)
510 m_aCommandsCB
.InsertEntry( *it
);
511 m_aQuickCB
.InsertEntry( *it
);
514 m_aCommandsCB
.SetText( m_pParent
->m_aJobData
.m_aCommand
);
516 m_aCommandsCB
.SetText( String() );
518 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nPdfEntry
)
520 for( it
= m_aPdfCommands
.begin(); it
!= m_aPdfCommands
.end(); ++it
)
522 m_aCommandsCB
.InsertEntry( *it
);
523 m_aQuickCB
.InsertEntry( *it
);
526 m_aCommandsCB
.SetText( m_pParent
->m_aJobData
.m_aCommand
);
528 m_aCommandsCB
.SetText( String() );
532 void RTSCommandPage::ConnectCommand()
534 String
aString( m_aConnectedTo
.GetText().GetToken( 0, ':' ) );
535 aString
.AppendAscii( ": " );
536 aString
+= m_aCommandsCB
.GetText();
538 m_aConnectedTo
.SetText( aString
);
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */