1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_unotools.hxx"
26 
27 #include <unotools/defaultoptions.hxx>
28 #include <unotools/pathoptions.hxx>
29 #include <unotools/configitem.hxx>
30 #include <unotools/configmgr.hxx>
31 #include <tools/debug.hxx>
32 #include <tools/resmgr.hxx>
33 #include <tools/urlobj.hxx>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/uno/Sequence.hxx>
36 #include <osl/mutex.hxx>
37 
38 #include <vos/process.hxx>
39 #include <unotools/localfilehelper.hxx>
40 #include <rtl/instance.hxx>
41 
42 #include <itemholder1.hxx>
43 
44 using namespace osl;
45 using namespace utl;
46 using namespace rtl;
47 using namespace com::sun::star::uno;
48 
49 // define ----------------------------------------------------------------
50 
51 #define ASCII_STR(s)				OUString( RTL_CONSTASCII_USTRINGPARAM(s) )
52 
53 #define DEFAULTPATH__ADDIN			0
54 #define DEFAULTPATH__AUTOCORRECT	1
55 #define DEFAULTPATH__AUTOTEXT		2
56 #define DEFAULTPATH__BACKUP			3
57 #define DEFAULTPATH__BASIC			4
58 #define DEFAULTPATH__BITMAP			5
59 #define DEFAULTPATH__CONFIG			6
60 #define DEFAULTPATH__DICTIONARY		7
61 #define DEFAULTPATH__FAVORITES		8
62 #define DEFAULTPATH__FILTER			9
63 #define DEFAULTPATH__GALLERY		10
64 #define DEFAULTPATH__GRAPHIC		11
65 #define DEFAULTPATH__HELP			12
66 #define DEFAULTPATH__LINGUISTIC		13
67 #define DEFAULTPATH__MODULE			14
68 #define DEFAULTPATH__PALETTE		15
69 #define DEFAULTPATH__PLUGIN			16
70 #define DEFAULTPATH__TEMP			17
71 #define DEFAULTPATH__TEMPLATE		18
72 #define DEFAULTPATH__USERCONFIG		19
73 #define DEFAULTPATH__WORK           20
74 #define DEFAULTPATH__USERDICTIONARY 21
75 
76 // class SvtDefaultOptions_Impl ------------------------------------------
77 
78 class SvtDefaultOptions_Impl : public utl::ConfigItem
79 {
80 public:
81 	String			m_aAddinPath;
82 	String			m_aAutoCorrectPath;
83 	String			m_aAutoTextPath;
84 	String			m_aBackupPath;
85 	String			m_aBasicPath;
86 	String			m_aBitmapPath;
87 	String			m_aConfigPath;
88 	String			m_aDictionaryPath;
89 	String			m_aFavoritesPath;
90 	String			m_aFilterPath;
91 	String			m_aGalleryPath;
92 	String			m_aGraphicPath;
93 	String			m_aHelpPath;
94 	String			m_aLinguisticPath;
95 	String			m_aModulePath;
96 	String			m_aPalettePath;
97 	String			m_aPluginPath;
98 	String			m_aTempPath;
99 	String			m_aTemplatePath;
100 	String			m_aUserConfigPath;
101 	String			m_aWorkPath;
102     String          m_aUserDictionaryPath;
103 
104 					SvtDefaultOptions_Impl();
105 
106 	String			GetDefaultPath( sal_uInt16 nId ) const;
107     virtual void    Commit();
108 	virtual void    Notify( const com::sun::star::uno::Sequence<rtl::OUString>& aPropertyNames);
109 };
110 
111 // global ----------------------------------------------------------------
112 
113 static SvtDefaultOptions_Impl*	pOptions = NULL;
114 static sal_Int32				nRefCount = 0;
115 
116 typedef String SvtDefaultOptions_Impl:: *PathStrPtr;
117 
118 struct PathToDefaultMapping_Impl
119 {
120 	SvtPathOptions::Pathes	_ePath;
121 	PathStrPtr				_pDefaultPath;
122 };
123 
124 static PathToDefaultMapping_Impl __READONLY_DATA PathMap_Impl[] =
125 {
126     { SvtPathOptions::PATH_ADDIN,           &SvtDefaultOptions_Impl::m_aAddinPath },
127     { SvtPathOptions::PATH_AUTOCORRECT,     &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
128     { SvtPathOptions::PATH_AUTOTEXT,        &SvtDefaultOptions_Impl::m_aAutoTextPath },
129     { SvtPathOptions::PATH_BACKUP,          &SvtDefaultOptions_Impl::m_aBackupPath },
130     { SvtPathOptions::PATH_BASIC,           &SvtDefaultOptions_Impl::m_aBasicPath },
131     { SvtPathOptions::PATH_BITMAP,          &SvtDefaultOptions_Impl::m_aBitmapPath },
132     { SvtPathOptions::PATH_CONFIG,          &SvtDefaultOptions_Impl::m_aConfigPath },
133     { SvtPathOptions::PATH_DICTIONARY,      &SvtDefaultOptions_Impl::m_aDictionaryPath },
134     { SvtPathOptions::PATH_FAVORITES,       &SvtDefaultOptions_Impl::m_aFavoritesPath },
135     { SvtPathOptions::PATH_FILTER,          &SvtDefaultOptions_Impl::m_aFilterPath },
136     { SvtPathOptions::PATH_GALLERY,         &SvtDefaultOptions_Impl::m_aGalleryPath },
137     { SvtPathOptions::PATH_GRAPHIC,         &SvtDefaultOptions_Impl::m_aGraphicPath },
138     { SvtPathOptions::PATH_HELP,            &SvtDefaultOptions_Impl::m_aHelpPath },
139     { SvtPathOptions::PATH_LINGUISTIC,      &SvtDefaultOptions_Impl::m_aLinguisticPath },
140     { SvtPathOptions::PATH_MODULE,          &SvtDefaultOptions_Impl::m_aModulePath },
141     { SvtPathOptions::PATH_PALETTE,         &SvtDefaultOptions_Impl::m_aPalettePath },
142     { SvtPathOptions::PATH_PLUGIN,          &SvtDefaultOptions_Impl::m_aPluginPath },
143     { SvtPathOptions::PATH_TEMP,            &SvtDefaultOptions_Impl::m_aTempPath },
144     { SvtPathOptions::PATH_TEMPLATE,        &SvtDefaultOptions_Impl::m_aTemplatePath },
145     { SvtPathOptions::PATH_USERCONFIG,      &SvtDefaultOptions_Impl::m_aUserConfigPath },
146     { SvtPathOptions::PATH_WORK,            &SvtDefaultOptions_Impl::m_aWorkPath }
147 };
148 
149 // functions -------------------------------------------------------------
150 
GetDefaultPropertyNames()151 Sequence< OUString > GetDefaultPropertyNames()
152 {
153 	static const char* aPropNames[] =
154 	{
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 	};
177 
178 	const int nCount = sizeof( aPropNames ) / sizeof( const char* );
179 	Sequence< OUString > aNames( nCount );
180 	OUString* pNames = aNames.getArray();
181 	for ( int i = 0; i < nCount; i++ )
182 		pNames[i] = OUString::createFromAscii( aPropNames[i] );
183 
184 	return aNames;
185 }
186 
Notify(const Sequence<rtl::OUString> &)187 void SvtDefaultOptions_Impl::Notify( const Sequence< rtl::OUString >&  )
188 {
189 	// no notification, will never be changed
190 }
191 
Commit()192 void SvtDefaultOptions_Impl::Commit()
193 {
194 	// will never be changed
195 }
196 
197 // class SvtDefaultOptions_Impl ------------------------------------------
198 
GetDefaultPath(sal_uInt16 nId) const199 String SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const
200 {
201 	String aRet;
202 	sal_uInt16 nIdx = 0;
203 
204 	while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_WORK )
205 	{
206 		if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
207 		{
208 			aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
209             if ( nId == SvtPathOptions::PATH_ADDIN ||
210                  nId == SvtPathOptions::PATH_FILTER ||
211                  nId == SvtPathOptions::PATH_HELP ||
212                  nId == SvtPathOptions::PATH_MODULE ||
213                  nId == SvtPathOptions::PATH_PLUGIN )
214             {
215                 String aTmp;
216                 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aRet, aTmp );
217                 aRet = aTmp;
218             }
219 
220 			break;
221 		}
222 		++nIdx;
223 	}
224 
225 	return aRet;
226 }
227 
228 // -----------------------------------------------------------------------
229 
SvtDefaultOptions_Impl()230 SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( ASCII_STR("Office.Common/Path/Default") )
231 {
232 	/*ConfigManager* pCfgMgr =*/ ConfigManager::GetConfigManager();
233 	Sequence< OUString > aNames = GetDefaultPropertyNames();
234 	Sequence< Any > aValues = GetProperties( aNames );
235 	EnableNotification( aNames );
236 	const Any* pValues = aValues.getConstArray();
237 	DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
238 	if ( aValues.getLength() == aNames.getLength() )
239 	{
240 		SvtPathOptions aPathOpt;
241 		OUString aTempStr, aFullPath;
242 
243 		for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
244 		{
245 			if ( pValues[nProp].hasValue() )
246 			{
247 				switch ( pValues[nProp].getValueTypeClass() )
248 				{
249 					case ::com::sun::star::uno::TypeClass_STRING :
250 					{
251 						// multi pathes
252 						if ( pValues[nProp] >>= aTempStr )
253                             aFullPath = aPathOpt.SubstituteVariable( aTempStr );
254 						else
255 						{
256 							DBG_ERRORFILE( "any operator >>= failed" );
257 						}
258 						break;
259 					}
260 
261 					case ::com::sun::star::uno::TypeClass_SEQUENCE :
262 					{
263 						// single pathes
264 						aFullPath = OUString();
265 						Sequence < OUString > aList;
266 						if ( pValues[nProp] >>= aList )
267 						{
268 							sal_Int32 nCount = aList.getLength();
269 							for ( sal_Int32 nPosition = 0; nPosition < nCount; ++nPosition )
270 							{
271                                 aTempStr = aPathOpt.SubstituteVariable( aList[ nPosition ] );
272 								aFullPath += aTempStr;
273 								if ( nPosition < nCount-1 )
274 									aFullPath += OUString( RTL_CONSTASCII_USTRINGPARAM(";") );
275 							}
276 						}
277 						else
278 						{
279 							DBG_ERRORFILE( "any operator >>= failed" );
280 						}
281 						break;
282 					}
283 
284 					default:
285 					{
286 						DBG_ERRORFILE( "Wrong any type" );
287 					}
288 				}
289 
290 				switch ( nProp )
291 				{
292 					case DEFAULTPATH__ADDIN:			m_aAddinPath = String( aFullPath );			break;
293 					case DEFAULTPATH__AUTOCORRECT:		m_aAutoCorrectPath = String( aFullPath );	break;
294 					case DEFAULTPATH__AUTOTEXT:			m_aAutoTextPath = String( aFullPath );		break;
295 					case DEFAULTPATH__BACKUP:			m_aBackupPath = String( aFullPath );		break;
296 					case DEFAULTPATH__BASIC:			m_aBasicPath = String( aFullPath );			break;
297 					case DEFAULTPATH__BITMAP:			m_aBitmapPath = String( aFullPath );		break;
298 					case DEFAULTPATH__CONFIG:			m_aConfigPath = String( aFullPath );		break;
299 					case DEFAULTPATH__DICTIONARY:		m_aDictionaryPath = String( aFullPath );	break;
300 					case DEFAULTPATH__FAVORITES:		m_aFavoritesPath = String( aFullPath );		break;
301 					case DEFAULTPATH__FILTER:			m_aFilterPath = String( aFullPath );		break;
302 					case DEFAULTPATH__GALLERY:			m_aGalleryPath = String( aFullPath );		break;
303 					case DEFAULTPATH__GRAPHIC:			m_aGraphicPath = String( aFullPath );		break;
304 					case DEFAULTPATH__HELP:				m_aHelpPath = String( aFullPath );			break;
305 					case DEFAULTPATH__LINGUISTIC:		m_aLinguisticPath = String( aFullPath );	break;
306 					case DEFAULTPATH__MODULE:			m_aModulePath = String( aFullPath );		break;
307 					case DEFAULTPATH__PALETTE:			m_aPalettePath = String( aFullPath );		break;
308 					case DEFAULTPATH__PLUGIN:			m_aPluginPath = String( aFullPath );		break;
309 					case DEFAULTPATH__TEMP:				m_aTempPath = String( aFullPath );			break;
310 					case DEFAULTPATH__TEMPLATE:			m_aTemplatePath = String( aFullPath );		break;
311 					case DEFAULTPATH__USERCONFIG:		m_aUserConfigPath = String( aFullPath );	break;
312 					case DEFAULTPATH__WORK:				m_aWorkPath = String( aFullPath );			break;
313                     case DEFAULTPATH__USERDICTIONARY:   m_aUserDictionaryPath = String( aFullPath );break;
314 
315 					default:
316 						DBG_ERRORFILE( "invalid index to load a default path" );
317 				}
318 			}
319 		}
320 	}
321 }
322 
323 // class SvtDefaultOptions -----------------------------------------------
324 namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }
325 
SvtDefaultOptions()326 SvtDefaultOptions::SvtDefaultOptions()
327 {
328     // Global access, must be guarded (multithreading)
329     ::osl::MutexGuard aGuard( lclMutex::get() );
330     if ( !pOptions )
331 	{
332         pOptions = new SvtDefaultOptions_Impl;
333 		ItemHolder1::holdConfigItem(E_DEFAULTOPTIONS);
334 	}
335     ++nRefCount;
336     pImp = pOptions;
337 }
338 
339 // -----------------------------------------------------------------------
340 
~SvtDefaultOptions()341 SvtDefaultOptions::~SvtDefaultOptions()
342 {
343     // Global access, must be guarded (multithreading)
344     ::osl::MutexGuard aGuard( lclMutex::get() );
345     if ( !--nRefCount )
346 	{
347 		if ( pOptions->IsModified() )
348 			pOptions->Commit();
349         DELETEZ( pOptions );
350 	}
351 }
352 
353 // -----------------------------------------------------------------------
354 
GetDefaultPath(sal_uInt16 nId) const355 String SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const
356 {
357 	return pImp->GetDefaultPath( nId );
358 }
359 
360