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