1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include "newppdlg.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>
36 using namespace padmin
;
41 APTabPage::APTabPage( AddPrinterDialog
* pParent
, const ResId
& rResId
)
42 : TabPage( pParent
, rResId
),
43 m_aTitle( PaResId( RID_ADDP_STR_TITLE
) ),
48 APChooseDevicePage::APChooseDevicePage( AddPrinterDialog
* pParent
) :
49 APTabPage( pParent
, PaResId( RID_ADDP_PAGE_CHOOSEDEV
) ),
50 m_aPrinterBtn( this, PaResId( RID_ADDP_CHDEV_BTN_PRINTER
) ),
51 m_aFaxBtn( this, PaResId( RID_ADDP_CHDEV_BTN_FAX
) ),
52 m_aPDFBtn( this, PaResId( RID_ADDP_CHDEV_BTN_PDF
) ),
53 m_aOldBtn( this, PaResId( RID_ADDP_CHDEV_BTN_OLD
) ),
54 m_aOverTxt( this, PaResId( RID_ADDP_CHDEV_TXT_OVER
) )
57 m_aPrinterBtn
.Check( sal_True
);
58 m_aFaxBtn
.Check( sal_False
);
59 m_aPDFBtn
.Check( sal_False
);
60 m_aOldBtn
.Check( sal_False
);
61 if( ! AddPrinterDialog::getOldPrinterLocation().Len() )
62 m_aOldBtn
.Enable( sal_False
);
63 if( ! PrinterInfoManager::get().addOrRemovePossible() )
65 m_aPrinterBtn
.Check( sal_False
);
66 m_aFaxBtn
.Check( sal_True
);
67 m_aPrinterBtn
.Enable( sal_False
);
68 m_aOldBtn
.Enable( sal_False
);
72 APChooseDevicePage::~APChooseDevicePage()
76 bool APChooseDevicePage::check()
81 void APChooseDevicePage::fill( PrinterInfo
& rInfo
)
83 if( m_aPDFBtn
.IsChecked() )
85 rInfo
.m_aFeatures
= OUString("pdf=");
87 else if( m_aFaxBtn
.IsChecked() )
89 rInfo
.m_aFeatures
= OUString("fax");
92 rInfo
.m_aFeatures
= "";
95 //--------------------------------------------------------------------
97 APChooseDriverPage::APChooseDriverPage( AddPrinterDialog
* pParent
)
98 : APTabPage( pParent
, PaResId( RID_ADDP_PAGE_CHOOSEDRIVER
) ),
99 m_aDriverTxt( this, PaResId( RID_ADDP_CHDRV_TXT_DRIVER
) ),
100 m_aDriverBox( this, PaResId( RID_ADDP_CHDRV_BOX_DRIVER
) ),
101 m_aAddBtn( this, PaResId( RID_ADDP_CHDRV_BTN_ADD
) ),
102 m_aRemBtn( this, PaResId( RID_ADDP_CHDRV_BTN_REMOVE
) ),
103 m_aRemStr( PaResId( RID_ADDP_CHDRV_STR_REMOVE
) )
106 m_aAddBtn
.SetClickHdl( LINK( this, APChooseDriverPage
, ClickBtnHdl
) );
107 m_aRemBtn
.SetClickHdl( LINK( this, APChooseDriverPage
, ClickBtnHdl
) );
108 m_aDriverBox
.setDelPressedLink( LINK( this, APChooseDriverPage
, DelPressedHdl
) );
112 APChooseDriverPage::~APChooseDriverPage()
114 for( int i
= 0; i
< m_aDriverBox
.GetEntryCount(); i
++ )
115 delete (String
*)m_aDriverBox
.GetEntryData( i
);
118 bool APChooseDriverPage::check()
120 return m_aDriverBox
.GetSelectEntryCount() > 0;
123 void APChooseDriverPage::fill( PrinterInfo
& rInfo
)
125 sal_uInt16 nPos
= m_aDriverBox
.GetSelectEntryPos();
126 OUString
* pDriver
= (OUString
*)m_aDriverBox
.GetEntryData( nPos
);
127 rInfo
.m_aDriverName
= *pDriver
;
128 #if OSL_DEBUG_LEVEL > 1
129 fprintf( stderr
, "m_aLastPrinterName = \"%s\", rInfo.m_aPrinterName = \"%s\"\n",
130 OUStringToOString( m_aLastPrinterName
, RTL_TEXTENCODING_ISO_8859_1
).getStr(),
131 OUStringToOString( rInfo
.m_aPrinterName
, RTL_TEXTENCODING_ISO_8859_1
).getStr() );
133 if( rInfo
.m_aPrinterName
.equals( m_aLastPrinterName
) )
135 OUString
aPrinter( AddPrinterDialog::uniquePrinterName( m_aDriverBox
.GetEntry( nPos
) ) );
136 rInfo
.m_aPrinterName
= m_aLastPrinterName
= aPrinter
;
140 void APChooseDriverPage::updateDrivers( bool bRefresh
, const OUString
& rSelectDriver
)
142 for( int k
= 0; k
< m_aDriverBox
.GetEntryCount(); k
++ )
143 delete (String
*)m_aDriverBox
.GetEntryData( k
);
144 m_aDriverBox
.Clear();
146 std::list
< OUString
> aDrivers
;
147 psp::PPDParser::getKnownPPDDrivers( aDrivers
, bRefresh
);
149 OUString
aSelectDriver( psp::PPDParser::getPPDPrinterName( rSelectDriver
) );
151 OUString aSelectedEntry
;
152 for( std::list
< OUString
>::const_iterator it
= aDrivers
.begin(); it
!= aDrivers
.end(); ++it
)
154 OUString
aDriver( psp::PPDParser::getPPDPrinterName( *it
) );
155 if( !aDriver
.isEmpty() )
157 int nPos
= m_aDriverBox
.InsertEntry( aDriver
);
158 m_aDriverBox
.SetEntryData( nPos
, new OUString( *it
) );
159 if( aDriver
== aSelectDriver
)
160 aSelectedEntry
= aDriver
;
164 m_aDriverBox
.SelectEntry( aSelectedEntry
);
165 m_aRemBtn
.Enable( m_aDriverBox
.GetEntryCount() > 0 );
168 IMPL_LINK( APChooseDriverPage
, DelPressedHdl
, ListBox
*, pListBox
)
170 if( pListBox
== &m_aDriverBox
)
171 ClickBtnHdl( &m_aRemBtn
);
176 IMPL_LINK( APChooseDriverPage
, ClickBtnHdl
, PushButton
*, pButton
)
178 if( pButton
== &m_aAddBtn
)
180 PPDImportDialog
aDlg( this );
183 const std::list
< OUString
>& rImported( aDlg
.getImportedFiles() );
184 if( rImported
.empty() )
185 updateDrivers( true );
187 updateDrivers( true, rImported
.front() );
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 OUString
aDriver( *(OUString
*)m_aDriverBox
.GetEntryData( nSelect
) );
199 if( !aDriver
.isEmpty() )
201 // never delete the default driver
202 if( aDriver
.equalsIgnoreAsciiCase( "SGENPRT" ) )
204 OUString
aText( PaResId( RID_ERR_REMOVESGENPRT
) );
205 aText
= aText
.replaceFirst( OUString( "%s" ), m_aDriverBox
.GetSelectEntry( i
) );
206 ErrorBox
aErrorBox( this, WB_OK
| WB_DEF_OK
, aText
);
207 aErrorBox
.SetText( m_aRemStr
);
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 OUString
aText( PaResId( RID_ERR_REMOVEDEFAULTDRIVER
) );
218 aText
= aText
.replaceFirst( OUString( "%s" ), m_aDriverBox
.GetSelectEntry( i
) );
219 ErrorBox
aErrorBox( this, WB_OK
| WB_DEF_OK
, aText
);
220 aErrorBox
.SetText( m_aRemStr
);
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
)
235 if( it
!= aPrinters
.end() )
237 OUString
aText( PaResId( RID_QUERY_DRIVERUSED
) );
238 aText
= aText
.replaceFirst( OUString( "%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
)
246 OUString
aText( PaResId( RID_QUERY_REMOVEDRIVER
) );
247 aText
= aText
.replaceFirst( OUString( "%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
)
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
< OUString
> aDirs
;
263 // get only psprint's directories, not eventual system dirs
264 psp::getPrinterPathList( aDirs
, NULL
);
265 std::list
< OUString
>::iterator dir
;
266 for( dir
= aDirs
.begin(); dir
!= aDirs
.end(); ++dir
)
268 ::std::list
< String
> aFiles
;
269 ::std::list
< String
>::iterator file
;
270 OUStringBuffer
aDir( *dir
);
271 aDir
.append( sal_Unicode( '/' ) );
272 aDir
.appendAscii( PRINTER_PPDDIR
);
273 OUString
aPPDDir( aDir
.makeStringAndClear() );
274 FindFiles( aPPDDir
, aFiles
, OUString( "PS;PPD;PS.GZ;PPD.GZ" ), true );
275 for( file
= aFiles
.begin(); file
!= aFiles
.end(); ++file
)
277 OUString
aFile( aPPDDir
);
278 if( !aFile
.endsWith( "/" ) )
282 int nPos
= file
->SearchBackward( '.' );
283 if( file
->Copy( 0, nPos
) == String( aPPD
) )
285 OString
aSysPath(OUStringToOString(aFile
, aEncoding
));
286 if (unlink(aSysPath
.getStr()))
288 OUString
aText( PaResId( RID_ERR_REMOVEDRIVERFAILED
) );
289 aText
= aText
.replaceFirst( OUString( "%s1" ), m_aDriverBox
.GetSelectEntry( i
) );
290 aText
= aText
.replaceFirst( OUString( "%s2" ), aFile
);
291 ErrorBox
aErrorBox( this, WB_OK
| WB_DEF_OK
, aText
);
292 aErrorBox
.SetText( m_aRemStr
);
305 //--------------------------------------------------------------------
307 APNamePage::APNamePage( AddPrinterDialog
* pParent
, const String
& rInitName
, DeviceKind::type eKind
)
308 : APTabPage( pParent
, PaResId( RID_ADDP_PAGE_NAME
) ),
312 eKind
== DeviceKind::Printer
? RID_ADDP_NAME_TXT_NAME
:
313 eKind
== DeviceKind::Fax
? RID_ADDP_NAME_TXT_FAXNAME
: RID_ADDP_NAME_TXT_PDFNAME
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
) )
327 if( eKind
!= DeviceKind::Printer
)
328 m_aDefaultBox
.Show( sal_False
);
330 m_aNameEdt
.SetText( rInitName
);
331 if( eKind
!= DeviceKind::Fax
)
332 m_aFaxSwallowBox
.Show( sal_False
);
334 m_aNameEdt
.SetText( AddPrinterDialog::uniquePrinterName( m_aNameEdt
.GetText() ) );
335 m_aDefaultBox
.Check( sal_False
);
336 m_aFaxSwallowBox
.Check( sal_False
);
339 APNamePage::~APNamePage()
343 bool APNamePage::check()
345 return !m_aNameEdt
.GetText().isEmpty();
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
) ),
367 ::std::list
< String
> aCommands
;
368 if( m_eKind
== DeviceKind::Printer
)
370 m_aHelpBtn
.Show( sal_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( sal_False
);
378 m_aPdfDirEdt
.Show( sal_False
);
379 m_aPdfDirTxt
.Show( sal_False
);
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
);
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 OUString
aLastCommand( m_aCommandBox
.GetText() );
419 for( int i
= 0; i
< m_aCommandBox
.GetEntryCount(); i
++ )
421 OUString
aCommand( m_aCommandBox
.GetEntry( i
) );
422 if( aCommand
!= aLastCommand
)
423 aCommands
.push_back( aCommand
);
425 aCommands
.push_back( aLastCommand
);
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
);
441 else if( pButton
== &m_aPdfDirBtn
)
443 OUString
aPath( m_aPdfDirEdt
.GetText() );
444 if( chooseDirectory( aPath
) )
445 m_aPdfDirEdt
.SetText( aPath
);
450 IMPL_LINK( APCommandPage
, ModifyHdl
, ComboBox
*, pBox
)
452 if( pBox
== &m_aCommandBox
)
454 m_pParent
->enableNext( !m_aCommandBox
.GetText().isEmpty() );
459 bool APCommandPage::check()
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
) )
479 m_aSelectAllBtn
.SetClickHdl( LINK( this, APOldPrinterPage
, ClickBtnHdl
) );
480 rtl_TextEncoding aEncoding
= osl_getThreadTextEncoding();
482 OUString
aFileName( AddPrinterDialog::getOldPrinterLocation() );
483 Config
aConfig( aFileName
);
486 aConfig
.SetGroup( "Xprinter,PostScript" );
487 OString
aDefPageSize( aConfig
.ReadKey( "PageSize" ) );
488 OString
aDefOrientation( aConfig
.ReadKey( "Orientation" ) );
489 OString
aDefMarginLeft( aConfig
.ReadKey( "MarginLeft" ) );
490 OString
aDefMarginRight( aConfig
.ReadKey( "MarginRight" ) );
491 OString
aDefMarginTop( aConfig
.ReadKey( "MarginTop" ) );
492 OString
aDefMarginBottom( aConfig
.ReadKey( "MarginBottom" ) );
493 OString
aDefScale( aConfig
.ReadKey( "Scale" ) );
495 aConfig
.SetGroup( "devices" );
496 int nDevices
= aConfig
.GetKeyCount();
497 for( int nKey
= 0; nKey
< nDevices
; nKey
++ )
499 aConfig
.SetGroup( "devices" );
500 OString
aPrinter(aConfig
.GetKeyName(nKey
));
501 OString
aValue(aConfig
.ReadKey(aPrinter
));
502 OString
aPort(aValue
.getToken(1, ','));
503 OString
aDriver(aValue
.getToken(0, ' '));
504 OString
aPS( aValue
.getToken(0, ',').getToken(1, ' ') );
505 OString
aNewDriver(aDriver
);
506 if( aDriver
== "GENERIC")
507 aNewDriver
= OString("SGENPRT");
509 if( aPS
!= "PostScript" )
512 const PPDParser
* pParser
= PPDParser::getParser(OStringToOUString(aNewDriver
, aEncoding
));
513 if( pParser
== NULL
)
515 OUString
aText( PaResId( RID_TXT_DRIVERDOESNOTEXIST
) );
516 aText
= aText
.replaceFirst( OUString( "%s1" ), OStringToOUString(aPrinter
, aEncoding
) );
517 aText
= aText
.replaceFirst( OUString( "%s2" ), OStringToOUString(aDriver
, aEncoding
) );
518 InfoBox
aBox( this, aText
);
524 aConfig
.SetGroup( "ports" );
525 OString
aCommand( aConfig
.ReadKey( aPort
) );
526 if (!aCommand
.isEmpty())
528 OUString
aText( PaResId( RID_TXT_PRINTERWITHOUTCOMMAND
) );
529 aText
= aText
.replaceFirst( OUString( "%s" ), OStringToOUString(aPrinter
, aEncoding
) );
530 InfoBox
aBox( this, aText
);
536 String
aUPrinter( AddPrinterDialog::uniquePrinterName(OStringToOUString(aPrinter
, aEncoding
)) );
539 aInfo
.m_aDriverName
= OStringToOUString(aNewDriver
, aEncoding
);
540 aInfo
.m_pParser
= pParser
;
541 aInfo
.m_aContext
.setParser( pParser
);
542 aInfo
.m_aPrinterName
= aUPrinter
;
543 aInfo
.m_aCommand
= OStringToOUString(aCommand
, aEncoding
);
545 // read the printer settings
546 OStringBuffer
aGroup(aDriver
);
547 aGroup
.append(",PostScript,");
548 aGroup
.append(aPort
);
549 aConfig
.SetGroup(aGroup
.makeStringAndClear());
551 aValue
= aConfig
.ReadKey( "PageSize", aDefPageSize
);
552 int nLeft
, nRight
, nTop
, nBottom
;
553 if( !aValue
.isEmpty() &&
554 aInfo
.m_pParser
->getMargins( OStringToOUString(aValue
, aEncoding
),
555 nLeft
, nRight
, nTop
, nBottom
) )
557 const PPDKey
* pKey
= aInfo
.m_pParser
->getKey( String( "PageSize" ) );
558 const PPDValue
* pValue
= pKey
? pKey
->getValue( OStringToOUString(aValue
, aEncoding
) ) : NULL
;
560 aInfo
.m_aContext
.setValue( pKey
, pValue
);
561 aValue
= aConfig
.ReadKey( "MarginLeft", aDefMarginLeft
);
562 if (!aValue
.isEmpty())
563 aInfo
.m_nLeftMarginAdjust
= aValue
.toInt32() - (int)((double)nLeft
* 35.27777778 );
564 aValue
= aConfig
.ReadKey( "MarginRight", aDefMarginRight
);
565 if (!aValue
.isEmpty())
566 aInfo
.m_nRightMarginAdjust
= aValue
.toInt32() - (int)((double)nRight
* 35.27777778 );
567 aValue
= aConfig
.ReadKey( "MarginTop", aDefMarginTop
);
568 if (!aValue
.isEmpty())
569 aInfo
.m_nTopMarginAdjust
= aValue
.toInt32() - (int)((double)nTop
* 35.27777778 );
570 aValue
= aConfig
.ReadKey( "MarginBottom", aDefMarginBottom
);
571 if (!aValue
.isEmpty())
572 aInfo
.m_nBottomMarginAdjust
= aValue
.toInt32() - (int)((double)nBottom
* 35.27777778 );
575 aValue
= aConfig
.ReadKey( "Copies", aDefScale
);
576 if (!aValue
.isEmpty())
577 aInfo
.m_nCopies
= aValue
.toInt32();
579 aValue
= aConfig
.ReadKey( "Comment" );
580 aInfo
.m_aComment
= OStringToOUString(aValue
, aEncoding
);
582 aValue
= aConfig
.ReadKey( "Level" );
583 if (!aValue
.isEmpty())
584 aInfo
.m_nPSLevel
= aValue
.toInt32();
586 aValue
= aConfig
.ReadKey( "Orientation", aDefOrientation
);
587 if (!aValue
.isEmpty())
588 aInfo
.m_eOrientation
= aValue
.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("landscape")) ? orientation::Landscape
: orientation::Portrait
;
589 int nGroupKeys
= aConfig
.GetKeyCount();
590 for( int nPPDKey
= 0; nPPDKey
< nGroupKeys
; nPPDKey
++ )
592 OString
aPPDKey( aConfig
.GetKeyName( nPPDKey
) );
593 // ignore page region
594 // there are some ppd keys in old Xpdefaults that
595 // should never have been writte because they are defaults
596 // PageRegion leads to problems in conjunction
597 // with a not matching PageSize
598 if (aPPDKey
.match("PPD_") && aPPDKey
!= "PPD_PageRegion")
600 aValue
= aConfig
.ReadKey( nPPDKey
);
601 aPPDKey
= aPPDKey
.copy(4);
602 const PPDKey
* pKey
= aInfo
.m_pParser
->getKey( OStringToOUString(aPPDKey
, RTL_TEXTENCODING_ISO_8859_1
) );
603 const PPDValue
* pValue
= pKey
? ( aValue
== "*nil" ? NULL
: pKey
->getValue(OStringToOUString(aValue
, RTL_TEXTENCODING_ISO_8859_1
)) ) : NULL
;
605 aInfo
.m_aContext
.setValue( pKey
, pValue
, true );
609 m_aOldPrinters
.push_back( aInfo
);
610 int nPos
= m_aOldPrinterBox
.InsertEntry( aInfo
.m_aPrinterName
);
611 m_aOldPrinterBox
.SetEntryData( nPos
, & m_aOldPrinters
.back() );
615 APOldPrinterPage::~APOldPrinterPage()
619 IMPL_LINK( APOldPrinterPage
, ClickBtnHdl
, PushButton
*, pButton
)
621 if( pButton
== &m_aSelectAllBtn
)
623 for( int i
= 0; i
< m_aOldPrinterBox
.GetEntryCount(); i
++ )
624 m_aOldPrinterBox
.SelectEntryPos( i
);
629 void APOldPrinterPage::addOldPrinters()
631 PrinterInfoManager
& rManager( PrinterInfoManager::get() );
632 for( int i
= 0; i
< m_aOldPrinterBox
.GetSelectEntryCount(); i
++ )
634 PrinterInfo
* pInfo
= (PrinterInfo
*)m_aOldPrinterBox
.GetEntryData( m_aOldPrinterBox
.GetSelectEntryPos( i
) );
635 pInfo
->m_aPrinterName
= AddPrinterDialog::uniquePrinterName( pInfo
->m_aPrinterName
);
636 if( ! rManager
.addPrinter( pInfo
->m_aPrinterName
, pInfo
->m_aDriverName
) )
638 OUString
aText( PaResId( RID_TXT_PRINTERADDFAILED
) );
639 aText
= aText
.replaceFirst( OUString( "%s" ), pInfo
->m_aPrinterName
);
640 ErrorBox
aBox( this, WB_OK
| WB_DEF_OK
, aText
);
644 rManager
.changePrinterInfo( pInfo
->m_aPrinterName
, *pInfo
);
648 bool APOldPrinterPage::check()
650 return m_aOldPrinterBox
.GetEntryCount() > 0;
653 void APOldPrinterPage::fill( PrinterInfo
& )
657 //--------------------------------------------------------------------
659 APFaxDriverPage::APFaxDriverPage( AddPrinterDialog
* pParent
)
660 : APTabPage( pParent
, PaResId( RID_ADDP_PAGE_FAXDRIVER
) ),
661 m_aFaxTxt( this, PaResId( RID_ADDP_FAXDRV_TXT_DRIVER
) ),
662 m_aDefBtn( this, PaResId( RID_ADDP_FAXDRV_BTN_DEFAULT
) ),
663 m_aSelectBtn( this, PaResId( RID_ADDP_FAXDRV_BTN_SELECT
) )
667 m_aDefBtn
.Check( sal_True
);
668 m_aSelectBtn
.Check( sal_False
);
669 m_aSelectBtn
.SetStyle( m_aSelectBtn
.GetStyle() | WB_WORDBREAK
);
672 APFaxDriverPage::~APFaxDriverPage()
676 bool APFaxDriverPage::check()
681 void APFaxDriverPage::fill( PrinterInfo
& rInfo
)
685 rInfo
.m_aDriverName
= OUString("SGENPRT");
689 //--------------------------------------------------------------------
691 APPdfDriverPage::APPdfDriverPage( AddPrinterDialog
* pParent
)
692 : APTabPage( pParent
, PaResId( RID_ADDP_PAGE_PDFDRIVER
) ),
693 m_aPdfTxt( this, PaResId( RID_ADDP_PDFDRV_TXT_DRIVER
) ),
694 m_aDefBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_DEFAULT
) ),
695 m_aDistBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_DIST
) ),
696 m_aSelectBtn( this, PaResId( RID_ADDP_PDFDRV_BTN_SELECT
) )
700 m_aDefBtn
.Check( sal_True
);
701 m_aDistBtn
.Check( sal_False
);
702 m_aSelectBtn
.Check( sal_False
);
703 m_aSelectBtn
.SetStyle( m_aSelectBtn
.GetStyle() | WB_WORDBREAK
);
706 APPdfDriverPage::~APPdfDriverPage()
710 bool APPdfDriverPage::check()
715 void APPdfDriverPage::fill( PrinterInfo
& rInfo
)
718 rInfo
.m_aDriverName
= OUString("SGENPRT");
720 rInfo
.m_aDriverName
= OUString("ADISTILL");
723 //--------------------------------------------------------------------
725 AddPrinterDialog::AddPrinterDialog( Window
* pParent
)
726 : ModalDialog( pParent
, PaResId( RID_ADD_PRINTER_DIALOG
) ),
727 m_aCancelPB( this, PaResId( RID_ADDP_BTN_CANCEL
) ),
728 m_aPrevPB( this, PaResId( RID_ADDP_BTN_PREV
) ),
729 m_aNextPB( this, PaResId( RID_ADDP_BTN_NEXT
) ),
730 m_aFinishPB( this, PaResId( RID_ADDP_BTN_FINISH
) ),
731 m_aLine( this, PaResId( RID_ADDP_LINE
) ),
732 m_aTitleImage( this, PaResId( RID_ADDP_CTRL_TITLE
) ),
733 m_pCurrentPage( NULL
),
734 m_pChooseDevicePage( NULL
),
735 m_pCommandPage( NULL
),
736 m_pChooseDriverPage( NULL
),
738 m_pOldPrinterPage( NULL
),
739 m_pFaxDriverPage( NULL
),
740 m_pFaxSelectDriverPage( NULL
),
741 m_pFaxNamePage( NULL
),
742 m_pFaxCommandPage( NULL
),
743 m_pPdfDriverPage( NULL
),
744 m_pPdfSelectDriverPage( NULL
),
745 m_pPdfNamePage( NULL
),
746 m_pPdfCommandPage( NULL
)
749 m_pCurrentPage
= m_pChooseDevicePage
= new APChooseDevicePage( this );
750 m_pCurrentPage
->Show( sal_True
);
751 m_aFinishPB
.Enable( sal_False
);
752 m_aPrevPB
.Enable( sal_False
);
754 m_aNextPB
.SetClickHdl( LINK( this, AddPrinterDialog
, ClickBtnHdl
) );
755 m_aPrevPB
.SetClickHdl( LINK( this, AddPrinterDialog
, ClickBtnHdl
) );
756 m_aFinishPB
.SetClickHdl( LINK( this, AddPrinterDialog
, ClickBtnHdl
) );
757 m_aCancelPB
.SetClickHdl( LINK( this, AddPrinterDialog
, ClickBtnHdl
) );
759 m_aTitleImage
.SetBackgroundColor( Color( 0xff, 0xff, 0xff ) );
760 m_aTitleImage
.SetText( m_pCurrentPage
->getTitle() );
764 AddPrinterDialog::~AddPrinterDialog()
766 if( m_pChooseDevicePage
)
767 delete m_pChooseDevicePage
;
768 if( m_pChooseDriverPage
)
769 delete m_pChooseDriverPage
;
773 delete m_pCommandPage
;
774 if( m_pOldPrinterPage
)
775 delete m_pOldPrinterPage
;
776 if( m_pFaxDriverPage
)
777 delete m_pFaxDriverPage
;
778 if( m_pFaxSelectDriverPage
)
779 delete m_pFaxSelectDriverPage
;
780 if( m_pFaxCommandPage
)
781 delete m_pFaxCommandPage
;
783 delete m_pFaxNamePage
;
784 if( m_pPdfDriverPage
)
785 delete m_pPdfDriverPage
;
786 if( m_pPdfSelectDriverPage
)
787 delete m_pPdfSelectDriverPage
;
789 delete m_pPdfNamePage
;
790 if( m_pPdfCommandPage
)
791 delete m_pPdfCommandPage
;
794 void AddPrinterDialog::updateSettings()
796 m_aTitleImage
.SetImage( Image( BitmapEx( PaResId( RID_BMP_PRINTER
) ) ) );
799 void AddPrinterDialog::DataChanged( const DataChangedEvent
& rEv
)
801 ModalDialog::DataChanged( rEv
);
802 if( (rEv
.GetType() == DATACHANGED_SETTINGS
) &&
803 (rEv
.GetFlags() & SETTINGS_STYLE
) )
809 void AddPrinterDialog::advance()
811 m_pCurrentPage
->Show( sal_False
);
812 if( m_pCurrentPage
== m_pChooseDevicePage
)
814 if( m_pChooseDevicePage
->isPrinter() )
816 if( ! m_pChooseDriverPage
)
817 m_pChooseDriverPage
= new APChooseDriverPage( this );
818 m_pCurrentPage
= m_pChooseDriverPage
;
819 m_aPrevPB
.Enable( sal_True
);
821 else if( m_pChooseDevicePage
->isOld() )
823 if( ! m_pOldPrinterPage
)
824 m_pOldPrinterPage
= new APOldPrinterPage( this );
825 m_pCurrentPage
= m_pOldPrinterPage
;
826 m_aPrevPB
.Enable( sal_True
);
827 m_aFinishPB
.Enable( sal_True
);
828 m_aNextPB
.Enable( sal_False
);
830 else if( m_pChooseDevicePage
->isFax() )
832 if( ! m_pFaxDriverPage
)
833 m_pFaxDriverPage
= new APFaxDriverPage( this );
834 m_pCurrentPage
= m_pFaxDriverPage
;
835 m_aPrevPB
.Enable( sal_True
);
837 else if( m_pChooseDevicePage
->isPDF() )
839 if( ! m_pPdfDriverPage
)
840 m_pPdfDriverPage
= new APPdfDriverPage( this );
841 m_pCurrentPage
= m_pPdfDriverPage
;
842 m_aPrevPB
.Enable( sal_True
);
845 else if( m_pCurrentPage
== m_pChooseDriverPage
)
847 if( ! m_pCommandPage
)
848 m_pCommandPage
= new APCommandPage( this, DeviceKind::Printer
);
849 m_pCurrentPage
= m_pCommandPage
;
851 else if( m_pCurrentPage
== m_pCommandPage
)
854 m_pNamePage
= new APNamePage( this, m_aPrinter
.m_aPrinterName
, DeviceKind::Printer
);
856 m_pNamePage
->setText( m_aPrinter
.m_aPrinterName
);
857 m_pCurrentPage
= m_pNamePage
;
858 m_aFinishPB
.Enable( sal_True
);
859 m_aNextPB
.Enable( sal_False
);
861 else if( m_pCurrentPage
== m_pFaxDriverPage
)
863 if( ! m_pFaxDriverPage
->isDefault() )
865 if( ! m_pFaxSelectDriverPage
)
866 m_pFaxSelectDriverPage
= new APChooseDriverPage( this );
867 m_pCurrentPage
= m_pFaxSelectDriverPage
;
871 if( ! m_pFaxCommandPage
)
872 m_pFaxCommandPage
= new APCommandPage( this, DeviceKind::Fax
);
873 m_pCurrentPage
= m_pFaxCommandPage
;
876 else if( m_pCurrentPage
== m_pFaxSelectDriverPage
)
878 if( ! m_pFaxCommandPage
)
879 m_pFaxCommandPage
= new APCommandPage( this, DeviceKind::Fax
);
880 m_pCurrentPage
= m_pFaxCommandPage
;
882 else if( m_pCurrentPage
== m_pFaxCommandPage
)
884 if( ! m_pFaxNamePage
)
885 m_pFaxNamePage
= new APNamePage( this, OUString(), DeviceKind::Fax
);
886 m_pCurrentPage
= m_pFaxNamePage
;
887 m_aNextPB
.Enable( sal_False
);
888 m_aFinishPB
.Enable( sal_True
);
890 else if( m_pCurrentPage
== m_pPdfDriverPage
)
892 if( ! m_pPdfDriverPage
->isDefault() && ! m_pPdfDriverPage
->isDist() )
894 if( ! m_pPdfSelectDriverPage
)
895 m_pPdfSelectDriverPage
= new APChooseDriverPage( this );
896 m_pCurrentPage
= m_pPdfSelectDriverPage
;
900 if( ! m_pPdfCommandPage
)
901 m_pPdfCommandPage
= new APCommandPage( this, DeviceKind::Pdf
);
902 m_pCurrentPage
= m_pPdfCommandPage
;
905 else if( m_pCurrentPage
== m_pPdfSelectDriverPage
)
907 if( ! m_pPdfCommandPage
)
908 m_pPdfCommandPage
= new APCommandPage( this, DeviceKind::Pdf
);
909 m_pCurrentPage
= m_pPdfCommandPage
;
911 else if( m_pCurrentPage
== m_pPdfCommandPage
)
913 if( ! m_pPdfNamePage
)
914 m_pPdfNamePage
= new APNamePage( this, OUString(), DeviceKind::Pdf
);
915 m_pCurrentPage
= m_pPdfNamePage
;
916 m_aNextPB
.Enable( sal_False
);
917 m_aFinishPB
.Enable( sal_True
);
920 m_pCurrentPage
->Show( sal_True
);
921 m_aTitleImage
.SetText( m_pCurrentPage
->getTitle() );
924 void AddPrinterDialog::back()
926 m_pCurrentPage
->Show( sal_False
);
927 if( m_pCurrentPage
== m_pChooseDriverPage
)
929 m_pCurrentPage
= m_pChooseDevicePage
;
930 m_aPrevPB
.Enable( sal_False
);
932 else if( m_pCurrentPage
== m_pNamePage
)
934 m_pCurrentPage
= m_pCommandPage
;
935 m_aNextPB
.Enable( sal_True
);
937 else if( m_pCurrentPage
== m_pCommandPage
)
939 m_pCurrentPage
= m_pChooseDriverPage
;
941 else if( m_pCurrentPage
== m_pOldPrinterPage
)
943 m_pCurrentPage
= m_pChooseDevicePage
;
944 m_aPrevPB
.Enable( sal_False
);
945 m_aNextPB
.Enable( sal_True
);
947 else if( m_pCurrentPage
== m_pFaxDriverPage
)
949 m_pCurrentPage
= m_pChooseDevicePage
;
950 m_aPrevPB
.Enable( sal_False
);
952 else if( m_pCurrentPage
== m_pFaxSelectDriverPage
)
954 m_pCurrentPage
= m_pFaxDriverPage
;
956 else if( m_pCurrentPage
== m_pFaxNamePage
)
958 m_pCurrentPage
= m_pFaxCommandPage
;
959 m_aNextPB
.Enable( sal_True
);
961 else if( m_pCurrentPage
== m_pFaxCommandPage
)
963 m_pCurrentPage
= m_pFaxDriverPage
->isDefault() ? (APTabPage
*)m_pFaxDriverPage
: (APTabPage
*)m_pFaxSelectDriverPage
;
964 m_aNextPB
.Enable( sal_True
);
966 else if( m_pCurrentPage
== m_pPdfDriverPage
)
968 m_pCurrentPage
= m_pChooseDevicePage
;
969 m_aPrevPB
.Enable( sal_False
);
971 else if( m_pCurrentPage
== m_pPdfSelectDriverPage
)
973 m_pCurrentPage
= m_pPdfDriverPage
;
975 else if( m_pCurrentPage
== m_pPdfNamePage
)
977 m_pCurrentPage
= m_pPdfCommandPage
;
978 m_aNextPB
.Enable( sal_True
);
980 else if( m_pCurrentPage
== m_pPdfCommandPage
)
982 m_pCurrentPage
= m_pPdfDriverPage
->isDefault() || m_pPdfDriverPage
->isDist() ? (APTabPage
*)m_pPdfDriverPage
: (APTabPage
*)m_pPdfSelectDriverPage
;
983 m_aNextPB
.Enable( sal_True
);
985 m_pCurrentPage
->Show( sal_True
);
986 m_aTitleImage
.SetText( m_pCurrentPage
->getTitle() );
989 void AddPrinterDialog::addPrinter()
991 PrinterInfoManager
& rManager( PrinterInfoManager::get() );
992 if( ! m_pChooseDevicePage
->isOld() )
994 m_aPrinter
.m_aPrinterName
= uniquePrinterName( m_aPrinter
.m_aPrinterName
);
995 if( rManager
.addPrinter( m_aPrinter
.m_aPrinterName
, m_aPrinter
.m_aDriverName
) )
997 PrinterInfo
aInfo( rManager
.getPrinterInfo( m_aPrinter
.m_aPrinterName
) );
998 aInfo
.m_aCommand
= m_aPrinter
.m_aCommand
;
999 if( m_pChooseDevicePage
->isPrinter() )
1001 if( m_pNamePage
->isDefault() )
1002 rManager
.setDefaultPrinter( m_aPrinter
.m_aPrinterName
);
1004 else if( m_pChooseDevicePage
->isFax() )
1006 aInfo
.m_aFeatures
= OUString("fax=");
1007 if( m_pFaxNamePage
->isFaxSwallow() )
1008 aInfo
.m_aFeatures
+= "swallow";
1010 else if( m_pChooseDevicePage
->isPDF() )
1012 OUString
aPdf( "pdf=" );
1013 aPdf
+= m_pPdfCommandPage
->getPdfDir();
1014 aInfo
.m_aFeatures
= aPdf
;
1016 rManager
.changePrinterInfo( m_aPrinter
.m_aPrinterName
, aInfo
);
1019 else if( m_pOldPrinterPage
)
1020 m_pOldPrinterPage
->addOldPrinters();
1023 IMPL_LINK( AddPrinterDialog
, ClickBtnHdl
, PushButton
*, pButton
)
1025 if( pButton
== &m_aNextPB
)
1027 if( m_pCurrentPage
->check() )
1029 m_pCurrentPage
->fill( m_aPrinter
);
1033 else if( pButton
== &m_aPrevPB
)
1035 if( m_pCurrentPage
->check() )
1036 m_pCurrentPage
->fill( m_aPrinter
);
1039 else if( pButton
== &m_aFinishPB
)
1041 if( m_pCurrentPage
->check() )
1043 m_pCurrentPage
->fill( m_aPrinter
);
1045 PrinterInfoManager::get().writePrinterConfig();
1049 else if( pButton
== &m_aCancelPB
)
1055 OUString
AddPrinterDialog::uniquePrinterName( const OUString
& rBase
)
1057 OUString
aResult( rBase
);
1059 PrinterInfoManager
& rManager( PrinterInfoManager::get() );
1061 sal_Int32 nVersion
= 1;
1062 list
< OUString
> aPrinterList
;
1063 rManager
.listPrinters( aPrinterList
);
1064 boost::unordered_set
< OUString
, OUStringHash
> aPrinters
;
1065 for( list
< OUString
>::const_iterator it
= aPrinterList
.begin(); it
!= aPrinterList
.end(); ++it
)
1066 aPrinters
.insert( *it
);
1067 while( aPrinters
.find( aResult
) != aPrinters
.end() )
1071 aResult
+= OUString::valueOf(nVersion
++);
1077 String
AddPrinterDialog::getOldPrinterLocation()
1079 static const char* pHome
= getenv( "HOME" );
1082 rtl_TextEncoding aEncoding
= osl_getThreadTextEncoding();
1085 aFileName
= OStringBuffer().append(pHome
).
1086 append("/.Xpdefaults").makeStringAndClear();
1087 if (access(aFileName
.getStr(), F_OK
))
1089 aFileName
= OStringBuffer().append(pHome
).
1090 append("/.sversionrc").makeStringAndClear();
1091 Config
aSVer(OStringToOUString(aFileName
, aEncoding
));
1092 aSVer
.SetGroup( "Versions" );
1093 aFileName
= aSVer
.ReadKey( "StarOffice 5.2" );
1094 if (!aFileName
.isEmpty())
1095 aFileName
= aFileName
+ OString("/share/xp3/Xpdefaults");
1097 (aFileName
= aSVer
.ReadKey( "StarOffice 5.1" ) ).getLength()
1099 (aFileName
= aSVer
.ReadKey( "StarOffice 5.0" ) ).getLength()
1101 (aFileName
= aSVer
.ReadKey( "StarOffice 4.0" ) ).getLength()
1104 aFileName
= aFileName
+ OString("/xp3/Xpdefaults");
1106 if (!aFileName
.isEmpty() && access(aFileName
.getStr(), F_OK
))
1107 aFileName
= OString();
1111 return !aFileName
.isEmpty() ? OStringToOUString(aFileName
, aEncoding
) : OUString();
1114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */