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