LibreOffice Module unotools (master) 1
useroptions.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
24#include <com/sun/star/uno/Any.hxx>
25#include "itemholder1.hxx"
26
28#include <com/sun/star/beans/Property.hpp>
29#include <com/sun/star/beans/XPropertySet.hpp>
30#include <com/sun/star/beans/PropertyAttribute.hpp>
31#include <com/sun/star/container/XNameAccess.hpp>
32#include <com/sun/star/util/XChangesListener.hpp>
33#include <com/sun/star/util/XChangesNotifier.hpp>
34#include <com/sun/star/util/ChangesEvent.hpp>
39#include <o3tl/enumarray.hxx>
40#include <o3tl/string_view.hxx>
42
43using namespace utl;
44using namespace com::sun::star;
45
46// vOptionNames[] -- names of the user option entries
47// The order must correspond to the enum class UserOptToken in useroptions.hxx.
49 "l", // UserOptToken::City
50 "o", // UserOptToken::Company
51 "c", // UserOptToken::Country
52 "mail", // UserOptToken::Email
53 "facsimiletelephonenumber", // UserOptToken::Fax
54 "givenname", // UserOptToken::FirstName
55 "sn", // UserOptToken::LastName
56 "position", // UserOptToken::Position
57 "st", // UserOptToken::State
58 "street", // UserOptToken::Street
59 "homephone", // UserOptToken::TelephoneHome
60 "telephonenumber", // UserOptToken::TelephoneWork
61 "title", // UserOptToken::Title
62 "initials", // UserOptToken::ID
63 "postalcode", // UserOptToken::Zip
64 "fathersname", // UserOptToken::FathersName
65 "apartment", // UserOptToken::Apartment
66 "signingkey", // UserOptToken::SigningKey
67 "encryptionkey", // UserOptToken::EncryptionKey
68 "encrypttoself" // UserOptToken::EncryptToSelf
69};
70
71std::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::xSharedImpl;
72
73class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper<util::XChangesListener>
74{
75public:
76 explicit ChangeListener (Impl& rParent): m_rParent(rParent) { }
77
78 // XChangesListener
79 virtual void SAL_CALL changesOccurred (util::ChangesEvent const& Event) override;
80 // XEventListener
81 virtual void SAL_CALL disposing (lang::EventObject const& Source) override;
82
83private:
85};
86
88{
89public:
90 Impl ();
91
92 OUString GetFullName () const;
93
95 OUString GetToken (UserOptToken nToken) const;
96 void SetToken (UserOptToken nToken, OUString const& rNewToken);
97 bool GetBoolValue (UserOptToken nToken) const;
98 void SetBoolValue (UserOptToken nToken, bool bNewValue);
99 void Notify ();
100
101private:
102 uno::Reference<util::XChangesListener> m_xChangeListener;
103 uno::Reference<container::XNameAccess> m_xCfg;
104 uno::Reference<beans::XPropertySet> m_xData;
105
106 template < typename ValueType >
107 ValueType GetValue_Impl( UserOptToken nToken ) const;
108 template < typename ValueType >
109 void SetValue_Impl( UserOptToken nToken, ValueType const& rNewValue );
110};
111
112void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent)
113{
114 if (rEvent.Changes.hasElements())
116}
117
118void SvtUserOptions::ChangeListener::disposing (lang::EventObject const& rSource)
119{
120 try
121 {
122 uno::Reference<util::XChangesNotifier> xChgNot(rSource.Source, uno::UNO_QUERY_THROW);
123 xChgNot->removeChangesListener(this);
124 }
125 catch (uno::Exception&)
126 {
127 }
128}
129
131 m_xChangeListener( new ChangeListener(*this) )
132{
133 try
134 {
135 m_xCfg.set(
138 "org.openoffice.UserProfile/Data",
140 ),
141 uno::UNO_QUERY
142 );
143
144 m_xData.set(m_xCfg, uno::UNO_QUERY);
145 uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY);
146 try
147 {
148 xChgNot->addChangesListener(m_xChangeListener);
149 }
150 catch (uno::RuntimeException&)
151 {
152 }
153 }
154 catch (uno::Exception const&)
155 {
156 DBG_UNHANDLED_EXCEPTION("unotools.config");
157 m_xCfg.clear();
158 }
159}
160
161template < typename ValueType >
163{
164 ValueType sToken = ValueType();
165 try
166 {
167 if (m_xData.is())
168 m_xData->getPropertyValue(OUString::createFromAscii(vOptionNames[nToken])) >>= sToken;
169 }
170 catch (uno::Exception const&)
171 {
172 DBG_UNHANDLED_EXCEPTION("unotools.config");
173 }
174 return sToken;
175}
176
177template < typename ValueType >
179{
180 try
181 {
182 if (m_xData.is())
183 m_xData->setPropertyValue(OUString::createFromAscii(vOptionNames[nToken]), uno::Any(sToken));
185 }
186 catch (uno::Exception const&)
187 {
188 DBG_UNHANDLED_EXCEPTION("unotools.config");
189 }
190}
191
193{
194 return GetValue_Impl<OUString>( nToken );
195}
196
197void SvtUserOptions::Impl::SetToken (UserOptToken nToken, OUString const& sToken)
198{
199 SetValue_Impl<OUString>( nToken, sToken );
200}
201
203{
204 return GetValue_Impl<bool>( nToken );
205}
206
208{
209 SetValue_Impl<bool>( nToken, bNewValue );
210}
211
213{
214 OUString sFullName;
216 if (eLang == LANGUAGE_RUSSIAN)
217 {
218 sFullName = GetToken(UserOptToken::FirstName).trim();
219 if (!sFullName.isEmpty())
220 sFullName += " ";
222 if (!sFullName.isEmpty())
223 sFullName += " ";
225 }
226 else
227 {
229 {
230 sFullName = GetToken(UserOptToken::LastName).trim();
231 if (!sFullName.isEmpty())
232 sFullName += " ";
234 }
235 else
236 {
237 sFullName = GetToken(UserOptToken::FirstName).trim();
238 if (!sFullName.isEmpty())
239 sFullName += " ";
241 }
242 }
243 sFullName = sFullName.trim();
244
245 return sFullName;
246}
247
249{
251}
252
254{
255 uno::Reference<beans::XPropertySet> xData(m_xCfg, uno::UNO_QUERY);
256 uno::Reference<beans::XPropertySetInfo> xInfo = xData->getPropertySetInfo();
257 beans::Property aProp = xInfo->getPropertyByName(OUString::createFromAscii(vOptionNames[nToken]));
258 return ((aProp.Attributes & beans::PropertyAttribute::READONLY) ==
259 beans::PropertyAttribute::READONLY);
260}
261
262static std::recursive_mutex& GetInitMutex()
263{
264 static std::recursive_mutex gMutex;
265 return gMutex;
266}
267
268
270{
271 // Global access, must be guarded (multithreading)
272 std::unique_lock aGuard(GetInitMutex());
273
274 xImpl = xSharedImpl.lock();
275 if (!xImpl)
276 {
277 xImpl = std::make_shared<Impl>();
279 aGuard.unlock(); // because holdConfigItem will call this constructor
281 }
282 xImpl->AddListener(this);
283}
284
286{
287 // Global access, must be guarded (multithreading)
288 std::unique_lock aGuard( GetInitMutex() );
289 xImpl->RemoveListener(this);
290}
291
295OUString SvtUserOptions::GetID () const { return GetToken(UserOptToken::ID); }
309
311{
312 std::unique_lock aGuard(GetInitMutex());
313 return xImpl->IsTokenReadonly(nToken);
314}
315
317{
318 std::unique_lock aGuard(GetInitMutex());
319 return xImpl->GetToken(nToken);
320}
321
322void SvtUserOptions::SetToken (UserOptToken nToken, OUString const& rNewToken)
323{
324 std::unique_lock aGuard(GetInitMutex());
325 xImpl->SetToken(nToken, rNewToken);
326}
327
328void SvtUserOptions::SetBoolValue (UserOptToken nToken, bool bNewValue)
329{
330 std::unique_lock aGuard(GetInitMutex());
331 xImpl->SetBoolValue(nToken, bNewValue);
332}
333
335{
336 std::unique_lock aGuard(GetInitMutex());
337 return xImpl->GetBoolValue(UserOptToken::EncryptToSelf);
338}
339
341{
342 std::unique_lock aGuard(GetInitMutex());
343 return xImpl->GetFullName();
344}
345
346/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void holdConfigItem(EItem eItem)
Definition: itemholder1.cxx:68
LanguageType getLanguageType(bool bResolveSystem=true) const
static bool isFamilyNameFirst(LanguageType nLang)
SvtSysLocale provides a refcounted single instance of an application wide LocaleDataWrapper and <type...
Definition: syslocale.hxx:44
const LanguageTag & GetUILanguageTag() const
Definition: syslocale.cxx:169
virtual void SAL_CALL disposing(lang::EventObject const &Source) override
virtual void SAL_CALL changesOccurred(util::ChangesEvent const &Event) override
bool IsTokenReadonly(UserOptToken nToken) const
uno::Reference< util::XChangesListener > m_xChangeListener
void SetBoolValue(UserOptToken nToken, bool bNewValue)
ValueType GetValue_Impl(UserOptToken nToken) const
void SetToken(UserOptToken nToken, OUString const &rNewToken)
uno::Reference< container::XNameAccess > m_xCfg
void SetValue_Impl(UserOptToken nToken, ValueType const &rNewValue)
bool GetBoolValue(UserOptToken nToken) const
OUString GetFullName() const
uno::Reference< beans::XPropertySet > m_xData
OUString GetToken(UserOptToken nToken) const
OUString GetZip() const
OUString GetFirstName() const
OUString GetSigningKey() const
OUString GetFullName() const
OUString GetID() const
OUString GetTelephoneHome() const
OUString GetCountry() const
bool IsTokenReadonly(UserOptToken nToken) const
bool GetEncryptToSelf() const
OUString GetEncryptionKey() const
OUString GetToken(UserOptToken nToken) const
OUString GetEmail() const
void SetBoolValue(UserOptToken nToken, bool bNewValue)
OUString GetTitle() const
OUString GetFax() const
OUString GetCity() const
virtual ~SvtUserOptions() override
OUString GetCompany() const
static std::weak_ptr< Impl > xSharedImpl
Definition: useroptions.hxx:93
std::shared_ptr< Impl > xImpl
Definition: useroptions.hxx:92
OUString GetPosition() const
void SetToken(UserOptToken nToken, OUString const &rNewToken)
OUString GetTelephoneWork() const
OUString GetStreet() const
OUString GetState() const
OUString GetLastName() const
static css::uno::Reference< css::uno::XInterface > openConfig(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &sPackage, EConfigurationModes eMode)
static void flush(const css::uno::Reference< css::uno::XInterface > &xCFG)
void NotifyListeners(ConfigurationHints nHint)
Definition: options.cxx:75
#define DBG_UNHANDLED_EXCEPTION(...)
std::u16string_view rNewToken
Definition: fontdefs.hxx:45
@ UserOptions
#define LANGUAGE_RUSSIAN
Reference< XComponentContext > getProcessComponentContext()
std::basic_string_view< charT, traits > trim(std::basic_string_view< charT, traits > str)
ValueType
DefTokenId nToken
static std::recursive_mutex & GetInitMutex()
static o3tl::enumarray< UserOptToken, char const * > vOptionNames
Definition: useroptions.cxx:48
UserOptToken
Definition: useroptions.hxx:31