1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: newppdlg.cxx,v $
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 ************************************************************************/
35 #include "padialog.hrc"
36 #include "newppdlg.hxx"
37 #include "padialog.hxx"
38 #include "progress.hxx"
40 #include "vcl/ppdparser.hxx"
41 #include "vcl/helper.hxx"
42 #include "vcl/svapp.hxx"
43 #include "vcl/mnemonic.hxx"
45 #include "tools/urlobj.hxx"
47 #include "osl/file.hxx"
51 #define PPDIMPORT_GROUP "PPDImport"
53 using namespace padmin
;
58 PPDImportDialog::PPDImportDialog( Window
* pParent
) :
59 ModalDialog( pParent
, PaResId( RID_PPDIMPORT_DLG
) ),
60 m_aOKBtn( this, PaResId( RID_PPDIMP_BTN_OK
) ),
61 m_aCancelBtn( this, PaResId( RID_PPDIMP_BTN_CANCEL
) ),
62 m_aPathTxt( this, PaResId( RID_PPDIMP_TXT_PATH
) ),
63 m_aPathBox( this, PaResId( RID_PPDIMP_LB_PATH
) ),
64 m_aSearchBtn( this, PaResId( RID_PPDIMP_BTN_SEARCH
) ),
65 m_aDriverTxt( this, PaResId( RID_PPDIMP_TXT_DRIVER
) ),
66 m_aDriverLB( this, PaResId( RID_PPDIMP_LB_DRIVER
) ),
67 m_aPathGroup( this, PaResId( RID_PPDIMP_GROUP_PATH
) ),
68 m_aDriverGroup( this, PaResId( RID_PPDIMP_GROUP_DRIVER
) ),
69 m_aLoadingPPD( PaResId( RID_PPDIMP_STR_LOADINGPPD
) )
73 String
aText( m_aDriverTxt
.GetText() );
74 aText
.SearchAndReplaceAscii( "%s", Button::GetStandardText( BUTTON_OK
) );
75 m_aDriverTxt
.SetText( MnemonicGenerator::EraseAllMnemonicChars( aText
) );
77 Config
& rConfig
= getPadminRC();
78 rConfig
.SetGroup( PPDIMPORT_GROUP
);
79 m_aPathBox
.SetText( String( rConfig
.ReadKey( "LastDir" ), RTL_TEXTENCODING_UTF8
) );
80 for( int i
= 0; i
< 11; i
++ )
82 ByteString
aEntry( rConfig
.ReadKey( ByteString::CreateFromInt32( i
) ) );
84 m_aPathBox
.InsertEntry( String( aEntry
, RTL_TEXTENCODING_UTF8
) );
87 m_aOKBtn
.SetClickHdl( LINK( this, PPDImportDialog
, ClickBtnHdl
) );
88 m_aCancelBtn
.SetClickHdl( LINK( this, PPDImportDialog
, ClickBtnHdl
) );
89 m_aSearchBtn
.SetClickHdl( LINK( this, PPDImportDialog
, ClickBtnHdl
) );
90 m_aPathBox
.SetSelectHdl( LINK( this, PPDImportDialog
, SelectHdl
) );
91 m_aPathBox
.SetModifyHdl( LINK( this, PPDImportDialog
, ModifyHdl
) );
93 if( m_aPathBox
.GetText().Len() )
97 PPDImportDialog::~PPDImportDialog()
99 while( m_aDriverLB
.GetEntryCount() )
101 delete (String
*)m_aDriverLB
.GetEntryData( 0 );
102 m_aDriverLB
.RemoveEntry( 0 );
106 void PPDImportDialog::Import()
108 String
aImportPath( m_aPathBox
.GetText() );
110 Config
& rConfig
= getPadminRC();
111 rConfig
.SetGroup( PPDIMPORT_GROUP
);
112 rConfig
.WriteKey( "LastDir", ByteString( aImportPath
, RTL_TEXTENCODING_UTF8
) );
114 int nEntries
= m_aPathBox
.GetEntryCount();
116 if( aImportPath
== m_aPathBox
.GetEntry( nEntries
) )
120 int nNextEntry
= rConfig
.ReadKey( "NextEntry" ).ToInt32();
121 rConfig
.WriteKey( ByteString::CreateFromInt32( nNextEntry
), ByteString( aImportPath
, RTL_TEXTENCODING_UTF8
) );
122 nNextEntry
= nNextEntry
< 10 ? nNextEntry
+1 : 0;
123 rConfig
.WriteKey( "NextEntry", ByteString::CreateFromInt32( nNextEntry
) );
124 m_aPathBox
.InsertEntry( aImportPath
);
126 while( m_aDriverLB
.GetEntryCount() )
128 delete (String
*)m_aDriverLB
.GetEntryData( 0 );
129 m_aDriverLB
.RemoveEntry( 0 );
132 ProgressDialog
aProgress( Application::GetFocusWindow() );
133 aProgress
.startOperation( m_aLoadingPPD
);
135 ::std::list
< String
> aFiles
;
136 FindFiles( aImportPath
, aFiles
, String( RTL_CONSTASCII_USTRINGPARAM( "PS;PPD;PS.GZ;PPD.GZ" ) ), true );
139 aProgress
.setRange( 0, aFiles
.size() );
140 while( aFiles
.size() )
142 aProgress
.setValue( ++i
);
143 aProgress
.setFilename( aFiles
.front() );
144 INetURLObject
aPath( aImportPath
, INET_PROT_FILE
, INetURLObject::ENCODE_ALL
);
145 aPath
.Append( aFiles
.front() );
146 String aPrinterName
= PPDParser::getPPDPrinterName( aPath
.PathToFileName() );
149 if( ! aPrinterName
.Len() )
151 #if OSL_DEBUG_LEVEL > 1
152 fprintf( stderr
, "Warning: File %s has empty printer name.\n",
153 rtl::OUStringToOString( aPath
.PathToFileName(), osl_getThreadTextEncoding() ).getStr() );
158 USHORT nPos
= m_aDriverLB
.InsertEntry( aPrinterName
);
159 m_aDriverLB
.SetEntryData( nPos
, new String( aPath
.PathToFileName() ) );
163 IMPL_LINK( PPDImportDialog
, ClickBtnHdl
, PushButton
*, pButton
)
165 if( pButton
== &m_aCancelBtn
)
169 else if( pButton
== &m_aOKBtn
)
172 ::std::list
< rtl::OUString
> aToDirs
;
173 psp::getPrinterPathList( aToDirs
, PRINTER_PPDDIR
);
174 ::std::list
< rtl::OUString
>::iterator writeDir
= aToDirs
.begin();
176 for( int i
= 0; i
< m_aDriverLB
.GetSelectEntryCount(); i
++ )
178 INetURLObject
aFile( *(String
*)m_aDriverLB
.GetEntryData(
179 m_aDriverLB
.GetSelectEntryPos( i
)
180 ), INET_PROT_FILE
, INetURLObject::ENCODE_ALL
);
181 OUString
aFromUni( aFile
.GetMainURL(INetURLObject::DECODE_TO_IURI
) );
185 INetURLObject
aToFile( *writeDir
, INET_PROT_FILE
, INetURLObject::ENCODE_ALL
);
186 aToFile
.Append( aFile
.GetName() );
187 OUString
aToUni( aToFile
.GetMainURL(INetURLObject::DECODE_TO_IURI
) );
188 if( ! File::copy( aFromUni
, aToUni
) )
191 } while( writeDir
!= aToDirs
.end() );
195 else if( pButton
== &m_aSearchBtn
)
197 String
aPath( m_aPathBox
.GetText() );
198 if( chooseDirectory( aPath
) )
200 m_aPathBox
.SetText( aPath
);
207 IMPL_LINK( PPDImportDialog
, SelectHdl
, ComboBox
*, pListBox
)
209 if( pListBox
== &m_aPathBox
)
216 IMPL_LINK( PPDImportDialog
, ModifyHdl
, ComboBox
*, pListBox
)
218 if( pListBox
== &m_aPathBox
)
220 ByteString
aDir( m_aPathBox
.GetText(), osl_getThreadTextEncoding() );
221 if( ! access( aDir
.GetBuffer(), F_OK
) )