xref: /aoo42x/main/padmin/source/helper.cxx (revision b8c015e0)
1466d5a0bSAndrew Rist /**************************************************************
2*b8c015e0Smseidel  *
3466d5a0bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4466d5a0bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5466d5a0bSAndrew Rist  * distributed with this work for additional information
6466d5a0bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7466d5a0bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8466d5a0bSAndrew Rist  * "License"); you may not use this file except in compliance
9466d5a0bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b8c015e0Smseidel  *
11466d5a0bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b8c015e0Smseidel  *
13466d5a0bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14466d5a0bSAndrew Rist  * software distributed under the License is distributed on an
15466d5a0bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16466d5a0bSAndrew Rist  * KIND, either express or implied.  See the License for the
17466d5a0bSAndrew Rist  * specific language governing permissions and limitations
18466d5a0bSAndrew Rist  * under the License.
19*b8c015e0Smseidel  *
20466d5a0bSAndrew Rist  *************************************************************/
21466d5a0bSAndrew Rist 
22466d5a0bSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <unistd.h>
25cdf0e10cSrcweir #include <helper.hxx>
26cdf0e10cSrcweir #ifndef _PAD_PADIALOG_HRC_
27cdf0e10cSrcweir #include <padialog.hrc>
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #include <osl/file.hxx>
30cdf0e10cSrcweir #include <tools/urlobj.hxx>
31cdf0e10cSrcweir #include <vcl/svapp.hxx>
32cdf0e10cSrcweir #include <vcl/msgbox.hxx>
33cdf0e10cSrcweir #include <tools/config.hxx>
34cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
35cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
36cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XControlAccess.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
39cdf0e10cSrcweir #include <tools/urlobj.hxx>
40cdf0e10cSrcweir #include <unotools/confignode.hxx>
41cdf0e10cSrcweir #include <vcl/unohelp.hxx>
42cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
43cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace osl;
47cdf0e10cSrcweir using namespace rtl;
48cdf0e10cSrcweir using namespace padmin;
49cdf0e10cSrcweir using namespace com::sun::star::uno;
50cdf0e10cSrcweir using namespace com::sun::star::lang;
51cdf0e10cSrcweir using namespace com::sun::star::ui::dialogs;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #define MAX_PATH 1024
54cdf0e10cSrcweir 
55cdf0e10cSrcweir /*
56cdf0e10cSrcweir  *	PaResId
57cdf0e10cSrcweir  */
58cdf0e10cSrcweir 
PaResId(sal_uInt32 nId)59cdf0e10cSrcweir ResId padmin::PaResId( sal_uInt32 nId )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 	static ResMgr* pPaResMgr = NULL;
62cdf0e10cSrcweir 	if( ! pPaResMgr )
63cdf0e10cSrcweir 	{
64*b8c015e0Smseidel 		::com::sun::star::lang::Locale aLocale;
65cdf0e10cSrcweir //		LanguageType nLang = LANGUAGE_SYSTEM;
66*b8c015e0Smseidel 
67*b8c015e0Smseidel 		utl::OConfigurationNode aNode =
68*b8c015e0Smseidel 			utl::OConfigurationTreeRoot::tryCreateWithServiceFactory(
69*b8c015e0Smseidel 					vcl::unohelper::GetMultiServiceFactory(),
70*b8c015e0Smseidel 					OUString::createFromAscii( "org.openoffice.Setup/L10N" ) );
71*b8c015e0Smseidel 		if ( aNode.isValid() )
72*b8c015e0Smseidel 		{
73*b8c015e0Smseidel 			rtl::OUString aLoc;
74*b8c015e0Smseidel 			Any aValue = aNode.getNodeValue( OUString::createFromAscii( "ooLocale" ) );
75*b8c015e0Smseidel 			if( aValue >>= aLoc )
76*b8c015e0Smseidel 			{
77*b8c015e0Smseidel //			LanguageType nTmpLang = MsLangId::convertIsoStringToLanguage( aLoc );
78*b8c015e0Smseidel //			if( nTmpLang != LANGUAGE_DONTKNOW )
79*b8c015e0Smseidel //				nLang = nTmpLang;
80*b8c015e0Smseidel 				sal_Int32 nIndex = 0;
81*b8c015e0Smseidel 				aLocale.Language = aLoc.getToken( 0, '-', nIndex );
82*b8c015e0Smseidel 				aLocale.Country = aLoc.getToken( 0, '-', nIndex );
83*b8c015e0Smseidel 				aLocale.Variant = aLoc.getToken( 0, '-', nIndex );
84*b8c015e0Smseidel 			}
85*b8c015e0Smseidel 		}
86cdf0e10cSrcweir 		pPaResMgr = ResMgr::SearchCreateResMgr( "spa", aLocale );
87cdf0e10cSrcweir 		AllSettings aSettings = Application::GetSettings();
88*b8c015e0Smseidel //		aSettings.SetUILanguage( nLang );
89*b8c015e0Smseidel 		aSettings.SetUILocale( aLocale );
90*b8c015e0Smseidel 		Application::SetSettings( aSettings );
91cdf0e10cSrcweir 	}
92cdf0e10cSrcweir 	return ResId( nId, *pPaResMgr );
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir /*
96cdf0e10cSrcweir  *	FindFiles
97cdf0e10cSrcweir  */
98cdf0e10cSrcweir 
FindFiles(const String & rDirectory,::std::list<String> & rResult,const String & rSuffixes,bool bRecursive)99cdf0e10cSrcweir void padmin::FindFiles( const String& rDirectory, ::std::list< String >& rResult, const String& rSuffixes, bool bRecursive )
100cdf0e10cSrcweir {
101*b8c015e0Smseidel 	rResult.clear();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	OUString aDirPath;
104cdf0e10cSrcweir 	::osl::FileBase::getFileURLFromSystemPath( rDirectory, aDirPath );
105cdf0e10cSrcweir 	Directory aDir( aDirPath );
106cdf0e10cSrcweir 	if( aDir.open() != FileBase::E_None )
107*b8c015e0Smseidel 		return;
108cdf0e10cSrcweir 	DirectoryItem aItem;
109cdf0e10cSrcweir 	while( aDir.getNextItem( aItem ) == FileBase::E_None )
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		FileStatus aStatus( FileStatusMask_FileName			|
112cdf0e10cSrcweir 							FileStatusMask_Type
113cdf0e10cSrcweir 							);
114cdf0e10cSrcweir 		if( aItem.getFileStatus( aStatus ) == FileBase::E_None )
115*b8c015e0Smseidel 		{
116*b8c015e0Smseidel 			if( aStatus.getFileType() == FileStatus::Regular ||
117*b8c015e0Smseidel 				aStatus.getFileType() == FileStatus::Link )
118*b8c015e0Smseidel 			{
119*b8c015e0Smseidel 				String aFileName = aStatus.getFileName();
120*b8c015e0Smseidel 				int nToken = rSuffixes.GetTokenCount( ';' );
121*b8c015e0Smseidel 				while( nToken-- )
122*b8c015e0Smseidel 				{
123*b8c015e0Smseidel 					String aSuffix = rSuffixes.GetToken( nToken, ';' );
124*b8c015e0Smseidel 					if( aFileName.Len() > aSuffix.Len()+1 )
125*b8c015e0Smseidel 					{
126*b8c015e0Smseidel 						String aExtension = aFileName.Copy( aFileName.Len()-aSuffix.Len() );
127*b8c015e0Smseidel 						if( aFileName.GetChar( aFileName.Len()-aSuffix.Len()-1 ) == '.' &&
128*b8c015e0Smseidel 							aExtension.EqualsIgnoreCaseAscii( aSuffix ) )
129*b8c015e0Smseidel 						{
130*b8c015e0Smseidel 							rResult.push_back( aFileName );
131*b8c015e0Smseidel 							break;
132*b8c015e0Smseidel 						}
133*b8c015e0Smseidel 					}
134*b8c015e0Smseidel 				}
135*b8c015e0Smseidel 			}
136*b8c015e0Smseidel 			else if( bRecursive && aStatus.getFileType() == FileStatus::Directory )
137*b8c015e0Smseidel 			{
138*b8c015e0Smseidel 				OUStringBuffer aSubDir( rDirectory );
139*b8c015e0Smseidel 				aSubDir.appendAscii( "/", 1 );
140*b8c015e0Smseidel 				aSubDir.append( aStatus.getFileName() );
141*b8c015e0Smseidel 				std::list< String > subfiles;
142*b8c015e0Smseidel 				FindFiles( aSubDir.makeStringAndClear(), subfiles, rSuffixes, bRecursive );
143*b8c015e0Smseidel 				for( std::list< String >::const_iterator it = subfiles.begin(); it != subfiles.end(); ++it )
144*b8c015e0Smseidel 				{
145*b8c015e0Smseidel 					OUStringBuffer aSubFile( aStatus.getFileName() );
146*b8c015e0Smseidel 					aSubFile.appendAscii( "/", 1 );
147*b8c015e0Smseidel 					aSubFile.append( *it );
148*b8c015e0Smseidel 					rResult.push_back( aSubFile.makeStringAndClear() );
149*b8c015e0Smseidel 				}
150*b8c015e0Smseidel 			}
151*b8c015e0Smseidel 		}
152cdf0e10cSrcweir 	}
153cdf0e10cSrcweir 	aDir.close();
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir /*
157cdf0e10cSrcweir  *	DelMultiListBox
158cdf0e10cSrcweir  */
159cdf0e10cSrcweir 
Notify(NotifyEvent & rEvent)160cdf0e10cSrcweir long DelMultiListBox::Notify( NotifyEvent& rEvent )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir 	long nRet = 0;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	if( rEvent.GetType() == EVENT_KEYINPUT &&
165cdf0e10cSrcweir 		rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_DELETE )
166cdf0e10cSrcweir 	{
167cdf0e10cSrcweir 		m_aDelPressedLink.Call( this );
168cdf0e10cSrcweir 		nRet = 1;
169cdf0e10cSrcweir 	}
170*b8c015e0Smseidel 	else
171*b8c015e0Smseidel 		nRet = MultiListBox::Notify( rEvent );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 	return nRet;
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir /*
177cdf0e10cSrcweir  *	DelListBox
178cdf0e10cSrcweir  */
179cdf0e10cSrcweir 
Notify(NotifyEvent & rEvent)180cdf0e10cSrcweir long DelListBox::Notify( NotifyEvent& rEvent )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir 	long nRet = 0;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	if( rEvent.GetType() == EVENT_KEYINPUT &&
185cdf0e10cSrcweir 		rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_DELETE )
186cdf0e10cSrcweir 	{
187cdf0e10cSrcweir 		m_aDelPressedLink.Call( this );
188cdf0e10cSrcweir 		nRet = 1;
189cdf0e10cSrcweir 	}
190*b8c015e0Smseidel 	else
191*b8c015e0Smseidel 		nRet = ListBox::Notify( rEvent );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	return nRet;
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir /*
197cdf0e10cSrcweir  *	QueryString
198cdf0e10cSrcweir  */
199cdf0e10cSrcweir 
QueryString(Window * pParent,String & rQuery,String & rRet,const::std::list<String> & rChoices)200cdf0e10cSrcweir QueryString::QueryString( Window* pParent, String& rQuery, String& rRet, const ::std::list< String >& rChoices ) :
201cdf0e10cSrcweir 		ModalDialog( pParent, PaResId( RID_STRINGQUERYDLG ) ),
202cdf0e10cSrcweir 		m_aOKButton( this, PaResId( RID_STRQRY_BTN_OK ) ),
203cdf0e10cSrcweir 		m_aCancelButton( this, PaResId( RID_STRQRY_BTN_CANCEL ) ),
204cdf0e10cSrcweir 		m_aFixedText( this, PaResId( RID_STRQRY_TXT_RENAME ) ),
205cdf0e10cSrcweir 		m_aEdit( this, PaResId( RID_STRQRY_EDT_NEWNAME ) ),
206*b8c015e0Smseidel 		m_aComboBox( this, PaResId( RID_STRQRY_BOX_NEWNAME ) ),
207cdf0e10cSrcweir 		m_rReturnValue( rRet )
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	FreeResource();
210cdf0e10cSrcweir 	m_aOKButton.SetClickHdl( LINK( this, QueryString, ClickBtnHdl ) );
211cdf0e10cSrcweir 	m_aFixedText.SetText( rQuery );
212*b8c015e0Smseidel 	if( rChoices.begin() != rChoices.end() )
213*b8c015e0Smseidel 	{
214*b8c015e0Smseidel 		m_aComboBox.SetText( m_rReturnValue );
215*b8c015e0Smseidel 		m_aComboBox.InsertEntry( m_rReturnValue );
216*b8c015e0Smseidel 		for( ::std::list<String>::const_iterator it = rChoices.begin(); it != rChoices.end(); ++it )
217*b8c015e0Smseidel 			m_aComboBox.InsertEntry( *it );
218*b8c015e0Smseidel 		m_aEdit.Show( sal_False );
219*b8c015e0Smseidel 		m_bUseEdit = false;
220*b8c015e0Smseidel 	}
221*b8c015e0Smseidel 	else
222*b8c015e0Smseidel 	{
223*b8c015e0Smseidel 		m_aEdit.SetText( m_rReturnValue );
224*b8c015e0Smseidel 		m_aComboBox.Show( sal_False );
225*b8c015e0Smseidel 		m_bUseEdit = true;
226*b8c015e0Smseidel 	}
227cdf0e10cSrcweir 	SetText( Application::GetDisplayName() );
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
~QueryString()230cdf0e10cSrcweir QueryString::~QueryString()
231cdf0e10cSrcweir {
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
IMPL_LINK(QueryString,ClickBtnHdl,Button *,pButton)234cdf0e10cSrcweir IMPL_LINK( QueryString, ClickBtnHdl, Button*, pButton )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir 	if( pButton == &m_aOKButton )
237cdf0e10cSrcweir 	{
238cdf0e10cSrcweir 		m_rReturnValue = m_bUseEdit ? m_aEdit.GetText() : m_aComboBox.GetText();
239cdf0e10cSrcweir 		EndDialog( 1 );
240cdf0e10cSrcweir 	}
241cdf0e10cSrcweir 	else
242cdf0e10cSrcweir 		EndDialog(0);
243cdf0e10cSrcweir 	return 0;
244cdf0e10cSrcweir }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir /*
247cdf0e10cSrcweir  *	AreYouSure
248cdf0e10cSrcweir  */
249cdf0e10cSrcweir 
AreYouSure(Window * pParent,int nRid)250cdf0e10cSrcweir sal_Bool padmin::AreYouSure( Window* pParent, int nRid )
251cdf0e10cSrcweir {
252cdf0e10cSrcweir 	if( nRid == -1 )
253cdf0e10cSrcweir 		nRid = RID_YOU_SURE;
254cdf0e10cSrcweir 	QueryBox aQueryBox( pParent, WB_YES_NO | WB_DEF_NO,
255cdf0e10cSrcweir 						String( PaResId( nRid ) ) );
256cdf0e10cSrcweir 	return aQueryBox.Execute() == RET_NO ? sal_False : sal_True;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir /*
260cdf0e10cSrcweir  *	getPadminRC
261cdf0e10cSrcweir  */
262cdf0e10cSrcweir 
263cdf0e10cSrcweir static Config* pRC = NULL;
264cdf0e10cSrcweir 
getPadminRC()265cdf0e10cSrcweir Config& padmin::getPadminRC()
266cdf0e10cSrcweir {
267cdf0e10cSrcweir 	if( ! pRC )
268cdf0e10cSrcweir 	{
269cdf0e10cSrcweir 		static const char* pEnv = getenv( "HOME" );
270cdf0e10cSrcweir 		String aFileName( pEnv ? pEnv : "", osl_getThreadTextEncoding() );
271cdf0e10cSrcweir 		aFileName.AppendAscii( "/.padminrc" );
272cdf0e10cSrcweir 		pRC = new Config( aFileName );
273cdf0e10cSrcweir 	}
274cdf0e10cSrcweir 	return *pRC;
275cdf0e10cSrcweir }
276cdf0e10cSrcweir 
freePadminRC()277cdf0e10cSrcweir void padmin::freePadminRC()
278cdf0e10cSrcweir {
279cdf0e10cSrcweir 	if( pRC )
280cdf0e10cSrcweir 		delete pRC, pRC = NULL;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
chooseDirectory(String & rInOutPath)283cdf0e10cSrcweir bool padmin::chooseDirectory( String& rInOutPath )
284cdf0e10cSrcweir {
285*b8c015e0Smseidel 	bool bRet = false;
286*b8c015e0Smseidel 	Reference< XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
287*b8c015e0Smseidel 	if( xFactory.is() )
288*b8c015e0Smseidel 	{
289*b8c015e0Smseidel 		Reference< XFolderPicker > xFolderPicker( xFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FolderPicker" ) ) ), UNO_QUERY );
290*b8c015e0Smseidel 		if( xFolderPicker.is() )
291*b8c015e0Smseidel 		{
292*b8c015e0Smseidel 			Reference< XControlAccess > xCA( xFolderPicker, UNO_QUERY );
293*b8c015e0Smseidel 			if( xCA.is() )
294*b8c015e0Smseidel 			{
295*b8c015e0Smseidel 				try
296*b8c015e0Smseidel 				{
297*b8c015e0Smseidel 					Any aState;
298*b8c015e0Smseidel 					aState <<= sal_False;
299*b8c015e0Smseidel 					xCA->setControlProperty( OUString( RTL_CONSTASCII_USTRINGPARAM( "HelpButton" ) ),
300*b8c015e0Smseidel 											 OUString( RTL_CONSTASCII_USTRINGPARAM( "Visible" ) ),
301*b8c015e0Smseidel 											 aState );
302*b8c015e0Smseidel 
303*b8c015e0Smseidel 				}
304*b8c015e0Smseidel 				catch( ... )
305*b8c015e0Smseidel 				{
306*b8c015e0Smseidel 				}
307*b8c015e0Smseidel 			}
308*b8c015e0Smseidel 			INetURLObject aObj( rInOutPath, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
309*b8c015e0Smseidel 			xFolderPicker->setDisplayDirectory( aObj.GetMainURL(INetURLObject::DECODE_TO_IURI) );
310*b8c015e0Smseidel 			if( xFolderPicker->execute() == ExecutableDialogResults::OK )
311*b8c015e0Smseidel 			{
312*b8c015e0Smseidel 				aObj = INetURLObject( xFolderPicker->getDirectory() );
313*b8c015e0Smseidel 				rInOutPath = aObj.PathToFileName();
314*b8c015e0Smseidel 				bRet = true;
315*b8c015e0Smseidel 			}
316*b8c015e0Smseidel 		}
317cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
318*b8c015e0Smseidel 		else
319*b8c015e0Smseidel 			fprintf( stderr, "could not get FolderPicker service\n" );
320cdf0e10cSrcweir #endif
321*b8c015e0Smseidel 	}
322*b8c015e0Smseidel 	return bRet;
323cdf0e10cSrcweir }
324*b8c015e0Smseidel 
325*b8c015e0Smseidel /* vim: set noet sw=4 ts=4: */
326