xref: /aoo41x/main/sfx2/source/doc/plugin.cxx (revision d119d52d)
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_sfx2.hxx"
26 
27 #include "plugin.hxx"
28 #include <com/sun/star/plugin/XPluginManager.hpp>
29 #include <com/sun/star/plugin/PluginMode.hpp>
30 #include <com/sun/star/awt/XControl.hpp>
31 
32 #include <tools/debug.hxx>
33 #include <rtl/ustring.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
35 #include <svtools/miscopt.hxx>
36 #include <vcl/window.hxx>
37 
38 using namespace ::com::sun::star;
39 
40 namespace sfx2
41 {
42 
43 class PluginWindow_Impl : public Window
44 {
45 public:
46     uno::Reference < awt::XWindow > xWindow;
PluginWindow_Impl(Window * pParent)47     PluginWindow_Impl( Window* pParent )
48      : Window( pParent, WB_CLIPCHILDREN )
49      {}
50 
51      virtual void Resize();
52 };
53 
Resize()54 void PluginWindow_Impl::Resize()
55 {
56     Size aSize( GetOutputSizePixel() );
57     if ( xWindow.is() )
58         xWindow->setPosSize( 0, 0, aSize.Width(), aSize.Height(), WINDOW_POSSIZE_SIZE );
59 }
60 
61 #define PROPERTY_UNBOUND 0
62 
63 #define WID_COMMANDS    1
64 #define WID_MIMETYPE    2
65 #define WID_URL         3
lcl_GetPluginPropertyMap_Impl()66 const SfxItemPropertyMapEntry* lcl_GetPluginPropertyMap_Impl()
67 {
68     static SfxItemPropertyMapEntry aPluginPropertyMap_Impl[] =
69     {
70         { MAP_CHAR_LEN("PluginCommands"), WID_COMMANDS, &::getCppuType((::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >*)0), PROPERTY_UNBOUND, 0},
71         { MAP_CHAR_LEN("PluginMimeType"), WID_MIMETYPE, &::getCppuType((const ::rtl::OUString*)0), PROPERTY_UNBOUND, 0 },
72         { MAP_CHAR_LEN("PluginURL"),      WID_URL     , &::getCppuType((const ::rtl::OUString*)0), PROPERTY_UNBOUND, 0 },
73         {0,0,0,0,0,0}
74     };
75     return aPluginPropertyMap_Impl;
76 }
77 
78 SFX_IMPL_XSERVICEINFO( PluginObject, "com.sun.star.embed.SpecialEmbeddedObject", "com.sun.star.comp.sfx2.PluginObject" )
79 SFX_IMPL_SINGLEFACTORY( PluginObject );
80 
PluginObject(const uno::Reference<lang::XMultiServiceFactory> & rFact)81 PluginObject::PluginObject( const uno::Reference < lang::XMultiServiceFactory >& rFact )
82     : mxFact( rFact )
83     , maPropMap( lcl_GetPluginPropertyMap_Impl() )
84 {
85 }
86 
~PluginObject()87 PluginObject::~PluginObject()
88 {
89 }
90 
initialize(const uno::Sequence<uno::Any> & aArguments)91 void SAL_CALL PluginObject::initialize( const uno::Sequence< uno::Any >& aArguments ) throw ( uno::Exception, uno::RuntimeException )
92 {
93 	if ( aArguments.getLength() )
94         aArguments[0] >>= mxObj;
95 }
96 
load(const uno::Sequence<com::sun::star::beans::PropertyValue> &,const uno::Reference<frame::XFrame> & xFrame)97 sal_Bool SAL_CALL PluginObject::load(
98     const uno::Sequence < com::sun::star::beans::PropertyValue >& /*lDescriptor*/,
99     const uno::Reference < frame::XFrame >& xFrame )
100 throw( uno::RuntimeException )
101 {
102     uno::Reference< plugin::XPluginManager > xPMgr( mxFact->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.plugin.PluginManager") ), uno::UNO_QUERY );
103     if (!xPMgr.is() )
104         return sal_False;
105 
106     if ( SvtMiscOptions().IsPluginsEnabled() )
107     {
108         Window* pParent = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
109         PluginWindow_Impl* pWin = new PluginWindow_Impl( pParent );
110         pWin->SetSizePixel( pParent->GetOutputSizePixel() );
111         pWin->SetBackground();
112         pWin->Show();
113 
114         sal_uIntPtr nCount = maCmdList.Count();
115         uno::Sequence < ::rtl::OUString > aCmds( nCount ), aArgs( nCount );
116         ::rtl::OUString *pCmds = aCmds.getArray(), *pArgs = aArgs.getArray();
117         for( sal_uIntPtr i = 0; i < nCount; i++ )
118         {
119             SvCommand & rCmd = maCmdList.GetObject( i );
120             pCmds[i] = rCmd.GetCommand();
121             pArgs[i] = rCmd.GetArgument();
122         }
123 
124         mxPlugin = xPMgr->createPluginFromURL(
125             xPMgr->createPluginContext(), plugin::PluginMode::EMBED, aCmds, aArgs, uno::Reference< awt::XToolkit >(),
126             uno::Reference< awt::XWindowPeer >( pWin->GetComponentInterface() ), maURL );
127 
128         if ( mxPlugin.is() )
129         {
130             uno::Reference< awt::XWindow > xWindow( mxPlugin, uno::UNO_QUERY );
131             if ( xWindow.is() )
132             {
133                 pWin->xWindow = xWindow;
134                 pWin->Resize();
135                 xWindow->setVisible( sal_True );
136             }
137 
138             try
139             {
140                 uno::Reference< awt::XControl > xControl( mxPlugin, uno::UNO_QUERY );
141                 if( xControl.is() )
142                 {
143                     uno::Reference< awt::XControlModel > xModel = xControl->getModel();
144                     uno::Reference< beans::XPropertySet > xProp( xModel, ::uno::UNO_QUERY );
145                     if( xProp.is() )
146                     {
147                         uno::Any aValue = xProp->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) ) );
148                         aValue >>= maURL;
149                         aValue = xProp->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TYPE" ) ) );
150                         aValue >>= maMimeType;
151                     }
152                 }
153             }
154             catch( uno::Exception& )
155             {
156             }
157         }
158 
159         uno::Reference < awt::XWindow > xWindow( pWin->GetComponentInterface(), uno::UNO_QUERY );
160 
161         // we must destroy the plugin before the parent is destroyed
162         xWindow->addEventListener( this );
163         xFrame->setComponent( xWindow, uno::Reference < frame::XController >() );
164         return mxPlugin.is() ? sal_True : sal_False;
165     }
166 
167     return sal_False;
168 }
169 
cancel()170 void SAL_CALL PluginObject::cancel() throw( com::sun::star::uno::RuntimeException )
171 {
172     uno::Reference< lang::XComponent > xComp( mxPlugin, uno::UNO_QUERY );
173 	if (xComp.is())
174 		xComp->dispose();
175     mxPlugin = 0;
176 }
177 
close(sal_Bool)178 void SAL_CALL PluginObject::close( sal_Bool /*bDeliverOwnership*/ ) throw( com::sun::star::util::CloseVetoException, com::sun::star::uno::RuntimeException )
179 {
180 }
181 
addCloseListener(const com::sun::star::uno::Reference<com::sun::star::util::XCloseListener> &)182 void SAL_CALL PluginObject::addCloseListener( const com::sun::star::uno::Reference < com::sun::star::util::XCloseListener >& ) throw( com::sun::star::uno::RuntimeException )
183 {
184 }
185 
removeCloseListener(const com::sun::star::uno::Reference<com::sun::star::util::XCloseListener> &)186 void SAL_CALL PluginObject::removeCloseListener( const com::sun::star::uno::Reference < com::sun::star::util::XCloseListener >& ) throw( com::sun::star::uno::RuntimeException )
187 {
188 }
189 
disposing(const com::sun::star::lang::EventObject &)190 void SAL_CALL PluginObject::disposing( const com::sun::star::lang::EventObject& ) throw (com::sun::star::uno::RuntimeException)
191 {
192     cancel();
193 }
194 
getPropertySetInfo()195 uno::Reference< beans::XPropertySetInfo > SAL_CALL PluginObject::getPropertySetInfo() throw( ::com::sun::star::uno::RuntimeException )
196 {
197     static uno::Reference< beans::XPropertySetInfo > xInfo = new SfxItemPropertySetInfo( &maPropMap );
198     return xInfo;
199 }
200 
setPropertyValue(const::rtl::OUString & aPropertyName,const uno::Any & aAny)201 void SAL_CALL PluginObject::setPropertyValue(const ::rtl::OUString& aPropertyName, const uno::Any& aAny)
202     throw ( beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
203 {
204     if ( aPropertyName.equalsAscii("PluginURL") )
205     {
206         aAny >>= maURL;
207     }
208     else if ( aPropertyName.equalsAscii("PluginMimeType") )
209     {
210         aAny >>= maMimeType;
211     }
212     else if ( aPropertyName.equalsAscii("PluginCommands") )
213     {
214         maCmdList.Clear();
215         uno::Sequence < beans::PropertyValue > aCommandSequence;
216         if( aAny >>= aCommandSequence )
217             maCmdList.FillFromSequence( aCommandSequence );
218     }
219     else
220          throw beans::UnknownPropertyException();
221 }
222 
getPropertyValue(const::rtl::OUString & aPropertyName)223 uno::Any SAL_CALL PluginObject::getPropertyValue(const ::rtl::OUString& aPropertyName)
224         throw ( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
225 {
226     uno::Any aAny;
227     if ( aPropertyName.equalsAscii("PluginURL") )
228     {
229         aAny <<= maURL;
230     }
231     else if ( aPropertyName.equalsAscii("PluginMimeType") )
232     {
233         aAny <<= maMimeType;
234     }
235     else if ( aPropertyName.equalsAscii("PluginCommands") )
236     {
237         uno::Sequence< beans::PropertyValue > aCommandSequence;
238         maCmdList.FillSequence( aCommandSequence );
239         aAny <<= aCommandSequence;
240     }
241     else
242          throw beans::UnknownPropertyException();
243     return aAny;
244 }
245 
addPropertyChangeListener(const::rtl::OUString &,const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertyChangeListener> &)246 void SAL_CALL PluginObject::addPropertyChangeListener(const ::rtl::OUString&, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener > & ) throw( ::com::sun::star::uno::RuntimeException )
247 {
248 }
249 
removePropertyChangeListener(const::rtl::OUString &,const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertyChangeListener> &)250 void SAL_CALL PluginObject::removePropertyChangeListener(const ::rtl::OUString&, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener > & ) throw( ::com::sun::star::uno::RuntimeException )
251 {
252 }
253 
addVetoableChangeListener(const::rtl::OUString &,const::com::sun::star::uno::Reference<::com::sun::star::beans::XVetoableChangeListener> &)254 void SAL_CALL PluginObject::addVetoableChangeListener(const ::rtl::OUString&, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener > & ) throw( ::com::sun::star::uno::RuntimeException )
255 {
256 }
257 
removeVetoableChangeListener(const::rtl::OUString &,const::com::sun::star::uno::Reference<::com::sun::star::beans::XVetoableChangeListener> &)258 void SAL_CALL PluginObject::removeVetoableChangeListener(const ::rtl::OUString&, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener > & ) throw( ::com::sun::star::uno::RuntimeException )
259 {
260 }
261 
262 }
263