update dev300-m58
[ooovba.git] / padmin / source / helper.cxx
blob52ba5d6e473a84ce3d37716370fbe992d65845b0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: helper.cxx,v $
10 * $Revision: 1.24 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <unistd.h>
32 #include <helper.hxx>
33 #ifndef _PAD_PADIALOG_HRC_
34 #include <padialog.hrc>
35 #endif
36 #include <osl/file.hxx>
37 #include <tools/urlobj.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/msgbox.hxx>
40 #include <tools/config.hxx>
41 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
42 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
43 #include <com/sun/star/ui/dialogs/XControlAccess.hpp>
44 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
45 #include <comphelper/processfactory.hxx>
46 #include <tools/urlobj.hxx>
47 #include <unotools/confignode.hxx>
48 #include <vcl/unohelp.hxx>
49 #include <i18npool/mslangid.hxx>
50 #include <rtl/ustrbuf.hxx>
53 using namespace osl;
54 using namespace rtl;
55 using namespace padmin;
56 using namespace com::sun::star::uno;
57 using namespace com::sun::star::lang;
58 using namespace com::sun::star::ui::dialogs;
60 #define MAX_PATH 1024
63 * PaResId
66 ResId padmin::PaResId( sal_uInt32 nId )
68 static ResMgr* pPaResMgr = NULL;
69 if( ! pPaResMgr )
71 ::com::sun::star::lang::Locale aLocale;
72 // LanguageType nLang = LANGUAGE_SYSTEM;
74 utl::OConfigurationNode aNode =
75 utl::OConfigurationTreeRoot::tryCreateWithServiceFactory(
76 vcl::unohelper::GetMultiServiceFactory(),
77 OUString::createFromAscii( "org.openoffice.Setup/L10N" ) );
78 if ( aNode.isValid() )
80 rtl::OUString aLoc;
81 Any aValue = aNode.getNodeValue( OUString::createFromAscii( "ooLocale" ) );
82 if( aValue >>= aLoc )
84 // LanguageType nTmpLang = MsLangId::convertIsoStringToLanguage( aLoc );
85 // if( nTmpLang != LANGUAGE_DONTKNOW )
86 // nLang = nTmpLang;
87 sal_Int32 nIndex = 0;
88 aLocale.Language = aLoc.getToken( 0, '-', nIndex );
89 aLocale.Country = aLoc.getToken( 0, '-', nIndex );
90 aLocale.Variant = aLoc.getToken( 0, '-', nIndex );
93 pPaResMgr = ResMgr::SearchCreateResMgr( "spa", aLocale );
94 AllSettings aSettings = Application::GetSettings();
95 // aSettings.SetUILanguage( nLang );
96 aSettings.SetUILocale( aLocale );
97 Application::SetSettings( aSettings );
99 return ResId( nId, *pPaResMgr );
103 * FindFiles
106 void padmin::FindFiles( const String& rDirectory, ::std::list< String >& rResult, const String& rSuffixes, bool bRecursive )
108 rResult.clear();
110 OUString aDirPath;
111 ::osl::FileBase::getFileURLFromSystemPath( rDirectory, aDirPath );
112 Directory aDir( aDirPath );
113 if( aDir.open() != FileBase::E_None )
114 return;
115 DirectoryItem aItem;
116 while( aDir.getNextItem( aItem ) == FileBase::E_None )
118 FileStatus aStatus( FileStatusMask_FileName |
119 FileStatusMask_Type
121 if( aItem.getFileStatus( aStatus ) == FileBase::E_None )
123 if( aStatus.getFileType() == FileStatus::Regular ||
124 aStatus.getFileType() == FileStatus::Link )
126 String aFileName = aStatus.getFileName();
127 int nToken = rSuffixes.GetTokenCount( ';' );
128 while( nToken-- )
130 String aSuffix = rSuffixes.GetToken( nToken, ';' );
131 if( aFileName.Len() > aSuffix.Len()+1 )
133 String aExtension = aFileName.Copy( aFileName.Len()-aSuffix.Len() );
134 if( aFileName.GetChar( aFileName.Len()-aSuffix.Len()-1 ) == '.' &&
135 aExtension.EqualsIgnoreCaseAscii( aSuffix ) )
137 rResult.push_back( aFileName );
138 break;
143 else if( bRecursive && aStatus.getFileType() == FileStatus::Directory )
145 OUStringBuffer aSubDir( rDirectory );
146 aSubDir.appendAscii( "/", 1 );
147 aSubDir.append( aStatus.getFileName() );
148 std::list< String > subfiles;
149 FindFiles( aSubDir.makeStringAndClear(), subfiles, rSuffixes, bRecursive );
150 for( std::list< String >::const_iterator it = subfiles.begin(); it != subfiles.end(); ++it )
152 OUStringBuffer aSubFile( aStatus.getFileName() );
153 aSubFile.appendAscii( "/", 1 );
154 aSubFile.append( *it );
155 rResult.push_back( aSubFile.makeStringAndClear() );
160 aDir.close();
164 * DelMultiListBox
167 long DelMultiListBox::Notify( NotifyEvent& rEvent )
169 long nRet = 0;
171 if( rEvent.GetType() == EVENT_KEYINPUT &&
172 rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_DELETE )
174 m_aDelPressedLink.Call( this );
175 nRet = 1;
177 else
178 nRet = MultiListBox::Notify( rEvent );
180 return nRet;
184 * DelListBox
187 long DelListBox::Notify( NotifyEvent& rEvent )
189 long nRet = 0;
191 if( rEvent.GetType() == EVENT_KEYINPUT &&
192 rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_DELETE )
194 m_aDelPressedLink.Call( this );
195 nRet = 1;
197 else
198 nRet = ListBox::Notify( rEvent );
200 return nRet;
204 * QueryString
207 QueryString::QueryString( Window* pParent, String& rQuery, String& rRet, const ::std::list< String >& rChoices ) :
208 ModalDialog( pParent, PaResId( RID_STRINGQUERYDLG ) ),
209 m_aOKButton( this, PaResId( RID_STRQRY_BTN_OK ) ),
210 m_aCancelButton( this, PaResId( RID_STRQRY_BTN_CANCEL ) ),
211 m_aFixedText( this, PaResId( RID_STRQRY_TXT_RENAME ) ),
212 m_aEdit( this, PaResId( RID_STRQRY_EDT_NEWNAME ) ),
213 m_aComboBox( this, PaResId( RID_STRQRY_BOX_NEWNAME ) ),
214 m_rReturnValue( rRet )
216 FreeResource();
217 m_aOKButton.SetClickHdl( LINK( this, QueryString, ClickBtnHdl ) );
218 m_aFixedText.SetText( rQuery );
219 if( rChoices.begin() != rChoices.end() )
221 m_aComboBox.SetText( m_rReturnValue );
222 m_aComboBox.InsertEntry( m_rReturnValue );
223 for( ::std::list<String>::const_iterator it = rChoices.begin(); it != rChoices.end(); ++it )
224 m_aComboBox.InsertEntry( *it );
225 m_aEdit.Show( FALSE );
226 m_bUseEdit = false;
228 else
230 m_aEdit.SetText( m_rReturnValue );
231 m_aComboBox.Show( FALSE );
232 m_bUseEdit = true;
234 SetText( Application::GetDisplayName() );
237 QueryString::~QueryString()
241 IMPL_LINK( QueryString, ClickBtnHdl, Button*, pButton )
243 if( pButton == &m_aOKButton )
245 m_rReturnValue = m_bUseEdit ? m_aEdit.GetText() : m_aComboBox.GetText();
246 EndDialog( 1 );
248 else
249 EndDialog(0);
250 return 0;
254 * AreYouSure
257 BOOL padmin::AreYouSure( Window* pParent, int nRid )
259 if( nRid == -1 )
260 nRid = RID_YOU_SURE;
261 QueryBox aQueryBox( pParent, WB_YES_NO | WB_DEF_NO,
262 String( PaResId( nRid ) ) );
263 return aQueryBox.Execute() == RET_NO ? FALSE : TRUE;
267 * getPadminRC
270 static Config* pRC = NULL;
272 Config& padmin::getPadminRC()
274 if( ! pRC )
276 static const char* pEnv = getenv( "HOME" );
277 String aFileName( pEnv ? pEnv : "", osl_getThreadTextEncoding() );
278 aFileName.AppendAscii( "/.padminrc" );
279 pRC = new Config( aFileName );
281 return *pRC;
284 void padmin::freePadminRC()
286 if( pRC )
287 delete pRC, pRC = NULL;
290 bool padmin::chooseDirectory( String& rInOutPath )
292 bool bRet = false;
293 Reference< XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
294 if( xFactory.is() )
296 Reference< XFolderPicker > xFolderPicker( xFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FolderPicker" ) ) ), UNO_QUERY );
297 if( xFolderPicker.is() )
299 Reference< XControlAccess > xCA( xFolderPicker, UNO_QUERY );
300 if( xCA.is() )
304 Any aState;
305 aState <<= sal_False;
306 xCA->setControlProperty( OUString( RTL_CONSTASCII_USTRINGPARAM( "HelpButton" ) ),
307 OUString( RTL_CONSTASCII_USTRINGPARAM( "Visible" ) ),
308 aState );
311 catch( ... )
315 INetURLObject aObj( rInOutPath, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
316 xFolderPicker->setDisplayDirectory( aObj.GetMainURL(INetURLObject::DECODE_TO_IURI) );
317 if( xFolderPicker->execute() == ExecutableDialogResults::OK )
319 aObj = INetURLObject( xFolderPicker->getDirectory() );
320 rInOutPath = aObj.PathToFileName();
321 bRet = true;
324 #if OSL_DEBUG_LEVEL > 1
325 else
326 fprintf( stderr, "could not get FolderPicker service\n" );
327 #endif
329 return bRet;