update emoji autocorrect entries from po-files
[LibreOffice.git] / unotools / source / config / defaultoptions.cxx
blobb937a10d84bc1577b32e91f4ef1e4a660f75ddbd
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 <sal/log.hxx>
23 #include <unotools/defaultoptions.hxx>
24 #include <unotools/pathoptions.hxx>
25 #include <unotools/configitem.hxx>
26 #include <unotools/configmgr.hxx>
27 #include <tools/debug.hxx>
28 #include <tools/solar.h>
29 #include <com/sun/star/uno/Any.hxx>
30 #include <com/sun/star/uno/Sequence.hxx>
31 #include <osl/mutex.hxx>
33 #include <unotools/localfilehelper.hxx>
34 #include <rtl/instance.hxx>
36 #include "itemholder1.hxx"
38 using namespace osl;
39 using namespace utl;
40 using namespace com::sun::star::uno;
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 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_aUserDictionaryPath;
93 SvtDefaultOptions_Impl();
95 OUString GetDefaultPath( sal_uInt16 nId ) const;
96 virtual void Notify( const com::sun::star::uno::Sequence<OUString>& aPropertyNames) SAL_OVERRIDE;
98 private:
99 virtual void ImplCommit() SAL_OVERRIDE;
102 // global ----------------------------------------------------------------
104 static SvtDefaultOptions_Impl* pOptions = NULL;
105 static sal_Int32 nRefCount = 0;
107 typedef OUString SvtDefaultOptions_Impl:: *PathStrPtr;
109 struct PathToDefaultMapping_Impl
111 SvtPathOptions::Paths _ePath;
112 PathStrPtr _pDefaultPath;
115 static PathToDefaultMapping_Impl const PathMap_Impl[] =
117 { SvtPathOptions::PATH_ADDIN, &SvtDefaultOptions_Impl::m_aAddinPath },
118 { SvtPathOptions::PATH_AUTOCORRECT, &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
119 { SvtPathOptions::PATH_AUTOTEXT, &SvtDefaultOptions_Impl::m_aAutoTextPath },
120 { SvtPathOptions::PATH_BACKUP, &SvtDefaultOptions_Impl::m_aBackupPath },
121 { SvtPathOptions::PATH_BASIC, &SvtDefaultOptions_Impl::m_aBasicPath },
122 { SvtPathOptions::PATH_BITMAP, &SvtDefaultOptions_Impl::m_aBitmapPath },
123 { SvtPathOptions::PATH_CONFIG, &SvtDefaultOptions_Impl::m_aConfigPath },
124 { SvtPathOptions::PATH_DICTIONARY, &SvtDefaultOptions_Impl::m_aDictionaryPath },
125 { SvtPathOptions::PATH_FAVORITES, &SvtDefaultOptions_Impl::m_aFavoritesPath },
126 { SvtPathOptions::PATH_FILTER, &SvtDefaultOptions_Impl::m_aFilterPath },
127 { SvtPathOptions::PATH_GALLERY, &SvtDefaultOptions_Impl::m_aGalleryPath },
128 { SvtPathOptions::PATH_GRAPHIC, &SvtDefaultOptions_Impl::m_aGraphicPath },
129 { SvtPathOptions::PATH_HELP, &SvtDefaultOptions_Impl::m_aHelpPath },
130 { SvtPathOptions::PATH_LINGUISTIC, &SvtDefaultOptions_Impl::m_aLinguisticPath },
131 { SvtPathOptions::PATH_MODULE, &SvtDefaultOptions_Impl::m_aModulePath },
132 { SvtPathOptions::PATH_PALETTE, &SvtDefaultOptions_Impl::m_aPalettePath },
133 { SvtPathOptions::PATH_PLUGIN, &SvtDefaultOptions_Impl::m_aPluginPath },
134 { SvtPathOptions::PATH_TEMP, &SvtDefaultOptions_Impl::m_aTempPath },
135 { SvtPathOptions::PATH_TEMPLATE, &SvtDefaultOptions_Impl::m_aTemplatePath },
136 { SvtPathOptions::PATH_USERCONFIG, &SvtDefaultOptions_Impl::m_aUserConfigPath },
137 { SvtPathOptions::PATH_WORK, &SvtDefaultOptions_Impl::m_aWorkPath }
140 // functions -------------------------------------------------------------
142 Sequence< OUString > GetDefaultPropertyNames()
144 static const char* aPropNames[] =
146 "Addin", // PATH_ADDIN
147 "AutoCorrect", // PATH_AUTOCORRECT
148 "AutoText", // PATH_AUTOTEXT
149 "Backup", // PATH_BACKUP
150 "Basic", // PATH_BASIC
151 "Bitmap", // PATH_BITMAP
152 "Config", // PATH_CONFIG
153 "Dictionary", // PATH_DICTIONARY
154 "Favorite", // PATH_FAVORITES
155 "Filter", // PATH_FILTER
156 "Gallery", // PATH_GALLERY
157 "Graphic", // PATH_GRAPHIC
158 "Help", // PATH_HELP
159 "Linguistic", // PATH_LINGUISTIC
160 "Module", // PATH_MODULE
161 "Palette", // PATH_PALETTE
162 "Plugin", // PATH_PLUGIN
163 "Temp", // PATH_TEMP
164 "Template", // PATH_TEMPLATE
165 "UserConfig", // PATH_USERCONFIG
166 "Work" // PATH_WORK
169 const int nCount = sizeof( aPropNames ) / sizeof( const char* );
170 Sequence< OUString > aNames( nCount );
171 OUString* pNames = aNames.getArray();
172 for ( int i = 0; i < nCount; i++ )
173 pNames[i] = OUString::createFromAscii( aPropNames[i] );
175 return aNames;
178 void SvtDefaultOptions_Impl::Notify( const Sequence< OUString >& )
180 // no notification, will never be changed
183 void SvtDefaultOptions_Impl::ImplCommit()
185 // will never be changed
188 // class SvtDefaultOptions_Impl ------------------------------------------
190 OUString SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const
192 OUString aRet;
193 sal_uInt16 nIdx = 0;
195 while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_WORK )
197 if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
199 aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
200 if ( nId == SvtPathOptions::PATH_ADDIN ||
201 nId == SvtPathOptions::PATH_FILTER ||
202 nId == SvtPathOptions::PATH_HELP ||
203 nId == SvtPathOptions::PATH_MODULE ||
204 nId == SvtPathOptions::PATH_PLUGIN )
206 OUString aTmp;
207 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aRet, aTmp );
208 aRet = aTmp;
211 break;
213 ++nIdx;
216 return aRet;
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.clear();
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 = aFullPath; break;
281 case DEFAULTPATH__AUTOCORRECT: m_aAutoCorrectPath = aFullPath; break;
282 case DEFAULTPATH__AUTOTEXT: m_aAutoTextPath = aFullPath; break;
283 case DEFAULTPATH__BACKUP: m_aBackupPath = aFullPath; break;
284 case DEFAULTPATH__BASIC: m_aBasicPath = aFullPath; break;
285 case DEFAULTPATH__BITMAP: m_aBitmapPath = aFullPath; break;
286 case DEFAULTPATH__CONFIG: m_aConfigPath = aFullPath; break;
287 case DEFAULTPATH__DICTIONARY: m_aDictionaryPath = aFullPath; break;
288 case DEFAULTPATH__FAVORITES: m_aFavoritesPath = aFullPath; break;
289 case DEFAULTPATH__FILTER: m_aFilterPath = aFullPath; break;
290 case DEFAULTPATH__GALLERY: m_aGalleryPath = aFullPath; break;
291 case DEFAULTPATH__GRAPHIC: m_aGraphicPath = aFullPath; break;
292 case DEFAULTPATH__HELP: m_aHelpPath = aFullPath; break;
293 case DEFAULTPATH__LINGUISTIC: m_aLinguisticPath = aFullPath; break;
294 case DEFAULTPATH__MODULE: m_aModulePath = aFullPath; break;
295 case DEFAULTPATH__PALETTE: m_aPalettePath = aFullPath; break;
296 case DEFAULTPATH__PLUGIN: m_aPluginPath = aFullPath; break;
297 case DEFAULTPATH__TEMP: m_aTempPath = aFullPath; break;
298 case DEFAULTPATH__TEMPLATE: m_aTemplatePath = aFullPath; break;
299 case DEFAULTPATH__USERCONFIG: m_aUserConfigPath = aFullPath; break;
300 case DEFAULTPATH__WORK: m_aWorkPath = aFullPath; break;
301 case DEFAULTPATH__USERDICTIONARY: m_aUserDictionaryPath = 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 SvtDefaultOptions::~SvtDefaultOptions()
329 // Global access, must be guarded (multithreading)
330 ::osl::MutexGuard aGuard( lclMutex::get() );
331 if ( !--nRefCount )
333 if ( pOptions->IsModified() )
334 pOptions->Commit();
335 DELETEZ( pOptions );
339 OUString SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const
341 return pImp->GetDefaultPath( nId );
344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */