1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cmddlg.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include <vcl/msgbox.hxx>
33 #include <vcl/svapp.hxx>
34 #ifndef _PAD_RTSETUP_HRC_
35 #include <rtsetup.hrc>
38 #include <padialog.hxx>
40 #include <prtsetup.hxx>
44 using namespace padmin
;
46 #define PRINTER_PERSISTENCE_GROUP "KnownPrinterCommands"
47 #define FAX_PERSISTENCE_GROUP "KnownFaxCommands"
48 #define PDF_PERSISTENCE_GROUP "KnowPdfCommands"
49 #define MAX_COMMANDS 50
51 void CommandStore::getSystemPrintCommands( ::std::list
< String
>& rCommands
)
53 static ::std::list
< OUString
> aSysCommands
;
54 static bool bOnce
= false;
58 PrinterInfoManager::get().getSystemPrintCommands( aSysCommands
);
61 ::std::list
< OUString
>::const_iterator it
;
62 for( it
= aSysCommands
.begin(); it
!= aSysCommands
.end(); ++it
)
63 rCommands
.push_back( *it
);
66 void CommandStore::getSystemPdfCommands( ::std::list
< String
>& rCommands
)
68 static bool bOnce
= false;
69 static ::std::list
< String
> aSysCommands
;
77 rtl_TextEncoding aEncoding
= osl_getThreadTextEncoding();
79 pPipe
= popen( "which gs 2>/dev/null", "r" );
82 fgets( pBuffer
, sizeof( pBuffer
), pPipe
);
83 int nLen
= strlen( pBuffer
);
84 if( pBuffer
[nLen
-1] == '\n' ) // strip newline
86 aCommand
= String( ByteString( pBuffer
), aEncoding
);
87 if( ( ( aCommand
.GetChar( 0 ) == '/' )
88 || ( aCommand
.GetChar( 0 ) == '.' && aCommand
.GetChar( 1 ) == '/' )
89 || ( aCommand
.GetChar( 0 ) == '.' && aCommand
.GetChar( 1 ) == '.' && aCommand
.GetChar( 2 ) == '/' ) )
91 && aCommand
.GetChar( nLen
-2 ) == 'g'
92 && aCommand
.GetChar( nLen
-1 ) == 's' )
94 aCommand
.AppendAscii( " -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=\"(OUTFILE)\" -" );
95 aSysCommands
.push_back( aCommand
);
100 pPipe
= popen( "which distill 2>/dev/null", "r" );
103 fgets( pBuffer
, sizeof( pBuffer
), pPipe
);
104 int nLen
= strlen( pBuffer
);
105 if( pBuffer
[nLen
-1] == '\n' ) // strip newline
107 aCommand
= String( ByteString( pBuffer
), aEncoding
);
108 if( ( ( aCommand
.GetChar( 0 ) == '/' )
109 || ( aCommand
.GetChar( 0 ) == '.' && aCommand
.GetChar( 1 ) == '/' )
110 || ( aCommand
.GetChar( 0 ) == '.' && aCommand
.GetChar( 1 ) == '.' && aCommand
.GetChar( 2 ) == '/' ) )
112 && aCommand
.Copy( nLen
- 8 ).EqualsAscii( "/distill" ) )
114 aCommand
.AppendAscii( " (TMP) ; mv `echo (TMP) | sed s/\\.ps\\$/.pdf/` \"(OUTFILE)\"" );
115 aSysCommands
.push_back( aCommand
);
120 ::std::list
< String
>::const_iterator it
;
121 for( it
= aSysCommands
.begin(); it
!= aSysCommands
.end(); ++it
)
122 rCommands
.push_back( *it
);
127 void CommandStore::getStoredCommands( const char* pGroup
, ::std::list
< String
>& rCommands
)
129 Config
& rConfig( getPadminRC() );
130 rConfig
.SetGroup( pGroup
);
131 int nKeys
= rConfig
.GetKeyCount();
132 ::std::list
< String
>::const_iterator it
;
135 String
aCommand( rConfig
.ReadKey( ByteString::CreateFromInt32( nKeys
), RTL_TEXTENCODING_UTF8
) );
138 for( it
= rCommands
.begin(); it
!= rCommands
.end() && *it
!= aCommand
; ++it
)
140 if( it
== rCommands
.end() )
141 rCommands
.push_back( aCommand
);
146 void CommandStore::setCommands(
148 const ::std::list
< String
>& rCommands
,
149 const ::std::list
< String
>& rSysCommands
152 Config
& rConfig( getPadminRC() );
153 rConfig
.DeleteGroup( pGroup
);
154 rConfig
.SetGroup( pGroup
);
155 ::std::list
< String
>::const_iterator it
, loop
;
156 ::std::list
< String
> aWriteList
;
159 for( it
= rCommands
.begin(); it
!= rCommands
.end(); ++it
)
163 for( loop
= rSysCommands
.begin(); loop
!= rSysCommands
.end() && *loop
!= *it
; ++loop
)
165 if( loop
== rSysCommands
.end() )
167 aWriteList
.push_back( *it
);
172 while( nWritten
> MAX_COMMANDS
)
174 aWriteList
.pop_front();
177 for( nWritten
= 0, it
= aWriteList
.begin(); it
!= aWriteList
.end(); ++it
, ++nWritten
)
178 rConfig
.WriteKey( ByteString::CreateFromInt32( nWritten
), ByteString( *it
, RTL_TEXTENCODING_UTF8
) );
182 void CommandStore::getPrintCommands( ::std::list
< String
>& rCommands
)
185 getSystemPrintCommands( rCommands
);
186 getStoredCommands( PRINTER_PERSISTENCE_GROUP
, rCommands
);
189 void CommandStore::getPdfCommands( ::std::list
< String
>& rCommands
)
192 getSystemPdfCommands( rCommands
);
193 getStoredCommands( PDF_PERSISTENCE_GROUP
, rCommands
);
196 void CommandStore::getFaxCommands( ::std::list
< String
>& rCommands
)
199 getStoredCommands( FAX_PERSISTENCE_GROUP
, rCommands
);
202 void CommandStore::setPrintCommands( const ::std::list
< String
>& rCommands
)
204 ::std::list
< String
> aSysCmds
;
205 getSystemPrintCommands( aSysCmds
);
206 setCommands( PRINTER_PERSISTENCE_GROUP
, rCommands
, aSysCmds
);
209 void CommandStore::setPdfCommands( const ::std::list
< String
>& rCommands
)
211 ::std::list
< String
> aSysCmds
;
212 getSystemPdfCommands( aSysCmds
);
213 setCommands( PDF_PERSISTENCE_GROUP
, rCommands
, aSysCmds
);
216 void CommandStore::setFaxCommands( const ::std::list
< String
>& rCommands
)
218 ::std::list
< String
> aSysCmds
;
219 setCommands( FAX_PERSISTENCE_GROUP
, rCommands
, aSysCmds
);
223 RTSCommandPage::RTSCommandPage( RTSDialog
* pParent
) :
224 TabPage( &pParent
->m_aTabControl
, PaResId( RID_RTS_COMMANDPAGE
) ),
225 m_pParent( pParent
),
226 m_aCommandsCB( this, PaResId( RID_RTS_CMD_CB_COMMANDS
) ),
227 m_aExternalCB( this, PaResId( RID_RTS_CMD_CB_EXTERNAL
) ),
228 m_aQuickFT( this, PaResId( RID_RTS_CMD_FT_QUICKCMD
) ),
229 m_aQuickCB( this, PaResId( RIT_RTS_CMD_CB_QUICKCMD
) ),
230 m_aCommandTitle( this, PaResId( RID_RTS_CMD_FL_INSTALL
) ),
231 m_aPrinterName( this, PaResId( RID_RTS_CMD_TXT_PRTNAME
) ),
232 m_aConnectedTo( this, PaResId( RID_RTS_CMD_TXT_CONNECT
) ),
233 m_aPrinterFL( this, PaResId( RID_RTS_CMD_FL_DEFAULT
) ),
234 m_aConfigureText( this, PaResId( RID_RTS_CMD_TXT_CONFIGURE
) ),
235 m_aConfigureBox( this, PaResId( RID_RTS_CMD_LB_CONFIGURE
) ),
236 m_aPdfDirectoryText( this, PaResId( RID_RTS_CMD_TXT_PDFDIR
) ),
237 m_aPdfDirectoryButton( this, PaResId( RID_RTS_CMD_BTN_PDFDIR
) ),
238 m_aPdfDirectoryEdit( this, PaResId( RID_RTS_CMD_EDT_PDFDIR
) ),
239 m_aFaxSwallowBox( this, PaResId( RID_RTS_CMD_BOX_SWALLOWFAXNO
) ),
240 m_aHelpButton( this, PaResId( RID_RTS_CMD_BTN_HELP
) ),
241 m_aRemovePB( this, PaResId( RID_RTS_CMD_BTN_REMOVE
) ),
242 m_aFaxHelp( PaResId( RID_RTS_CMD_STR_FAXHELP
) ),
243 m_aPrinterHelp( PaResId( RID_RTS_CMD_STR_PRINTERHELP
) ),
244 m_aPdfHelp( PaResId( RID_RTS_CMD_STR_PDFHELP
) )
246 // configuring as printer is only sensible in default print system
247 PrinterInfoManager
& rMgr( PrinterInfoManager::get() );
248 if( rMgr
.getType() == PrinterInfoManager::Default
|| rMgr
.isCUPSDisabled() )
249 m_nPrinterEntry
= m_aConfigureBox
.InsertEntry( String( PaResId( RID_RTS_CMD_STR_CONFIGURE_PRINTER
) ) );
251 m_nPrinterEntry
= ~0;
252 m_nFaxEntry
= m_aConfigureBox
.InsertEntry( String( PaResId( RID_RTS_CMD_STR_CONFIGURE_FAX
) ) );
253 m_nPdfEntry
= m_aConfigureBox
.InsertEntry( String( PaResId( RID_RTS_CMD_STR_CONFIGURE_PDF
) ) );
257 CommandStore::getPrintCommands( m_aPrinterCommands
);
258 CommandStore::getFaxCommands( m_aFaxCommands
);
259 CommandStore::getPdfCommands( m_aPdfCommands
);
261 m_aPrinterName
.SetText( m_pParent
->m_aPrinter
);
263 m_aCommandsCB
.SetDoubleClickHdl( LINK( this, RTSCommandPage
, DoubleClickHdl
) );
264 m_aCommandsCB
.SetSelectHdl( LINK( this, RTSCommandPage
, SelectHdl
) );
265 m_aCommandsCB
.SetModifyHdl( LINK( this, RTSCommandPage
, ModifyHdl
) );
266 m_aConfigureBox
.SetSelectHdl( LINK( this, RTSCommandPage
, SelectHdl
) );
267 m_aHelpButton
.SetClickHdl( LINK( this, RTSCommandPage
, ClickBtnHdl
) );
268 m_aRemovePB
.SetClickHdl( LINK( this, RTSCommandPage
, ClickBtnHdl
) );
269 m_aPdfDirectoryButton
.SetClickHdl( LINK( this, RTSCommandPage
, ClickBtnHdl
) );
270 m_aExternalCB
.SetToggleHdl( LINK( this, RTSCommandPage
, ClickBtnHdl
) );
272 m_aPdfDirectoryButton
.Show( FALSE
);
273 m_aPdfDirectoryEdit
.Show( FALSE
);
274 m_aPdfDirectoryText
.Show( FALSE
);
275 m_aFaxSwallowBox
.Show( FALSE
);
276 m_aCommandsCB
.SetText( m_pParent
->m_aJobData
.m_aCommand
);
277 m_aQuickCB
.SetText( m_pParent
->m_aJobData
.m_aQuickCommand
);
281 m_aConfigureBox
.SelectEntryPos( m_nPrinterEntry
);
282 sal_Int32 nIndex
= 0;
283 while( nIndex
!= -1 )
285 OUString
aToken( m_pParent
->m_aJobData
.m_aFeatures
.getToken( 0, ',', nIndex
) );
286 if( ! aToken
.compareToAscii( "fax", 3 ) )
289 m_aFaxSwallowBox
.Show( TRUE
);
291 m_aFaxSwallowBox
.Check( ! aToken
.getToken( 1, '=', nPos
).compareToAscii( "swallow", 7 ) ? TRUE
: FALSE
);
292 m_aConfigureBox
.SelectEntryPos( m_nFaxEntry
);
294 else if( ! aToken
.compareToAscii( "pdf=", 4 ) )
298 m_aPdfDirectoryEdit
.SetText( aToken
.getToken( 1, '=', nPos
) );
299 m_aPdfDirectoryEdit
.Show( TRUE
);
300 m_aPdfDirectoryButton
.Show( TRUE
);
301 m_aPdfDirectoryText
.Show( TRUE
);
302 m_aConfigureBox
.SelectEntryPos( m_nPdfEntry
);
304 else if( ! aToken
.compareToAscii( "external_dialog" ) )
306 m_aExternalCB
.Check();
307 m_bWasExternalDialog
= true;
311 m_aQuickCB
.Enable( m_aExternalCB
.IsChecked() );
313 String
aString( m_aConnectedTo
.GetText() );
314 aString
+= String( m_pParent
->m_aJobData
.m_aCommand
);
315 m_aConnectedTo
.SetText( aString
);
320 RTSCommandPage::~RTSCommandPage()
324 void RTSCommandPage::save()
326 String aCommand
,aQuickCommand
;
327 bool bHaveFax
= m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
? true : false;
328 bool bHavePdf
= m_aConfigureBox
.GetSelectEntryPos() == m_nPdfEntry
? true : false;
329 ::std::list
< String
>::iterator it
;
332 sal_Int32 nIndex
= 0;
334 bool bOldFaxSwallow
= false;
335 bool bFaxSwallow
= m_aFaxSwallowBox
.IsChecked() ? true : false;
336 bool bOldExternalDialog
= false, bExternalDialog
= m_aExternalCB
.IsChecked() ? true : false;
338 while( nIndex
!= -1 )
340 OUString
aToken( m_pParent
->m_aJobData
.m_aFeatures
.getToken( 0, ',', nIndex
) );
341 if( aToken
.compareToAscii( "fax", 3 ) &&
342 aToken
.compareToAscii( "pdf", 3 ) &&
343 aToken
.compareToAscii( "external_dialog" )
346 if( aToken
.getLength() )
348 if( aFeatures
.Len() )
350 aFeatures
+= String( aToken
);
353 else if( ! aToken
.compareToAscii( "pdf=", 4 ) )
356 aOldPdfPath
= aToken
.getToken( 1, '=', nPos
);
358 else if( ! aToken
.compareToAscii( "fax=", 4 ) )
361 bOldFaxSwallow
= aToken
.getToken( 1, '=', nPos
).compareToAscii( "swallow", 7 ) ? false : true;
363 else if( ! aToken
.compareToAscii( "external_dialog" ) )
365 bOldExternalDialog
= true;
368 ::std::list
< String
>* pList
= &m_aPrinterCommands
;
369 if( bExternalDialog
)
371 if( aFeatures
.Len() )
373 aFeatures
.AppendAscii( "external_dialog" );
377 if( aFeatures
.Len() )
379 aFeatures
.AppendAscii( "fax=" );
381 aFeatures
.AppendAscii( "swallow" );
382 pList
= &m_aFaxCommands
;
386 if( aFeatures
.Len() )
388 aFeatures
.AppendAscii( "pdf=" );
389 aFeatures
.Append( m_aPdfDirectoryEdit
.GetText() );
390 pList
= &m_aPdfCommands
;
392 aCommand
= m_aCommandsCB
.GetText();
393 aQuickCommand
= m_aQuickCB
.GetText();
394 for( it
= pList
->begin(); it
!= pList
->end() && *it
!= aCommand
; ++it
)
396 if( it
== pList
->end() )
397 pList
->push_back( aCommand
);
399 if( aCommand
!= String( m_pParent
->m_aJobData
.m_aCommand
) ||
400 aQuickCommand
!= String( m_pParent
->m_aJobData
.m_aQuickCommand
) ||
401 ( m_bWasFax
&& ! bHaveFax
) ||
402 ( ! m_bWasFax
&& bHaveFax
) ||
403 ( m_bWasPdf
&& ! bHavePdf
) ||
404 ( ! m_bWasPdf
&& bHavePdf
) ||
405 ( bHavePdf
&& aOldPdfPath
!= m_aPdfDirectoryEdit
.GetText() ) ||
406 ( bHaveFax
&& bFaxSwallow
!= bOldFaxSwallow
) ||
407 ( m_bWasExternalDialog
&& ! bExternalDialog
) ||
408 ( ! m_bWasExternalDialog
&& bExternalDialog
)
411 m_pParent
->m_aJobData
.m_aCommand
= aCommand
;
412 m_pParent
->m_aJobData
.m_aQuickCommand
= aQuickCommand
;
413 m_pParent
->m_aJobData
.m_aFeatures
= aFeatures
;
415 PrinterInfoManager::get().changePrinterInfo( m_pParent
->m_aPrinter
, m_pParent
->m_aJobData
);
417 CommandStore::setPrintCommands( m_aPrinterCommands
);
418 CommandStore::setFaxCommands( m_aFaxCommands
);
419 CommandStore::setPdfCommands( m_aPdfCommands
);
423 IMPL_LINK( RTSCommandPage
, SelectHdl
, Control
*, pBox
)
425 if( pBox
== &m_aConfigureBox
)
427 BOOL bEnable
= m_aConfigureBox
.GetSelectEntryPos() == m_nPdfEntry
? TRUE
: FALSE
;
428 m_aPdfDirectoryButton
.Show( bEnable
);
429 m_aPdfDirectoryEdit
.Show( bEnable
);
430 m_aPdfDirectoryText
.Show( bEnable
);
431 bEnable
= m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
? TRUE
: FALSE
;
432 m_aFaxSwallowBox
.Show( bEnable
);
435 else if( pBox
== &m_aCommandsCB
)
437 m_aRemovePB
.Enable( TRUE
);
443 IMPL_LINK( RTSCommandPage
, ClickBtnHdl
, Button
*, pButton
)
445 if( pButton
== & m_aPdfDirectoryButton
)
447 String
aPath( m_aPdfDirectoryEdit
.GetText() );
448 if( chooseDirectory( aPath
) )
449 m_aPdfDirectoryEdit
.SetText( aPath
);
451 else if( pButton
== &m_aRemovePB
)
453 String
aEntry( m_aCommandsCB
.GetText() );
454 ::std::list
< String
>* pList
;
455 if( m_aConfigureBox
.GetSelectEntryPos() == m_nPrinterEntry
)
456 pList
= &m_aPrinterCommands
;
457 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
)
458 pList
= &m_aFaxCommands
;
460 pList
= &m_aPdfCommands
;
462 pList
->remove( aEntry
);
463 m_aCommandsCB
.RemoveEntry( aEntry
);
464 m_aQuickCB
.RemoveEntry( aEntry
);
466 else if( pButton
== &m_aHelpButton
)
469 if( m_aConfigureBox
.GetSelectEntryPos() == m_nPrinterEntry
)
470 aHelpText
= m_aPrinterHelp
;
471 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
)
472 aHelpText
= m_aFaxHelp
;
473 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nPdfEntry
)
474 aHelpText
= m_aPdfHelp
;
476 InfoBox
aBox( this, aHelpText
);
479 else if( pButton
== &m_aExternalCB
)
481 m_aQuickCB
.Enable( m_aExternalCB
.IsChecked() );
486 IMPL_LINK( RTSCommandPage
, DoubleClickHdl
, ComboBox
*, pComboBox
)
488 if( pComboBox
== &m_aCommandsCB
)
493 IMPL_LINK( RTSCommandPage
, ModifyHdl
, Edit
*, pEdit
)
495 if( pEdit
== &m_aCommandsCB
)
496 m_aRemovePB
.Enable( m_aCommandsCB
.GetEntryPos( m_aCommandsCB
.GetText() ) != LISTBOX_ENTRY_NOTFOUND
);
501 void RTSCommandPage::UpdateCommands()
503 m_aCommandsCB
.Clear();
504 ::std::list
< String
>::iterator it
;
505 if( m_aConfigureBox
.GetSelectEntryPos() == m_nPrinterEntry
)
507 for( it
= m_aPrinterCommands
.begin(); it
!= m_aPrinterCommands
.end(); ++it
)
509 m_aCommandsCB
.InsertEntry( *it
);
510 m_aQuickCB
.InsertEntry( *it
);
513 m_aCommandsCB
.SetText( m_pParent
->m_aJobData
.m_aCommand
);
515 m_aCommandsCB
.SetText( String() );
517 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nFaxEntry
)
519 for( it
= m_aFaxCommands
.begin(); it
!= m_aFaxCommands
.end(); ++it
)
521 m_aCommandsCB
.InsertEntry( *it
);
522 m_aQuickCB
.InsertEntry( *it
);
525 m_aCommandsCB
.SetText( m_pParent
->m_aJobData
.m_aCommand
);
527 m_aCommandsCB
.SetText( String() );
529 else if( m_aConfigureBox
.GetSelectEntryPos() == m_nPdfEntry
)
531 for( it
= m_aPdfCommands
.begin(); it
!= m_aPdfCommands
.end(); ++it
)
533 m_aCommandsCB
.InsertEntry( *it
);
534 m_aQuickCB
.InsertEntry( *it
);
537 m_aCommandsCB
.SetText( m_pParent
->m_aJobData
.m_aCommand
);
539 m_aCommandsCB
.SetText( String() );
543 void RTSCommandPage::ConnectCommand()
545 String
aString( m_aConnectedTo
.GetText().GetToken( 0, ':' ) );
546 aString
.AppendAscii( ": " );
547 aString
+= m_aCommandsCB
.GetText();
549 m_aConnectedTo
.SetText( aString
);