nss: upgrade to release 3.73
[LibreOffice.git] / unotools / source / config / defaultoptions.cxx
blobc19df297905753ce68f804dceac80f078656ebb7
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 <sal/config.h>
22 #include <osl/file.hxx>
23 #include <sal/log.hxx>
24 #include <unotools/defaultoptions.hxx>
25 #include <unotools/pathoptions.hxx>
26 #include <unotools/configitem.hxx>
27 #include <tools/debug.hxx>
28 #include <com/sun/star/uno/Any.hxx>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <osl/mutex.hxx>
32 #include <rtl/instance.hxx>
33 #include <rtl/ustrbuf.hxx>
35 #include "itemholder1.hxx"
37 using namespace osl;
38 using namespace utl;
39 using namespace com::sun::star::uno;
41 #define DEFAULTPATH_ADDIN 0
42 #define DEFAULTPATH_AUTOCORRECT 1
43 #define DEFAULTPATH_AUTOTEXT 2
44 #define DEFAULTPATH_BACKUP 3
45 #define DEFAULTPATH_BASIC 4
46 #define DEFAULTPATH_BITMAP 5
47 #define DEFAULTPATH_CONFIG 6
48 #define DEFAULTPATH_DICTIONARY 7
49 #define DEFAULTPATH_FAVORITES 8
50 #define DEFAULTPATH_FILTER 9
51 #define DEFAULTPATH_GALLERY 10
52 #define DEFAULTPATH_GRAPHIC 11
53 #define DEFAULTPATH_HELP 12
54 #define DEFAULTPATH_LINGUISTIC 13
55 #define DEFAULTPATH_MODULE 14
56 #define DEFAULTPATH_PALETTE 15
57 #define DEFAULTPATH_PLUGIN 16
58 #define DEFAULTPATH_TEMP 17
59 #define DEFAULTPATH_TEMPLATE 18
60 #define DEFAULTPATH_USERCONFIG 19
61 #define DEFAULTPATH_WORK 20
62 #define DEFAULTPATH_CLASSIFICATION 21
63 #define DEFAULTPATH_USERDICTIONARY 22
65 // class SvtDefaultOptions_Impl ------------------------------------------
67 class SvtDefaultOptions_Impl : public utl::ConfigItem
69 public:
70 OUString m_aAddinPath;
71 OUString m_aAutoCorrectPath;
72 OUString m_aAutoTextPath;
73 OUString m_aBackupPath;
74 OUString m_aBasicPath;
75 OUString m_aBitmapPath;
76 OUString m_aConfigPath;
77 OUString m_aDictionaryPath;
78 OUString m_aFavoritesPath;
79 OUString m_aFilterPath;
80 OUString m_aGalleryPath;
81 OUString m_aGraphicPath;
82 OUString m_aHelpPath;
83 OUString m_aLinguisticPath;
84 OUString m_aModulePath;
85 OUString m_aPalettePath;
86 OUString m_aPluginPath;
87 OUString m_aTempPath;
88 OUString m_aTemplatePath;
89 OUString m_aUserConfigPath;
90 OUString m_aWorkPath;
91 OUString m_aClassificationPath;
93 SvtDefaultOptions_Impl();
94 virtual ~SvtDefaultOptions_Impl() override;
96 OUString GetDefaultPath( sal_uInt16 nId ) const;
97 virtual void Notify( const css::uno::Sequence<OUString>& aPropertyNames) override;
99 private:
100 virtual void ImplCommit() final override;
103 // global ----------------------------------------------------------------
105 namespace {
107 std::weak_ptr<SvtDefaultOptions_Impl> g_pOptions;
111 typedef OUString SvtDefaultOptions_Impl::*PathStrPtr;
113 namespace {
115 struct PathToDefaultMapping_Impl
117 SvtPathOptions::Paths _ePath;
118 PathStrPtr _pDefaultPath;
123 PathToDefaultMapping_Impl const PathMap_Impl[] =
125 { SvtPathOptions::PATH_ADDIN, &SvtDefaultOptions_Impl::m_aAddinPath },
126 { SvtPathOptions::PATH_AUTOCORRECT, &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
127 { SvtPathOptions::PATH_AUTOTEXT, &SvtDefaultOptions_Impl::m_aAutoTextPath },
128 { SvtPathOptions::PATH_BACKUP, &SvtDefaultOptions_Impl::m_aBackupPath },
129 { SvtPathOptions::PATH_BASIC, &SvtDefaultOptions_Impl::m_aBasicPath },
130 { SvtPathOptions::PATH_BITMAP, &SvtDefaultOptions_Impl::m_aBitmapPath },
131 { SvtPathOptions::PATH_CONFIG, &SvtDefaultOptions_Impl::m_aConfigPath },
132 { SvtPathOptions::PATH_DICTIONARY, &SvtDefaultOptions_Impl::m_aDictionaryPath },
133 { SvtPathOptions::PATH_FAVORITES, &SvtDefaultOptions_Impl::m_aFavoritesPath },
134 { SvtPathOptions::PATH_FILTER, &SvtDefaultOptions_Impl::m_aFilterPath },
135 { SvtPathOptions::PATH_GALLERY, &SvtDefaultOptions_Impl::m_aGalleryPath },
136 { SvtPathOptions::PATH_GRAPHIC, &SvtDefaultOptions_Impl::m_aGraphicPath },
137 { SvtPathOptions::PATH_HELP, &SvtDefaultOptions_Impl::m_aHelpPath },
138 { SvtPathOptions::PATH_LINGUISTIC, &SvtDefaultOptions_Impl::m_aLinguisticPath },
139 { SvtPathOptions::PATH_MODULE, &SvtDefaultOptions_Impl::m_aModulePath },
140 { SvtPathOptions::PATH_PALETTE, &SvtDefaultOptions_Impl::m_aPalettePath },
141 { SvtPathOptions::PATH_PLUGIN, &SvtDefaultOptions_Impl::m_aPluginPath },
142 { SvtPathOptions::PATH_TEMP, &SvtDefaultOptions_Impl::m_aTempPath },
143 { SvtPathOptions::PATH_TEMPLATE, &SvtDefaultOptions_Impl::m_aTemplatePath },
144 { SvtPathOptions::PATH_USERCONFIG, &SvtDefaultOptions_Impl::m_aUserConfigPath },
145 { SvtPathOptions::PATH_WORK, &SvtDefaultOptions_Impl::m_aWorkPath },
146 { SvtPathOptions::PATH_CLASSIFICATION, &SvtDefaultOptions_Impl::m_aClassificationPath }
149 // functions -------------------------------------------------------------
151 static Sequence< OUString > GetDefaultPropertyNames()
153 static const char* aPropNames[] =
155 "Addin", // PATH_ADDIN
156 "AutoCorrect", // PATH_AUTOCORRECT
157 "AutoText", // PATH_AUTOTEXT
158 "Backup", // PATH_BACKUP
159 "Basic", // PATH_BASIC
160 "Bitmap", // PATH_BITMAP
161 "Config", // PATH_CONFIG
162 "Dictionary", // PATH_DICTIONARY
163 "Favorite", // PATH_FAVORITES
164 "Filter", // PATH_FILTER
165 "Gallery", // PATH_GALLERY
166 "Graphic", // PATH_GRAPHIC
167 "Help", // PATH_HELP
168 "Linguistic", // PATH_LINGUISTIC
169 "Module", // PATH_MODULE
170 "Palette", // PATH_PALETTE
171 "Plugin", // PATH_PLUGIN
172 "Temp", // PATH_TEMP
173 "Template", // PATH_TEMPLATE
174 "UserConfig", // PATH_USERCONFIG
175 "Work", // PATH_WORK
176 "Classification" // PATH_CLASSIFICATION
179 const int nCount = SAL_N_ELEMENTS( aPropNames );
180 Sequence< OUString > aNames( nCount );
181 OUString* pNames = aNames.getArray();
182 for ( int i = 0; i < nCount; i++ )
183 pNames[i] = OUString::createFromAscii( aPropNames[i] );
185 return aNames;
188 void SvtDefaultOptions_Impl::Notify( const Sequence< OUString >& )
190 // no notification, will never be changed
193 void SvtDefaultOptions_Impl::ImplCommit()
195 // will never be changed
198 // class SvtDefaultOptions_Impl ------------------------------------------
200 OUString SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const
202 OUString aRet;
203 sal_uInt16 nIdx = 0;
205 while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_CLASSIFICATION )
207 if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
209 aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
210 if ( nId == SvtPathOptions::PATH_ADDIN ||
211 nId == SvtPathOptions::PATH_FILTER ||
212 nId == SvtPathOptions::PATH_HELP ||
213 nId == SvtPathOptions::PATH_MODULE ||
214 nId == SvtPathOptions::PATH_PLUGIN )
216 OUString aTmp;
217 osl::FileBase::getFileURLFromSystemPath( aRet, aTmp );
218 aRet = aTmp;
221 break;
223 ++nIdx;
226 return aRet;
229 SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( "Office.Common/Path/Default" )
231 Sequence< OUString > aNames = GetDefaultPropertyNames();
232 Sequence< Any > aValues = GetProperties( aNames );
233 EnableNotification( aNames );
234 const Any* pValues = aValues.getConstArray();
235 DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
236 if ( aValues.getLength() != aNames.getLength() )
237 return;
239 SvtPathOptions aPathOpt;
240 OUString aTempStr;
241 OUStringBuffer aFullPathBuf;
243 for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
245 if ( pValues[nProp].hasValue() )
247 switch ( pValues[nProp].getValueTypeClass() )
249 case css::uno::TypeClass_STRING :
251 // multi paths
252 if ( pValues[nProp] >>= aTempStr )
253 aFullPathBuf = aPathOpt.SubstituteVariable( aTempStr );
254 else
256 SAL_WARN( "unotools.config", "any operator >>= failed" );
258 break;
261 case css::uno::TypeClass_SEQUENCE :
263 // single paths
264 aFullPathBuf.setLength(0);
265 Sequence < OUString > aList;
266 if ( pValues[nProp] >>= aList )
268 sal_Int32 nCount = aList.getLength();
269 for ( sal_Int32 nPosition = 0; nPosition < nCount; ++nPosition )
271 aFullPathBuf.append(aPathOpt.SubstituteVariable( aList[ nPosition ] ));
272 if ( nPosition < nCount-1 )
273 aFullPathBuf.append(";");
276 else
278 SAL_WARN( "unotools.config", "any operator >>= failed" );
280 break;
283 default:
285 SAL_WARN( "unotools.config", "Wrong any type" );
289 auto aFullPath = aFullPathBuf.makeStringAndClear();
290 switch ( nProp )
292 case DEFAULTPATH_ADDIN: m_aAddinPath = aFullPath; break;
293 case DEFAULTPATH_AUTOCORRECT: m_aAutoCorrectPath = aFullPath; break;
294 case DEFAULTPATH_AUTOTEXT: m_aAutoTextPath = aFullPath; break;
295 case DEFAULTPATH_BACKUP: m_aBackupPath = aFullPath; break;
296 case DEFAULTPATH_BASIC: m_aBasicPath = aFullPath; break;
297 case DEFAULTPATH_BITMAP: m_aBitmapPath = aFullPath; break;
298 case DEFAULTPATH_CONFIG: m_aConfigPath = aFullPath; break;
299 case DEFAULTPATH_DICTIONARY: m_aDictionaryPath = aFullPath; break;
300 case DEFAULTPATH_FAVORITES: m_aFavoritesPath = aFullPath; break;
301 case DEFAULTPATH_FILTER: m_aFilterPath = aFullPath; break;
302 case DEFAULTPATH_GALLERY: m_aGalleryPath = aFullPath; break;
303 case DEFAULTPATH_GRAPHIC: m_aGraphicPath = aFullPath; break;
304 case DEFAULTPATH_HELP: m_aHelpPath = aFullPath; break;
305 case DEFAULTPATH_LINGUISTIC: m_aLinguisticPath = aFullPath; break;
306 case DEFAULTPATH_MODULE: m_aModulePath = aFullPath; break;
307 case DEFAULTPATH_PALETTE: m_aPalettePath = aFullPath; break;
308 case DEFAULTPATH_PLUGIN: m_aPluginPath = aFullPath; break;
309 case DEFAULTPATH_TEMP: m_aTempPath = aFullPath; break;
310 case DEFAULTPATH_TEMPLATE: m_aTemplatePath = aFullPath; break;
311 case DEFAULTPATH_USERCONFIG: m_aUserConfigPath = aFullPath; break;
312 case DEFAULTPATH_WORK: m_aWorkPath = aFullPath; break;
313 case DEFAULTPATH_CLASSIFICATION: m_aClassificationPath = aFullPath;break;
314 case DEFAULTPATH_USERDICTIONARY: break;
316 default:
317 SAL_WARN( "unotools.config", "invalid index to load a default path" );
323 SvtDefaultOptions_Impl::~SvtDefaultOptions_Impl()
325 if ( IsModified() )
326 Commit();
329 // class SvtDefaultOptions -----------------------------------------------
330 namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }
332 SvtDefaultOptions::SvtDefaultOptions()
334 // Global access, must be guarded (multithreading)
335 ::osl::MutexGuard aGuard( lclMutex::get() );
336 pImpl = g_pOptions.lock();
337 if ( !pImpl )
339 pImpl = std::make_shared<SvtDefaultOptions_Impl>();
340 g_pOptions = pImpl;
341 ItemHolder1::holdConfigItem(EItem::DefaultOptions);
345 SvtDefaultOptions::~SvtDefaultOptions()
347 // Global access, must be guarded (multithreading)
348 ::osl::MutexGuard aGuard( lclMutex::get() );
349 pImpl.reset();
352 OUString SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const
354 return pImpl->GetDefaultPath( nId );
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */