| 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 <CommandDispatch.hxx> |
| 23 |
#include <com/sun/star/util/URLTransformer.hpp> |
| 24 |
|
| 25 |
using namespace ::com::sun::star; |
| 26 |
|
| 27 |
using ::com::sun::star::uno::Reference; |
| 28 |
using ::com::sun::star::uno::Sequence; |
| 29 |
|
| 30 |
namespace chart |
| 31 |
{ |
| 32 |
|
| 33 |
CommandDispatch::CommandDispatch( |
| 34 |
const Reference< uno::XComponentContext > & xContext ) : |
| 35 |
m_xContext( xContext ) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
CommandDispatch::~CommandDispatch() |
| 40 |
{} |
| 41 |
|
| 42 |
void CommandDispatch::initialize() |
| 43 |
{} |
| 44 |
|
| 45 |
// ____ WeakComponentImplHelperBase ____ |
| 46 |
/// is called when this is disposed |
| 47 |
void CommandDispatch::disposing(std::unique_lock<std::mutex>& rGuard) |
| 48 |
{ |
| 49 |
Reference< uno::XInterface > xEventSource(static_cast< cppu::OWeakObject* >( this )); |
| 50 |
for( auto& rElement : m_aListeners ) |
| 51 |
rElement.second.disposeAndClear( rGuard, xEventSource ); |
| 52 |
m_aListeners.clear(); |
| 53 |
} |
| 54 |
|
| 55 |
// ____ XDispatch ____ |
| 56 |
void SAL_CALL CommandDispatch::dispatch( const util::URL& /* URL */, const Sequence< beans::PropertyValue >& /* Arguments */ ) |
| 57 |
{} |
| 58 |
|
| 59 |
void SAL_CALL CommandDispatch::addStatusListener( const Reference< frame::XStatusListener >& Control, const util::URL& URL ) |
| 60 |
{ |
| 61 |
{ |
| 62 |
std::unique_lock g(m_aMutex); |
| 63 |
tListenerMap::iterator aIt( m_aListeners.find( URL.Complete )); |
| 64 |
if( aIt == m_aListeners.end()) |
| 65 |
{ |
| 66 |
aIt = m_aListeners.emplace( |
| 67 |
std::piecewise_construct, |
| 68 |
std::forward_as_tuple(URL.Complete), |
| 69 |
std::forward_as_tuple()).first; |
| 70 |
} |
| 71 |
assert( aIt != m_aListeners.end()); |
| 72 |
|
| 73 |
aIt->second.addInterface( g, Control ); |
| 74 |
} |
| 75 |
fireStatusEvent( URL.Complete, Control ); |
| 76 |
} |
| 77 |
|
| 78 |
void SAL_CALL CommandDispatch::removeStatusListener( const Reference< frame::XStatusListener >& Control, const util::URL& URL ) |
| 79 |
{ |
| 80 |
std::unique_lock g(m_aMutex); |
| 81 |
tListenerMap::iterator aIt( m_aListeners.find( URL.Complete )); |
| 82 |
if( aIt != m_aListeners.end()) |
| 83 |
(*aIt).second.removeInterface( g, Control ); |
| 84 |
} |
| 85 |
|
| 86 |
// ____ XModifyListener ____ |
| 87 |
void SAL_CALL CommandDispatch::modified( const lang::EventObject& /* aEvent */ ) |
| 88 |
{ |
| 89 |
fireAllStatusEvents( nullptr ); |
| 90 |
} |
| 91 |
|
| 92 |
// ____ XEventListener (base of XModifyListener) ____ |
| 93 |
void SAL_CALL CommandDispatch::disposing( const lang::EventObject& /* Source */ ) |
| 94 |
{} |
| 95 |
|
| 96 |
void CommandDispatch::fireAllStatusEvents( |
| 97 |
const css::uno::Reference< css::frame::XStatusListener > & xSingleListener ) |
| 98 |
{ |
| 99 |
fireStatusEvent( OUString(), xSingleListener ); |
| 100 |
} |
| 101 |
|
| 102 |
void CommandDispatch::fireStatusEventForURL( |
| 103 |
const OUString & rURL, |
| 104 |
const uno::Any & rState, |
| 105 |
bool bEnabled, |
| 106 |
const Reference< frame::XStatusListener > & xSingleListener /* = 0 */) |
| 107 |
{ |
| 108 |
// prepare event to send |
| 109 |
util::URL aURL; |
| 110 |
aURL.Complete = rURL; |
| 111 |
if( !m_xURLTransformer.is()) |
| 112 |
{ |
| 113 |
m_xURLTransformer.set( util::URLTransformer::create(m_xContext) ); |
| 114 |
} |
| 115 |
m_xURLTransformer->parseStrict( aURL ); |
| 116 |
|
| 117 |
frame::FeatureStateEvent aEventToSend( |
| 118 |
static_cast< cppu::OWeakObject* >( this ), // Source |
| 119 |
aURL, // FeatureURL |
| 120 |
OUString(), // FeatureDescriptor |
| 121 |
bEnabled, // IsEnabled |
| 122 |
false, // Requery |
| 123 |
rState // State |
| 124 |
); |
| 125 |
|
| 126 |
// send event either to single listener or all registered ones |
| 127 |
if( xSingleListener.is()) |
| 128 |
xSingleListener->statusChanged( aEventToSend ); |
| 129 |
else |
| 130 |
{ |
| 131 |
tListenerMap::iterator aIt( m_aListeners.find( aURL.Complete )); |
| 132 |
if( aIt != m_aListeners.end()) |
| 133 |
{ |
| 134 |
std::unique_lock g(m_aMutex); |
| 135 |
aIt->second.notifyEach(g, &css::frame::XStatusListener::statusChanged, aEventToSend); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
} // namespace chart |
| 141 |
|
| 142 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |