merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / config / workingsetoptions.cxx
blob7a5a51341f7c54cf6ff81705302f15235b0156ac
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: workingsetoptions.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #ifndef GCC
34 #endif
36 //_________________________________________________________________________________________________________________
37 // includes
38 //_________________________________________________________________________________________________________________
40 #include <svtools/workingsetoptions.hxx>
41 #include <unotools/configmgr.hxx>
42 #include <unotools/configitem.hxx>
43 #include <tools/debug.hxx>
44 #include <com/sun/star/uno/Any.hxx>
45 #include <com/sun/star/uno/Sequence.hxx>
47 #include <itemholder1.hxx>
49 //_________________________________________________________________________________________________________________
50 // namespaces
51 //_________________________________________________________________________________________________________________
53 using namespace ::utl ;
54 using namespace ::rtl ;
55 using namespace ::osl ;
56 using namespace ::com::sun::star::uno ;
58 //_________________________________________________________________________________________________________________
59 // const
60 //_________________________________________________________________________________________________________________
62 #define ROOTNODE_WORKINGSET OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/WorkingSet"))
63 #define DEFAULT_WINDOWLIST Sequence< OUString >()
65 #define PROPERTYNAME_WINDOWLIST OUString(RTL_CONSTASCII_USTRINGPARAM("WindowList" ))
67 #define PROPERTYHANDLE_WINDOWLIST 0
69 #define PROPERTYCOUNT 1
71 //_________________________________________________________________________________________________________________
72 // private declarations!
73 //_________________________________________________________________________________________________________________
75 class SvtWorkingSetOptions_Impl : public ConfigItem
77 //-------------------------------------------------------------------------------------------------------------
78 // public methods
79 //-------------------------------------------------------------------------------------------------------------
81 public:
83 //---------------------------------------------------------------------------------------------------------
84 // constructor / destructor
85 //---------------------------------------------------------------------------------------------------------
87 SvtWorkingSetOptions_Impl();
88 ~SvtWorkingSetOptions_Impl();
90 //---------------------------------------------------------------------------------------------------------
91 // overloaded methods of baseclass
92 //---------------------------------------------------------------------------------------------------------
94 /*-****************************************************************************************************//**
95 @short called for notify of configmanager
96 @descr These method is called from the ConfigManager before application ends or from the
97 PropertyChangeListener if the sub tree broadcasts changes. You must update your
98 internal values.
100 @seealso baseclass ConfigItem
102 @param "seqPropertyNames" is the list of properties which should be updated.
103 @return -
105 @onerror -
106 *//*-*****************************************************************************************************/
108 virtual void Notify( const Sequence< OUString >& seqPropertyNames );
110 /*-****************************************************************************************************//**
111 @short write changes to configuration
112 @descr These method writes the changed values into the sub tree
113 and should always called in our destructor to guarantee consistency of config data.
115 @seealso baseclass ConfigItem
117 @param -
118 @return -
120 @onerror -
121 *//*-*****************************************************************************************************/
123 virtual void Commit();
125 //---------------------------------------------------------------------------------------------------------
126 // public interface
127 //---------------------------------------------------------------------------------------------------------
129 /*-****************************************************************************************************//**
130 @short access method to get internal values
131 @descr These method give us a chance to regulate acces to ouer internal values.
132 It's not used in the moment - but it's possible for the feature!
134 @seealso -
136 @param -
137 @return -
139 @onerror -
140 *//*-*****************************************************************************************************/
142 Sequence< OUString > GetWindowList( ) const ;
143 void SetWindowList( const Sequence< OUString >& seqWindowList ) ;
145 //-------------------------------------------------------------------------------------------------------------
146 // private methods
147 //-------------------------------------------------------------------------------------------------------------
149 private:
151 /*-****************************************************************************************************//**
152 @short return list of key names of ouer configuration management which represent oue module tree
153 @descr These methods return a static const list of key names. We need it to get needed values from our
154 configuration management.
156 @seealso -
158 @param -
159 @return A list of needed configuration keys is returned.
161 @onerror -
162 *//*-*****************************************************************************************************/
164 static Sequence< OUString > GetPropertyNames();
166 //-------------------------------------------------------------------------------------------------------------
167 // private member
168 //-------------------------------------------------------------------------------------------------------------
170 private:
172 Sequence< OUString > m_seqWindowList ;
175 //_________________________________________________________________________________________________________________
176 // definitions
177 //_________________________________________________________________________________________________________________
179 //*****************************************************************************************************************
180 // constructor
181 //*****************************************************************************************************************
182 SvtWorkingSetOptions_Impl::SvtWorkingSetOptions_Impl()
183 // Init baseclasses first
184 : ConfigItem ( ROOTNODE_WORKINGSET )
185 // Init member then.
186 , m_seqWindowList ( DEFAULT_WINDOWLIST )
188 // Use our static list of configuration keys to get his values.
189 Sequence< OUString > seqNames = GetPropertyNames ( );
190 Sequence< Any > seqValues = GetProperties ( seqNames );
192 // Safe impossible cases.
193 // We need values from ALL configuration keys.
194 // Follow assignment use order of values in relation to our list of key names!
195 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtWorkingSetOptions_Impl::SvtWorkingSetOptions_Impl()\nI miss some values of configuration keys!\n" );
197 // Copy values from list in right order to ouer internal member.
198 sal_Int32 nPropertyCount = seqValues.getLength();
199 for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
201 // Safe impossible cases.
202 // Check any for valid value.
203 DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtWorkingSetOptions_Impl::SvtWorkingSetOptions_Impl()\nInvalid property value detected!\n" );
204 switch( nProperty )
206 case PROPERTYHANDLE_WINDOWLIST : {
207 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_SEQUENCE), "SvtWorkingSetOptions_Impl::SvtWorkingSetOptions_Impl()\nWho has changed the value type of \"Office.Common\\WorkingSet\\WindowList\"?" );
208 seqValues[nProperty] >>= m_seqWindowList;
210 break;
214 // Enable notification mechanism of ouer baseclass.
215 // We need it to get information about changes outside these class on ouer used configuration keys!
216 EnableNotification( seqNames );
219 //*****************************************************************************************************************
220 // destructor
221 //*****************************************************************************************************************
222 SvtWorkingSetOptions_Impl::~SvtWorkingSetOptions_Impl()
224 // We must save our current values .. if user forget it!
225 if( IsModified() == sal_True )
227 Commit();
231 //*****************************************************************************************************************
232 // public method
233 //*****************************************************************************************************************
234 void SvtWorkingSetOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
236 // Use given list of updated properties to get his values from configuration directly!
237 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
238 // Safe impossible cases.
239 // We need values from ALL notified configuration keys.
240 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtWorkingSetOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
241 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
242 sal_Int32 nCount = seqPropertyNames.getLength();
243 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
245 if( seqPropertyNames[nProperty] == PROPERTYNAME_WINDOWLIST )
247 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_SEQUENCE), "SvtWorkingSetOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\WorkingSet\\WindowList\"?" );
248 seqValues[nProperty] >>= m_seqWindowList;
250 #if OSL_DEBUG_LEVEL > 1
251 else DBG_ASSERT( sal_False, "SvtWorkingSetOptions_Impl::Notify()\nUnkown property detected ... I can't handle these!\n" );
252 #endif
256 //*****************************************************************************************************************
257 // public method
258 //*****************************************************************************************************************
259 void SvtWorkingSetOptions_Impl::Commit()
261 // Get names of supported properties, create a list for values and copy current values to it.
262 Sequence< OUString > seqNames = GetPropertyNames ();
263 sal_Int32 nCount = seqNames.getLength();
264 Sequence< Any > seqValues ( nCount );
265 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
267 switch( nProperty )
269 case PROPERTYHANDLE_WINDOWLIST : {
270 seqValues[nProperty] <<= m_seqWindowList;
272 break;
275 // Set properties in configuration.
276 PutProperties( seqNames, seqValues );
279 //*****************************************************************************************************************
280 // public method
281 //*****************************************************************************************************************
282 Sequence< OUString > SvtWorkingSetOptions_Impl::GetWindowList() const
284 return m_seqWindowList;
287 //*****************************************************************************************************************
288 // public method
289 //*****************************************************************************************************************
290 void SvtWorkingSetOptions_Impl::SetWindowList( const Sequence< OUString >& seqWindowList )
292 m_seqWindowList = seqWindowList;
293 SetModified();
296 //*****************************************************************************************************************
297 // private method
298 //*****************************************************************************************************************
299 Sequence< OUString > SvtWorkingSetOptions_Impl::GetPropertyNames()
301 // Build static list of configuration key names.
302 static const OUString pProperties[] =
304 PROPERTYNAME_WINDOWLIST ,
306 // Initialize return sequence with these list ...
307 static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
308 // ... and return it.
309 return seqPropertyNames;
312 //*****************************************************************************************************************
313 // initialize static member
314 // DON'T DO IT IN YOUR HEADER!
315 // see definition for further informations
316 //*****************************************************************************************************************
317 SvtWorkingSetOptions_Impl* SvtWorkingSetOptions::m_pDataContainer = NULL ;
318 sal_Int32 SvtWorkingSetOptions::m_nRefCount = 0 ;
320 //*****************************************************************************************************************
321 // constructor
322 //*****************************************************************************************************************
323 SvtWorkingSetOptions::SvtWorkingSetOptions()
325 // Global access, must be guarded (multithreading!).
326 MutexGuard aGuard( GetOwnStaticMutex() );
327 // Increase ouer refcount ...
328 ++m_nRefCount;
329 // ... and initialize ouer data container only if it not already exist!
330 if( m_pDataContainer == NULL )
332 m_pDataContainer = new SvtWorkingSetOptions_Impl;
333 ItemHolder1::holdConfigItem(E_WORKINGSETOPTIONS);
337 //*****************************************************************************************************************
338 // destructor
339 //*****************************************************************************************************************
340 SvtWorkingSetOptions::~SvtWorkingSetOptions()
342 // Global access, must be guarded (multithreading!)
343 MutexGuard aGuard( GetOwnStaticMutex() );
344 // Decrease ouer refcount.
345 --m_nRefCount;
346 // If last instance was deleted ...
347 // we must destroy ouer static data container!
348 if( m_nRefCount <= 0 )
350 delete m_pDataContainer;
351 m_pDataContainer = NULL;
355 //*****************************************************************************************************************
356 // public method
357 //*****************************************************************************************************************
358 Sequence< OUString > SvtWorkingSetOptions::GetWindowList() const
360 MutexGuard aGuard( GetOwnStaticMutex() );
361 return m_pDataContainer->GetWindowList();
364 //*****************************************************************************************************************
365 // public method
366 //*****************************************************************************************************************
367 void SvtWorkingSetOptions::SetWindowList( const Sequence< OUString >& seqWindowList )
369 MutexGuard aGuard( GetOwnStaticMutex() );
370 m_pDataContainer->SetWindowList( seqWindowList );
373 //*****************************************************************************************************************
374 // private method
375 //*****************************************************************************************************************
376 Mutex& SvtWorkingSetOptions::GetOwnStaticMutex()
378 // Initialize static mutex only for one time!
379 static Mutex* pMutex = NULL;
380 // If these method first called (Mutex not already exist!) ...
381 if( pMutex == NULL )
383 // ... we must create a new one. Protect follow code with the global mutex -
384 // It must be - we create a static variable!
385 MutexGuard aGuard( Mutex::getGlobalMutex() );
386 // We must check our pointer again - because it can be that another instance of ouer class will be fastr then these!
387 if( pMutex == NULL )
389 // Create the new mutex and set it for return on static variable.
390 static Mutex aMutex;
391 pMutex = &aMutex;
394 // Return new created or already existing mutex object.
395 return *pMutex;