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