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