LCOV - code coverage report
Current view: top level - libreoffice/unotools/source/config - useroptions.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 48 100 48.0 %
Date: 2012-12-27 Functions: 19 40 47.5 %
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             : 
      21             : #include <unotools/useroptions.hxx>
      22             : 
      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"
      31             : 
      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>
      43             : 
      44             : using namespace utl;
      45             : using namespace com::sun::star;
      46             : //using namespace com::sun::star::uno;
      47             : using rtl::OUString;
      48             : 
      49             : //namespace css = ::com::sun::star;
      50             : 
      51             : namespace
      52             : {
      53             : 
      54             : // vOptionNames[] -- names of the user option entries
      55             : // The order corresponds to the #define USER_OPT_* list in useroptions.hxx.
      56             : char const * const vOptionNames[] = {
      57             :     "l",                         // USER_OPT_CITY
      58             :     "o",                         // USER_OPT_COMPANY
      59             :     "c",                         // USER_OPT_COUNTRY
      60             :     "mail",                      // USER_OPT_EMAIL
      61             :     "facsimiletelephonenumber",  // USER_OPT_FAX
      62             :     "givenname",                 // USER_OPT_FIRSTNAME
      63             :     "sn",                        // USER_OPT_LASTNAME
      64             :     "position",                  // USER_OPT_POSITION
      65             :     "st",                        // USER_OPT_STATE
      66             :     "street",                    // USER_OPT_STREET
      67             :     "homephone",                 // USER_OPT_TELEPHONEHOME
      68             :     "telephonenumber",           // USER_OPT_TELEPHONEWORK
      69             :     "title",                     // USER_OPT_TITLE
      70             :     "initials",                  // USER_OPT_ID
      71             :     "postalcode",                // USER_OPT_ZIP
      72             :     "fathersname",               // USER_OPT_FATHERSNAME
      73             :     "apartment",                 // USER_OPT_APARTMENT
      74             :     "customernumber"             // USER_OPT_CUSTOMERNUMBER
      75             : };
      76             : const sal_uInt16 nOptionNameCount = SAL_N_ELEMENTS(vOptionNames);
      77             : 
      78             : } // namespace
      79             : 
      80          38 : boost::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::pSharedImpl;
      81             : 
      82             : // class ChangeListener --------------------------------------------------
      83             : 
      84          10 : class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper1<util::XChangesListener>
      85             : {
      86             : public:
      87          13 :     ChangeListener (Impl& rParent): m_rParent(rParent) { }
      88             : 
      89             :     // XChangesListener
      90             :     virtual void SAL_CALL changesOccurred (util::ChangesEvent const& Event) throw(uno::RuntimeException);
      91             :     // XEventListener
      92             :     virtual void SAL_CALL disposing (lang::EventObject const& Source) throw(uno::RuntimeException);
      93             : 
      94             : private:
      95             :     Impl& m_rParent;
      96             : };
      97             : 
      98             : // class Impl ------------------------------------------------------------
      99             : 
     100          10 : class SvtUserOptions::Impl : public utl::ConfigurationBroadcaster
     101             : {
     102             : public:
     103             :     Impl ();
     104             : 
     105             :     OUString GetFullName () const;
     106             : 
     107             :     sal_Bool IsTokenReadonly (sal_uInt16 nToken) const;
     108             :     OUString GetToken (sal_uInt16 nToken) const;
     109             :     void     SetToken (sal_uInt16 nToken, OUString const& rNewToken);
     110             :     void     Notify ();
     111             : 
     112             : private:
     113             :     uno::Reference<util::XChangesListener> m_xChangeListener;
     114             :     uno::Reference<container::XNameAccess> m_xCfg;
     115             :     uno::Reference<beans::XPropertySet>    m_xData;
     116             : };
     117             : 
     118             : // class SvtUserOptions::ChangeListener ----------------------------------
     119             : 
     120           0 : void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent) throw(uno::RuntimeException)
     121             : {
     122           0 :     if (rEvent.Changes.getLength())
     123           0 :         m_rParent.Notify();
     124           0 : }
     125             : 
     126           0 : void SvtUserOptions::ChangeListener::disposing (lang::EventObject const& rSource) throw(uno::RuntimeException)
     127             : {
     128             :     try
     129             :     {
     130           0 :         uno::Reference<util::XChangesNotifier> xChgNot(rSource.Source, uno::UNO_QUERY_THROW);
     131           0 :         xChgNot->removeChangesListener(this);
     132             :     }
     133           0 :     catch (uno::Exception&)
     134             :     {
     135             :     }
     136           0 : }
     137             : 
     138             : // class SvtUserOptions::Impl --------------------------------------------
     139             : 
     140          13 : SvtUserOptions::Impl::Impl() :
     141          13 :     m_xChangeListener( new ChangeListener(*this) )
     142             : {
     143             :     try
     144             :     {
     145             :         m_xCfg = uno::Reference<container::XNameAccess>(
     146             :             comphelper::ConfigurationHelper::openConfig(
     147             :                 comphelper::getProcessComponentContext(),
     148             :                 "org.openoffice.UserProfile/Data",
     149             :                 comphelper::ConfigurationHelper::E_STANDARD
     150             :             ),
     151             :             uno::UNO_QUERY
     152          13 :         );
     153             : 
     154          13 :         m_xData = uno::Reference<beans::XPropertySet>(m_xCfg, uno::UNO_QUERY);
     155          13 :         uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY);
     156             :         try
     157             :         {
     158          13 :             xChgNot->addChangesListener(m_xChangeListener);
     159             :         }
     160           0 :         catch (uno::RuntimeException&)
     161             :         {
     162          13 :         }
     163             :     }
     164           0 :     catch (uno::Exception const& ex)
     165             :     {
     166           0 :         m_xCfg.clear();
     167             :         SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
     168             :     }
     169          13 : }
     170             : 
     171             : // -----------------------------------------------------------------------
     172             : 
     173         581 : OUString SvtUserOptions::Impl::GetToken (sal_uInt16 nToken) const
     174             : {
     175         581 :     OUString sToken;
     176         581 :     if (nToken < nOptionNameCount)
     177             :     {
     178             :         try
     179             :         {
     180         581 :             if (m_xData.is())
     181         581 :                 m_xData->getPropertyValue(OUString::createFromAscii(vOptionNames[nToken])) >>= sToken;
     182             :         }
     183           0 :         catch (uno::Exception const& ex)
     184             :         {
     185             :             SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
     186             :         }
     187             :     }
     188             :     else
     189             :         SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
     190         581 :     return sToken;
     191             : }
     192             : 
     193             : // -----------------------------------------------------------------------
     194             : 
     195           0 : void SvtUserOptions::Impl::SetToken (sal_uInt16 nToken, OUString const& sToken)
     196             : {
     197           0 :     if (nToken < nOptionNameCount)
     198             :     {
     199             :         try
     200             :         {
     201           0 :             if (m_xData.is())
     202           0 :                 m_xData->setPropertyValue(OUString::createFromAscii(vOptionNames[nToken]), uno::makeAny(sToken));
     203           0 :             comphelper::ConfigurationHelper::flush(m_xCfg);
     204             :         }
     205           0 :         catch (uno::Exception const& ex)
     206             :         {
     207             :             SAL_WARN("unotools", "Caught unexpected: " << ex.Message);
     208             :         }
     209             :     }
     210             :     else
     211             :         SAL_WARN("unotools.config", "SvtUserOptions::Impl::GetToken(): invalid token");
     212           0 : }
     213             : 
     214             : // -----------------------------------------------------------------------
     215             : 
     216         245 : OUString SvtUserOptions::Impl::GetFullName () const
     217             : {
     218             :     // TODO international name
     219         245 :     OUString sFullName = GetToken(USER_OPT_FIRSTNAME).trim();
     220         245 :     if (!sFullName.isEmpty())
     221           0 :         sFullName += " ";
     222         245 :     sFullName += GetToken(USER_OPT_LASTNAME).trim();
     223         245 :     return sFullName;
     224             : }
     225             : 
     226             : // -----------------------------------------------------------------------
     227             : 
     228           0 : void SvtUserOptions::Impl::Notify ()
     229             : {
     230           0 :     NotifyListeners(0);
     231           0 : }
     232             : 
     233             : // -----------------------------------------------------------------------
     234             : 
     235           0 : sal_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             :         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 sal_False;
     249             :     }
     250             : }
     251             : 
     252             : // class SvtUserOptions --------------------------------------------------
     253             : 
     254          63 : SvtUserOptions::SvtUserOptions ()
     255             : {
     256             :     // Global access, must be guarded (multithreading)
     257          63 :     osl::MutexGuard aGuard(GetInitMutex());
     258             : 
     259          63 :     if (pSharedImpl.expired())
     260             :     {
     261             :         RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) SvtUserOptions::Impl::ctor()");
     262          13 :         pImpl.reset(new Impl);
     263          13 :         pSharedImpl = pImpl;
     264          13 :         ItemHolder1::holdConfigItem(E_USEROPTIONS);
     265             :     }
     266          63 :     pImpl = pSharedImpl.lock();
     267          63 :     pImpl->AddListener(this);
     268          63 : }
     269             : 
     270             : // -----------------------------------------------------------------------
     271             : 
     272         123 : SvtUserOptions::~SvtUserOptions()
     273             : {
     274             :     // Global access, must be guarded (multithreading)
     275          55 :     osl::MutexGuard aGuard( GetInitMutex() );
     276          55 :     pImpl->RemoveListener(this);
     277          68 : }
     278             : 
     279             : // -----------------------------------------------------------------------
     280             : 
     281             : namespace
     282             : {
     283             :     class theUserOptionsMutex : public rtl::Static<osl::Mutex, theUserOptionsMutex>{};
     284             : }
     285             : 
     286         454 : osl::Mutex& SvtUserOptions::GetInitMutex()
     287             : {
     288         454 :     return theUserOptionsMutex::get();
     289             : }
     290             : 
     291             : // -----------------------------------------------------------------------
     292             : 
     293             : //
     294             : // getters
     295             : //
     296           0 : OUString SvtUserOptions::GetCompany        () const { return GetToken(USER_OPT_COMPANY); }
     297          14 : OUString SvtUserOptions::GetFirstName      () const { return GetToken(USER_OPT_FIRSTNAME); }
     298          35 : OUString SvtUserOptions::GetLastName       () const { return GetToken(USER_OPT_LASTNAME); }
     299          18 : OUString SvtUserOptions::GetID             () const { return GetToken(USER_OPT_ID); }
     300           0 : OUString SvtUserOptions::GetStreet         () const { return GetToken(USER_OPT_STREET); }
     301           0 : OUString SvtUserOptions::GetCity           () const { return GetToken(USER_OPT_CITY); }
     302           0 : OUString SvtUserOptions::GetState          () const { return GetToken(USER_OPT_STATE); }
     303           0 : OUString SvtUserOptions::GetZip            () const { return GetToken(USER_OPT_ZIP); }
     304           0 : OUString SvtUserOptions::GetCountry        () const { return GetToken(USER_OPT_COUNTRY); }
     305           0 : OUString SvtUserOptions::GetPosition       () const { return GetToken(USER_OPT_POSITION); }
     306           0 : OUString SvtUserOptions::GetTitle          () const { return GetToken(USER_OPT_TITLE); }
     307           0 : OUString SvtUserOptions::GetTelephoneHome  () const { return GetToken(USER_OPT_TELEPHONEHOME); }
     308           0 : OUString SvtUserOptions::GetTelephoneWork  () const { return GetToken(USER_OPT_TELEPHONEWORK); }
     309           0 : OUString SvtUserOptions::GetFax            () const { return GetToken(USER_OPT_FAX); }
     310           0 : OUString SvtUserOptions::GetEmail          () const { return GetToken(USER_OPT_EMAIL); }
     311           0 : OUString SvtUserOptions::GetCustomerNumber () const { return GetToken(USER_OPT_CUSTOMERNUMBER); }
     312             : 
     313             : // -----------------------------------------------------------------------
     314             : 
     315             : //
     316             : // setters
     317             : //
     318           0 : void SvtUserOptions::SetCustomerNumber (OUString const& sToken) { SetToken(USER_OPT_CUSTOMERNUMBER, sToken); }
     319             : 
     320             : // -----------------------------------------------------------------------
     321             : 
     322           0 : sal_Bool SvtUserOptions::IsTokenReadonly (sal_uInt16 nToken) const
     323             : {
     324           0 :     osl::MutexGuard aGuard(GetInitMutex());
     325           0 :     return pImpl->IsTokenReadonly(nToken);
     326             : }
     327             : 
     328             : // -----------------------------------------------------------------------
     329             : 
     330          91 : OUString SvtUserOptions::GetToken (sal_uInt16 nToken) const
     331             : {
     332          91 :     osl::MutexGuard aGuard(GetInitMutex());
     333          91 :     return pImpl->GetToken(nToken);
     334             : }
     335             : 
     336             : // -----------------------------------------------------------------------
     337             : 
     338           0 : void SvtUserOptions::SetToken (sal_uInt16 nToken, OUString const& rNewToken)
     339             : {
     340           0 :     osl::MutexGuard aGuard(GetInitMutex());
     341           0 :     pImpl->SetToken(nToken, rNewToken);
     342           0 : }
     343             : 
     344             : // -----------------------------------------------------------------------
     345             : 
     346         245 : OUString SvtUserOptions::GetFullName () const
     347             : {
     348         245 :     osl::MutexGuard aGuard(GetInitMutex());
     349         245 :     return pImpl->GetFullName();
     350         114 : }
     351             : 
     352             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10