1 /*************************************************************************
2 *
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 *
7 * OpenOffice.org - a multi-platform office productivity suite
8 *
9 * This file is part of OpenOffice.org.
10 *
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org.  If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 *
26 ************************************************************************/
27 
28 #include "precompiled_desktop.hxx"
29 #include "sal/config.h"
30 
31 #include "boost/noncopyable.hpp"
32 #include "com/sun/star/awt/MessageBoxButtons.hpp"
33 #include "com/sun/star/awt/Rectangle.hpp"
34 #include "com/sun/star/awt/XMessageBox.hpp"
35 #include "com/sun/star/awt/XMessageBoxFactory.hpp"
36 #include "com/sun/star/awt/XWindowPeer.hpp"
37 #include "com/sun/star/beans/PropertyValue.hpp"
38 #include "com/sun/star/frame/DispatchDescriptor.hpp"
39 #include "com/sun/star/frame/XDesktop.hpp"
40 #include "com/sun/star/frame/XDispatch.hpp"
41 #include "com/sun/star/frame/XDispatchProvider.hpp"
42 #include "com/sun/star/frame/XFrame.hpp"
43 #include "com/sun/star/frame/XStatusListener.hpp"
44 #include "com/sun/star/lang/XComponent.hpp"
45 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
46 #include "com/sun/star/lang/XServiceInfo.hpp"
47 #include "com/sun/star/uno/DeploymentException.hpp"
48 #include "com/sun/star/uno/Exception.hpp"
49 #include "com/sun/star/uno/Reference.hxx"
50 #include "com/sun/star/uno/RuntimeException.hpp"
51 #include "com/sun/star/uno/Sequence.hxx"
52 #include "com/sun/star/uno/XComponentContext.hpp"
53 #include "com/sun/star/uno/XInterface.hpp"
54 #include "com/sun/star/util/URL.hpp"
55 #include "cppuhelper/factory.hxx"
56 #include "cppuhelper/implbase2.hxx"
57 #include "cppuhelper/implementationentry.hxx"
58 #include "cppuhelper/weak.hxx"
59 #include "osl/diagnose.h"
60 #include "rtl/ustring.h"
61 #include "rtl/ustring.hxx"
62 #include "sal/types.h"
63 #include "uno/lbnames.h"
64 
65 namespace {
66 
67 namespace css = com::sun::star;
68 
69 class Provider:
70     public cppu::WeakImplHelper2<
71         css::lang::XServiceInfo, css::frame::XDispatchProvider >,
72     private boost::noncopyable
73 {
74 public:
75     static css::uno::Reference< css::uno::XInterface > SAL_CALL static_create(
76         css::uno::Reference< css::uno::XComponentContext > const & xContext)
77         SAL_THROW((css::uno::Exception))
78     { return static_cast< cppu::OWeakObject * >(new Provider(xContext)); }
79 
80     static rtl::OUString SAL_CALL static_getImplementationName();
81 
82     static css::uno::Sequence< rtl::OUString > SAL_CALL
83     static_getSupportedServiceNames();
84 
85 private:
86     Provider(
87         css::uno::Reference< css::uno::XComponentContext > const & context):
88         context_(context) { OSL_ASSERT(context.is()); }
89 
90     virtual ~Provider() {}
91 
92     virtual rtl::OUString SAL_CALL getImplementationName()
93         throw (css::uno::RuntimeException)
94     { return static_getImplementationName(); }
95 
96     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
97         throw (css::uno::RuntimeException)
98     { return ServiceName == getSupportedServiceNames()[0]; } //TODO
99 
100     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
101     getSupportedServiceNames() throw (css::uno::RuntimeException)
102     { return static_getSupportedServiceNames(); }
103 
104     virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(
105         css::util::URL const &, rtl::OUString const &, sal_Int32)
106         throw (css::uno::RuntimeException);
107 
108     virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > >
109     SAL_CALL queryDispatches(
110         css::uno::Sequence< css::frame::DispatchDescriptor > const & Requests)
111         throw (css::uno::RuntimeException);
112 
113     css::uno::Reference< css::uno::XComponentContext > context_;
114 };
115 
116 rtl::OUString Provider::static_getImplementationName() {
117     return rtl::OUString(
118         RTL_CONSTASCII_USTRINGPARAM(
119             "com.sun.star.comp.test.deployment.passive_native"));
120 }
121 
122 css::uno::Sequence< rtl::OUString > Provider::static_getSupportedServiceNames()
123 {
124     rtl::OUString name(
125         RTL_CONSTASCII_USTRINGPARAM(
126             "com.sun.star.test.deployment.passive_native"));
127     return css::uno::Sequence< rtl::OUString >(&name, 1);
128 }
129 
130 css::uno::Reference< css::frame::XDispatch > Provider::queryDispatch(
131     css::util::URL const &, rtl::OUString const &, sal_Int32)
132     throw (css::uno::RuntimeException)
133 {
134     css::uno::Reference< css::frame::XDispatch > dispatch;
135     if (!(context_->getValueByName(
136               rtl::OUString(
137                   RTL_CONSTASCII_USTRINGPARAM(
138                       "/singletons/com.sun.star.test.deployment."
139                       "passive_native_singleton"))) >>=
140           dispatch) ||
141         !dispatch.is())
142     {
143         throw css::uno::DeploymentException(
144             rtl::OUString(
145                 RTL_CONSTASCII_USTRINGPARAM(
146                     "component context fails to supply singleton"
147                     " com.sun.star.test.deployment.passive_native_singleton of"
148                     " type com.sun.star.frame.XDispatch")),
149             context_);
150     }
151     return dispatch;
152 }
153 
154 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > >
155 Provider::queryDispatches(
156     css::uno::Sequence< css::frame::DispatchDescriptor > const & Requests)
157     throw (css::uno::RuntimeException)
158 {
159     css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > s(
160         Requests.getLength());
161     for (sal_Int32 i = 0; i < s.getLength(); ++i) {
162         s[i] = queryDispatch(
163             Requests[i].FeatureURL, Requests[i].FrameName,
164             Requests[i].SearchFlags);
165     }
166     return s;
167 }
168 
169 class Dispatch:
170     public cppu::WeakImplHelper2<
171         css::lang::XServiceInfo, css::frame::XDispatch >,
172     private boost::noncopyable
173 {
174 public:
175     static css::uno::Reference< css::uno::XInterface > SAL_CALL static_create(
176         css::uno::Reference< css::uno::XComponentContext > const & xContext)
177         SAL_THROW((css::uno::Exception))
178     { return static_cast< cppu::OWeakObject * >(new Dispatch(xContext)); }
179 
180     static rtl::OUString SAL_CALL static_getImplementationName();
181 
182     static css::uno::Sequence< rtl::OUString > SAL_CALL
183     static_getSupportedServiceNames()
184     { return css::uno::Sequence< rtl::OUString >(); }
185 
186 private:
187     Dispatch(
188         css::uno::Reference< css::uno::XComponentContext > const & context):
189         context_(context) { OSL_ASSERT(context.is()); }
190 
191     virtual ~Dispatch() {}
192 
193     virtual rtl::OUString SAL_CALL getImplementationName()
194         throw (css::uno::RuntimeException)
195     { return static_getImplementationName(); }
196 
197     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const &)
198         throw (css::uno::RuntimeException)
199     { return false; } //TODO
200 
201     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
202     getSupportedServiceNames() throw (css::uno::RuntimeException)
203     { return static_getSupportedServiceNames(); }
204 
205     virtual void SAL_CALL dispatch(
206         css::util::URL const &,
207         css::uno::Sequence< css::beans::PropertyValue > const &)
208         throw (css::uno::RuntimeException);
209 
210     virtual void SAL_CALL addStatusListener(
211         css::uno::Reference< css::frame::XStatusListener > const &,
212         css::util::URL const &)
213         throw (css::uno::RuntimeException)
214     {}
215 
216     virtual void SAL_CALL removeStatusListener(
217         css::uno::Reference< css::frame::XStatusListener > const &,
218         css::util::URL const &)
219         throw (css::uno::RuntimeException)
220     {}
221 
222     css::uno::Reference< css::uno::XComponentContext > context_;
223 };
224 
225 rtl::OUString Dispatch::static_getImplementationName() {
226     return rtl::OUString(
227         RTL_CONSTASCII_USTRINGPARAM(
228             "com.sun.star.comp.test.deployment.passive_native_singleton"));
229 }
230 
231 void Dispatch::dispatch(
232     css::util::URL const &,
233     css::uno::Sequence< css::beans::PropertyValue > const &)
234     throw (css::uno::RuntimeException)
235 {
236     css::uno::Reference< css::lang::XMultiComponentFactory > smgr(
237         context_->getServiceManager(), css::uno::UNO_SET_THROW);
238     css::uno::Reference< css::awt::XMessageBox > box(
239         css::uno::Reference< css::awt::XMessageBoxFactory >(
240             smgr->createInstanceWithContext(
241                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
242                                   "com.sun.star.awt.Toolkit")), context_),
243             css::uno::UNO_QUERY_THROW)->createMessageBox(
244                 css::uno::Reference< css::awt::XWindowPeer >(
245                     css::uno::Reference< css::frame::XFrame >(
246                         css::uno::Reference< css::frame::XDesktop >(
247                             smgr->createInstanceWithContext(
248                                 rtl::OUString(
249                                     RTL_CONSTASCII_USTRINGPARAM(
250                                         "com.sun.star.frame.Desktop")),
251                                 context_),
252                             css::uno::UNO_QUERY_THROW)->getCurrentFrame(),
253                         css::uno::UNO_SET_THROW)->getComponentWindow(),
254                     css::uno::UNO_QUERY_THROW),
255                 css::awt::Rectangle(),
256                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("infobox")),
257                 css::awt::MessageBoxButtons::BUTTONS_OK,
258                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("passive")),
259                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("native"))),
260         css::uno::UNO_SET_THROW);
261     box->execute();
262     css::uno::Reference< css::lang::XComponent >(
263         box, css::uno::UNO_QUERY_THROW)->dispose();
264 }
265 
266 static cppu::ImplementationEntry const services[] = {
267     { &Provider::static_create, &Provider::static_getImplementationName,
268       &Provider::static_getSupportedServiceNames,
269       &cppu::createSingleComponentFactory, 0, 0 },
270     { &Dispatch::static_create, &Dispatch::static_getImplementationName,
271       &Dispatch::static_getSupportedServiceNames,
272       &cppu::createSingleComponentFactory, 0, 0 },
273     { 0, 0, 0, 0, 0, 0 }
274 };
275 
276 }
277 
278 extern "C" void * SAL_CALL component_getFactory(
279     char const * pImplName, void * pServiceManager, void * pRegistryKey)
280 {
281     return cppu::component_getFactoryHelper(
282         pImplName, pServiceManager, pRegistryKey, services);
283 }
284 
285 extern "C" void SAL_CALL component_getImplementationEnvironment(
286     char const ** ppEnvTypeName, uno_Environment **)
287 {
288     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
289 }
290