1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*5b190011SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*5b190011SAndrew Rist * distributed with this work for additional information
6*5b190011SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*5b190011SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist * with the License. You may obtain a copy of the License at
10*5b190011SAndrew Rist *
11*5b190011SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist *
13*5b190011SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist * software distributed under the License is distributed on an
15*5b190011SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist * KIND, either express or implied. See the License for the
17*5b190011SAndrew Rist * specific language governing permissions and limitations
18*5b190011SAndrew Rist * under the License.
19*5b190011SAndrew Rist *
20*5b190011SAndrew Rist *************************************************************/
21*5b190011SAndrew Rist
22*5b190011SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "precompiled_sd.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include "ReadOnlyModeObserver.hxx"
27cdf0e10cSrcweir
28cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XControllerManager.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
30cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
31cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
32cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.hxx>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #include "tools/SlotStateListener.hxx"
35cdf0e10cSrcweir #include "framework/FrameworkHelper.hxx"
36cdf0e10cSrcweir
37cdf0e10cSrcweir using namespace ::com::sun::star::uno;
38cdf0e10cSrcweir using namespace ::com::sun::star;
39cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework;
40cdf0e10cSrcweir using ::rtl::OUString;
41cdf0e10cSrcweir
42cdf0e10cSrcweir namespace sd { namespace framework {
43cdf0e10cSrcweir
44cdf0e10cSrcweir class ReadOnlyModeObserver::ModifyBroadcaster
45cdf0e10cSrcweir : public ::cppu::OBroadcastHelper
46cdf0e10cSrcweir {
47cdf0e10cSrcweir public:
ModifyBroadcaster(::osl::Mutex & rOslMutex)48cdf0e10cSrcweir explicit ModifyBroadcaster (::osl::Mutex& rOslMutex) : ::cppu::OBroadcastHelper(rOslMutex) {}
49cdf0e10cSrcweir };
50cdf0e10cSrcweir
51cdf0e10cSrcweir
52cdf0e10cSrcweir
53cdf0e10cSrcweir
ReadOnlyModeObserver(const Reference<frame::XController> & rxController)54cdf0e10cSrcweir ReadOnlyModeObserver::ReadOnlyModeObserver (
55cdf0e10cSrcweir const Reference<frame::XController>& rxController)
56cdf0e10cSrcweir : ReadOnlyModeObserverInterfaceBase(maMutex),
57cdf0e10cSrcweir maSlotNameURL(),
58cdf0e10cSrcweir mxController(rxController),
59cdf0e10cSrcweir mxConfigurationController(NULL),
60cdf0e10cSrcweir mxDispatch(NULL),
61cdf0e10cSrcweir mpBroadcaster(new ModifyBroadcaster(maMutex))
62cdf0e10cSrcweir {
63cdf0e10cSrcweir // Create a URL object for the slot name.
64cdf0e10cSrcweir maSlotNameURL.Complete = OUString::createFromAscii(".uno:EditDoc");
65cdf0e10cSrcweir uno::Reference<lang::XMultiServiceFactory> xServiceManager (
66cdf0e10cSrcweir ::comphelper::getProcessServiceFactory());
67cdf0e10cSrcweir if (xServiceManager.is())
68cdf0e10cSrcweir {
69cdf0e10cSrcweir Reference<util::XURLTransformer> xTransformer(xServiceManager->createInstance(
70cdf0e10cSrcweir OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.util.URLTransformer"))),
71cdf0e10cSrcweir UNO_QUERY);
72cdf0e10cSrcweir if (xTransformer.is())
73cdf0e10cSrcweir xTransformer->parseStrict(maSlotNameURL);
74cdf0e10cSrcweir }
75cdf0e10cSrcweir
76cdf0e10cSrcweir if ( ! ConnectToDispatch())
77cdf0e10cSrcweir {
78cdf0e10cSrcweir // The controller is not yet connected to a frame. This means that
79cdf0e10cSrcweir // the dispatcher is not yet set up. We wait for this to happen by
80cdf0e10cSrcweir // waiting for configuration updates and try again.
81cdf0e10cSrcweir Reference<XControllerManager> xControllerManager (rxController, UNO_QUERY);
82cdf0e10cSrcweir if (xControllerManager.is())
83cdf0e10cSrcweir {
84cdf0e10cSrcweir mxConfigurationController = xControllerManager->getConfigurationController();
85cdf0e10cSrcweir if (mxConfigurationController.is())
86cdf0e10cSrcweir {
87cdf0e10cSrcweir mxConfigurationController->addConfigurationChangeListener(
88cdf0e10cSrcweir this,
89cdf0e10cSrcweir FrameworkHelper::msConfigurationUpdateStartEvent,
90cdf0e10cSrcweir Any());
91cdf0e10cSrcweir }
92cdf0e10cSrcweir }
93cdf0e10cSrcweir }
94cdf0e10cSrcweir }
95cdf0e10cSrcweir
96cdf0e10cSrcweir
97cdf0e10cSrcweir
98cdf0e10cSrcweir
~ReadOnlyModeObserver(void)99cdf0e10cSrcweir ReadOnlyModeObserver::~ReadOnlyModeObserver (void)
100cdf0e10cSrcweir {
101cdf0e10cSrcweir }
102cdf0e10cSrcweir
103cdf0e10cSrcweir
104cdf0e10cSrcweir
105cdf0e10cSrcweir
disposing(void)106cdf0e10cSrcweir void SAL_CALL ReadOnlyModeObserver::disposing (void)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir if (mxController.is())
109cdf0e10cSrcweir {
110cdf0e10cSrcweir mxController = NULL;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir if (mxConfigurationController.is())
113cdf0e10cSrcweir {
114cdf0e10cSrcweir mxConfigurationController->removeConfigurationChangeListener(this);
115cdf0e10cSrcweir mxConfigurationController = NULL;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir if (mxDispatch.is())
118cdf0e10cSrcweir {
119cdf0e10cSrcweir mxDispatch->removeStatusListener(this, maSlotNameURL);
120cdf0e10cSrcweir mxDispatch = NULL;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir
123cdf0e10cSrcweir lang::EventObject aEvent;
124cdf0e10cSrcweir aEvent.Source = static_cast<XWeak*>(this);
125cdf0e10cSrcweir ::cppu::OInterfaceContainerHelper* pIterator
126cdf0e10cSrcweir = mpBroadcaster->getContainer(getCppuType((Reference<frame::XStatusListener>*)NULL));
127cdf0e10cSrcweir pIterator->disposeAndClear(aEvent);
128cdf0e10cSrcweir }
129cdf0e10cSrcweir
130cdf0e10cSrcweir
131cdf0e10cSrcweir
132cdf0e10cSrcweir
AddStatusListener(const Reference<frame::XStatusListener> & rxListener)133cdf0e10cSrcweir void ReadOnlyModeObserver::AddStatusListener (
134cdf0e10cSrcweir const Reference<frame::XStatusListener>& rxListener)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir mpBroadcaster->addListener(
137cdf0e10cSrcweir getCppuType((Reference<frame::XStatusListener>*)NULL),
138cdf0e10cSrcweir rxListener);
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
141cdf0e10cSrcweir
142cdf0e10cSrcweir
143cdf0e10cSrcweir
ConnectToDispatch(void)144cdf0e10cSrcweir bool ReadOnlyModeObserver::ConnectToDispatch (void)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir if ( ! mxDispatch.is())
147cdf0e10cSrcweir {
148cdf0e10cSrcweir // Get the dispatch object.
149cdf0e10cSrcweir Reference<frame::XDispatchProvider> xProvider (mxController->getFrame(), UNO_QUERY);
150cdf0e10cSrcweir if (xProvider.is())
151cdf0e10cSrcweir {
152cdf0e10cSrcweir mxDispatch = xProvider->queryDispatch(maSlotNameURL, OUString(), 0);
153cdf0e10cSrcweir if (mxDispatch.is())
154cdf0e10cSrcweir {
155cdf0e10cSrcweir mxDispatch->addStatusListener(this, maSlotNameURL);
156cdf0e10cSrcweir }
157cdf0e10cSrcweir }
158cdf0e10cSrcweir }
159cdf0e10cSrcweir
160cdf0e10cSrcweir return mxDispatch.is();
161cdf0e10cSrcweir }
162cdf0e10cSrcweir
163cdf0e10cSrcweir
164cdf0e10cSrcweir
165cdf0e10cSrcweir
statusChanged(const frame::FeatureStateEvent & rEvent)166cdf0e10cSrcweir void ReadOnlyModeObserver::statusChanged (const frame::FeatureStateEvent& rEvent)
167cdf0e10cSrcweir throw (RuntimeException)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir ::cppu::OInterfaceContainerHelper* pIterator
170cdf0e10cSrcweir = mpBroadcaster->getContainer(getCppuType((Reference<frame::XStatusListener>*)NULL));
171cdf0e10cSrcweir if (pIterator != NULL)
172cdf0e10cSrcweir {
173cdf0e10cSrcweir pIterator->notifyEach(&frame::XStatusListener::statusChanged, rEvent);
174cdf0e10cSrcweir }
175cdf0e10cSrcweir }
176cdf0e10cSrcweir
177cdf0e10cSrcweir
178cdf0e10cSrcweir
179cdf0e10cSrcweir
180cdf0e10cSrcweir //----- XEventListener --------------------------------------------------------
181cdf0e10cSrcweir
disposing(const lang::EventObject & rEvent)182cdf0e10cSrcweir void SAL_CALL ReadOnlyModeObserver::disposing (
183cdf0e10cSrcweir const lang::EventObject& rEvent)
184cdf0e10cSrcweir throw (RuntimeException)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir if (rEvent.Source == mxConfigurationController)
187cdf0e10cSrcweir mxConfigurationController = NULL;
188cdf0e10cSrcweir else if (rEvent.Source == mxDispatch)
189cdf0e10cSrcweir mxDispatch = NULL;
190cdf0e10cSrcweir
191cdf0e10cSrcweir dispose();
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
194cdf0e10cSrcweir
195cdf0e10cSrcweir
196cdf0e10cSrcweir
notifyConfigurationChange(const ConfigurationChangeEvent & rEvent)197cdf0e10cSrcweir void SAL_CALL ReadOnlyModeObserver::notifyConfigurationChange (
198cdf0e10cSrcweir const ConfigurationChangeEvent& rEvent)
199cdf0e10cSrcweir throw (RuntimeException)
200cdf0e10cSrcweir {
201cdf0e10cSrcweir if (rEvent.Type.equals(FrameworkHelper::msConfigurationUpdateStartEvent))
202cdf0e10cSrcweir {
203cdf0e10cSrcweir if (mxController.is() && mxController->getFrame().is())
204cdf0e10cSrcweir {
205cdf0e10cSrcweir if (ConnectToDispatch())
206cdf0e10cSrcweir {
207cdf0e10cSrcweir // We have connected successfully to the dispatcher and
208cdf0e10cSrcweir // therefore can disconnect from the configuration controller.
209cdf0e10cSrcweir mxConfigurationController->removeConfigurationChangeListener(this);
210cdf0e10cSrcweir mxConfigurationController = NULL;
211cdf0e10cSrcweir }
212cdf0e10cSrcweir }
213cdf0e10cSrcweir }
214cdf0e10cSrcweir }
215cdf0e10cSrcweir
216cdf0e10cSrcweir } } // end of namespace sd::framework
217