Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Components / Addons / ProtocolHandlerAddon_cpp / addon.cxx
blob4f9ed116a69742937c9cc7b673e58b3671cdf590
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 #include <addon.hxx>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/frame/XFrame.hpp>
39 #include <com/sun/star/frame/XController.hpp>
40 #include <com/sun/star/awt/Toolkit.hpp>
41 #include <com/sun/star/awt/XWindowPeer.hpp>
42 #include <com/sun/star/awt/WindowAttribute.hpp>
43 #include <com/sun/star/awt/XMessageBox.hpp>
44 #include <com/sun/star/uno/XComponentContext.hpp>
45 #include <cppuhelper/supportsservice.hxx>
46 #include <osl/diagnose.h>
47 #include <rtl/ustring.hxx>
49 using rtl::OUString;
50 using namespace com::sun::star::uno;
51 using namespace com::sun::star::frame;
52 using namespace com::sun::star::awt;
53 using com::sun::star::beans::PropertyValue;
54 using com::sun::star::util::URL;
56 // This is the service name an Add-On has to implement
57 #define SERVICE_NAME "com.sun.star.frame.ProtocolHandler"
60 /**
61 * Show a message box with the UNO based toolkit
63 static void ShowMessageBox( const Reference< XToolkit >& rToolkit, const Reference< XFrame >& rFrame, const OUString& aTitle, const OUString& aMsgText )
65 if ( rFrame.is() && rToolkit.is() )
67 // describe window properties.
68 WindowDescriptor aDescriptor;
69 aDescriptor.Type = WindowClass_MODALTOP;
70 aDescriptor.WindowServiceName = "infobox";
71 aDescriptor.ParentIndex = -1;
72 aDescriptor.Parent = Reference< XWindowPeer >( rFrame->getContainerWindow(), UNO_QUERY );
73 aDescriptor.Bounds = Rectangle(0,0,300,200);
74 aDescriptor.WindowAttributes = WindowAttribute::BORDER |
75 WindowAttribute::MOVEABLE |
76 WindowAttribute::CLOSEABLE;
78 Reference< XWindowPeer > xPeer = rToolkit->createWindow( aDescriptor );
79 if ( xPeer.is() )
81 Reference< XMessageBox > xMsgBox( xPeer, UNO_QUERY );
82 if ( xMsgBox.is() )
84 xMsgBox->setCaptionText( aTitle );
85 xMsgBox->setMessageText( aMsgText );
86 xMsgBox->execute();
92 /**
93 * Called by the Office framework.
94 * One-time initialization. We have to store the context information
95 * given, like the frame we are bound to, into our members.
97 void SAL_CALL Addon::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException)
99 Reference < XFrame > xFrame;
100 if ( aArguments.getLength() )
102 aArguments[0] >>= xFrame;
103 mxFrame = xFrame;
106 // Create the toolkit to have access to it later
107 mxToolkit = Reference< XToolkit >( Toolkit::create(mxContext), UNO_QUERY_THROW );
111 * Called by the Office framework.
112 * We are ask to query the given URL and return a dispatch object if the URL
113 * contains an Add-On command.
115 Reference< XDispatch > SAL_CALL Addon::queryDispatch( const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags )
116 throw( RuntimeException )
118 Reference < XDispatch > xRet;
119 if ( aURL.Protocol.equalsAscii("org.openoffice.Office.addon.example:") )
121 if ( aURL.Path.equalsAscii( "Function1" ) )
122 xRet = this;
123 else if ( aURL.Path.equalsAscii( "Function2" ) )
124 xRet = this;
125 else if ( aURL.Path.equalsAscii( "Help" ) )
126 xRet = this;
129 return xRet;
133 * Called by the Office framework.
134 * We are ask to execute the given Add-On command URL.
136 void SAL_CALL Addon::dispatch( const URL& aURL, const Sequence < PropertyValue >& lArgs ) throw (RuntimeException)
138 if ( aURL.Protocol.equalsAscii("org.openoffice.Office.addon.example:") )
140 if ( aURL.Path.equalsAscii( "Function1" ) )
142 ShowMessageBox( mxToolkit, mxFrame,
143 OUString( "SDK Add-On example" ),
144 OUString( "Function 1 activated" ) );
146 else if ( aURL.Path.equalsAscii( "Function2" ) )
148 ShowMessageBox( mxToolkit, mxFrame,
149 OUString( "SDK Add-On example" ),
150 OUString( "Function 2 activated" ) );
152 else if ( aURL.Path.equalsAscii( "Help" ) )
154 // Show info box
155 ShowMessageBox( mxToolkit, mxFrame,
156 OUString( "About SDK Add-On example" ),
157 OUString( "This is the SDK Add-On example" ) );
163 * Called by the Office framework.
164 * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
165 * contain Add-On commands.
167 Sequence < Reference< XDispatch > > SAL_CALL Addon::queryDispatches( const Sequence < DispatchDescriptor >& seqDescripts )
168 throw( RuntimeException )
170 sal_Int32 nCount = seqDescripts.getLength();
171 Sequence < Reference < XDispatch > > lDispatcher( nCount );
173 for( sal_Int32 i=0; i<nCount; ++i )
174 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
176 return lDispatcher;
180 * Called by the Office framework.
181 * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
182 * contain Add-On commands.
184 void SAL_CALL Addon::addStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
189 * Called by the Office framework.
190 * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
191 * contain Add-On commands.
193 void SAL_CALL Addon::removeStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
197 // Helper functions for the implementation of UNO component interfaces.
198 OUString Addon_getImplementationName()
199 throw (RuntimeException)
201 return OUString ( IMPLEMENTATION_NAME );
204 Sequence< ::rtl::OUString > SAL_CALL Addon_getSupportedServiceNames()
205 throw (RuntimeException)
207 Sequence < ::rtl::OUString > aRet(1);
208 ::rtl::OUString* pArray = aRet.getArray();
209 pArray[0] = OUString ( SERVICE_NAME );
210 return aRet;
213 Reference< XInterface > SAL_CALL Addon_createInstance( const Reference< XComponentContext > & rContext)
214 throw( Exception )
216 return (cppu::OWeakObject*) new Addon( rContext );
219 // Implementation of the recommended/mandatory interfaces of a UNO component.
220 // XServiceInfo
221 ::rtl::OUString SAL_CALL Addon::getImplementationName( )
222 throw (RuntimeException)
224 return Addon_getImplementationName();
227 sal_Bool SAL_CALL Addon::supportsService( const ::rtl::OUString& rServiceName )
228 throw (RuntimeException)
230 return cppu::supportsService(this, rServiceName);
233 Sequence< ::rtl::OUString > SAL_CALL Addon::getSupportedServiceNames( )
234 throw (RuntimeException)
236 return Addon_getSupportedServiceNames();
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */