xref: /trunk/main/odk/examples/DevelopersGuide/Components/Addons/ProtocolHandlerAddon_cpp/addon.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 #include <addon.hxx>
25 #include <osl/diagnose.h>
26 #include <rtl/ustring.hxx>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <com/sun/star/frame/XFrame.hpp>
30 #include <com/sun/star/frame/XController.hpp>
31 #include <com/sun/star/awt/XToolkit.hpp>
32 #include <com/sun/star/awt/XWindowPeer.hpp>
33 #include <com/sun/star/awt/WindowAttribute.hpp>
34 #include <com/sun/star/awt/XMessageBox.hpp>
35 
36 using rtl::OUString;
37 using namespace com::sun::star::uno;
38 using namespace com::sun::star::frame;
39 using namespace com::sun::star::awt;
40 using com::sun::star::lang::XMultiServiceFactory;
41 using com::sun::star::beans::PropertyValue;
42 using com::sun::star::util::URL;
43 
44 // This is the service name an Add-On has to implement
45 #define SERVICE_NAME "com.sun.star.frame.ProtocolHandler"
46 
47 
48 /**
49   * Show a message box with the UNO based toolkit
50   */
ShowMessageBox(const Reference<XToolkit> & rToolkit,const Reference<XFrame> & rFrame,const OUString & aTitle,const OUString & aMsgText)51 static void ShowMessageBox( const Reference< XToolkit >& rToolkit, const Reference< XFrame >& rFrame, const OUString& aTitle, const OUString& aMsgText )
52 {
53     if ( rFrame.is() && rToolkit.is() )
54     {
55         // describe window properties.
56         WindowDescriptor                aDescriptor;
57         aDescriptor.Type              = WindowClass_MODALTOP;
58         aDescriptor.WindowServiceName = OUString( RTL_CONSTASCII_USTRINGPARAM( "infobox" ));
59         aDescriptor.ParentIndex       = -1;
60         aDescriptor.Parent            = Reference< XWindowPeer >( rFrame->getContainerWindow(), UNO_QUERY );
61         aDescriptor.Bounds            = Rectangle(0,0,300,200);
62         aDescriptor.WindowAttributes  = WindowAttribute::BORDER |
63 WindowAttribute::MOVEABLE |
64 WindowAttribute::CLOSEABLE;
65 
66         Reference< XWindowPeer > xPeer = rToolkit->createWindow( aDescriptor );
67         if ( xPeer.is() )
68         {
69             Reference< XMessageBox > xMsgBox( xPeer, UNO_QUERY );
70             if ( xMsgBox.is() )
71             {
72                 xMsgBox->setCaptionText( aTitle );
73                 xMsgBox->setMessageText( aMsgText );
74                 xMsgBox->execute();
75             }
76         }
77     }
78 }
79 
80 /**
81   * Called by the Office framework.
82   * One-time initialization. We have to store the context information
83   * given, like the frame we are bound to, into our members.
84   */
initialize(const Sequence<Any> & aArguments)85 void SAL_CALL Addon::initialize( const Sequence< Any >& aArguments )
86 {
87     Reference < XFrame > xFrame;
88     if ( aArguments.getLength() )
89     {
90         aArguments[0] >>= xFrame;
91         mxFrame = xFrame;
92     }
93 
94     // Create the toolkit to have access to it later
95     mxToolkit = Reference< XToolkit >( mxMSF->createInstance(
96                                         OUString( RTL_CONSTASCII_USTRINGPARAM(
97                                             "com.sun.star.awt.Toolkit" ))), UNO_QUERY );
98 }
99 
100 /**
101   * Called by the Office framework.
102   * We are ask to query the given URL and return a dispatch object if the URL
103   * contains an Add-On command.
104   */
queryDispatch(const URL & aURL,const::rtl::OUString & sTargetFrameName,sal_Int32 nSearchFlags)105 Reference< XDispatch > SAL_CALL Addon::queryDispatch( const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags )
106 {
107     Reference < XDispatch > xRet;
108     if ( aURL.Protocol.compareToAscii("org.openoffice.Office.addon.example:") == 0 )
109     {
110         if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
111             xRet = this;
112         else if ( aURL.Path.compareToAscii( "Function2" ) == 0 )
113             xRet = this;
114         else if ( aURL.Path.compareToAscii( "Help" ) == 0 )
115             xRet = this;
116     }
117 
118     return xRet;
119 }
120 
121 /**
122   * Called by the Office framework.
123   * We are ask to execute the given Add-On command URL.
124   */
dispatch(const URL & aURL,const Sequence<PropertyValue> & lArgs)125 void SAL_CALL Addon::dispatch( const URL& aURL, const Sequence < PropertyValue >& lArgs )
126 {
127     if ( aURL.Protocol.compareToAscii("org.openoffice.Office.addon.example:") == 0 )
128     {
129         if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
130         {
131             ShowMessageBox( mxToolkit, mxFrame,
132                             OUString( RTL_CONSTASCII_USTRINGPARAM( "SDK Add-On example" )),
133                             OUString( RTL_CONSTASCII_USTRINGPARAM( "Function 1 activated" )) );
134         }
135         else if ( aURL.Path.compareToAscii( "Function2" ) == 0 )
136         {
137             ShowMessageBox( mxToolkit, mxFrame,
138                             OUString( RTL_CONSTASCII_USTRINGPARAM( "SDK Add-On example" )),
139                             OUString( RTL_CONSTASCII_USTRINGPARAM( "Function 2 activated" )) );
140         }
141         else if ( aURL.Path.compareToAscii( "Help" ) == 0 )
142         {
143             // Show info box
144             ShowMessageBox( mxToolkit, mxFrame,
145                             OUString( RTL_CONSTASCII_USTRINGPARAM( "About SDK Add-On example" )),
146                             OUString( RTL_CONSTASCII_USTRINGPARAM( "This is the SDK Add-On example" )) );
147     }
148     }
149 }
150 
151 /**
152   * Called by the Office framework.
153   * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
154   * contain Add-On commands.
155   */
queryDispatches(const Sequence<DispatchDescriptor> & seqDescripts)156 Sequence < Reference< XDispatch > > SAL_CALL Addon::queryDispatches( const Sequence < DispatchDescriptor >& seqDescripts )
157 {
158     sal_Int32 nCount = seqDescripts.getLength();
159     Sequence < Reference < XDispatch > > lDispatcher( nCount );
160 
161     for( sal_Int32 i=0; i<nCount; ++i )
162         lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
163 
164     return lDispatcher;
165 }
166 
167 /**
168   * Called by the Office framework.
169   * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
170   * contain Add-On commands.
171   */
addStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)172 void SAL_CALL Addon::addStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL )
173 {
174 }
175 
176 /**
177   * Called by the Office framework.
178   * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
179   * contain Add-On commands.
180   */
removeStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)181 void SAL_CALL Addon::removeStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL )
182 {
183 }
184 
185 //##################################################################################################
186 //#### Helper functions for the implementation of UNO component interfaces #########################
187 //##################################################################################################
188 
Addon_getImplementationName()189 ::rtl::OUString Addon_getImplementationName()
190 {
191     return ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
192 }
193 
Addon_supportsService(const::rtl::OUString & ServiceName)194 sal_Bool SAL_CALL Addon_supportsService( const ::rtl::OUString& ServiceName )
195 {
196     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
197 }
198 
Addon_getSupportedServiceNames()199 Sequence< ::rtl::OUString > SAL_CALL Addon_getSupportedServiceNames()
200 {
201     Sequence < ::rtl::OUString > aRet(1);
202     ::rtl::OUString* pArray = aRet.getArray();
203     pArray[0] =  ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
204     return aRet;
205 }
206 
Addon_createInstance(const Reference<XMultiServiceFactory> & rSMgr)207 Reference< XInterface > SAL_CALL Addon_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
208 {
209     return (cppu::OWeakObject*) new Addon( rSMgr );
210 }
211 
212 //##################################################################################################
213 //#### Implementation of the recommended/mandatory interfaces of a UNO component ###################
214 //##################################################################################################
215 
216 // XServiceInfo
getImplementationName()217 ::rtl::OUString SAL_CALL Addon::getImplementationName(  )
218 {
219     return Addon_getImplementationName();
220 }
221 
supportsService(const::rtl::OUString & rServiceName)222 sal_Bool SAL_CALL Addon::supportsService( const ::rtl::OUString& rServiceName )
223 {
224     return Addon_supportsService( rServiceName );
225 }
226 
getSupportedServiceNames()227 Sequence< ::rtl::OUString > SAL_CALL Addon::getSupportedServiceNames(  )
228 {
229     return Addon_getSupportedServiceNames();
230 }
231