Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / config / defaultoptions.cxx
blobdddbd22d3b13f3cb6201d007ef737a26ac4a35c8
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 struct PathToDefaultMapping_Impl
115 SvtPathOptions::Paths const _ePath;
116 PathStrPtr const _pDefaultPath;
119 static PathToDefaultMapping_Impl const PathMap_Impl[] =
121 { SvtPathOptions::PATH_ADDIN, &SvtDefaultOptions_Impl::m_aAddinPath },
122 { SvtPathOptions::PATH_AUTOCORRECT, &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
123 { SvtPathOptions::PATH_AUTOTEXT, &SvtDefaultOptions_Impl::m_aAutoTextPath },
124 { SvtPathOptions::PATH_BACKUP, &SvtDefaultOptions_Impl::m_aBackupPath },
125 { SvtPathOptions::PATH_BASIC, &SvtDefaultOptions_Impl::m_aBasicPath },
126 { SvtPathOptions::PATH_BITMAP, &SvtDefaultOptions_Impl::m_aBitmapPath },
127 { SvtPathOptions::PATH_CONFIG, &SvtDefaultOptions_Impl::m_aConfigPath },
128 { SvtPathOptions::PATH_DICTIONARY, &SvtDefaultOptions_Impl::m_aDictionaryPath },
129 { SvtPathOptions::PATH_FAVORITES, &SvtDefaultOptions_Impl::m_aFavoritesPath },
130 { SvtPathOptions::PATH_FILTER, &SvtDefaultOptions_Impl::m_aFilterPath },
131 { SvtPathOptions::PATH_GALLERY, &SvtDefaultOptions_Impl::m_aGalleryPath },
132 { SvtPathOptions::PATH_GRAPHIC, &SvtDefaultOptions_Impl::m_aGraphicPath },
133 { SvtPathOptions::PATH_HELP, &SvtDefaultOptions_Impl::m_aHelpPath },
134 { SvtPathOptions::PATH_LINGUISTIC, &SvtDefaultOptions_Impl::m_aLinguisticPath },
135 { SvtPathOptions::PATH_MODULE, &SvtDefaultOptions_Impl::m_aModulePath },
136 { SvtPathOptions::PATH_PALETTE, &SvtDefaultOptions_Impl::m_aPalettePath },
137 { SvtPathOptions::PATH_PLUGIN, &SvtDefaultOptions_Impl::m_aPluginPath },
138 { SvtPathOptions::PATH_TEMP, &SvtDefaultOptions_Impl::m_aTempPath },
139 { SvtPathOptions::PATH_TEMPLATE, &SvtDefaultOptions_Impl::m_aTemplatePath },
140 { SvtPathOptions::PATH_USERCONFIG, &SvtDefaultOptions_Impl::m_aUserConfigPath },
141 { SvtPathOptions::PATH_WORK, &SvtDefaultOptions_Impl::m_aWorkPath },
142 { SvtPathOptions::PATH_CLASSIFICATION, &SvtDefaultOptions_Impl::m_aClassificationPath }
145 // functions -------------------------------------------------------------
147 static Sequence< OUString > GetDefaultPropertyNames()
149 static const char* aPropNames[] =
151 "Addin", // PATH_ADDIN
152 "AutoCorrect", // PATH_AUTOCORRECT
153 "AutoText", // PATH_AUTOTEXT
154 "Backup", // PATH_BACKUP
155 "Basic", // PATH_BASIC
156 "Bitmap", // PATH_BITMAP
157 "Config", // PATH_CONFIG
158 "Dictionary", // PATH_DICTIONARY
159 "Favorite", // PATH_FAVORITES
160 "Filter", // PATH_FILTER
161 "Gallery", // PATH_GALLERY
162 "Graphic", // PATH_GRAPHIC
163 "Help", // PATH_HELP
164 "Linguistic", // PATH_LINGUISTIC
165 "Module", // PATH_MODULE
166 "Palette", // PATH_PALETTE
167 "Plugin", // PATH_PLUGIN
168 "Temp", // PATH_TEMP
169 "Template", // PATH_TEMPLATE
170 "UserConfig", // PATH_USERCONFIG
171 "Work", // PATH_WORK
172 "Classification" // PATH_CLASSIFICATION
175 const int nCount = SAL_N_ELEMENTS( aPropNames );
176 Sequence< OUString > aNames( nCount );
177 OUString* pNames = aNames.getArray();
178 for ( int i = 0; i < nCount; i++ )
179 pNames[i] = OUString::createFromAscii( aPropNames[i] );
181 return aNames;
184 void SvtDefaultOptions_Impl::Notify( const Sequence< OUString >& )
186 // no notification, will never be changed
189 void SvtDefaultOptions_Impl::ImplCommit()
191 // will never be changed
194 // class SvtDefaultOptions_Impl ------------------------------------------
196 OUString SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const
198 OUString aRet;
199 sal_uInt16 nIdx = 0;
201 while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_CLASSIFICATION )
203 if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
205 aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
206 if ( nId == SvtPathOptions::PATH_ADDIN ||
207 nId == SvtPathOptions::PATH_FILTER ||
208 nId == SvtPathOptions::PATH_HELP ||
209 nId == SvtPathOptions::PATH_MODULE ||
210 nId == SvtPathOptions::PATH_PLUGIN )
212 OUString aTmp;
213 osl::FileBase::getFileURLFromSystemPath( aRet, aTmp );
214 aRet = aTmp;
217 break;
219 ++nIdx;
222 return aRet;
225 SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( "Office.Common/Path/Default" )
227 Sequence< OUString > aNames = GetDefaultPropertyNames();
228 Sequence< Any > aValues = GetProperties( aNames );
229 EnableNotification( aNames );
230 const Any* pValues = aValues.getConstArray();
231 DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
232 if ( aValues.getLength() == aNames.getLength() )
234 SvtPathOptions aPathOpt;
235 OUString aTempStr;
236 OUStringBuffer aFullPathBuf;
238 for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
240 if ( pValues[nProp].hasValue() )
242 switch ( pValues[nProp].getValueTypeClass() )
244 case css::uno::TypeClass_STRING :
246 // multi paths
247 if ( pValues[nProp] >>= aTempStr )
248 aFullPathBuf = aPathOpt.SubstituteVariable( aTempStr );
249 else
251 SAL_WARN( "unotools.config", "any operator >>= failed" );
253 break;
256 case css::uno::TypeClass_SEQUENCE :
258 // single paths
259 aFullPathBuf.setLength(0);
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 aFullPathBuf.append(aPathOpt.SubstituteVariable( aList[ nPosition ] ));
267 if ( nPosition < nCount-1 )
268 aFullPathBuf.append(";");
271 else
273 SAL_WARN( "unotools.config", "any operator >>= failed" );
275 break;
278 default:
280 SAL_WARN( "unotools.config", "Wrong any type" );
284 auto aFullPath = aFullPathBuf.makeStringAndClear();
285 switch ( nProp )
287 case DEFAULTPATH_ADDIN: m_aAddinPath = aFullPath; break;
288 case DEFAULTPATH_AUTOCORRECT: m_aAutoCorrectPath = aFullPath; break;
289 case DEFAULTPATH_AUTOTEXT: m_aAutoTextPath = aFullPath; break;
290 case DEFAULTPATH_BACKUP: m_aBackupPath = aFullPath; break;
291 case DEFAULTPATH_BASIC: m_aBasicPath = aFullPath; break;
292 case DEFAULTPATH_BITMAP: m_aBitmapPath = aFullPath; break;
293 case DEFAULTPATH_CONFIG: m_aConfigPath = aFullPath; break;
294 case DEFAULTPATH_DICTIONARY: m_aDictionaryPath = aFullPath; break;
295 case DEFAULTPATH_FAVORITES: m_aFavoritesPath = aFullPath; break;
296 case DEFAULTPATH_FILTER: m_aFilterPath = aFullPath; break;
297 case DEFAULTPATH_GALLERY: m_aGalleryPath = aFullPath; break;
298 case DEFAULTPATH_GRAPHIC: m_aGraphicPath = aFullPath; break;
299 case DEFAULTPATH_HELP: m_aHelpPath = aFullPath; break;
300 case DEFAULTPATH_LINGUISTIC: m_aLinguisticPath = aFullPath; break;
301 case DEFAULTPATH_MODULE: m_aModulePath = aFullPath; break;
302 case DEFAULTPATH_PALETTE: m_aPalettePath = aFullPath; break;
303 case DEFAULTPATH_PLUGIN: m_aPluginPath = aFullPath; break;
304 case DEFAULTPATH_TEMP: m_aTempPath = aFullPath; break;
305 case DEFAULTPATH_TEMPLATE: m_aTemplatePath = aFullPath; break;
306 case DEFAULTPATH_USERCONFIG: m_aUserConfigPath = aFullPath; break;
307 case DEFAULTPATH_WORK: m_aWorkPath = aFullPath; break;
308 case DEFAULTPATH_CLASSIFICATION: m_aClassificationPath = aFullPath;break;
309 case DEFAULTPATH_USERDICTIONARY: break;
311 default:
312 SAL_WARN( "unotools.config", "invalid index to load a default path" );
319 SvtDefaultOptions_Impl::~SvtDefaultOptions_Impl()
321 if ( IsModified() )
322 Commit();
325 // class SvtDefaultOptions -----------------------------------------------
326 namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }
328 SvtDefaultOptions::SvtDefaultOptions()
330 // Global access, must be guarded (multithreading)
331 ::osl::MutexGuard aGuard( lclMutex::get() );
332 pImpl = g_pOptions.lock();
333 if ( !pImpl )
335 pImpl = std::make_shared<SvtDefaultOptions_Impl>();
336 g_pOptions = pImpl;
337 ItemHolder1::holdConfigItem(EItem::DefaultOptions);
341 SvtDefaultOptions::~SvtDefaultOptions()
343 // Global access, must be guarded (multithreading)
344 ::osl::MutexGuard aGuard( lclMutex::get() );
345 pImpl.reset();
348 OUString SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const
350 return pImpl->GetDefaultPath( nId );
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */