Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / padmin / source / helper.cxx
blob54ff53726059151b61ad3e44d2b6047e4f2e8624
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <unistd.h>
21 #include <helper.hxx>
22 #include <padialog.hrc>
23 #include <osl/file.hxx>
24 #include <tools/urlobj.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/msgbox.hxx>
27 #include <tools/config.hxx>
28 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
29 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
30 #include <com/sun/star/ui/dialogs/XControlAccess.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <comphelper/processfactory.hxx>
33 #include <comphelper/string.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <tools/urlobj.hxx>
36 #include <unotools/confignode.hxx>
37 #include <i18npool/mslangid.hxx>
38 #include <rtl/ustrbuf.hxx>
41 using namespace osl;
42 using namespace padmin;
43 using namespace com::sun::star::uno;
44 using namespace com::sun::star::lang;
45 using namespace com::sun::star::ui::dialogs;
47 using ::rtl::OUString;
48 using ::rtl::OUStringBuffer;
50 #define MAX_PATH 1024
53 * PaResId
56 ResId padmin::PaResId( sal_uInt32 nId )
58 static ResMgr* pPaResMgr = NULL;
59 if( ! pPaResMgr )
61 ::com::sun::star::lang::Locale aLocale;
63 utl::OConfigurationNode aNode =
64 utl::OConfigurationTreeRoot::tryCreateWithComponentContext(
65 comphelper::getProcessComponentContext(),
66 OUString("org.openoffice.Setup/L10N") );
67 if ( aNode.isValid() )
69 rtl::OUString aLoc;
70 Any aValue = aNode.getNodeValue( OUString("ooLocale") );
71 if( aValue >>= aLoc )
73 /* FIXME-BCP47: handle language tags! */
74 sal_Int32 nIndex = 0;
75 aLocale.Language = aLoc.getToken( 0, '-', nIndex );
76 aLocale.Country = aLoc.getToken( 0, '-', nIndex );
77 aLocale.Variant = aLoc.getToken( 0, '-', nIndex );
80 pPaResMgr = ResMgr::SearchCreateResMgr( "spa", aLocale );
81 AllSettings aSettings = Application::GetSettings();
82 aSettings.SetUILanguageTag( LanguageTag( aLocale) );
83 Application::SetSettings( aSettings );
85 return ResId( nId, *pPaResMgr );
89 * FindFiles
92 void padmin::FindFiles( const String& rDirectory, ::std::list< String >& rResult, const String& rSuffixes, bool bRecursive )
94 rResult.clear();
96 OUString aDirPath;
97 ::osl::FileBase::getFileURLFromSystemPath( rDirectory, aDirPath );
98 Directory aDir( aDirPath );
99 if( aDir.open() != FileBase::E_None )
100 return;
101 DirectoryItem aItem;
102 while( aDir.getNextItem( aItem ) == FileBase::E_None )
104 FileStatus aStatus( osl_FileStatus_Mask_FileName |
105 osl_FileStatus_Mask_Type
107 if( aItem.getFileStatus( aStatus ) == FileBase::E_None )
109 if( aStatus.getFileType() == FileStatus::Regular ||
110 aStatus.getFileType() == FileStatus::Link )
112 String aFileName = aStatus.getFileName();
113 int nToken = comphelper::string::getTokenCount(rSuffixes, ';');
114 while( nToken-- )
116 String aSuffix = rSuffixes.GetToken( nToken, ';' );
117 if( aFileName.Len() > aSuffix.Len()+1 )
119 String aExtension = aFileName.Copy( aFileName.Len()-aSuffix.Len() );
120 if( aFileName.GetChar( aFileName.Len()-aSuffix.Len()-1 ) == '.' &&
121 aExtension.EqualsIgnoreCaseAscii( aSuffix ) )
123 rResult.push_back( aFileName );
124 break;
129 else if( bRecursive && aStatus.getFileType() == FileStatus::Directory )
131 OUStringBuffer aSubDir( rDirectory );
132 aSubDir.appendAscii( "/", 1 );
133 aSubDir.append( aStatus.getFileName() );
134 std::list< String > subfiles;
135 FindFiles( aSubDir.makeStringAndClear(), subfiles, rSuffixes, bRecursive );
136 for( std::list< String >::const_iterator it = subfiles.begin(); it != subfiles.end(); ++it )
138 OUStringBuffer aSubFile( aStatus.getFileName() );
139 aSubFile.appendAscii( "/", 1 );
140 aSubFile.append( *it );
141 rResult.push_back( aSubFile.makeStringAndClear() );
146 aDir.close();
150 * DelMultiListBox
153 long DelMultiListBox::Notify( NotifyEvent& rEvent )
155 long nRet = 0;
157 if( rEvent.GetType() == EVENT_KEYINPUT &&
158 rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_DELETE )
160 m_aDelPressedLink.Call( this );
161 nRet = 1;
163 else
164 nRet = MultiListBox::Notify( rEvent );
166 return nRet;
170 * DelListBox
173 long DelListBox::Notify( NotifyEvent& rEvent )
175 long nRet = 0;
177 if( rEvent.GetType() == EVENT_KEYINPUT &&
178 rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_DELETE )
180 m_aDelPressedLink.Call( this );
181 nRet = 1;
183 else
184 nRet = ListBox::Notify( rEvent );
186 return nRet;
190 * QueryString
193 QueryString::QueryString( Window* pParent, String& rQuery, String& rRet, const ::std::list< String >& rChoices ) :
194 ModalDialog( pParent, PaResId( RID_STRINGQUERYDLG ) ),
195 m_aOKButton( this, PaResId( RID_STRQRY_BTN_OK ) ),
196 m_aCancelButton( this, PaResId( RID_STRQRY_BTN_CANCEL ) ),
197 m_aFixedText( this, PaResId( RID_STRQRY_TXT_RENAME ) ),
198 m_aEdit( this, PaResId( RID_STRQRY_EDT_NEWNAME ) ),
199 m_aComboBox( this, PaResId( RID_STRQRY_BOX_NEWNAME ) ),
200 m_rReturnValue( rRet )
202 FreeResource();
203 m_aOKButton.SetClickHdl( LINK( this, QueryString, ClickBtnHdl ) );
204 m_aFixedText.SetText( rQuery );
205 if( rChoices.begin() != rChoices.end() )
207 m_aComboBox.SetText( m_rReturnValue );
208 m_aComboBox.InsertEntry( m_rReturnValue );
209 for( ::std::list<String>::const_iterator it = rChoices.begin(); it != rChoices.end(); ++it )
210 m_aComboBox.InsertEntry( *it );
211 m_aEdit.Show( sal_False );
212 m_bUseEdit = false;
214 else
216 m_aEdit.SetText( m_rReturnValue );
217 m_aComboBox.Show( sal_False );
218 m_bUseEdit = true;
220 SetText( Application::GetDisplayName() );
223 QueryString::~QueryString()
227 IMPL_LINK( QueryString, ClickBtnHdl, Button*, pButton )
229 if( pButton == &m_aOKButton )
231 m_rReturnValue = m_bUseEdit ? m_aEdit.GetText() : m_aComboBox.GetText();
232 EndDialog( 1 );
234 else
235 EndDialog(0);
236 return 0;
240 * AreYouSure
243 sal_Bool padmin::AreYouSure( Window* pParent, int nRid )
245 if( nRid == -1 )
246 nRid = RID_YOU_SURE;
247 QueryBox aQueryBox( pParent, WB_YES_NO | WB_DEF_NO,
248 String( PaResId( nRid ) ) );
249 return aQueryBox.Execute() == RET_NO ? sal_False : sal_True;
253 * getPadminRC
256 static Config* pRC = NULL;
258 Config& padmin::getPadminRC()
260 if( ! pRC )
262 static const char* pEnv = getenv( "HOME" );
263 String aFileName( pEnv ? pEnv : "", osl_getThreadTextEncoding() );
264 aFileName.AppendAscii( "/.padminrc" );
265 pRC = new Config( aFileName );
267 return *pRC;
270 void padmin::freePadminRC()
272 if( pRC )
273 delete pRC, pRC = NULL;
276 bool padmin::chooseDirectory( String& rInOutPath )
278 bool bRet = false;
279 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
280 Reference< XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);;
281 Reference< XControlAccess > xCA( xFolderPicker, UNO_QUERY );
282 if( xCA.is() )
286 Any aState;
287 aState <<= sal_False;
288 xCA->setControlProperty( OUString( "HelpButton" ),
289 OUString( "Visible" ),
290 aState );
293 catch( ... )
297 INetURLObject aObj( rInOutPath, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
298 xFolderPicker->setDisplayDirectory( aObj.GetMainURL(INetURLObject::DECODE_TO_IURI) );
299 if( xFolderPicker->execute() == ExecutableDialogResults::OK )
301 aObj = INetURLObject( xFolderPicker->getDirectory() );
302 rInOutPath = aObj.PathToFileName();
303 bRet = true;
305 #if OSL_DEBUG_LEVEL > 1
306 else
307 fprintf( stderr, "could not get FolderPicker service\n" );
308 #endif
309 return bRet;
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */