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 "framework/preventduplicateinteraction.hxx"
22 #include <com/sun/star/task/InteractionHandler.hpp>
23 #include <com/sun/star/task/XInteractionAbort.hpp>
24 #include <com/sun/star/task/XInteractionRetry.hpp>
28 PreventDuplicateInteraction::PreventDuplicateInteraction(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
)
30 , m_xContext(rxContext
)
34 //_________________________________________________________________________________________________________________
36 PreventDuplicateInteraction::~PreventDuplicateInteraction()
40 //_________________________________________________________________________________________________________________
42 void PreventDuplicateInteraction::setHandler(const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
)
45 ::osl::ResettableMutexGuard
aLock(m_aLock
);
46 m_xHandler
= xHandler
;
51 //_________________________________________________________________________________________________________________
53 void PreventDuplicateInteraction::useDefaultUUIHandler()
56 ::osl::ResettableMutexGuard
aLock(m_aLock
);
60 css::uno::Reference
< css::task::XInteractionHandler
> xHandler( css::task::InteractionHandler::createWithParent( m_xContext
, 0 ), css::uno::UNO_QUERY_THROW
);
64 m_xHandler
= xHandler
;
69 //_________________________________________________________________________________________________________________
70 css::uno::Any SAL_CALL
PreventDuplicateInteraction::queryInterface( const css::uno::Type
& aType
)
71 throw (css::uno::RuntimeException
)
73 if ( aType
.equals( XInteractionHandler2::static_type() ) )
75 ::osl::ResettableMutexGuard
aLock(m_aLock
);
76 css::uno::Reference
< css::task::XInteractionHandler2
> xHandler( m_xHandler
, css::uno::UNO_QUERY
);
78 return css::uno::Any();
80 return ::cppu::WeakImplHelper1
< css::task::XInteractionHandler2
>::queryInterface( aType
);
83 //_________________________________________________________________________________________________________________
85 void SAL_CALL
PreventDuplicateInteraction::handle(const css::uno::Reference
< css::task::XInteractionRequest
>& xRequest
)
86 throw(css::uno::RuntimeException
)
88 css::uno::Any aRequest
= xRequest
->getRequest();
89 sal_Bool bHandleIt
= sal_True
;
92 ::osl::ResettableMutexGuard
aLock(m_aLock
);
94 InteractionList::iterator pIt
;
95 for ( pIt
= m_lInteractionRules
.begin();
96 pIt
!= m_lInteractionRules
.end() ;
99 InteractionInfo
& rInfo
= *pIt
;
101 if (aRequest
.isExtractableTo(rInfo
.m_aInteraction
))
103 ++rInfo
.m_nCallCount
;
104 rInfo
.m_xRequest
= xRequest
;
105 bHandleIt
= (rInfo
.m_nCallCount
<= rInfo
.m_nMaxCount
);
110 css::uno::Reference
< css::task::XInteractionHandler
> xHandler
= m_xHandler
;
120 xHandler
->handle(xRequest
);
124 const css::uno::Sequence
< css::uno::Reference
< css::task::XInteractionContinuation
> > lContinuations
= xRequest
->getContinuations();
125 sal_Int32 c
= lContinuations
.getLength();
129 css::uno::Reference
< css::task::XInteractionAbort
> xAbort(lContinuations
[i
], css::uno::UNO_QUERY
);
139 //_________________________________________________________________________________________________________________
141 ::sal_Bool SAL_CALL
PreventDuplicateInteraction::handleInteractionRequest( const css::uno::Reference
< css::task::XInteractionRequest
>& xRequest
)
142 throw (css::uno::RuntimeException
)
144 css::uno::Any aRequest
= xRequest
->getRequest();
145 sal_Bool bHandleIt
= sal_True
;
148 ::osl::ResettableMutexGuard
aLock(m_aLock
);
150 InteractionList::iterator pIt
;
151 for ( pIt
= m_lInteractionRules
.begin();
152 pIt
!= m_lInteractionRules
.end() ;
155 InteractionInfo
& rInfo
= *pIt
;
157 if (aRequest
.isExtractableTo(rInfo
.m_aInteraction
))
159 ++rInfo
.m_nCallCount
;
160 rInfo
.m_xRequest
= xRequest
;
161 bHandleIt
= (rInfo
.m_nCallCount
<= rInfo
.m_nMaxCount
);
166 css::uno::Reference
< css::task::XInteractionHandler2
> xHandler( m_xHandler
, css::uno::UNO_QUERY
);
167 OSL_ENSURE( xHandler
.is() || !m_xHandler
.is(),
168 "PreventDuplicateInteraction::handleInteractionRequest: inconsistency!" );
178 return xHandler
->handleInteractionRequest(xRequest
);
182 const css::uno::Sequence
< css::uno::Reference
< css::task::XInteractionContinuation
> > lContinuations
= xRequest
->getContinuations();
183 sal_Int32 c
= lContinuations
.getLength();
187 css::uno::Reference
< css::task::XInteractionAbort
> xAbort(lContinuations
[i
], css::uno::UNO_QUERY
);
198 //_________________________________________________________________________________________________________________
200 void PreventDuplicateInteraction::addInteractionRule(const PreventDuplicateInteraction::InteractionInfo
& aInteractionInfo
)
203 ::osl::ResettableMutexGuard
aLock(m_aLock
);
205 InteractionList::iterator pIt
;
206 for ( pIt
= m_lInteractionRules
.begin();
207 pIt
!= m_lInteractionRules
.end() ;
210 InteractionInfo
& rInfo
= *pIt
;
211 if (rInfo
.m_aInteraction
== aInteractionInfo
.m_aInteraction
)
213 rInfo
.m_nMaxCount
= aInteractionInfo
.m_nMaxCount
;
214 rInfo
.m_nCallCount
= aInteractionInfo
.m_nCallCount
;
219 m_lInteractionRules
.push_back(aInteractionInfo
);
225 //_________________________________________________________________________________________________________________
227 sal_Bool
PreventDuplicateInteraction::getInteractionInfo(const css::uno::Type
& aInteraction
,
228 PreventDuplicateInteraction::InteractionInfo
* pReturn
) const
231 ::osl::ResettableMutexGuard
aLock(m_aLock
);
233 PreventDuplicateInteraction::InteractionList::const_iterator pIt
;
234 for ( pIt
= m_lInteractionRules
.begin();
235 pIt
!= m_lInteractionRules
.end() ;
238 const PreventDuplicateInteraction::InteractionInfo
& rInfo
= *pIt
;
239 if (rInfo
.m_aInteraction
== aInteraction
)
252 } // namespace framework
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */