xref: /AOO41X/main/framework/source/fwe/interaction/preventduplicateinteraction.cxx (revision 6d739b60ff8f4ed2134ae1442e284f9da90334b4)
1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "framework/preventduplicateinteraction.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir //_________________________________________________________________________________________________________________
30cdf0e10cSrcweir //	my own includes
31cdf0e10cSrcweir //_________________________________________________________________________________________________________________
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //_________________________________________________________________________________________________________________
34cdf0e10cSrcweir //	interface includes
35cdf0e10cSrcweir //_________________________________________________________________________________________________________________
36cdf0e10cSrcweir #include <com/sun/star/task/XInteractionAbort.hpp>
37cdf0e10cSrcweir #include <com/sun/star/task/XInteractionRetry.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //_________________________________________________________________________________________________________________
40cdf0e10cSrcweir //	other includes
41cdf0e10cSrcweir //_________________________________________________________________________________________________________________
42cdf0e10cSrcweir 
43cdf0e10cSrcweir //_________________________________________________________________________________________________________________
44cdf0e10cSrcweir //	namespace
45cdf0e10cSrcweir //_________________________________________________________________________________________________________________
46cdf0e10cSrcweir 
47cdf0e10cSrcweir namespace framework{
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace css = ::com::sun::star;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir //_________________________________________________________________________________________________________________
52cdf0e10cSrcweir //	exported const
53cdf0e10cSrcweir //_________________________________________________________________________________________________________________
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #define IMPLEMENTATIONNAME_UIINTERACTIONHANDLER                 ::rtl::OUString::createFromAscii("com.sun.star.comp.uui.UUIInteractionHandler")
56cdf0e10cSrcweir 
57cdf0e10cSrcweir //_________________________________________________________________________________________________________________
58cdf0e10cSrcweir //	exported definitions
59cdf0e10cSrcweir //_________________________________________________________________________________________________________________
60cdf0e10cSrcweir 
61cdf0e10cSrcweir //_________________________________________________________________________________________________________________
62cdf0e10cSrcweir 
PreventDuplicateInteraction(const css::uno::Reference<css::lang::XMultiServiceFactory> & xSMGR)63cdf0e10cSrcweir PreventDuplicateInteraction::PreventDuplicateInteraction(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
64cdf0e10cSrcweir     : ThreadHelpBase2()
65cdf0e10cSrcweir     , m_xSMGR(xSMGR)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir //_________________________________________________________________________________________________________________
70cdf0e10cSrcweir 
~PreventDuplicateInteraction()71cdf0e10cSrcweir PreventDuplicateInteraction::~PreventDuplicateInteraction()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir //_________________________________________________________________________________________________________________
76cdf0e10cSrcweir 
setHandler(const css::uno::Reference<css::task::XInteractionHandler> & xHandler)77cdf0e10cSrcweir void PreventDuplicateInteraction::setHandler(const css::uno::Reference< css::task::XInteractionHandler >& xHandler)
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     // SAFE ->
80cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
81cdf0e10cSrcweir     m_xHandler = xHandler;
82cdf0e10cSrcweir     aLock.clear();
83cdf0e10cSrcweir     // <- SAFE
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir //_________________________________________________________________________________________________________________
87cdf0e10cSrcweir 
useDefaultUUIHandler()88cdf0e10cSrcweir void PreventDuplicateInteraction::useDefaultUUIHandler()
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     // SAFE ->
91cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
92cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
93cdf0e10cSrcweir     aLock.clear();
94cdf0e10cSrcweir     // <- SAFE
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     css::uno::Reference< css::task::XInteractionHandler > xHandler(
97cdf0e10cSrcweir                             xSMGR->createInstance(IMPLEMENTATIONNAME_UIINTERACTIONHANDLER),
98cdf0e10cSrcweir                             css::uno::UNO_QUERY_THROW);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     // SAFE ->
101cdf0e10cSrcweir     aLock.reset();
102cdf0e10cSrcweir     m_xHandler = xHandler;
103cdf0e10cSrcweir     aLock.clear();
104cdf0e10cSrcweir     // <- SAFE
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir //_________________________________________________________________________________________________________________
queryInterface(const css::uno::Type & aType)108cdf0e10cSrcweir css::uno::Any SAL_CALL PreventDuplicateInteraction::queryInterface( const css::uno::Type& aType )
109cdf0e10cSrcweir     throw (css::uno::RuntimeException)
110cdf0e10cSrcweir {
111cdf0e10cSrcweir     if ( aType.equals( XInteractionHandler2::static_type() ) )
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         ::osl::ResettableMutexGuard aLock(m_aLock);
114cdf0e10cSrcweir         css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY );
115cdf0e10cSrcweir         if ( !xHandler.is() )
116cdf0e10cSrcweir             return css::uno::Any();
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir     return ::cppu::WeakImplHelper1< css::task::XInteractionHandler2 >::queryInterface( aType );
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir //_________________________________________________________________________________________________________________
122cdf0e10cSrcweir 
handle(const css::uno::Reference<css::task::XInteractionRequest> & xRequest)123cdf0e10cSrcweir void SAL_CALL PreventDuplicateInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
124cdf0e10cSrcweir     throw(css::uno::RuntimeException)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir     css::uno::Any aRequest  = xRequest->getRequest();
127cdf0e10cSrcweir     sal_Bool      bHandleIt = sal_True;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     // SAFE ->
130cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     InteractionList::iterator pIt;
133cdf0e10cSrcweir     for (  pIt  = m_lInteractionRules.begin();
134cdf0e10cSrcweir            pIt != m_lInteractionRules.end()  ;
135cdf0e10cSrcweir          ++pIt                               )
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         InteractionInfo& rInfo = *pIt;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         if (aRequest.isExtractableTo(rInfo.m_aInteraction))
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             ++rInfo.m_nCallCount;
142cdf0e10cSrcweir             rInfo.m_xRequest = xRequest;
143cdf0e10cSrcweir             bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount);
144cdf0e10cSrcweir             break;
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     css::uno::Reference< css::task::XInteractionHandler > xHandler = m_xHandler;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     aLock.clear();
151cdf0e10cSrcweir     // <- SAFE
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     if (
154cdf0e10cSrcweir         (bHandleIt    ) &&
155cdf0e10cSrcweir         (xHandler.is())
156cdf0e10cSrcweir        )
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         xHandler->handle(xRequest);
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir     else
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
163cdf0e10cSrcweir         sal_Int32 c = lContinuations.getLength();
164cdf0e10cSrcweir         sal_Int32 i = 0;
165cdf0e10cSrcweir         for (i=0; i<c; ++i)
166cdf0e10cSrcweir         {
167cdf0e10cSrcweir             css::uno::Reference< css::task::XInteractionAbort > xAbort(lContinuations[i], css::uno::UNO_QUERY);
168cdf0e10cSrcweir             if (xAbort.is())
169cdf0e10cSrcweir             {
170cdf0e10cSrcweir                 xAbort->select();
171cdf0e10cSrcweir                 break;
172cdf0e10cSrcweir             }
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir //_________________________________________________________________________________________________________________
178cdf0e10cSrcweir 
handleInteractionRequest(const css::uno::Reference<css::task::XInteractionRequest> & xRequest)179cdf0e10cSrcweir ::sal_Bool SAL_CALL PreventDuplicateInteraction::handleInteractionRequest( const css::uno::Reference< css::task::XInteractionRequest >& xRequest )
180cdf0e10cSrcweir             throw (css::uno::RuntimeException)
181cdf0e10cSrcweir {
182cdf0e10cSrcweir     css::uno::Any aRequest  = xRequest->getRequest();
183cdf0e10cSrcweir     sal_Bool      bHandleIt = sal_True;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     // SAFE ->
186cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     InteractionList::iterator pIt;
189cdf0e10cSrcweir     for (  pIt  = m_lInteractionRules.begin();
190cdf0e10cSrcweir            pIt != m_lInteractionRules.end()  ;
191cdf0e10cSrcweir          ++pIt                               )
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir         InteractionInfo& rInfo = *pIt;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         if (aRequest.isExtractableTo(rInfo.m_aInteraction))
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             ++rInfo.m_nCallCount;
198cdf0e10cSrcweir             rInfo.m_xRequest = xRequest;
199cdf0e10cSrcweir             bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount);
200cdf0e10cSrcweir             break;
201cdf0e10cSrcweir         }
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY );
205cdf0e10cSrcweir     OSL_ENSURE( xHandler.is() || !m_xHandler.is(),
206cdf0e10cSrcweir         "PreventDuplicateInteraction::handleInteractionRequest: inconsistency!" );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     aLock.clear();
209cdf0e10cSrcweir     // <- SAFE
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     if (
212cdf0e10cSrcweir         (bHandleIt    ) &&
213cdf0e10cSrcweir         (xHandler.is())
214cdf0e10cSrcweir        )
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         return xHandler->handleInteractionRequest(xRequest);
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir     else
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
221cdf0e10cSrcweir         sal_Int32 c = lContinuations.getLength();
222cdf0e10cSrcweir         sal_Int32 i = 0;
223cdf0e10cSrcweir         for (i=0; i<c; ++i)
224cdf0e10cSrcweir         {
225cdf0e10cSrcweir             css::uno::Reference< css::task::XInteractionAbort > xAbort(lContinuations[i], css::uno::UNO_QUERY);
226cdf0e10cSrcweir             if (xAbort.is())
227cdf0e10cSrcweir             {
228cdf0e10cSrcweir                 xAbort->select();
229cdf0e10cSrcweir                 break;
230cdf0e10cSrcweir             }
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir     return false;
234cdf0e10cSrcweir }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir //_________________________________________________________________________________________________________________
237cdf0e10cSrcweir 
addInteractionRule(const PreventDuplicateInteraction::InteractionInfo & aInteractionInfo)238cdf0e10cSrcweir void PreventDuplicateInteraction::addInteractionRule(const PreventDuplicateInteraction::InteractionInfo& aInteractionInfo)
239cdf0e10cSrcweir {
240cdf0e10cSrcweir     // SAFE ->
241cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     InteractionList::iterator pIt;
244cdf0e10cSrcweir     for (  pIt  = m_lInteractionRules.begin();
245cdf0e10cSrcweir            pIt != m_lInteractionRules.end()  ;
246cdf0e10cSrcweir          ++pIt                               )
247cdf0e10cSrcweir     {
248cdf0e10cSrcweir         InteractionInfo& rInfo = *pIt;
249cdf0e10cSrcweir         if (rInfo.m_aInteraction == aInteractionInfo.m_aInteraction)
250cdf0e10cSrcweir         {
251cdf0e10cSrcweir             rInfo.m_nMaxCount  = aInteractionInfo.m_nMaxCount ;
252cdf0e10cSrcweir             rInfo.m_nCallCount = aInteractionInfo.m_nCallCount;
253cdf0e10cSrcweir             return;
254cdf0e10cSrcweir         }
255cdf0e10cSrcweir     }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     m_lInteractionRules.push_back(aInteractionInfo);
258cdf0e10cSrcweir 
259cdf0e10cSrcweir     aLock.clear();
260cdf0e10cSrcweir     // <- SAFE
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir //_________________________________________________________________________________________________________________
264cdf0e10cSrcweir 
getInteractionInfo(const css::uno::Type & aInteraction,PreventDuplicateInteraction::InteractionInfo * pReturn) const265cdf0e10cSrcweir sal_Bool PreventDuplicateInteraction::getInteractionInfo(const css::uno::Type&                               aInteraction,
266cdf0e10cSrcweir                                                                PreventDuplicateInteraction::InteractionInfo* pReturn     ) const
267cdf0e10cSrcweir {
268cdf0e10cSrcweir     // SAFE ->
269cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     PreventDuplicateInteraction::InteractionList::const_iterator pIt;
272cdf0e10cSrcweir     for (  pIt  = m_lInteractionRules.begin();
273cdf0e10cSrcweir            pIt != m_lInteractionRules.end()  ;
274cdf0e10cSrcweir          ++pIt                               )
275cdf0e10cSrcweir     {
276cdf0e10cSrcweir         const PreventDuplicateInteraction::InteractionInfo& rInfo = *pIt;
277cdf0e10cSrcweir         if (rInfo.m_aInteraction == aInteraction)
278cdf0e10cSrcweir         {
279cdf0e10cSrcweir             *pReturn = rInfo;
280cdf0e10cSrcweir             return sal_True;
281cdf0e10cSrcweir         }
282cdf0e10cSrcweir     }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir     aLock.clear();
285cdf0e10cSrcweir     // <- SAFE
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     return sal_False;
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir } // namespace framework
291