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