sync master with lastest vba changes
[ooovba.git] / padmin / source / newppdlg.cxx
blob9652f46873ad383c7dfd12873c51e988755de1b7
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: newppdlg.cxx,v $
10 * $Revision: 1.16 $
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 <stdio.h>
32 #include <unistd.h>
33 #include <psprint/ppdparser.hxx>
34 #include <psprint/helper.hxx>
35 #include <vcl/svapp.hxx>
36 #include <vcl/mnemonic.hxx>
37 #include <tools/urlobj.hxx>
38 #ifndef __SGI_STL_LIST
39 #include <list>
40 #endif
41 #include <osl/file.hxx>
42 #include <helper.hxx>
43 #ifndef _PAD_PADIALOG_HRC_
44 #include <padialog.hrc>
45 #endif
46 #include <newppdlg.hxx>
47 #include <padialog.hxx>
48 #include <progress.hxx>
50 #define PPDIMPORT_GROUP "PPDImport"
52 using namespace padmin;
53 using namespace psp;
54 using namespace osl;
55 using namespace rtl;
57 PPDImportDialog::PPDImportDialog( Window* pParent ) :
58 ModalDialog( pParent, PaResId( RID_PPDIMPORT_DLG ) ),
59 m_aOKBtn( this, PaResId( RID_PPDIMP_BTN_OK ) ),
60 m_aCancelBtn( this, PaResId( RID_PPDIMP_BTN_CANCEL ) ),
61 m_aPathTxt( this, PaResId( RID_PPDIMP_TXT_PATH ) ),
62 m_aPathBox( this, PaResId( RID_PPDIMP_LB_PATH ) ),
63 m_aSearchBtn( this, PaResId( RID_PPDIMP_BTN_SEARCH ) ),
64 m_aDriverTxt( this, PaResId( RID_PPDIMP_TXT_DRIVER ) ),
65 m_aDriverLB( this, PaResId( RID_PPDIMP_LB_DRIVER ) ),
66 m_aPathGroup( this, PaResId( RID_PPDIMP_GROUP_PATH ) ),
67 m_aDriverGroup( this, PaResId( RID_PPDIMP_GROUP_DRIVER ) ),
68 m_aLoadingPPD( PaResId( RID_PPDIMP_STR_LOADINGPPD ) )
70 FreeResource();
72 String aText( m_aDriverTxt.GetText() );
73 aText.SearchAndReplaceAscii( "%s", Button::GetStandardText( BUTTON_OK ) );
74 m_aDriverTxt.SetText( MnemonicGenerator::EraseAllMnemonicChars( aText ) );
76 Config& rConfig = getPadminRC();
77 rConfig.SetGroup( PPDIMPORT_GROUP );
78 m_aPathBox.SetText( String( rConfig.ReadKey( "LastDir" ), RTL_TEXTENCODING_UTF8 ) );
79 for( int i = 0; i < 11; i++ )
81 ByteString aEntry( rConfig.ReadKey( ByteString::CreateFromInt32( i ) ) );
82 if( aEntry.Len() )
83 m_aPathBox.InsertEntry( String( aEntry, RTL_TEXTENCODING_UTF8 ) );
86 m_aOKBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
87 m_aCancelBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
88 m_aSearchBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
89 m_aPathBox.SetSelectHdl( LINK( this, PPDImportDialog, SelectHdl ) );
90 m_aPathBox.SetModifyHdl( LINK( this, PPDImportDialog, ModifyHdl ) );
92 if( m_aPathBox.GetText().Len() )
93 Import();
96 PPDImportDialog::~PPDImportDialog()
98 while( m_aDriverLB.GetEntryCount() )
100 delete (String*)m_aDriverLB.GetEntryData( 0 );
101 m_aDriverLB.RemoveEntry( 0 );
105 void PPDImportDialog::Import()
107 String aImportPath( m_aPathBox.GetText() );
109 Config& rConfig = getPadminRC();
110 rConfig.SetGroup( PPDIMPORT_GROUP );
111 rConfig.WriteKey( "LastDir", ByteString( aImportPath, RTL_TEXTENCODING_UTF8 ) );
113 int nEntries = m_aPathBox.GetEntryCount();
114 while( nEntries-- )
115 if( aImportPath == m_aPathBox.GetEntry( nEntries ) )
116 break;
117 if( nEntries < 0 )
119 int nNextEntry = rConfig.ReadKey( "NextEntry" ).ToInt32();
120 rConfig.WriteKey( ByteString::CreateFromInt32( nNextEntry ), ByteString( aImportPath, RTL_TEXTENCODING_UTF8 ) );
121 nNextEntry = nNextEntry < 10 ? nNextEntry+1 : 0;
122 rConfig.WriteKey( "NextEntry", ByteString::CreateFromInt32( nNextEntry ) );
123 m_aPathBox.InsertEntry( aImportPath );
125 while( m_aDriverLB.GetEntryCount() )
127 delete (String*)m_aDriverLB.GetEntryData( 0 );
128 m_aDriverLB.RemoveEntry( 0 );
131 ProgressDialog aProgress( Application::GetFocusWindow() );
132 aProgress.startOperation( m_aLoadingPPD );
134 ::std::list< String > aFiles;
135 FindFiles( aImportPath, aFiles, String( RTL_CONSTASCII_USTRINGPARAM( "PS;PPD;PS.GZ;PPD.GZ" ) ), true );
137 int i = 0;
138 aProgress.setRange( 0, aFiles.size() );
139 while( aFiles.size() )
141 aProgress.setValue( ++i );
142 aProgress.setFilename( aFiles.front() );
143 INetURLObject aPath( aImportPath, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
144 aPath.Append( aFiles.front() );
145 String aPrinterName = PPDParser::getPPDPrinterName( aPath.PathToFileName() );
146 aFiles.pop_front();
148 if( ! aPrinterName.Len() )
150 #if OSL_DEBUG_LEVEL > 1
151 fprintf( stderr, "Warning: File %s has empty printer name.\n",
152 rtl::OUStringToOString( aPath.PathToFileName(), osl_getThreadTextEncoding() ).getStr() );
153 #endif
154 continue;
157 USHORT nPos = m_aDriverLB.InsertEntry( aPrinterName );
158 m_aDriverLB.SetEntryData( nPos, new String( aPath.PathToFileName() ) );
162 IMPL_LINK( PPDImportDialog, ClickBtnHdl, PushButton*, pButton )
164 if( pButton == &m_aCancelBtn )
166 EndDialog( 0 );
168 else if( pButton == &m_aOKBtn )
170 // copy the files
171 ::std::list< rtl::OUString > aToDirs;
172 psp::getPrinterPathList( aToDirs, PRINTER_PPDDIR );
173 ::std::list< rtl::OUString >::iterator writeDir = aToDirs.begin();
175 for( int i = 0; i < m_aDriverLB.GetSelectEntryCount(); i++ )
177 INetURLObject aFile( *(String*)m_aDriverLB.GetEntryData(
178 m_aDriverLB.GetSelectEntryPos( i )
179 ), INET_PROT_FILE, INetURLObject::ENCODE_ALL );
180 OUString aFromUni( aFile.GetMainURL(INetURLObject::DECODE_TO_IURI) );
184 INetURLObject aToFile( *writeDir, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
185 aToFile.Append( aFile.GetName() );
186 OUString aToUni( aToFile.GetMainURL(INetURLObject::DECODE_TO_IURI) );
187 if( ! File::copy( aFromUni, aToUni ) )
188 break;
189 ++writeDir;
190 } while( writeDir != aToDirs.end() );
192 EndDialog( 1 );
194 else if( pButton == &m_aSearchBtn )
196 String aPath( m_aPathBox.GetText() );
197 if( chooseDirectory( aPath ) )
199 m_aPathBox.SetText( aPath );
200 Import();
203 return 0;
206 IMPL_LINK( PPDImportDialog, SelectHdl, ComboBox*, pListBox )
208 if( pListBox == &m_aPathBox )
210 Import();
212 return 0;
215 IMPL_LINK( PPDImportDialog, ModifyHdl, ComboBox*, pListBox )
217 if( pListBox == &m_aPathBox )
219 ByteString aDir( m_aPathBox.GetText(), osl_getThreadTextEncoding() );
220 if( ! access( aDir.GetBuffer(), F_OK ) )
221 Import();
223 return 0;