Line data Source code
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 "tools/SlotStateListener.hxx"
21 : #include <com/sun/star/frame/XStatusListener.hpp>
22 : #include <com/sun/star/frame/XDispatchProvider.hpp>
23 : #include <com/sun/star/frame/XDispatch.hpp>
24 : #include <com/sun/star/util/URLTransformer.hpp>
25 : #include <com/sun/star/util/XURLTransformer.hpp>
26 : #include <com/sun/star/beans/PropertyChangeEvent.hpp>
27 :
28 : #include <comphelper/processfactory.hxx>
29 :
30 : using namespace ::com::sun::star;
31 :
32 : namespace sd { namespace tools {
33 :
34 24 : SlotStateListener::SlotStateListener (
35 : Link& rCallback,
36 : const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider,
37 : const OUString& rSlotName)
38 : : SlotStateListenerInterfaceBase(maMutex),
39 : maCallback(),
40 24 : mxDispatchProviderWeak(NULL)
41 : {
42 24 : SetCallback(rCallback);
43 24 : ConnectToDispatchProvider(rxDispatchProvider);
44 24 : ObserveSlot(rSlotName);
45 24 : }
46 :
47 72 : SlotStateListener::~SlotStateListener (void)
48 : {
49 24 : ReleaseListeners();
50 48 : }
51 :
52 24 : void SlotStateListener::SetCallback (const Link& rCallback)
53 : {
54 24 : ThrowIfDisposed();
55 :
56 24 : maCallback = rCallback;
57 24 : }
58 :
59 24 : void SlotStateListener::ConnectToDispatchProvider (
60 : const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider)
61 : {
62 24 : ThrowIfDisposed();
63 :
64 : // When we are listening to state changes of slots of another frame then
65 : // release these listeners first.
66 24 : if ( ! maRegisteredURLList.empty())
67 0 : ReleaseListeners();
68 :
69 24 : mxDispatchProviderWeak = rxDispatchProvider;
70 24 : }
71 :
72 24 : void SlotStateListener::ObserveSlot (const OUString& rSlotName)
73 : {
74 24 : ThrowIfDisposed();
75 :
76 24 : if (maCallback.IsSet())
77 : {
78 : // Connect the state change listener.
79 24 : util::URL aURL (MakeURL(rSlotName));
80 48 : uno::Reference<frame::XDispatch> xDispatch (GetDispatch(aURL));
81 24 : if (xDispatch.is())
82 : {
83 24 : maRegisteredURLList.push_back(aURL);
84 24 : xDispatch->addStatusListener(this,aURL);
85 24 : }
86 : }
87 24 : }
88 :
89 24 : void SlotStateListener::disposing (void)
90 : {
91 24 : ReleaseListeners();
92 24 : mxDispatchProviderWeak = uno::WeakReference<frame::XDispatchProvider>(NULL);
93 24 : maCallback = Link();
94 24 : }
95 :
96 24 : util::URL SlotStateListener::MakeURL (const OUString& rSlotName) const
97 : {
98 24 : util::URL aURL;
99 24 : aURL.Complete = rSlotName;
100 :
101 48 : uno::Reference<util::XURLTransformer> xTransformer(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
102 24 : xTransformer->parseStrict(aURL);
103 :
104 48 : return aURL;
105 : }
106 :
107 : uno::Reference<frame::XDispatch>
108 72 : SlotStateListener::GetDispatch (const util::URL& rURL) const
109 : {
110 72 : uno::Reference<frame::XDispatch> xDispatch;
111 :
112 144 : uno::Reference<frame::XDispatchProvider> xDispatchProvider (mxDispatchProviderWeak);
113 72 : if (xDispatchProvider.is())
114 48 : xDispatch = xDispatchProvider->queryDispatch(rURL, OUString(), 0);
115 :
116 144 : return xDispatch;
117 : }
118 :
119 24 : void SlotStateListener::statusChanged (
120 : const frame::FeatureStateEvent& rState)
121 : throw (uno::RuntimeException, std::exception)
122 : {
123 24 : ThrowIfDisposed();
124 24 : OUString sSlotName (rState.FeatureURL.Complete);
125 24 : if (maCallback.IsSet())
126 24 : maCallback.Call(&sSlotName);
127 24 : }
128 :
129 48 : void SlotStateListener::ReleaseListeners (void)
130 : {
131 48 : if ( ! maRegisteredURLList.empty())
132 : {
133 48 : RegisteredURLList::iterator iURL (maRegisteredURLList.begin());
134 48 : RegisteredURLList::iterator iEnd (maRegisteredURLList.end());
135 96 : for (; iURL!=iEnd; ++iURL)
136 : {
137 48 : uno::Reference<frame::XDispatch> xDispatch (GetDispatch(*iURL));
138 48 : if (xDispatch.is())
139 : {
140 0 : xDispatch->removeStatusListener(this,*iURL);
141 : }
142 48 : }
143 : }
144 48 : }
145 :
146 : //===== lang::XEventListener ================================================
147 :
148 0 : void SAL_CALL SlotStateListener::disposing (
149 : const lang::EventObject& )
150 : throw (uno::RuntimeException, std::exception)
151 : {
152 0 : }
153 :
154 96 : void SlotStateListener::ThrowIfDisposed (void)
155 : throw (lang::DisposedException)
156 : {
157 96 : if (rBHelper.bDisposed || rBHelper.bInDispose)
158 : {
159 : throw lang::DisposedException ("SlideSorterController object has already been disposed",
160 0 : static_cast<uno::XWeak*>(this));
161 : }
162 96 : }
163 :
164 : } } // end of namespace ::sd::tools
165 :
166 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|