1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __FRAMEWORK_INTERACTION_PREVENTDUPLICATEINTERACTION_HXX_
29 #define __FRAMEWORK_INTERACTION_PREVENTDUPLICATEINTERACTION_HXX_
30 
31 #include <framework/fwedllapi.h>
32 //_________________________________________________________________________________________________________________
33 //	my own includes
34 //_________________________________________________________________________________________________________________
35 
36 #include <vector>
37 
38 //_________________________________________________________________________________________________________________
39 //	interface includes
40 //_________________________________________________________________________________________________________________
41 #include <com/sun/star/task/XInteractionHandler2.hpp>
42 #include <com/sun/star/task/XInteractionRequest.hpp>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 
45 //_________________________________________________________________________________________________________________
46 //	other includes
47 //_________________________________________________________________________________________________________________
48 #include <cppuhelper/implbase1.hxx>
49 
50 //_________________________________________________________________________________________________________________
51 //	namespace
52 //_________________________________________________________________________________________________________________
53 
54 namespace framework{
55 
56 #ifdef css
57     #error "Conflict during define of namespace alias ..."
58 #else
59     #define css ::com::sun::star
60 #endif
61 
62 //_________________________________________________________________________________________________________________
63 //	exported const
64 //_________________________________________________________________________________________________________________
65 
66 //_________________________________________________________________________________________________________________
67 //	exported definitions
68 //_________________________________________________________________________________________________________________
69 
70 /**
71     @short      Prevent us from showing the same interaction more then once during
72                 the same transaction.
73 
74     @descr      Every interaction provided to this helper will be safed ... handled by the internal
75                 used UUIInteractionHandler (!) and never be handled a second time!
76 
77                 On the other side there exists some interactions, which allow a retry.
78                 So this helper allow to set a list of interactions combined with a retry value.
79  */
80 struct ThreadHelpBase2
81 {
82     public:
83         mutable ::osl::Mutex m_aLock;
84 };
85 
86 class FWE_DLLPUBLIC PreventDuplicateInteraction : private ThreadHelpBase2
87                                     ,public ::cppu::WeakImplHelper1< css::task::XInteractionHandler2 >
88 {
89     //_____________________________________
90     // structs, types etcp.
91     public:
92 
93         struct InteractionInfo
94         {
95             public:
96                 /// describe the interaction.
97                 css::uno::Type m_aInteraction;
98                 /// after max count was reached this interaction will be blocked.
99                 sal_Int32 m_nMaxCount;
100                 /// count how often this interaction was called.
101                 sal_Int32 m_nCallCount;
102                 /** hold the last intercepted request (matching the set interaction type) alive
103                 so it can be used for further checks */
104                 css::uno::Reference< css::task::XInteractionRequest > m_xRequest;
105 
106             public:
107 
108                 InteractionInfo(const css::uno::Type& aInteraction,
109                                       sal_Int32       nMaxCount   )
110                     : m_aInteraction(aInteraction)
111                     , m_nMaxCount   (nMaxCount   )
112                     , m_nCallCount  (0           )
113                 {}
114 
115                 InteractionInfo(const InteractionInfo& aCopy)
116                     : m_aInteraction(aCopy.m_aInteraction)
117                     , m_nMaxCount   (aCopy.m_nMaxCount   )
118                     , m_nCallCount  (aCopy.m_nCallCount  )
119                     , m_xRequest    (aCopy.m_xRequest    )
120                 {}
121         };
122 
123         typedef ::std::vector< InteractionInfo > InteractionList;
124 
125     //_____________________________________
126     // member
127     private:
128 
129         /// Used to create needed uno services at runtime.
130         css::uno::Reference< css::lang::XMultiServiceFactory > m_xSMGR;
131 
132         /** The outside interaction handler, which is used to handle every incoming interaction,
133             if it's not blocked. */
134         css::uno::Reference< css::task::XInteractionHandler > m_xHandler;
135 
136         /** This list describe which and how incoming interactions must be handled.
137             Further it contains all collected informations after this interaction
138             object was used.*/
139         InteractionList m_lInteractionRules;
140 
141     //_____________________________________
142     // uno interface
143     public:
144 
145         //_________________________________
146         /**
147             @interface  XInteractionHandler
148             @short      called from outside to handle a problem
149             @descr      We filter the incoming interactions. some of them
150                         will be forwarded to the generic UI interaction handler.
151                         So we must not implement it twice. Some other ones
152                         will be aborted only.
153 
154             @threadsafe yes
155         */
156         virtual void SAL_CALL handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
157             throw(css::uno::RuntimeException);
158 
159         //_________________________________
160         /**
161             @interface  XInteractionHandler2
162             @short      called from outside to handle a problem
163             @descr      We filter the incoming interactions. some of them
164                         will be forwarded to the generic UI interaction handler.
165                         So we must not implement it twice. Some other ones
166                         will be aborted only.
167 
168             @threadsafe yes
169         */
170         virtual ::sal_Bool SAL_CALL handleInteractionRequest( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest )
171             throw (::com::sun::star::uno::RuntimeException);
172 
173         //_________________________________
174         /**
175             @interface  XInterface
176             @short      called to query another interface of the component
177             @descr      Will allow to query for XInteractionHandler2 if and only if m_xHandler supports this interface, too.
178 
179             @threadsafe yes
180         */
181         virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type& aType )
182             throw (::com::sun::star::uno::RuntimeException);
183     //_____________________________________
184     // c++ interface
185     public:
186 
187         //_________________________________
188         /**
189             @short      ctor to guarantee right initialized instances of this class
190             @descr      It uses the given uno service manager to create the global
191                         generic UI interaction handler for later internal using.
192 
193             @param      xSMGR
194                             uno service manager for creating services internaly
195 
196             @threadsafe not neccessary
197         */
198         PreventDuplicateInteraction(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR);
199 
200         //_________________________________
201         /**
202             @short      dtor to free used memory.
203          */
204         virtual ~PreventDuplicateInteraction();
205 
206         //_________________________________
207         /**
208             @short      set the outside interaction handler, which must be used internaly
209                         if the interaction will not be blocked by the set list of rules.
210 
211             @note       This overwrites the settings of e.g. useDefaultUUIHandler()!
212 
213             @param      xHandler
214                         the new interaction handler
215          */
216         virtual void setHandler(const css::uno::Reference< css::task::XInteractionHandler >& xHandler);
217 
218         //_________________________________
219         /**
220             @short      instead of setting an outside interaction handler, this method
221                         make sure the default UUI interaction handler of the office is used.
222 
223             @note       This overwrites the settings of e.g. setHandler()!
224          */
225         virtual void useDefaultUUIHandler();
226 
227         //_________________________________
228         /**
229             @short      add a new interaction to the list of interactions, which
230                         must be handled by this helper.
231 
232             @descr      This method must be called immediatly after a new instance of this helper was
233                         created. Without such list of InteractionRules, this instances does nothing!
234                         On the other side there is no possibility to remove rules.
235                         So the same instance cant be used within different transactions.
236                         It's a OneWay-object .-)
237 
238             @param      aInteractionInfo
239                         describe the type of interaction, hos often it can be called etcpp.
240 
241             @threadsafe yes
242         */
243         virtual void addInteractionRule(const PreventDuplicateInteraction::InteractionInfo& aInteractionInfo);
244 
245         //_________________________________
246         /**
247             @short      return the info struct for the specified interaction.
248 
249             @param      aInteraction
250                         specify the interaction.
251 
252             @param      pReturn
253                         provides informations about:
254                         - the count how often this interaction was handled during the
255                           lifetime of this helper.
256                         - the interaction itself, so it can be analyzed further
257 
258             @return     [boolean]
259                         sal_True if the queried interaction could be found.
260                         sal_False otherwise.
261 
262             @threadsafe yes
263         */
264         virtual sal_Bool getInteractionInfo(const css::uno::Type&                               aInteraction,
265                                                   PreventDuplicateInteraction::InteractionInfo* pReturn     ) const;
266 };
267 
268 #undef css
269 
270 } // namespace framework
271 
272 #endif // #ifndef __FRAMEWORK_INTERACTION_PREVENTDUPLICATEINTERACTION_HXX_
273