Update ooo320-m1
[ooovba.git] / sd / source / ui / framework / module / ReadOnlyModeObserver.cxx
blob944f5dde52535014b5410efab439d9000ec2f5cc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ReadOnlyModeObserver.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sd.hxx"
33 #include "ReadOnlyModeObserver.hxx"
35 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
36 #include <com/sun/star/lang/DisposedException.hpp>
37 #include <com/sun/star/util/XURLTransformer.hpp>
38 #include <comphelper/processfactory.hxx>
39 #include <cppuhelper/interfacecontainer.hxx>
41 #include "tools/SlotStateListener.hxx"
42 #include "framework/FrameworkHelper.hxx"
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::drawing::framework;
47 using ::rtl::OUString;
49 namespace sd { namespace framework {
51 class ReadOnlyModeObserver::ModifyBroadcaster
52 : public ::cppu::OBroadcastHelper
54 public:
55 explicit ModifyBroadcaster (::osl::Mutex& rOslMutex) : ::cppu::OBroadcastHelper(rOslMutex) {}
61 ReadOnlyModeObserver::ReadOnlyModeObserver (
62 const Reference<frame::XController>& rxController)
63 : ReadOnlyModeObserverInterfaceBase(maMutex),
64 maSlotNameURL(),
65 mxController(rxController),
66 mxConfigurationController(NULL),
67 mxDispatch(NULL),
68 mpBroadcaster(new ModifyBroadcaster(maMutex))
70 // Create a URL object for the slot name.
71 maSlotNameURL.Complete = OUString::createFromAscii(".uno:EditDoc");
72 uno::Reference<lang::XMultiServiceFactory> xServiceManager (
73 ::comphelper::getProcessServiceFactory());
74 if (xServiceManager.is())
76 Reference<util::XURLTransformer> xTransformer(xServiceManager->createInstance(
77 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.util.URLTransformer"))),
78 UNO_QUERY);
79 if (xTransformer.is())
80 xTransformer->parseStrict(maSlotNameURL);
83 if ( ! ConnectToDispatch())
85 // The controller is not yet connected to a frame. This means that
86 // the dispatcher is not yet set up. We wait for this to happen by
87 // waiting for configuration updates and try again.
88 Reference<XControllerManager> xControllerManager (rxController, UNO_QUERY);
89 if (xControllerManager.is())
91 mxConfigurationController = xControllerManager->getConfigurationController();
92 if (mxConfigurationController.is())
94 mxConfigurationController->addConfigurationChangeListener(
95 this,
96 FrameworkHelper::msConfigurationUpdateStartEvent,
97 Any());
106 ReadOnlyModeObserver::~ReadOnlyModeObserver (void)
113 void SAL_CALL ReadOnlyModeObserver::disposing (void)
115 if (mxController.is())
117 mxController = NULL;
119 if (mxConfigurationController.is())
121 mxConfigurationController->removeConfigurationChangeListener(this);
122 mxConfigurationController = NULL;
124 if (mxDispatch.is())
126 mxDispatch->removeStatusListener(this, maSlotNameURL);
127 mxDispatch = NULL;
130 lang::EventObject aEvent;
131 aEvent.Source = static_cast<XWeak*>(this);
132 ::cppu::OInterfaceContainerHelper* pIterator
133 = mpBroadcaster->getContainer(getCppuType((Reference<frame::XStatusListener>*)NULL));
134 pIterator->disposeAndClear(aEvent);
140 void ReadOnlyModeObserver::AddStatusListener (
141 const Reference<frame::XStatusListener>& rxListener)
143 mpBroadcaster->addListener(
144 getCppuType((Reference<frame::XStatusListener>*)NULL),
145 rxListener);
151 void ReadOnlyModeObserver::RemoveStatusListener (
152 const Reference<frame::XStatusListener>& rxListener)
154 mpBroadcaster->removeListener(
155 getCppuType((Reference<frame::XStatusListener>*)NULL),
156 rxListener);
162 bool ReadOnlyModeObserver::ConnectToDispatch (void)
164 if ( ! mxDispatch.is())
166 // Get the dispatch object.
167 Reference<frame::XDispatchProvider> xProvider (mxController->getFrame(), UNO_QUERY);
168 if (xProvider.is())
170 mxDispatch = xProvider->queryDispatch(maSlotNameURL, OUString(), 0);
171 if (mxDispatch.is())
173 mxDispatch->addStatusListener(this, maSlotNameURL);
178 return mxDispatch.is();
184 void ReadOnlyModeObserver::statusChanged (const frame::FeatureStateEvent& rEvent)
185 throw (RuntimeException)
187 ::cppu::OInterfaceContainerHelper* pIterator
188 = mpBroadcaster->getContainer(getCppuType((Reference<frame::XStatusListener>*)NULL));
189 if (pIterator != NULL)
191 pIterator->notifyEach(&frame::XStatusListener::statusChanged, rEvent);
198 //----- XEventListener --------------------------------------------------------
200 void SAL_CALL ReadOnlyModeObserver::disposing (
201 const lang::EventObject& rEvent)
202 throw (RuntimeException)
204 if (rEvent.Source == mxConfigurationController)
205 mxConfigurationController = NULL;
206 else if (rEvent.Source == mxDispatch)
207 mxDispatch = NULL;
209 dispose();
215 void SAL_CALL ReadOnlyModeObserver::notifyConfigurationChange (
216 const ConfigurationChangeEvent& rEvent)
217 throw (RuntimeException)
219 if (rEvent.Type.equals(FrameworkHelper::msConfigurationUpdateStartEvent))
221 if (mxController.is() && mxController->getFrame().is())
223 if (ConnectToDispatch())
225 // We have connected successfully to the dispatcher and
226 // therefore can disconnect from the configuration controller.
227 mxConfigurationController->removeConfigurationChangeListener(this);
228 mxConfigurationController = NULL;
234 } } // end of namespace sd::framework