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 ) throw ( Exception, RuntimeException)
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 				throw( RuntimeException )
107 {
108     Reference < XDispatch > xRet;
109     if ( aURL.Protocol.compareToAscii("org.openoffice.Office.addon.example:") == 0 )
110     {
111         if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
112             xRet = this;
113         else if ( aURL.Path.compareToAscii( "Function2" ) == 0 )
114             xRet = this;
115         else if ( aURL.Path.compareToAscii( "Help" ) == 0 )
116             xRet = this;
117     }
118 
119     return xRet;
120 }
121 
122 /**
123   * Called by the Office framework.
124   * We are ask to execute the given Add-On command URL.
125   */
dispatch(const URL & aURL,const Sequence<PropertyValue> & lArgs)126 void SAL_CALL Addon::dispatch( const URL& aURL, const Sequence < PropertyValue >& lArgs ) throw (RuntimeException)
127 {
128     if ( aURL.Protocol.compareToAscii("org.openoffice.Office.addon.example:") == 0 )
129     {
130         if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
131         {
132             ShowMessageBox( mxToolkit, mxFrame,
133                             OUString( RTL_CONSTASCII_USTRINGPARAM( "SDK Add-On example" )),
134                             OUString( RTL_CONSTASCII_USTRINGPARAM( "Function 1 activated" )) );
135         }
136         else if ( aURL.Path.compareToAscii( "Function2" ) == 0 )
137         {
138             ShowMessageBox( mxToolkit, mxFrame,
139                             OUString( RTL_CONSTASCII_USTRINGPARAM( "SDK Add-On example" )),
140                             OUString( RTL_CONSTASCII_USTRINGPARAM( "Function 2 activated" )) );
141         }
142         else if ( aURL.Path.compareToAscii( "Help" ) == 0 )
143         {
144             // Show info box
145             ShowMessageBox( mxToolkit, mxFrame,
146                             OUString( RTL_CONSTASCII_USTRINGPARAM( "About SDK Add-On example" )),
147                             OUString( RTL_CONSTASCII_USTRINGPARAM( "This is the SDK Add-On example" )) );
148 	}
149     }
150 }
151 
152 /**
153   * Called by the Office framework.
154   * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
155   * contain Add-On commands.
156   */
queryDispatches(const Sequence<DispatchDescriptor> & seqDescripts)157 Sequence < Reference< XDispatch > > SAL_CALL Addon::queryDispatches( const Sequence < DispatchDescriptor >& seqDescripts )
158 			throw( RuntimeException )
159 {
160     sal_Int32 nCount = seqDescripts.getLength();
161     Sequence < Reference < XDispatch > > lDispatcher( nCount );
162 
163     for( sal_Int32 i=0; i<nCount; ++i )
164         lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
165 
166     return lDispatcher;
167 }
168 
169 /**
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.
173   */
addStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)174 void SAL_CALL Addon::addStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
175 {
176 }
177 
178 /**
179   * Called by the Office framework.
180   * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
181   * contain Add-On commands.
182   */
removeStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)183 void SAL_CALL Addon::removeStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
184 {
185 }
186 
187 //##################################################################################################
188 //#### Helper functions for the implementation of UNO component interfaces #########################
189 //##################################################################################################
190 
Addon_getImplementationName()191 ::rtl::OUString Addon_getImplementationName()
192 throw (RuntimeException)
193 {
194     return ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
195 }
196 
Addon_supportsService(const::rtl::OUString & ServiceName)197 sal_Bool SAL_CALL Addon_supportsService( const ::rtl::OUString& ServiceName )
198 throw (RuntimeException)
199 {
200     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
201 }
202 
Addon_getSupportedServiceNames()203 Sequence< ::rtl::OUString > SAL_CALL Addon_getSupportedServiceNames()
204 throw (RuntimeException)
205 {
206 	Sequence < ::rtl::OUString > aRet(1);
207     ::rtl::OUString* pArray = aRet.getArray();
208     pArray[0] =  ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
209     return aRet;
210 }
211 
Addon_createInstance(const Reference<XMultiServiceFactory> & rSMgr)212 Reference< XInterface > SAL_CALL Addon_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
213 	throw( Exception )
214 {
215 	return (cppu::OWeakObject*) new Addon( rSMgr );
216 }
217 
218 //##################################################################################################
219 //#### Implementation of the recommended/mandatory interfaces of a UNO component ###################
220 //##################################################################################################
221 
222 // XServiceInfo
getImplementationName()223 ::rtl::OUString SAL_CALL Addon::getImplementationName(  )
224 	throw (RuntimeException)
225 {
226 	return Addon_getImplementationName();
227 }
228 
supportsService(const::rtl::OUString & rServiceName)229 sal_Bool SAL_CALL Addon::supportsService( const ::rtl::OUString& rServiceName )
230 	throw (RuntimeException)
231 {
232     return Addon_supportsService( rServiceName );
233 }
234 
getSupportedServiceNames()235 Sequence< ::rtl::OUString > SAL_CALL Addon::getSupportedServiceNames(  )
236 	throw (RuntimeException)
237 {
238     return Addon_getSupportedServiceNames();
239 }
240