update dev300-m58
[ooovba.git] / padmin / source / adddlg.cxx
blob2f8f7b43295a6d30f4a6721b5dc83c4cbd568c60
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: adddlg.cxx,v $
10 * $Revision: 1.24 $
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 ************************************************************************/
31 #include <unistd.h>
33 #include "adddlg.hxx"
34 #include "newppdlg.hxx"
35 #include "cmddlg.hxx"
36 #include "padialog.hrc"
38 #include "vcl/msgbox.hxx"
39 #include "vcl/strhelper.hxx"
41 #include "osl/thread.h"
43 #include <hash_set>
46 using namespace rtl;
47 using namespace psp;
48 using namespace padmin;
49 using namespace std;
51 APTabPage::APTabPage( AddPrinterDialog* pParent, const ResId& rResId )
52 : TabPage( pParent, rResId ),
53 m_aTitle( PaResId( RID_ADDP_STR_TITLE ) ),
54 m_pParent( pParent )
58 APChooseDevicePage::APChooseDevicePage( AddPrinterDialog* pParent ) :
59 APTabPage( pParent, PaResId( RID_ADDP_PAGE_CHOOSEDEV ) ),
60 m_aPrinterBtn( this, PaResId( RID_ADDP_CHDEV_BTN_PRINTER ) ),
61 m_aFaxBtn( this, PaResId( RID_ADDP_CHDEV_BTN_FAX ) ),
62 m_aPDFBtn( this, PaResId( RID_ADDP_CHDEV_BTN_PDF ) ),
63 m_aOldBtn( this, PaResId( RID_ADDP_CHDEV_BTN_OLD ) ),
64 m_aOverTxt( this, PaResId( RID_ADDP_CHDEV_TXT_OVER ) )
66 FreeResource();
67 m_aPrinterBtn.Check( TRUE );
68 m_aFaxBtn.Check( FALSE );
69 m_aPDFBtn.Check( FALSE );
70 m_aOldBtn.Check( FALSE );
71 if( ! AddPrinterDialog::getOldPrinterLocation().Len() )
72 m_aOldBtn.Enable( FALSE );
73 if( ! PrinterInfoManager::get().addOrRemovePossible() )
75 m_aPrinterBtn.Check( FALSE );
76 m_aFaxBtn.Check( TRUE );
77 m_aPrinterBtn.Enable( FALSE );
78 m_aOldBtn.Enable( FALSE );
82 APChooseDevicePage::~APChooseDevicePage()
86 bool APChooseDevicePage::check()
88 return true;
91 void APChooseDevicePage::fill( PrinterInfo& rInfo )
93 if( m_aPDFBtn.IsChecked() )
95 rInfo.m_aFeatures = OUString::createFromAscii( "pdf=" );
97 else if( m_aFaxBtn.IsChecked() )
99 rInfo.m_aFeatures = OUString::createFromAscii( "fax" );
101 else
102 rInfo.m_aFeatures = OUString();
105 //--------------------------------------------------------------------
107 APChooseDriverPage::APChooseDriverPage( AddPrinterDialog* pParent )
108 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_CHOOSEDRIVER ) ),
109 m_aDriverTxt( this, PaResId( RID_ADDP_CHDRV_TXT_DRIVER ) ),
110 m_aDriverBox( this, PaResId( RID_ADDP_CHDRV_BOX_DRIVER ) ),
111 m_aAddBtn( this, PaResId( RID_ADDP_CHDRV_BTN_ADD ) ),
112 m_aRemBtn( this, PaResId( RID_ADDP_CHDRV_BTN_REMOVE ) ),
113 m_aRemStr( PaResId( RID_ADDP_CHDRV_STR_REMOVE ) )
115 FreeResource();
116 m_aAddBtn.SetClickHdl( LINK( this, APChooseDriverPage, ClickBtnHdl ) );
117 m_aRemBtn.SetClickHdl( LINK( this, APChooseDriverPage, ClickBtnHdl ) );
118 m_aDriverBox.setDelPressedLink( LINK( this, APChooseDriverPage, DelPressedHdl ) );
119 updateDrivers();
122 APChooseDriverPage::~APChooseDriverPage()
124 for( int i = 0; i < m_aDriverBox.GetEntryCount(); i++ )
125 delete (String*)m_aDriverBox.GetEntryData( i );
128 bool APChooseDriverPage::check()
130 return m_aDriverBox.GetSelectEntryCount() > 0;
133 void APChooseDriverPage::fill( PrinterInfo& rInfo )
135 USHORT nPos = m_aDriverBox.GetSelectEntryPos();
136 String* pDriver = (String*)m_aDriverBox.GetEntryData( nPos );
137 rInfo.m_aDriverName = *pDriver;
138 #if OSL_DEBUG_LEVEL > 1
139 fprintf( stderr, "m_aLastPrinterName = \"%s\", rInfo.m_aPrinterName = \"%s\"\n",
140 OUStringToOString( m_aLastPrinterName, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
141 OUStringToOString( rInfo.m_aPrinterName, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
142 #endif
143 if( rInfo.m_aPrinterName.equals( m_aLastPrinterName ) )
145 String aPrinter( AddPrinterDialog::uniquePrinterName( m_aDriverBox.GetEntry( nPos ) ) );
146 rInfo.m_aPrinterName = m_aLastPrinterName = aPrinter;
150 void APChooseDriverPage::updateDrivers()
152 for( int k = 0; k < m_aDriverBox.GetEntryCount(); k++ )
153 delete (String*)m_aDriverBox.GetEntryData( k );
154 m_aDriverBox.Clear();
156 std::list< rtl::OUString > aDrivers;
157 psp::PPDParser::getKnownPPDDrivers( aDrivers );
159 for( std::list< rtl::OUString >::const_iterator it = aDrivers.begin(); it != aDrivers.end(); ++it )
161 String aDriver( ::psp::PPDParser::getPPDPrinterName( *it ) );
162 if( aDriver.Len() )
164 int nPos = m_aDriverBox.InsertEntry( aDriver );
165 m_aDriverBox.SetEntryData( nPos, new String( *it ) );
166 if( it->equalsAscii( "SGENPRT" ) )
167 m_aDriverBox.SelectEntryPos( nPos );
171 m_aRemBtn.Enable( m_aDriverBox.GetEntryCount() > 0 );
174 IMPL_LINK( APChooseDriverPage, DelPressedHdl, ListBox*, pListBox )
176 if( pListBox == &m_aDriverBox )
177 ClickBtnHdl( &m_aRemBtn );
179 return 0;
182 IMPL_LINK( APChooseDriverPage, ClickBtnHdl, PushButton*, pButton )
184 if( pButton == &m_aAddBtn )
186 PPDImportDialog aDlg( this );
187 if( aDlg.Execute() )
188 updateDrivers();
190 else if( pButton == &m_aRemBtn )
192 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
193 PrinterInfoManager& rPIManager( PrinterInfoManager::get() );
195 for( int i = 0; i < m_aDriverBox.GetSelectEntryCount(); i++ )
197 int nSelect = m_aDriverBox.GetSelectEntryPos(i);
198 String aDriver( *(String*)m_aDriverBox.GetEntryData( nSelect ) );
199 if( aDriver.Len() )
201 // never delete the default driver
202 if( aDriver.EqualsIgnoreCaseAscii( "SGENPRT" ) )
204 String aText( PaResId( RID_ERR_REMOVESGENPRT ) );
205 aText.SearchAndReplace( String::CreateFromAscii( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
206 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
207 aErrorBox.SetText( m_aRemStr );
208 aErrorBox.Execute();
209 continue;
212 PrinterInfo aDefInfo( rPIManager.getPrinterInfo( rPIManager.getDefaultPrinter() ) );
213 // for comparisons convert to a OUString
214 OUString aPPD( aDriver );
215 if( aDefInfo.m_aDriverName == aPPD )
217 String aText( PaResId( RID_ERR_REMOVEDEFAULTDRIVER ) );
218 aText.SearchAndReplace( String::CreateFromAscii( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
219 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
220 aErrorBox.SetText( m_aRemStr );
221 aErrorBox.Execute();
222 continue;
225 ::std::list< OUString > aPrinters;
226 ::std::list< OUString >::iterator it;
227 rPIManager.listPrinters( aPrinters );
228 for( it = aPrinters.begin(); it != aPrinters.end(); ++it )
230 PrinterInfo aInfo( rPIManager.getPrinterInfo( *it ) );
231 if( aInfo.m_aDriverName == aPPD )
232 break;
235 if( it != aPrinters.end() )
237 String aText( PaResId( RID_QUERY_DRIVERUSED ) );
238 aText.SearchAndReplace( String::CreateFromAscii( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
239 QueryBox aBox( this, WB_YES_NO | WB_DEF_NO, aText );
240 aBox.SetText( m_aRemStr );
241 if( aBox.Execute() == RET_NO )
242 continue;
244 else
246 String aText( PaResId( RID_QUERY_REMOVEDRIVER ) );
247 aText.SearchAndReplace( String::CreateFromAscii( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
248 QueryBox aBox( this, WB_YES_NO | WB_DEF_NO, aText );
249 aBox.SetText( m_aRemStr );
250 if( aBox.Execute() == RET_NO )
251 continue;
254 // remove the printers using this driver
255 for( it = aPrinters.begin(); it != aPrinters.end(); ++it )
257 PrinterInfo aInfo( rPIManager.getPrinterInfo( *it ) );
258 if( aInfo.m_aDriverName == aPPD )
259 rPIManager.removePrinter( *it );
262 ::std::list< rtl::OUString > aDirs;
263 // get only psprint's directories, not eventual system dirs
264 psp::getPrinterPathList( aDirs, NULL );
265 ::std::list< rtl::OUString >::iterator dir;
267 for( dir = aDirs.begin(); dir != aDirs.end(); ++dir )
269 ::std::list< String > aFiles;
270 ::std::list< String >::iterator file;
271 OUStringBuffer aDir( *dir );
272 aDir.append( sal_Unicode( '/' ) );
273 aDir.appendAscii( PRINTER_PPDDIR );
274 FindFiles( aDir.makeStringAndClear(), aFiles, String( RTL_CONSTASCII_USTRINGPARAM( "PS;PPD;PS.GZ;PPD.GZ" ) ), true );
275 for( file = aFiles.begin(); file != aFiles.end(); ++file )
277 String aFile( *dir );
278 if( aFile.GetChar( aFile.Len() ) != '/' )
279 aFile.AppendAscii( "/" );
280 aFile.Append( *file );
282 int nPos = file->SearchBackward( '.' );
283 if( file->Copy( 0, nPos ) == String( aPPD ) )
285 ByteString aSysPath( aFile, aEncoding );
286 if( unlink( aSysPath.GetBuffer() ) )
288 String aText( PaResId( RID_ERR_REMOVEDRIVERFAILED ) );
289 aText.SearchAndReplace( String::CreateFromAscii( "%s1" ), m_aDriverBox.GetSelectEntry( i ) );
290 aText.SearchAndReplace( String::CreateFromAscii( "%s2" ), aFile );
291 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
292 aErrorBox.SetText( m_aRemStr );
293 aErrorBox.Execute();
300 updateDrivers();
302 return 0;
305 //--------------------------------------------------------------------
307 APNamePage::APNamePage( AddPrinterDialog* pParent, const String& rInitName, DeviceKind::type eKind )
308 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_NAME ) ),
309 m_aNameTxt(
310 this,
311 PaResId(
312 eKind == DeviceKind::Printer ? RID_ADDP_NAME_TXT_NAME :
313 eKind == DeviceKind::Fax ? RID_ADDP_NAME_TXT_FAXNAME : RID_ADDP_NAME_TXT_PDFNAME
316 m_aNameEdt(
317 this,
318 PaResId(
319 eKind == DeviceKind::Printer ? RID_ADDP_NAME_EDT_NAME :
320 eKind == DeviceKind::Fax ? RID_ADDP_NAME_EDT_FAXNAME : RID_ADDP_NAME_EDT_PDFNAME
323 m_aDefaultBox( this, PaResId( RID_ADDP_NAME_BOX_DEFAULT ) ),
324 m_aFaxSwallowBox( this, PaResId( RID_ADDP_NAME_BOX_FAXSWALLOW ) )
326 FreeResource();
327 if( eKind != DeviceKind::Printer )
328 m_aDefaultBox.Show( FALSE );
329 else
330 m_aNameEdt.SetText( rInitName );
331 if( eKind != DeviceKind::Fax )
332 m_aFaxSwallowBox.Show( FALSE );
334 m_aNameEdt.SetText( AddPrinterDialog::uniquePrinterName( m_aNameEdt.GetText() ) );
335 m_aDefaultBox.Check( FALSE );
336 m_aFaxSwallowBox.Check( FALSE );
339 APNamePage::~APNamePage()
343 bool APNamePage::check()
345 return m_aNameEdt.GetText().Len();
348 void APNamePage::fill( PrinterInfo& rInfo )
350 rInfo.m_aPrinterName = m_aNameEdt.GetText();
353 //--------------------------------------------------------------------
355 APCommandPage::APCommandPage( AddPrinterDialog* pParent, DeviceKind::type eKind )
356 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_COMMAND ) ),
357 m_aCommandTxt( this, PaResId( RID_ADDP_CMD_TXT_COMMAND ) ),
358 m_aCommandBox( this, PaResId( eKind == DeviceKind::Pdf ? RID_ADDP_CMD_BOX_PDFCOMMAND : RID_ADDP_CMD_BOX_COMMAND ) ),
359 m_aHelpBtn( this, PaResId( RID_ADDP_CMD_BTN_HELP ) ),
360 m_aHelpTxt( PaResId( eKind == DeviceKind::Fax ? RID_ADDP_CMD_STR_FAXHELP : RID_ADDP_CMD_STR_PDFHELP ) ),
361 m_aPdfDirTxt( this, PaResId( RID_ADDP_CMD_TXT_PDFDIR ) ),
362 m_aPdfDirEdt( this, PaResId( RID_ADDP_CMD_EDT_PDFDIR ) ),
363 m_aPdfDirBtn( this, PaResId( RID_ADDP_CMD_BTN_PDFDIR ) ),
364 m_eKind( eKind )
366 FreeResource();
367 ::std::list< String > aCommands;
368 if( m_eKind == DeviceKind::Printer )
370 m_aHelpBtn.Show( FALSE );
371 Size aSize = m_aCommandTxt.GetSizePixel();
372 aSize.Width() = m_aCommandBox.GetSizePixel().Width();
373 m_aCommandTxt.SetSizePixel( aSize );
375 if( m_eKind != DeviceKind::Pdf )
377 m_aPdfDirBtn.Show( FALSE );
378 m_aPdfDirEdt.Show( FALSE );
379 m_aPdfDirTxt.Show( FALSE );
381 switch( m_eKind )
383 case DeviceKind::Printer: CommandStore::getPrintCommands( aCommands );break;
384 case DeviceKind::Fax: CommandStore::getFaxCommands( aCommands );break;
385 case DeviceKind::Pdf: CommandStore::getPdfCommands( aCommands );break;
387 // adjust height of command text and help button
388 Rectangle aPosSize( m_aCommandTxt.GetPosPixel(), m_aCommandTxt.GetSizePixel() );
389 Rectangle aTextSize = m_aCommandTxt.GetTextRect( Rectangle( Point(), aPosSize.GetSize() ), m_aCommandTxt.GetText() );
390 if( aTextSize.GetWidth() <= 2*(aPosSize.GetWidth()+1) )
392 Size aNewSize( aPosSize.GetWidth(), aPosSize.GetHeight()*2/3 );
393 if( aNewSize.Height() < m_aHelpBtn.GetSizePixel().Height()+2 )
394 aNewSize.Height() = m_aHelpBtn.GetSizePixel().Height()+2;
395 Point aNewPos( aPosSize.Left(), aPosSize.Top() + aPosSize.GetHeight() - aNewSize.Height() );
396 m_aCommandTxt.SetPosSizePixel( aNewPos, aNewSize );
397 aNewPos.X() = m_aHelpBtn.GetPosPixel().X();
398 m_aHelpBtn.SetPosPixel( aNewPos );
401 // fill in commands
402 ::std::list< String >::iterator it;
403 for( it = aCommands.begin(); it != aCommands.end(); ++it )
404 m_aCommandBox.InsertEntry( *it );
406 m_aHelpBtn.SetClickHdl( LINK( this, APCommandPage, ClickBtnHdl ) );
407 m_aPdfDirBtn.SetClickHdl( LINK( this, APCommandPage, ClickBtnHdl ) );
408 if( m_eKind != DeviceKind::Printer )
410 m_aCommandBox.SetModifyHdl( LINK( this, APCommandPage, ModifyHdl ) );
411 m_pParent->enableNext( false );
415 APCommandPage::~APCommandPage()
417 ::std::list< String > aCommands;
418 String aLastCommand( m_aCommandBox.GetText() );
419 for( int i = 0; i < m_aCommandBox.GetEntryCount(); i++ )
421 String aCommand( m_aCommandBox.GetEntry( i ) );
422 if( aCommand != aLastCommand )
423 aCommands.push_back( aCommand );
425 aCommands.push_back( aLastCommand );
426 switch( m_eKind )
428 case DeviceKind::Printer: CommandStore::setPrintCommands( aCommands );break;
429 case DeviceKind::Fax: CommandStore::setFaxCommands( aCommands );break;
430 case DeviceKind::Pdf: CommandStore::setPdfCommands( aCommands );break;
434 IMPL_LINK( APCommandPage, ClickBtnHdl, PushButton*, pButton )
436 if( pButton == &m_aHelpBtn )
438 InfoBox aBox( this, m_aHelpTxt );
439 aBox.Execute();
441 else if( pButton == &m_aPdfDirBtn )
443 String aPath( m_aPdfDirEdt.GetText() );
444 if( chooseDirectory( aPath ) )
445 m_aPdfDirEdt.SetText( aPath );
447 return 0;
450 IMPL_LINK( APCommandPage, ModifyHdl, ComboBox*, pBox )
452 if( pBox == &m_aCommandBox )
454 m_pParent->enableNext( m_aCommandBox.GetText().Len() );
456 return 0;
459 bool APCommandPage::check()
461 return true;
464 void APCommandPage::fill( PrinterInfo& rInfo )
466 rInfo.m_aCommand = m_aCommandBox.GetText();
469 //--------------------------------------------------------------------
471 APOldPrinterPage::APOldPrinterPage( AddPrinterDialog* pParent )
472 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_OLDPRINTERS ) ),
473 m_aOldPrinterTxt( this, PaResId( RID_ADDP_OLD_TXT_PRINTERS ) ),
474 m_aOldPrinterBox( this, PaResId( RID_ADDP_OLD_BOX_PRINTERS ) ),
475 m_aSelectAllBtn( this, PaResId( RID_ADDP_OLD_BTN_SELECTALL ) )
477 FreeResource();
479 m_aSelectAllBtn.SetClickHdl( LINK( this, APOldPrinterPage, ClickBtnHdl ) );
480 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
482 String aFileName( AddPrinterDialog::getOldPrinterLocation() );
483 Config aConfig( aFileName );
485 // read defaults
486 aConfig.SetGroup( "Xprinter,PostScript" );
487 ByteString aDefPageSize( aConfig.ReadKey( "PageSize" ) );
488 ByteString aDefOrientation( aConfig.ReadKey( "Orientation" ) );
489 ByteString aDefMarginLeft( aConfig.ReadKey( "MarginLeft" ) );
490 ByteString aDefMarginRight( aConfig.ReadKey( "MarginRight" ) );
491 ByteString aDefMarginTop( aConfig.ReadKey( "MarginTop" ) );
492 ByteString aDefMarginBottom( aConfig.ReadKey( "MarginBottom" ) );
493 ByteString aDefScale( aConfig.ReadKey( "Scale" ) );
494 ByteString aDefCopies( aConfig.ReadKey( "Copies" ) );
495 ByteString aDefDPI( aConfig.ReadKey( "DPI" ) );
497 aConfig.SetGroup( "devices" );
498 int nDevices = aConfig.GetKeyCount();
499 for( int nKey = 0; nKey < nDevices; nKey++ )
501 aConfig.SetGroup( "devices" );
502 ByteString aPrinter( aConfig.GetKeyName( nKey ) );
503 ByteString aValue( aConfig.ReadKey( aPrinter ) );
504 ByteString aPort( aValue.GetToken( 1, ',' ) );
505 ByteString aDriver( aValue.GetToken( 0, ' ' ) );
506 ByteString aPS( aValue.GetToken( 0, ',' ).GetToken( 1, ' ' ) );
507 ByteString aNewDriver( aDriver );
508 if( aDriver == "GENERIC" )
509 aNewDriver = "SGENPRT";
511 if( aPS != "PostScript" )
512 continue;
514 const PPDParser* pParser = PPDParser::getParser( String( aNewDriver, aEncoding ) );
515 if( pParser == NULL )
517 String aText( PaResId( RID_TXT_DRIVERDOESNOTEXIST ) );
518 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s1" ) ), String( aPrinter, aEncoding ) );
519 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s2" ) ), String( aDriver, aEncoding ) );
520 InfoBox aBox( this, aText );
521 aBox.Execute();
522 continue;
525 // read the command
526 aConfig.SetGroup( "ports" );
527 ByteString aCommand( aConfig.ReadKey( aPort ) );
528 if( ! aCommand.Len() )
530 String aText( PaResId( RID_TXT_PRINTERWITHOUTCOMMAND ) );
531 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s" ) ), String( aPrinter, aEncoding ) );
532 InfoBox aBox( this, aText );
533 aBox.Execute();
534 continue;
538 String aUPrinter( AddPrinterDialog::uniquePrinterName( String( aPrinter, aEncoding ) ) );
540 PrinterInfo aInfo;
541 aInfo.m_aDriverName = String( aNewDriver, aEncoding );
542 aInfo.m_pParser = pParser;
543 aInfo.m_aContext.setParser( pParser );
544 aInfo.m_aPrinterName = aUPrinter;
545 aInfo.m_aCommand = String( aCommand, aEncoding );
547 // read the printer settings
548 ByteString aGroup( aDriver );
549 aGroup += ",PostScript,";
550 aGroup += aPort;
551 aConfig.SetGroup( aGroup );
553 aValue = aConfig.ReadKey( "PageSize", aDefPageSize );
554 int nLeft, nRight, nTop, nBottom;
555 if( aValue.Len() &&
556 aInfo.m_pParser->getMargins( String( aValue, aEncoding ),
557 nLeft, nRight, nTop, nBottom ) )
559 const PPDKey* pKey = aInfo.m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) ) );
560 const PPDValue* pValue = pKey ? pKey->getValue( String( aValue, aEncoding ) ) : NULL;
561 if( pKey && pValue )
562 aInfo.m_aContext.setValue( pKey, pValue );
563 aValue = aConfig.ReadKey( "MarginLeft", aDefMarginLeft );
564 if( aValue.Len() )
565 aInfo.m_nLeftMarginAdjust = aValue.ToInt32() - (int)((double)nLeft * 35.27777778 );
566 aValue = aConfig.ReadKey( "MarginRight", aDefMarginRight );
567 if( aValue.Len() )
568 aInfo.m_nRightMarginAdjust = aValue.ToInt32() - (int)((double)nRight * 35.27777778 );
569 aValue = aConfig.ReadKey( "MarginTop", aDefMarginTop );
570 if( aValue.Len() )
571 aInfo.m_nTopMarginAdjust = aValue.ToInt32() - (int)((double)nTop * 35.27777778 );
572 aValue = aConfig.ReadKey( "MarginBottom", aDefMarginBottom );
573 if( aValue.Len() )
574 aInfo.m_nBottomMarginAdjust = aValue.ToInt32() - (int)((double)nBottom * 35.27777778 );
577 aValue = aConfig.ReadKey( "Copies", aDefScale );
578 if( aValue.Len() )
579 aInfo.m_nCopies = aValue.ToInt32();
581 aValue = aConfig.ReadKey( "Comment" );
582 aInfo.m_aComment = String( aValue, aEncoding );
584 aValue = aConfig.ReadKey( "Level" );
585 if( aValue.Len() )
586 aInfo.m_nPSLevel = aValue.ToInt32();
588 aValue = aConfig.ReadKey( "Orientation", aDefOrientation );
589 if( aValue.Len() )
590 aInfo.m_eOrientation = aValue.CompareIgnoreCaseToAscii( "landscape" ) == COMPARE_EQUAL ? orientation::Landscape : orientation::Portrait;
591 int nGroupKeys = aConfig.GetKeyCount();
592 for( int nPPDKey = 0; nPPDKey < nGroupKeys; nPPDKey++ )
594 ByteString aPPDKey( aConfig.GetKeyName( nPPDKey ) );
595 // ignore page region
596 // there are some ppd keys in old Xpdefaults that
597 // should never have been writte because they are defaults
598 // PageRegion leads to problems in conjunction
599 // with a not matching PageSize
600 if( aPPDKey.CompareTo( "PPD_", 4 ) == COMPARE_EQUAL &&
601 aPPDKey != "PPD_PageRegion"
604 aValue = aConfig.ReadKey( nPPDKey );
605 aPPDKey.Erase( 0, 4 );
606 const PPDKey* pKey = aInfo.m_pParser->getKey( String( aPPDKey, RTL_TEXTENCODING_ISO_8859_1 ) );
607 const PPDValue* pValue = pKey ? ( aValue.Equals( "*nil" ) ? NULL : pKey->getValue( String( aValue, RTL_TEXTENCODING_ISO_8859_1 ) ) ) : NULL;
608 if( pKey )
609 aInfo.m_aContext.setValue( pKey, pValue, true );
613 m_aOldPrinters.push_back( aInfo );
614 int nPos = m_aOldPrinterBox.InsertEntry( aInfo.m_aPrinterName );
615 m_aOldPrinterBox.SetEntryData( nPos, & m_aOldPrinters.back() );
619 APOldPrinterPage::~APOldPrinterPage()
623 IMPL_LINK( APOldPrinterPage, ClickBtnHdl, PushButton*, pButton )
625 if( pButton == &m_aSelectAllBtn )
627 for( int i = 0; i < m_aOldPrinterBox.GetEntryCount(); i++ )
628 m_aOldPrinterBox.SelectEntryPos( i );
630 return 0;
633 void APOldPrinterPage::addOldPrinters()
635 PrinterInfoManager& rManager( PrinterInfoManager::get() );
636 for( int i = 0; i < m_aOldPrinterBox.GetSelectEntryCount(); i++ )
638 PrinterInfo* pInfo = (PrinterInfo*)m_aOldPrinterBox.GetEntryData( m_aOldPrinterBox.GetSelectEntryPos( i ) );
639 pInfo->m_aPrinterName = AddPrinterDialog::uniquePrinterName( pInfo->m_aPrinterName );
640 if( ! rManager.addPrinter( pInfo->m_aPrinterName, pInfo->m_aDriverName ) )
642 String aText( PaResId( RID_TXT_PRINTERADDFAILED ) );
643 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s" ) ), pInfo->m_aPrinterName );
644 ErrorBox aBox( this, WB_OK | WB_DEF_OK, aText );
645 aBox.Execute();
646 continue;
648 rManager.changePrinterInfo( pInfo->m_aPrinterName, *pInfo );
652 bool APOldPrinterPage::check()
654 return m_aOldPrinterBox.GetEntryCount() > 0;
657 void APOldPrinterPage::fill( PrinterInfo& )
661 //--------------------------------------------------------------------
663 APFaxDriverPage::APFaxDriverPage( AddPrinterDialog* pParent )
664 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_FAXDRIVER ) ),
665 m_aFaxTxt( this, PaResId( RID_ADDP_FAXDRV_TXT_DRIVER ) ),
666 m_aDefBtn( this, PaResId( RID_ADDP_FAXDRV_BTN_DEFAULT ) ),
667 m_aSelectBtn( this, PaResId( RID_ADDP_FAXDRV_BTN_SELECT ) )
669 FreeResource();
671 m_aDefBtn.Check( TRUE );
672 m_aSelectBtn.Check( FALSE );
673 m_aSelectBtn.SetStyle( m_aSelectBtn.GetStyle() | WB_WORDBREAK );
676 APFaxDriverPage::~APFaxDriverPage()
680 bool APFaxDriverPage::check()
682 return true;
685 void APFaxDriverPage::fill( PrinterInfo& rInfo )
687 if( isDefault() )
689 rInfo.m_aDriverName = OUString::createFromAscii( "SGENPRT" );
693 //--------------------------------------------------------------------
695 APPdfDriverPage::APPdfDriverPage( AddPrinterDialog* pParent )
696 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_PDFDRIVER ) ),
697 m_aPdfTxt( this, PaResId( RID_ADDP_PDFDRV_TXT_DRIVER ) ),
698 m_aDefBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_DEFAULT ) ),
699 m_aDistBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_DIST ) ),
700 m_aSelectBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_SELECT ) )
702 FreeResource();
704 m_aDefBtn.Check( TRUE );
705 m_aDistBtn.Check( FALSE );
706 m_aSelectBtn.Check( FALSE );
707 m_aSelectBtn.SetStyle( m_aSelectBtn.GetStyle() | WB_WORDBREAK );
710 APPdfDriverPage::~APPdfDriverPage()
714 bool APPdfDriverPage::check()
716 return true;
719 void APPdfDriverPage::fill( PrinterInfo& rInfo )
721 if( isDefault() )
722 rInfo.m_aDriverName = OUString::createFromAscii( "SGENPRT" );
723 else if( isDist() )
724 rInfo.m_aDriverName = OUString::createFromAscii( "ADISTILL" );
727 //--------------------------------------------------------------------
729 AddPrinterDialog::AddPrinterDialog( Window* pParent )
730 : ModalDialog( pParent, PaResId( RID_ADD_PRINTER_DIALOG ) ),
731 m_aCancelPB( this, PaResId( RID_ADDP_BTN_CANCEL ) ),
732 m_aPrevPB( this, PaResId( RID_ADDP_BTN_PREV ) ),
733 m_aNextPB( this, PaResId( RID_ADDP_BTN_NEXT ) ),
734 m_aFinishPB( this, PaResId( RID_ADDP_BTN_FINISH ) ),
735 m_aLine( this, PaResId( RID_ADDP_LINE ) ),
736 m_aTitleImage( this, PaResId( RID_ADDP_CTRL_TITLE ) ),
737 m_pCurrentPage( NULL ),
738 m_pChooseDevicePage( NULL ),
739 m_pCommandPage( NULL ),
740 m_pChooseDriverPage( NULL ),
741 m_pNamePage( NULL ),
742 m_pOldPrinterPage( NULL ),
743 m_pFaxDriverPage( NULL ),
744 m_pFaxSelectDriverPage( NULL ),
745 m_pFaxNamePage( NULL ),
746 m_pFaxCommandPage( NULL ),
747 m_pPdfDriverPage( NULL ),
748 m_pPdfSelectDriverPage( NULL ),
749 m_pPdfNamePage( NULL ),
750 m_pPdfCommandPage( NULL )
752 FreeResource();
753 m_pCurrentPage = m_pChooseDevicePage = new APChooseDevicePage( this );
754 m_pCurrentPage->Show( TRUE );
755 m_aFinishPB.Enable( FALSE );
756 m_aPrevPB.Enable( FALSE );
758 m_aNextPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
759 m_aPrevPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
760 m_aFinishPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
761 m_aCancelPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
763 m_aTitleImage.SetBackgroundColor( Color( 0xff, 0xff, 0xff ) );
764 m_aTitleImage.SetText( m_pCurrentPage->getTitle() );
765 updateSettings();
768 AddPrinterDialog::~AddPrinterDialog()
770 if( m_pChooseDevicePage )
771 delete m_pChooseDevicePage;
772 if( m_pChooseDriverPage )
773 delete m_pChooseDriverPage;
774 if( m_pNamePage )
775 delete m_pNamePage;
776 if( m_pCommandPage )
777 delete m_pCommandPage;
778 if( m_pOldPrinterPage )
779 delete m_pOldPrinterPage;
780 if( m_pFaxDriverPage )
781 delete m_pFaxDriverPage;
782 if( m_pFaxSelectDriverPage )
783 delete m_pFaxSelectDriverPage;
784 if( m_pFaxCommandPage )
785 delete m_pFaxCommandPage;
786 if( m_pFaxNamePage )
787 delete m_pFaxNamePage;
788 if( m_pPdfDriverPage )
789 delete m_pPdfDriverPage;
790 if( m_pPdfSelectDriverPage )
791 delete m_pPdfSelectDriverPage;
792 if( m_pPdfNamePage )
793 delete m_pPdfNamePage;
794 if( m_pPdfCommandPage )
795 delete m_pPdfCommandPage;
798 void AddPrinterDialog::updateSettings()
800 if( ! GetDisplayBackground().GetColor().IsDark() )
801 m_aTitleImage.SetImage( Image( BitmapEx( PaResId( RID_BMP_PRINTER ) ) ) );
802 else
803 m_aTitleImage.SetImage( Image( BitmapEx( PaResId( RID_BMP_PRINTER_HC ) ) ) );
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( 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( 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( TRUE );
834 m_aFinishPB.Enable( TRUE );
835 m_aNextPB.Enable( 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( 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( 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( TRUE );
866 m_aNextPB.Enable( 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( FALSE );
895 m_aFinishPB.Enable( 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( FALSE );
924 m_aFinishPB.Enable( TRUE );
927 m_pCurrentPage->Show( TRUE );
928 m_aTitleImage.SetText( m_pCurrentPage->getTitle() );
931 void AddPrinterDialog::back()
933 m_pCurrentPage->Show( FALSE );
934 if( m_pCurrentPage == m_pChooseDriverPage )
936 m_pCurrentPage = m_pChooseDevicePage;
937 m_aPrevPB.Enable( FALSE );
939 else if( m_pCurrentPage == m_pNamePage )
941 m_pCurrentPage = m_pCommandPage;
942 m_aNextPB.Enable( 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( FALSE );
952 m_aNextPB.Enable( TRUE );
954 else if( m_pCurrentPage == m_pFaxDriverPage )
956 m_pCurrentPage = m_pChooseDevicePage;
957 m_aPrevPB.Enable( 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( TRUE );
968 else if( m_pCurrentPage == m_pFaxCommandPage )
970 m_pCurrentPage = m_pFaxDriverPage->isDefault() ? (APTabPage*)m_pFaxDriverPage : (APTabPage*)m_pFaxSelectDriverPage;
971 m_aNextPB.Enable( TRUE );
973 else if( m_pCurrentPage == m_pPdfDriverPage )
975 m_pCurrentPage = m_pChooseDevicePage;
976 m_aPrevPB.Enable( 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( 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( TRUE );
992 m_pCurrentPage->Show( 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::createFromAscii( "fax=" );
1014 if( m_pFaxNamePage->isFaxSwallow() )
1015 aInfo.m_aFeatures += OUString::createFromAscii( "swallow" );
1017 else if( m_pChooseDevicePage->isPDF() )
1019 OUString aPdf( OUString::createFromAscii( "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 int nVersion = 1;
1069 list< OUString > aPrinterList;
1070 rManager.listPrinters( aPrinterList );
1071 hash_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 += String::CreateFromInt32( nVersion++ );
1081 return aResult;
1084 String AddPrinterDialog::getOldPrinterLocation()
1086 static const char* pHome = getenv( "HOME" );
1087 String aRet;
1088 ByteString aFileName;
1090 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
1091 if( pHome )
1093 aFileName = pHome;
1094 aFileName.Append( "/.Xpdefaults" );
1095 if( access( aFileName.GetBuffer(), F_OK ) )
1097 aFileName = pHome;
1098 aFileName.Append( "/.sversionrc" );
1099 Config aSVer( String( aFileName, aEncoding ) );
1100 aSVer.SetGroup( "Versions" );
1101 aFileName = aSVer.ReadKey( "StarOffice 5.2" );
1102 if( aFileName.Len() )
1103 aFileName.Append( "/share/xp3/Xpdefaults" );
1104 else if(
1105 (aFileName = aSVer.ReadKey( "StarOffice 5.1" ) ).Len()
1107 (aFileName = aSVer.ReadKey( "StarOffice 5.0" ) ).Len()
1109 (aFileName = aSVer.ReadKey( "StarOffice 4.0" ) ).Len()
1112 aFileName.Append( "/xp3/Xpdefaults" );
1114 if( aFileName.Len() && access( aFileName.GetBuffer(), F_OK ) )
1115 aFileName.Erase();
1118 if( aFileName.Len() )
1119 aRet = String( aFileName, aEncoding );
1120 return aRet;