1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <unotools/useroptions.hxx>
23 #include <unotools/syslocale.hxx>
24 #include <com/sun/star/uno/Any.hxx>
25 #include "itemholder1.hxx"
27 #include <cppuhelper/implbase.hxx>
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>
35 #include <comphelper/configurationhelper.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <i18nlangtag/mslangid.hxx>
38 #include <i18nlangtag/languagetag.hxx>
39 #include <o3tl/enumarray.hxx>
40 #include <o3tl/string_view.hxx>
41 #include <comphelper/diagnose_ex.hxx>
44 using namespace com::sun::star
;
46 // vOptionNames[] -- names of the user option entries
47 // The order must correspond to the enum class UserOptToken in useroptions.hxx.
48 static o3tl::enumarray
<UserOptToken
, char const *> vOptionNames
= {
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
71 std::weak_ptr
<SvtUserOptions::Impl
> SvtUserOptions::xSharedImpl
;
73 class SvtUserOptions::ChangeListener
: public cppu::WeakImplHelper
<util::XChangesListener
>
76 explicit ChangeListener (Impl
& rParent
): m_rParent(rParent
) { }
79 virtual void SAL_CALL
changesOccurred (util::ChangesEvent
const& Event
) override
;
81 virtual void SAL_CALL
disposing (lang::EventObject
const& Source
) override
;
87 class SvtUserOptions::Impl
: public utl::ConfigurationBroadcaster
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 bool GetBoolValue (UserOptToken nToken
) const;
98 void SetBoolValue (UserOptToken nToken
, bool bNewValue
);
102 uno::Reference
<util::XChangesListener
> m_xChangeListener
;
103 uno::Reference
<container::XNameAccess
> m_xCfg
;
104 uno::Reference
<beans::XPropertySet
> m_xData
;
106 template < typename ValueType
>
107 ValueType
GetValue_Impl( UserOptToken nToken
) const;
108 template < typename ValueType
>
109 void SetValue_Impl( UserOptToken nToken
, ValueType
const& rNewValue
);
112 void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent
const& rEvent
)
114 if (rEvent
.Changes
.hasElements())
118 void SvtUserOptions::ChangeListener::disposing (lang::EventObject
const& rSource
)
122 uno::Reference
<util::XChangesNotifier
> xChgNot(rSource
.Source
, uno::UNO_QUERY_THROW
);
123 xChgNot
->removeChangesListener(this);
125 catch (uno::Exception
&)
130 SvtUserOptions::Impl::Impl() :
131 m_xChangeListener( new ChangeListener(*this) )
136 comphelper::ConfigurationHelper::openConfig(
137 comphelper::getProcessComponentContext(),
138 "org.openoffice.UserProfile/Data",
139 comphelper::EConfigurationModes::Standard
144 m_xData
.set(m_xCfg
, uno::UNO_QUERY
);
145 uno::Reference
<util::XChangesNotifier
> xChgNot(m_xCfg
, uno::UNO_QUERY
);
148 xChgNot
->addChangesListener(m_xChangeListener
);
150 catch (uno::RuntimeException
&)
154 catch (uno::Exception
const&)
156 DBG_UNHANDLED_EXCEPTION("unotools.config");
161 template < typename ValueType
>
162 ValueType
SvtUserOptions::Impl::GetValue_Impl (UserOptToken nToken
) const
164 ValueType sToken
= ValueType();
168 m_xData
->getPropertyValue(OUString::createFromAscii(vOptionNames
[nToken
])) >>= sToken
;
170 catch (uno::Exception
const&)
172 DBG_UNHANDLED_EXCEPTION("unotools.config");
177 template < typename ValueType
>
178 void SvtUserOptions::Impl::SetValue_Impl (UserOptToken nToken
, ValueType
const& sToken
)
183 m_xData
->setPropertyValue(OUString::createFromAscii(vOptionNames
[nToken
]), uno::Any(sToken
));
184 comphelper::ConfigurationHelper::flush(m_xCfg
);
186 catch (uno::Exception
const&)
188 DBG_UNHANDLED_EXCEPTION("unotools.config");
192 OUString
SvtUserOptions::Impl::GetToken (UserOptToken nToken
) const
194 return GetValue_Impl
<OUString
>( nToken
);
197 void SvtUserOptions::Impl::SetToken (UserOptToken nToken
, OUString
const& sToken
)
199 SetValue_Impl
<OUString
>( nToken
, sToken
);
202 bool SvtUserOptions::Impl::GetBoolValue (UserOptToken nToken
) const
204 return GetValue_Impl
<bool>( nToken
);
207 void SvtUserOptions::Impl::SetBoolValue (UserOptToken nToken
, bool bNewValue
)
209 SetValue_Impl
<bool>( nToken
, bNewValue
);
212 OUString
SvtUserOptions::Impl::GetFullName () const
215 LanguageType
const eLang
= SvtSysLocale().GetUILanguageTag().getLanguageType();
216 if (eLang
== LANGUAGE_RUSSIAN
)
218 sFullName
= GetToken(UserOptToken::FirstName
).trim();
219 if (!sFullName
.isEmpty())
221 sFullName
+= o3tl::trim(GetToken(UserOptToken::FathersName
));
222 if (!sFullName
.isEmpty())
224 sFullName
+= o3tl::trim(GetToken(UserOptToken::LastName
));
228 if (MsLangId::isFamilyNameFirst(eLang
))
230 sFullName
= GetToken(UserOptToken::LastName
).trim();
231 if (!sFullName
.isEmpty())
233 sFullName
+= o3tl::trim(GetToken(UserOptToken::FirstName
));
237 sFullName
= GetToken(UserOptToken::FirstName
).trim();
238 if (!sFullName
.isEmpty())
240 sFullName
+= o3tl::trim(GetToken(UserOptToken::LastName
));
243 sFullName
= sFullName
.trim();
248 void SvtUserOptions::Impl::Notify ()
250 NotifyListeners(ConfigurationHints::NONE
);
253 bool SvtUserOptions::Impl::IsTokenReadonly (UserOptToken nToken
) const
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
);
262 static std::recursive_mutex
& GetInitMutex()
264 static std::recursive_mutex gMutex
;
269 SvtUserOptions::SvtUserOptions ()
271 // Global access, must be guarded (multithreading)
272 std::unique_lock
aGuard(GetInitMutex());
274 xImpl
= xSharedImpl
.lock();
277 xImpl
= std::make_shared
<Impl
>();
279 aGuard
.unlock(); // because holdConfigItem will call this constructor
280 ItemHolder1::holdConfigItem(EItem::UserOptions
);
282 xImpl
->AddListener(this);
285 SvtUserOptions::~SvtUserOptions()
287 // Global access, must be guarded (multithreading)
288 std::unique_lock
aGuard( GetInitMutex() );
289 xImpl
->RemoveListener(this);
292 OUString
SvtUserOptions::GetCompany () const { return GetToken(UserOptToken::Company
); }
293 OUString
SvtUserOptions::GetFirstName () const { return GetToken(UserOptToken::FirstName
); }
294 OUString
SvtUserOptions::GetLastName () const { return GetToken(UserOptToken::LastName
); }
295 OUString
SvtUserOptions::GetID () const { return GetToken(UserOptToken::ID
); }
296 OUString
SvtUserOptions::GetStreet () const { return GetToken(UserOptToken::Street
); }
297 OUString
SvtUserOptions::GetCity () const { return GetToken(UserOptToken::City
); }
298 OUString
SvtUserOptions::GetState () const { return GetToken(UserOptToken::State
); }
299 OUString
SvtUserOptions::GetZip () const { return GetToken(UserOptToken::Zip
); }
300 OUString
SvtUserOptions::GetCountry () const { return GetToken(UserOptToken::Country
); }
301 OUString
SvtUserOptions::GetPosition () const { return GetToken(UserOptToken::Position
); }
302 OUString
SvtUserOptions::GetTitle () const { return GetToken(UserOptToken::Title
); }
303 OUString
SvtUserOptions::GetTelephoneHome () const { return GetToken(UserOptToken::TelephoneHome
); }
304 OUString
SvtUserOptions::GetTelephoneWork () const { return GetToken(UserOptToken::TelephoneWork
); }
305 OUString
SvtUserOptions::GetFax () const { return GetToken(UserOptToken::Fax
); }
306 OUString
SvtUserOptions::GetEmail () const { return GetToken(UserOptToken::Email
); }
307 OUString
SvtUserOptions::GetSigningKey () const { return GetToken(UserOptToken::SigningKey
); }
308 OUString
SvtUserOptions::GetEncryptionKey () const { return GetToken(UserOptToken::EncryptionKey
); }
310 bool SvtUserOptions::IsTokenReadonly (UserOptToken nToken
) const
312 std::unique_lock
aGuard(GetInitMutex());
313 return xImpl
->IsTokenReadonly(nToken
);
316 OUString
SvtUserOptions::GetToken (UserOptToken nToken
) const
318 std::unique_lock
aGuard(GetInitMutex());
319 return xImpl
->GetToken(nToken
);
322 void SvtUserOptions::SetToken (UserOptToken nToken
, OUString
const& rNewToken
)
324 std::unique_lock
aGuard(GetInitMutex());
325 xImpl
->SetToken(nToken
, rNewToken
);
328 void SvtUserOptions::SetBoolValue (UserOptToken nToken
, bool bNewValue
)
330 std::unique_lock
aGuard(GetInitMutex());
331 xImpl
->SetBoolValue(nToken
, bNewValue
);
334 bool SvtUserOptions::GetEncryptToSelf() const
336 std::unique_lock
aGuard(GetInitMutex());
337 return xImpl
->GetBoolValue(UserOptToken::EncryptToSelf
);
340 OUString
SvtUserOptions::GetFullName () const
342 std::unique_lock
aGuard(GetInitMutex());
343 return xImpl
->GetFullName();
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */