Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <unotools/useroptions.hxx>
22 :
23 : #include <unotools/configmgr.hxx>
24 : #include <com/sun/star/uno/Any.hxx>
25 : #include <com/sun/star/uno/Sequence.hxx>
26 : #include <tools/solar.h>
27 : #include <osl/mutex.hxx>
28 : #include <rtl/instance.hxx>
29 : #include <rtl/logfile.hxx>
30 : #include "itemholder1.hxx"
31 :
32 : #include <com/sun/star/beans/Property.hpp>
33 : #include <com/sun/star/beans/XPropertySet.hpp>
34 : #include <com/sun/star/beans/PropertyAttribute.hpp>
35 : #include <com/sun/star/container/XNameAccess.hpp>
36 : #include <com/sun/star/container/XNameContainer.hpp>
37 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
38 : #include <com/sun/star/util/XChangesListener.hpp>
39 : #include <com/sun/star/util/XChangesNotifier.hpp>
40 : #include <com/sun/star/util/ChangesEvent.hpp>
41 : #include <comphelper/configurationhelper.hxx>
42 : #include <comphelper/processfactory.hxx>
43 :
44 : using namespace utl;
45 : using namespace com::sun::star;
46 :
47 : namespace
48 : {
49 :
50 : // vOptionNames[] -- names of the user option entries
51 : // The order corresponds to the #define USER_OPT_* list in useroptions.hxx.
52 : char const * const vOptionNames[] = {
53 : "l", // USER_OPT_CITY
54 : "o", // USER_OPT_COMPANY
55 : "c", // USER_OPT_COUNTRY
56 : "mail", // USER_OPT_EMAIL
57 : "facsimiletelephonenumber", // USER_OPT_FAX
58 : "givenname", // USER_OPT_FIRSTNAME
59 : "sn", // USER_OPT_LASTNAME
60 : "position", // USER_OPT_POSITION
61 : "st", // USER_OPT_STATE
62 : "street", // USER_OPT_STREET
63 : "homephone", // USER_OPT_TELEPHONEHOME
64 : "telephonenumber", // USER_OPT_TELEPHONEWORK
65 : "title", // USER_OPT_TITLE
66 : "initials", // USER_OPT_ID
67 : "postalcode", // USER_OPT_ZIP
68 : "fathersname", // USER_OPT_FATHERSNAME
69 : "apartment", // USER_OPT_APARTMENT
70 : "customernumber" // USER_OPT_CUSTOMERNUMBER
71 : };
72 : const sal_uInt16 nOptionNameCount = SAL_N_ELEMENTS(vOptionNames);
73 :
74 : } // namespace
75 :
76 159 : boost::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::pSharedImpl;
77 :
78 : // class ChangeListener --------------------------------------------------
79 :
80 82 : class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper1<util::XChangesListener>
81 : {
82 : public:
83 52 : ChangeListener (Impl& rParent): m_rParent(rParent) { }
84 :
85 : // XChangesListener
86 : virtual void SAL_CALL changesOccurred (util::ChangesEvent const& Event) throw(uno::RuntimeException);
87 : // XEventListener
88 : virtual void SAL_CALL disposing (lang::EventObject const& Source) throw(uno::RuntimeException);
89 :
90 : private:
91 : Impl& m_rParent;
92 : };
93 :
94 : // class Impl ------------------------------------------------------------
95 :
96 82 : class SvtUserOptions::Impl : public utl::ConfigurationBroadcaster
97 : {
98 : public:
99 : Impl ();
100 :
101 : OUString GetFullName () const;
102 :
103 : sal_Bool IsTokenReadonly (sal_uInt16 nToken) const;
104 : OUString GetToken (sal_uInt16 nToken) const;
105 : void SetToken (sal_uInt16 nToken, OUString const& rNewToken);
106 : void Notify ();
107 :
108 : private:
109 : uno::Reference<util::XChangesListener> m_xChangeListener;
110 : uno::Reference<container::XNameAccess> m_xCfg;
111 : uno::Reference<beans::XPropertySet> m_xData;
112 : };
113 :
114 : // class SvtUserOptions::ChangeListener ----------------------------------
115 :
116 0 : void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent) throw(uno::RuntimeException)
117 : {
118 0 : if (rEvent.Changes.getLength())
119 0 : m_rParent.Notify();
120 0 : }
121 :
122 0 : void SvtUserOptions::ChangeListener::disposing (lang::EventObject const& rSource) throw(uno::RuntimeException)
123 : {
124 : try
125 : {
126 0 : uno::Reference<util::XChangesNotifier> xChgNot(rSource.Source, uno::UNO_QUERY_THROW);
127 0 : xChgNot->removeChangesListener(this);
128 : }
129 0 : catch (uno::Exception&)
130 : {
131 : }
132 0 : }
133 :
134 : // class SvtUserOptions::Impl --------------------------------------------
135 :
136 52 : SvtUserOptions::Impl::Impl() :
137 52 : m_xChangeListener( new ChangeListener(*this) )
138 : {
139 : try
140 : {
141 104 : m_xCfg = uno::Reference<container::XNameAccess>(
142 : comphelper::ConfigurationHelper::openConfig(
143 : comphelper::getProcessComponentContext(),
144 : "org.openoffice.UserProfile/Data",
145 : comphelper::ConfigurationHelper::E_STANDARD
146 : ),
147 : uno::UNO_QUERY
148 52 : );
149 :
150 52 : m_xData = uno::Reference<beans::XPropertySet>(m_xCfg, uno::UNO_QUERY);
151 52 : uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY);
152 : try
153 : {
154 52 : xChgNot->addChangesListener(m_xChangeListener);
155 : }
156 0 : catch (uno::RuntimeException&)
157 : {
158 52 : }
159 : }
160 0 : catch (uno::Exception const& ex)
161 : {
162 0 : m_xCfg.clear();
163 : SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
164 : }
165 52 : }
166 :
167 : // -----------------------------------------------------------------------
168 :
169 3719 : OUString SvtUserOptions::Impl::GetToken (sal_uInt16 nToken) const
170 : {
171 3719 : OUString sToken;
172 3719 : if (nToken < nOptionNameCount)
173 : {
174 : try
175 : {
176 3719 : if (m_xData.is())
177 3719 : m_xData->getPropertyValue(OUString::createFromAscii(vOptionNames[nToken])) >>= sToken;
178 : }
179 0 : catch (uno::Exception const& ex)
180 : {
181 : SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
182 : }
183 : }
184 : else
185 : SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
186 3719 : return sToken;
187 : }
188 :
189 : // -----------------------------------------------------------------------
190 :
191 0 : void SvtUserOptions::Impl::SetToken (sal_uInt16 nToken, OUString const& sToken)
192 : {
193 0 : if (nToken < nOptionNameCount)
194 : {
195 : try
196 : {
197 0 : if (m_xData.is())
198 0 : m_xData->setPropertyValue(OUString::createFromAscii(vOptionNames[nToken]), uno::makeAny(sToken));
199 0 : comphelper::ConfigurationHelper::flush(m_xCfg);
200 : }
201 0 : catch (uno::Exception const& ex)
202 : {
203 : SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
204 : }
205 : }
206 : else
207 : SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
208 0 : }
209 :
210 : // -----------------------------------------------------------------------
211 :
212 1436 : OUString SvtUserOptions::Impl::GetFullName () const
213 : {
214 : // TODO international name
215 1436 : OUString sFullName = GetToken(USER_OPT_FIRSTNAME).trim();
216 1436 : if (!sFullName.isEmpty())
217 0 : sFullName += " ";
218 1436 : sFullName += GetToken(USER_OPT_LASTNAME).trim();
219 1436 : return sFullName;
220 : }
221 :
222 : // -----------------------------------------------------------------------
223 :
224 0 : void SvtUserOptions::Impl::Notify ()
225 : {
226 0 : NotifyListeners(0);
227 0 : }
228 :
229 : // -----------------------------------------------------------------------
230 :
231 0 : sal_Bool SvtUserOptions::Impl::IsTokenReadonly (sal_uInt16 nToken) const
232 : {
233 0 : if (nToken < nOptionNameCount)
234 : {
235 0 : uno::Reference<beans::XPropertySet> xData(m_xCfg, uno::UNO_QUERY);
236 0 : uno::Reference<beans::XPropertySetInfo> xInfo = xData->getPropertySetInfo();
237 0 : beans::Property aProp = xInfo->getPropertyByName(OUString::createFromAscii(vOptionNames[nToken]));
238 0 : return ((aProp.Attributes & beans::PropertyAttribute::READONLY) ==
239 0 : beans::PropertyAttribute::READONLY);
240 : }
241 : else
242 : {
243 : SAL_WARN("unotools.config", "SvtUserOptions::Impl::IsTokenReadonly(): invalid token");
244 0 : return sal_False;
245 : }
246 : }
247 :
248 : // class SvtUserOptions --------------------------------------------------
249 :
250 890 : SvtUserOptions::SvtUserOptions ()
251 : {
252 : // Global access, must be guarded (multithreading)
253 890 : osl::MutexGuard aGuard(GetInitMutex());
254 :
255 890 : if (pSharedImpl.expired())
256 : {
257 : RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) SvtUserOptions::Impl::ctor()");
258 52 : pImpl.reset(new Impl);
259 52 : pSharedImpl = pImpl;
260 52 : ItemHolder1::holdConfigItem(E_USEROPTIONS);
261 : }
262 890 : pImpl = pSharedImpl.lock();
263 890 : pImpl->AddListener(this);
264 890 : }
265 :
266 : // -----------------------------------------------------------------------
267 :
268 1827 : SvtUserOptions::~SvtUserOptions()
269 : {
270 : // Global access, must be guarded (multithreading)
271 878 : osl::MutexGuard aGuard( GetInitMutex() );
272 878 : pImpl->RemoveListener(this);
273 949 : }
274 :
275 : // -----------------------------------------------------------------------
276 :
277 : namespace
278 : {
279 : class theUserOptionsMutex : public rtl::Static<osl::Mutex, theUserOptionsMutex>{};
280 : }
281 :
282 4051 : osl::Mutex& SvtUserOptions::GetInitMutex()
283 : {
284 4051 : return theUserOptionsMutex::get();
285 : }
286 :
287 : // -----------------------------------------------------------------------
288 :
289 : //
290 : // getters
291 : //
292 20 : OUString SvtUserOptions::GetCompany () const { return GetToken(USER_OPT_COMPANY); }
293 126 : OUString SvtUserOptions::GetFirstName () const { return GetToken(USER_OPT_FIRSTNAME); }
294 181 : OUString SvtUserOptions::GetLastName () const { return GetToken(USER_OPT_LASTNAME); }
295 204 : OUString SvtUserOptions::GetID () const { return GetToken(USER_OPT_ID); }
296 20 : OUString SvtUserOptions::GetStreet () const { return GetToken(USER_OPT_STREET); }
297 20 : OUString SvtUserOptions::GetCity () const { return GetToken(USER_OPT_CITY); }
298 0 : OUString SvtUserOptions::GetState () const { return GetToken(USER_OPT_STATE); }
299 0 : OUString SvtUserOptions::GetZip () const { return GetToken(USER_OPT_ZIP); }
300 0 : OUString SvtUserOptions::GetCountry () const { return GetToken(USER_OPT_COUNTRY); }
301 20 : OUString SvtUserOptions::GetPosition () const { return GetToken(USER_OPT_POSITION); }
302 20 : OUString SvtUserOptions::GetTitle () const { return GetToken(USER_OPT_TITLE); }
303 0 : OUString SvtUserOptions::GetTelephoneHome () const { return GetToken(USER_OPT_TELEPHONEHOME); }
304 0 : OUString SvtUserOptions::GetTelephoneWork () const { return GetToken(USER_OPT_TELEPHONEWORK); }
305 0 : OUString SvtUserOptions::GetFax () const { return GetToken(USER_OPT_FAX); }
306 20 : OUString SvtUserOptions::GetEmail () const { return GetToken(USER_OPT_EMAIL); }
307 0 : OUString SvtUserOptions::GetCustomerNumber () const { return GetToken(USER_OPT_CUSTOMERNUMBER); }
308 :
309 : // -----------------------------------------------------------------------
310 :
311 : //
312 : // setters
313 : //
314 0 : void SvtUserOptions::SetCustomerNumber (OUString const& sToken) { SetToken(USER_OPT_CUSTOMERNUMBER, sToken); }
315 :
316 : // -----------------------------------------------------------------------
317 :
318 0 : sal_Bool SvtUserOptions::IsTokenReadonly (sal_uInt16 nToken) const
319 : {
320 0 : osl::MutexGuard aGuard(GetInitMutex());
321 0 : return pImpl->IsTokenReadonly(nToken);
322 : }
323 :
324 : // -----------------------------------------------------------------------
325 :
326 847 : OUString SvtUserOptions::GetToken (sal_uInt16 nToken) const
327 : {
328 847 : osl::MutexGuard aGuard(GetInitMutex());
329 847 : return pImpl->GetToken(nToken);
330 : }
331 :
332 : // -----------------------------------------------------------------------
333 :
334 0 : void SvtUserOptions::SetToken (sal_uInt16 nToken, OUString const& rNewToken)
335 : {
336 0 : osl::MutexGuard aGuard(GetInitMutex());
337 0 : pImpl->SetToken(nToken, rNewToken);
338 0 : }
339 :
340 : // -----------------------------------------------------------------------
341 :
342 1436 : OUString SvtUserOptions::GetFullName () const
343 : {
344 1436 : osl::MutexGuard aGuard(GetInitMutex());
345 1436 : return pImpl->GetFullName();
346 477 : }
347 :
348 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|