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 .
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>
45 using namespace com::sun::star
;
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
[] = {
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
72 const sal_uInt16 nOptionNameCount
= SAL_N_ELEMENTS(vOptionNames
);
76 boost::weak_ptr
<SvtUserOptions::Impl
> SvtUserOptions::pSharedImpl
;
78 // class ChangeListener --------------------------------------------------
80 class SvtUserOptions::ChangeListener
: public cppu::WeakImplHelper1
<util::XChangesListener
>
83 ChangeListener (Impl
& rParent
): m_rParent(rParent
) { }
86 virtual void SAL_CALL
changesOccurred (util::ChangesEvent
const& Event
) throw(uno::RuntimeException
);
88 virtual void SAL_CALL
disposing (lang::EventObject
const& Source
) throw(uno::RuntimeException
);
94 // class Impl ------------------------------------------------------------
96 class SvtUserOptions::Impl
: public utl::ConfigurationBroadcaster
101 OUString
GetFullName () const;
103 sal_Bool
IsTokenReadonly (sal_uInt16 nToken
) const;
104 OUString
GetToken (sal_uInt16 nToken
) const;
105 void SetToken (sal_uInt16 nToken
, OUString
const& rNewToken
);
109 uno::Reference
<util::XChangesListener
> m_xChangeListener
;
110 uno::Reference
<container::XNameAccess
> m_xCfg
;
111 uno::Reference
<beans::XPropertySet
> m_xData
;
114 // class SvtUserOptions::ChangeListener ----------------------------------
116 void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent
const& rEvent
) throw(uno::RuntimeException
)
118 if (rEvent
.Changes
.getLength())
122 void SvtUserOptions::ChangeListener::disposing (lang::EventObject
const& rSource
) throw(uno::RuntimeException
)
126 uno::Reference
<util::XChangesNotifier
> xChgNot(rSource
.Source
, uno::UNO_QUERY_THROW
);
127 xChgNot
->removeChangesListener(this);
129 catch (uno::Exception
&)
134 // class SvtUserOptions::Impl --------------------------------------------
136 SvtUserOptions::Impl::Impl() :
137 m_xChangeListener( new ChangeListener(*this) )
141 m_xCfg
= uno::Reference
<container::XNameAccess
>(
142 comphelper::ConfigurationHelper::openConfig(
143 comphelper::getProcessComponentContext(),
144 "org.openoffice.UserProfile/Data",
145 comphelper::ConfigurationHelper::E_STANDARD
150 m_xData
= uno::Reference
<beans::XPropertySet
>(m_xCfg
, uno::UNO_QUERY
);
151 uno::Reference
<util::XChangesNotifier
> xChgNot(m_xCfg
, uno::UNO_QUERY
);
154 xChgNot
->addChangesListener(m_xChangeListener
);
156 catch (uno::RuntimeException
&)
160 catch (uno::Exception
const& ex
)
163 SAL_WARN("unotools.config", "Caught unexpected: " << ex
.Message
);
167 // -----------------------------------------------------------------------
169 OUString
SvtUserOptions::Impl::GetToken (sal_uInt16 nToken
) const
172 if (nToken
< nOptionNameCount
)
177 m_xData
->getPropertyValue(OUString::createFromAscii(vOptionNames
[nToken
])) >>= sToken
;
179 catch (uno::Exception
const& ex
)
181 SAL_WARN("unotools.config", "Caught unexpected: " << ex
.Message
);
185 SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
189 // -----------------------------------------------------------------------
191 void SvtUserOptions::Impl::SetToken (sal_uInt16 nToken
, OUString
const& sToken
)
193 if (nToken
< nOptionNameCount
)
198 m_xData
->setPropertyValue(OUString::createFromAscii(vOptionNames
[nToken
]), uno::makeAny(sToken
));
199 comphelper::ConfigurationHelper::flush(m_xCfg
);
201 catch (uno::Exception
const& ex
)
203 SAL_WARN("unotools.config", "Caught unexpected: " << ex
.Message
);
207 SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
210 // -----------------------------------------------------------------------
212 OUString
SvtUserOptions::Impl::GetFullName () const
214 // TODO international name
215 OUString sFullName
= GetToken(USER_OPT_FIRSTNAME
).trim();
216 if (!sFullName
.isEmpty())
218 sFullName
+= GetToken(USER_OPT_LASTNAME
).trim();
222 // -----------------------------------------------------------------------
224 void SvtUserOptions::Impl::Notify ()
229 // -----------------------------------------------------------------------
231 sal_Bool
SvtUserOptions::Impl::IsTokenReadonly (sal_uInt16 nToken
) const
233 if (nToken
< nOptionNameCount
)
235 uno::Reference
<beans::XPropertySet
> xData(m_xCfg
, uno::UNO_QUERY
);
236 uno::Reference
<beans::XPropertySetInfo
> xInfo
= xData
->getPropertySetInfo();
237 beans::Property aProp
= xInfo
->getPropertyByName(OUString::createFromAscii(vOptionNames
[nToken
]));
238 return ((aProp
.Attributes
& beans::PropertyAttribute::READONLY
) ==
239 beans::PropertyAttribute::READONLY
);
243 SAL_WARN("unotools.config", "SvtUserOptions::Impl::IsTokenReadonly(): invalid token");
248 // class SvtUserOptions --------------------------------------------------
250 SvtUserOptions::SvtUserOptions ()
252 // Global access, must be guarded (multithreading)
253 osl::MutexGuard
aGuard(GetInitMutex());
255 if (pSharedImpl
.expired())
257 RTL_LOGFILE_CONTEXT(aLog
, "unotools ( ??? ) SvtUserOptions::Impl::ctor()");
258 pImpl
.reset(new Impl
);
260 ItemHolder1::holdConfigItem(E_USEROPTIONS
);
262 pImpl
= pSharedImpl
.lock();
263 pImpl
->AddListener(this);
266 // -----------------------------------------------------------------------
268 SvtUserOptions::~SvtUserOptions()
270 // Global access, must be guarded (multithreading)
271 osl::MutexGuard
aGuard( GetInitMutex() );
272 pImpl
->RemoveListener(this);
275 // -----------------------------------------------------------------------
279 class theUserOptionsMutex
: public rtl::Static
<osl::Mutex
, theUserOptionsMutex
>{};
282 osl::Mutex
& SvtUserOptions::GetInitMutex()
284 return theUserOptionsMutex::get();
287 // -----------------------------------------------------------------------
292 OUString
SvtUserOptions::GetCompany () const { return GetToken(USER_OPT_COMPANY
); }
293 OUString
SvtUserOptions::GetFirstName () const { return GetToken(USER_OPT_FIRSTNAME
); }
294 OUString
SvtUserOptions::GetLastName () const { return GetToken(USER_OPT_LASTNAME
); }
295 OUString
SvtUserOptions::GetID () const { return GetToken(USER_OPT_ID
); }
296 OUString
SvtUserOptions::GetStreet () const { return GetToken(USER_OPT_STREET
); }
297 OUString
SvtUserOptions::GetCity () const { return GetToken(USER_OPT_CITY
); }
298 OUString
SvtUserOptions::GetState () const { return GetToken(USER_OPT_STATE
); }
299 OUString
SvtUserOptions::GetZip () const { return GetToken(USER_OPT_ZIP
); }
300 OUString
SvtUserOptions::GetCountry () const { return GetToken(USER_OPT_COUNTRY
); }
301 OUString
SvtUserOptions::GetPosition () const { return GetToken(USER_OPT_POSITION
); }
302 OUString
SvtUserOptions::GetTitle () const { return GetToken(USER_OPT_TITLE
); }
303 OUString
SvtUserOptions::GetTelephoneHome () const { return GetToken(USER_OPT_TELEPHONEHOME
); }
304 OUString
SvtUserOptions::GetTelephoneWork () const { return GetToken(USER_OPT_TELEPHONEWORK
); }
305 OUString
SvtUserOptions::GetFax () const { return GetToken(USER_OPT_FAX
); }
306 OUString
SvtUserOptions::GetEmail () const { return GetToken(USER_OPT_EMAIL
); }
307 OUString
SvtUserOptions::GetCustomerNumber () const { return GetToken(USER_OPT_CUSTOMERNUMBER
); }
309 // -----------------------------------------------------------------------
314 void SvtUserOptions::SetCustomerNumber (OUString
const& sToken
) { SetToken(USER_OPT_CUSTOMERNUMBER
, sToken
); }
316 // -----------------------------------------------------------------------
318 sal_Bool
SvtUserOptions::IsTokenReadonly (sal_uInt16 nToken
) const
320 osl::MutexGuard
aGuard(GetInitMutex());
321 return pImpl
->IsTokenReadonly(nToken
);
324 // -----------------------------------------------------------------------
326 OUString
SvtUserOptions::GetToken (sal_uInt16 nToken
) const
328 osl::MutexGuard
aGuard(GetInitMutex());
329 return pImpl
->GetToken(nToken
);
332 // -----------------------------------------------------------------------
334 void SvtUserOptions::SetToken (sal_uInt16 nToken
, OUString
const& rNewToken
)
336 osl::MutexGuard
aGuard(GetInitMutex());
337 pImpl
->SetToken(nToken
, rNewToken
);
340 // -----------------------------------------------------------------------
342 OUString
SvtUserOptions::GetFullName () const
344 osl::MutexGuard
aGuard(GetInitMutex());
345 return pImpl
->GetFullName();
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */