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 <osl/diagnose.h>
24 #include <com/sun/star/task/InteractionHandler.hpp>
25 #include <com/sun/star/task/XInteractionAbort.hpp>
26 #include <com/sun/star/task/XInteractionRetry.hpp>
30 PreventDuplicateInteraction::PreventDuplicateInteraction(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
)
32 , m_xContext(rxContext
)
36 PreventDuplicateInteraction::~PreventDuplicateInteraction()
40 void PreventDuplicateInteraction::setHandler(const css::uno::Reference
< css::task::XInteractionHandler
>& xHandler
)
43 ::osl::ResettableMutexGuard
aLock(m_aLock
);
44 m_xHandler
= xHandler
;
49 void PreventDuplicateInteraction::useDefaultUUIHandler()
52 ::osl::ResettableMutexGuard
aLock(m_aLock
);
56 css::uno::Reference
< css::task::XInteractionHandler
> xHandler( css::task::InteractionHandler::createWithParent( m_xContext
, 0 ), css::uno::UNO_QUERY_THROW
);
60 m_xHandler
= xHandler
;
65 css::uno::Any SAL_CALL
PreventDuplicateInteraction::queryInterface( const css::uno::Type
& aType
)
66 throw (css::uno::RuntimeException
, std::exception
)
68 if ( aType
.equals( cppu::UnoType
<XInteractionHandler2
>::get() ) )
70 ::osl::ResettableMutexGuard
aLock(m_aLock
);
71 css::uno::Reference
< css::task::XInteractionHandler2
> xHandler( m_xHandler
, css::uno::UNO_QUERY
);
73 return css::uno::Any();
75 return ::cppu::WeakImplHelper1
< css::task::XInteractionHandler2
>::queryInterface( aType
);
78 void SAL_CALL
PreventDuplicateInteraction::handle(const css::uno::Reference
< css::task::XInteractionRequest
>& xRequest
)
79 throw(css::uno::RuntimeException
, std::exception
)
81 css::uno::Any aRequest
= xRequest
->getRequest();
82 bool bHandleIt
= true;
85 ::osl::ResettableMutexGuard
aLock(m_aLock
);
87 InteractionList::iterator pIt
;
88 for ( pIt
= m_lInteractionRules
.begin();
89 pIt
!= m_lInteractionRules
.end();
92 InteractionInfo
& rInfo
= *pIt
;
94 if (aRequest
.isExtractableTo(rInfo
.m_aInteraction
))
97 rInfo
.m_xRequest
= xRequest
;
98 bHandleIt
= (rInfo
.m_nCallCount
<= rInfo
.m_nMaxCount
);
103 css::uno::Reference
< css::task::XInteractionHandler
> xHandler
= m_xHandler
;
113 xHandler
->handle(xRequest
);
117 const css::uno::Sequence
< css::uno::Reference
< css::task::XInteractionContinuation
> > lContinuations
= xRequest
->getContinuations();
118 sal_Int32 c
= lContinuations
.getLength();
122 css::uno::Reference
< css::task::XInteractionAbort
> xAbort(lContinuations
[i
], css::uno::UNO_QUERY
);
132 sal_Bool SAL_CALL
PreventDuplicateInteraction::handleInteractionRequest( const css::uno::Reference
< css::task::XInteractionRequest
>& xRequest
)
133 throw (css::uno::RuntimeException
, std::exception
)
135 css::uno::Any aRequest
= xRequest
->getRequest();
136 bool bHandleIt
= true;
139 ::osl::ResettableMutexGuard
aLock(m_aLock
);
141 InteractionList::iterator pIt
;
142 for ( pIt
= m_lInteractionRules
.begin();
143 pIt
!= m_lInteractionRules
.end();
146 InteractionInfo
& rInfo
= *pIt
;
148 if (aRequest
.isExtractableTo(rInfo
.m_aInteraction
))
150 ++rInfo
.m_nCallCount
;
151 rInfo
.m_xRequest
= xRequest
;
152 bHandleIt
= (rInfo
.m_nCallCount
<= rInfo
.m_nMaxCount
);
157 css::uno::Reference
< css::task::XInteractionHandler2
> xHandler( m_xHandler
, css::uno::UNO_QUERY
);
158 OSL_ENSURE( xHandler
.is() || !m_xHandler
.is(),
159 "PreventDuplicateInteraction::handleInteractionRequest: inconsistency!" );
169 return xHandler
->handleInteractionRequest(xRequest
);
173 const css::uno::Sequence
< css::uno::Reference
< css::task::XInteractionContinuation
> > lContinuations
= xRequest
->getContinuations();
174 sal_Int32 c
= lContinuations
.getLength();
178 css::uno::Reference
< css::task::XInteractionAbort
> xAbort(lContinuations
[i
], css::uno::UNO_QUERY
);
189 void PreventDuplicateInteraction::addInteractionRule(const PreventDuplicateInteraction::InteractionInfo
& aInteractionInfo
)
192 ::osl::ResettableMutexGuard
aLock(m_aLock
);
194 InteractionList::iterator pIt
;
195 for ( pIt
= m_lInteractionRules
.begin();
196 pIt
!= m_lInteractionRules
.end();
199 InteractionInfo
& rInfo
= *pIt
;
200 if (rInfo
.m_aInteraction
== aInteractionInfo
.m_aInteraction
)
202 rInfo
.m_nMaxCount
= aInteractionInfo
.m_nMaxCount
;
203 rInfo
.m_nCallCount
= aInteractionInfo
.m_nCallCount
;
208 m_lInteractionRules
.push_back(aInteractionInfo
);
214 bool PreventDuplicateInteraction::getInteractionInfo(const css::uno::Type
& aInteraction
,
215 PreventDuplicateInteraction::InteractionInfo
* pReturn
) const
218 ::osl::ResettableMutexGuard
aLock(m_aLock
);
220 PreventDuplicateInteraction::InteractionList::const_iterator pIt
;
221 for ( pIt
= m_lInteractionRules
.begin();
222 pIt
!= m_lInteractionRules
.end();
225 const PreventDuplicateInteraction::InteractionInfo
& rInfo
= *pIt
;
226 if (rInfo
.m_aInteraction
== aInteraction
)
239 } // namespace framework
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */