merge the formfield patch from ooo-build
[ooovba.git] / padmin / source / newppdlg.cxx
blob1ef313c88f06a9db82be57498aade9433068bf27
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>
34 #include "helper.hxx"
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"
49 #include <list>
51 #define PPDIMPORT_GROUP "PPDImport"
53 using namespace padmin;
54 using namespace psp;
55 using namespace osl;
56 using namespace rtl;
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 ) )
71 FreeResource();
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 ) ) );
83 if( aEntry.Len() )
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() )
94 Import();
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();
115 while( nEntries-- )
116 if( aImportPath == m_aPathBox.GetEntry( nEntries ) )
117 break;
118 if( nEntries < 0 )
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 );
138 int i = 0;
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() );
147 aFiles.pop_front();
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() );
154 #endif
155 continue;
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 )
167 EndDialog( 0 );
169 else if( pButton == &m_aOKBtn )
171 // copy the files
172 ::std::list< rtl::OUString > aToDirs;
173 psp::getPrinterPathList( aToDirs, PRINTER_PPDDIR );
174 ::std::list< rtl::OUString >::iterator writeDir = aToDirs.begin();
175 m_aImportedFiles.clear();
177 for( int i = 0; i < m_aDriverLB.GetSelectEntryCount(); i++ )
179 INetURLObject aFile( *(String*)m_aDriverLB.GetEntryData(
180 m_aDriverLB.GetSelectEntryPos( i )
181 ), INET_PROT_FILE, INetURLObject::ENCODE_ALL );
182 OUString aFromUni( aFile.GetMainURL(INetURLObject::DECODE_TO_IURI) );
186 INetURLObject aToFile( *writeDir, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
187 aToFile.Append( aFile.GetName() );
188 OUString aToUni( aToFile.GetMainURL(INetURLObject::DECODE_TO_IURI) );
189 if( ! File::copy( aFromUni, aToUni ) )
191 m_aImportedFiles.push_back( aToUni );
192 break;
194 ++writeDir;
195 } while( writeDir != aToDirs.end() );
197 EndDialog( 1 );
199 else if( pButton == &m_aSearchBtn )
201 String aPath( m_aPathBox.GetText() );
202 if( chooseDirectory( aPath ) )
204 m_aPathBox.SetText( aPath );
205 Import();
208 return 0;
211 IMPL_LINK( PPDImportDialog, SelectHdl, ComboBox*, pListBox )
213 if( pListBox == &m_aPathBox )
215 Import();
217 return 0;
220 IMPL_LINK( PPDImportDialog, ModifyHdl, ComboBox*, pListBox )
222 if( pListBox == &m_aPathBox )
224 ByteString aDir( m_aPathBox.GetText(), osl_getThreadTextEncoding() );
225 if( ! access( aDir.GetBuffer(), F_OK ) )
226 Import();
228 return 0;