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 "precompiled_desktop.hxx"
25 #include "sal/config.h"
26 
27 #include "boost/noncopyable.hpp"
28 #include "com/sun/star/awt/MessageBoxButtons.hpp"
29 #include "com/sun/star/awt/Rectangle.hpp"
30 #include "com/sun/star/awt/XMessageBox.hpp"
31 #include "com/sun/star/awt/XMessageBoxFactory.hpp"
32 #include "com/sun/star/awt/XWindowPeer.hpp"
33 #include "com/sun/star/beans/PropertyValue.hpp"
34 #include "com/sun/star/frame/DispatchDescriptor.hpp"
35 #include "com/sun/star/frame/XDesktop.hpp"
36 #include "com/sun/star/frame/XDispatch.hpp"
37 #include "com/sun/star/frame/XDispatchProvider.hpp"
38 #include "com/sun/star/frame/XFrame.hpp"
39 #include "com/sun/star/frame/XStatusListener.hpp"
40 #include "com/sun/star/lang/XComponent.hpp"
41 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
42 #include "com/sun/star/lang/XServiceInfo.hpp"
43 #include "com/sun/star/uno/DeploymentException.hpp"
44 #include "com/sun/star/uno/Exception.hpp"
45 #include "com/sun/star/uno/Reference.hxx"
46 #include "com/sun/star/uno/RuntimeException.hpp"
47 #include "com/sun/star/uno/Sequence.hxx"
48 #include "com/sun/star/uno/XComponentContext.hpp"
49 #include "com/sun/star/uno/XInterface.hpp"
50 #include "com/sun/star/util/URL.hpp"
51 #include "cppuhelper/factory.hxx"
52 #include "cppuhelper/implbase2.hxx"
53 #include "cppuhelper/implementationentry.hxx"
54 #include "cppuhelper/weak.hxx"
55 #include "osl/diagnose.h"
56 #include "rtl/ustring.h"
57 #include "rtl/ustring.hxx"
58 #include "sal/types.h"
59 #include "uno/lbnames.h"
60 
61 namespace {
62 
63 namespace css = com::sun::star;
64 
65 class Provider:
66     public cppu::WeakImplHelper2<
67         css::lang::XServiceInfo, css::frame::XDispatchProvider >,
68     private boost::noncopyable
69 {
70 public:
71     static css::uno::Reference< css::uno::XInterface > SAL_CALL static_create(
72         css::uno::Reference< css::uno::XComponentContext > const & xContext)
73         SAL_THROW((css::uno::Exception))
74     { return static_cast< cppu::OWeakObject * >(new Provider(xContext)); }
75 
76     static rtl::OUString SAL_CALL static_getImplementationName();
77 
78     static css::uno::Sequence< rtl::OUString > SAL_CALL
79     static_getSupportedServiceNames();
80 
81 private:
82     Provider(
83         css::uno::Reference< css::uno::XComponentContext > const & context):
84         context_(context) { OSL_ASSERT(context.is()); }
85 
86     virtual ~Provider() {}
87 
88     virtual rtl::OUString SAL_CALL getImplementationName()
89         throw (css::uno::RuntimeException)
90     { return static_getImplementationName(); }
91 
92     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
93         throw (css::uno::RuntimeException)
94     { return ServiceName == getSupportedServiceNames()[0]; } //TODO
95 
96     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
97     getSupportedServiceNames() throw (css::uno::RuntimeException)
98     { return static_getSupportedServiceNames(); }
99 
100     virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(
101         css::util::URL const &, rtl::OUString const &, sal_Int32)
102         throw (css::uno::RuntimeException);
103 
104     virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > >
105     SAL_CALL queryDispatches(
106         css::uno::Sequence< css::frame::DispatchDescriptor > const & Requests)
107         throw (css::uno::RuntimeException);
108 
109     css::uno::Reference< css::uno::XComponentContext > context_;
110 };
111 
112 rtl::OUString Provider::static_getImplementationName() {
113     return rtl::OUString(
114         RTL_CONSTASCII_USTRINGPARAM(
115             "com.sun.star.comp.test.deployment.passive_native"));
116 }
117 
118 css::uno::Sequence< rtl::OUString > Provider::static_getSupportedServiceNames()
119 {
120     rtl::OUString name(
121         RTL_CONSTASCII_USTRINGPARAM(
122             "com.sun.star.test.deployment.passive_native"));
123     return css::uno::Sequence< rtl::OUString >(&name, 1);
124 }
125 
126 css::uno::Reference< css::frame::XDispatch > Provider::queryDispatch(
127     css::util::URL const &, rtl::OUString const &, sal_Int32)
128     throw (css::uno::RuntimeException)
129 {
130     css::uno::Reference< css::frame::XDispatch > dispatch;
131     if (!(context_->getValueByName(
132               rtl::OUString(
133                   RTL_CONSTASCII_USTRINGPARAM(
134                       "/singletons/com.sun.star.test.deployment."
135                       "passive_native_singleton"))) >>=
136           dispatch) ||
137         !dispatch.is())
138     {
139         throw css::uno::DeploymentException(
140             rtl::OUString(
141                 RTL_CONSTASCII_USTRINGPARAM(
142                     "component context fails to supply singleton"
143                     " com.sun.star.test.deployment.passive_native_singleton of"
144                     " type com.sun.star.frame.XDispatch")),
145             context_);
146     }
147     return dispatch;
148 }
149 
150 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > >
151 Provider::queryDispatches(
152     css::uno::Sequence< css::frame::DispatchDescriptor > const & Requests)
153     throw (css::uno::RuntimeException)
154 {
155     css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > s(
156         Requests.getLength());
157     for (sal_Int32 i = 0; i < s.getLength(); ++i) {
158         s[i] = queryDispatch(
159             Requests[i].FeatureURL, Requests[i].FrameName,
160             Requests[i].SearchFlags);
161     }
162     return s;
163 }
164 
165 class Dispatch:
166     public cppu::WeakImplHelper2<
167         css::lang::XServiceInfo, css::frame::XDispatch >,
168     private boost::noncopyable
169 {
170 public:
171     static css::uno::Reference< css::uno::XInterface > SAL_CALL static_create(
172         css::uno::Reference< css::uno::XComponentContext > const & xContext)
173         SAL_THROW((css::uno::Exception))
174     { return static_cast< cppu::OWeakObject * >(new Dispatch(xContext)); }
175 
176     static rtl::OUString SAL_CALL static_getImplementationName();
177 
178     static css::uno::Sequence< rtl::OUString > SAL_CALL
179     static_getSupportedServiceNames()
180     { return css::uno::Sequence< rtl::OUString >(); }
181 
182 private:
183     Dispatch(
184         css::uno::Reference< css::uno::XComponentContext > const & context):
185         context_(context) { OSL_ASSERT(context.is()); }
186 
187     virtual ~Dispatch() {}
188 
189     virtual rtl::OUString SAL_CALL getImplementationName()
190         throw (css::uno::RuntimeException)
191     { return static_getImplementationName(); }
192 
193     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const &)
194         throw (css::uno::RuntimeException)
195     { return false; } //TODO
196 
197     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
198     getSupportedServiceNames() throw (css::uno::RuntimeException)
199     { return static_getSupportedServiceNames(); }
200 
201     virtual void SAL_CALL dispatch(
202         css::util::URL const &,
203         css::uno::Sequence< css::beans::PropertyValue > const &)
204         throw (css::uno::RuntimeException);
205 
206     virtual void SAL_CALL addStatusListener(
207         css::uno::Reference< css::frame::XStatusListener > const &,
208         css::util::URL const &)
209         throw (css::uno::RuntimeException)
210     {}
211 
212     virtual void SAL_CALL removeStatusListener(
213         css::uno::Reference< css::frame::XStatusListener > const &,
214         css::util::URL const &)
215         throw (css::uno::RuntimeException)
216     {}
217 
218     css::uno::Reference< css::uno::XComponentContext > context_;
219 };
220 
221 rtl::OUString Dispatch::static_getImplementationName() {
222     return rtl::OUString(
223         RTL_CONSTASCII_USTRINGPARAM(
224             "com.sun.star.comp.test.deployment.passive_native_singleton"));
225 }
226 
227 void Dispatch::dispatch(
228     css::util::URL const &,
229     css::uno::Sequence< css::beans::PropertyValue > const &)
230     throw (css::uno::RuntimeException)
231 {
232     css::uno::Reference< css::lang::XMultiComponentFactory > smgr(
233         context_->getServiceManager(), css::uno::UNO_SET_THROW);
234     css::uno::Reference< css::awt::XMessageBox > box(
235         css::uno::Reference< css::awt::XMessageBoxFactory >(
236             smgr->createInstanceWithContext(
237                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
238                                   "com.sun.star.awt.Toolkit")), context_),
239             css::uno::UNO_QUERY_THROW)->createMessageBox(
240                 css::uno::Reference< css::awt::XWindowPeer >(
241                     css::uno::Reference< css::frame::XFrame >(
242                         css::uno::Reference< css::frame::XDesktop >(
243                             smgr->createInstanceWithContext(
244                                 rtl::OUString(
245                                     RTL_CONSTASCII_USTRINGPARAM(
246                                         "com.sun.star.frame.Desktop")),
247                                 context_),
248                             css::uno::UNO_QUERY_THROW)->getCurrentFrame(),
249                         css::uno::UNO_SET_THROW)->getComponentWindow(),
250                     css::uno::UNO_QUERY_THROW),
251                 css::awt::Rectangle(),
252                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("infobox")),
253                 css::awt::MessageBoxButtons::BUTTONS_OK,
254                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("passive")),
255                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("native"))),
256         css::uno::UNO_SET_THROW);
257     box->execute();
258     css::uno::Reference< css::lang::XComponent >(
259         box, css::uno::UNO_QUERY_THROW)->dispose();
260 }
261 
262 static cppu::ImplementationEntry const services[] = {
263     { &Provider::static_create, &Provider::static_getImplementationName,
264       &Provider::static_getSupportedServiceNames,
265       &cppu::createSingleComponentFactory, 0, 0 },
266     { &Dispatch::static_create, &Dispatch::static_getImplementationName,
267       &Dispatch::static_getSupportedServiceNames,
268       &cppu::createSingleComponentFactory, 0, 0 },
269     { 0, 0, 0, 0, 0, 0 }
270 };
271 
272 }
273 
274 extern "C" void * SAL_CALL component_getFactory(
275     char const * pImplName, void * pServiceManager, void * pRegistryKey)
276 {
277     return cppu::component_getFactoryHelper(
278         pImplName, pServiceManager, pRegistryKey, services);
279 }
280 
281 extern "C" void SAL_CALL component_getImplementationEnvironment(
282     char const ** ppEnvTypeName, uno_Environment **)
283 {
284     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
285 }
286