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 FPICKER_WIN32_VISTA_ASYNCREQUESTS_HXX
25 #define FPICKER_WIN32_VISTA_ASYNCREQUESTS_HXX
26 
27 //-----------------------------------------------------------------------------
28 // includes
29 //-----------------------------------------------------------------------------
30 
31 #include <cppuhelper/basemutex.hxx>
32 #include <comphelper/sequenceashashmap.hxx>
33 #include <osl/conditn.hxx>
34 #include <osl/thread.hxx>
35 #include <osl/time.h>
36 #include <queue>
37 #include <boost/shared_ptr.hpp>
38 
39 //-----------------------------------------------------------------------------
40 // namespace
41 //-----------------------------------------------------------------------------
42 
43 #ifdef css
44     #error "Clash on using CSS as namespace define."
45 #else
46     #define css ::com::sun::star
47 #endif
48 
49 namespace fpicker{
50 namespace win32{
51 namespace vista{
52 
53 //-----------------------------------------------------------------------------
54 /** @todo document me
55  */
56 class Request
57 {
58     //-------------------------------------------------------------------------
59     public:
60 
61         static const ::sal_Int32 WAIT_INFINITE = 0;
62 
63     //-------------------------------------------------------------------------
64     // interface
65     //-------------------------------------------------------------------------
66 
67     public:
68 
69         //---------------------------------------------------------------------
Request()70         explicit Request()
71             : m_aJoiner   (  )
72             , m_nRequest  (-1)
73             , m_lArguments(  )
74         {
75             m_aJoiner.reset();
76         }
77 
78         //---------------------------------------------------------------------
~Request()79         virtual ~Request() {};
80 
81         //---------------------------------------------------------------------
setRequest(::sal_Int32 nRequest)82         void setRequest(::sal_Int32 nRequest)
83         {
84             m_nRequest = nRequest;
85         }
86 
87         //---------------------------------------------------------------------
getRequest()88         ::sal_Int32 getRequest()
89         {
90             return m_nRequest;
91         }
92 
93         //---------------------------------------------------------------------
clearArguments()94         void clearArguments()
95         {
96             m_lArguments.clear();
97         }
98 
99         //---------------------------------------------------------------------
100         template< class TArgumentType >
setArgument(const::rtl::OUString & sName,const TArgumentType & aValue)101         void setArgument(const ::rtl::OUString& sName ,
102                          const TArgumentType&   aValue)
103         {
104             m_lArguments[sName] <<= aValue;
105         }
106 
107         //---------------------------------------------------------------------
108         template< class TArgumentType >
getArgumentOrDefault(const::rtl::OUString & sName,const TArgumentType & aDefault)109         TArgumentType getArgumentOrDefault(const ::rtl::OUString& sName   ,
110                                            const TArgumentType&   aDefault)
111         {
112             return m_lArguments.getUnpackedValueOrDefault(sName, aDefault);
113         }
114 
115         //---------------------------------------------------------------------
116         void wait(::sal_Int32 nMilliSeconds = WAIT_INFINITE);
117 
118 		void waitProcessMessages();
119 
120         //---------------------------------------------------------------------
121         void notify();
122 
123     //-------------------------------------------------------------------------
124     // member
125     //-------------------------------------------------------------------------
126 
127     private:
128 
129         ::osl::Condition m_aJoiner;
130         ::sal_Int32 m_nRequest;
131         ::comphelper::SequenceAsHashMap m_lArguments;
132 };
133 
134 typedef ::boost::shared_ptr< Request > RequestRef;
135 typedef ::std::queue< RequestRef >  RequestQueue;
136 
137 //-----------------------------------------------------------------------------
138 class RequestHandler
139 {
140     public:
141         virtual void before() = 0;
142         virtual void doRequest(const RequestRef& rRequest) = 0;
143         virtual void after() = 0;
144 };
145 
146 typedef ::boost::shared_ptr< RequestHandler > RequestHandlerRef;
147 
148 //-----------------------------------------------------------------------------
149 /** @todo docuemnt me
150  */
151 class AsyncRequests : private ::cppu::BaseMutex
152                     , public  ::osl::Thread
153 {
154     public:
155 		static const ::sal_Int16  PROCESS_MESSAGES = 2;
156         static const ::sal_Int16  BLOCKED       = 1;
157         static const ::sal_Int16  NON_BLOCKED   = 0;
158 
159         //---------------------------------------------------------------------
160         /** creates the new asynchronous request executor.
161          */
162         explicit AsyncRequests(const RequestHandlerRef& rHandler);
163 
setHandler(const RequestHandlerRef & rHandler)164         void setHandler(const RequestHandlerRef& rHandler)
165         {
166             m_rHandler = rHandler;
167         }
168 
169         //---------------------------------------------------------------------
170         /** does nothing special / excepting to make sure our class wont be inline .-)
171          */
172         virtual ~AsyncRequests();
173 
174 		//---------------------------------------------------------------------
175         /** @todo document me
176          */
177 		void triggerRequestProcessMessages (const RequestRef& rRequest);
178 
179         //---------------------------------------------------------------------
180         /** @todo document me
181          */
182         void triggerRequestBlocked(const RequestRef& rRequest);
183 
184         //---------------------------------------------------------------------
185         /** @todo document me
186          */
187         void triggerRequestNonBlocked(const RequestRef& rRequest);
188 
189         //---------------------------------------------------------------------
190         /** @todo document me
191          */
192         void triggerRequestDirectly(const RequestRef& rRequest);
193 
194         //---------------------------------------------------------------------
195         /** @todo document me
196          */
197         void triggerRequestThreadAware(const RequestRef& rRequest,
198                                              ::sal_Int16  nWait   );
199 
200     private:
201 
202         //---------------------------------------------------------------------
203         /** our STA .-)
204          *  Will run between start() & finish(). Internaly it runs a loop ...
205          *  waiting for requests. Every request will be executed synchronously
206          *  in blocked mode.
207          */
208         virtual void SAL_CALL run();
209 
210     private:
211 
212         ::sal_Bool m_bFinish;
213         RequestHandlerRef m_rHandler;
214         RequestQueue m_lRequests;
215 };
216 
217 } // namespace vista
218 } // namespace win32
219 } // namespace fpicker
220 
221 #undef css
222 
223 #endif // FPICKER_WIN32_VISTA_ASYNCREQUESTS_HXX
224