Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <unotools/localisationoptions.hxx>
21 : #include <unotools/configmgr.hxx>
22 : #include <unotools/configitem.hxx>
23 : #include <tools/debug.hxx>
24 : #include <com/sun/star/uno/Any.hxx>
25 : #include <com/sun/star/uno/Sequence.hxx>
26 :
27 : #include "itemholder1.hxx"
28 :
29 : using namespace ::utl;
30 : using namespace ::osl;
31 : using namespace ::com::sun::star::uno;
32 :
33 : #define ROOTNODE_LOCALISATION OUString("Office.Common/View/Localisation")
34 : #define DEFAULT_AUTOMNEMONIC false
35 : #define DEFAULT_DIALOGSCALE 0
36 :
37 : #define PROPERTYNAME_AUTOMNEMONIC "AutoMnemonic"
38 : #define PROPERTYNAME_DIALOGSCALE "DialogScale"
39 :
40 : #define PROPERTYHANDLE_AUTOMNEMONIC 0
41 : #define PROPERTYHANDLE_DIALOGSCALE 1
42 :
43 : #define PROPERTYCOUNT 2
44 :
45 : class SvtLocalisationOptions_Impl : public ConfigItem
46 : {
47 : public:
48 :
49 : SvtLocalisationOptions_Impl();
50 : virtual ~SvtLocalisationOptions_Impl();
51 :
52 : /*-****************************************************************************************************
53 : @short called for notify of configmanager
54 : @descr These method is called from the ConfigManager before application ends or from the
55 : PropertyChangeListener if the sub tree broadcasts changes. You must update your
56 : internal values.
57 :
58 : @seealso baseclass ConfigItem
59 :
60 : @param "seqPropertyNames" is the list of properties which should be updated.
61 : *//*-*****************************************************************************************************/
62 :
63 : virtual void Notify( const Sequence< OUString >& seqPropertyNames ) SAL_OVERRIDE;
64 :
65 : /*-****************************************************************************************************
66 : @short access method to get internal values
67 : @descr These method give us a chance to regulate access to our internal values.
68 : It's not used in the moment - but it's possible for the feature!
69 : *//*-*****************************************************************************************************/
70 :
71 208 : bool IsAutoMnemonic ( ) const { return m_bAutoMnemonic;}
72 208 : sal_Int32 GetDialogScale ( ) const { return m_nDialogScale;}
73 :
74 : private:
75 :
76 : virtual void ImplCommit() SAL_OVERRIDE;
77 :
78 : /*-****************************************************************************************************
79 : @short return list of key names of our configuration management which represent oue module tree
80 : @descr These methods return a static const list of key names. We need it to get needed values from our
81 : configuration management.
82 : @return A list of needed configuration keys is returned.
83 : *//*-*****************************************************************************************************/
84 :
85 : static Sequence< OUString > GetPropertyNames();
86 :
87 : private:
88 :
89 : bool m_bAutoMnemonic;
90 : sal_Int32 m_nDialogScale;
91 : };
92 :
93 : // constructor
94 :
95 208 : SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()
96 : // Init baseclasses first
97 : : ConfigItem ( ROOTNODE_LOCALISATION )
98 : // Init member then.
99 : , m_bAutoMnemonic ( DEFAULT_AUTOMNEMONIC )
100 208 : , m_nDialogScale ( DEFAULT_DIALOGSCALE )
101 : {
102 : // Use our static list of configuration keys to get his values.
103 208 : Sequence< OUString > seqNames = GetPropertyNames ( );
104 416 : Sequence< Any > seqValues = GetProperties ( seqNames );
105 :
106 : // Safe impossible cases.
107 : // We need values from ALL configuration keys.
108 : // Follow assignment use order of values in relation to our list of key names!
109 : DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nI miss some values of configuration keys!\n" );
110 :
111 : // Copy values from list in right order to our internal member.
112 208 : sal_Int32 nPropertyCount = seqValues.getLength();
113 624 : for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
114 : {
115 416 : if (!seqValues[nProperty].hasValue())
116 0 : continue;
117 416 : switch( nProperty )
118 : {
119 : case PROPERTYHANDLE_AUTOMNEMONIC : {
120 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\AutoMnemonic\"?" );
121 208 : seqValues[nProperty] >>= m_bAutoMnemonic;
122 : }
123 208 : break;
124 :
125 : case PROPERTYHANDLE_DIALOGSCALE : {
126 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_LONG), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\DialogScale\"?" );
127 208 : seqValues[nProperty] >>= m_nDialogScale;
128 : }
129 208 : break;
130 : }
131 : }
132 :
133 : // Enable notification mechanism of our baseclass.
134 : // We need it to get information about changes outside these class on our used configuration keys!
135 416 : EnableNotification( seqNames );
136 208 : }
137 :
138 : // destructor
139 :
140 412 : SvtLocalisationOptions_Impl::~SvtLocalisationOptions_Impl()
141 : {
142 : assert(!IsModified()); // should have been committed
143 412 : }
144 :
145 : // public method
146 :
147 0 : void SvtLocalisationOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
148 : {
149 : // Use given list of updated properties to get his values from configuration directly!
150 0 : Sequence< Any > seqValues = GetProperties( seqPropertyNames );
151 : // Safe impossible cases.
152 : // We need values from ALL notified configuration keys.
153 : DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtLocalisationOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
154 : // Step over list of property names and get right value from coreesponding value list to set it on internal members!
155 0 : sal_Int32 nCount = seqPropertyNames.getLength();
156 0 : for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
157 : {
158 0 : if( seqPropertyNames[nProperty] == PROPERTYNAME_AUTOMNEMONIC )
159 : {
160 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\AutoMnemonic\"?" );
161 0 : seqValues[nProperty] >>= m_bAutoMnemonic;
162 : }
163 : else
164 0 : if( seqPropertyNames[nProperty] == PROPERTYNAME_DIALOGSCALE )
165 : {
166 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_LONG), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\DialogScale\"?" );
167 0 : seqValues[nProperty] >>= m_nDialogScale;
168 : }
169 : #if OSL_DEBUG_LEVEL > 1
170 : else DBG_ASSERT( sal_False, "SvtLocalisationOptions_Impl::Notify()\nUnknown property detected ... I can't handle these!\n" );
171 : #endif
172 : }
173 :
174 0 : NotifyListeners(0);
175 0 : }
176 :
177 : // public method
178 :
179 0 : void SvtLocalisationOptions_Impl::ImplCommit()
180 : {
181 : // Get names of supported properties, create a list for values and copy current values to it.
182 0 : Sequence< OUString > seqNames = GetPropertyNames ();
183 0 : sal_Int32 nCount = seqNames.getLength();
184 0 : Sequence< Any > seqValues ( nCount );
185 0 : for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
186 : {
187 0 : switch( nProperty )
188 : {
189 : case PROPERTYHANDLE_AUTOMNEMONIC : {
190 0 : seqValues[nProperty] <<= m_bAutoMnemonic;
191 : }
192 0 : break;
193 :
194 : case PROPERTYHANDLE_DIALOGSCALE : {
195 0 : seqValues[nProperty] <<= m_nDialogScale;
196 : }
197 0 : break;
198 : }
199 : }
200 : // Set properties in configuration.
201 0 : PutProperties( seqNames, seqValues );
202 0 : }
203 :
204 : // public method
205 :
206 :
207 208 : Sequence< OUString > SvtLocalisationOptions_Impl::GetPropertyNames()
208 : {
209 : // Build static list of configuration key names.
210 : const OUString aProperties[] =
211 : {
212 : OUString(PROPERTYNAME_AUTOMNEMONIC) ,
213 : OUString(PROPERTYNAME_DIALOGSCALE) ,
214 624 : };
215 : // Initialize return sequence with these list ...
216 208 : Sequence< OUString > seqPropertyNames(aProperties, PROPERTYCOUNT);
217 : // ... and return it.
218 624 : return seqPropertyNames;
219 : }
220 :
221 : // initialize static member
222 : // DON'T DO IT IN YOUR HEADER!
223 : // see definition for further information
224 :
225 : SvtLocalisationOptions_Impl* SvtLocalisationOptions::m_pDataContainer = NULL;
226 : sal_Int32 SvtLocalisationOptions::m_nRefCount = 0;
227 :
228 : // constructor
229 :
230 416 : SvtLocalisationOptions::SvtLocalisationOptions()
231 : {
232 : // Global access, must be guarded (multithreading!).
233 416 : MutexGuard aGuard( GetOwnStaticMutex() );
234 : // Increase our refcount ...
235 416 : ++m_nRefCount;
236 : // ... and initialize our data container only if it not already exist!
237 416 : if( m_pDataContainer == NULL )
238 : {
239 208 : m_pDataContainer = new SvtLocalisationOptions_Impl;
240 :
241 208 : ItemHolder1::holdConfigItem(E_LOCALISATIONOPTIONS);
242 416 : }
243 416 : }
244 :
245 : // destructor
246 :
247 1034 : SvtLocalisationOptions::~SvtLocalisationOptions()
248 : {
249 : // Global access, must be guarded (multithreading!)
250 414 : MutexGuard aGuard( GetOwnStaticMutex() );
251 : // Decrease our refcount.
252 414 : --m_nRefCount;
253 : // If last instance was deleted ...
254 : // we must destroy our static data container!
255 414 : if( m_nRefCount <= 0 )
256 : {
257 206 : delete m_pDataContainer;
258 206 : m_pDataContainer = NULL;
259 414 : }
260 620 : }
261 :
262 : // public method
263 :
264 208 : bool SvtLocalisationOptions::IsAutoMnemonic() const
265 : {
266 208 : MutexGuard aGuard( GetOwnStaticMutex() );
267 208 : return m_pDataContainer->IsAutoMnemonic();
268 : }
269 :
270 : // public method
271 :
272 208 : sal_Int32 SvtLocalisationOptions::GetDialogScale() const
273 : {
274 208 : MutexGuard aGuard( GetOwnStaticMutex() );
275 208 : return m_pDataContainer->GetDialogScale();
276 : }
277 :
278 : namespace
279 : {
280 : class theLocalisationOptionsMutex : public rtl::Static<osl::Mutex, theLocalisationOptionsMutex>{};
281 : }
282 :
283 : // private method
284 :
285 1246 : Mutex& SvtLocalisationOptions::GetOwnStaticMutex()
286 : {
287 1246 : return theLocalisationOptionsMutex::get();
288 : }
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|