Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / config / defaultoptions.cxx
blob227e858cf32fd7d448b179bb4d407429983767bf
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;
39 using ::rtl::OUString;
41 // define ----------------------------------------------------------------
43 #define ASCII_STR(s) OUString( RTL_CONSTASCII_USTRINGPARAM(s) )
45 #define DEFAULTPATH__ADDIN 0
46 #define DEFAULTPATH__AUTOCORRECT 1
47 #define DEFAULTPATH__AUTOTEXT 2
48 #define DEFAULTPATH__BACKUP 3
49 #define DEFAULTPATH__BASIC 4
50 #define DEFAULTPATH__BITMAP 5
51 #define DEFAULTPATH__CONFIG 6
52 #define DEFAULTPATH__DICTIONARY 7
53 #define DEFAULTPATH__FAVORITES 8
54 #define DEFAULTPATH__FILTER 9
55 #define DEFAULTPATH__GALLERY 10
56 #define DEFAULTPATH__GRAPHIC 11
57 #define DEFAULTPATH__HELP 12
58 #define DEFAULTPATH__LINGUISTIC 13
59 #define DEFAULTPATH__MODULE 14
60 #define DEFAULTPATH__PALETTE 15
61 #define DEFAULTPATH__PLUGIN 16
62 #define DEFAULTPATH__TEMP 17
63 #define DEFAULTPATH__TEMPLATE 18
64 #define DEFAULTPATH__USERCONFIG 19
65 #define DEFAULTPATH__WORK 20
66 #define DEFAULTPATH__USERDICTIONARY 21
68 // class SvtDefaultOptions_Impl ------------------------------------------
70 class SvtDefaultOptions_Impl : public utl::ConfigItem
72 public:
73 String m_aAddinPath;
74 String m_aAutoCorrectPath;
75 String m_aAutoTextPath;
76 String m_aBackupPath;
77 String m_aBasicPath;
78 String m_aBitmapPath;
79 String m_aConfigPath;
80 String m_aDictionaryPath;
81 String m_aFavoritesPath;
82 String m_aFilterPath;
83 String m_aGalleryPath;
84 String m_aGraphicPath;
85 String m_aHelpPath;
86 String m_aLinguisticPath;
87 String m_aModulePath;
88 String m_aPalettePath;
89 String m_aPluginPath;
90 String m_aTempPath;
91 String m_aTemplatePath;
92 String m_aUserConfigPath;
93 String m_aWorkPath;
94 String m_aUserDictionaryPath;
96 SvtDefaultOptions_Impl();
98 String GetDefaultPath( sal_uInt16 nId ) const;
99 virtual void Commit();
100 virtual void Notify( const com::sun::star::uno::Sequence<rtl::OUString>& aPropertyNames);
103 // global ----------------------------------------------------------------
105 static SvtDefaultOptions_Impl* pOptions = NULL;
106 static sal_Int32 nRefCount = 0;
108 typedef String SvtDefaultOptions_Impl:: *PathStrPtr;
110 struct PathToDefaultMapping_Impl
112 SvtPathOptions::Paths _ePath;
113 PathStrPtr _pDefaultPath;
116 static PathToDefaultMapping_Impl const PathMap_Impl[] =
118 { SvtPathOptions::PATH_ADDIN, &SvtDefaultOptions_Impl::m_aAddinPath },
119 { SvtPathOptions::PATH_AUTOCORRECT, &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
120 { SvtPathOptions::PATH_AUTOTEXT, &SvtDefaultOptions_Impl::m_aAutoTextPath },
121 { SvtPathOptions::PATH_BACKUP, &SvtDefaultOptions_Impl::m_aBackupPath },
122 { SvtPathOptions::PATH_BASIC, &SvtDefaultOptions_Impl::m_aBasicPath },
123 { SvtPathOptions::PATH_BITMAP, &SvtDefaultOptions_Impl::m_aBitmapPath },
124 { SvtPathOptions::PATH_CONFIG, &SvtDefaultOptions_Impl::m_aConfigPath },
125 { SvtPathOptions::PATH_DICTIONARY, &SvtDefaultOptions_Impl::m_aDictionaryPath },
126 { SvtPathOptions::PATH_FAVORITES, &SvtDefaultOptions_Impl::m_aFavoritesPath },
127 { SvtPathOptions::PATH_FILTER, &SvtDefaultOptions_Impl::m_aFilterPath },
128 { SvtPathOptions::PATH_GALLERY, &SvtDefaultOptions_Impl::m_aGalleryPath },
129 { SvtPathOptions::PATH_GRAPHIC, &SvtDefaultOptions_Impl::m_aGraphicPath },
130 { SvtPathOptions::PATH_HELP, &SvtDefaultOptions_Impl::m_aHelpPath },
131 { SvtPathOptions::PATH_LINGUISTIC, &SvtDefaultOptions_Impl::m_aLinguisticPath },
132 { SvtPathOptions::PATH_MODULE, &SvtDefaultOptions_Impl::m_aModulePath },
133 { SvtPathOptions::PATH_PALETTE, &SvtDefaultOptions_Impl::m_aPalettePath },
134 { SvtPathOptions::PATH_PLUGIN, &SvtDefaultOptions_Impl::m_aPluginPath },
135 { SvtPathOptions::PATH_TEMP, &SvtDefaultOptions_Impl::m_aTempPath },
136 { SvtPathOptions::PATH_TEMPLATE, &SvtDefaultOptions_Impl::m_aTemplatePath },
137 { SvtPathOptions::PATH_USERCONFIG, &SvtDefaultOptions_Impl::m_aUserConfigPath },
138 { SvtPathOptions::PATH_WORK, &SvtDefaultOptions_Impl::m_aWorkPath }
141 // functions -------------------------------------------------------------
143 Sequence< OUString > GetDefaultPropertyNames()
145 static const char* aPropNames[] =
147 "Addin", // PATH_ADDIN
148 "AutoCorrect", // PATH_AUTOCORRECT
149 "AutoText", // PATH_AUTOTEXT
150 "Backup", // PATH_BACKUP
151 "Basic", // PATH_BASIC
152 "Bitmap", // PATH_BITMAP
153 "Config", // PATH_CONFIG
154 "Dictionary", // PATH_DICTIONARY
155 "Favorite", // PATH_FAVORITES
156 "Filter", // PATH_FILTER
157 "Gallery", // PATH_GALLERY
158 "Graphic", // PATH_GRAPHIC
159 "Help", // PATH_HELP
160 "Linguistic", // PATH_LINGUISTIC
161 "Module", // PATH_MODULE
162 "Palette", // PATH_PALETTE
163 "Plugin", // PATH_PLUGIN
164 "Temp", // PATH_TEMP
165 "Template", // PATH_TEMPLATE
166 "UserConfig", // PATH_USERCONFIG
167 "Work" // PATH_WORK
170 const int nCount = sizeof( aPropNames ) / sizeof( const char* );
171 Sequence< OUString > aNames( nCount );
172 OUString* pNames = aNames.getArray();
173 for ( int i = 0; i < nCount; i++ )
174 pNames[i] = OUString::createFromAscii( aPropNames[i] );
176 return aNames;
179 void SvtDefaultOptions_Impl::Notify( const Sequence< rtl::OUString >& )
181 // no notification, will never be changed
184 void SvtDefaultOptions_Impl::Commit()
186 // will never be changed
189 // class SvtDefaultOptions_Impl ------------------------------------------
191 String SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const
193 rtl::OUString aRet;
194 sal_uInt16 nIdx = 0;
196 while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_WORK )
198 if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
200 aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
201 if ( nId == SvtPathOptions::PATH_ADDIN ||
202 nId == SvtPathOptions::PATH_FILTER ||
203 nId == SvtPathOptions::PATH_HELP ||
204 nId == SvtPathOptions::PATH_MODULE ||
205 nId == SvtPathOptions::PATH_PLUGIN )
207 rtl::OUString aTmp;
208 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aRet, aTmp );
209 aRet = aTmp;
212 break;
214 ++nIdx;
217 return aRet;
220 // -----------------------------------------------------------------------
222 SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( ASCII_STR("Office.Common/Path/Default") )
224 Sequence< OUString > aNames = GetDefaultPropertyNames();
225 Sequence< Any > aValues = GetProperties( aNames );
226 EnableNotification( aNames );
227 const Any* pValues = aValues.getConstArray();
228 DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
229 if ( aValues.getLength() == aNames.getLength() )
231 SvtPathOptions aPathOpt;
232 OUString aTempStr, aFullPath;
234 for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
236 if ( pValues[nProp].hasValue() )
238 switch ( pValues[nProp].getValueTypeClass() )
240 case ::com::sun::star::uno::TypeClass_STRING :
242 // multi paths
243 if ( pValues[nProp] >>= aTempStr )
244 aFullPath = aPathOpt.SubstituteVariable( aTempStr );
245 else
247 SAL_WARN( "unotools.config", "any operator >>= failed" );
249 break;
252 case ::com::sun::star::uno::TypeClass_SEQUENCE :
254 // single paths
255 aFullPath = OUString();
256 Sequence < OUString > aList;
257 if ( pValues[nProp] >>= aList )
259 sal_Int32 nCount = aList.getLength();
260 for ( sal_Int32 nPosition = 0; nPosition < nCount; ++nPosition )
262 aTempStr = aPathOpt.SubstituteVariable( aList[ nPosition ] );
263 aFullPath += aTempStr;
264 if ( nPosition < nCount-1 )
265 aFullPath += OUString( RTL_CONSTASCII_USTRINGPARAM(";") );
268 else
270 SAL_WARN( "unotools.config", "any operator >>= failed" );
272 break;
275 default:
277 SAL_WARN( "unotools.config", "Wrong any type" );
281 switch ( nProp )
283 case DEFAULTPATH__ADDIN: m_aAddinPath = String( aFullPath ); break;
284 case DEFAULTPATH__AUTOCORRECT: m_aAutoCorrectPath = String( aFullPath ); break;
285 case DEFAULTPATH__AUTOTEXT: m_aAutoTextPath = String( aFullPath ); break;
286 case DEFAULTPATH__BACKUP: m_aBackupPath = String( aFullPath ); break;
287 case DEFAULTPATH__BASIC: m_aBasicPath = String( aFullPath ); break;
288 case DEFAULTPATH__BITMAP: m_aBitmapPath = String( aFullPath ); break;
289 case DEFAULTPATH__CONFIG: m_aConfigPath = String( aFullPath ); break;
290 case DEFAULTPATH__DICTIONARY: m_aDictionaryPath = String( aFullPath ); break;
291 case DEFAULTPATH__FAVORITES: m_aFavoritesPath = String( aFullPath ); break;
292 case DEFAULTPATH__FILTER: m_aFilterPath = String( aFullPath ); break;
293 case DEFAULTPATH__GALLERY: m_aGalleryPath = String( aFullPath ); break;
294 case DEFAULTPATH__GRAPHIC: m_aGraphicPath = String( aFullPath ); break;
295 case DEFAULTPATH__HELP: m_aHelpPath = String( aFullPath ); break;
296 case DEFAULTPATH__LINGUISTIC: m_aLinguisticPath = String( aFullPath ); break;
297 case DEFAULTPATH__MODULE: m_aModulePath = String( aFullPath ); break;
298 case DEFAULTPATH__PALETTE: m_aPalettePath = String( aFullPath ); break;
299 case DEFAULTPATH__PLUGIN: m_aPluginPath = String( aFullPath ); break;
300 case DEFAULTPATH__TEMP: m_aTempPath = String( aFullPath ); break;
301 case DEFAULTPATH__TEMPLATE: m_aTemplatePath = String( aFullPath ); break;
302 case DEFAULTPATH__USERCONFIG: m_aUserConfigPath = String( aFullPath ); break;
303 case DEFAULTPATH__WORK: m_aWorkPath = String( aFullPath ); break;
304 case DEFAULTPATH__USERDICTIONARY: m_aUserDictionaryPath = String( aFullPath );break;
306 default:
307 SAL_WARN( "unotools.config", "invalid index to load a default path" );
314 // class SvtDefaultOptions -----------------------------------------------
315 namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }
317 SvtDefaultOptions::SvtDefaultOptions()
319 // Global access, must be guarded (multithreading)
320 ::osl::MutexGuard aGuard( lclMutex::get() );
321 if ( !pOptions )
323 pOptions = new SvtDefaultOptions_Impl;
324 ItemHolder1::holdConfigItem(E_DEFAULTOPTIONS);
326 ++nRefCount;
327 pImp = pOptions;
330 // -----------------------------------------------------------------------
332 SvtDefaultOptions::~SvtDefaultOptions()
334 // Global access, must be guarded (multithreading)
335 ::osl::MutexGuard aGuard( lclMutex::get() );
336 if ( !--nRefCount )
338 if ( pOptions->IsModified() )
339 pOptions->Commit();
340 DELETEZ( pOptions );
344 // -----------------------------------------------------------------------
346 String SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const
348 return pImp->GetDefaultPath( nId );
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */