Branch data 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/cmdoptions.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 : : #include <cppuhelper/weakref.hxx>
27 : : #include <rtl/ustrbuf.hxx>
28 : : #include <rtl/instance.hxx>
29 : :
30 : : #include <itemholder1.hxx>
31 : :
32 : : #include <algorithm>
33 : : #include <boost/unordered_map.hpp>
34 : :
35 : : using namespace ::std ;
36 : : using namespace ::utl ;
37 : : using namespace ::rtl ;
38 : : using namespace ::osl ;
39 : : using namespace ::com::sun::star::uno ;
40 : : using namespace ::com::sun::star::beans ;
41 : :
42 : : #define ROOTNODE_CMDOPTIONS OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Commands/Execute" ))
43 : : #define PATHDELIMITER OUString(RTL_CONSTASCII_USTRINGPARAM("/" ))
44 : :
45 : : #define SETNODE_DISABLED OUString(RTL_CONSTASCII_USTRINGPARAM("Disabled" ))
46 : :
47 : : #define PROPERTYNAME_CMD OUString(RTL_CONSTASCII_USTRINGPARAM("Command" ))
48 : :
49 : : // Method to retrieve a hash code from a string. May be we have to change it to decrease collisions in the hash map
50 : : struct OUStringHashCode
51 : : {
52 : 29106 : size_t operator()( const ::rtl::OUString& sString ) const
53 : : {
54 : 29106 : return sString.hashCode();
55 : : }
56 : : };
57 : :
58 : : /*-****************************************************************************************************************
59 : : @descr support simple command option structures and operations on it
60 : : ****************************************************************************************************************-*/
61 : 406 : class SvtCmdOptions
62 : : {
63 : : public:
64 : : //---------------------------------------------------------------------------------------------------------
65 : : // the only way to free memory!
66 : 0 : void Clear()
67 : : {
68 : 0 : m_aCommandHashMap.clear();
69 : 0 : }
70 : :
71 : 3745 : sal_Bool HasEntries() const
72 : : {
73 : 3745 : return ( m_aCommandHashMap.size() > 0 );
74 : : }
75 : :
76 : 213079 : sal_Bool Lookup( const OUString& aCmd ) const
77 : : {
78 [ + - ]: 213079 : CommandHashMap::const_iterator pEntry = m_aCommandHashMap.find( aCmd );
79 [ + - ]: 213079 : return ( pEntry != m_aCommandHashMap.end() );
80 : : }
81 : :
82 : 0 : void AddCommand( const OUString& aCmd )
83 : : {
84 [ # # ][ # # ]: 0 : m_aCommandHashMap.insert( CommandHashMap::value_type( aCmd, 0 ) );
85 : 0 : }
86 : :
87 : : //---------------------------------------------------------------------------------------------------------
88 : : // convert internal list to external format
89 : : // for using it on right menus realy
90 : : // Notice: We build a property list with 4 entries and set it on result list then.
91 : : // The while-loop starts with pointer on internal member list lSetupEntries, change to
92 : : // lUserEntries then and stop after that with NULL!
93 : : // Separator entries will be packed in another way then normal entries! We define
94 : : // special strings "sEmpty" and "sSeperator" to perform too ...
95 : : Sequence< OUString > GetList() const
96 : : {
97 : : sal_Int32 nCount = (sal_Int32)m_aCommandHashMap.size();
98 : : sal_Int32 nIndex = 0;
99 : : Sequence< OUString > aList( nCount );
100 : :
101 : : CommandHashMap::const_iterator pEntry = m_aCommandHashMap.begin();
102 : : while ( pEntry != m_aCommandHashMap.end() )
103 : : aList[nIndex++] = pEntry->first;
104 : :
105 : : return aList;
106 : : }
107 : :
108 : : private:
109 [ + - ]: 406 : class CommandHashMap : public ::boost::unordered_map< ::rtl::OUString ,
110 : : sal_Int32 ,
111 : : OUStringHashCode ,
112 : : ::std::equal_to< ::rtl::OUString > >
113 : : {
114 : : public:
115 : : inline void free()
116 : : {
117 : : CommandHashMap().swap( *this );
118 : : }
119 : : };
120 : :
121 : : CommandHashMap m_aCommandHashMap;
122 : : };
123 : :
124 : : typedef ::std::vector< ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > > SvtFrameVector;
125 : :
126 : : class SvtCommandOptions_Impl : public ConfigItem
127 : : {
128 : : public:
129 : :
130 : : SvtCommandOptions_Impl();
131 : : ~SvtCommandOptions_Impl();
132 : :
133 : : /*-****************************************************************************************************//**
134 : : @short called for notify of configmanager
135 : : @descr These method is called from the ConfigManager before application ends or from the
136 : : PropertyChangeListener if the sub tree broadcasts changes. You must update your
137 : : internal values.
138 : :
139 : : @seealso baseclass ConfigItem
140 : :
141 : : @param "lPropertyNames" is the list of properties which should be updated.
142 : : @return -
143 : :
144 : : @onerror -
145 : : *//*-*****************************************************************************************************/
146 : :
147 : : virtual void Notify( const Sequence< OUString >& lPropertyNames );
148 : :
149 : : /*-****************************************************************************************************//**
150 : : @short write changes to configuration
151 : : @descr These method writes the changed values into the sub tree
152 : : and should always called in our destructor to guarantee consistency of config data.
153 : :
154 : : @seealso baseclass ConfigItem
155 : :
156 : : @param -
157 : : @return -
158 : :
159 : : @onerror -
160 : : *//*-*****************************************************************************************************/
161 : :
162 : : virtual void Commit();
163 : :
164 : : /*-****************************************************************************************************//**
165 : : @short base implementation of public interface for "SvtDynamicMenuOptions"!
166 : : @descr These class is used as static member of "SvtDynamicMenuOptions" ...
167 : : => The code exist only for one time and isn't duplicated for every instance!
168 : :
169 : : @seealso -
170 : :
171 : : @param -
172 : : @return -
173 : :
174 : : @onerror -
175 : : *//*-*****************************************************************************************************/
176 : :
177 : : sal_Bool HasEntries ( SvtCommandOptions::CmdOption eOption ) const;
178 : : sal_Bool Lookup ( SvtCommandOptions::CmdOption eCmdOption, const OUString& ) const;
179 : : void EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame);
180 : :
181 : : private:
182 : :
183 : : /*-****************************************************************************************************//**
184 : : @short return list of key names of our configuration management which represent oue module tree
185 : : @descr These methods return the current list of key names! We need it to get needed values from our
186 : : configuration management and support dynamical menu item lists!
187 : :
188 : : @seealso -
189 : :
190 : : @param "nDisabledCount" , returns count of menu entries for "new"
191 : : @return A list of configuration key names is returned.
192 : :
193 : : @onerror -
194 : : *//*-*****************************************************************************************************/
195 : :
196 : : Sequence< OUString > impl_GetPropertyNames();
197 : :
198 : : private:
199 : : SvtCmdOptions m_aDisabledCommands;
200 : : SvtFrameVector m_lFrames;
201 : : };
202 : :
203 : : //*****************************************************************************************************************
204 : : // constructor
205 : : //*****************************************************************************************************************
206 : 233 : SvtCommandOptions_Impl::SvtCommandOptions_Impl()
207 : : // Init baseclasses first
208 [ + - ][ + - ]: 233 : : ConfigItem( ROOTNODE_CMDOPTIONS )
[ + - ]
209 : : // Init member then...
210 : : {
211 : : // Get names and values of all accessable menu entries and fill internal structures.
212 : : // See impl_GetPropertyNames() for further informations.
213 [ + - ]: 233 : Sequence< OUString > lNames = impl_GetPropertyNames ();
214 [ + - ]: 233 : Sequence< Any > lValues = GetProperties ( lNames );
215 : :
216 : : // Safe impossible cases.
217 : : // We need values from ALL configuration keys.
218 : : // Follow assignment use order of values in relation to our list of key names!
219 : : DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::SvtCommandOptions_Impl()\nI miss some values of configuration keys!\n" );
220 : :
221 : : // Copy values from list in right order to ouer internal member.
222 : : // Attention: List for names and values have an internal construction pattern!
223 : 233 : sal_Int32 nItem = 0 ;
224 : 233 : OUString sCmd ;
225 : :
226 : : // Get names/values for disabled commands.
227 [ - + ]: 233 : for( nItem=0; nItem < lNames.getLength(); ++nItem )
228 : : {
229 : : // Currently only one value
230 [ # # ]: 0 : lValues[nItem] >>= sCmd;
231 [ # # ]: 0 : m_aDisabledCommands.AddCommand( sCmd );
232 : : }
233 : :
234 : : /*TODO: Not used in the moment! see Notify() ...
235 : : // Enable notification mechanism of ouer baseclass.
236 : : // We need it to get information about changes outside these class on ouer used configuration keys! */
237 [ + - ]: 233 : Sequence< OUString > aNotifySeq( 1 );
238 [ + - ][ + - ]: 233 : aNotifySeq[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "Disabled" ));
239 [ + - ][ + - ]: 233 : EnableNotification( aNotifySeq, sal_True );
[ + - ][ + - ]
240 : 233 : }
241 : :
242 : : //*****************************************************************************************************************
243 : : // destructor
244 : : //*****************************************************************************************************************
245 [ + - ]: 173 : SvtCommandOptions_Impl::~SvtCommandOptions_Impl()
246 : : {
247 : : // We must save our current values .. if user forget it!
248 [ + - ][ - + ]: 173 : if( IsModified() == sal_True )
249 : : {
250 : 0 : Commit();
251 : : }
252 [ - + ]: 346 : }
253 : :
254 : : //*****************************************************************************************************************
255 : : // public method
256 : : //*****************************************************************************************************************
257 : 0 : void SvtCommandOptions_Impl::Notify( const Sequence< OUString >& )
258 : : {
259 [ # # ][ # # ]: 0 : MutexGuard aGuard( SvtCommandOptions::GetOwnStaticMutex() );
260 : :
261 [ # # ]: 0 : Sequence< OUString > lNames = impl_GetPropertyNames ();
262 [ # # ]: 0 : Sequence< Any > lValues = GetProperties ( lNames );
263 : :
264 : : // Safe impossible cases.
265 : : // We need values from ALL configuration keys.
266 : : // Follow assignment use order of values in relation to our list of key names!
267 : : DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::SvtCommandOptions_Impl()\nI miss some values of configuration keys!\n" );
268 : :
269 : : // Copy values from list in right order to ouer internal member.
270 : : // Attention: List for names and values have an internal construction pattern!
271 : 0 : sal_Int32 nItem = 0 ;
272 : 0 : OUString sCmd ;
273 : :
274 [ # # ]: 0 : m_aDisabledCommands.Clear();
275 : :
276 : : // Get names/values for disabled commands.
277 [ # # ]: 0 : for( nItem=0; nItem < lNames.getLength(); ++nItem )
278 : : {
279 : : // Currently only one value
280 [ # # ]: 0 : lValues[nItem] >>= sCmd;
281 [ # # ]: 0 : m_aDisabledCommands.AddCommand( sCmd );
282 : : }
283 : :
284 : : // dont forget to update all existing frames and her might cached dispatch objects!
285 : : // But look for already killed frames. We hold weak references instead of hard ones ...
286 [ # # ][ # # ]: 0 : for (SvtFrameVector::const_iterator pIt = m_lFrames.begin();
[ # # ]
287 : 0 : pIt != m_lFrames.end() ;
288 : : ++pIt )
289 : : {
290 [ # # ][ # # ]: 0 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
291 [ # # ]: 0 : if (xFrame.is())
292 [ # # ][ # # ]: 0 : xFrame->contextChanged();
293 [ # # ][ # # ]: 0 : }
[ # # ]
294 : 0 : }
295 : :
296 : : //*****************************************************************************************************************
297 : : // public method
298 : : //*****************************************************************************************************************
299 : 0 : void SvtCommandOptions_Impl::Commit()
300 : : {
301 : : OSL_FAIL( "SvtCommandOptions_Impl::Commit()\nNot implemented yet!\n" );
302 : 0 : }
303 : :
304 : : //*****************************************************************************************************************
305 : : // public method
306 : : //*****************************************************************************************************************
307 : 3745 : sal_Bool SvtCommandOptions_Impl::HasEntries( SvtCommandOptions::CmdOption eOption ) const
308 : : {
309 [ + - ]: 3745 : if ( eOption == SvtCommandOptions::CMDOPTION_DISABLED )
310 : 3745 : return ( m_aDisabledCommands.HasEntries() > 0 );
311 : : else
312 : 3745 : return sal_False;
313 : : }
314 : :
315 : : //*****************************************************************************************************************
316 : : // public method
317 : : //*****************************************************************************************************************
318 : 213079 : sal_Bool SvtCommandOptions_Impl::Lookup( SvtCommandOptions::CmdOption eCmdOption, const OUString& aCommand ) const
319 : : {
320 [ + - ]: 213079 : switch( eCmdOption )
321 : : {
322 : : case SvtCommandOptions::CMDOPTION_DISABLED:
323 : : {
324 : 213079 : return m_aDisabledCommands.Lookup( aCommand );
325 : : }
326 : : default:
327 : : DBG_ASSERT( sal_False, "SvtCommandOptions_Impl::GetList()\nUnknown option type given!\n" );
328 : : }
329 : :
330 : 213079 : return sal_False;
331 : : }
332 : :
333 : : //*****************************************************************************************************************
334 : : // public method
335 : : //*****************************************************************************************************************
336 : 1747 : void SvtCommandOptions_Impl::EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
337 : : {
338 : : // check if frame already exists inside list
339 : : // ignore double registrations
340 : : // every frame must be notified one times only!
341 [ + - ]: 1747 : ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > xWeak(xFrame);
342 [ + - ][ + - ]: 1747 : SvtFrameVector::const_iterator pIt = ::std::find(m_lFrames.begin(), m_lFrames.end(), xWeak);
343 [ + - ][ + - ]: 1747 : if (pIt == m_lFrames.end())
344 [ + - ][ + - ]: 1747 : m_lFrames.push_back(xWeak);
345 : 1747 : }
346 : :
347 : : //*****************************************************************************************************************
348 : : // private method
349 : : //*****************************************************************************************************************
350 : 233 : Sequence< OUString > SvtCommandOptions_Impl::impl_GetPropertyNames()
351 : : {
352 : : // First get ALL names of current existing list items in configuration!
353 [ + - ][ + - ]: 233 : Sequence< OUString > lDisabledItems = GetNodeNames( SETNODE_DISABLED, utl::CONFIG_NAME_LOCAL_PATH );
354 : :
355 [ + - ]: 233 : OUString aSetNode( SETNODE_DISABLED );
356 [ + - ]: 233 : aSetNode += PATHDELIMITER;
357 : :
358 [ + - ]: 233 : OUString aCommandKey( PATHDELIMITER );
359 [ + - ]: 233 : aCommandKey += PROPERTYNAME_CMD;
360 : :
361 : : // Expand all keys
362 [ - + ]: 233 : for (sal_Int32 i=0; i<lDisabledItems.getLength(); ++i )
363 : : {
364 : 0 : OUStringBuffer aBuffer( 32 );
365 [ # # ]: 0 : aBuffer.append( aSetNode );
366 [ # # ][ # # ]: 0 : aBuffer.append( lDisabledItems[i] );
367 [ # # ]: 0 : aBuffer.append( aCommandKey );
368 [ # # ][ # # ]: 0 : lDisabledItems[i] = aBuffer.makeStringAndClear();
369 : 0 : }
370 : :
371 : : // Return result.
372 : 233 : return lDisabledItems;
373 : : }
374 : :
375 : : //*****************************************************************************************************************
376 : : // initialize static member
377 : : // DON'T DO IT IN YOUR HEADER!
378 : : // see definition for further informations
379 : : //*****************************************************************************************************************
380 : : SvtCommandOptions_Impl* SvtCommandOptions::m_pDataContainer = NULL ;
381 : : sal_Int32 SvtCommandOptions::m_nRefCount = 0 ;
382 : :
383 : : //*****************************************************************************************************************
384 : : // constructor
385 : : //*****************************************************************************************************************
386 : 15963 : SvtCommandOptions::SvtCommandOptions()
387 : : {
388 : : // Global access, must be guarded (multithreading!).
389 [ + - ][ + - ]: 15963 : MutexGuard aGuard( GetOwnStaticMutex() );
390 : : // Increase ouer refcount ...
391 : 15963 : ++m_nRefCount;
392 : : // ... and initialize ouer data container only if it not already exist!
393 [ + + ]: 15963 : if( m_pDataContainer == NULL )
394 : : {
395 [ + - ][ + - ]: 233 : m_pDataContainer = new SvtCommandOptions_Impl;
396 [ + - ]: 233 : ItemHolder1::holdConfigItem(E_CMDOPTIONS);
397 [ + - ]: 15963 : }
398 : 15963 : }
399 : :
400 : : //*****************************************************************************************************************
401 : : // destructor
402 : : //*****************************************************************************************************************
403 : 15812 : SvtCommandOptions::~SvtCommandOptions()
404 : : {
405 : : // Global access, must be guarded (multithreading!)
406 [ + - ][ + - ]: 15812 : MutexGuard aGuard( GetOwnStaticMutex() );
407 : : // Decrease ouer refcount.
408 : 15812 : --m_nRefCount;
409 : : // If last instance was deleted ...
410 : : // we must destroy ouer static data container!
411 [ + + ]: 15812 : if( m_nRefCount <= 0 )
412 : : {
413 [ + - ][ + - ]: 173 : delete m_pDataContainer;
414 : 173 : m_pDataContainer = NULL;
415 [ + - ]: 15812 : }
416 [ - + ]: 16045 : }
417 : :
418 : : //*****************************************************************************************************************
419 : : // public method
420 : : //*****************************************************************************************************************
421 : 3745 : sal_Bool SvtCommandOptions::HasEntries( CmdOption eOption ) const
422 : : {
423 [ + - ][ + - ]: 3745 : MutexGuard aGuard( GetOwnStaticMutex() );
424 [ + - ][ + - ]: 3745 : return m_pDataContainer->HasEntries( eOption );
425 : : }
426 : :
427 : : //*****************************************************************************************************************
428 : : // public method
429 : : //*****************************************************************************************************************
430 : 213079 : sal_Bool SvtCommandOptions::Lookup( CmdOption eCmdOption, const OUString& aCommandURL ) const
431 : : {
432 [ + - ][ + - ]: 213079 : MutexGuard aGuard( GetOwnStaticMutex() );
433 [ + - ][ + - ]: 213079 : return m_pDataContainer->Lookup( eCmdOption, aCommandURL );
434 : : }
435 : :
436 : : //*****************************************************************************************************************
437 : : // public method
438 : : //*****************************************************************************************************************
439 : 1747 : void SvtCommandOptions::EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
440 : : {
441 [ + - ][ + - ]: 1747 : MutexGuard aGuard( GetOwnStaticMutex() );
442 [ + - ][ + - ]: 1747 : m_pDataContainer->EstablisFrameCallback(xFrame);
443 : 1747 : }
444 : :
445 : : namespace
446 : : {
447 : : class theCommandOptionsMutex : public rtl::Static<osl::Mutex, theCommandOptionsMutex>{};
448 : : }
449 : :
450 : : //*****************************************************************************************************************
451 : : // private method
452 : : //*****************************************************************************************************************
453 : 250346 : Mutex& SvtCommandOptions::GetOwnStaticMutex()
454 : : {
455 : 250346 : return theCommandOptionsMutex::get();
456 : : }
457 : :
458 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|