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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 //_________________________________________________________________________________________________________________
27 //	my own includes
28 //_________________________________________________________________________________________________________________
29 
30 #ifndef __FRAMEWORK_DISPATCH_OXT_HANDLER_HXX_
31 #include <dispatch/oxt_handler.hxx>
32 #endif
33 
34 #ifndef __FRAMEWORK_THREADHELP_TRANSACTIONGUARD_HXX_
35 #include <threadhelp/transactionguard.hxx>
36 #endif
37 
38 #ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
39 #include <threadhelp/writeguard.hxx>
40 #endif
41 
42 #ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_
43 #include <threadhelp/readguard.hxx>
44 #endif
45 
46 #ifndef __FRAMEWORK_MACROS_DEBUG_HXX_
47 #include <macros/debug.hxx>
48 #endif
49 
50 #ifndef __FRAMEWORK_SERVICES_H_
51 #include <services.h>
52 #endif
53 
54 #ifndef __COMPHELPER_MEDIADESCRIPTOR_HXX_
55 #include <comphelper/mediadescriptor.hxx>
56 #endif
57 
58 //_________________________________________________________________________________________________________________
59 //	interface includes
60 //_________________________________________________________________________________________________________________
61 
62 #include <com/sun/star/io/XInputStream.hpp>
63 #include <com/sun/star/frame/DispatchResultState.hpp>
64 #include <com/sun/star/task/XJobExecutor.hpp>
65 
66 //_________________________________________________________________________________________________________________
67 //	includes of other projects
68 //_________________________________________________________________________________________________________________
69 
70 #ifndef _COMPHELPER_SEQUENCEASHASHMAP_HXX_
71 #include <comphelper/sequenceashashmap.hxx>
72 #endif
73 
74 #ifndef _RTL_USTRBUF_HXX_
75 #include <rtl/ustrbuf.hxx>
76 #endif
77 
78 //_________________________________________________________________________________________________________________
79 //	namespace
80 //_________________________________________________________________________________________________________________
81 
82 namespace framework{
83 
84 //_________________________________________________________________________________________________________________
85 //	non exported const
86 //_________________________________________________________________________________________________________________
87 
88 //_________________________________________________________________________________________________________________
89 //	non exported definitions
90 //_________________________________________________________________________________________________________________
91 
92 #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
93 
94 //_________________________________________________________________________________________________________________
95 //	declarations
96 //_________________________________________________________________________________________________________________
97 
98 //*****************************************************************************************************************
99 //  XInterface, XTypeProvider, XServiceInfo
100 //*****************************************************************************************************************
DEFINE_XINTERFACE_5(Oxt_Handler,OWeakObject,DIRECT_INTERFACE (css::lang::XTypeProvider),DIRECT_INTERFACE (css::lang::XServiceInfo),DIRECT_INTERFACE (css::frame::XNotifyingDispatch),DIRECT_INTERFACE (css::frame::XDispatch),DIRECT_INTERFACE (css::document::XExtendedFilterDetection))101 DEFINE_XINTERFACE_5                 (   Oxt_Handler                                                ,
102                                         OWeakObject                                                ,
103                                         DIRECT_INTERFACE( css::lang::XTypeProvider                ),
104                                         DIRECT_INTERFACE( css::lang::XServiceInfo                 ),
105                                         DIRECT_INTERFACE( css::frame::XNotifyingDispatch          ),
106                                         DIRECT_INTERFACE( css::frame::XDispatch                   ),
107                                         DIRECT_INTERFACE( css::document::XExtendedFilterDetection )
108                                     )
109 
110 DEFINE_XTYPEPROVIDER_5              (   Oxt_Handler                                                ,
111                                         css::lang::XTypeProvider                                   ,
112                                         css::lang::XServiceInfo                                    ,
113                                         css::frame::XNotifyingDispatch                             ,
114                                         css::frame::XDispatch                                      ,
115                                         css::document::XExtendedFilterDetection
116                                     )
117 
118 DEFINE_XSERVICEINFO_MULTISERVICE    (   Oxt_Handler                                                ,
119                                         ::cppu::OWeakObject                                        ,
120                                         SERVICENAME_CONTENTHANDLER                                 ,
121                                         IMPLEMENTATIONNAME_OXT_HANDLER
122 									)
123 
124 DEFINE_INIT_SERVICE                 (   Oxt_Handler,
125                                         {
126                                         }
127                                     )
128 
129 /*-************************************************************************************************************//**
130     @short      standard ctor
131     @descr      These initialize a new instance of this class with needed informations for work.
132 
133     @seealso    using at owner
134 
135     @param      "xFactory", reference to service manager for creation of new services
136     @return     -
137 
138     @onerror    Show an assertion and do nothing else.
139     @threadsafe yes
140 *//*-*************************************************************************************************************/
141 Oxt_Handler::Oxt_Handler( const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory )
142 		//	Init baseclasses first
143         :   ThreadHelpBase      (          )
144         ,   ::cppu::OWeakObject (          )
145         // Init member
146         ,   m_xFactory          ( xFactory )
147 {
148 }
149 
150 /*-************************************************************************************************************//**
151     @short      standard dtor
152     @descr      -
153 
154     @seealso    -
155 
156     @param      -
157     @return     -
158 
159     @onerror    -
160     @threadsafe -
161 *//*-*************************************************************************************************************/
~Oxt_Handler()162 Oxt_Handler::~Oxt_Handler()
163 {
164     if ( m_xListener.is() )
165     {
166         css::frame::DispatchResultEvent aEvent;
167         aEvent.State = css::frame::DispatchResultState::FAILURE;
168         m_xListener->dispatchFinished( aEvent );
169         m_xListener = css::uno::Reference< css::frame::XDispatchResultListener >();
170     }
171 }
172 
173 /*-************************************************************************************************************//**
174     @interface  ::com::sun::star::frame::XDispatch
175 
176     @short      try to load audio file
177     @descr      This method try to load given audio file by URL and play it. We use vcl/Sound class to do that.
178                 Playing of sound is asynchron every time.
179 
180     @attention  We must hold us alive by ourself ... because we use async. vcl sound player ... but playing is started
181                 in async interface call "dispatch()" too. And caller forget us imediatly. But then our uno ref count
182                 will decreased to 0 and will die. The only solution is to use own reference to our implementation.
183                 But we do it for really started jobs only and release it during call back of vcl.
184 
185     @seealso    class vcl/Sound
186     @seealso    method implts_PlayerNotify()
187 
188     @param      "aURL"      , URL to dispatch.
189     @param      "lArguments", list of optional arguments.
190     @return     -
191 
192     @onerror    We do nothing.
193     @threadsafe yes
194 *//*-*************************************************************************************************************/
dispatchWithNotification(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> &,const css::uno::Reference<css::frame::XDispatchResultListener> & xListener)195 void SAL_CALL Oxt_Handler::dispatchWithNotification( const css::util::URL& aURL,
196                                                      const css::uno::Sequence< css::beans::PropertyValue >&            /*lArguments*/,
197                                                      const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
198     throw( css::uno::RuntimeException )
199 {
200     // SAFE {
201     ResetableGuard aLock( m_aLock );
202 
203     rtl::OUString sServiceName = UNISTRING( "com.sun.star.deployment.ui.PackageManagerDialog" );
204     css::uno::Sequence< css::uno::Any > lParams(1);
205     lParams[0] <<= aURL.Main;
206 
207     css::uno::Reference< css::uno::XInterface > xService;
208 
209     xService = m_xFactory->createInstanceWithArguments( sServiceName, lParams );
210     css::uno::Reference< css::task::XJobExecutor > xExecuteable( xService, css::uno::UNO_QUERY );
211     if ( xExecuteable.is() )
212         xExecuteable->trigger( rtl::OUString() );
213 
214     if ( xListener.is() )
215     {
216         css::frame::DispatchResultEvent aEvent;
217         aEvent.State = css::frame::DispatchResultState::SUCCESS;
218         xListener->dispatchFinished( aEvent );
219     }
220 
221 /*
222     // Try to initialize player.
223     m_xListener = xListener;
224     if (m_aPlayer.SetSoundName(aURL.Complete))
225     {
226         // OK- we can start async playing ...
227         // Count this request and initialize self-holder against dying by uno ref count ...
228         m_xSelfHold = css::uno::Reference< css::uno::XInterface >(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
229         m_aPlayer.Play();
230     }
231 */
232     // } SAFE
233     aLock.unlock();
234 }
235 
dispatch(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments)236 void SAL_CALL Oxt_Handler::dispatch( const css::util::URL&                                  aURL       ,
237                                      const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
238     throw( css::uno::RuntimeException )
239 {
240     dispatchWithNotification( aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >() );
241 }
242 
243 /*-************************************************************************************************************//**
244     @interface  ::com::sun::star::document::XExtendedFilterDetection
245 
246     @short      try to detect file (given as argument included in "lDescriptor")
247     @descr      We try to detect, if given file could be handled by this class and is a well known one.
248                 If it is - we return right internal type name - otherwise we return nothing!
249                 So call can search for another detect service and ask him too.
250 
251     @attention  a) We don't need any mutex here ... because we don't use any member!
252                 b) Dont' use internal player instance "m_pPlayer" to detect given sound file!
253                    It's not necessary to do that ... and we can use temp. variable to do the same.
254                    This way is easy - we don't must synchronize it with currently played sounds!
255                    Another reason to do so ... We are a listener on our internal ma_Player object.
256                    If you would call "IsSoundFile()" on this instance, he would call us back and
257                    we make some uneccssary things ...
258 
259     @seealso    -
260 
261     @param      "lDescriptor", description of file to detect
262     @return     Internal type name which match this file ... or nothing if it is unknown.
263 
264     @onerror    We return nothing.
265     @threadsafe yes
266 *//*-*************************************************************************************************************/
detect(css::uno::Sequence<css::beans::PropertyValue> & lDescriptor)267 ::rtl::OUString SAL_CALL Oxt_Handler::detect( css::uno::Sequence< css::beans::PropertyValue >& lDescriptor )
268     throw( css::uno::RuntimeException )
269 {
270     // Our default is "nothing". So we can return it, if detection failed or fily type is really unknown.
271     ::rtl::OUString sTypeName;
272 
273     // Analyze given descriptor to find filename or input stream or ...
274     ::comphelper::MediaDescriptor aDescriptor( lDescriptor );
275     ::rtl::OUString               sURL       = aDescriptor.getUnpackedValueOrDefault( ::comphelper::MediaDescriptor::PROP_URL(), ::rtl::OUString() );
276 
277     long nLength = sURL.getLength();
278     if ( ( nLength > 4 ) && sURL.matchIgnoreAsciiCase( UNISTRING(".oxt"), nLength-4 ) )
279     {
280         // "IsSoundFile" idffer between different "wav" and "au" file versions ...
281         // couldn't return this information ... because: He use the OS to detect it!
282         // I think we can the following ones:
283         //  a) look for given extension of url to map our type decision HARD CODED!!!
284         //  b) return preferred type every time... it's easy :-)
285         sTypeName = ::rtl::OUString::createFromAscii("oxt_OpenOffice_Extension");
286         aDescriptor[::comphelper::MediaDescriptor::PROP_TYPENAME()] <<= sTypeName;
287         aDescriptor >> lDescriptor;
288     }
289 
290     // Return our decision.
291     return sTypeName;
292 }
293 
294 } // namespace framework
295