1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 
31 #include <unotools/defaultoptions.hxx>
32 #include <unotools/pathoptions.hxx>
33 #include <unotools/configitem.hxx>
34 #include <unotools/configmgr.hxx>
35 #include <tools/debug.hxx>
36 #include <tools/resmgr.hxx>
37 #include <tools/urlobj.hxx>
38 #include <com/sun/star/uno/Any.hxx>
39 #include <com/sun/star/uno/Sequence.hxx>
40 #include <osl/mutex.hxx>
41 
42 #include <vos/process.hxx>
43 #include <unotools/localfilehelper.hxx>
44 #include <rtl/instance.hxx>
45 
46 #include <itemholder1.hxx>
47 
48 using namespace osl;
49 using namespace utl;
50 using namespace rtl;
51 using namespace com::sun::star::uno;
52 
53 // define ----------------------------------------------------------------
54 
55 #define ASCII_STR(s)				OUString( RTL_CONSTASCII_USTRINGPARAM(s) )
56 
57 #define DEFAULTPATH__ADDIN			0
58 #define DEFAULTPATH__AUTOCORRECT	1
59 #define DEFAULTPATH__AUTOTEXT		2
60 #define DEFAULTPATH__BACKUP			3
61 #define DEFAULTPATH__BASIC			4
62 #define DEFAULTPATH__BITMAP			5
63 #define DEFAULTPATH__CONFIG			6
64 #define DEFAULTPATH__DICTIONARY		7
65 #define DEFAULTPATH__FAVORITES		8
66 #define DEFAULTPATH__FILTER			9
67 #define DEFAULTPATH__GALLERY		10
68 #define DEFAULTPATH__GRAPHIC		11
69 #define DEFAULTPATH__HELP			12
70 #define DEFAULTPATH__LINGUISTIC		13
71 #define DEFAULTPATH__MODULE			14
72 #define DEFAULTPATH__PALETTE		15
73 #define DEFAULTPATH__PLUGIN			16
74 #define DEFAULTPATH__TEMP			17
75 #define DEFAULTPATH__TEMPLATE		18
76 #define DEFAULTPATH__USERCONFIG		19
77 #define DEFAULTPATH__WORK           20
78 #define DEFAULTPATH__USERDICTIONARY 21
79 
80 // class SvtDefaultOptions_Impl ------------------------------------------
81 
82 class SvtDefaultOptions_Impl : public utl::ConfigItem
83 {
84 public:
85 	String			m_aAddinPath;
86 	String			m_aAutoCorrectPath;
87 	String			m_aAutoTextPath;
88 	String			m_aBackupPath;
89 	String			m_aBasicPath;
90 	String			m_aBitmapPath;
91 	String			m_aConfigPath;
92 	String			m_aDictionaryPath;
93 	String			m_aFavoritesPath;
94 	String			m_aFilterPath;
95 	String			m_aGalleryPath;
96 	String			m_aGraphicPath;
97 	String			m_aHelpPath;
98 	String			m_aLinguisticPath;
99 	String			m_aModulePath;
100 	String			m_aPalettePath;
101 	String			m_aPluginPath;
102 	String			m_aTempPath;
103 	String			m_aTemplatePath;
104 	String			m_aUserConfigPath;
105 	String			m_aWorkPath;
106     String          m_aUserDictionaryPath;
107 
108 					SvtDefaultOptions_Impl();
109 
110 	String			GetDefaultPath( sal_uInt16 nId ) const;
111     virtual void    Commit();
112 	virtual void    Notify( const com::sun::star::uno::Sequence<rtl::OUString>& aPropertyNames);
113 };
114 
115 // global ----------------------------------------------------------------
116 
117 static SvtDefaultOptions_Impl*	pOptions = NULL;
118 static sal_Int32				nRefCount = 0;
119 
120 typedef String SvtDefaultOptions_Impl:: *PathStrPtr;
121 
122 struct PathToDefaultMapping_Impl
123 {
124 	SvtPathOptions::Pathes	_ePath;
125 	PathStrPtr				_pDefaultPath;
126 };
127 
128 static PathToDefaultMapping_Impl __READONLY_DATA PathMap_Impl[] =
129 {
130     { SvtPathOptions::PATH_ADDIN,           &SvtDefaultOptions_Impl::m_aAddinPath },
131     { SvtPathOptions::PATH_AUTOCORRECT,     &SvtDefaultOptions_Impl::m_aAutoCorrectPath },
132     { SvtPathOptions::PATH_AUTOTEXT,        &SvtDefaultOptions_Impl::m_aAutoTextPath },
133     { SvtPathOptions::PATH_BACKUP,          &SvtDefaultOptions_Impl::m_aBackupPath },
134     { SvtPathOptions::PATH_BASIC,           &SvtDefaultOptions_Impl::m_aBasicPath },
135     { SvtPathOptions::PATH_BITMAP,          &SvtDefaultOptions_Impl::m_aBitmapPath },
136     { SvtPathOptions::PATH_CONFIG,          &SvtDefaultOptions_Impl::m_aConfigPath },
137     { SvtPathOptions::PATH_DICTIONARY,      &SvtDefaultOptions_Impl::m_aDictionaryPath },
138     { SvtPathOptions::PATH_FAVORITES,       &SvtDefaultOptions_Impl::m_aFavoritesPath },
139     { SvtPathOptions::PATH_FILTER,          &SvtDefaultOptions_Impl::m_aFilterPath },
140     { SvtPathOptions::PATH_GALLERY,         &SvtDefaultOptions_Impl::m_aGalleryPath },
141     { SvtPathOptions::PATH_GRAPHIC,         &SvtDefaultOptions_Impl::m_aGraphicPath },
142     { SvtPathOptions::PATH_HELP,            &SvtDefaultOptions_Impl::m_aHelpPath },
143     { SvtPathOptions::PATH_LINGUISTIC,      &SvtDefaultOptions_Impl::m_aLinguisticPath },
144     { SvtPathOptions::PATH_MODULE,          &SvtDefaultOptions_Impl::m_aModulePath },
145     { SvtPathOptions::PATH_PALETTE,         &SvtDefaultOptions_Impl::m_aPalettePath },
146     { SvtPathOptions::PATH_PLUGIN,          &SvtDefaultOptions_Impl::m_aPluginPath },
147     { SvtPathOptions::PATH_TEMP,            &SvtDefaultOptions_Impl::m_aTempPath },
148     { SvtPathOptions::PATH_TEMPLATE,        &SvtDefaultOptions_Impl::m_aTemplatePath },
149     { SvtPathOptions::PATH_USERCONFIG,      &SvtDefaultOptions_Impl::m_aUserConfigPath },
150     { SvtPathOptions::PATH_WORK,            &SvtDefaultOptions_Impl::m_aWorkPath }
151 };
152 
153 // functions -------------------------------------------------------------
154 
155 Sequence< OUString > GetDefaultPropertyNames()
156 {
157 	static const char* aPropNames[] =
158 	{
159 		"Addin",			// PATH_ADDIN
160 		"AutoCorrect",		// PATH_AUTOCORRECT
161 		"AutoText",			// PATH_AUTOTEXT
162 		"Backup",			// PATH_BACKUP
163 		"Basic",			// PATH_BASIC
164 		"Bitmap",			// PATH_BITMAP
165 		"Config",			// PATH_CONFIG
166 		"Dictionary",		// PATH_DICTIONARY
167 		"Favorite",			// PATH_FAVORITES
168 		"Filter",			// PATH_FILTER
169 		"Gallery",			// PATH_GALLERY
170 		"Graphic",			// PATH_GRAPHIC
171 		"Help",				// PATH_HELP
172 		"Linguistic",		// PATH_LINGUISTIC
173 		"Module",			// PATH_MODULE
174 		"Palette",			// PATH_PALETTE
175 		"Plugin",			// PATH_PLUGIN
176 		"Temp",		 		// PATH_TEMP
177 		"Template",			// PATH_TEMPLATE
178 		"UserConfig",		// PATH_USERCONFIG
179 		"Work"				// PATH_WORK
180 	};
181 
182 	const int nCount = sizeof( aPropNames ) / sizeof( const char* );
183 	Sequence< OUString > aNames( nCount );
184 	OUString* pNames = aNames.getArray();
185 	for ( int i = 0; i < nCount; i++ )
186 		pNames[i] = OUString::createFromAscii( aPropNames[i] );
187 
188 	return aNames;
189 }
190 
191 void SvtDefaultOptions_Impl::Notify( const Sequence< rtl::OUString >&  )
192 {
193 	// no notification, will never be changed
194 }
195 
196 void SvtDefaultOptions_Impl::Commit()
197 {
198 	// will never be changed
199 }
200 
201 // class SvtDefaultOptions_Impl ------------------------------------------
202 
203 String SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const
204 {
205 	String aRet;
206 	sal_uInt16 nIdx = 0;
207 
208 	while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_WORK )
209 	{
210 		if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath )
211 		{
212 			aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath);
213             if ( nId == SvtPathOptions::PATH_ADDIN ||
214                  nId == SvtPathOptions::PATH_FILTER ||
215                  nId == SvtPathOptions::PATH_HELP ||
216                  nId == SvtPathOptions::PATH_MODULE ||
217                  nId == SvtPathOptions::PATH_PLUGIN )
218             {
219                 String aTmp;
220                 ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aRet, aTmp );
221                 aRet = aTmp;
222             }
223 
224 			break;
225 		}
226 		++nIdx;
227 	}
228 
229 	return aRet;
230 }
231 
232 // -----------------------------------------------------------------------
233 
234 SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( ASCII_STR("Office.Common/Path/Default") )
235 {
236 	/*ConfigManager* pCfgMgr =*/ ConfigManager::GetConfigManager();
237 	Sequence< OUString > aNames = GetDefaultPropertyNames();
238 	Sequence< Any > aValues = GetProperties( aNames );
239 	EnableNotification( aNames );
240 	const Any* pValues = aValues.getConstArray();
241 	DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" );
242 	if ( aValues.getLength() == aNames.getLength() )
243 	{
244 		SvtPathOptions aPathOpt;
245 		OUString aTempStr, aFullPath;
246 
247 		for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
248 		{
249 			if ( pValues[nProp].hasValue() )
250 			{
251 				switch ( pValues[nProp].getValueTypeClass() )
252 				{
253 					case ::com::sun::star::uno::TypeClass_STRING :
254 					{
255 						// multi pathes
256 						if ( pValues[nProp] >>= aTempStr )
257                             aFullPath = aPathOpt.SubstituteVariable( aTempStr );
258 						else
259 						{
260 							DBG_ERRORFILE( "any operator >>= failed" );
261 						}
262 						break;
263 					}
264 
265 					case ::com::sun::star::uno::TypeClass_SEQUENCE :
266 					{
267 						// single pathes
268 						aFullPath = OUString();
269 						Sequence < OUString > aList;
270 						if ( pValues[nProp] >>= aList )
271 						{
272 							sal_Int32 nCount = aList.getLength();
273 							for ( sal_Int32 nPosition = 0; nPosition < nCount; ++nPosition )
274 							{
275                                 aTempStr = aPathOpt.SubstituteVariable( aList[ nPosition ] );
276 								aFullPath += aTempStr;
277 								if ( nPosition < nCount-1 )
278 									aFullPath += OUString( RTL_CONSTASCII_USTRINGPARAM(";") );
279 							}
280 						}
281 						else
282 						{
283 							DBG_ERRORFILE( "any operator >>= failed" );
284 						}
285 						break;
286 					}
287 
288 					default:
289 					{
290 						DBG_ERRORFILE( "Wrong any type" );
291 					}
292 				}
293 
294 				switch ( nProp )
295 				{
296 					case DEFAULTPATH__ADDIN:			m_aAddinPath = String( aFullPath );			break;
297 					case DEFAULTPATH__AUTOCORRECT:		m_aAutoCorrectPath = String( aFullPath );	break;
298 					case DEFAULTPATH__AUTOTEXT:			m_aAutoTextPath = String( aFullPath );		break;
299 					case DEFAULTPATH__BACKUP:			m_aBackupPath = String( aFullPath );		break;
300 					case DEFAULTPATH__BASIC:			m_aBasicPath = String( aFullPath );			break;
301 					case DEFAULTPATH__BITMAP:			m_aBitmapPath = String( aFullPath );		break;
302 					case DEFAULTPATH__CONFIG:			m_aConfigPath = String( aFullPath );		break;
303 					case DEFAULTPATH__DICTIONARY:		m_aDictionaryPath = String( aFullPath );	break;
304 					case DEFAULTPATH__FAVORITES:		m_aFavoritesPath = String( aFullPath );		break;
305 					case DEFAULTPATH__FILTER:			m_aFilterPath = String( aFullPath );		break;
306 					case DEFAULTPATH__GALLERY:			m_aGalleryPath = String( aFullPath );		break;
307 					case DEFAULTPATH__GRAPHIC:			m_aGraphicPath = String( aFullPath );		break;
308 					case DEFAULTPATH__HELP:				m_aHelpPath = String( aFullPath );			break;
309 					case DEFAULTPATH__LINGUISTIC:		m_aLinguisticPath = String( aFullPath );	break;
310 					case DEFAULTPATH__MODULE:			m_aModulePath = String( aFullPath );		break;
311 					case DEFAULTPATH__PALETTE:			m_aPalettePath = String( aFullPath );		break;
312 					case DEFAULTPATH__PLUGIN:			m_aPluginPath = String( aFullPath );		break;
313 					case DEFAULTPATH__TEMP:				m_aTempPath = String( aFullPath );			break;
314 					case DEFAULTPATH__TEMPLATE:			m_aTemplatePath = String( aFullPath );		break;
315 					case DEFAULTPATH__USERCONFIG:		m_aUserConfigPath = String( aFullPath );	break;
316 					case DEFAULTPATH__WORK:				m_aWorkPath = String( aFullPath );			break;
317                     case DEFAULTPATH__USERDICTIONARY:   m_aUserDictionaryPath = String( aFullPath );break;
318 
319 					default:
320 						DBG_ERRORFILE( "invalid index to load a default path" );
321 				}
322 			}
323 		}
324 	}
325 }
326 
327 // class SvtDefaultOptions -----------------------------------------------
328 namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; }
329 
330 SvtDefaultOptions::SvtDefaultOptions()
331 {
332     // Global access, must be guarded (multithreading)
333     ::osl::MutexGuard aGuard( lclMutex::get() );
334     if ( !pOptions )
335 	{
336         pOptions = new SvtDefaultOptions_Impl;
337 		ItemHolder1::holdConfigItem(E_DEFAULTOPTIONS);
338 	}
339     ++nRefCount;
340     pImp = pOptions;
341 }
342 
343 // -----------------------------------------------------------------------
344 
345 SvtDefaultOptions::~SvtDefaultOptions()
346 {
347     // Global access, must be guarded (multithreading)
348     ::osl::MutexGuard aGuard( lclMutex::get() );
349     if ( !--nRefCount )
350 	{
351 		if ( pOptions->IsModified() )
352 			pOptions->Commit();
353         DELETEZ( pOptions );
354 	}
355 }
356 
357 // -----------------------------------------------------------------------
358 
359 String SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const
360 {
361 	return pImp->GetDefaultPath( nId );
362 }
363 
364