LibreOffice Module unotools (master) 1
cmdoptions.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <sal/log.hxx>
25#include <tools/debug.hxx>
26#include <com/sun/star/uno/Any.hxx>
27#include <com/sun/star/uno/Sequence.hxx>
28#include <com/sun/star/frame/XFrame.hpp>
30
31#include "itemholder1.hxx"
32
33#include <algorithm>
34#include <unordered_map>
35
36using namespace ::utl;
37using namespace ::osl;
38using namespace ::com::sun::star::uno;
39using namespace ::com::sun::star::beans;
40
41constexpr OUStringLiteral ROOTNODE_CMDOPTIONS = u"Office.Commands/Execute";
42#define PATHDELIMITER "/"
43
44#define SETNODE_DISABLED "Disabled"
45
46#define PROPERTYNAME_CMD "Command"
47
48namespace {
49
50/*-****************************************************************************************************************
51 @descr support simple command option structures and operations on it
52****************************************************************************************************************-*/
53class SvtCmdOptions
54{
55 public:
56
57 // the only way to free memory!
58 void Clear()
59 {
60 m_aCommandHashMap.clear();
61 }
62
63 bool HasEntries() const
64 {
65 return ( !m_aCommandHashMap.empty() );
66 }
67
68 bool Lookup( const OUString& aCmd ) const
69 {
70 CommandHashMap::const_iterator pEntry = m_aCommandHashMap.find( aCmd );
71 return ( pEntry != m_aCommandHashMap.end() );
72 }
73
74 void AddCommand( const OUString& aCmd )
75 {
76 m_aCommandHashMap.emplace( aCmd, 0 );
77 }
78
79 private:
80 typedef std::unordered_map<OUString, sal_Int32>
82
83 CommandHashMap m_aCommandHashMap;
84};
85
87{
88 static std::mutex theCommandOptionsMutex;
89 return theCommandOptionsMutex;
90}
91
92}
93
94typedef ::std::vector< css::uno::WeakReference< css::frame::XFrame > > SvtFrameVector;
95
96class SvtCommandOptions_Impl : public ConfigItem
97{
98 public:
99
101 virtual ~SvtCommandOptions_Impl() override;
102
103 /*-****************************************************************************************************
104 @short called for notify of configmanager
105 @descr This method is called from the ConfigManager before the application ends or from the
106 PropertyChangeListener if the sub tree broadcasts changes. You must update your
107 internal values.
108
109 @seealso baseclass ConfigItem
110
111 @param "lPropertyNames" is the list of properties which should be updated.
112 *//*-*****************************************************************************************************/
113
114 virtual void Notify( const Sequence< OUString >& lPropertyNames ) override;
115
116 /*-****************************************************************************************************
117 @short base implementation of public interface for "SvtDynamicMenuOptions"!
118 @descr These class is used as static member of "SvtDynamicMenuOptions" ...
119 => The code exist only for one time and isn't duplicated for every instance!
120 *//*-*****************************************************************************************************/
121
122 bool HasEntriesDisabled() const;
123 bool LookupDisabled( const OUString& ) const;
124 void EstablishFrameCallback(const css::uno::Reference< css::frame::XFrame >& xFrame);
125
126 private:
127
128 virtual void ImplCommit() override;
129
130 /*-****************************************************************************************************
131 @short return list of key names of our configuration management which represent our module tree
132 @descr This method returns the current list of key names! We need it to get needed values from our
133 configuration management and support dynamical menu item lists!
134 @param "nDisabledCount", returns count of menu entries for "new"
135 @return A list of configuration key names is returned.
136 *//*-*****************************************************************************************************/
137
139
140 private:
141 SvtCmdOptions m_aDisabledCommands;
143};
144
145// constructor
146
148 // Init baseclasses first
149 : ConfigItem( ROOTNODE_CMDOPTIONS )
150 // Init member then...
151{
152 // Get names and values of all accessible menu entries and fill internal structures.
153 // See impl_GetPropertyNames() for further information.
155 Sequence< Any > lValues = GetProperties ( lNames );
156
157 // Safe impossible cases.
158 // We need values from ALL configuration keys.
159 // Follow assignment use order of values in relation to our list of key names!
160 DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::SvtCommandOptions_Impl()\nI miss some values of configuration keys!\n" );
161
162 // Copy values from list in right order to our internal member.
163 // Attention: List for names and values have an internal construction pattern!
164 sal_Int32 nItem = 0;
165 OUString sCmd;
166
167 // Get names/values for disabled commands.
168 for( nItem=0; nItem < lNames.getLength(); ++nItem )
169 {
170 // Currently only one value
171 lValues[nItem] >>= sCmd;
172 m_aDisabledCommands.AddCommand( sCmd );
173 }
174
175/*TODO: Not used in the moment! see Notify() ...
176 // Enable notification mechanism of our baseclass.
177 // We need it to get information about changes outside these class on our used configuration keys! */
178 Sequence<OUString> aNotifySeq { "Disabled" };
179 EnableNotification( aNotifySeq, true );
180}
181
182// destructor
183
185{
186 assert(!IsModified()); // should have been committed
187}
188
189// public method
190
192{
193 std::unique_lock aGuard( GetOwnStaticMutex() );
194
196 Sequence< Any > lValues = GetProperties ( lNames );
197
198 // Safe impossible cases.
199 // We need values from ALL configuration keys.
200 // Follow assignment use order of values in relation to our list of key names!
201 DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
202
203 // Copy values from list in right order to our internal member.
204 // Attention: List for names and values have an internal construction pattern!
205 sal_Int32 nItem = 0;
206 OUString sCmd;
207
208 m_aDisabledCommands.Clear();
209
210 // Get names/values for disabled commands.
211 for( nItem=0; nItem < lNames.getLength(); ++nItem )
212 {
213 // Currently only one value
214 lValues[nItem] >>= sCmd;
215 m_aDisabledCommands.AddCommand( sCmd );
216 }
217
218 // don't forget to update all existing frames and her might cached dispatch objects!
219 // But look for already killed frames. We hold weak references instead of hard ones ...
220 for (SvtFrameVector::iterator pIt = m_lFrames.begin(); pIt != m_lFrames.end(); )
221 {
222 css::uno::Reference< css::frame::XFrame > xFrame(pIt->get(), css::uno::UNO_QUERY);
223 if (xFrame.is())
224 {
225 xFrame->contextChanged();
226 ++pIt;
227 }
228 else
229 pIt = m_lFrames.erase(pIt);
230 }
231}
232
233// public method
234
236{
237 SAL_WARN("unotools.config","SvtCommandOptions_Impl::ImplCommit(): Not implemented yet!");
238}
239
240// public method
241
243{
244 return m_aDisabledCommands.HasEntries();
245}
246
247// public method
248
249bool SvtCommandOptions_Impl::LookupDisabled( const OUString& aCommand ) const
250{
251 return m_aDisabledCommands.Lookup( aCommand );
252}
253
254// public method
255
256void SvtCommandOptions_Impl::EstablishFrameCallback(const css::uno::Reference< css::frame::XFrame >& xFrame)
257{
258 // check if frame already exists inside list
259 // ignore double registrations
260 // every frame must be notified one times only!
261 css::uno::WeakReference< css::frame::XFrame > xWeak(xFrame);
262 SvtFrameVector::const_iterator pIt = ::std::find(m_lFrames.begin(), m_lFrames.end(), xWeak);
263 if (pIt == m_lFrames.end())
264 m_lFrames.push_back(xWeak);
265}
266
267// private method
268
270{
271 // First get ALL names of current existing list items in configuration!
273
274 // Expand all keys
275 for (OUString& rItem : asNonConstRange(lDisabledItems))
277
278 // Return result.
279 return lDisabledItems;
280}
281
282namespace {
283
284std::weak_ptr<SvtCommandOptions_Impl> g_pCommandOptions;
285
286}
287
289{
290 // Global access, must be guarded (multithreading!).
291 std::unique_lock aGuard( GetOwnStaticMutex() );
292
293 m_pImpl = g_pCommandOptions.lock();
294 if( !m_pImpl )
295 {
296 m_pImpl = std::make_shared<SvtCommandOptions_Impl>();
297 g_pCommandOptions = m_pImpl;
298 aGuard.unlock(); // because holdConfigItem will call this constructor
300 }
301}
302
304{
305 // Global access, must be guarded (multithreading!)
306 std::unique_lock aGuard( GetOwnStaticMutex() );
307
308 m_pImpl.reset();
309}
310
311// public method
312
314{
315 std::unique_lock aGuard( GetOwnStaticMutex() );
316 return m_pImpl->HasEntriesDisabled();
317}
318
319// public method
320
321bool SvtCommandOptions::LookupDisabled( const OUString& aCommandURL ) const
322{
323 std::unique_lock aGuard( GetOwnStaticMutex() );
324 return m_pImpl->LookupDisabled( aCommandURL );
325}
326
327// public method
328
329void SvtCommandOptions::EstablishFrameCallback(const css::uno::Reference< css::frame::XFrame >& xFrame)
330{
331 std::unique_lock aGuard( GetOwnStaticMutex() );
332 m_pImpl->EstablishFrameCallback(xFrame);
333}
334
335/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void holdConfigItem(EItem eItem)
Definition: itemholder1.cxx:68
virtual void Notify(const Sequence< OUString > &lPropertyNames) override
Definition: cmdoptions.cxx:191
SvtFrameVector m_lFrames
Definition: cmdoptions.cxx:142
SvtCmdOptions m_aDisabledCommands
Definition: cmdoptions.cxx:141
virtual void ImplCommit() override
Definition: cmdoptions.cxx:235
virtual ~SvtCommandOptions_Impl() override
Definition: cmdoptions.cxx:184
bool HasEntriesDisabled() const
Definition: cmdoptions.cxx:242
Sequence< OUString > impl_GetPropertyNames()
Definition: cmdoptions.cxx:269
void EstablishFrameCallback(const css::uno::Reference< css::frame::XFrame > &xFrame)
Definition: cmdoptions.cxx:256
bool LookupDisabled(const OUString &) const
Definition: cmdoptions.cxx:249
bool LookupDisabled(const OUString &aCommandURL) const
Definition: cmdoptions.cxx:321
bool HasEntriesDisabled() const
Definition: cmdoptions.cxx:313
virtual ~SvtCommandOptions() override
Definition: cmdoptions.cxx:303
std::shared_ptr< SvtCommandOptions_Impl > m_pImpl
Definition: cmdoptions.hxx:107
void EstablishFrameCallback(const css::uno::Reference< css::frame::XFrame > &xFrame)
Definition: cmdoptions.cxx:329
constexpr OUStringLiteral ROOTNODE_CMDOPTIONS
Definition: cmdoptions.cxx:41
#define SETNODE_DISABLED
Definition: cmdoptions.cxx:44
#define PROPERTYNAME_CMD
Definition: cmdoptions.cxx:46
::std::vector< css::uno::WeakReference< css::frame::XFrame > > SvtFrameVector
Definition: cmdoptions.cxx:94
#define PATHDELIMITER
Definition: cmdoptions.cxx:42
#define DBG_ASSERT(sCon, aError)
float u
static std::mutex & GetOwnStaticMutex()
Definition: eventcfg.cxx:83
std::unordered_map< sal_Int16, sal_Int16 > CommandHashMap
@ CmdOptions
#define SAL_WARN(area, stream)
Reference< XFrame > xFrame
std::mutex mutex
Definition: textsearch.cxx:94
OUString aCommand