1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <dispatch/interceptionhelper.hxx>
22 #include <com/sun/star/frame/XInterceptorInfo.hpp>
24 #include <vcl/svapp.hxx>
28 bool InterceptionHelper::m_bPreferrFirstInterceptor
= true;
30 InterceptionHelper::InterceptionHelper(const css::uno::Reference
< css::frame::XFrame
>& xOwner
,
31 const css::uno::Reference
< css::frame::XDispatchProvider
>& xSlave
)
32 : m_xOwnerWeak (xOwner
)
37 InterceptionHelper::~InterceptionHelper()
41 css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
InterceptionHelper::queryDispatch(const css::util::URL
& aURL
,
42 const OUString
& sTargetFrameName
,
43 sal_Int32 nSearchFlags
)
44 throw(css::uno::RuntimeException
, std::exception
)
47 SolarMutexClearableGuard aReadLock
;
49 // a) first search an interceptor, which match to this URL by its URL pattern registration
50 // Note: if it return NULL - it does not mean an empty interceptor list automatically!
51 css::uno::Reference
< css::frame::XDispatchProvider
> xInterceptor
;
52 InterceptorList::const_iterator pIt
= m_lInterceptionRegs
.findByPattern(aURL
.Complete
);
53 if (pIt
!= m_lInterceptionRegs
.end())
54 xInterceptor
= pIt
->xInterceptor
;
56 // b) No match by registration - but a valid interceptor list.
57 // Use first interceptor everytimes.
58 // Note: it doesn't matter, which direction this helper implementation use to ask interceptor objects.
59 // Using of member m_aInterceptorList will starts at the beginning everytimes.
60 // It depends from the filling operation, in which direction it works really!
61 if (!xInterceptor
.is() && m_lInterceptionRegs
.size()>0)
63 pIt
= m_lInterceptionRegs
.begin();
64 xInterceptor
= pIt
->xInterceptor
;
67 // c) No registered interceptor => use our direct slave.
68 // This helper exist by design and must be valid everytimes ...
69 // But to be more feature proof - we should check that .-)
70 if (!xInterceptor
.is() && m_xSlave
.is())
71 xInterceptor
= m_xSlave
;
76 css::uno::Reference
< css::frame::XDispatch
> xReturn
;
77 if (xInterceptor
.is())
78 xReturn
= xInterceptor
->queryDispatch(aURL
, sTargetFrameName
, nSearchFlags
);
82 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > SAL_CALL
InterceptionHelper::queryDispatches( const css::uno::Sequence
< css::frame::DispatchDescriptor
>& lDescriptor
)
83 throw(css::uno::RuntimeException
, std::exception
)
85 sal_Int32 c
= lDescriptor
.getLength();
86 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > lDispatches (c
);
87 css::uno::Reference
< css::frame::XDispatch
>* pDispatches
= lDispatches
.getArray();
88 const css::frame::DispatchDescriptor
* pDescriptor
= lDescriptor
.getConstArray();
90 for (sal_Int32 i
=0; i
<c
; ++i
)
91 pDispatches
[i
] = queryDispatch(pDescriptor
[i
].FeatureURL
, pDescriptor
[i
].FrameName
, pDescriptor
[i
].SearchFlags
);
96 void SAL_CALL
InterceptionHelper::registerDispatchProviderInterceptor(const css::uno::Reference
< css::frame::XDispatchProviderInterceptor
>& xInterceptor
)
97 throw(css::uno::RuntimeException
, std::exception
)
99 // reject wrong calling of this interface method
100 css::uno::Reference
< css::frame::XDispatchProvider
> xThis(static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
101 if (!xInterceptor
.is())
102 throw css::uno::RuntimeException("NULL references not allowed as in parameter", xThis
);
104 // Fill a new info structure for new interceptor.
105 // Save his reference and try to get an additional URL/pattern list from him.
106 // If no list exist register these interceptor for all dispatch events with "*"!
107 InterceptorInfo aInfo
;
109 aInfo
.xInterceptor
= css::uno::Reference
< css::frame::XDispatchProvider
>(xInterceptor
, css::uno::UNO_QUERY
);
110 css::uno::Reference
< css::frame::XInterceptorInfo
> xInfo(xInterceptor
, css::uno::UNO_QUERY
);
112 aInfo
.lURLPattern
= xInfo
->getInterceptedURLs();
115 aInfo
.lURLPattern
.realloc(1);
116 aInfo
.lURLPattern
[0] = "*";
120 SolarMutexClearableGuard aWriteLock
;
122 // a) no interceptor at all - set this instance as master for given interceptor
123 // and set our slave as it's slave - and put this interceptor to the list.
124 // It's place there doesn matter. Because this list is currently empty.
125 if (m_lInterceptionRegs
.empty())
127 xInterceptor
->setMasterDispatchProvider(xThis
);
128 xInterceptor
->setSlaveDispatchProvider (m_xSlave
);
129 m_lInterceptionRegs
.push_back(aInfo
);
132 // b) OK - there is at least one interceptor already registered.
133 // It's slave and it's master must be valid references ...
134 // because we created it. But we have to look for the static bool which
135 // regulate direction of using of interceptor objects!
137 // b1) If "m_bPreferrFirstInterceptor" is set to true, we have to
138 // insert it behind any other existing interceptor - means at the end of our list.
139 else if (m_bPreferrFirstInterceptor
)
141 css::uno::Reference
< css::frame::XDispatchProvider
> xMasterD
= m_lInterceptionRegs
.rbegin()->xInterceptor
;
142 css::uno::Reference
< css::frame::XDispatchProviderInterceptor
> xMasterI (xMasterD
, css::uno::UNO_QUERY
);
144 xInterceptor
->setMasterDispatchProvider(xMasterD
);
145 xInterceptor
->setSlaveDispatchProvider (m_xSlave
);
146 xMasterI
->setSlaveDispatchProvider (aInfo
.xInterceptor
);
148 m_lInterceptionRegs
.push_back(aInfo
);
151 // b2) If "m_bPreferrFirstInterceptor" is set to false, we have to
152 // insert it before any other existing interceptor - means at the beginning of our list.
155 css::uno::Reference
< css::frame::XDispatchProvider
> xSlaveD
= m_lInterceptionRegs
.begin()->xInterceptor
;
156 css::uno::Reference
< css::frame::XDispatchProviderInterceptor
> xSlaveI (xSlaveD
, css::uno::UNO_QUERY
);
158 xInterceptor
->setMasterDispatchProvider(xThis
);
159 xInterceptor
->setSlaveDispatchProvider (xSlaveD
);
160 xSlaveI
->setMasterDispatchProvider (aInfo
.xInterceptor
);
162 m_lInterceptionRegs
.push_front(aInfo
);
165 css::uno::Reference
< css::frame::XFrame
> xOwner(m_xOwnerWeak
.get(), css::uno::UNO_QUERY
);
170 // Don't forget to send a frame action event "context changed".
171 // Any cached dispatch objects must be validated now!
173 xOwner
->contextChanged();
176 void SAL_CALL
InterceptionHelper::releaseDispatchProviderInterceptor(const css::uno::Reference
< css::frame::XDispatchProviderInterceptor
>& xInterceptor
)
177 throw(css::uno::RuntimeException
, std::exception
)
179 // reject wrong calling of this interface method
180 css::uno::Reference
< css::frame::XDispatchProvider
> xThis(static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
181 if (!xInterceptor
.is())
182 throw css::uno::RuntimeException("NULL references not allowed as in parameter", xThis
);
185 SolarMutexClearableGuard aWriteLock
;
187 // search this interceptor ...
188 // If it could be located inside cache -
189 // use it's slave/master relations to update the interception list;
190 // set empty references for it as new master and slave;
191 // and relase it from out cache.
192 InterceptorList::iterator pIt
= m_lInterceptionRegs
.findByReference(xInterceptor
);
193 if (pIt
!= m_lInterceptionRegs
.end())
195 css::uno::Reference
< css::frame::XDispatchProvider
> xSlaveD (xInterceptor
->getSlaveDispatchProvider() , css::uno::UNO_QUERY
);
196 css::uno::Reference
< css::frame::XDispatchProvider
> xMasterD (xInterceptor
->getMasterDispatchProvider(), css::uno::UNO_QUERY
);
197 css::uno::Reference
< css::frame::XDispatchProviderInterceptor
> xSlaveI (xSlaveD
, css::uno::UNO_QUERY
);
198 css::uno::Reference
< css::frame::XDispatchProviderInterceptor
> xMasterI (xMasterD
, css::uno::UNO_QUERY
);
201 xMasterI
->setSlaveDispatchProvider(xSlaveD
);
204 xSlaveI
->setMasterDispatchProvider(xMasterD
);
206 xInterceptor
->setSlaveDispatchProvider (css::uno::Reference
< css::frame::XDispatchProvider
>());
207 xInterceptor
->setMasterDispatchProvider(css::uno::Reference
< css::frame::XDispatchProvider
>());
209 m_lInterceptionRegs
.erase(pIt
);
212 css::uno::Reference
< css::frame::XFrame
> xOwner(m_xOwnerWeak
.get(), css::uno::UNO_QUERY
);
217 // Don't forget to send a frame action event "context changed".
218 // Any cached dispatch objects must be validated now!
220 xOwner
->contextChanged();
223 #define FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
224 void SAL_CALL
InterceptionHelper::disposing(const css::lang::EventObject
& aEvent
)
225 throw(css::uno::RuntimeException
, std::exception
)
227 #ifdef FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
229 SolarMutexResettableGuard aReadLock
;
231 // check calli ... we accept such disposing call's only from our onwer frame.
232 css::uno::Reference
< css::frame::XFrame
> xOwner(m_xOwnerWeak
.get(), css::uno::UNO_QUERY
);
233 if (aEvent
.Source
!= xOwner
)
236 // Because every interceptor hold at least one reference to us ... and we destruct this list
237 // of interception objects ... we should hold ourself alive .-)
238 css::uno::Reference
< css::frame::XDispatchProvider
> xThis(static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY_THROW
);
240 // We need a full copy of all currently registered interceptor objects.
241 // Otherwise we can't iterate over this vector without the risk, that our iterator will be invalid.
242 // Because this vetor will be influenced by every deregistered interceptor.
243 InterceptionHelper::InterceptorList aCopy
= m_lInterceptionRegs
;
248 InterceptionHelper::InterceptorList::iterator pIt
;
249 for ( pIt
= aCopy
.begin();
253 InterceptionHelper::InterceptorInfo
& rInfo
= *pIt
;
254 if (rInfo
.xInterceptor
.is())
256 css::uno::Reference
< css::frame::XDispatchProviderInterceptor
> xInterceptor(rInfo
.xInterceptor
, css::uno::UNO_QUERY_THROW
);
257 releaseDispatchProviderInterceptor(xInterceptor
);
258 rInfo
.xInterceptor
.clear();
264 #if OSL_DEBUG_LEVEL > 0
267 if (!m_lInterceptionRegs
.empty() )
268 OSL_FAIL("There are some pending interceptor objects, which seems to be registered during (!) the destruction of a frame.");
271 #endif // ODL_DEBUG_LEVEL>0
273 #endif // FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
276 } // namespace framework
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */