1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include <stdio.h> 29 #include <unistd.h> 30 31 #include "helper.hxx" 32 #include "padialog.hrc" 33 #include "newppdlg.hxx" 34 #include "padialog.hxx" 35 #include "progress.hxx" 36 37 #include "vcl/ppdparser.hxx" 38 #include "vcl/helper.hxx" 39 #include "vcl/svapp.hxx" 40 #include "vcl/mnemonic.hxx" 41 42 #include "tools/urlobj.hxx" 43 44 #include "osl/file.hxx" 45 46 #include <list> 47 48 #define PPDIMPORT_GROUP "PPDImport" 49 50 using namespace padmin; 51 using namespace psp; 52 using namespace osl; 53 using namespace rtl; 54 55 PPDImportDialog::PPDImportDialog( Window* pParent ) : 56 ModalDialog( pParent, PaResId( RID_PPDIMPORT_DLG ) ), 57 m_aOKBtn( this, PaResId( RID_PPDIMP_BTN_OK ) ), 58 m_aCancelBtn( this, PaResId( RID_PPDIMP_BTN_CANCEL ) ), 59 m_aPathTxt( this, PaResId( RID_PPDIMP_TXT_PATH ) ), 60 m_aPathBox( this, PaResId( RID_PPDIMP_LB_PATH ) ), 61 m_aSearchBtn( this, PaResId( RID_PPDIMP_BTN_SEARCH ) ), 62 m_aDriverTxt( this, PaResId( RID_PPDIMP_TXT_DRIVER ) ), 63 m_aDriverLB( this, PaResId( RID_PPDIMP_LB_DRIVER ) ), 64 m_aPathGroup( this, PaResId( RID_PPDIMP_GROUP_PATH ) ), 65 m_aDriverGroup( this, PaResId( RID_PPDIMP_GROUP_DRIVER ) ), 66 m_aLoadingPPD( PaResId( RID_PPDIMP_STR_LOADINGPPD ) ) 67 { 68 FreeResource(); 69 70 String aText( m_aDriverTxt.GetText() ); 71 aText.SearchAndReplaceAscii( "%s", Button::GetStandardText( BUTTON_OK ) ); 72 m_aDriverTxt.SetText( MnemonicGenerator::EraseAllMnemonicChars( aText ) ); 73 74 Config& rConfig = getPadminRC(); 75 rConfig.SetGroup( PPDIMPORT_GROUP ); 76 m_aPathBox.SetText( String( rConfig.ReadKey( "LastDir" ), RTL_TEXTENCODING_UTF8 ) ); 77 for( int i = 0; i < 11; i++ ) 78 { 79 ByteString aEntry( rConfig.ReadKey( ByteString::CreateFromInt32( i ) ) ); 80 if( aEntry.Len() ) 81 m_aPathBox.InsertEntry( String( aEntry, RTL_TEXTENCODING_UTF8 ) ); 82 } 83 84 m_aOKBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) ); 85 m_aCancelBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) ); 86 m_aSearchBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) ); 87 m_aPathBox.SetSelectHdl( LINK( this, PPDImportDialog, SelectHdl ) ); 88 m_aPathBox.SetModifyHdl( LINK( this, PPDImportDialog, ModifyHdl ) ); 89 90 if( m_aPathBox.GetText().Len() ) 91 Import(); 92 } 93 94 PPDImportDialog::~PPDImportDialog() 95 { 96 while( m_aDriverLB.GetEntryCount() ) 97 { 98 delete (String*)m_aDriverLB.GetEntryData( 0 ); 99 m_aDriverLB.RemoveEntry( 0 ); 100 } 101 } 102 103 void PPDImportDialog::Import() 104 { 105 String aImportPath( m_aPathBox.GetText() ); 106 107 Config& rConfig = getPadminRC(); 108 rConfig.SetGroup( PPDIMPORT_GROUP ); 109 rConfig.WriteKey( "LastDir", ByteString( aImportPath, RTL_TEXTENCODING_UTF8 ) ); 110 111 int nEntries = m_aPathBox.GetEntryCount(); 112 while( nEntries-- ) 113 if( aImportPath == m_aPathBox.GetEntry( nEntries ) ) 114 break; 115 if( nEntries < 0 ) 116 { 117 int nNextEntry = rConfig.ReadKey( "NextEntry" ).ToInt32(); 118 rConfig.WriteKey( ByteString::CreateFromInt32( nNextEntry ), ByteString( aImportPath, RTL_TEXTENCODING_UTF8 ) ); 119 nNextEntry = nNextEntry < 10 ? nNextEntry+1 : 0; 120 rConfig.WriteKey( "NextEntry", ByteString::CreateFromInt32( nNextEntry ) ); 121 m_aPathBox.InsertEntry( aImportPath ); 122 } 123 while( m_aDriverLB.GetEntryCount() ) 124 { 125 delete (String*)m_aDriverLB.GetEntryData( 0 ); 126 m_aDriverLB.RemoveEntry( 0 ); 127 } 128 129 ProgressDialog aProgress( Application::GetFocusWindow() ); 130 aProgress.startOperation( m_aLoadingPPD ); 131 132 ::std::list< String > aFiles; 133 FindFiles( aImportPath, aFiles, String( RTL_CONSTASCII_USTRINGPARAM( "PS;PPD;PS.GZ;PPD.GZ" ) ), true ); 134 135 int i = 0; 136 aProgress.setRange( 0, aFiles.size() ); 137 while( aFiles.size() ) 138 { 139 aProgress.setValue( ++i ); 140 aProgress.setFilename( aFiles.front() ); 141 INetURLObject aPath( aImportPath, INET_PROT_FILE, INetURLObject::ENCODE_ALL ); 142 aPath.Append( aFiles.front() ); 143 String aPrinterName = PPDParser::getPPDPrinterName( aPath.PathToFileName() ); 144 aFiles.pop_front(); 145 146 if( ! aPrinterName.Len() ) 147 { 148 #if OSL_DEBUG_LEVEL > 1 149 fprintf( stderr, "Warning: File %s has empty printer name.\n", 150 rtl::OUStringToOString( aPath.PathToFileName(), osl_getThreadTextEncoding() ).getStr() ); 151 #endif 152 continue; 153 } 154 155 sal_uInt16 nPos = m_aDriverLB.InsertEntry( aPrinterName ); 156 m_aDriverLB.SetEntryData( nPos, new String( aPath.PathToFileName() ) ); 157 } 158 } 159 160 IMPL_LINK( PPDImportDialog, ClickBtnHdl, PushButton*, pButton ) 161 { 162 if( pButton == &m_aCancelBtn ) 163 { 164 EndDialog( 0 ); 165 } 166 else if( pButton == &m_aOKBtn ) 167 { 168 // copy the files 169 ::std::list< rtl::OUString > aToDirs; 170 psp::getPrinterPathList( aToDirs, PRINTER_PPDDIR ); 171 ::std::list< rtl::OUString >::iterator writeDir = aToDirs.begin(); 172 m_aImportedFiles.clear(); 173 174 for( int i = 0; i < m_aDriverLB.GetSelectEntryCount(); i++ ) 175 { 176 INetURLObject aFile( *(String*)m_aDriverLB.GetEntryData( 177 m_aDriverLB.GetSelectEntryPos( i ) 178 ), INET_PROT_FILE, INetURLObject::ENCODE_ALL ); 179 OUString aFromUni( aFile.GetMainURL(INetURLObject::DECODE_TO_IURI) ); 180 181 do 182 { 183 INetURLObject aToFile( *writeDir, INET_PROT_FILE, INetURLObject::ENCODE_ALL ); 184 aToFile.Append( aFile.GetName() ); 185 OUString aToUni( aToFile.GetMainURL(INetURLObject::DECODE_TO_IURI) ); 186 if( ! File::copy( aFromUni, aToUni ) ) 187 { 188 m_aImportedFiles.push_back( aToUni ); 189 break; 190 } 191 ++writeDir; 192 } while( writeDir != aToDirs.end() ); 193 } 194 EndDialog( 1 ); 195 } 196 else if( pButton == &m_aSearchBtn ) 197 { 198 String aPath( m_aPathBox.GetText() ); 199 if( chooseDirectory( aPath ) ) 200 { 201 m_aPathBox.SetText( aPath ); 202 Import(); 203 } 204 } 205 return 0; 206 } 207 208 IMPL_LINK( PPDImportDialog, SelectHdl, ComboBox*, pListBox ) 209 { 210 if( pListBox == &m_aPathBox ) 211 { 212 Import(); 213 } 214 return 0; 215 } 216 217 IMPL_LINK( PPDImportDialog, ModifyHdl, ComboBox*, pListBox ) 218 { 219 if( pListBox == &m_aPathBox ) 220 { 221 ByteString aDir( m_aPathBox.GetText(), osl_getThreadTextEncoding() ); 222 if( ! access( aDir.GetBuffer(), F_OK ) ) 223 Import(); 224 } 225 return 0; 226 } 227