merge the formfield patch from ooo-build
[ooovba.git] / padmin / source / adddlg.cxx
blob1f95d4e272bdc466fc89cd2097858a9f7c9502a0
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( bool bRefresh, const rtl::OUString& rSelectDriver )
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, bRefresh );
159 rtl::OUString aSelectDriver( psp::PPDParser::getPPDPrinterName( rSelectDriver ) );
161 rtl::OUString aSelectedEntry;
162 for( std::list< rtl::OUString >::const_iterator it = aDrivers.begin(); it != aDrivers.end(); ++it )
164 rtl::OUString aDriver( psp::PPDParser::getPPDPrinterName( *it ) );
165 if( aDriver.getLength() )
167 int nPos = m_aDriverBox.InsertEntry( aDriver );
168 m_aDriverBox.SetEntryData( nPos, new String( *it ) );
169 if( aDriver == aSelectDriver )
170 aSelectedEntry = aDriver;
174 m_aDriverBox.SelectEntry( aSelectedEntry );
175 m_aRemBtn.Enable( m_aDriverBox.GetEntryCount() > 0 );
178 IMPL_LINK( APChooseDriverPage, DelPressedHdl, ListBox*, pListBox )
180 if( pListBox == &m_aDriverBox )
181 ClickBtnHdl( &m_aRemBtn );
183 return 0;
186 IMPL_LINK( APChooseDriverPage, ClickBtnHdl, PushButton*, pButton )
188 if( pButton == &m_aAddBtn )
190 PPDImportDialog aDlg( this );
191 if( aDlg.Execute() )
193 const std::list< rtl::OUString >& rImported( aDlg.getImportedFiles() );
194 if( rImported.empty() )
195 updateDrivers( true );
196 else
197 updateDrivers( true, rImported.front() );
200 else if( pButton == &m_aRemBtn )
202 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
203 PrinterInfoManager& rPIManager( PrinterInfoManager::get() );
205 for( int i = 0; i < m_aDriverBox.GetSelectEntryCount(); i++ )
207 int nSelect = m_aDriverBox.GetSelectEntryPos(i);
208 String aDriver( *(String*)m_aDriverBox.GetEntryData( nSelect ) );
209 if( aDriver.Len() )
211 // never delete the default driver
212 if( aDriver.EqualsIgnoreCaseAscii( "SGENPRT" ) )
214 String aText( PaResId( RID_ERR_REMOVESGENPRT ) );
215 aText.SearchAndReplace( String::CreateFromAscii( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
216 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
217 aErrorBox.SetText( m_aRemStr );
218 aErrorBox.Execute();
219 continue;
222 PrinterInfo aDefInfo( rPIManager.getPrinterInfo( rPIManager.getDefaultPrinter() ) );
223 // for comparisons convert to a OUString
224 OUString aPPD( aDriver );
225 if( aDefInfo.m_aDriverName == aPPD )
227 String aText( PaResId( RID_ERR_REMOVEDEFAULTDRIVER ) );
228 aText.SearchAndReplace( String::CreateFromAscii( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
229 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
230 aErrorBox.SetText( m_aRemStr );
231 aErrorBox.Execute();
232 continue;
235 ::std::list< OUString > aPrinters;
236 ::std::list< OUString >::iterator it;
237 rPIManager.listPrinters( aPrinters );
238 for( it = aPrinters.begin(); it != aPrinters.end(); ++it )
240 PrinterInfo aInfo( rPIManager.getPrinterInfo( *it ) );
241 if( aInfo.m_aDriverName == aPPD )
242 break;
245 if( it != aPrinters.end() )
247 String aText( PaResId( RID_QUERY_DRIVERUSED ) );
248 aText.SearchAndReplace( String::CreateFromAscii( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
249 QueryBox aBox( this, WB_YES_NO | WB_DEF_NO, aText );
250 aBox.SetText( m_aRemStr );
251 if( aBox.Execute() == RET_NO )
252 continue;
254 else
256 String aText( PaResId( RID_QUERY_REMOVEDRIVER ) );
257 aText.SearchAndReplace( String::CreateFromAscii( "%s" ), m_aDriverBox.GetSelectEntry( i ) );
258 QueryBox aBox( this, WB_YES_NO | WB_DEF_NO, aText );
259 aBox.SetText( m_aRemStr );
260 if( aBox.Execute() == RET_NO )
261 continue;
264 // remove the printers using this driver
265 for( it = aPrinters.begin(); it != aPrinters.end(); ++it )
267 PrinterInfo aInfo( rPIManager.getPrinterInfo( *it ) );
268 if( aInfo.m_aDriverName == aPPD )
269 rPIManager.removePrinter( *it );
272 std::list< rtl::OUString > aDirs;
273 // get only psprint's directories, not eventual system dirs
274 psp::getPrinterPathList( aDirs, NULL );
275 std::list< rtl::OUString >::iterator dir;
276 for( dir = aDirs.begin(); dir != aDirs.end(); ++dir )
278 ::std::list< String > aFiles;
279 ::std::list< String >::iterator file;
280 OUStringBuffer aDir( *dir );
281 aDir.append( sal_Unicode( '/' ) );
282 aDir.appendAscii( PRINTER_PPDDIR );
283 rtl::OUString aPPDDir( aDir.makeStringAndClear() );
284 FindFiles( aPPDDir, aFiles, String( RTL_CONSTASCII_USTRINGPARAM( "PS;PPD;PS.GZ;PPD.GZ" ) ), true );
285 for( file = aFiles.begin(); file != aFiles.end(); ++file )
287 String aFile( aPPDDir );
288 if( aFile.GetChar( aFile.Len() ) != '/' )
289 aFile.AppendAscii( "/" );
290 aFile.Append( *file );
292 int nPos = file->SearchBackward( '.' );
293 if( file->Copy( 0, nPos ) == String( aPPD ) )
295 ByteString aSysPath( aFile, aEncoding );
296 if( unlink( aSysPath.GetBuffer() ) )
298 String aText( PaResId( RID_ERR_REMOVEDRIVERFAILED ) );
299 aText.SearchAndReplace( String::CreateFromAscii( "%s1" ), m_aDriverBox.GetSelectEntry( i ) );
300 aText.SearchAndReplace( String::CreateFromAscii( "%s2" ), aFile );
301 ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK, aText );
302 aErrorBox.SetText( m_aRemStr );
303 aErrorBox.Execute();
310 updateDrivers();
312 return 0;
315 //--------------------------------------------------------------------
317 APNamePage::APNamePage( AddPrinterDialog* pParent, const String& rInitName, DeviceKind::type eKind )
318 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_NAME ) ),
319 m_aNameTxt(
320 this,
321 PaResId(
322 eKind == DeviceKind::Printer ? RID_ADDP_NAME_TXT_NAME :
323 eKind == DeviceKind::Fax ? RID_ADDP_NAME_TXT_FAXNAME : RID_ADDP_NAME_TXT_PDFNAME
326 m_aNameEdt(
327 this,
328 PaResId(
329 eKind == DeviceKind::Printer ? RID_ADDP_NAME_EDT_NAME :
330 eKind == DeviceKind::Fax ? RID_ADDP_NAME_EDT_FAXNAME : RID_ADDP_NAME_EDT_PDFNAME
333 m_aDefaultBox( this, PaResId( RID_ADDP_NAME_BOX_DEFAULT ) ),
334 m_aFaxSwallowBox( this, PaResId( RID_ADDP_NAME_BOX_FAXSWALLOW ) )
336 FreeResource();
337 if( eKind != DeviceKind::Printer )
338 m_aDefaultBox.Show( FALSE );
339 else
340 m_aNameEdt.SetText( rInitName );
341 if( eKind != DeviceKind::Fax )
342 m_aFaxSwallowBox.Show( FALSE );
344 m_aNameEdt.SetText( AddPrinterDialog::uniquePrinterName( m_aNameEdt.GetText() ) );
345 m_aDefaultBox.Check( FALSE );
346 m_aFaxSwallowBox.Check( FALSE );
349 APNamePage::~APNamePage()
353 bool APNamePage::check()
355 return m_aNameEdt.GetText().Len();
358 void APNamePage::fill( PrinterInfo& rInfo )
360 rInfo.m_aPrinterName = m_aNameEdt.GetText();
363 //--------------------------------------------------------------------
365 APCommandPage::APCommandPage( AddPrinterDialog* pParent, DeviceKind::type eKind )
366 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_COMMAND ) ),
367 m_aCommandTxt( this, PaResId( RID_ADDP_CMD_TXT_COMMAND ) ),
368 m_aCommandBox( this, PaResId( eKind == DeviceKind::Pdf ? RID_ADDP_CMD_BOX_PDFCOMMAND : RID_ADDP_CMD_BOX_COMMAND ) ),
369 m_aHelpBtn( this, PaResId( RID_ADDP_CMD_BTN_HELP ) ),
370 m_aHelpTxt( PaResId( eKind == DeviceKind::Fax ? RID_ADDP_CMD_STR_FAXHELP : RID_ADDP_CMD_STR_PDFHELP ) ),
371 m_aPdfDirTxt( this, PaResId( RID_ADDP_CMD_TXT_PDFDIR ) ),
372 m_aPdfDirEdt( this, PaResId( RID_ADDP_CMD_EDT_PDFDIR ) ),
373 m_aPdfDirBtn( this, PaResId( RID_ADDP_CMD_BTN_PDFDIR ) ),
374 m_eKind( eKind )
376 FreeResource();
377 ::std::list< String > aCommands;
378 if( m_eKind == DeviceKind::Printer )
380 m_aHelpBtn.Show( FALSE );
381 Size aSize = m_aCommandTxt.GetSizePixel();
382 aSize.Width() = m_aCommandBox.GetSizePixel().Width();
383 m_aCommandTxt.SetSizePixel( aSize );
385 if( m_eKind != DeviceKind::Pdf )
387 m_aPdfDirBtn.Show( FALSE );
388 m_aPdfDirEdt.Show( FALSE );
389 m_aPdfDirTxt.Show( FALSE );
391 switch( m_eKind )
393 case DeviceKind::Printer: CommandStore::getPrintCommands( aCommands );break;
394 case DeviceKind::Fax: CommandStore::getFaxCommands( aCommands );break;
395 case DeviceKind::Pdf: CommandStore::getPdfCommands( aCommands );break;
397 // adjust height of command text and help button
398 Rectangle aPosSize( m_aCommandTxt.GetPosPixel(), m_aCommandTxt.GetSizePixel() );
399 Rectangle aTextSize = m_aCommandTxt.GetTextRect( Rectangle( Point(), aPosSize.GetSize() ), m_aCommandTxt.GetText() );
400 if( aTextSize.GetWidth() <= 2*(aPosSize.GetWidth()+1) )
402 Size aNewSize( aPosSize.GetWidth(), aPosSize.GetHeight()*2/3 );
403 if( aNewSize.Height() < m_aHelpBtn.GetSizePixel().Height()+2 )
404 aNewSize.Height() = m_aHelpBtn.GetSizePixel().Height()+2;
405 Point aNewPos( aPosSize.Left(), aPosSize.Top() + aPosSize.GetHeight() - aNewSize.Height() );
406 m_aCommandTxt.SetPosSizePixel( aNewPos, aNewSize );
407 aNewPos.X() = m_aHelpBtn.GetPosPixel().X();
408 m_aHelpBtn.SetPosPixel( aNewPos );
411 // fill in commands
412 ::std::list< String >::iterator it;
413 for( it = aCommands.begin(); it != aCommands.end(); ++it )
414 m_aCommandBox.InsertEntry( *it );
416 m_aHelpBtn.SetClickHdl( LINK( this, APCommandPage, ClickBtnHdl ) );
417 m_aPdfDirBtn.SetClickHdl( LINK( this, APCommandPage, ClickBtnHdl ) );
418 if( m_eKind != DeviceKind::Printer )
420 m_aCommandBox.SetModifyHdl( LINK( this, APCommandPage, ModifyHdl ) );
421 m_pParent->enableNext( false );
425 APCommandPage::~APCommandPage()
427 ::std::list< String > aCommands;
428 String aLastCommand( m_aCommandBox.GetText() );
429 for( int i = 0; i < m_aCommandBox.GetEntryCount(); i++ )
431 String aCommand( m_aCommandBox.GetEntry( i ) );
432 if( aCommand != aLastCommand )
433 aCommands.push_back( aCommand );
435 aCommands.push_back( aLastCommand );
436 switch( m_eKind )
438 case DeviceKind::Printer: CommandStore::setPrintCommands( aCommands );break;
439 case DeviceKind::Fax: CommandStore::setFaxCommands( aCommands );break;
440 case DeviceKind::Pdf: CommandStore::setPdfCommands( aCommands );break;
444 IMPL_LINK( APCommandPage, ClickBtnHdl, PushButton*, pButton )
446 if( pButton == &m_aHelpBtn )
448 InfoBox aBox( this, m_aHelpTxt );
449 aBox.Execute();
451 else if( pButton == &m_aPdfDirBtn )
453 String aPath( m_aPdfDirEdt.GetText() );
454 if( chooseDirectory( aPath ) )
455 m_aPdfDirEdt.SetText( aPath );
457 return 0;
460 IMPL_LINK( APCommandPage, ModifyHdl, ComboBox*, pBox )
462 if( pBox == &m_aCommandBox )
464 m_pParent->enableNext( m_aCommandBox.GetText().Len() );
466 return 0;
469 bool APCommandPage::check()
471 return true;
474 void APCommandPage::fill( PrinterInfo& rInfo )
476 rInfo.m_aCommand = m_aCommandBox.GetText();
479 //--------------------------------------------------------------------
481 APOldPrinterPage::APOldPrinterPage( AddPrinterDialog* pParent )
482 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_OLDPRINTERS ) ),
483 m_aOldPrinterTxt( this, PaResId( RID_ADDP_OLD_TXT_PRINTERS ) ),
484 m_aOldPrinterBox( this, PaResId( RID_ADDP_OLD_BOX_PRINTERS ) ),
485 m_aSelectAllBtn( this, PaResId( RID_ADDP_OLD_BTN_SELECTALL ) )
487 FreeResource();
489 m_aSelectAllBtn.SetClickHdl( LINK( this, APOldPrinterPage, ClickBtnHdl ) );
490 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
492 String aFileName( AddPrinterDialog::getOldPrinterLocation() );
493 Config aConfig( aFileName );
495 // read defaults
496 aConfig.SetGroup( "Xprinter,PostScript" );
497 ByteString aDefPageSize( aConfig.ReadKey( "PageSize" ) );
498 ByteString aDefOrientation( aConfig.ReadKey( "Orientation" ) );
499 ByteString aDefMarginLeft( aConfig.ReadKey( "MarginLeft" ) );
500 ByteString aDefMarginRight( aConfig.ReadKey( "MarginRight" ) );
501 ByteString aDefMarginTop( aConfig.ReadKey( "MarginTop" ) );
502 ByteString aDefMarginBottom( aConfig.ReadKey( "MarginBottom" ) );
503 ByteString aDefScale( aConfig.ReadKey( "Scale" ) );
504 ByteString aDefCopies( aConfig.ReadKey( "Copies" ) );
505 ByteString aDefDPI( aConfig.ReadKey( "DPI" ) );
507 aConfig.SetGroup( "devices" );
508 int nDevices = aConfig.GetKeyCount();
509 for( int nKey = 0; nKey < nDevices; nKey++ )
511 aConfig.SetGroup( "devices" );
512 ByteString aPrinter( aConfig.GetKeyName( nKey ) );
513 ByteString aValue( aConfig.ReadKey( aPrinter ) );
514 ByteString aPort( aValue.GetToken( 1, ',' ) );
515 ByteString aDriver( aValue.GetToken( 0, ' ' ) );
516 ByteString aPS( aValue.GetToken( 0, ',' ).GetToken( 1, ' ' ) );
517 ByteString aNewDriver( aDriver );
518 if( aDriver == "GENERIC" )
519 aNewDriver = "SGENPRT";
521 if( aPS != "PostScript" )
522 continue;
524 const PPDParser* pParser = PPDParser::getParser( String( aNewDriver, aEncoding ) );
525 if( pParser == NULL )
527 String aText( PaResId( RID_TXT_DRIVERDOESNOTEXIST ) );
528 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s1" ) ), String( aPrinter, aEncoding ) );
529 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s2" ) ), String( aDriver, aEncoding ) );
530 InfoBox aBox( this, aText );
531 aBox.Execute();
532 continue;
535 // read the command
536 aConfig.SetGroup( "ports" );
537 ByteString aCommand( aConfig.ReadKey( aPort ) );
538 if( ! aCommand.Len() )
540 String aText( PaResId( RID_TXT_PRINTERWITHOUTCOMMAND ) );
541 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s" ) ), String( aPrinter, aEncoding ) );
542 InfoBox aBox( this, aText );
543 aBox.Execute();
544 continue;
548 String aUPrinter( AddPrinterDialog::uniquePrinterName( String( aPrinter, aEncoding ) ) );
550 PrinterInfo aInfo;
551 aInfo.m_aDriverName = String( aNewDriver, aEncoding );
552 aInfo.m_pParser = pParser;
553 aInfo.m_aContext.setParser( pParser );
554 aInfo.m_aPrinterName = aUPrinter;
555 aInfo.m_aCommand = String( aCommand, aEncoding );
557 // read the printer settings
558 ByteString aGroup( aDriver );
559 aGroup += ",PostScript,";
560 aGroup += aPort;
561 aConfig.SetGroup( aGroup );
563 aValue = aConfig.ReadKey( "PageSize", aDefPageSize );
564 int nLeft, nRight, nTop, nBottom;
565 if( aValue.Len() &&
566 aInfo.m_pParser->getMargins( String( aValue, aEncoding ),
567 nLeft, nRight, nTop, nBottom ) )
569 const PPDKey* pKey = aInfo.m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) ) );
570 const PPDValue* pValue = pKey ? pKey->getValue( String( aValue, aEncoding ) ) : NULL;
571 if( pKey && pValue )
572 aInfo.m_aContext.setValue( pKey, pValue );
573 aValue = aConfig.ReadKey( "MarginLeft", aDefMarginLeft );
574 if( aValue.Len() )
575 aInfo.m_nLeftMarginAdjust = aValue.ToInt32() - (int)((double)nLeft * 35.27777778 );
576 aValue = aConfig.ReadKey( "MarginRight", aDefMarginRight );
577 if( aValue.Len() )
578 aInfo.m_nRightMarginAdjust = aValue.ToInt32() - (int)((double)nRight * 35.27777778 );
579 aValue = aConfig.ReadKey( "MarginTop", aDefMarginTop );
580 if( aValue.Len() )
581 aInfo.m_nTopMarginAdjust = aValue.ToInt32() - (int)((double)nTop * 35.27777778 );
582 aValue = aConfig.ReadKey( "MarginBottom", aDefMarginBottom );
583 if( aValue.Len() )
584 aInfo.m_nBottomMarginAdjust = aValue.ToInt32() - (int)((double)nBottom * 35.27777778 );
587 aValue = aConfig.ReadKey( "Copies", aDefScale );
588 if( aValue.Len() )
589 aInfo.m_nCopies = aValue.ToInt32();
591 aValue = aConfig.ReadKey( "Comment" );
592 aInfo.m_aComment = String( aValue, aEncoding );
594 aValue = aConfig.ReadKey( "Level" );
595 if( aValue.Len() )
596 aInfo.m_nPSLevel = aValue.ToInt32();
598 aValue = aConfig.ReadKey( "Orientation", aDefOrientation );
599 if( aValue.Len() )
600 aInfo.m_eOrientation = aValue.CompareIgnoreCaseToAscii( "landscape" ) == COMPARE_EQUAL ? orientation::Landscape : orientation::Portrait;
601 int nGroupKeys = aConfig.GetKeyCount();
602 for( int nPPDKey = 0; nPPDKey < nGroupKeys; nPPDKey++ )
604 ByteString aPPDKey( aConfig.GetKeyName( nPPDKey ) );
605 // ignore page region
606 // there are some ppd keys in old Xpdefaults that
607 // should never have been writte because they are defaults
608 // PageRegion leads to problems in conjunction
609 // with a not matching PageSize
610 if( aPPDKey.CompareTo( "PPD_", 4 ) == COMPARE_EQUAL &&
611 aPPDKey != "PPD_PageRegion"
614 aValue = aConfig.ReadKey( nPPDKey );
615 aPPDKey.Erase( 0, 4 );
616 const PPDKey* pKey = aInfo.m_pParser->getKey( String( aPPDKey, RTL_TEXTENCODING_ISO_8859_1 ) );
617 const PPDValue* pValue = pKey ? ( aValue.Equals( "*nil" ) ? NULL : pKey->getValue( String( aValue, RTL_TEXTENCODING_ISO_8859_1 ) ) ) : NULL;
618 if( pKey )
619 aInfo.m_aContext.setValue( pKey, pValue, true );
623 m_aOldPrinters.push_back( aInfo );
624 int nPos = m_aOldPrinterBox.InsertEntry( aInfo.m_aPrinterName );
625 m_aOldPrinterBox.SetEntryData( nPos, & m_aOldPrinters.back() );
629 APOldPrinterPage::~APOldPrinterPage()
633 IMPL_LINK( APOldPrinterPage, ClickBtnHdl, PushButton*, pButton )
635 if( pButton == &m_aSelectAllBtn )
637 for( int i = 0; i < m_aOldPrinterBox.GetEntryCount(); i++ )
638 m_aOldPrinterBox.SelectEntryPos( i );
640 return 0;
643 void APOldPrinterPage::addOldPrinters()
645 PrinterInfoManager& rManager( PrinterInfoManager::get() );
646 for( int i = 0; i < m_aOldPrinterBox.GetSelectEntryCount(); i++ )
648 PrinterInfo* pInfo = (PrinterInfo*)m_aOldPrinterBox.GetEntryData( m_aOldPrinterBox.GetSelectEntryPos( i ) );
649 pInfo->m_aPrinterName = AddPrinterDialog::uniquePrinterName( pInfo->m_aPrinterName );
650 if( ! rManager.addPrinter( pInfo->m_aPrinterName, pInfo->m_aDriverName ) )
652 String aText( PaResId( RID_TXT_PRINTERADDFAILED ) );
653 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s" ) ), pInfo->m_aPrinterName );
654 ErrorBox aBox( this, WB_OK | WB_DEF_OK, aText );
655 aBox.Execute();
656 continue;
658 rManager.changePrinterInfo( pInfo->m_aPrinterName, *pInfo );
662 bool APOldPrinterPage::check()
664 return m_aOldPrinterBox.GetEntryCount() > 0;
667 void APOldPrinterPage::fill( PrinterInfo& )
671 //--------------------------------------------------------------------
673 APFaxDriverPage::APFaxDriverPage( AddPrinterDialog* pParent )
674 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_FAXDRIVER ) ),
675 m_aFaxTxt( this, PaResId( RID_ADDP_FAXDRV_TXT_DRIVER ) ),
676 m_aDefBtn( this, PaResId( RID_ADDP_FAXDRV_BTN_DEFAULT ) ),
677 m_aSelectBtn( this, PaResId( RID_ADDP_FAXDRV_BTN_SELECT ) )
679 FreeResource();
681 m_aDefBtn.Check( TRUE );
682 m_aSelectBtn.Check( FALSE );
683 m_aSelectBtn.SetStyle( m_aSelectBtn.GetStyle() | WB_WORDBREAK );
686 APFaxDriverPage::~APFaxDriverPage()
690 bool APFaxDriverPage::check()
692 return true;
695 void APFaxDriverPage::fill( PrinterInfo& rInfo )
697 if( isDefault() )
699 rInfo.m_aDriverName = OUString::createFromAscii( "SGENPRT" );
703 //--------------------------------------------------------------------
705 APPdfDriverPage::APPdfDriverPage( AddPrinterDialog* pParent )
706 : APTabPage( pParent, PaResId( RID_ADDP_PAGE_PDFDRIVER ) ),
707 m_aPdfTxt( this, PaResId( RID_ADDP_PDFDRV_TXT_DRIVER ) ),
708 m_aDefBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_DEFAULT ) ),
709 m_aDistBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_DIST ) ),
710 m_aSelectBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_SELECT ) )
712 FreeResource();
714 m_aDefBtn.Check( TRUE );
715 m_aDistBtn.Check( FALSE );
716 m_aSelectBtn.Check( FALSE );
717 m_aSelectBtn.SetStyle( m_aSelectBtn.GetStyle() | WB_WORDBREAK );
720 APPdfDriverPage::~APPdfDriverPage()
724 bool APPdfDriverPage::check()
726 return true;
729 void APPdfDriverPage::fill( PrinterInfo& rInfo )
731 if( isDefault() )
732 rInfo.m_aDriverName = OUString::createFromAscii( "SGENPRT" );
733 else if( isDist() )
734 rInfo.m_aDriverName = OUString::createFromAscii( "ADISTILL" );
737 //--------------------------------------------------------------------
739 AddPrinterDialog::AddPrinterDialog( Window* pParent )
740 : ModalDialog( pParent, PaResId( RID_ADD_PRINTER_DIALOG ) ),
741 m_aCancelPB( this, PaResId( RID_ADDP_BTN_CANCEL ) ),
742 m_aPrevPB( this, PaResId( RID_ADDP_BTN_PREV ) ),
743 m_aNextPB( this, PaResId( RID_ADDP_BTN_NEXT ) ),
744 m_aFinishPB( this, PaResId( RID_ADDP_BTN_FINISH ) ),
745 m_aLine( this, PaResId( RID_ADDP_LINE ) ),
746 m_aTitleImage( this, PaResId( RID_ADDP_CTRL_TITLE ) ),
747 m_pCurrentPage( NULL ),
748 m_pChooseDevicePage( NULL ),
749 m_pCommandPage( NULL ),
750 m_pChooseDriverPage( NULL ),
751 m_pNamePage( NULL ),
752 m_pOldPrinterPage( NULL ),
753 m_pFaxDriverPage( NULL ),
754 m_pFaxSelectDriverPage( NULL ),
755 m_pFaxNamePage( NULL ),
756 m_pFaxCommandPage( NULL ),
757 m_pPdfDriverPage( NULL ),
758 m_pPdfSelectDriverPage( NULL ),
759 m_pPdfNamePage( NULL ),
760 m_pPdfCommandPage( NULL )
762 FreeResource();
763 m_pCurrentPage = m_pChooseDevicePage = new APChooseDevicePage( this );
764 m_pCurrentPage->Show( TRUE );
765 m_aFinishPB.Enable( FALSE );
766 m_aPrevPB.Enable( FALSE );
768 m_aNextPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
769 m_aPrevPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
770 m_aFinishPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
771 m_aCancelPB.SetClickHdl( LINK( this, AddPrinterDialog, ClickBtnHdl ) );
773 m_aTitleImage.SetBackgroundColor( Color( 0xff, 0xff, 0xff ) );
774 m_aTitleImage.SetText( m_pCurrentPage->getTitle() );
775 updateSettings();
778 AddPrinterDialog::~AddPrinterDialog()
780 if( m_pChooseDevicePage )
781 delete m_pChooseDevicePage;
782 if( m_pChooseDriverPage )
783 delete m_pChooseDriverPage;
784 if( m_pNamePage )
785 delete m_pNamePage;
786 if( m_pCommandPage )
787 delete m_pCommandPage;
788 if( m_pOldPrinterPage )
789 delete m_pOldPrinterPage;
790 if( m_pFaxDriverPage )
791 delete m_pFaxDriverPage;
792 if( m_pFaxSelectDriverPage )
793 delete m_pFaxSelectDriverPage;
794 if( m_pFaxCommandPage )
795 delete m_pFaxCommandPage;
796 if( m_pFaxNamePage )
797 delete m_pFaxNamePage;
798 if( m_pPdfDriverPage )
799 delete m_pPdfDriverPage;
800 if( m_pPdfSelectDriverPage )
801 delete m_pPdfSelectDriverPage;
802 if( m_pPdfNamePage )
803 delete m_pPdfNamePage;
804 if( m_pPdfCommandPage )
805 delete m_pPdfCommandPage;
808 void AddPrinterDialog::updateSettings()
810 if( ! GetDisplayBackground().GetColor().IsDark() )
811 m_aTitleImage.SetImage( Image( BitmapEx( PaResId( RID_BMP_PRINTER ) ) ) );
812 else
813 m_aTitleImage.SetImage( Image( BitmapEx( PaResId( RID_BMP_PRINTER_HC ) ) ) );
816 void AddPrinterDialog::DataChanged( const DataChangedEvent& rEv )
818 ModalDialog::DataChanged( rEv );
819 if( (rEv.GetType() == DATACHANGED_SETTINGS) &&
820 (rEv.GetFlags() & SETTINGS_STYLE) )
822 updateSettings();
826 void AddPrinterDialog::advance()
828 m_pCurrentPage->Show( FALSE );
829 if( m_pCurrentPage == m_pChooseDevicePage )
831 if( m_pChooseDevicePage->isPrinter() )
833 if( ! m_pChooseDriverPage )
834 m_pChooseDriverPage = new APChooseDriverPage( this );
835 m_pCurrentPage = m_pChooseDriverPage;
836 m_aPrevPB.Enable( TRUE );
838 else if( m_pChooseDevicePage->isOld() )
840 if( ! m_pOldPrinterPage )
841 m_pOldPrinterPage = new APOldPrinterPage( this );
842 m_pCurrentPage = m_pOldPrinterPage;
843 m_aPrevPB.Enable( TRUE );
844 m_aFinishPB.Enable( TRUE );
845 m_aNextPB.Enable( FALSE );
847 else if( m_pChooseDevicePage->isFax() )
849 if( ! m_pFaxDriverPage )
850 m_pFaxDriverPage = new APFaxDriverPage( this );
851 m_pCurrentPage = m_pFaxDriverPage;
852 m_aPrevPB.Enable( TRUE );
854 else if( m_pChooseDevicePage->isPDF() )
856 if( ! m_pPdfDriverPage )
857 m_pPdfDriverPage = new APPdfDriverPage( this );
858 m_pCurrentPage = m_pPdfDriverPage;
859 m_aPrevPB.Enable( TRUE );
862 else if( m_pCurrentPage == m_pChooseDriverPage )
864 if( ! m_pCommandPage )
865 m_pCommandPage = new APCommandPage( this, DeviceKind::Printer );
866 m_pCurrentPage = m_pCommandPage;
868 else if( m_pCurrentPage == m_pCommandPage )
870 if( ! m_pNamePage )
871 m_pNamePage = new APNamePage( this, m_aPrinter.m_aPrinterName, DeviceKind::Printer );
872 else
873 m_pNamePage->setText( m_aPrinter.m_aPrinterName );
874 m_pCurrentPage = m_pNamePage;
875 m_aFinishPB.Enable( TRUE );
876 m_aNextPB.Enable( FALSE );
878 else if( m_pCurrentPage == m_pFaxDriverPage )
880 if( ! m_pFaxDriverPage->isDefault() )
882 if( ! m_pFaxSelectDriverPage )
883 m_pFaxSelectDriverPage = new APChooseDriverPage( this );
884 m_pCurrentPage = m_pFaxSelectDriverPage;
886 else
888 if( ! m_pFaxCommandPage )
889 m_pFaxCommandPage = new APCommandPage( this, DeviceKind::Fax );
890 m_pCurrentPage = m_pFaxCommandPage;
893 else if( m_pCurrentPage == m_pFaxSelectDriverPage )
895 if( ! m_pFaxCommandPage )
896 m_pFaxCommandPage = new APCommandPage( this, DeviceKind::Fax );
897 m_pCurrentPage = m_pFaxCommandPage;
899 else if( m_pCurrentPage == m_pFaxCommandPage )
901 if( ! m_pFaxNamePage )
902 m_pFaxNamePage = new APNamePage( this, String(), DeviceKind::Fax );
903 m_pCurrentPage = m_pFaxNamePage;
904 m_aNextPB.Enable( FALSE );
905 m_aFinishPB.Enable( TRUE );
907 else if( m_pCurrentPage == m_pPdfDriverPage )
909 if( ! m_pPdfDriverPage->isDefault() && ! m_pPdfDriverPage->isDist() )
911 if( ! m_pPdfSelectDriverPage )
912 m_pPdfSelectDriverPage = new APChooseDriverPage( this );
913 m_pCurrentPage = m_pPdfSelectDriverPage;
915 else
917 if( ! m_pPdfCommandPage )
918 m_pPdfCommandPage = new APCommandPage( this, DeviceKind::Pdf );
919 m_pCurrentPage = m_pPdfCommandPage;
922 else if( m_pCurrentPage == m_pPdfSelectDriverPage )
924 if( ! m_pPdfCommandPage )
925 m_pPdfCommandPage = new APCommandPage( this, DeviceKind::Pdf );
926 m_pCurrentPage = m_pPdfCommandPage;
928 else if( m_pCurrentPage == m_pPdfCommandPage )
930 if( ! m_pPdfNamePage )
931 m_pPdfNamePage = new APNamePage( this, String(), DeviceKind::Pdf );
932 m_pCurrentPage = m_pPdfNamePage;
933 m_aNextPB.Enable( FALSE );
934 m_aFinishPB.Enable( TRUE );
937 m_pCurrentPage->Show( TRUE );
938 m_aTitleImage.SetText( m_pCurrentPage->getTitle() );
941 void AddPrinterDialog::back()
943 m_pCurrentPage->Show( FALSE );
944 if( m_pCurrentPage == m_pChooseDriverPage )
946 m_pCurrentPage = m_pChooseDevicePage;
947 m_aPrevPB.Enable( FALSE );
949 else if( m_pCurrentPage == m_pNamePage )
951 m_pCurrentPage = m_pCommandPage;
952 m_aNextPB.Enable( TRUE );
954 else if( m_pCurrentPage == m_pCommandPage )
956 m_pCurrentPage = m_pChooseDriverPage;
958 else if( m_pCurrentPage == m_pOldPrinterPage )
960 m_pCurrentPage = m_pChooseDevicePage;
961 m_aPrevPB.Enable( FALSE );
962 m_aNextPB.Enable( TRUE );
964 else if( m_pCurrentPage == m_pFaxDriverPage )
966 m_pCurrentPage = m_pChooseDevicePage;
967 m_aPrevPB.Enable( FALSE );
969 else if( m_pCurrentPage == m_pFaxSelectDriverPage )
971 m_pCurrentPage = m_pFaxDriverPage;
973 else if( m_pCurrentPage == m_pFaxNamePage )
975 m_pCurrentPage = m_pFaxCommandPage;
976 m_aNextPB.Enable( TRUE );
978 else if( m_pCurrentPage == m_pFaxCommandPage )
980 m_pCurrentPage = m_pFaxDriverPage->isDefault() ? (APTabPage*)m_pFaxDriverPage : (APTabPage*)m_pFaxSelectDriverPage;
981 m_aNextPB.Enable( TRUE );
983 else if( m_pCurrentPage == m_pPdfDriverPage )
985 m_pCurrentPage = m_pChooseDevicePage;
986 m_aPrevPB.Enable( FALSE );
988 else if( m_pCurrentPage == m_pPdfSelectDriverPage )
990 m_pCurrentPage = m_pPdfDriverPage;
992 else if( m_pCurrentPage == m_pPdfNamePage )
994 m_pCurrentPage = m_pPdfCommandPage;
995 m_aNextPB.Enable( TRUE );
997 else if( m_pCurrentPage == m_pPdfCommandPage )
999 m_pCurrentPage = m_pPdfDriverPage->isDefault() || m_pPdfDriverPage->isDist() ? (APTabPage*)m_pPdfDriverPage : (APTabPage*)m_pPdfSelectDriverPage;
1000 m_aNextPB.Enable( TRUE );
1002 m_pCurrentPage->Show( TRUE );
1003 m_aTitleImage.SetText( m_pCurrentPage->getTitle() );
1006 void AddPrinterDialog::addPrinter()
1008 PrinterInfoManager& rManager( PrinterInfoManager::get() );
1009 if( ! m_pChooseDevicePage->isOld() )
1011 m_aPrinter.m_aPrinterName = uniquePrinterName( m_aPrinter.m_aPrinterName );
1012 if( rManager.addPrinter( m_aPrinter.m_aPrinterName, m_aPrinter.m_aDriverName ) )
1014 PrinterInfo aInfo( rManager.getPrinterInfo( m_aPrinter.m_aPrinterName ) );
1015 aInfo.m_aCommand = m_aPrinter.m_aCommand;
1016 if( m_pChooseDevicePage->isPrinter() )
1018 if( m_pNamePage->isDefault() )
1019 rManager.setDefaultPrinter( m_aPrinter.m_aPrinterName );
1021 else if( m_pChooseDevicePage->isFax() )
1023 aInfo.m_aFeatures = OUString::createFromAscii( "fax=" );
1024 if( m_pFaxNamePage->isFaxSwallow() )
1025 aInfo.m_aFeatures += OUString::createFromAscii( "swallow" );
1027 else if( m_pChooseDevicePage->isPDF() )
1029 OUString aPdf( OUString::createFromAscii( "pdf=" ) );
1030 aPdf += m_pPdfCommandPage->getPdfDir();
1031 aInfo.m_aFeatures = aPdf;
1033 rManager.changePrinterInfo( m_aPrinter.m_aPrinterName, aInfo );
1036 else if( m_pOldPrinterPage )
1037 m_pOldPrinterPage->addOldPrinters();
1040 IMPL_LINK( AddPrinterDialog, ClickBtnHdl, PushButton*, pButton )
1042 if( pButton == &m_aNextPB )
1044 if( m_pCurrentPage->check() )
1046 m_pCurrentPage->fill( m_aPrinter );
1047 advance();
1050 else if( pButton == &m_aPrevPB )
1052 if( m_pCurrentPage->check() )
1053 m_pCurrentPage->fill( m_aPrinter );
1054 back();
1056 else if( pButton == &m_aFinishPB )
1058 if( m_pCurrentPage->check() )
1060 m_pCurrentPage->fill( m_aPrinter );
1061 addPrinter();
1062 PrinterInfoManager::get().writePrinterConfig();
1063 EndDialog( 1 );
1066 else if( pButton == &m_aCancelPB )
1067 EndDialog( 0 );
1069 return 0;
1072 String AddPrinterDialog::uniquePrinterName( const String& rBase )
1074 String aResult( rBase );
1076 PrinterInfoManager& rManager( PrinterInfoManager::get() );
1078 int nVersion = 1;
1079 list< OUString > aPrinterList;
1080 rManager.listPrinters( aPrinterList );
1081 hash_set< OUString, OUStringHash > aPrinters;
1082 for( list< OUString >::const_iterator it = aPrinterList.begin(); it != aPrinterList.end(); ++it )
1083 aPrinters.insert( *it );
1084 while( aPrinters.find( aResult ) != aPrinters.end() )
1086 aResult = rBase;
1087 aResult.AppendAscii( "_" );
1088 aResult += String::CreateFromInt32( nVersion++ );
1091 return aResult;
1094 String AddPrinterDialog::getOldPrinterLocation()
1096 static const char* pHome = getenv( "HOME" );
1097 String aRet;
1098 ByteString aFileName;
1100 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
1101 if( pHome )
1103 aFileName = pHome;
1104 aFileName.Append( "/.Xpdefaults" );
1105 if( access( aFileName.GetBuffer(), F_OK ) )
1107 aFileName = pHome;
1108 aFileName.Append( "/.sversionrc" );
1109 Config aSVer( String( aFileName, aEncoding ) );
1110 aSVer.SetGroup( "Versions" );
1111 aFileName = aSVer.ReadKey( "StarOffice 5.2" );
1112 if( aFileName.Len() )
1113 aFileName.Append( "/share/xp3/Xpdefaults" );
1114 else if(
1115 (aFileName = aSVer.ReadKey( "StarOffice 5.1" ) ).Len()
1117 (aFileName = aSVer.ReadKey( "StarOffice 5.0" ) ).Len()
1119 (aFileName = aSVer.ReadKey( "StarOffice 4.0" ) ).Len()
1122 aFileName.Append( "/xp3/Xpdefaults" );
1124 if( aFileName.Len() && access( aFileName.GetBuffer(), F_OK ) )
1125 aFileName.Erase();
1128 if( aFileName.Len() )
1129 aRet = String( aFileName, aEncoding );
1130 return aRet;