LCOV - code coverage report
Current view: top level - unotools/source/config - useroptions.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 76 116 65.5 %
Date: 2014-11-03 Functions: 29 38 76.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <unotools/useroptions.hxx>
      21             : #include <unotools/syslocale.hxx>
      22             : #include <unotools/configmgr.hxx>
      23             : #include <com/sun/star/uno/Any.hxx>
      24             : #include <com/sun/star/uno/Sequence.hxx>
      25             : #include <osl/mutex.hxx>
      26             : #include <rtl/instance.hxx>
      27             : #include "itemholder1.hxx"
      28             : 
      29             : #include <com/sun/star/beans/Property.hpp>
      30             : #include <com/sun/star/beans/XPropertySet.hpp>
      31             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      32             : #include <com/sun/star/container/XNameAccess.hpp>
      33             : #include <com/sun/star/container/XNameContainer.hpp>
      34             : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
      35             : #include <com/sun/star/util/XChangesListener.hpp>
      36             : #include <com/sun/star/util/XChangesNotifier.hpp>
      37             : #include <com/sun/star/util/ChangesEvent.hpp>
      38             : #include <comphelper/configurationhelper.hxx>
      39             : #include <comphelper/processfactory.hxx>
      40             : #include <i18nlangtag/mslangid.hxx>
      41             : 
      42             : using namespace utl;
      43             : using namespace com::sun::star;
      44             : 
      45             : namespace
      46             : {
      47             : 
      48             : // vOptionNames[] -- names of the user option entries
      49             : // The order corresponds to the #define USER_OPT_* list in useroptions.hxx.
      50             : char const * const vOptionNames[] = {
      51             :     "l",                         // USER_OPT_CITY
      52             :     "o",                         // USER_OPT_COMPANY
      53             :     "c",                         // USER_OPT_COUNTRY
      54             :     "mail",                      // USER_OPT_EMAIL
      55             :     "facsimiletelephonenumber",  // USER_OPT_FAX
      56             :     "givenname",                 // USER_OPT_FIRSTNAME
      57             :     "sn",                        // USER_OPT_LASTNAME
      58             :     "position",                  // USER_OPT_POSITION
      59             :     "st",                        // USER_OPT_STATE
      60             :     "street",                    // USER_OPT_STREET
      61             :     "homephone",                 // USER_OPT_TELEPHONEHOME
      62             :     "telephonenumber",           // USER_OPT_TELEPHONEWORK
      63             :     "title",                     // USER_OPT_TITLE
      64             :     "initials",                  // USER_OPT_ID
      65             :     "postalcode",                // USER_OPT_ZIP
      66             :     "fathersname",               // USER_OPT_FATHERSNAME
      67             :     "apartment"                  // USER_OPT_APARTMENT
      68             : };
      69             : const sal_uInt16 nOptionNameCount = SAL_N_ELEMENTS(vOptionNames);
      70             : 
      71             : } // namespace
      72             : 
      73         423 : boost::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::pSharedImpl;
      74             : 
      75         156 : class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper1<util::XChangesListener>
      76             : {
      77             : public:
      78         128 :     ChangeListener (Impl& rParent): m_rParent(rParent) { }
      79             : 
      80             :     // XChangesListener
      81             :     virtual void SAL_CALL changesOccurred (util::ChangesEvent const& Event) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE;
      82             :     // XEventListener
      83             :     virtual void SAL_CALL disposing (lang::EventObject const& Source) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE;
      84             : 
      85             : private:
      86             :     Impl& m_rParent;
      87             : };
      88             : 
      89         156 : class SvtUserOptions::Impl : public utl::ConfigurationBroadcaster
      90             : {
      91             : public:
      92             :     Impl ();
      93             : 
      94             :     OUString GetFullName () const;
      95             : 
      96             :     bool IsTokenReadonly (sal_uInt16 nToken) const;
      97             :     OUString GetToken (sal_uInt16 nToken) const;
      98             :     void     SetToken (sal_uInt16 nToken, OUString const& rNewToken);
      99             :     void     Notify ();
     100             : 
     101             : private:
     102             :     uno::Reference<util::XChangesListener> m_xChangeListener;
     103             :     uno::Reference<container::XNameAccess> m_xCfg;
     104             :     uno::Reference<beans::XPropertySet>    m_xData;
     105             : };
     106             : 
     107           4 : void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent) throw(uno::RuntimeException, std::exception)
     108             : {
     109           4 :     if (rEvent.Changes.getLength())
     110           4 :         m_rParent.Notify();
     111           4 : }
     112             : 
     113           0 : void SvtUserOptions::ChangeListener::disposing (lang::EventObject const& rSource) throw(uno::RuntimeException, std::exception)
     114             : {
     115             :     try
     116             :     {
     117           0 :         uno::Reference<util::XChangesNotifier> xChgNot(rSource.Source, uno::UNO_QUERY_THROW);
     118           0 :         xChgNot->removeChangesListener(this);
     119             :     }
     120           0 :     catch (uno::Exception&)
     121             :     {
     122             :     }
     123           0 : }
     124             : 
     125         128 : SvtUserOptions::Impl::Impl() :
     126         128 :     m_xChangeListener( new ChangeListener(*this) )
     127             : {
     128             :     try
     129             :     {
     130         256 :         m_xCfg = uno::Reference<container::XNameAccess>(
     131             :             comphelper::ConfigurationHelper::openConfig(
     132             :                 comphelper::getProcessComponentContext(),
     133             :                 "org.openoffice.UserProfile/Data",
     134             :                 comphelper::ConfigurationHelper::E_STANDARD
     135             :             ),
     136             :             uno::UNO_QUERY
     137         128 :         );
     138             : 
     139         128 :         m_xData = uno::Reference<beans::XPropertySet>(m_xCfg, uno::UNO_QUERY);
     140         128 :         uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY);
     141             :         try
     142             :         {
     143         128 :             xChgNot->addChangesListener(m_xChangeListener);
     144             :         }
     145           0 :         catch (uno::RuntimeException&)
     146             :         {
     147         128 :         }
     148             :     }
     149           0 :     catch (uno::Exception const& ex)
     150             :     {
     151           0 :         m_xCfg.clear();
     152             :         SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
     153             :     }
     154         128 : }
     155             : 
     156       14332 : OUString SvtUserOptions::Impl::GetToken (sal_uInt16 nToken) const
     157             : {
     158       14332 :     OUString sToken;
     159       14332 :     if (nToken < nOptionNameCount)
     160             :     {
     161             :         try
     162             :         {
     163       14332 :             if (m_xData.is())
     164       14332 :                 m_xData->getPropertyValue(OUString::createFromAscii(vOptionNames[nToken])) >>= sToken;
     165             :         }
     166           0 :         catch (uno::Exception const& ex)
     167             :         {
     168             :             SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
     169             :         }
     170             :     }
     171             :     else
     172             :         SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
     173       14332 :     return sToken;
     174             : }
     175             : 
     176           4 : void SvtUserOptions::Impl::SetToken (sal_uInt16 nToken, OUString const& sToken)
     177             : {
     178           4 :     if (nToken < nOptionNameCount)
     179             :     {
     180             :         try
     181             :         {
     182           4 :             if (m_xData.is())
     183           4 :                 m_xData->setPropertyValue(OUString::createFromAscii(vOptionNames[nToken]), uno::makeAny(sToken));
     184           4 :             comphelper::ConfigurationHelper::flush(m_xCfg);
     185             :         }
     186           0 :         catch (uno::Exception const& ex)
     187             :         {
     188             :             SAL_WARN("unotools.config", "Caught unexpected: " << ex.Message);
     189             :         }
     190             :     }
     191             :     else
     192             :         SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
     193           4 : }
     194             : 
     195        5984 : OUString SvtUserOptions::Impl::GetFullName () const
     196             : {
     197        5984 :     OUString sFullName;
     198        5984 :     switch (LanguageType const eLang = SvtSysLocale().GetUILanguageTag().getLanguageType())
     199             :     {
     200             :         case LANGUAGE_RUSSIAN:
     201           0 :             sFullName = GetToken(USER_OPT_FIRSTNAME).trim();
     202           0 :             if (!sFullName.isEmpty())
     203           0 :                 sFullName += " ";
     204           0 :             sFullName += GetToken(USER_OPT_FATHERSNAME).trim();
     205           0 :             if (!sFullName.isEmpty())
     206           0 :                 sFullName += " ";
     207           0 :             sFullName += GetToken(USER_OPT_LASTNAME).trim();
     208           0 :             break;
     209             :         default:
     210        5984 :             if (MsLangId::isFamilyNameFirst(eLang))
     211             :             {
     212           0 :                 sFullName = GetToken(USER_OPT_LASTNAME).trim();
     213           0 :                 if (!sFullName.isEmpty())
     214           0 :                     sFullName += " ";
     215           0 :                 sFullName += GetToken(USER_OPT_FIRSTNAME).trim();
     216             :             }
     217             :             else
     218             :             {
     219        5984 :                 sFullName = GetToken(USER_OPT_FIRSTNAME).trim();
     220        5984 :                 if (!sFullName.isEmpty())
     221           0 :                     sFullName += " ";
     222        5984 :                 sFullName += GetToken(USER_OPT_LASTNAME).trim();
     223             :             }
     224        5984 :             break;
     225             :     }
     226             : 
     227        5984 :     return sFullName;
     228             : }
     229             : 
     230           4 : void SvtUserOptions::Impl::Notify ()
     231             : {
     232           4 :     NotifyListeners(0);
     233           4 : }
     234             : 
     235           0 : bool SvtUserOptions::Impl::IsTokenReadonly (sal_uInt16 nToken) const
     236             : {
     237           0 :     if (nToken < nOptionNameCount)
     238             :     {
     239           0 :         uno::Reference<beans::XPropertySet> xData(m_xCfg, uno::UNO_QUERY);
     240           0 :         uno::Reference<beans::XPropertySetInfo> xInfo = xData->getPropertySetInfo();
     241           0 :         beans::Property aProp = xInfo->getPropertyByName(OUString::createFromAscii(vOptionNames[nToken]));
     242           0 :         return ((aProp.Attributes & beans::PropertyAttribute::READONLY) ==
     243           0 :             beans::PropertyAttribute::READONLY);
     244             :     }
     245             :     else
     246             :     {
     247             :         SAL_WARN("unotools.config", "SvtUserOptions::Impl::IsTokenReadonly(): invalid token");
     248           0 :         return false;
     249             :     }
     250             : }
     251             : 
     252        2184 : SvtUserOptions::SvtUserOptions ()
     253             : {
     254             :     // Global access, must be guarded (multithreading)
     255        2184 :     osl::MutexGuard aGuard(GetInitMutex());
     256             : 
     257        2184 :     if (pSharedImpl.expired())
     258             :     {
     259         128 :         pImpl.reset(new Impl);
     260         128 :         pSharedImpl = pImpl;
     261         128 :         ItemHolder1::holdConfigItem(E_USEROPTIONS);
     262             :     }
     263        2184 :     pImpl = pSharedImpl.lock();
     264        2184 :     pImpl->AddListener(this);
     265        2184 : }
     266             : 
     267        4426 : SvtUserOptions::~SvtUserOptions()
     268             : {
     269             :     // Global access, must be guarded (multithreading)
     270        2132 :     osl::MutexGuard aGuard( GetInitMutex() );
     271        2132 :     pImpl->RemoveListener(this);
     272        2294 : }
     273             : 
     274             : namespace
     275             : {
     276             :     class theUserOptionsMutex : public rtl::Static<osl::Mutex, theUserOptionsMutex>{};
     277             : }
     278             : 
     279       12668 : osl::Mutex& SvtUserOptions::GetInitMutex()
     280             : {
     281       12668 :     return theUserOptionsMutex::get();
     282             : }
     283             : 
     284          38 : OUString SvtUserOptions::GetCompany        () const { return GetToken(USER_OPT_COMPANY); }
     285         450 : OUString SvtUserOptions::GetFirstName      () const { return GetToken(USER_OPT_FIRSTNAME); }
     286         658 : OUString SvtUserOptions::GetLastName       () const { return GetToken(USER_OPT_LASTNAME); }
     287         524 : OUString SvtUserOptions::GetID             () const { return GetToken(USER_OPT_ID); }
     288          38 : OUString SvtUserOptions::GetStreet         () const { return GetToken(USER_OPT_STREET); }
     289          38 : OUString SvtUserOptions::GetCity           () const { return GetToken(USER_OPT_CITY); }
     290           0 : OUString SvtUserOptions::GetState          () const { return GetToken(USER_OPT_STATE); }
     291           0 : OUString SvtUserOptions::GetZip            () const { return GetToken(USER_OPT_ZIP); }
     292           0 : OUString SvtUserOptions::GetCountry        () const { return GetToken(USER_OPT_COUNTRY); }
     293          38 : OUString SvtUserOptions::GetPosition       () const { return GetToken(USER_OPT_POSITION); }
     294          38 : OUString SvtUserOptions::GetTitle          () const { return GetToken(USER_OPT_TITLE); }
     295           0 : OUString SvtUserOptions::GetTelephoneHome  () const { return GetToken(USER_OPT_TELEPHONEHOME); }
     296           0 : OUString SvtUserOptions::GetTelephoneWork  () const { return GetToken(USER_OPT_TELEPHONEWORK); }
     297           0 : OUString SvtUserOptions::GetFax            () const { return GetToken(USER_OPT_FAX); }
     298          38 : OUString SvtUserOptions::GetEmail          () const { return GetToken(USER_OPT_EMAIL); }
     299             : 
     300           0 : bool SvtUserOptions::IsTokenReadonly (sal_uInt16 nToken) const
     301             : {
     302           0 :     osl::MutexGuard aGuard(GetInitMutex());
     303           0 :     return pImpl->IsTokenReadonly(nToken);
     304             : }
     305             : 
     306        2364 : OUString SvtUserOptions::GetToken (sal_uInt16 nToken) const
     307             : {
     308        2364 :     osl::MutexGuard aGuard(GetInitMutex());
     309        2364 :     return pImpl->GetToken(nToken);
     310             : }
     311             : 
     312           4 : void SvtUserOptions::SetToken (sal_uInt16 nToken, OUString const& rNewToken)
     313             : {
     314           4 :     osl::MutexGuard aGuard(GetInitMutex());
     315           4 :     pImpl->SetToken(nToken, rNewToken);
     316           4 : }
     317             : 
     318        5984 : OUString SvtUserOptions::GetFullName () const
     319             : {
     320        5984 :     osl::MutexGuard aGuard(GetInitMutex());
     321        5984 :     return pImpl->GetFullName();
     322        1269 : }
     323             : 
     324             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10