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