LCOV - code coverage report
Current view: top level - unotools/source/config - useroptions.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 2 102 2.0 %
Date: 2014-04-14 Functions: 2 40 5.0 %
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             : 
      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             : 
      41             : using namespace utl;
      42             : using namespace com::sun::star;
      43             : 
      44             : namespace
      45             : {
      46             : 
      47             : // vOptionNames[] -- names of the user option entries
      48             : // The order corresponds to the #define USER_OPT_* list in useroptions.hxx.
      49             : char const * const vOptionNames[] = {
      50             :     "l",                         // USER_OPT_CITY
      51             :     "o",                         // USER_OPT_COMPANY
      52             :     "c",                         // USER_OPT_COUNTRY
      53             :     "mail",                      // USER_OPT_EMAIL
      54             :     "facsimiletelephonenumber",  // USER_OPT_FAX
      55             :     "givenname",                 // USER_OPT_FIRSTNAME
      56             :     "sn",                        // USER_OPT_LASTNAME
      57             :     "position",                  // USER_OPT_POSITION
      58             :     "st",                        // USER_OPT_STATE
      59             :     "street",                    // USER_OPT_STREET
      60             :     "homephone",                 // USER_OPT_TELEPHONEHOME
      61             :     "telephonenumber",           // USER_OPT_TELEPHONEWORK
      62             :     "title",                     // USER_OPT_TITLE
      63             :     "initials",                  // USER_OPT_ID
      64             :     "postalcode",                // USER_OPT_ZIP
      65             :     "fathersname",               // USER_OPT_FATHERSNAME
      66             :     "apartment",                 // USER_OPT_APARTMENT
      67             :     "customernumber"             // USER_OPT_CUSTOMERNUMBER
      68             : };
      69             : const sal_uInt16 nOptionNameCount = SAL_N_ELEMENTS(vOptionNames);
      70             : 
      71             : } // namespace
      72             : 
      73           1 : boost::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::pSharedImpl;
      74             : 
      75           0 : class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper1<util::XChangesListener>
      76             : {
      77             : public:
      78           0 :     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           0 : 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           0 : void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent) throw(uno::RuntimeException, std::exception)
     108             : {
     109           0 :     if (rEvent.Changes.getLength())
     110           0 :         m_rParent.Notify();
     111           0 : }
     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           0 : SvtUserOptions::Impl::Impl() :
     126           0 :     m_xChangeListener( new ChangeListener(*this) )
     127             : {
     128             :     try
     129             :     {
     130           0 :         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           0 :         );
     138             : 
     139           0 :         m_xData = uno::Reference<beans::XPropertySet>(m_xCfg, uno::UNO_QUERY);
     140           0 :         uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY);
     141             :         try
     142             :         {
     143           0 :             xChgNot->addChangesListener(m_xChangeListener);
     144             :         }
     145           0 :         catch (uno::RuntimeException&)
     146             :         {
     147           0 :         }
     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           0 : }
     155             : 
     156           0 : OUString SvtUserOptions::Impl::GetToken (sal_uInt16 nToken) const
     157             : {
     158           0 :     OUString sToken;
     159           0 :     if (nToken < nOptionNameCount)
     160             :     {
     161             :         try
     162             :         {
     163           0 :             if (m_xData.is())
     164           0 :                 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           0 :     return sToken;
     174             : }
     175             : 
     176           0 : void SvtUserOptions::Impl::SetToken (sal_uInt16 nToken, OUString const& sToken)
     177             : {
     178           0 :     if (nToken < nOptionNameCount)
     179             :     {
     180             :         try
     181             :         {
     182           0 :             if (m_xData.is())
     183           0 :                 m_xData->setPropertyValue(OUString::createFromAscii(vOptionNames[nToken]), uno::makeAny(sToken));
     184           0 :             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           0 : }
     194             : 
     195           0 : OUString SvtUserOptions::Impl::GetFullName () const
     196             : {
     197             :     // TODO international name
     198           0 :     OUString sFullName = GetToken(USER_OPT_FIRSTNAME).trim();
     199           0 :     if (!sFullName.isEmpty())
     200           0 :         sFullName += " ";
     201           0 :     sFullName += GetToken(USER_OPT_LASTNAME).trim();
     202           0 :     return sFullName;
     203             : }
     204             : 
     205           0 : void SvtUserOptions::Impl::Notify ()
     206             : {
     207           0 :     NotifyListeners(0);
     208           0 : }
     209             : 
     210           0 : bool SvtUserOptions::Impl::IsTokenReadonly (sal_uInt16 nToken) const
     211             : {
     212           0 :     if (nToken < nOptionNameCount)
     213             :     {
     214           0 :         uno::Reference<beans::XPropertySet> xData(m_xCfg, uno::UNO_QUERY);
     215           0 :         uno::Reference<beans::XPropertySetInfo> xInfo = xData->getPropertySetInfo();
     216           0 :         beans::Property aProp = xInfo->getPropertyByName(OUString::createFromAscii(vOptionNames[nToken]));
     217           0 :         return ((aProp.Attributes & beans::PropertyAttribute::READONLY) ==
     218           0 :             beans::PropertyAttribute::READONLY);
     219             :     }
     220             :     else
     221             :     {
     222             :         SAL_WARN("unotools.config", "SvtUserOptions::Impl::IsTokenReadonly(): invalid token");
     223           0 :         return false;
     224             :     }
     225             : }
     226             : 
     227           0 : SvtUserOptions::SvtUserOptions ()
     228             : {
     229             :     // Global access, must be guarded (multithreading)
     230           0 :     osl::MutexGuard aGuard(GetInitMutex());
     231             : 
     232           0 :     if (pSharedImpl.expired())
     233             :     {
     234           0 :         pImpl.reset(new Impl);
     235           0 :         pSharedImpl = pImpl;
     236           0 :         ItemHolder1::holdConfigItem(E_USEROPTIONS);
     237             :     }
     238           0 :     pImpl = pSharedImpl.lock();
     239           0 :     pImpl->AddListener(this);
     240           0 : }
     241             : 
     242           0 : SvtUserOptions::~SvtUserOptions()
     243             : {
     244             :     // Global access, must be guarded (multithreading)
     245           0 :     osl::MutexGuard aGuard( GetInitMutex() );
     246           0 :     pImpl->RemoveListener(this);
     247           0 : }
     248             : 
     249             : namespace
     250             : {
     251             :     class theUserOptionsMutex : public rtl::Static<osl::Mutex, theUserOptionsMutex>{};
     252             : }
     253             : 
     254           0 : osl::Mutex& SvtUserOptions::GetInitMutex()
     255             : {
     256           0 :     return theUserOptionsMutex::get();
     257             : }
     258             : 
     259           0 : OUString SvtUserOptions::GetCompany        () const { return GetToken(USER_OPT_COMPANY); }
     260           0 : OUString SvtUserOptions::GetFirstName      () const { return GetToken(USER_OPT_FIRSTNAME); }
     261           0 : OUString SvtUserOptions::GetLastName       () const { return GetToken(USER_OPT_LASTNAME); }
     262           0 : OUString SvtUserOptions::GetID             () const { return GetToken(USER_OPT_ID); }
     263           0 : OUString SvtUserOptions::GetStreet         () const { return GetToken(USER_OPT_STREET); }
     264           0 : OUString SvtUserOptions::GetCity           () const { return GetToken(USER_OPT_CITY); }
     265           0 : OUString SvtUserOptions::GetState          () const { return GetToken(USER_OPT_STATE); }
     266           0 : OUString SvtUserOptions::GetZip            () const { return GetToken(USER_OPT_ZIP); }
     267           0 : OUString SvtUserOptions::GetCountry        () const { return GetToken(USER_OPT_COUNTRY); }
     268           0 : OUString SvtUserOptions::GetPosition       () const { return GetToken(USER_OPT_POSITION); }
     269           0 : OUString SvtUserOptions::GetTitle          () const { return GetToken(USER_OPT_TITLE); }
     270           0 : OUString SvtUserOptions::GetTelephoneHome  () const { return GetToken(USER_OPT_TELEPHONEHOME); }
     271           0 : OUString SvtUserOptions::GetTelephoneWork  () const { return GetToken(USER_OPT_TELEPHONEWORK); }
     272           0 : OUString SvtUserOptions::GetFax            () const { return GetToken(USER_OPT_FAX); }
     273           0 : OUString SvtUserOptions::GetEmail          () const { return GetToken(USER_OPT_EMAIL); }
     274           0 : OUString SvtUserOptions::GetCustomerNumber () const { return GetToken(USER_OPT_CUSTOMERNUMBER); }
     275             : 
     276           0 : void SvtUserOptions::SetCustomerNumber (OUString const& sToken) { SetToken(USER_OPT_CUSTOMERNUMBER, sToken); }
     277             : 
     278           0 : bool SvtUserOptions::IsTokenReadonly (sal_uInt16 nToken) const
     279             : {
     280           0 :     osl::MutexGuard aGuard(GetInitMutex());
     281           0 :     return pImpl->IsTokenReadonly(nToken);
     282             : }
     283             : 
     284           0 : OUString SvtUserOptions::GetToken (sal_uInt16 nToken) const
     285             : {
     286           0 :     osl::MutexGuard aGuard(GetInitMutex());
     287           0 :     return pImpl->GetToken(nToken);
     288             : }
     289             : 
     290           0 : void SvtUserOptions::SetToken (sal_uInt16 nToken, OUString const& rNewToken)
     291             : {
     292           0 :     osl::MutexGuard aGuard(GetInitMutex());
     293           0 :     pImpl->SetToken(nToken, rNewToken);
     294           0 : }
     295             : 
     296           0 : OUString SvtUserOptions::GetFullName () const
     297             : {
     298           0 :     osl::MutexGuard aGuard(GetInitMutex());
     299           0 :     return pImpl->GetFullName();
     300           3 : }
     301             : 
     302             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10