Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / padmin / source / adddlg.cxx
blobe8b5941b77bb3a467c41f1bd18e136c79bf94b85
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 <unistd.h>
22 #include "adddlg.hxx"
23 #include "newppdlg.hxx"
24 #include "cmddlg.hxx"
25 #include "padialog.hrc"
27 #include "vcl/msgbox.hxx"
28 #include "vcl/strhelper.hxx"
30 #include <tools/config.hxx>
31 #include <osl/thread.h>
32 #include <rtl/strbuf.hxx>
33 #include <boost/unordered_set.hpp>
35 using namespace psp;
36 using namespace padmin;
37 using namespace std;
39 using ::rtl::OUString;
40 using ::rtl::OUStringBuffer;
41 using ::rtl::OUStringHash;
42 using ::rtl::OUStringToOString;
45 APTabPage::APTabPage( AddPrinterDialog* pParent, const ResId& rResId )
46 : TabPage( pParent, rResId ),
47 m_aTitle( PaResId( RID_ADDP_STR_TITLE ) ),
48 m_pParent( pParent )
52 APChooseDevicePage::APChooseDevicePage( AddPrinterDialog* pParent ) :
53 APTabPage( pParent, PaResId( RID_ADDP_PAGE_CHOOSEDEV ) ),
54 m_aPrinterBtn( this, PaResId( RID_ADDP_CHDEV_BTN_PRINTER ) ),
55 m_aFaxBtn( this, PaResId( RID_ADDP_CHDEV_BTN_FAX ) ),
56 m_aPDFBtn( this, PaResId( RID_ADDP_CHDEV_BTN_PDF ) ),
57 m_aOldBtn( this, PaResId( RID_ADDP_CHDEV_BTN_OLD ) ),
58 m_aOverTxt( this, PaResId( RID_ADDP_CHDEV_TXT_OVER ) )
60 FreeResource();
61 m_aPrinterBtn.Check( sal_True );
62 m_aFaxBtn.Check( sal_False );
63 m_aPDFBtn.Check( sal_False );
64 m_aOldBtn.Check( sal_False );
65 if( ! AddPrinterDialog::getOldPrinterLocation().Len() )
66 m_aOldBtn.Enable( sal_False );
67 if( ! PrinterInfoManager::get().addOrRemovePossible() )
69 m_aPrinterBtn.Check( sal_False );
70 m_aFaxBtn.Check( sal_True );
71 m_aPrinterBtn.Enable( sal_False );
72 m_aOldBtn.Enable( sal_False );
76 APChooseDevicePage::~APChooseDevicePage()
80 bool APChooseDevicePage::check()
82 return true;
85 void APChooseDevicePage::fill( PrinterInfo& rInfo )
87 if( m_aPDFBtn.IsChecked() )
89 rInfo.m_aFeatures = OUString("pdf=");
91 else if( m_aFaxBtn.IsChecked() )
93 rInfo.m_aFeatures = OUString("fax");
95 else
96 rInfo.m_aFeatures = OUString();
99 //--------------------------------------------------------------------
101 APChooseDriverPage::APChooseDriverPage( AddPrinterDialog* pParent )
102 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_CHOOSEDRIVER ) ),
103 m_aDriverTxt( this, PaResId( RID_ADDP_CHDRV_TXT_DRIVER ) ),
104 m_aDriverBox( this, PaResId( RID_ADDP_CHDRV_BOX_DRIVER ) ),
105 m_aAddBtn( this, PaResId( RID_ADDP_CHDRV_BTN_ADD ) ),
106 m_aRemBtn( this, PaResId( RID_ADDP_CHDRV_BTN_REMOVE ) ),
107 m_aRemStr( PaResId( RID_ADDP_CHDRV_STR_REMOVE ) )
109 FreeResource();
110 m_aAddBtn.SetClickHdl( LINK( this, APChooseDriverPage, ClickBtnHdl ) );
111 m_aRemBtn.SetClickHdl( LINK( this, APChooseDriverPage, ClickBtnHdl ) );
112 m_aDriverBox.setDelPressedLink( LINK( this, APChooseDriverPage, DelPressedHdl ) );
113 updateDrivers();
116 APChooseDriverPage::~APChooseDriverPage()
118 for( int i = 0; i < m_aDriverBox.GetEntryCount(); i++ )
119 delete (String*)m_aDriverBox.GetEntryData( i );
122 bool APChooseDriverPage::check()
124 return m_aDriverBox.GetSelectEntryCount() > 0;
127 void APChooseDriverPage::fill( PrinterInfo& rInfo )
129 sal_uInt16 nPos = m_aDriverBox.GetSelectEntryPos();
130 String* pDriver = (String*)m_aDriverBox.GetEntryData( nPos );
131 rInfo.m_aDriverName = *pDriver;
132 #if OSL_DEBUG_LEVEL > 1
133 fprintf( stderr, "m_aLastPrinterName = \"%s\", rInfo.m_aPrinterName = \"%s\"\n",
134 OUStringToOString( m_aLastPrinterName, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
135 OUStringToOString( rInfo.m_aPrinterName, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
136 #endif
137 if( rInfo.m_aPrinterName.equals( m_aLastPrinterName ) )
139 String aPrinter( AddPrinterDialog::uniquePrinterName( m_aDriverBox.GetEntry( nPos ) ) );
140 rInfo.m_aPrinterName = m_aLastPrinterName = aPrinter;
144 void APChooseDriverPage::updateDrivers( bool bRefresh, const rtl::OUString& rSelectDriver )
146 for( int k = 0; k < m_aDriverBox.GetEntryCount(); k++ )
147 delete (String*)m_aDriverBox.GetEntryData( k );
148 m_aDriverBox.Clear();
150 std::list< rtl::OUString > aDrivers;
151 psp::PPDParser::getKnownPPDDrivers( aDrivers, bRefresh );
153 rtl::OUString aSelectDriver( psp::PPDParser::getPPDPrinterName( rSelectDriver ) );
155 rtl::OUString aSelectedEntry;
156 for( std::list< rtl::OUString >::const_iterator it = aDrivers.begin(); it != aDrivers.end(); ++it )
158 rtl::OUString aDriver( psp::PPDParser::getPPDPrinterName( *it ) );
159 if( !aDriver.isEmpty() )
161 int nPos = m_aDriverBox.InsertEntry( aDriver );
162 m_aDriverBox.SetEntryData( nPos, new String( *it ) );
163 if( aDriver == aSelectDriver )
164 aSelectedEntry = aDriver;
168 m_aDriverBox.SelectEntry( aSelectedEntry );
169 m_aRemBtn.Enable( m_aDriverBox.GetEntryCount() > 0 );
172 IMPL_LINK( APChooseDriverPage, DelPressedHdl, ListBox*, pListBox )
174 if( pListBox == &m_aDriverBox )
175 ClickBtnHdl( &m_aRemBtn );
177 return 0;
180 IMPL_LINK( APChooseDriverPage, ClickBtnHdl, PushButton*, pButton )
182 if( pButton == &m_aAddBtn )
184 PPDImportDialog aDlg( this );
185 if( aDlg.Execute() )
187 const std::list< rtl::OUString >& rImported( aDlg.getImportedFiles() );
188 if( rImported.empty() )
189 updateDrivers( true );
190 else
191 updateDrivers( true, rImported.front() );
194 else if( pButton == &m_aRemBtn )
196 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
197 PrinterInfoManager& rPIManager( PrinterInfoManager::get() );
199 for( int i = 0; i < m_aDriverBox.GetSelectEntryCount(); i++ )
201 int nSelect = m_aDriverBox.GetSelectEntryPos(i);
202 String aDriver( *(String*)m_aDriverBox.GetEntryData( nSelect ) );
203 if( aDriver.Len() )
205 // never delete the default driver
206 if( aDriver.EqualsIgnoreCaseAscii( "SGENPRT" ) )
208 String aText( PaResId( RID_ERR_REMOVESGENPRT ) );
209 aText.SearchAndReplace( rtl::OUString( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
210 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
211 aErrorBox.SetText( m_aRemStr );
212 aErrorBox.Execute();
213 continue;
216 PrinterInfo aDefInfo( rPIManager.getPrinterInfo( rPIManager.getDefaultPrinter() ) );
217 // for comparisons convert to a OUString
218 OUString aPPD( aDriver );
219 if( aDefInfo.m_aDriverName == aPPD )
221 String aText( PaResId( RID_ERR_REMOVEDEFAULTDRIVER ) );
222 aText.SearchAndReplace( rtl::OUString( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
223 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
224 aErrorBox.SetText( m_aRemStr );
225 aErrorBox.Execute();
226 continue;
229 ::std::list< OUString > aPrinters;
230 ::std::list< OUString >::iterator it;
231 rPIManager.listPrinters( aPrinters );
232 for( it = aPrinters.begin(); it != aPrinters.end(); ++it )
234 PrinterInfo aInfo( rPIManager.getPrinterInfo( *it ) );
235 if( aInfo.m_aDriverName == aPPD )
236 break;
239 if( it != aPrinters.end() )
241 String aText( PaResId( RID_QUERY_DRIVERUSED ) );
242 aText.SearchAndReplace( rtl::OUString( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
243 QueryBox aBox( this, WB_YES_NO | WB_DEF_NO, aText );
244 aBox.SetText( m_aRemStr );
245 if( aBox.Execute() == RET_NO )
246 continue;
248 else
250 String aText( PaResId( RID_QUERY_REMOVEDRIVER ) );
251 aText.SearchAndReplace( rtl::OUString( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
252 QueryBox aBox( this, WB_YES_NO | WB_DEF_NO, aText );
253 aBox.SetText( m_aRemStr );
254 if( aBox.Execute() == RET_NO )
255 continue;
258 // remove the printers using this driver
259 for( it = aPrinters.begin(); it != aPrinters.end(); ++it )
261 PrinterInfo aInfo( rPIManager.getPrinterInfo( *it ) );
262 if( aInfo.m_aDriverName == aPPD )
263 rPIManager.removePrinter( *it );
266 std::list< rtl::OUString > aDirs;
267 // get only psprint's directories, not eventual system dirs
268 psp::getPrinterPathList( aDirs, NULL );
269 std::list< rtl::OUString >::iterator dir;
270 for( dir = aDirs.begin(); dir != aDirs.end(); ++dir )
272 ::std::list< String > aFiles;
273 ::std::list< String >::iterator file;
274 OUStringBuffer aDir( *dir );
275 aDir.append( sal_Unicode( '/' ) );
276 aDir.appendAscii( PRINTER_PPDDIR );
277 rtl::OUString aPPDDir( aDir.makeStringAndClear() );
278 FindFiles( aPPDDir, aFiles, String( "PS;PPD;PS.GZ;PPD.GZ" ), true );
279 for( file = aFiles.begin(); file != aFiles.end(); ++file )
281 String aFile( aPPDDir );
282 if( aFile.GetChar( aFile.Len() ) != '/' )
283 aFile.AppendAscii( "/" );
284 aFile.Append( *file );
286 int nPos = file->SearchBackward( '.' );
287 if( file->Copy( 0, nPos ) == String( aPPD ) )
289 rtl::OString aSysPath(rtl::OUStringToOString(aFile, aEncoding));
290 if (unlink(aSysPath.getStr()))
292 String aText( PaResId( RID_ERR_REMOVEDRIVERFAILED ) );
293 aText.SearchAndReplace( rtl::OUString( "%s1" ), m_aDriverBox.GetSelectEntry( i ) );
294 aText.SearchAndReplace( rtl::OUString( "%s2" ), aFile );
295 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
296 aErrorBox.SetText( m_aRemStr );
297 aErrorBox.Execute();
304 updateDrivers();
306 return 0;
309 //--------------------------------------------------------------------
311 APNamePage::APNamePage( AddPrinterDialog* pParent, const String& rInitName, DeviceKind::type eKind )
312 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_NAME ) ),
313 m_aNameTxt(
314 this,
315 PaResId(
316 eKind == DeviceKind::Printer ? RID_ADDP_NAME_TXT_NAME :
317 eKind == DeviceKind::Fax ? RID_ADDP_NAME_TXT_FAXNAME : RID_ADDP_NAME_TXT_PDFNAME
320 m_aNameEdt(
321 this,
322 PaResId(
323 eKind == DeviceKind::Printer ? RID_ADDP_NAME_EDT_NAME :
324 eKind == DeviceKind::Fax ? RID_ADDP_NAME_EDT_FAXNAME : RID_ADDP_NAME_EDT_PDFNAME
327 m_aDefaultBox( this, PaResId( RID_ADDP_NAME_BOX_DEFAULT ) ),
328 m_aFaxSwallowBox( this, PaResId( RID_ADDP_NAME_BOX_FAXSWALLOW ) )
330 FreeResource();
331 if( eKind != DeviceKind::Printer )
332 m_aDefaultBox.Show( sal_False );
333 else
334 m_aNameEdt.SetText( rInitName );
335 if( eKind != DeviceKind::Fax )
336 m_aFaxSwallowBox.Show( sal_False );
338 m_aNameEdt.SetText( AddPrinterDialog::uniquePrinterName( m_aNameEdt.GetText() ) );
339 m_aDefaultBox.Check( sal_False );
340 m_aFaxSwallowBox.Check( sal_False );
343 APNamePage::~APNamePage()
347 bool APNamePage::check()
349 return m_aNameEdt.GetText().Len();
352 void APNamePage::fill( PrinterInfo& rInfo )
354 rInfo.m_aPrinterName = m_aNameEdt.GetText();
357 //--------------------------------------------------------------------
359 APCommandPage::APCommandPage( AddPrinterDialog* pParent, DeviceKind::type eKind )
360 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_COMMAND ) ),
361 m_aCommandTxt( this, PaResId( RID_ADDP_CMD_TXT_COMMAND ) ),
362 m_aCommandBox( this, PaResId( eKind == DeviceKind::Pdf ? RID_ADDP_CMD_BOX_PDFCOMMAND : RID_ADDP_CMD_BOX_COMMAND ) ),
363 m_aHelpBtn( this, PaResId( RID_ADDP_CMD_BTN_HELP ) ),
364 m_aHelpTxt( PaResId( eKind == DeviceKind::Fax ? RID_ADDP_CMD_STR_FAXHELP : RID_ADDP_CMD_STR_PDFHELP ) ),
365 m_aPdfDirTxt( this, PaResId( RID_ADDP_CMD_TXT_PDFDIR ) ),
366 m_aPdfDirEdt( this, PaResId( RID_ADDP_CMD_EDT_PDFDIR ) ),
367 m_aPdfDirBtn( this, PaResId( RID_ADDP_CMD_BTN_PDFDIR ) ),
368 m_eKind( eKind )
370 FreeResource();
371 ::std::list< String > aCommands;
372 if( m_eKind == DeviceKind::Printer )
374 m_aHelpBtn.Show( sal_False );
375 Size aSize = m_aCommandTxt.GetSizePixel();
376 aSize.Width() = m_aCommandBox.GetSizePixel().Width();
377 m_aCommandTxt.SetSizePixel( aSize );
379 if( m_eKind != DeviceKind::Pdf )
381 m_aPdfDirBtn.Show( sal_False );
382 m_aPdfDirEdt.Show( sal_False );
383 m_aPdfDirTxt.Show( sal_False );
385 switch( m_eKind )
387 case DeviceKind::Printer: CommandStore::getPrintCommands( aCommands );break;
388 case DeviceKind::Fax: CommandStore::getFaxCommands( aCommands );break;
389 case DeviceKind::Pdf: CommandStore::getPdfCommands( aCommands );break;
391 // adjust height of command text and help button
392 Rectangle aPosSize( m_aCommandTxt.GetPosPixel(), m_aCommandTxt.GetSizePixel() );
393 Rectangle aTextSize = m_aCommandTxt.GetTextRect( Rectangle( Point(), aPosSize.GetSize() ), m_aCommandTxt.GetText() );
394 if( aTextSize.GetWidth() <= 2*(aPosSize.GetWidth()+1) )
396 Size aNewSize( aPosSize.GetWidth(), aPosSize.GetHeight()*2/3 );
397 if( aNewSize.Height() < m_aHelpBtn.GetSizePixel().Height()+2 )
398 aNewSize.Height() = m_aHelpBtn.GetSizePixel().Height()+2;
399 Point aNewPos( aPosSize.Left(), aPosSize.Top() + aPosSize.GetHeight() - aNewSize.Height() );
400 m_aCommandTxt.SetPosSizePixel( aNewPos, aNewSize );
401 aNewPos.X() = m_aHelpBtn.GetPosPixel().X();
402 m_aHelpBtn.SetPosPixel( aNewPos );
405 // fill in commands
406 ::std::list< String >::iterator it;
407 for( it = aCommands.begin(); it != aCommands.end(); ++it )
408 m_aCommandBox.InsertEntry( *it );
410 m_aHelpBtn.SetClickHdl( LINK( this, APCommandPage, ClickBtnHdl ) );
411 m_aPdfDirBtn.SetClickHdl( LINK( this, APCommandPage, ClickBtnHdl ) );
412 if( m_eKind != DeviceKind::Printer )
414 m_aCommandBox.SetModifyHdl( LINK( this, APCommandPage, ModifyHdl ) );
415 m_pParent->enableNext( false );
419 APCommandPage::~APCommandPage()
421 ::std::list< String > aCommands;
422 String aLastCommand( m_aCommandBox.GetText() );
423 for( int i = 0; i < m_aCommandBox.GetEntryCount(); i++ )
425 String aCommand( m_aCommandBox.GetEntry( i ) );
426 if( aCommand != aLastCommand )
427 aCommands.push_back( aCommand );
429 aCommands.push_back( aLastCommand );
430 switch( m_eKind )
432 case DeviceKind::Printer: CommandStore::setPrintCommands( aCommands );break;
433 case DeviceKind::Fax: CommandStore::setFaxCommands( aCommands );break;
434 case DeviceKind::Pdf: CommandStore::setPdfCommands( aCommands );break;
438 IMPL_LINK( APCommandPage, ClickBtnHdl, PushButton*, pButton )
440 if( pButton == &m_aHelpBtn )
442 InfoBox aBox( this, m_aHelpTxt );
443 aBox.Execute();
445 else if( pButton == &m_aPdfDirBtn )
447 String aPath( m_aPdfDirEdt.GetText() );
448 if( chooseDirectory( aPath ) )
449 m_aPdfDirEdt.SetText( aPath );
451 return 0;
454 IMPL_LINK( APCommandPage, ModifyHdl, ComboBox*, pBox )
456 if( pBox == &m_aCommandBox )
458 m_pParent->enableNext( m_aCommandBox.GetText().Len() );
460 return 0;
463 bool APCommandPage::check()
465 return true;
468 void APCommandPage::fill( PrinterInfo& rInfo )
470 rInfo.m_aCommand = m_aCommandBox.GetText();
473 //--------------------------------------------------------------------
475 APOldPrinterPage::APOldPrinterPage( AddPrinterDialog* pParent )
476 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_OLDPRINTERS ) ),
477 m_aOldPrinterTxt( this, PaResId( RID_ADDP_OLD_TXT_PRINTERS ) ),
478 m_aOldPrinterBox( this, PaResId( RID_ADDP_OLD_BOX_PRINTERS ) ),
479 m_aSelectAllBtn( this, PaResId( RID_ADDP_OLD_BTN_SELECTALL ) )
481 FreeResource();
483 m_aSelectAllBtn.SetClickHdl( LINK( this, APOldPrinterPage, ClickBtnHdl ) );
484 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
486 String aFileName( AddPrinterDialog::getOldPrinterLocation() );
487 Config aConfig( aFileName );
489 // read defaults
490 aConfig.SetGroup( "Xprinter,PostScript" );
491 rtl::OString aDefPageSize( aConfig.ReadKey( "PageSize" ) );
492 rtl::OString aDefOrientation( aConfig.ReadKey( "Orientation" ) );
493 rtl::OString aDefMarginLeft( aConfig.ReadKey( "MarginLeft" ) );
494 rtl::OString aDefMarginRight( aConfig.ReadKey( "MarginRight" ) );
495 rtl::OString aDefMarginTop( aConfig.ReadKey( "MarginTop" ) );
496 rtl::OString aDefMarginBottom( aConfig.ReadKey( "MarginBottom" ) );
497 rtl::OString aDefScale( aConfig.ReadKey( "Scale" ) );
498 rtl::OString aDefCopies( aConfig.ReadKey( "Copies" ) );
499 rtl::OString aDefDPI( aConfig.ReadKey( "DPI" ) );
501 aConfig.SetGroup( "devices" );
502 int nDevices = aConfig.GetKeyCount();
503 for( int nKey = 0; nKey < nDevices; nKey++ )
505 aConfig.SetGroup( "devices" );
506 rtl::OString aPrinter(aConfig.GetKeyName(nKey));
507 rtl::OString aValue(aConfig.ReadKey(aPrinter));
508 rtl::OString aPort(aValue.getToken(1, ','));
509 rtl::OString aDriver(aValue.getToken(0, ' '));
510 rtl::OString aPS( aValue.getToken(0, ',').getToken(1, ' ') );
511 rtl::OString aNewDriver(aDriver);
512 if( aDriver.equalsL(RTL_CONSTASCII_STRINGPARAM("GENERIC")))
513 aNewDriver = rtl::OString(RTL_CONSTASCII_STRINGPARAM("SGENPRT"));
515 if( aPS != "PostScript" )
516 continue;
518 const PPDParser* pParser = PPDParser::getParser(rtl::OStringToOUString(aNewDriver, aEncoding));
519 if( pParser == NULL )
521 String aText( PaResId( RID_TXT_DRIVERDOESNOTEXIST ) );
522 aText.SearchAndReplace( String( "%s1" ), rtl::OStringToOUString(aPrinter, aEncoding) );
523 aText.SearchAndReplace( String( "%s2" ), rtl::OStringToOUString(aDriver, aEncoding) );
524 InfoBox aBox( this, aText );
525 aBox.Execute();
526 continue;
529 // read the command
530 aConfig.SetGroup( "ports" );
531 rtl::OString aCommand( aConfig.ReadKey( aPort ) );
532 if (aCommand.isEmpty())
534 String aText( PaResId( RID_TXT_PRINTERWITHOUTCOMMAND ) );
535 aText.SearchAndReplace( String( "%s" ), rtl::OStringToOUString(aPrinter, aEncoding) );
536 InfoBox aBox( this, aText );
537 aBox.Execute();
538 continue;
542 String aUPrinter( AddPrinterDialog::uniquePrinterName(rtl::OStringToOUString(aPrinter, aEncoding)) );
544 PrinterInfo aInfo;
545 aInfo.m_aDriverName = rtl::OStringToOUString(aNewDriver, aEncoding);
546 aInfo.m_pParser = pParser;
547 aInfo.m_aContext.setParser( pParser );
548 aInfo.m_aPrinterName = aUPrinter;
549 aInfo.m_aCommand = rtl::OStringToOUString(aCommand, aEncoding);
551 // read the printer settings
552 rtl::OStringBuffer aGroup(aDriver);
553 aGroup.append(RTL_CONSTASCII_STRINGPARAM(",PostScript,"));
554 aGroup.append(aPort);
555 aConfig.SetGroup(aGroup.makeStringAndClear());
557 aValue = aConfig.ReadKey( "PageSize", aDefPageSize );
558 int nLeft, nRight, nTop, nBottom;
559 if( !aValue.isEmpty() &&
560 aInfo.m_pParser->getMargins( rtl::OStringToOUString(aValue, aEncoding),
561 nLeft, nRight, nTop, nBottom ) )
563 const PPDKey* pKey = aInfo.m_pParser->getKey( String( "PageSize" ) );
564 const PPDValue* pValue = pKey ? pKey->getValue( rtl::OStringToOUString(aValue, aEncoding) ) : NULL;
565 if( pKey && pValue )
566 aInfo.m_aContext.setValue( pKey, pValue );
567 aValue = aConfig.ReadKey( "MarginLeft", aDefMarginLeft );
568 if (!aValue.isEmpty())
569 aInfo.m_nLeftMarginAdjust = aValue.toInt32() - (int)((double)nLeft * 35.27777778 );
570 aValue = aConfig.ReadKey( "MarginRight", aDefMarginRight );
571 if (!aValue.isEmpty())
572 aInfo.m_nRightMarginAdjust = aValue.toInt32() - (int)((double)nRight * 35.27777778 );
573 aValue = aConfig.ReadKey( "MarginTop", aDefMarginTop );
574 if (!aValue.isEmpty())
575 aInfo.m_nTopMarginAdjust = aValue.toInt32() - (int)((double)nTop * 35.27777778 );
576 aValue = aConfig.ReadKey( "MarginBottom", aDefMarginBottom );
577 if (!aValue.isEmpty())
578 aInfo.m_nBottomMarginAdjust = aValue.toInt32() - (int)((double)nBottom * 35.27777778 );
581 aValue = aConfig.ReadKey( "Copies", aDefScale );
582 if (!aValue.isEmpty())
583 aInfo.m_nCopies = aValue.toInt32();
585 aValue = aConfig.ReadKey( "Comment" );
586 aInfo.m_aComment = rtl::OStringToOUString(aValue, aEncoding);
588 aValue = aConfig.ReadKey( "Level" );
589 if (!aValue.isEmpty())
590 aInfo.m_nPSLevel = aValue.toInt32();
592 aValue = aConfig.ReadKey( "Orientation", aDefOrientation );
593 if (!aValue.isEmpty())
594 aInfo.m_eOrientation = aValue.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("landscape")) ? orientation::Landscape : orientation::Portrait;
595 int nGroupKeys = aConfig.GetKeyCount();
596 for( int nPPDKey = 0; nPPDKey < nGroupKeys; nPPDKey++ )
598 rtl::OString aPPDKey( aConfig.GetKeyName( nPPDKey ) );
599 // ignore page region
600 // there are some ppd keys in old Xpdefaults that
601 // should never have been writte because they are defaults
602 // PageRegion leads to problems in conjunction
603 // with a not matching PageSize
604 if (aPPDKey.matchL(RTL_CONSTASCII_STRINGPARAM("PPD_")) &&
605 !aPPDKey.equalsL(RTL_CONSTASCII_STRINGPARAM("PPD_PageRegion")))
607 aValue = aConfig.ReadKey( nPPDKey );
608 aPPDKey = aPPDKey.copy(4);
609 const PPDKey* pKey = aInfo.m_pParser->getKey( rtl::OStringToOUString(aPPDKey, RTL_TEXTENCODING_ISO_8859_1) );
610 const PPDValue* pValue = pKey ? ( aValue.equalsL(RTL_CONSTASCII_STRINGPARAM("*nil")) ? NULL : pKey->getValue(rtl::OStringToOUString(aValue, RTL_TEXTENCODING_ISO_8859_1)) ) : NULL;
611 if( pKey )
612 aInfo.m_aContext.setValue( pKey, pValue, true );
616 m_aOldPrinters.push_back( aInfo );
617 int nPos = m_aOldPrinterBox.InsertEntry( aInfo.m_aPrinterName );
618 m_aOldPrinterBox.SetEntryData( nPos, & m_aOldPrinters.back() );
622 APOldPrinterPage::~APOldPrinterPage()
626 IMPL_LINK( APOldPrinterPage, ClickBtnHdl, PushButton*, pButton )
628 if( pButton == &m_aSelectAllBtn )
630 for( int i = 0; i < m_aOldPrinterBox.GetEntryCount(); i++ )
631 m_aOldPrinterBox.SelectEntryPos( i );
633 return 0;
636 void APOldPrinterPage::addOldPrinters()
638 PrinterInfoManager& rManager( PrinterInfoManager::get() );
639 for( int i = 0; i < m_aOldPrinterBox.GetSelectEntryCount(); i++ )
641 PrinterInfo* pInfo = (PrinterInfo*)m_aOldPrinterBox.GetEntryData( m_aOldPrinterBox.GetSelectEntryPos( i ) );
642 pInfo->m_aPrinterName = AddPrinterDialog::uniquePrinterName( pInfo->m_aPrinterName );
643 if( ! rManager.addPrinter( pInfo->m_aPrinterName, pInfo->m_aDriverName ) )
645 String aText( PaResId( RID_TXT_PRINTERADDFAILED ) );
646 aText.SearchAndReplace( String( "%s" ), pInfo->m_aPrinterName );
647 ErrorBox aBox( this, WB_OK | WB_DEF_OK, aText );
648 aBox.Execute();
649 continue;
651 rManager.changePrinterInfo( pInfo->m_aPrinterName, *pInfo );
655 bool APOldPrinterPage::check()
657 return m_aOldPrinterBox.GetEntryCount() > 0;
660 void APOldPrinterPage::fill( PrinterInfo& )
664 //--------------------------------------------------------------------
666 APFaxDriverPage::APFaxDriverPage( AddPrinterDialog* pParent )
667 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_FAXDRIVER ) ),
668 m_aFaxTxt( this, PaResId( RID_ADDP_FAXDRV_TXT_DRIVER ) ),
669 m_aDefBtn( this, PaResId( RID_ADDP_FAXDRV_BTN_DEFAULT ) ),
670 m_aSelectBtn( this, PaResId( RID_ADDP_FAXDRV_BTN_SELECT ) )
672 FreeResource();
674 m_aDefBtn.Check( sal_True );
675 m_aSelectBtn.Check( sal_False );
676 m_aSelectBtn.SetStyle( m_aSelectBtn.GetStyle() | WB_WORDBREAK );
679 APFaxDriverPage::~APFaxDriverPage()
683 bool APFaxDriverPage::check()
685 return true;
688 void APFaxDriverPage::fill( PrinterInfo& rInfo )
690 if( isDefault() )
692 rInfo.m_aDriverName = OUString("SGENPRT");
696 //--------------------------------------------------------------------
698 APPdfDriverPage::APPdfDriverPage( AddPrinterDialog* pParent )
699 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_PDFDRIVER ) ),
700 m_aPdfTxt( this, PaResId( RID_ADDP_PDFDRV_TXT_DRIVER ) ),
701 m_aDefBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_DEFAULT ) ),
702 m_aDistBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_DIST ) ),
703 m_aSelectBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_SELECT ) )
705 FreeResource();
707 m_aDefBtn.Check( sal_True );
708 m_aDistBtn.Check( sal_False );
709 m_aSelectBtn.Check( sal_False );
710 m_aSelectBtn.SetStyle( m_aSelectBtn.GetStyle() | WB_WORDBREAK );
713 APPdfDriverPage::~APPdfDriverPage()
717 bool APPdfDriverPage::check()
719 return true;
722 void APPdfDriverPage::fill( PrinterInfo& rInfo )
724 if( isDefault() )
725 rInfo.m_aDriverName = OUString("SGENPRT");
726 else if( isDist() )
727 rInfo.m_aDriverName = OUString("ADISTILL");
730 //--------------------------------------------------------------------
732 AddPrinterDialog::AddPrinterDialog( Window* pParent )
733 : ModalDialog( pParent, PaResId( RID_ADD_PRINTER_DIALOG ) ),
734 m_aCancelPB( this, PaResId( RID_ADDP_BTN_CANCEL ) ),
735 m_aPrevPB( this, PaResId( RID_ADDP_BTN_PREV ) ),
736 m_aNextPB( this, PaResId( RID_ADDP_BTN_NEXT ) ),
737 m_aFinishPB( this, PaResId( RID_ADDP_BTN_FINISH ) ),
738 m_aLine( this, PaResId( RID_ADDP_LINE ) ),
739 m_aTitleImage( this, PaResId( RID_ADDP_CTRL_TITLE ) ),
740 m_pCurrentPage( NULL ),
741 m_pChooseDevicePage( NULL ),
742 m_pCommandPage( NULL ),
743 m_pChooseDriverPage( NULL ),
744 m_pNamePage( NULL ),
745 m_pOldPrinterPage( NULL ),
746 m_pFaxDriverPage( NULL ),
747 m_pFaxSelectDriverPage( NULL ),
748 m_pFaxNamePage( NULL ),
749 m_pFaxCommandPage( NULL ),
750 m_pPdfDriverPage( NULL ),
751 m_pPdfSelectDriverPage( NULL ),
752 m_pPdfNamePage( NULL ),
753 m_pPdfCommandPage( NULL )
755 FreeResource();
756 m_pCurrentPage = m_pChooseDevicePage = new APChooseDevicePage( this );
757 m_pCurrentPage->Show( sal_True );
758 m_aFinishPB.Enable( sal_False );
759 m_aPrevPB.Enable( sal_False );
761 m_aNextPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
762 m_aPrevPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
763 m_aFinishPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
764 m_aCancelPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
766 m_aTitleImage.SetBackgroundColor( Color( 0xff, 0xff, 0xff ) );
767 m_aTitleImage.SetText( m_pCurrentPage->getTitle() );
768 updateSettings();
771 AddPrinterDialog::~AddPrinterDialog()
773 if( m_pChooseDevicePage )
774 delete m_pChooseDevicePage;
775 if( m_pChooseDriverPage )
776 delete m_pChooseDriverPage;
777 if( m_pNamePage )
778 delete m_pNamePage;
779 if( m_pCommandPage )
780 delete m_pCommandPage;
781 if( m_pOldPrinterPage )
782 delete m_pOldPrinterPage;
783 if( m_pFaxDriverPage )
784 delete m_pFaxDriverPage;
785 if( m_pFaxSelectDriverPage )
786 delete m_pFaxSelectDriverPage;
787 if( m_pFaxCommandPage )
788 delete m_pFaxCommandPage;
789 if( m_pFaxNamePage )
790 delete m_pFaxNamePage;
791 if( m_pPdfDriverPage )
792 delete m_pPdfDriverPage;
793 if( m_pPdfSelectDriverPage )
794 delete m_pPdfSelectDriverPage;
795 if( m_pPdfNamePage )
796 delete m_pPdfNamePage;
797 if( m_pPdfCommandPage )
798 delete m_pPdfCommandPage;
801 void AddPrinterDialog::updateSettings()
803 m_aTitleImage.SetImage( Image( BitmapEx( PaResId( RID_BMP_PRINTER ) ) ) );
806 void AddPrinterDialog::DataChanged( const DataChangedEvent& rEv )
808 ModalDialog::DataChanged( rEv );
809 if( (rEv.GetType() == DATACHANGED_SETTINGS) &&
810 (rEv.GetFlags() & SETTINGS_STYLE) )
812 updateSettings();
816 void AddPrinterDialog::advance()
818 m_pCurrentPage->Show( sal_False );
819 if( m_pCurrentPage == m_pChooseDevicePage )
821 if( m_pChooseDevicePage->isPrinter() )
823 if( ! m_pChooseDriverPage )
824 m_pChooseDriverPage = new APChooseDriverPage( this );
825 m_pCurrentPage = m_pChooseDriverPage;
826 m_aPrevPB.Enable( sal_True );
828 else if( m_pChooseDevicePage->isOld() )
830 if( ! m_pOldPrinterPage )
831 m_pOldPrinterPage = new APOldPrinterPage( this );
832 m_pCurrentPage = m_pOldPrinterPage;
833 m_aPrevPB.Enable( sal_True );
834 m_aFinishPB.Enable( sal_True );
835 m_aNextPB.Enable( sal_False );
837 else if( m_pChooseDevicePage->isFax() )
839 if( ! m_pFaxDriverPage )
840 m_pFaxDriverPage = new APFaxDriverPage( this );
841 m_pCurrentPage = m_pFaxDriverPage;
842 m_aPrevPB.Enable( sal_True );
844 else if( m_pChooseDevicePage->isPDF() )
846 if( ! m_pPdfDriverPage )
847 m_pPdfDriverPage = new APPdfDriverPage( this );
848 m_pCurrentPage = m_pPdfDriverPage;
849 m_aPrevPB.Enable( sal_True );
852 else if( m_pCurrentPage == m_pChooseDriverPage )
854 if( ! m_pCommandPage )
855 m_pCommandPage = new APCommandPage( this, DeviceKind::Printer );
856 m_pCurrentPage = m_pCommandPage;
858 else if( m_pCurrentPage == m_pCommandPage )
860 if( ! m_pNamePage )
861 m_pNamePage = new APNamePage( this, m_aPrinter.m_aPrinterName, DeviceKind::Printer );
862 else
863 m_pNamePage->setText( m_aPrinter.m_aPrinterName );
864 m_pCurrentPage = m_pNamePage;
865 m_aFinishPB.Enable( sal_True );
866 m_aNextPB.Enable( sal_False );
868 else if( m_pCurrentPage == m_pFaxDriverPage )
870 if( ! m_pFaxDriverPage->isDefault() )
872 if( ! m_pFaxSelectDriverPage )
873 m_pFaxSelectDriverPage = new APChooseDriverPage( this );
874 m_pCurrentPage = m_pFaxSelectDriverPage;
876 else
878 if( ! m_pFaxCommandPage )
879 m_pFaxCommandPage = new APCommandPage( this, DeviceKind::Fax );
880 m_pCurrentPage = m_pFaxCommandPage;
883 else if( m_pCurrentPage == m_pFaxSelectDriverPage )
885 if( ! m_pFaxCommandPage )
886 m_pFaxCommandPage = new APCommandPage( this, DeviceKind::Fax );
887 m_pCurrentPage = m_pFaxCommandPage;
889 else if( m_pCurrentPage == m_pFaxCommandPage )
891 if( ! m_pFaxNamePage )
892 m_pFaxNamePage = new APNamePage( this, String(), DeviceKind::Fax );
893 m_pCurrentPage = m_pFaxNamePage;
894 m_aNextPB.Enable( sal_False );
895 m_aFinishPB.Enable( sal_True );
897 else if( m_pCurrentPage == m_pPdfDriverPage )
899 if( ! m_pPdfDriverPage->isDefault() && ! m_pPdfDriverPage->isDist() )
901 if( ! m_pPdfSelectDriverPage )
902 m_pPdfSelectDriverPage = new APChooseDriverPage( this );
903 m_pCurrentPage = m_pPdfSelectDriverPage;
905 else
907 if( ! m_pPdfCommandPage )
908 m_pPdfCommandPage = new APCommandPage( this, DeviceKind::Pdf );
909 m_pCurrentPage = m_pPdfCommandPage;
912 else if( m_pCurrentPage == m_pPdfSelectDriverPage )
914 if( ! m_pPdfCommandPage )
915 m_pPdfCommandPage = new APCommandPage( this, DeviceKind::Pdf );
916 m_pCurrentPage = m_pPdfCommandPage;
918 else if( m_pCurrentPage == m_pPdfCommandPage )
920 if( ! m_pPdfNamePage )
921 m_pPdfNamePage = new APNamePage( this, String(), DeviceKind::Pdf );
922 m_pCurrentPage = m_pPdfNamePage;
923 m_aNextPB.Enable( sal_False );
924 m_aFinishPB.Enable( sal_True );
927 m_pCurrentPage->Show( sal_True );
928 m_aTitleImage.SetText( m_pCurrentPage->getTitle() );
931 void AddPrinterDialog::back()
933 m_pCurrentPage->Show( sal_False );
934 if( m_pCurrentPage == m_pChooseDriverPage )
936 m_pCurrentPage = m_pChooseDevicePage;
937 m_aPrevPB.Enable( sal_False );
939 else if( m_pCurrentPage == m_pNamePage )
941 m_pCurrentPage = m_pCommandPage;
942 m_aNextPB.Enable( sal_True );
944 else if( m_pCurrentPage == m_pCommandPage )
946 m_pCurrentPage = m_pChooseDriverPage;
948 else if( m_pCurrentPage == m_pOldPrinterPage )
950 m_pCurrentPage = m_pChooseDevicePage;
951 m_aPrevPB.Enable( sal_False );
952 m_aNextPB.Enable( sal_True );
954 else if( m_pCurrentPage == m_pFaxDriverPage )
956 m_pCurrentPage = m_pChooseDevicePage;
957 m_aPrevPB.Enable( sal_False );
959 else if( m_pCurrentPage == m_pFaxSelectDriverPage )
961 m_pCurrentPage = m_pFaxDriverPage;
963 else if( m_pCurrentPage == m_pFaxNamePage )
965 m_pCurrentPage = m_pFaxCommandPage;
966 m_aNextPB.Enable( sal_True );
968 else if( m_pCurrentPage == m_pFaxCommandPage )
970 m_pCurrentPage = m_pFaxDriverPage->isDefault() ? (APTabPage*)m_pFaxDriverPage : (APTabPage*)m_pFaxSelectDriverPage;
971 m_aNextPB.Enable( sal_True );
973 else if( m_pCurrentPage == m_pPdfDriverPage )
975 m_pCurrentPage = m_pChooseDevicePage;
976 m_aPrevPB.Enable( sal_False );
978 else if( m_pCurrentPage == m_pPdfSelectDriverPage )
980 m_pCurrentPage = m_pPdfDriverPage;
982 else if( m_pCurrentPage == m_pPdfNamePage )
984 m_pCurrentPage = m_pPdfCommandPage;
985 m_aNextPB.Enable( sal_True );
987 else if( m_pCurrentPage == m_pPdfCommandPage )
989 m_pCurrentPage = m_pPdfDriverPage->isDefault() || m_pPdfDriverPage->isDist() ? (APTabPage*)m_pPdfDriverPage : (APTabPage*)m_pPdfSelectDriverPage;
990 m_aNextPB.Enable( sal_True );
992 m_pCurrentPage->Show( sal_True );
993 m_aTitleImage.SetText( m_pCurrentPage->getTitle() );
996 void AddPrinterDialog::addPrinter()
998 PrinterInfoManager& rManager( PrinterInfoManager::get() );
999 if( ! m_pChooseDevicePage->isOld() )
1001 m_aPrinter.m_aPrinterName = uniquePrinterName( m_aPrinter.m_aPrinterName );
1002 if( rManager.addPrinter( m_aPrinter.m_aPrinterName, m_aPrinter.m_aDriverName ) )
1004 PrinterInfo aInfo( rManager.getPrinterInfo( m_aPrinter.m_aPrinterName ) );
1005 aInfo.m_aCommand = m_aPrinter.m_aCommand;
1006 if( m_pChooseDevicePage->isPrinter() )
1008 if( m_pNamePage->isDefault() )
1009 rManager.setDefaultPrinter( m_aPrinter.m_aPrinterName );
1011 else if( m_pChooseDevicePage->isFax() )
1013 aInfo.m_aFeatures = OUString("fax=");
1014 if( m_pFaxNamePage->isFaxSwallow() )
1015 aInfo.m_aFeatures += OUString("swallow");
1017 else if( m_pChooseDevicePage->isPDF() )
1019 OUString aPdf( "pdf=" );
1020 aPdf += m_pPdfCommandPage->getPdfDir();
1021 aInfo.m_aFeatures = aPdf;
1023 rManager.changePrinterInfo( m_aPrinter.m_aPrinterName, aInfo );
1026 else if( m_pOldPrinterPage )
1027 m_pOldPrinterPage->addOldPrinters();
1030 IMPL_LINK( AddPrinterDialog, ClickBtnHdl, PushButton*, pButton )
1032 if( pButton == &m_aNextPB )
1034 if( m_pCurrentPage->check() )
1036 m_pCurrentPage->fill( m_aPrinter );
1037 advance();
1040 else if( pButton == &m_aPrevPB )
1042 if( m_pCurrentPage->check() )
1043 m_pCurrentPage->fill( m_aPrinter );
1044 back();
1046 else if( pButton == &m_aFinishPB )
1048 if( m_pCurrentPage->check() )
1050 m_pCurrentPage->fill( m_aPrinter );
1051 addPrinter();
1052 PrinterInfoManager::get().writePrinterConfig();
1053 EndDialog( 1 );
1056 else if( pButton == &m_aCancelPB )
1057 EndDialog( 0 );
1059 return 0;
1062 String AddPrinterDialog::uniquePrinterName( const String& rBase )
1064 String aResult( rBase );
1066 PrinterInfoManager& rManager( PrinterInfoManager::get() );
1068 sal_Int32 nVersion = 1;
1069 list< OUString > aPrinterList;
1070 rManager.listPrinters( aPrinterList );
1071 boost::unordered_set< OUString, OUStringHash > aPrinters;
1072 for( list< OUString >::const_iterator it = aPrinterList.begin(); it != aPrinterList.end(); ++it )
1073 aPrinters.insert( *it );
1074 while( aPrinters.find( aResult ) != aPrinters.end() )
1076 aResult = rBase;
1077 aResult.AppendAscii( "_" );
1078 aResult += rtl::OUString::valueOf(nVersion++);
1081 return aResult;
1084 String AddPrinterDialog::getOldPrinterLocation()
1086 static const char* pHome = getenv( "HOME" );
1087 rtl::OString aFileName;
1089 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
1090 if( pHome )
1092 aFileName = rtl::OStringBuffer().append(pHome).
1093 append(RTL_CONSTASCII_STRINGPARAM("/.Xpdefaults")).
1094 makeStringAndClear();
1095 if (access(aFileName.getStr(), F_OK))
1097 aFileName = rtl::OStringBuffer().append(pHome).
1098 append(RTL_CONSTASCII_STRINGPARAM("/.sversionrc")).
1099 makeStringAndClear();
1100 Config aSVer(rtl::OStringToOUString(aFileName, aEncoding));
1101 aSVer.SetGroup( "Versions" );
1102 aFileName = aSVer.ReadKey( "StarOffice 5.2" );
1103 if (!aFileName.isEmpty())
1104 aFileName = aFileName + rtl::OString(RTL_CONSTASCII_STRINGPARAM("/share/xp3/Xpdefaults"));
1105 else if(
1106 (aFileName = aSVer.ReadKey( "StarOffice 5.1" ) ).getLength()
1108 (aFileName = aSVer.ReadKey( "StarOffice 5.0" ) ).getLength()
1110 (aFileName = aSVer.ReadKey( "StarOffice 4.0" ) ).getLength()
1113 aFileName = aFileName + rtl::OString(RTL_CONSTASCII_STRINGPARAM("/xp3/Xpdefaults"));
1115 if (!aFileName.isEmpty() && access(aFileName.getStr(), F_OK))
1116 aFileName = rtl::OString();
1120 return !aFileName.isEmpty() ? rtl::OStringToOUString(aFileName, aEncoding) : rtl::OUString();
1123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */