1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <addon.hxx>
25cdf0e10cSrcweir #include <osl/diagnose.h>
26cdf0e10cSrcweir #include <rtl/ustring.hxx>
27cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
29cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
30cdf0e10cSrcweir #include <com/sun/star/frame/XController.hpp>
31cdf0e10cSrcweir #include <com/sun/star/awt/XToolkit.hpp>
32cdf0e10cSrcweir #include <com/sun/star/awt/XWindowPeer.hpp>
33cdf0e10cSrcweir #include <com/sun/star/awt/WindowAttribute.hpp>
34cdf0e10cSrcweir #include <com/sun/star/awt/XMessageBox.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using rtl::OUString;
37cdf0e10cSrcweir using namespace com::sun::star::uno;
38cdf0e10cSrcweir using namespace com::sun::star::frame;
39cdf0e10cSrcweir using namespace com::sun::star::awt;
40cdf0e10cSrcweir using com::sun::star::lang::XMultiServiceFactory;
41cdf0e10cSrcweir using com::sun::star::beans::PropertyValue;
42cdf0e10cSrcweir using com::sun::star::util::URL;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // This is the service name an Add-On has to implement
45cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.frame.ProtocolHandler"
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /**
49cdf0e10cSrcweir   * Show a message box with the UNO based toolkit
50cdf0e10cSrcweir   */
ShowMessageBox(const Reference<XToolkit> & rToolkit,const Reference<XFrame> & rFrame,const OUString & aTitle,const OUString & aMsgText)51cdf0e10cSrcweir static void ShowMessageBox( const Reference< XToolkit >& rToolkit, const Reference< XFrame >& rFrame, const OUString& aTitle, const OUString& aMsgText )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     if ( rFrame.is() && rToolkit.is() )
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         // describe window properties.
56cdf0e10cSrcweir         WindowDescriptor                aDescriptor;
57cdf0e10cSrcweir         aDescriptor.Type              = WindowClass_MODALTOP;
58cdf0e10cSrcweir         aDescriptor.WindowServiceName = OUString( RTL_CONSTASCII_USTRINGPARAM( "infobox" ));
59cdf0e10cSrcweir         aDescriptor.ParentIndex       = -1;
60cdf0e10cSrcweir         aDescriptor.Parent            = Reference< XWindowPeer >( rFrame->getContainerWindow(), UNO_QUERY );
61cdf0e10cSrcweir         aDescriptor.Bounds            = Rectangle(0,0,300,200);
62cdf0e10cSrcweir         aDescriptor.WindowAttributes  = WindowAttribute::BORDER |
63cdf0e10cSrcweir WindowAttribute::MOVEABLE |
64cdf0e10cSrcweir WindowAttribute::CLOSEABLE;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         Reference< XWindowPeer > xPeer = rToolkit->createWindow( aDescriptor );
67cdf0e10cSrcweir         if ( xPeer.is() )
68cdf0e10cSrcweir         {
69cdf0e10cSrcweir             Reference< XMessageBox > xMsgBox( xPeer, UNO_QUERY );
70cdf0e10cSrcweir             if ( xMsgBox.is() )
71cdf0e10cSrcweir             {
72cdf0e10cSrcweir                 xMsgBox->setCaptionText( aTitle );
73cdf0e10cSrcweir                 xMsgBox->setMessageText( aMsgText );
74cdf0e10cSrcweir                 xMsgBox->execute();
75cdf0e10cSrcweir             }
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir /**
81cdf0e10cSrcweir   * Called by the Office framework.
82cdf0e10cSrcweir   * One-time initialization. We have to store the context information
83cdf0e10cSrcweir   * given, like the frame we are bound to, into our members.
84cdf0e10cSrcweir   */
initialize(const Sequence<Any> & aArguments)85cdf0e10cSrcweir void SAL_CALL Addon::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException)
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     Reference < XFrame > xFrame;
88cdf0e10cSrcweir     if ( aArguments.getLength() )
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         aArguments[0] >>= xFrame;
91cdf0e10cSrcweir         mxFrame = xFrame;
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     // Create the toolkit to have access to it later
95cdf0e10cSrcweir     mxToolkit = Reference< XToolkit >( mxMSF->createInstance(
96cdf0e10cSrcweir                                         OUString( RTL_CONSTASCII_USTRINGPARAM(
97cdf0e10cSrcweir                                             "com.sun.star.awt.Toolkit" ))), UNO_QUERY );
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir /**
101cdf0e10cSrcweir   * Called by the Office framework.
102cdf0e10cSrcweir   * We are ask to query the given URL and return a dispatch object if the URL
103cdf0e10cSrcweir   * contains an Add-On command.
104cdf0e10cSrcweir   */
queryDispatch(const URL & aURL,const::rtl::OUString & sTargetFrameName,sal_Int32 nSearchFlags)105cdf0e10cSrcweir Reference< XDispatch > SAL_CALL Addon::queryDispatch( const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags )
106cdf0e10cSrcweir 				throw( RuntimeException )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     Reference < XDispatch > xRet;
109cdf0e10cSrcweir     if ( aURL.Protocol.compareToAscii("org.openoffice.Office.addon.example:") == 0 )
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
112cdf0e10cSrcweir             xRet = this;
113cdf0e10cSrcweir         else if ( aURL.Path.compareToAscii( "Function2" ) == 0 )
114cdf0e10cSrcweir             xRet = this;
115cdf0e10cSrcweir         else if ( aURL.Path.compareToAscii( "Help" ) == 0 )
116cdf0e10cSrcweir             xRet = this;
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     return xRet;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir /**
123cdf0e10cSrcweir   * Called by the Office framework.
124cdf0e10cSrcweir   * We are ask to execute the given Add-On command URL.
125cdf0e10cSrcweir   */
dispatch(const URL & aURL,const Sequence<PropertyValue> & lArgs)126cdf0e10cSrcweir void SAL_CALL Addon::dispatch( const URL& aURL, const Sequence < PropertyValue >& lArgs ) throw (RuntimeException)
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     if ( aURL.Protocol.compareToAscii("org.openoffice.Office.addon.example:") == 0 )
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
131cdf0e10cSrcweir         {
132cdf0e10cSrcweir             ShowMessageBox( mxToolkit, mxFrame,
133cdf0e10cSrcweir                             OUString( RTL_CONSTASCII_USTRINGPARAM( "SDK Add-On example" )),
134cdf0e10cSrcweir                             OUString( RTL_CONSTASCII_USTRINGPARAM( "Function 1 activated" )) );
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir         else if ( aURL.Path.compareToAscii( "Function2" ) == 0 )
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             ShowMessageBox( mxToolkit, mxFrame,
139cdf0e10cSrcweir                             OUString( RTL_CONSTASCII_USTRINGPARAM( "SDK Add-On example" )),
140cdf0e10cSrcweir                             OUString( RTL_CONSTASCII_USTRINGPARAM( "Function 2 activated" )) );
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir         else if ( aURL.Path.compareToAscii( "Help" ) == 0 )
143cdf0e10cSrcweir         {
144cdf0e10cSrcweir             // Show info box
145cdf0e10cSrcweir             ShowMessageBox( mxToolkit, mxFrame,
146cdf0e10cSrcweir                             OUString( RTL_CONSTASCII_USTRINGPARAM( "About SDK Add-On example" )),
147cdf0e10cSrcweir                             OUString( RTL_CONSTASCII_USTRINGPARAM( "This is the SDK Add-On example" )) );
148cdf0e10cSrcweir 	}
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir /**
153cdf0e10cSrcweir   * Called by the Office framework.
154cdf0e10cSrcweir   * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
155cdf0e10cSrcweir   * contain Add-On commands.
156cdf0e10cSrcweir   */
queryDispatches(const Sequence<DispatchDescriptor> & seqDescripts)157cdf0e10cSrcweir Sequence < Reference< XDispatch > > SAL_CALL Addon::queryDispatches( const Sequence < DispatchDescriptor >& seqDescripts )
158cdf0e10cSrcweir 			throw( RuntimeException )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     sal_Int32 nCount = seqDescripts.getLength();
161cdf0e10cSrcweir     Sequence < Reference < XDispatch > > lDispatcher( nCount );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     for( sal_Int32 i=0; i<nCount; ++i )
164cdf0e10cSrcweir         lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     return lDispatcher;
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir /**
170cdf0e10cSrcweir   * Called by the Office framework.
171cdf0e10cSrcweir   * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
172cdf0e10cSrcweir   * contain Add-On commands.
173cdf0e10cSrcweir   */
addStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)174cdf0e10cSrcweir void SAL_CALL Addon::addStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir /**
179cdf0e10cSrcweir   * Called by the Office framework.
180cdf0e10cSrcweir   * We are ask to query the given sequence of URLs and return dispatch objects if the URLs
181cdf0e10cSrcweir   * contain Add-On commands.
182cdf0e10cSrcweir   */
removeStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)183cdf0e10cSrcweir void SAL_CALL Addon::removeStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir //##################################################################################################
188cdf0e10cSrcweir //#### Helper functions for the implementation of UNO component interfaces #########################
189cdf0e10cSrcweir //##################################################################################################
190cdf0e10cSrcweir 
Addon_getImplementationName()191cdf0e10cSrcweir ::rtl::OUString Addon_getImplementationName()
192cdf0e10cSrcweir throw (RuntimeException)
193cdf0e10cSrcweir {
194cdf0e10cSrcweir     return ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
Addon_supportsService(const::rtl::OUString & ServiceName)197cdf0e10cSrcweir sal_Bool SAL_CALL Addon_supportsService( const ::rtl::OUString& ServiceName )
198cdf0e10cSrcweir throw (RuntimeException)
199cdf0e10cSrcweir {
200cdf0e10cSrcweir     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
Addon_getSupportedServiceNames()203cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL Addon_getSupportedServiceNames()
204cdf0e10cSrcweir throw (RuntimeException)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	Sequence < ::rtl::OUString > aRet(1);
207cdf0e10cSrcweir     ::rtl::OUString* pArray = aRet.getArray();
208cdf0e10cSrcweir     pArray[0] =  ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
209cdf0e10cSrcweir     return aRet;
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
Addon_createInstance(const Reference<XMultiServiceFactory> & rSMgr)212cdf0e10cSrcweir Reference< XInterface > SAL_CALL Addon_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
213cdf0e10cSrcweir 	throw( Exception )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	return (cppu::OWeakObject*) new Addon( rSMgr );
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir //##################################################################################################
219cdf0e10cSrcweir //#### Implementation of the recommended/mandatory interfaces of a UNO component ###################
220cdf0e10cSrcweir //##################################################################################################
221cdf0e10cSrcweir 
222cdf0e10cSrcweir // XServiceInfo
getImplementationName()223cdf0e10cSrcweir ::rtl::OUString SAL_CALL Addon::getImplementationName(  )
224cdf0e10cSrcweir 	throw (RuntimeException)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	return Addon_getImplementationName();
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
supportsService(const::rtl::OUString & rServiceName)229cdf0e10cSrcweir sal_Bool SAL_CALL Addon::supportsService( const ::rtl::OUString& rServiceName )
230cdf0e10cSrcweir 	throw (RuntimeException)
231cdf0e10cSrcweir {
232cdf0e10cSrcweir     return Addon_supportsService( rServiceName );
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
getSupportedServiceNames()235cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL Addon::getSupportedServiceNames(  )
236cdf0e10cSrcweir 	throw (RuntimeException)
237cdf0e10cSrcweir {
238cdf0e10cSrcweir     return Addon_getSupportedServiceNames();
239cdf0e10cSrcweir }
240