bump product version to 4.1.6.2
[LibreOffice.git] / unotools / source / config / defaultoptions.cxx
blobb31176cd6ed0a3c7a6f72bc76e7c659c5192dbad
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 .
21 #include <unotools/defaultoptions.hxx>
22 #include <unotools/pathoptions.hxx>
23 #include <unotools/configitem.hxx>
24 #include <unotools/configmgr.hxx>
25 #include <tools/debug.hxx>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <osl/mutex.hxx>
30 #include <unotools/localfilehelper.hxx>
31 #include <rtl/instance.hxx>
33 #include <itemholder1.hxx>
35 using namespace osl;
36 using namespace utl;
37 using namespace com::sun::star::uno;
40 // define ----------------------------------------------------------------
42 #define DEFAULTPATH__ADDIN 0
43 #define DEFAULTPATH__AUTOCORRECT 1
44 #define DEFAULTPATH__AUTOTEXT 2
45 #define DEFAULTPATH__BACKUP 3
46 #define DEFAULTPATH__BASIC 4
47 #define DEFAULTPATH__BITMAP 5
48 #define DEFAULTPATH__CONFIG 6
49 #define DEFAULTPATH__DICTIONARY 7
50 #define DEFAULTPATH__FAVORITES 8
51 #define DEFAULTPATH__FILTER 9
52 #define DEFAULTPATH__GALLERY 10
53 #define DEFAULTPATH__GRAPHIC 11
54 #define DEFAULTPATH__HELP 12
55 #define DEFAULTPATH__LINGUISTIC 13
56 #define DEFAULTPATH__MODULE 14
57 #define DEFAULTPATH__PALETTE 15
58 #define DEFAULTPATH__PLUGIN 16
59 #define DEFAULTPATH__TEMP 17
60 #define DEFAULTPATH__TEMPLATE 18
61 #define DEFAULTPATH__USERCONFIG 19
62 #define DEFAULTPATH__WORK 20
63 #define DEFAULTPATH__USERDICTIONARY 21
65 // class SvtDefaultOptions_Impl ------------------------------------------
67 class SvtDefaultOptions_Impl : public utl::ConfigItem
69 public:
70 String m_aAddinPath;
71 String m_aAutoCorrectPath;
72 String m_aAutoTextPath;
73 String m_aBackupPath;
74 String m_aBasicPath;
75 String m_aBitmapPath;
76 String m_aConfigPath;
77 String m_aDictionaryPath;
78 String m_aFavoritesPath;
79 String m_aFilterPath;
80 String m_aGalleryPath;
81 String m_aGraphicPath;
82 String m_aHelpPath;
83 String m_aLinguisticPath;
84 String m_aModulePath;
85 String m_aPalettePath;
86 String m_aPluginPath;
87 String m_aTempPath;
88 String m_aTemplatePath;
89 String m_aUserConfigPath;
90 String m_aWorkPath;
91 String m_aUserDictionaryPath;
93 SvtDefaultOptions_Impl();
95 String GetDefaultPath( sal_uInt16 nId ) const;
96 virtual void Commit();
97 virtual void Notify( const com::sun::star::uno::Sequence<OUString>& aPropertyNames);
100 // global ----------------------------------------------------------------
102 static SvtDefaultOptions_Impl* pOptions = NULL;
103 static sal_Int32 nRefCount = 0;
105 typedef String SvtDefaultOptions_Impl:: *PathStrPtr;
107 struct PathToDefaultMapping_Impl
109 SvtPathOptions::Paths _ePath;
110 PathStrPtr _pDefaultPath;
113 static PathToDefaultMapping_Impl const PathMap_Impl[] =
115 { SvtPathOptions::PATH_ADDIN, &SvtDefaultOptions_Impl::m_aAddinPath },
116 { SvtPathOptions::PATH_AUTOCORRECT, &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
117 { SvtPathOptions::PATH_AUTOTEXT, &SvtDefaultOptions_Impl::m_aAutoTextPath },
118 { SvtPathOptions::PATH_BACKUP, &SvtDefaultOptions_Impl::m_aBackupPath },
119 { SvtPathOptions::PATH_BASIC, &SvtDefaultOptions_Impl::m_aBasicPath },
120 { SvtPathOptions::PATH_BITMAP, &SvtDefaultOptions_Impl::m_aBitmapPath },
121 { SvtPathOptions::PATH_CONFIG, &SvtDefaultOptions_Impl::m_aConfigPath },
122 { SvtPathOptions::PATH_DICTIONARY, &SvtDefaultOptions_Impl::m_aDictionaryPath },
123 { SvtPathOptions::PATH_FAVORITES, &SvtDefaultOptions_Impl::m_aFavoritesPath },
124 { SvtPathOptions::PATH_FILTER, &SvtDefaultOptions_Impl::m_aFilterPath },
125 { SvtPathOptions::PATH_GALLERY, &SvtDefaultOptions_Impl::m_aGalleryPath },
126 { SvtPathOptions::PATH_GRAPHIC, &SvtDefaultOptions_Impl::m_aGraphicPath },
127 { SvtPathOptions::PATH_HELP, &SvtDefaultOptions_Impl::m_aHelpPath },
128 { SvtPathOptions::PATH_LINGUISTIC, &SvtDefaultOptions_Impl::m_aLinguisticPath },
129 { SvtPathOptions::PATH_MODULE, &SvtDefaultOptions_Impl::m_aModulePath },
130 { SvtPathOptions::PATH_PALETTE, &SvtDefaultOptions_Impl::m_aPalettePath },
131 { SvtPathOptions::PATH_PLUGIN, &SvtDefaultOptions_Impl::m_aPluginPath },
132 { SvtPathOptions::PATH_TEMP, &SvtDefaultOptions_Impl::m_aTempPath },
133 { SvtPathOptions::PATH_TEMPLATE, &SvtDefaultOptions_Impl::m_aTemplatePath },
134 { SvtPathOptions::PATH_USERCONFIG, &SvtDefaultOptions_Impl::m_aUserConfigPath },
135 { SvtPathOptions::PATH_WORK, &SvtDefaultOptions_Impl::m_aWorkPath }
138 // functions -------------------------------------------------------------
140 Sequence< OUString > GetDefaultPropertyNames()
142 static const char* aPropNames[] =
144 "Addin", // PATH_ADDIN
145 "AutoCorrect", // PATH_AUTOCORRECT
146 "AutoText", // PATH_AUTOTEXT
147 "Backup", // PATH_BACKUP
148 "Basic", // PATH_BASIC
149 "Bitmap", // PATH_BITMAP
150 "Config", // PATH_CONFIG
151 "Dictionary", // PATH_DICTIONARY
152 "Favorite", // PATH_FAVORITES
153 "Filter", // PATH_FILTER
154 "Gallery", // PATH_GALLERY
155 "Graphic", // PATH_GRAPHIC
156 "Help", // PATH_HELP
157 "Linguistic", // PATH_LINGUISTIC
158 "Module", // PATH_MODULE
159 "Palette", // PATH_PALETTE
160 "Plugin", // PATH_PLUGIN
161 "Temp", // PATH_TEMP
162 "Template", // PATH_TEMPLATE
163 "UserConfig", // PATH_USERCONFIG
164 "Work" // PATH_WORK
167 const int nCount = sizeof( aPropNames ) / sizeof( const char* );
168 Sequence< OUString > aNames( nCount );
169 OUString* pNames = aNames.getArray();
170 for ( int i = 0; i < nCount; i++ )
171 pNames[i] = OUString::createFromAscii( aPropNames[i] );
173 return aNames;
176 void SvtDefaultOptions_Impl::Notify( const Sequence< OUString >& )
178 // no notification, will never be changed
181 void SvtDefaultOptions_Impl::Commit()
183 // will never be changed
186 // class SvtDefaultOptions_Impl ------------------------------------------
188 String SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const
190 OUString aRet;
191 sal_uInt16 nIdx = 0;
193 while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_WORK )
195 if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
197 aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
198 if ( nId == SvtPathOptions::PATH_ADDIN ||
199 nId == SvtPathOptions::PATH_FILTER ||
200 nId == SvtPathOptions::PATH_HELP ||
201 nId == SvtPathOptions::PATH_MODULE ||
202 nId == SvtPathOptions::PATH_PLUGIN )
204 OUString aTmp;
205 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aRet, aTmp );
206 aRet = aTmp;
209 break;
211 ++nIdx;
214 return aRet;
217 // -----------------------------------------------------------------------
219 SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( "Office.Common/Path/Default" )
221 Sequence< OUString > aNames = GetDefaultPropertyNames();
222 Sequence< Any > aValues = GetProperties( aNames );
223 EnableNotification( aNames );
224 const Any* pValues = aValues.getConstArray();
225 DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
226 if ( aValues.getLength() == aNames.getLength() )
228 SvtPathOptions aPathOpt;
229 OUString aTempStr, aFullPath;
231 for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
233 if ( pValues[nProp].hasValue() )
235 switch ( pValues[nProp].getValueTypeClass() )
237 case ::com::sun::star::uno::TypeClass_STRING :
239 // multi paths
240 if ( pValues[nProp] >>= aTempStr )
241 aFullPath = aPathOpt.SubstituteVariable( aTempStr );
242 else
244 SAL_WARN( "unotools.config", "any operator >>= failed" );
246 break;
249 case ::com::sun::star::uno::TypeClass_SEQUENCE :
251 // single paths
252 aFullPath = OUString();
253 Sequence < OUString > aList;
254 if ( pValues[nProp] >>= aList )
256 sal_Int32 nCount = aList.getLength();
257 for ( sal_Int32 nPosition = 0; nPosition < nCount; ++nPosition )
259 aTempStr = aPathOpt.SubstituteVariable( aList[ nPosition ] );
260 aFullPath += aTempStr;
261 if ( nPosition < nCount-1 )
262 aFullPath += ";";
265 else
267 SAL_WARN( "unotools.config", "any operator >>= failed" );
269 break;
272 default:
274 SAL_WARN( "unotools.config", "Wrong any type" );
278 switch ( nProp )
280 case DEFAULTPATH__ADDIN: m_aAddinPath = String( aFullPath ); break;
281 case DEFAULTPATH__AUTOCORRECT: m_aAutoCorrectPath = String( aFullPath ); break;
282 case DEFAULTPATH__AUTOTEXT: m_aAutoTextPath = String( aFullPath ); break;
283 case DEFAULTPATH__BACKUP: m_aBackupPath = String( aFullPath ); break;
284 case DEFAULTPATH__BASIC: m_aBasicPath = String( aFullPath ); break;
285 case DEFAULTPATH__BITMAP: m_aBitmapPath = String( aFullPath ); break;
286 case DEFAULTPATH__CONFIG: m_aConfigPath = String( aFullPath ); break;
287 case DEFAULTPATH__DICTIONARY: m_aDictionaryPath = String( aFullPath ); break;
288 case DEFAULTPATH__FAVORITES: m_aFavoritesPath = String( aFullPath ); break;
289 case DEFAULTPATH__FILTER: m_aFilterPath = String( aFullPath ); break;
290 case DEFAULTPATH__GALLERY: m_aGalleryPath = String( aFullPath ); break;
291 case DEFAULTPATH__GRAPHIC: m_aGraphicPath = String( aFullPath ); break;
292 case DEFAULTPATH__HELP: m_aHelpPath = String( aFullPath ); break;
293 case DEFAULTPATH__LINGUISTIC: m_aLinguisticPath = String( aFullPath ); break;
294 case DEFAULTPATH__MODULE: m_aModulePath = String( aFullPath ); break;
295 case DEFAULTPATH__PALETTE: m_aPalettePath = String( aFullPath ); break;
296 case DEFAULTPATH__PLUGIN: m_aPluginPath = String( aFullPath ); break;
297 case DEFAULTPATH__TEMP: m_aTempPath = String( aFullPath ); break;
298 case DEFAULTPATH__TEMPLATE: m_aTemplatePath = String( aFullPath ); break;
299 case DEFAULTPATH__USERCONFIG: m_aUserConfigPath = String( aFullPath ); break;
300 case DEFAULTPATH__WORK: m_aWorkPath = String( aFullPath ); break;
301 case DEFAULTPATH__USERDICTIONARY: m_aUserDictionaryPath = String( aFullPath );break;
303 default:
304 SAL_WARN( "unotools.config", "invalid index to load a default path" );
311 // class SvtDefaultOptions -----------------------------------------------
312 namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }
314 SvtDefaultOptions::SvtDefaultOptions()
316 // Global access, must be guarded (multithreading)
317 ::osl::MutexGuard aGuard( lclMutex::get() );
318 if ( !pOptions )
320 pOptions = new SvtDefaultOptions_Impl;
321 ItemHolder1::holdConfigItem(E_DEFAULTOPTIONS);
323 ++nRefCount;
324 pImp = pOptions;
327 // -----------------------------------------------------------------------
329 SvtDefaultOptions::~SvtDefaultOptions()
331 // Global access, must be guarded (multithreading)
332 ::osl::MutexGuard aGuard( lclMutex::get() );
333 if ( !--nRefCount )
335 if ( pOptions->IsModified() )
336 pOptions->Commit();
337 DELETEZ( pOptions );
341 // -----------------------------------------------------------------------
343 String SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const
345 return pImp->GetDefaultPath( nId );
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */