xref: /aoo4110/main/padmin/source/newppdlg.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <stdio.h>
25 #include <unistd.h>
26 
27 #include "helper.hxx"
28 #include "padialog.hrc"
29 #include "newppdlg.hxx"
30 #include "padialog.hxx"
31 #include "progress.hxx"
32 
33 #include "vcl/ppdparser.hxx"
34 #include "vcl/helper.hxx"
35 #include "vcl/svapp.hxx"
36 #include "vcl/mnemonic.hxx"
37 
38 #include "tools/urlobj.hxx"
39 
40 #include "osl/file.hxx"
41 
42 #include <list>
43 
44 #define PPDIMPORT_GROUP "PPDImport"
45 
46 using namespace padmin;
47 using namespace psp;
48 using namespace osl;
49 using namespace rtl;
50 
PPDImportDialog(Window * pParent)51 PPDImportDialog::PPDImportDialog( Window* pParent ) :
52 		ModalDialog( pParent, PaResId( RID_PPDIMPORT_DLG ) ),
53 		m_aOKBtn( this, PaResId( RID_PPDIMP_BTN_OK ) ),
54 		m_aCancelBtn( this, PaResId( RID_PPDIMP_BTN_CANCEL ) ),
55 		m_aPathTxt( this, PaResId( RID_PPDIMP_TXT_PATH ) ),
56 		m_aPathBox( this, PaResId( RID_PPDIMP_LB_PATH ) ),
57 		m_aSearchBtn( this, PaResId( RID_PPDIMP_BTN_SEARCH ) ),
58 		m_aDriverTxt( this, PaResId( RID_PPDIMP_TXT_DRIVER ) ),
59 		m_aDriverLB( this, PaResId( RID_PPDIMP_LB_DRIVER ) ),
60 		m_aPathGroup( this, PaResId( RID_PPDIMP_GROUP_PATH ) ),
61 		m_aDriverGroup( this, PaResId( RID_PPDIMP_GROUP_DRIVER ) ),
62 		m_aLoadingPPD( PaResId( RID_PPDIMP_STR_LOADINGPPD ) )
63 {
64 	FreeResource();
65 
66 	String aText( m_aDriverTxt.GetText() );
67 	aText.SearchAndReplaceAscii( "%s", Button::GetStandardText( BUTTON_OK ) );
68 	m_aDriverTxt.SetText( MnemonicGenerator::EraseAllMnemonicChars( aText ) );
69 
70 	Config& rConfig = getPadminRC();
71 	rConfig.SetGroup( PPDIMPORT_GROUP );
72 	m_aPathBox.SetText( String( rConfig.ReadKey( "LastDir" ), RTL_TEXTENCODING_UTF8 ) );
73 	for( int i = 0; i < 11; i++ )
74 	{
75 		ByteString aEntry( rConfig.ReadKey( ByteString::CreateFromInt32( i ) ) );
76 		if( aEntry.Len() )
77 			m_aPathBox.InsertEntry( String( aEntry, RTL_TEXTENCODING_UTF8 ) );
78 	}
79 
80 	m_aOKBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
81 	m_aCancelBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
82 	m_aSearchBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
83 	m_aPathBox.SetSelectHdl( LINK( this, PPDImportDialog, SelectHdl ) );
84 	m_aPathBox.SetModifyHdl( LINK( this, PPDImportDialog, ModifyHdl ) );
85 
86     if( m_aPathBox.GetText().Len() )
87         Import();
88 }
89 
~PPDImportDialog()90 PPDImportDialog::~PPDImportDialog()
91 {
92 	while( m_aDriverLB.GetEntryCount() )
93 	{
94 		delete (String*)m_aDriverLB.GetEntryData( 0 );
95 		m_aDriverLB.RemoveEntry( 0 );
96 	}
97 }
98 
Import()99 void PPDImportDialog::Import()
100 {
101 	String aImportPath( m_aPathBox.GetText() );
102 
103 	Config& rConfig = getPadminRC();
104 	rConfig.SetGroup( PPDIMPORT_GROUP );
105 	rConfig.WriteKey( "LastDir", ByteString( aImportPath, RTL_TEXTENCODING_UTF8 ) );
106 
107 	int nEntries = m_aPathBox.GetEntryCount();
108 	while( nEntries-- )
109 		if( aImportPath == m_aPathBox.GetEntry( nEntries ) )
110 			break;
111 	if( nEntries < 0 )
112 	{
113 		int nNextEntry = rConfig.ReadKey( "NextEntry" ).ToInt32();
114 		rConfig.WriteKey( ByteString::CreateFromInt32( nNextEntry ), ByteString( aImportPath, RTL_TEXTENCODING_UTF8 ) );
115 		nNextEntry = nNextEntry < 10 ? nNextEntry+1 : 0;
116 		rConfig.WriteKey( "NextEntry", ByteString::CreateFromInt32( nNextEntry ) );
117 		m_aPathBox.InsertEntry( aImportPath );
118 	}
119 	while( m_aDriverLB.GetEntryCount() )
120 	{
121 		delete (String*)m_aDriverLB.GetEntryData( 0 );
122 		m_aDriverLB.RemoveEntry( 0 );
123 	}
124 
125 	ProgressDialog aProgress( Application::GetFocusWindow() );
126 	aProgress.startOperation( m_aLoadingPPD );
127 
128 	::std::list< String > aFiles;
129 	FindFiles( aImportPath, aFiles, String( RTL_CONSTASCII_USTRINGPARAM( "PS;PPD;PS.GZ;PPD.GZ" ) ), true );
130 
131 	int i = 0;
132 	aProgress.setRange( 0, aFiles.size() );
133 	while( aFiles.size() )
134 	{
135 		aProgress.setValue( ++i );
136 		aProgress.setFilename( aFiles.front() );
137 		INetURLObject aPath( aImportPath, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
138 		aPath.Append( aFiles.front() );
139 		String aPrinterName = PPDParser::getPPDPrinterName( aPath.PathToFileName() );
140 		aFiles.pop_front();
141 
142 		if( ! aPrinterName.Len() )
143 		{
144 #if OSL_DEBUG_LEVEL > 1
145 			fprintf( stderr, "Warning: File %s has empty printer name.\n",
146 					 rtl::OUStringToOString( aPath.PathToFileName(), osl_getThreadTextEncoding() ).getStr() );
147 #endif
148 			continue;
149 		}
150 
151 		sal_uInt16 nPos = m_aDriverLB.InsertEntry( aPrinterName );
152 		m_aDriverLB.SetEntryData( nPos, new String( aPath.PathToFileName() ) );
153 	}
154 }
155 
IMPL_LINK(PPDImportDialog,ClickBtnHdl,PushButton *,pButton)156 IMPL_LINK( PPDImportDialog, ClickBtnHdl, PushButton*, pButton )
157 {
158 	if( pButton == &m_aCancelBtn )
159 	{
160 		EndDialog( 0 );
161 	}
162 	else if( pButton == &m_aOKBtn )
163 	{
164 		// copy the files
165 		::std::list< rtl::OUString > aToDirs;
166 		psp::getPrinterPathList( aToDirs, PRINTER_PPDDIR );
167 		::std::list< rtl::OUString >::iterator writeDir = aToDirs.begin();
168         m_aImportedFiles.clear();
169 
170 		for( int i = 0; i < m_aDriverLB.GetSelectEntryCount(); i++ )
171 		{
172 			INetURLObject aFile( *(String*)m_aDriverLB.GetEntryData(
173 				m_aDriverLB.GetSelectEntryPos( i )
174 				), INET_PROT_FILE, INetURLObject::ENCODE_ALL );
175 			OUString aFromUni( aFile.GetMainURL(INetURLObject::DECODE_TO_IURI) );
176 
177 			do
178 			{
179 				INetURLObject aToFile( *writeDir, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
180 				aToFile.Append( aFile.GetName() );
181 				OUString aToUni( aToFile.GetMainURL(INetURLObject::DECODE_TO_IURI) );
182 				if( ! File::copy( aFromUni, aToUni ) )
183                 {
184                     m_aImportedFiles.push_back( aToUni );
185 					break;
186                 }
187 				++writeDir;
188 			} while( writeDir != aToDirs.end() );
189 		}
190 		EndDialog( 1 );
191 	}
192 	else if( pButton == &m_aSearchBtn )
193 	{
194         String aPath( m_aPathBox.GetText() );
195         if( chooseDirectory( aPath ) )
196 		{
197 			m_aPathBox.SetText( aPath );
198 			Import();
199 		}
200 	}
201 	return 0;
202 }
203 
IMPL_LINK(PPDImportDialog,SelectHdl,ComboBox *,pListBox)204 IMPL_LINK( PPDImportDialog, SelectHdl, ComboBox*, pListBox )
205 {
206 	if( pListBox == &m_aPathBox )
207 	{
208 		Import();
209 	}
210 	return 0;
211 }
212 
IMPL_LINK(PPDImportDialog,ModifyHdl,ComboBox *,pListBox)213 IMPL_LINK( PPDImportDialog, ModifyHdl, ComboBox*, pListBox )
214 {
215 	if( pListBox == &m_aPathBox )
216 	{
217 		ByteString aDir( m_aPathBox.GetText(), osl_getThreadTextEncoding() );
218 		if( ! access( aDir.GetBuffer(), F_OK ) )
219 			Import();
220 	}
221 	return 0;
222 }
223