Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / Components / Addons / ProtocolHandlerAddon_cpp / addon.cxx
blob35870d6bc389bdd35061bca5f38b65da05537cb1
1 /*************************************************************************
3 * $RCSfile: addon.cxx,v $
5 * $Revision: 1.6 $
7 * last change: $Author: rt $ $Date: 2008-04-10 16:28:13 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 #include <addon.hxx>
42 #include <osl/diagnose.h>
43 #include <rtl/ustring.hxx>
44 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 #include <com/sun/star/frame/XFrame.hpp>
47 #include <com/sun/star/frame/XController.hpp>
48 #include <com/sun/star/awt/XToolkit.hpp>
49 #include <com/sun/star/awt/XWindowPeer.hpp>
50 #include <com/sun/star/awt/WindowAttribute.hpp>
51 #include <com/sun/star/awt/XMessageBox.hpp>
53 using rtl::OUString;
54 using namespace com::sun::star::uno;
55 using namespace com::sun::star::frame;
56 using namespace com::sun::star::awt;
57 using com::sun::star::lang::XMultiServiceFactory;
58 using com::sun::star::beans::PropertyValue;
59 using com::sun::star::util::URL;
61 // This is the service name an Add-On has to implement
62 #define SERVICE_NAME "com.sun.star.frame.ProtocolHandler"
65 /**
66 * Show a message box with the UNO based toolkit
68 static void ShowMessageBox( const Reference< XToolkit >& rToolkit, const Reference< XFrame >& rFrame, const OUString& aTitle, const OUString& aMsgText )
70 if ( rFrame.is() && rToolkit.is() )
72 // describe window properties.
73 WindowDescriptor aDescriptor;
74 aDescriptor.Type = WindowClass_MODALTOP;
75 aDescriptor.WindowServiceName = OUString( RTL_CONSTASCII_USTRINGPARAM( "infobox" ));
76 aDescriptor.ParentIndex = -1;
77 aDescriptor.Parent = Reference< XWindowPeer >( rFrame->getContainerWindow(), UNO_QUERY );
78 aDescriptor.Bounds = Rectangle(0,0,300,200);
79 aDescriptor.WindowAttributes = WindowAttribute::BORDER |
80 WindowAttribute::MOVEABLE |
81 WindowAttribute::CLOSEABLE;
83 Reference< XWindowPeer > xPeer = rToolkit->createWindow( aDescriptor );
84 if ( xPeer.is() )
86 Reference< XMessageBox > xMsgBox( xPeer, UNO_QUERY );
87 if ( xMsgBox.is() )
89 xMsgBox->setCaptionText( aTitle );
90 xMsgBox->setMessageText( aMsgText );
91 xMsgBox->execute();
97 /**
98 * Called by the Office framework.
99 * One-time initialization. We have to store the context information
100 * given, like the frame we are bound to, into our members.
102 void SAL_CALL Addon::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException)
104 Reference < XFrame > xFrame;
105 if ( aArguments.getLength() )
107 aArguments[0] >>= xFrame;
108 mxFrame = xFrame;
111 // Create the toolkit to have access to it later
112 mxToolkit = Reference< XToolkit >( mxMSF->createInstance(
113 OUString( RTL_CONSTASCII_USTRINGPARAM(
114 "com.sun.star.awt.Toolkit" ))), UNO_QUERY );
118 * Called by the Office framework.
119 * We are ask to query the given URL and return a dispatch object if the URL
120 * contains an Add-On command.
122 Reference< XDispatch > SAL_CALL Addon::queryDispatch( const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags )
123 throw( RuntimeException )
125 Reference < XDispatch > xRet;
126 if ( aURL.Protocol.compareToAscii("org.openoffice.Office.addon.example:") == 0 )
128 if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
129 xRet = this;
130 else if ( aURL.Path.compareToAscii( "Function2" ) == 0 )
131 xRet = this;
132 else if ( aURL.Path.compareToAscii( "Help" ) == 0 )
133 xRet = this;
136 return xRet;
140 * Called by the Office framework.
141 * We are ask to execute the given Add-On command URL.
143 void SAL_CALL Addon::dispatch( const URL& aURL, const Sequence < PropertyValue >& lArgs ) throw (RuntimeException)
145 if ( aURL.Protocol.compareToAscii("org.openoffice.Office.addon.example:") == 0 )
147 if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
149 ShowMessageBox( mxToolkit, mxFrame,
150 OUString( RTL_CONSTASCII_USTRINGPARAM( "SDK Add-On example" )),
151 OUString( RTL_CONSTASCII_USTRINGPARAM( "Function 1 activated" )) );
153 else if ( aURL.Path.compareToAscii( "Function2" ) == 0 )
155 ShowMessageBox( mxToolkit, mxFrame,
156 OUString( RTL_CONSTASCII_USTRINGPARAM( "SDK Add-On example" )),
157 OUString( RTL_CONSTASCII_USTRINGPARAM( "Function 2 activated" )) );
159 else if ( aURL.Path.compareToAscii( "Help" ) == 0 )
161 // Show info box
162 ShowMessageBox( mxToolkit, mxFrame,
163 OUString( RTL_CONSTASCII_USTRINGPARAM( "About SDK Add-On example" )),
164 OUString( RTL_CONSTASCII_USTRINGPARAM( "This is the SDK Add-On example" )) );
170 * Called by the Office framework.
171 * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
172 * contain Add-On commands.
174 Sequence < Reference< XDispatch > > SAL_CALL Addon::queryDispatches( const Sequence < DispatchDescriptor >& seqDescripts )
175 throw( RuntimeException )
177 sal_Int32 nCount = seqDescripts.getLength();
178 Sequence < Reference < XDispatch > > lDispatcher( nCount );
180 for( sal_Int32 i=0; i<nCount; ++i )
181 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
183 return lDispatcher;
187 * Called by the Office framework.
188 * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
189 * contain Add-On commands.
191 void SAL_CALL Addon::addStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
196 * Called by the Office framework.
197 * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
198 * contain Add-On commands.
200 void SAL_CALL Addon::removeStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
204 //##################################################################################################
205 //#### Helper functions for the implementation of UNO component interfaces #########################
206 //##################################################################################################
208 ::rtl::OUString Addon_getImplementationName()
209 throw (RuntimeException)
211 return ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
214 sal_Bool SAL_CALL Addon_supportsService( const ::rtl::OUString& ServiceName )
215 throw (RuntimeException)
217 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
220 Sequence< ::rtl::OUString > SAL_CALL Addon_getSupportedServiceNames()
221 throw (RuntimeException)
223 Sequence < ::rtl::OUString > aRet(1);
224 ::rtl::OUString* pArray = aRet.getArray();
225 pArray[0] = ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
226 return aRet;
229 Reference< XInterface > SAL_CALL Addon_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
230 throw( Exception )
232 return (cppu::OWeakObject*) new Addon( rSMgr );
235 //##################################################################################################
236 //#### Implementation of the recommended/mandatory interfaces of a UNO component ###################
237 //##################################################################################################
239 // XServiceInfo
240 ::rtl::OUString SAL_CALL Addon::getImplementationName( )
241 throw (RuntimeException)
243 return Addon_getImplementationName();
246 sal_Bool SAL_CALL Addon::supportsService( const ::rtl::OUString& rServiceName )
247 throw (RuntimeException)
249 return Addon_supportsService( rServiceName );
252 Sequence< ::rtl::OUString > SAL_CALL Addon::getSupportedServiceNames( )
253 throw (RuntimeException)
255 return Addon_getSupportedServiceNames();