merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / config / defaultoptions.cxx
blob9707ae4ddab3011a72e26db852888539dbd4100c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: defaultoptions.cxx,v $
10 * $Revision: 1.15 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include <svtools/defaultoptions.hxx>
35 #include <svtools/pathoptions.hxx>
36 #include <unotools/configitem.hxx>
37 #include <unotools/configmgr.hxx>
38 #include <tools/debug.hxx>
39 #include <tools/resmgr.hxx>
40 #include <tools/urlobj.hxx>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
43 #include <osl/mutex.hxx>
45 #include <vos/process.hxx>
46 #include <unotools/localfilehelper.hxx>
47 #include <rtl/instance.hxx>
49 #include <itemholder1.hxx>
51 using namespace osl;
52 using namespace utl;
53 using namespace rtl;
54 using namespace com::sun::star::uno;
56 // define ----------------------------------------------------------------
58 #define ASCII_STR(s) OUString( RTL_CONSTASCII_USTRINGPARAM(s) )
60 #define DEFAULTPATH__ADDIN 0
61 #define DEFAULTPATH__AUTOCORRECT 1
62 #define DEFAULTPATH__AUTOTEXT 2
63 #define DEFAULTPATH__BACKUP 3
64 #define DEFAULTPATH__BASIC 4
65 #define DEFAULTPATH__BITMAP 5
66 #define DEFAULTPATH__CONFIG 6
67 #define DEFAULTPATH__DICTIONARY 7
68 #define DEFAULTPATH__FAVORITES 8
69 #define DEFAULTPATH__FILTER 9
70 #define DEFAULTPATH__GALLERY 10
71 #define DEFAULTPATH__GRAPHIC 11
72 #define DEFAULTPATH__HELP 12
73 #define DEFAULTPATH__LINGUISTIC 13
74 #define DEFAULTPATH__MODULE 14
75 #define DEFAULTPATH__PALETTE 15
76 #define DEFAULTPATH__PLUGIN 16
77 #define DEFAULTPATH__TEMP 17
78 #define DEFAULTPATH__TEMPLATE 18
79 #define DEFAULTPATH__USERCONFIG 19
80 #define DEFAULTPATH__WORK 20
81 #define DEFAULTPATH__USERDICTIONARY 21
83 // class SvtDefaultOptions_Impl ------------------------------------------
85 class SvtDefaultOptions_Impl : public utl::ConfigItem
87 public:
88 String m_aAddinPath;
89 String m_aAutoCorrectPath;
90 String m_aAutoTextPath;
91 String m_aBackupPath;
92 String m_aBasicPath;
93 String m_aBitmapPath;
94 String m_aConfigPath;
95 String m_aDictionaryPath;
96 String m_aFavoritesPath;
97 String m_aFilterPath;
98 String m_aGalleryPath;
99 String m_aGraphicPath;
100 String m_aHelpPath;
101 String m_aLinguisticPath;
102 String m_aModulePath;
103 String m_aPalettePath;
104 String m_aPluginPath;
105 String m_aTempPath;
106 String m_aTemplatePath;
107 String m_aUserConfigPath;
108 String m_aWorkPath;
109 String m_aUserDictionaryPath;
111 SvtDefaultOptions_Impl();
113 String GetDefaultPath( USHORT nId ) const;
116 // global ----------------------------------------------------------------
118 static SvtDefaultOptions_Impl* pOptions = NULL;
119 static sal_Int32 nRefCount = 0;
121 typedef String SvtDefaultOptions_Impl:: *PathStrPtr;
123 struct PathToDefaultMapping_Impl
125 SvtPathOptions::Pathes _ePath;
126 PathStrPtr _pDefaultPath;
129 static PathToDefaultMapping_Impl __READONLY_DATA PathMap_Impl[] =
131 { SvtPathOptions::PATH_ADDIN, &SvtDefaultOptions_Impl::m_aAddinPath },
132 { SvtPathOptions::PATH_AUTOCORRECT, &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
133 { SvtPathOptions::PATH_AUTOTEXT, &SvtDefaultOptions_Impl::m_aAutoTextPath },
134 { SvtPathOptions::PATH_BACKUP, &SvtDefaultOptions_Impl::m_aBackupPath },
135 { SvtPathOptions::PATH_BASIC, &SvtDefaultOptions_Impl::m_aBasicPath },
136 { SvtPathOptions::PATH_BITMAP, &SvtDefaultOptions_Impl::m_aBitmapPath },
137 { SvtPathOptions::PATH_CONFIG, &SvtDefaultOptions_Impl::m_aConfigPath },
138 { SvtPathOptions::PATH_DICTIONARY, &SvtDefaultOptions_Impl::m_aDictionaryPath },
139 { SvtPathOptions::PATH_FAVORITES, &SvtDefaultOptions_Impl::m_aFavoritesPath },
140 { SvtPathOptions::PATH_FILTER, &SvtDefaultOptions_Impl::m_aFilterPath },
141 { SvtPathOptions::PATH_GALLERY, &SvtDefaultOptions_Impl::m_aGalleryPath },
142 { SvtPathOptions::PATH_GRAPHIC, &SvtDefaultOptions_Impl::m_aGraphicPath },
143 { SvtPathOptions::PATH_HELP, &SvtDefaultOptions_Impl::m_aHelpPath },
144 { SvtPathOptions::PATH_LINGUISTIC, &SvtDefaultOptions_Impl::m_aLinguisticPath },
145 { SvtPathOptions::PATH_MODULE, &SvtDefaultOptions_Impl::m_aModulePath },
146 { SvtPathOptions::PATH_PALETTE, &SvtDefaultOptions_Impl::m_aPalettePath },
147 { SvtPathOptions::PATH_PLUGIN, &SvtDefaultOptions_Impl::m_aPluginPath },
148 { SvtPathOptions::PATH_TEMP, &SvtDefaultOptions_Impl::m_aTempPath },
149 { SvtPathOptions::PATH_TEMPLATE, &SvtDefaultOptions_Impl::m_aTemplatePath },
150 { SvtPathOptions::PATH_USERCONFIG, &SvtDefaultOptions_Impl::m_aUserConfigPath },
151 { SvtPathOptions::PATH_WORK, &SvtDefaultOptions_Impl::m_aWorkPath }
154 // functions -------------------------------------------------------------
156 Sequence< OUString > GetDefaultPropertyNames()
158 static const char* aPropNames[] =
160 "Addin", // PATH_ADDIN
161 "AutoCorrect", // PATH_AUTOCORRECT
162 "AutoText", // PATH_AUTOTEXT
163 "Backup", // PATH_BACKUP
164 "Basic", // PATH_BASIC
165 "Bitmap", // PATH_BITMAP
166 "Config", // PATH_CONFIG
167 "Dictionary", // PATH_DICTIONARY
168 "Favorite", // PATH_FAVORITES
169 "Filter", // PATH_FILTER
170 "Gallery", // PATH_GALLERY
171 "Graphic", // PATH_GRAPHIC
172 "Help", // PATH_HELP
173 "Linguistic", // PATH_LINGUISTIC
174 "Module", // PATH_MODULE
175 "Palette", // PATH_PALETTE
176 "Plugin", // PATH_PLUGIN
177 "Temp", // PATH_TEMP
178 "Template", // PATH_TEMPLATE
179 "UserConfig", // PATH_USERCONFIG
180 "Work" // PATH_WORK
183 const int nCount = sizeof( aPropNames ) / sizeof( const char* );
184 Sequence< OUString > aNames( nCount );
185 OUString* pNames = aNames.getArray();
186 for ( int i = 0; i < nCount; i++ )
187 pNames[i] = OUString::createFromAscii( aPropNames[i] );
189 return aNames;
192 // class SvtDefaultOptions_Impl ------------------------------------------
194 String SvtDefaultOptions_Impl::GetDefaultPath( USHORT nId ) const
196 String aRet;
197 USHORT nIdx = 0;
199 while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_WORK )
201 if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
203 aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
204 if ( nId == SvtPathOptions::PATH_ADDIN ||
205 nId == SvtPathOptions::PATH_FILTER ||
206 nId == SvtPathOptions::PATH_HELP ||
207 nId == SvtPathOptions::PATH_MODULE ||
208 nId == SvtPathOptions::PATH_PLUGIN )
210 String aTmp;
211 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aRet, aTmp );
212 aRet = aTmp;
215 break;
217 ++nIdx;
220 return aRet;
223 // -----------------------------------------------------------------------
225 SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( ASCII_STR("Office.Common/Path/Default") )
227 /*ConfigManager* pCfgMgr =*/ ConfigManager::GetConfigManager();
228 Sequence< OUString > aNames = GetDefaultPropertyNames();
229 Sequence< Any > aValues = GetProperties( aNames );
230 EnableNotification( aNames );
231 const Any* pValues = aValues.getConstArray();
232 DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
233 if ( aValues.getLength() == aNames.getLength() )
235 SvtPathOptions aPathOpt;
236 OUString aTempStr, aFullPath;
238 for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
240 if ( pValues[nProp].hasValue() )
242 switch ( pValues[nProp].getValueTypeClass() )
244 case ::com::sun::star::uno::TypeClass_STRING :
246 // multi pathes
247 if ( pValues[nProp] >>= aTempStr )
248 aFullPath = aPathOpt.SubstituteVariable( aTempStr );
249 else
251 DBG_ERRORFILE( "any operator >>= failed" );
253 break;
256 case ::com::sun::star::uno::TypeClass_SEQUENCE :
258 // single pathes
259 aFullPath = OUString();
260 Sequence < OUString > aList;
261 if ( pValues[nProp] >>= aList )
263 sal_Int32 nCount = aList.getLength();
264 for ( sal_Int32 nPosition = 0; nPosition < nCount; ++nPosition )
266 aTempStr = aPathOpt.SubstituteVariable( aList[ nPosition ] );
267 aFullPath += aTempStr;
268 if ( nPosition < nCount-1 )
269 aFullPath += OUString( RTL_CONSTASCII_USTRINGPARAM(";") );
272 else
274 DBG_ERRORFILE( "any operator >>= failed" );
276 break;
279 default:
281 DBG_ERRORFILE( "Wrong any type" );
285 switch ( nProp )
287 case DEFAULTPATH__ADDIN: m_aAddinPath = String( aFullPath ); break;
288 case DEFAULTPATH__AUTOCORRECT: m_aAutoCorrectPath = String( aFullPath ); break;
289 case DEFAULTPATH__AUTOTEXT: m_aAutoTextPath = String( aFullPath ); break;
290 case DEFAULTPATH__BACKUP: m_aBackupPath = String( aFullPath ); break;
291 case DEFAULTPATH__BASIC: m_aBasicPath = String( aFullPath ); break;
292 case DEFAULTPATH__BITMAP: m_aBitmapPath = String( aFullPath ); break;
293 case DEFAULTPATH__CONFIG: m_aConfigPath = String( aFullPath ); break;
294 case DEFAULTPATH__DICTIONARY: m_aDictionaryPath = String( aFullPath ); break;
295 case DEFAULTPATH__FAVORITES: m_aFavoritesPath = String( aFullPath ); break;
296 case DEFAULTPATH__FILTER: m_aFilterPath = String( aFullPath ); break;
297 case DEFAULTPATH__GALLERY: m_aGalleryPath = String( aFullPath ); break;
298 case DEFAULTPATH__GRAPHIC: m_aGraphicPath = String( aFullPath ); break;
299 case DEFAULTPATH__HELP: m_aHelpPath = String( aFullPath ); break;
300 case DEFAULTPATH__LINGUISTIC: m_aLinguisticPath = String( aFullPath ); break;
301 case DEFAULTPATH__MODULE: m_aModulePath = String( aFullPath ); break;
302 case DEFAULTPATH__PALETTE: m_aPalettePath = String( aFullPath ); break;
303 case DEFAULTPATH__PLUGIN: m_aPluginPath = String( aFullPath ); break;
304 case DEFAULTPATH__TEMP: m_aTempPath = String( aFullPath ); break;
305 case DEFAULTPATH__TEMPLATE: m_aTemplatePath = String( aFullPath ); break;
306 case DEFAULTPATH__USERCONFIG: m_aUserConfigPath = String( aFullPath ); break;
307 case DEFAULTPATH__WORK: m_aWorkPath = String( aFullPath ); break;
308 case DEFAULTPATH__USERDICTIONARY: m_aUserDictionaryPath = String( aFullPath );break;
310 default:
311 DBG_ERRORFILE( "invalid index to load a default path" );
318 // class SvtDefaultOptions -----------------------------------------------
319 namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }
321 SvtDefaultOptions::SvtDefaultOptions()
323 // Global access, must be guarded (multithreading)
324 ::osl::MutexGuard aGuard( lclMutex::get() );
325 if ( !pOptions )
327 pOptions = new SvtDefaultOptions_Impl;
328 ItemHolder1::holdConfigItem(E_DEFAULTOPTIONS);
330 ++nRefCount;
331 pImp = pOptions;
334 // -----------------------------------------------------------------------
336 SvtDefaultOptions::~SvtDefaultOptions()
338 // Global access, must be guarded (multithreading)
339 ::osl::MutexGuard aGuard( lclMutex::get() );
340 if ( !--nRefCount )
342 if ( pOptions->IsModified() )
343 pOptions->Commit();
344 DELETEZ( pOptions );
348 // -----------------------------------------------------------------------
350 String SvtDefaultOptions::GetDefaultPath( USHORT nId ) const
352 return pImp->GetDefaultPath( nId );