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_shell.hxx"
29 #include "sal/config.h"
30 
31 #include <cstddef>
32 
33 #include "boost/noncopyable.hpp"
34 #include "com/sun/star/beans/Optional.hpp"
35 #include "com/sun/star/beans/PropertyVetoException.hpp"
36 #include "com/sun/star/beans/UnknownPropertyException.hpp"
37 #include "com/sun/star/beans/XPropertyChangeListener.hpp"
38 #include "com/sun/star/beans/XPropertySet.hpp"
39 #include "com/sun/star/beans/XPropertySetInfo.hpp"
40 #include "com/sun/star/beans/XVetoableChangeListener.hpp"
41 #include "com/sun/star/lang/IllegalArgumentException.hpp"
42 #include "com/sun/star/lang/WrappedTargetException.hpp"
43 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
44 #include "com/sun/star/lang/XServiceInfo.hpp"
45 #include "com/sun/star/lang/WrappedTargetException.hpp"
46 #include "com/sun/star/uno/Any.hxx"
47 #include "com/sun/star/uno/Reference.hxx"
48 #include "com/sun/star/uno/RuntimeException.hpp"
49 #include "com/sun/star/uno/Sequence.hxx"
50 #include "com/sun/star/uno/XComponentContext.hpp"
51 #include "com/sun/star/uno/XCurrentContext.hpp"
52 #include "cppuhelper/factory.hxx"
53 #include "cppuhelper/implbase2.hxx"
54 #include "cppuhelper/implementationentry.hxx"
55 #include "cppuhelper/weak.hxx"
56 #include "rtl/string.h"
57 #include "rtl/ustring.h"
58 #include "rtl/ustring.hxx"
59 #include "sal/types.h"
60 #include "uno/current_context.hxx"
61 #include "uno/lbnames.h"
62 
63 #include "gconfaccess.hxx"
64 #include "orbit.h"
65 
66 namespace {
67 
68 namespace css = com::sun::star;
69 
70 rtl::OUString SAL_CALL getServiceImplementationName() {
71     return rtl::OUString(
72         RTL_CONSTASCII_USTRINGPARAM(
73             "com.sun.star.comp.configuration.backend.GconfBackend"));
74 }
75 
76 css::uno::Sequence< rtl::OUString > SAL_CALL getServiceSupportedServiceNames() {
77     rtl::OUString name(
78         RTL_CONSTASCII_USTRINGPARAM(
79             "com.sun.star.configuration.backend.GconfBackend"));
80     return css::uno::Sequence< rtl::OUString >(&name, 1);
81 }
82 
83 class Service:
84     public cppu::WeakImplHelper2<
85         css::lang::XServiceInfo, css::beans::XPropertySet >,
86     private boost::noncopyable
87 {
88 public:
89     Service();
90 
91 private:
92     virtual ~Service() {}
93 
94     virtual rtl::OUString SAL_CALL getImplementationName()
95         throw (css::uno::RuntimeException)
96     { return getServiceImplementationName(); }
97 
98     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
99         throw (css::uno::RuntimeException)
100     { return ServiceName == getSupportedServiceNames()[0]; }
101 
102     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
103     getSupportedServiceNames() throw (css::uno::RuntimeException)
104     { return getServiceSupportedServiceNames(); }
105 
106     virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL
107     getPropertySetInfo() throw (css::uno::RuntimeException)
108     { return css::uno::Reference< css::beans::XPropertySetInfo >(); }
109 
110     virtual void SAL_CALL setPropertyValue(
111         rtl::OUString const &, css::uno::Any const &)
112         throw (
113             css::beans::UnknownPropertyException,
114             css::beans::PropertyVetoException,
115             css::lang::IllegalArgumentException,
116             css::lang::WrappedTargetException, css::uno::RuntimeException);
117 
118     virtual css::uno::Any SAL_CALL getPropertyValue(
119         rtl::OUString const & PropertyName)
120         throw (
121             css::beans::UnknownPropertyException,
122             css::lang::WrappedTargetException, css::uno::RuntimeException);
123 
124     virtual void SAL_CALL addPropertyChangeListener(
125         rtl::OUString const &,
126         css::uno::Reference< css::beans::XPropertyChangeListener > const &)
127         throw (
128             css::beans::UnknownPropertyException,
129             css::lang::WrappedTargetException, css::uno::RuntimeException)
130     {}
131 
132     virtual void SAL_CALL removePropertyChangeListener(
133         rtl::OUString const &,
134         css::uno::Reference< css::beans::XPropertyChangeListener > const &)
135         throw (
136             css::beans::UnknownPropertyException,
137             css::lang::WrappedTargetException, css::uno::RuntimeException)
138     {}
139 
140     virtual void SAL_CALL addVetoableChangeListener(
141         rtl::OUString const &,
142         css::uno::Reference< css::beans::XVetoableChangeListener > const &)
143         throw (
144             css::beans::UnknownPropertyException,
145             css::lang::WrappedTargetException, css::uno::RuntimeException)
146     {}
147 
148     virtual void SAL_CALL removeVetoableChangeListener(
149         rtl::OUString const &,
150         css::uno::Reference< css::beans::XVetoableChangeListener > const &)
151         throw (
152             css::beans::UnknownPropertyException,
153             css::lang::WrappedTargetException, css::uno::RuntimeException)
154     {}
155 
156     bool enabled_;
157 };
158 
159 Service::Service(): enabled_(false) {
160     css::uno::Reference< css::uno::XCurrentContext > context(
161         css::uno::getCurrentContext());
162     if (context.is()) {
163         rtl::OUString desktop;
164         context->getValueByName(
165             rtl::OUString(
166                 RTL_CONSTASCII_USTRINGPARAM("system.desktop-environment"))) >>=
167             desktop;
168         enabled_ = desktop.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("GNOME")) &&
169             ((orbit_major_version == 2 && orbit_minor_version >= 8) ||
170              orbit_major_version > 2);
171             // ORBit-2 versions < 2.8 cause a deadlock with the gtk+ VCL plugin
172     }
173 }
174 
175 void Service::setPropertyValue(rtl::OUString const &, css::uno::Any const &)
176     throw (
177         css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
178         css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
179         css::uno::RuntimeException)
180 {
181     throw css::lang::IllegalArgumentException(
182         rtl::OUString(
183             RTL_CONSTASCII_USTRINGPARAM("setPropertyValue not supported")),
184         static_cast< cppu::OWeakObject * >(this), -1);
185 }
186 
187 css::uno::Any Service::getPropertyValue(rtl::OUString const & PropertyName)
188     throw (
189         css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
190         css::uno::RuntimeException)
191 {
192     for (std::size_t i = 0; i < gconfaccess::nConfigurationValues; ++i) {
193         if (PropertyName.equalsAscii(
194                 gconfaccess::ConfigurationValues[i].OOoConfItem))
195         {
196             return css::uno::makeAny(
197                 enabled_
198                 ? gconfaccess::getValue(gconfaccess::ConfigurationValues[i])
199                 : css::beans::Optional< css::uno::Any >());
200         }
201     }
202     throw css::beans::UnknownPropertyException(
203         PropertyName, static_cast< cppu::OWeakObject * >(this));
204 }
205 
206 css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
207     css::uno::Reference< css::uno::XComponentContext > const &)
208 {
209     return static_cast< cppu::OWeakObject * >(new Service);
210 }
211 
212 static cppu::ImplementationEntry const services[] = {
213     { &createInstance, &getServiceImplementationName,
214       &getServiceSupportedServiceNames, &cppu::createSingleComponentFactory, 0,
215       0 },
216     { 0, 0, 0, 0, 0, 0 }
217 };
218 
219 }
220 
221 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
222     char const * pImplName, void * pServiceManager, void * pRegistryKey)
223 {
224     return cppu::component_getFactoryHelper(
225         pImplName, pServiceManager, pRegistryKey, services);
226 }
227 
228 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL
229 component_getImplementationEnvironment(
230     char const ** ppEnvTypeName, uno_Environment **)
231 {
232     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
233 }
234