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