Bump version to 4.3-4
[LibreOffice.git] / unotools / source / config / cmdoptions.cxx
blob06cdff9d691a9deb713a81214fc3eb4d770bbfc0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 #include "itemholder1.hxx"
32 #include <algorithm>
33 #include <boost/unordered_map.hpp>
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;
42 #define ROOTNODE_CMDOPTIONS OUString("Office.Commands/Execute")
43 #define PATHDELIMITER OUString("/")
45 #define SETNODE_DISABLED OUString("Disabled")
47 #define PROPERTYNAME_CMD OUString("Command")
49 /*-****************************************************************************************************************
50 @descr support simple command option structures and operations on it
51 ****************************************************************************************************************-*/
52 class SvtCmdOptions
54 public:
56 // the only way to free memory!
57 void Clear()
59 m_aCommandHashMap.clear();
62 bool HasEntries() const
64 return ( m_aCommandHashMap.size() > 0 );
67 bool Lookup( const OUString& aCmd ) const
69 CommandHashMap::const_iterator pEntry = m_aCommandHashMap.find( aCmd );
70 return ( pEntry != m_aCommandHashMap.end() );
73 void AddCommand( const OUString& aCmd )
75 m_aCommandHashMap.insert( CommandHashMap::value_type( aCmd, 0 ) );
78 private:
79 typedef boost::unordered_map<OUString, sal_Int32, OUStringHash>
80 CommandHashMap;
82 CommandHashMap m_aCommandHashMap;
85 typedef ::std::vector< ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > > SvtFrameVector;
87 class SvtCommandOptions_Impl : public ConfigItem
89 public:
91 SvtCommandOptions_Impl();
92 virtual ~SvtCommandOptions_Impl();
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 "lPropertyNames" is the list of properties which should be updated.
103 *//*-*****************************************************************************************************/
105 virtual void Notify( const Sequence< OUString >& lPropertyNames ) SAL_OVERRIDE;
107 /*-****************************************************************************************************
108 @short write changes to configuration
109 @descr These method writes the changed values into the sub tree
110 and should always called in our destructor to guarantee consistency of config data.
112 @seealso baseclass ConfigItem
113 *//*-*****************************************************************************************************/
115 virtual void Commit() SAL_OVERRIDE;
117 /*-****************************************************************************************************
118 @short base implementation of public interface for "SvtDynamicMenuOptions"!
119 @descr These class is used as static member of "SvtDynamicMenuOptions" ...
120 => The code exist only for one time and isn't duplicated for every instance!
121 *//*-*****************************************************************************************************/
123 bool HasEntries ( SvtCommandOptions::CmdOption eOption ) const;
124 bool Lookup ( SvtCommandOptions::CmdOption eCmdOption, const OUString& ) const;
125 void EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame);
127 private:
129 /*-****************************************************************************************************
130 @short return list of key names of our configuration management which represent oue module tree
131 @descr These methods return the current list of key names! We need it to get needed values from our
132 configuration management and support dynamical menu item lists!
133 @param "nDisabledCount" , returns count of menu entries for "new"
134 @return A list of configuration key names is returned.
135 *//*-*****************************************************************************************************/
137 Sequence< OUString > impl_GetPropertyNames();
139 private:
140 SvtCmdOptions m_aDisabledCommands;
141 SvtFrameVector m_lFrames;
144 // constructor
146 SvtCommandOptions_Impl::SvtCommandOptions_Impl()
147 // Init baseclasses first
148 : ConfigItem( ROOTNODE_CMDOPTIONS )
149 // Init member then...
151 // Get names and values of all accessible menu entries and fill internal structures.
152 // See impl_GetPropertyNames() for further information.
153 Sequence< OUString > lNames = impl_GetPropertyNames ();
154 Sequence< Any > lValues = GetProperties ( lNames );
156 // Safe impossible cases.
157 // We need values from ALL configuration keys.
158 // Follow assignment use order of values in relation to our list of key names!
159 DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::SvtCommandOptions_Impl()\nI miss some values of configuration keys!\n" );
161 // Copy values from list in right order to our internal member.
162 // Attention: List for names and values have an internal construction pattern!
163 sal_Int32 nItem = 0;
164 OUString sCmd;
166 // Get names/values for disabled commands.
167 for( nItem=0; nItem < lNames.getLength(); ++nItem )
169 // Currently only one value
170 lValues[nItem] >>= sCmd;
171 m_aDisabledCommands.AddCommand( sCmd );
174 /*TODO: Not used in the moment! see Notify() ...
175 // Enable notification mechanism of our baseclass.
176 // We need it to get information about changes outside these class on our used configuration keys! */
177 Sequence< OUString > aNotifySeq( 1 );
178 aNotifySeq[0] = "Disabled";
179 EnableNotification( aNotifySeq, true );
182 // destructor
184 SvtCommandOptions_Impl::~SvtCommandOptions_Impl()
186 // We must save our current values .. if user forget it!
187 if( IsModified() )
189 Commit();
193 // public method
195 void SvtCommandOptions_Impl::Notify( const Sequence< OUString >& )
197 MutexGuard aGuard( SvtCommandOptions::GetOwnStaticMutex() );
199 Sequence< OUString > lNames = impl_GetPropertyNames ();
200 Sequence< Any > lValues = GetProperties ( lNames );
202 // Safe impossible cases.
203 // We need values from ALL configuration keys.
204 // Follow assignment use order of values in relation to our list of key names!
205 DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
207 // Copy values from list in right order to our internal member.
208 // Attention: List for names and values have an internal construction pattern!
209 sal_Int32 nItem = 0;
210 OUString sCmd;
212 m_aDisabledCommands.Clear();
214 // Get names/values for disabled commands.
215 for( nItem=0; nItem < lNames.getLength(); ++nItem )
217 // Currently only one value
218 lValues[nItem] >>= sCmd;
219 m_aDisabledCommands.AddCommand( sCmd );
222 // dont forget to update all existing frames and her might cached dispatch objects!
223 // But look for already killed frames. We hold weak references instead of hard ones ...
224 for (SvtFrameVector::const_iterator pIt = m_lFrames.begin();
225 pIt != m_lFrames.end();
226 ++pIt )
228 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
229 if (xFrame.is())
230 xFrame->contextChanged();
234 // public method
236 void SvtCommandOptions_Impl::Commit()
238 OSL_FAIL( "SvtCommandOptions_Impl::Commit()\nNot implemented yet!\n" );
241 // public method
243 bool SvtCommandOptions_Impl::HasEntries( SvtCommandOptions::CmdOption eOption ) const
245 if ( eOption == SvtCommandOptions::CMDOPTION_DISABLED )
246 return m_aDisabledCommands.HasEntries();
247 else
248 return false;
251 // public method
253 bool SvtCommandOptions_Impl::Lookup( SvtCommandOptions::CmdOption eCmdOption, const OUString& aCommand ) const
255 switch( eCmdOption )
257 case SvtCommandOptions::CMDOPTION_DISABLED:
259 return m_aDisabledCommands.Lookup( aCommand );
261 default:
262 DBG_ASSERT( false, "SvtCommandOptions_Impl::Lookup()\nUnknown option type given!\n" );
265 return false;
268 // public method
270 void SvtCommandOptions_Impl::EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
272 // check if frame already exists inside list
273 // ignore double registrations
274 // every frame must be notified one times only!
275 ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > xWeak(xFrame);
276 SvtFrameVector::const_iterator pIt = ::std::find(m_lFrames.begin(), m_lFrames.end(), xWeak);
277 if (pIt == m_lFrames.end())
278 m_lFrames.push_back(xWeak);
281 // private method
283 Sequence< OUString > SvtCommandOptions_Impl::impl_GetPropertyNames()
285 // First get ALL names of current existing list items in configuration!
286 Sequence< OUString > lDisabledItems = GetNodeNames( SETNODE_DISABLED, utl::CONFIG_NAME_LOCAL_PATH );
288 OUString aSetNode( SETNODE_DISABLED );
289 aSetNode += PATHDELIMITER;
291 OUString aCommandKey( PATHDELIMITER );
292 aCommandKey += PROPERTYNAME_CMD;
294 // Expand all keys
295 for (sal_Int32 i=0; i<lDisabledItems.getLength(); ++i )
297 OUStringBuffer aBuffer( 32 );
298 aBuffer.append( aSetNode );
299 aBuffer.append( lDisabledItems[i] );
300 aBuffer.append( aCommandKey );
301 lDisabledItems[i] = aBuffer.makeStringAndClear();
304 // Return result.
305 return lDisabledItems;
308 // initialize static member
309 // DON'T DO IT IN YOUR HEADER!
310 // see definition for further information
312 SvtCommandOptions_Impl* SvtCommandOptions::m_pDataContainer = NULL;
313 sal_Int32 SvtCommandOptions::m_nRefCount = 0;
315 // constructor
317 SvtCommandOptions::SvtCommandOptions()
319 // Global access, must be guarded (multithreading!).
320 MutexGuard aGuard( GetOwnStaticMutex() );
321 // Increase our refcount ...
322 ++m_nRefCount;
323 // ... and initialize our data container only if it not already exist!
324 if( m_pDataContainer == NULL )
326 m_pDataContainer = new SvtCommandOptions_Impl;
327 ItemHolder1::holdConfigItem(E_CMDOPTIONS);
331 // destructor
333 SvtCommandOptions::~SvtCommandOptions()
335 // Global access, must be guarded (multithreading!)
336 MutexGuard aGuard( GetOwnStaticMutex() );
337 // Decrease our refcount.
338 --m_nRefCount;
339 // If last instance was deleted ...
340 // we must destroy our static data container!
341 if( m_nRefCount <= 0 )
343 delete m_pDataContainer;
344 m_pDataContainer = NULL;
348 // public method
350 bool SvtCommandOptions::HasEntries( CmdOption eOption ) const
352 MutexGuard aGuard( GetOwnStaticMutex() );
353 return m_pDataContainer->HasEntries( eOption );
356 // public method
358 bool SvtCommandOptions::Lookup( CmdOption eCmdOption, const OUString& aCommandURL ) const
360 MutexGuard aGuard( GetOwnStaticMutex() );
361 return m_pDataContainer->Lookup( eCmdOption, aCommandURL );
364 // public method
366 void SvtCommandOptions::EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
368 MutexGuard aGuard( GetOwnStaticMutex() );
369 m_pDataContainer->EstablisFrameCallback(xFrame);
372 namespace
374 class theCommandOptionsMutex : public rtl::Static<osl::Mutex, theCommandOptionsMutex>{};
377 // private method
379 Mutex& SvtCommandOptions::GetOwnStaticMutex()
381 return theCommandOptionsMutex::get();
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */