Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / config / useroptions.cxx
blob5da601831adbee78731f3c3056bdf1fe83e916dc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <unotools/useroptions.hxx>
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"
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>
44 using namespace utl;
45 using namespace com::sun::star;
46 //using namespace com::sun::star::uno;
47 using rtl::OUString;
49 //namespace css = ::com::sun::star;
51 namespace
54 // vOptionNames[] -- names of the user option entries
55 // The order corresponds to the #define USER_OPT_* list in useroptions.hxx.
56 char const * const vOptionNames[] = {
57 "l", // USER_OPT_CITY
58 "o", // USER_OPT_COMPANY
59 "c", // USER_OPT_COUNTRY
60 "mail", // USER_OPT_EMAIL
61 "facsimiletelephonenumber", // USER_OPT_FAX
62 "givenname", // USER_OPT_FIRSTNAME
63 "sn", // USER_OPT_LASTNAME
64 "position", // USER_OPT_POSITION
65 "st", // USER_OPT_STATE
66 "street", // USER_OPT_STREET
67 "homephone", // USER_OPT_TELEPHONEHOME
68 "telephonenumber", // USER_OPT_TELEPHONEWORK
69 "title", // USER_OPT_TITLE
70 "initials", // USER_OPT_ID
71 "postalcode", // USER_OPT_ZIP
72 "fathersname", // USER_OPT_FATHERSNAME
73 "apartment", // USER_OPT_APARTMENT
74 "customernumber" // USER_OPT_CUSTOMERNUMBER
76 const sal_uInt16 nOptionNameCount = SAL_N_ELEMENTS(vOptionNames);
78 } // namespace
80 boost::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::pSharedImpl;
82 // class ChangeListener --------------------------------------------------
84 class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper1<util::XChangesListener>
86 public:
87 ChangeListener (Impl& rParent): m_rParent(rParent) { }
89 // XChangesListener
90 virtual void SAL_CALL changesOccurred (util::ChangesEvent const& Event) throw(uno::RuntimeException);
91 // XEventListener
92 virtual void SAL_CALL disposing (lang::EventObject const& Source) throw(uno::RuntimeException);
94 private:
95 Impl& m_rParent;
98 // class Impl ------------------------------------------------------------
100 class SvtUserOptions::Impl : public utl::ConfigurationBroadcaster
102 public:
103 Impl ();
105 OUString GetFullName () const;
107 sal_Bool IsTokenReadonly (sal_uInt16 nToken) const;
108 OUString GetToken (sal_uInt16 nToken) const;
109 void SetToken (sal_uInt16 nToken, OUString const& rNewToken);
110 void Notify ();
112 private:
113 uno::Reference<util::XChangesListener> m_xChangeListener;
114 uno::Reference<container::XNameAccess> m_xCfg;
115 uno::Reference<beans::XPropertySet> m_xData;
118 // class SvtUserOptions::ChangeListener ----------------------------------
120 void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent) throw(uno::RuntimeException)
122 if (rEvent.Changes.getLength())
123 m_rParent.Notify();
126 void SvtUserOptions::ChangeListener::disposing (lang::EventObject const& rSource) throw(uno::RuntimeException)
130 uno::Reference<util::XChangesNotifier> xChgNot(rSource.Source, uno::UNO_QUERY_THROW);
131 xChgNot->removeChangesListener(this);
133 catch (uno::Exception&)
138 // class SvtUserOptions::Impl --------------------------------------------
140 SvtUserOptions::Impl::Impl() :
141 m_xChangeListener( new ChangeListener(*this) )
145 m_xCfg = uno::Reference<container::XNameAccess>(
146 comphelper::ConfigurationHelper::openConfig(
147 comphelper::getProcessComponentContext(),
148 "org.openoffice.UserProfile/Data",
149 comphelper::ConfigurationHelper::E_STANDARD
151 uno::UNO_QUERY
154 m_xData = uno::Reference<beans::XPropertySet>(m_xCfg, uno::UNO_QUERY);
155 uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY);
158 xChgNot->addChangesListener(m_xChangeListener);
160 catch (uno::RuntimeException&)
164 catch (uno::Exception const& ex)
166 m_xCfg.clear();
167 SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
171 // -----------------------------------------------------------------------
173 OUString SvtUserOptions::Impl::GetToken (sal_uInt16 nToken) const
175 OUString sToken;
176 if (nToken < nOptionNameCount)
180 if (m_xData.is())
181 m_xData->getPropertyValue(OUString::createFromAscii(vOptionNames[nToken])) >>= sToken;
183 catch (uno::Exception const& ex)
185 SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
188 else
189 SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
190 return sToken;
193 // -----------------------------------------------------------------------
195 void SvtUserOptions::Impl::SetToken (sal_uInt16 nToken, OUString const& sToken)
197 if (nToken < nOptionNameCount)
201 if (m_xData.is())
202 m_xData->setPropertyValue(OUString::createFromAscii(vOptionNames[nToken]), uno::makeAny(sToken));
203 comphelper::ConfigurationHelper::flush(m_xCfg);
205 catch (uno::Exception const& ex)
207 SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
210 else
211 SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
214 // -----------------------------------------------------------------------
216 OUString SvtUserOptions::Impl::GetFullName () const
218 // TODO international name
219 OUString sFullName = GetToken(USER_OPT_FIRSTNAME).trim();
220 if (!sFullName.isEmpty())
221 sFullName += " ";
222 sFullName += GetToken(USER_OPT_LASTNAME).trim();
223 return sFullName;
226 // -----------------------------------------------------------------------
228 void SvtUserOptions::Impl::Notify ()
230 NotifyListeners(0);
233 // -----------------------------------------------------------------------
235 sal_Bool SvtUserOptions::Impl::IsTokenReadonly (sal_uInt16 nToken) const
237 if (nToken < nOptionNameCount)
239 uno::Reference<beans::XPropertySet> xData(m_xCfg, uno::UNO_QUERY);
240 uno::Reference<beans::XPropertySetInfo> xInfo = xData->getPropertySetInfo();
241 beans::Property aProp = xInfo->getPropertyByName(OUString::createFromAscii(vOptionNames[nToken]));
242 return ((aProp.Attributes & beans::PropertyAttribute::READONLY) ==
243 beans::PropertyAttribute::READONLY);
245 else
247 SAL_WARN("unotools.config", "SvtUserOptions::Impl::IsTokenReadonly(): invalid token");
248 return sal_False;
252 // class SvtUserOptions --------------------------------------------------
254 SvtUserOptions::SvtUserOptions ()
256 // Global access, must be guarded (multithreading)
257 osl::MutexGuard aGuard(GetInitMutex());
259 if (pSharedImpl.expired())
261 RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) SvtUserOptions::Impl::ctor()");
262 pImpl.reset(new Impl);
263 pSharedImpl = pImpl;
264 ItemHolder1::holdConfigItem(E_USEROPTIONS);
266 pImpl = pSharedImpl.lock();
267 pImpl->AddListener(this);
270 // -----------------------------------------------------------------------
272 SvtUserOptions::~SvtUserOptions()
274 // Global access, must be guarded (multithreading)
275 osl::MutexGuard aGuard( GetInitMutex() );
276 pImpl->RemoveListener(this);
279 // -----------------------------------------------------------------------
281 namespace
283 class theUserOptionsMutex : public rtl::Static<osl::Mutex, theUserOptionsMutex>{};
286 osl::Mutex& SvtUserOptions::GetInitMutex()
288 return theUserOptionsMutex::get();
291 // -----------------------------------------------------------------------
294 // getters
296 OUString SvtUserOptions::GetCompany () const { return GetToken(USER_OPT_COMPANY); }
297 OUString SvtUserOptions::GetFirstName () const { return GetToken(USER_OPT_FIRSTNAME); }
298 OUString SvtUserOptions::GetLastName () const { return GetToken(USER_OPT_LASTNAME); }
299 OUString SvtUserOptions::GetID () const { return GetToken(USER_OPT_ID); }
300 OUString SvtUserOptions::GetStreet () const { return GetToken(USER_OPT_STREET); }
301 OUString SvtUserOptions::GetCity () const { return GetToken(USER_OPT_CITY); }
302 OUString SvtUserOptions::GetState () const { return GetToken(USER_OPT_STATE); }
303 OUString SvtUserOptions::GetZip () const { return GetToken(USER_OPT_ZIP); }
304 OUString SvtUserOptions::GetCountry () const { return GetToken(USER_OPT_COUNTRY); }
305 OUString SvtUserOptions::GetPosition () const { return GetToken(USER_OPT_POSITION); }
306 OUString SvtUserOptions::GetTitle () const { return GetToken(USER_OPT_TITLE); }
307 OUString SvtUserOptions::GetTelephoneHome () const { return GetToken(USER_OPT_TELEPHONEHOME); }
308 OUString SvtUserOptions::GetTelephoneWork () const { return GetToken(USER_OPT_TELEPHONEWORK); }
309 OUString SvtUserOptions::GetFax () const { return GetToken(USER_OPT_FAX); }
310 OUString SvtUserOptions::GetEmail () const { return GetToken(USER_OPT_EMAIL); }
311 OUString SvtUserOptions::GetCustomerNumber () const { return GetToken(USER_OPT_CUSTOMERNUMBER); }
313 // -----------------------------------------------------------------------
316 // setters
318 void SvtUserOptions::SetCustomerNumber (OUString const& sToken) { SetToken(USER_OPT_CUSTOMERNUMBER, sToken); }
320 // -----------------------------------------------------------------------
322 sal_Bool SvtUserOptions::IsTokenReadonly (sal_uInt16 nToken) const
324 osl::MutexGuard aGuard(GetInitMutex());
325 return pImpl->IsTokenReadonly(nToken);
328 // -----------------------------------------------------------------------
330 OUString SvtUserOptions::GetToken (sal_uInt16 nToken) const
332 osl::MutexGuard aGuard(GetInitMutex());
333 return pImpl->GetToken(nToken);
336 // -----------------------------------------------------------------------
338 void SvtUserOptions::SetToken (sal_uInt16 nToken, OUString const& rNewToken)
340 osl::MutexGuard aGuard(GetInitMutex());
341 pImpl->SetToken(nToken, rNewToken);
344 // -----------------------------------------------------------------------
346 OUString SvtUserOptions::GetFullName () const
348 osl::MutexGuard aGuard(GetInitMutex());
349 return pImpl->GetFullName();
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */