Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / padmin / source / cmddlg.cxx
blobf5496fbfa2547509364d4294a2fea5b1c46e9854
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <stdio.h>
21 #include <tools/config.hxx>
22 #include <vcl/msgbox.hxx>
23 #include <vcl/svapp.hxx>
24 #include <rtsetup.hrc>
25 #include <cmddlg.hxx>
26 #include <padialog.hxx>
27 #include <helper.hxx>
28 #include <prtsetup.hxx>
30 using namespace psp;
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;
44 if( ! bOnce )
46 bOnce = true;
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;
60 if( ! bOnce )
62 bOnce = true;
63 char pBuffer[1024];
64 FILE* pPipe;
65 String aCommand;
66 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
68 pPipe = popen( "which gs 2>/dev/null", "r" );
69 if( pPipe )
71 if (fgets( pBuffer, sizeof( pBuffer ), pPipe ) != NULL)
73 int nLen = strlen( pBuffer );
74 if( pBuffer[nLen-1] == '\n' ) // strip newline
75 pBuffer[--nLen] = 0;
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 ) == '/' ) )
80 && nLen > 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 );
88 pclose( pPipe );
91 pPipe = popen( "which distill 2>/dev/null", "r" );
92 if( pPipe )
94 if (fgets( pBuffer, sizeof( pBuffer ), pPipe ) != NULL)
96 int nLen = strlen( pBuffer );
97 if( pBuffer[nLen-1] == '\n' ) // strip newline
98 pBuffer[--nLen] = 0;
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 ) == '/' ) )
103 && nLen > 7
104 && aCommand.Copy( nLen - 8 ).EqualsAscii( "/distill" ) )
106 aCommand.AppendAscii( " (TMP) ; mv `echo (TMP) | sed s/\\.ps\\$/.pdf/` \"(OUTFILE)\"" );
107 aSysCommands.push_back( aCommand );
110 pclose( pPipe );
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;
126 while( nKeys-- )
128 String aCommand( rConfig.ReadKey(rtl::OString::valueOf(nKeys), RTL_TEXTENCODING_UTF8 ) );
129 if( aCommand.Len() )
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(
140 const char* pGroup,
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 )
154 if( it->Len() )
156 for( loop = rSysCommands.begin(); loop != rSysCommands.end() && *loop != *it; ++loop )
158 if( loop == rSysCommands.end() )
160 aWriteList.push_back( *it );
161 nWritten++;
165 while( nWritten > MAX_COMMANDS )
167 aWriteList.pop_front();
168 nWritten--;
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 )
177 rCommands.clear();
178 getSystemPrintCommands( rCommands );
179 getStoredCommands( PRINTER_PERSISTENCE_GROUP, rCommands );
182 void CommandStore::getPdfCommands( ::std::list< String >& rCommands )
184 rCommands.clear();
185 getSystemPdfCommands( rCommands );
186 getStoredCommands( PDF_PERSISTENCE_GROUP, rCommands );
189 void CommandStore::getFaxCommands( ::std::list< String >& rCommands )
191 rCommands.clear();
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 ) ) );
243 else
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 ) ) );
248 FreeResource();
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 );
272 m_bWasFax = false;
273 m_bWasPdf = false;
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 ) )
281 m_bWasFax = true;
282 m_aFaxSwallowBox.Show( sal_True );
283 sal_Int32 nPos = 0;
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 ) )
289 m_bWasPdf = true;
290 sal_Int32 nPos = 0;
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 );
310 UpdateCommands();
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;
324 String aFeatures;
325 sal_Int32 nIndex = 0;
326 String aOldPdfPath;
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() )
342 aFeatures += ',';
343 aFeatures += String( aToken );
346 else if( ! aToken.compareToAscii( "pdf=", 4 ) )
348 sal_Int32 nPos = 0;
349 aOldPdfPath = aToken.getToken( 1, '=', nPos );
351 else if( ! aToken.compareToAscii( "fax=", 4 ) )
353 sal_Int32 nPos = 0;
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() )
361 aFeatures += ',';
362 aFeatures.AppendAscii( "external_dialog" );
364 if( bHaveFax )
366 if( aFeatures.Len() )
367 aFeatures += ',';
368 aFeatures.AppendAscii( "fax=" );
369 if( bFaxSwallow )
370 aFeatures.AppendAscii( "swallow" );
371 pList = &m_aFaxCommands;
373 if( bHavePdf )
375 if( aFeatures.Len() )
376 aFeatures += ',';
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 );
422 UpdateCommands();
424 else if( pBox == &m_aCommandsCB )
426 m_aRemovePB.Enable( sal_True );
429 return 0;
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;
448 else
449 pList = &m_aPdfCommands;
451 pList->remove( aEntry );
452 m_aCommandsCB.RemoveEntry( aEntry );
453 m_aQuickCB.RemoveEntry( aEntry );
455 else if( pButton == &m_aHelpButton )
457 String aHelpText;
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 );
466 aBox.Execute();
468 else if( pButton == &m_aExternalCB )
470 m_aQuickCB.Enable( m_aExternalCB.IsChecked() );
472 return 0;
475 IMPL_LINK( RTSCommandPage, DoubleClickHdl, ComboBox*, pComboBox )
477 if( pComboBox == &m_aCommandsCB )
478 ConnectCommand();
479 return 0;
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 );
487 return 0;
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 );
501 if( ! m_bWasFax )
502 m_aCommandsCB.SetText( m_pParent->m_aJobData.m_aCommand );
503 else
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 );
513 if( m_bWasFax )
514 m_aCommandsCB.SetText( m_pParent->m_aJobData.m_aCommand );
515 else
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 );
525 if( m_bWasPdf )
526 m_aCommandsCB.SetText( m_pParent->m_aJobData.m_aCommand );
527 else
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: */