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