update emoji autocorrect entries from po-files
[LibreOffice.git] / unotools / source / config / useroptions.cxx
blob1e0efb8817e39cf80c52f6c362a1b678b90a9a2a
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 .
20 #include <sal/config.h>
22 #include <sal/log.hxx>
23 #include <unotools/useroptions.hxx>
24 #include <unotools/syslocale.hxx>
25 #include <unotools/configmgr.hxx>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <osl/mutex.hxx>
29 #include <rtl/instance.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>
43 #include <i18nlangtag/mslangid.hxx>
44 #include <o3tl/enumarray.hxx>
46 using namespace utl;
47 using namespace com::sun::star;
49 // vOptionNames[] -- names of the user option entries
50 // The order corresponds to the #define USER_OPT_* list in useroptions.hxx.
51 static o3tl::enumarray<UserOptToken, char const *> vOptionNames = {
52 "l", // UserOptToken::City
53 "o", // UserOptToken::Company
54 "c", // UserOptToken::Country
55 "mail", // UserOptToken::Email
56 "facsimiletelephonenumber", // UserOptToken::Fax
57 "givenname", // UserOptToken::FirstName
58 "sn", // UserOptToken::LastName
59 "position", // UserOptToken::Position
60 "st", // UserOptToken::State
61 "street", // UserOptToken::Street
62 "homephone", // UserOptToken::TelephoneHome
63 "telephonenumber", // UserOptToken::TelephoneWork
64 "title", // UserOptToken::Title
65 "initials", // UserOptToken::ID
66 "postalcode", // UserOptToken::Zip
67 "fathersname", // UserOptToken::FathersName
68 "apartment" // UserOptToken::Apartment
71 std::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::xSharedImpl;
73 class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper1<util::XChangesListener>
75 public:
76 ChangeListener (Impl& rParent): m_rParent(rParent) { }
78 // XChangesListener
79 virtual void SAL_CALL changesOccurred (util::ChangesEvent const& Event) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE;
80 // XEventListener
81 virtual void SAL_CALL disposing (lang::EventObject const& Source) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE;
83 private:
84 Impl& m_rParent;
87 class SvtUserOptions::Impl : public utl::ConfigurationBroadcaster
89 public:
90 Impl ();
92 OUString GetFullName () const;
94 bool IsTokenReadonly (UserOptToken nToken) const;
95 OUString GetToken (UserOptToken nToken) const;
96 void SetToken (UserOptToken nToken, OUString const& rNewToken);
97 void Notify ();
99 private:
100 uno::Reference<util::XChangesListener> m_xChangeListener;
101 uno::Reference<container::XNameAccess> m_xCfg;
102 uno::Reference<beans::XPropertySet> m_xData;
105 void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent) throw(uno::RuntimeException, std::exception)
107 if (rEvent.Changes.getLength())
108 m_rParent.Notify();
111 void SvtUserOptions::ChangeListener::disposing (lang::EventObject const& rSource) throw(uno::RuntimeException, std::exception)
115 uno::Reference<util::XChangesNotifier> xChgNot(rSource.Source, uno::UNO_QUERY_THROW);
116 xChgNot->removeChangesListener(this);
118 catch (uno::Exception&)
123 SvtUserOptions::Impl::Impl() :
124 m_xChangeListener( new ChangeListener(*this) )
128 m_xCfg = uno::Reference<container::XNameAccess>(
129 comphelper::ConfigurationHelper::openConfig(
130 comphelper::getProcessComponentContext(),
131 "org.openoffice.UserProfile/Data",
132 comphelper::ConfigurationHelper::E_STANDARD
134 uno::UNO_QUERY
137 m_xData = uno::Reference<beans::XPropertySet>(m_xCfg, uno::UNO_QUERY);
138 uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY);
141 xChgNot->addChangesListener(m_xChangeListener);
143 catch (uno::RuntimeException&)
147 catch (uno::Exception const& ex)
149 m_xCfg.clear();
150 SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
154 OUString SvtUserOptions::Impl::GetToken (UserOptToken nToken) const
156 OUString sToken;
159 if (m_xData.is())
160 m_xData->getPropertyValue(OUString::createFromAscii(vOptionNames[nToken])) >>= sToken;
162 catch (uno::Exception const& ex)
164 SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
166 return sToken;
169 void SvtUserOptions::Impl::SetToken (UserOptToken nToken, OUString const& sToken)
173 if (m_xData.is())
174 m_xData->setPropertyValue(OUString::createFromAscii(vOptionNames[nToken]), uno::makeAny(sToken));
175 comphelper::ConfigurationHelper::flush(m_xCfg);
177 catch (uno::Exception const& ex)
179 SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
183 OUString SvtUserOptions::Impl::GetFullName () const
185 OUString sFullName;
186 switch (LanguageType const eLang = SvtSysLocale().GetUILanguageTag().getLanguageType())
188 case LANGUAGE_RUSSIAN:
189 sFullName = GetToken(UserOptToken::FirstName).trim();
190 if (!sFullName.isEmpty())
191 sFullName += " ";
192 sFullName += GetToken(UserOptToken::FathersName).trim();
193 if (!sFullName.isEmpty())
194 sFullName += " ";
195 sFullName += GetToken(UserOptToken::LastName).trim();
196 break;
197 default:
198 if (MsLangId::isFamilyNameFirst(eLang))
200 sFullName = GetToken(UserOptToken::LastName).trim();
201 if (!sFullName.isEmpty())
202 sFullName += " ";
203 sFullName += GetToken(UserOptToken::FirstName).trim();
205 else
207 sFullName = GetToken(UserOptToken::FirstName).trim();
208 if (!sFullName.isEmpty())
209 sFullName += " ";
210 sFullName += GetToken(UserOptToken::LastName).trim();
212 break;
215 return sFullName;
218 void SvtUserOptions::Impl::Notify ()
220 NotifyListeners(0);
223 bool SvtUserOptions::Impl::IsTokenReadonly (UserOptToken nToken) const
225 uno::Reference<beans::XPropertySet> xData(m_xCfg, uno::UNO_QUERY);
226 uno::Reference<beans::XPropertySetInfo> xInfo = xData->getPropertySetInfo();
227 beans::Property aProp = xInfo->getPropertyByName(OUString::createFromAscii(vOptionNames[nToken]));
228 return ((aProp.Attributes & beans::PropertyAttribute::READONLY) ==
229 beans::PropertyAttribute::READONLY);
232 SvtUserOptions::SvtUserOptions ()
234 // Global access, must be guarded (multithreading)
235 osl::MutexGuard aGuard(GetInitMutex());
237 if (xSharedImpl.expired())
239 xImpl.reset(new Impl);
240 xSharedImpl = xImpl;
241 ItemHolder1::holdConfigItem(E_USEROPTIONS);
243 xImpl = xSharedImpl.lock();
244 xImpl->AddListener(this);
247 SvtUserOptions::~SvtUserOptions()
249 // Global access, must be guarded (multithreading)
250 osl::MutexGuard aGuard( GetInitMutex() );
251 xImpl->RemoveListener(this);
254 namespace
256 class theUserOptionsMutex : public rtl::Static<osl::Mutex, theUserOptionsMutex>{};
259 osl::Mutex& SvtUserOptions::GetInitMutex()
261 return theUserOptionsMutex::get();
264 OUString SvtUserOptions::GetCompany () const { return GetToken(UserOptToken::Company); }
265 OUString SvtUserOptions::GetFirstName () const { return GetToken(UserOptToken::FirstName); }
266 OUString SvtUserOptions::GetLastName () const { return GetToken(UserOptToken::LastName); }
267 OUString SvtUserOptions::GetID () const { return GetToken(UserOptToken::ID); }
268 OUString SvtUserOptions::GetStreet () const { return GetToken(UserOptToken::Street); }
269 OUString SvtUserOptions::GetCity () const { return GetToken(UserOptToken::City); }
270 OUString SvtUserOptions::GetState () const { return GetToken(UserOptToken::State); }
271 OUString SvtUserOptions::GetZip () const { return GetToken(UserOptToken::Zip); }
272 OUString SvtUserOptions::GetCountry () const { return GetToken(UserOptToken::Country); }
273 OUString SvtUserOptions::GetPosition () const { return GetToken(UserOptToken::Position); }
274 OUString SvtUserOptions::GetTitle () const { return GetToken(UserOptToken::Title); }
275 OUString SvtUserOptions::GetTelephoneHome () const { return GetToken(UserOptToken::TelephoneHome); }
276 OUString SvtUserOptions::GetTelephoneWork () const { return GetToken(UserOptToken::TelephoneWork); }
277 OUString SvtUserOptions::GetFax () const { return GetToken(UserOptToken::Fax); }
278 OUString SvtUserOptions::GetEmail () const { return GetToken(UserOptToken::Email); }
280 bool SvtUserOptions::IsTokenReadonly (UserOptToken nToken) const
282 osl::MutexGuard aGuard(GetInitMutex());
283 return xImpl->IsTokenReadonly(nToken);
286 OUString SvtUserOptions::GetToken (UserOptToken nToken) const
288 osl::MutexGuard aGuard(GetInitMutex());
289 return xImpl->GetToken(nToken);
292 void SvtUserOptions::SetToken (UserOptToken nToken, OUString const& rNewToken)
294 osl::MutexGuard aGuard(GetInitMutex());
295 xImpl->SetToken(nToken, rNewToken);
298 OUString SvtUserOptions::GetFullName () const
300 osl::MutexGuard aGuard(GetInitMutex());
301 return xImpl->GetFullName();
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */