LCOV - code coverage report
Current view: top level - libreoffice/comphelper/source/misc - configurationhelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 58 76 76.3 %
Date: 2012-12-27 Functions: 7 7 100.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 <comphelper/configurationhelper.hxx>
      21             : #include <comphelper/processfactory.hxx>
      22             : #include <com/sun/star/beans/XPropertySet.hpp>
      23             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      24             : #include <com/sun/star/container/XNameAccess.hpp>
      25             : #include <com/sun/star/container/XNameContainer.hpp>
      26             : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
      27             : 
      28             : 
      29             : namespace comphelper{
      30             : 
      31             : //-----------------------------------------------
      32        2099 : css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
      33             :                                                                             const ::rtl::OUString&                                    sPackage,
      34             :                                                                                   sal_Int32                                           eMode   )
      35             : {
      36             :     css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
      37        2099 :         css::configuration::theDefaultProvider::get( rxContext ) );
      38             : 
      39        2099 :     ::comphelper::SequenceAsVector< css::uno::Any > lParams;
      40        2099 :     css::beans::PropertyValue                       aParam ;
      41             : 
      42             :     // set root path
      43        2099 :     aParam.Name    = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath"));
      44        2099 :     aParam.Value <<= sPackage;
      45        2099 :     lParams.push_back(css::uno::makeAny(aParam));
      46             : 
      47             :     // enable all locales mode
      48        2099 :     if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
      49             :     {
      50           0 :         aParam.Name    = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("locale"));
      51           0 :         aParam.Value <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*"));
      52           0 :         lParams.push_back(css::uno::makeAny(aParam));
      53             :     }
      54             : 
      55             :     // enable lazy writing
      56        2099 :     sal_Bool bLazy = ((eMode & ConfigurationHelper::E_LAZY_WRITE)==ConfigurationHelper::E_LAZY_WRITE);
      57        2099 :     aParam.Name    = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("lazywrite"));
      58        2099 :     aParam.Value   = css::uno::makeAny(bLazy);
      59        2099 :     lParams.push_back(css::uno::makeAny(aParam));
      60             : 
      61             :     // open it
      62        2099 :     css::uno::Reference< css::uno::XInterface > xCFG;
      63             : 
      64        2099 :     sal_Bool bReadOnly = ((eMode & ConfigurationHelper::E_READONLY)==ConfigurationHelper::E_READONLY);
      65        2099 :     if (bReadOnly)
      66        1890 :         xCFG = xConfigProvider->createInstanceWithArguments(
      67             :                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")),
      68        1890 :                 lParams.getAsConstList());
      69             :     else
      70         209 :         xCFG = xConfigProvider->createInstanceWithArguments(
      71             :                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationUpdateAccess")),
      72         209 :                 lParams.getAsConstList());
      73             : 
      74        2099 :     return xCFG;
      75             : }
      76             : 
      77             : //-----------------------------------------------
      78        1271 : css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG    ,
      79             :                                                    const ::rtl::OUString&                            sRelPath,
      80             :                                                    const ::rtl::OUString&                            sKey    )
      81             : {
      82        1271 :     css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
      83             : 
      84        1271 :     css::uno::Reference< css::beans::XPropertySet > xProps;
      85        1271 :     xAccess->getByHierarchicalName(sRelPath) >>= xProps;
      86        1267 :     if (!xProps.is())
      87             :     {
      88           0 :         ::rtl::OUStringBuffer sMsg(256);
      89           0 :         sMsg.appendAscii("The requested path \"");
      90           0 :         sMsg.append     (sRelPath               );
      91           0 :         sMsg.appendAscii("\" does not exists."  );
      92             : 
      93             :         throw css::container::NoSuchElementException(
      94             :                     sMsg.makeStringAndClear(),
      95           0 :                     css::uno::Reference< css::uno::XInterface >());
      96             :     }
      97        1271 :     return xProps->getPropertyValue(sKey);
      98             : }
      99             : 
     100             : //-----------------------------------------------
     101          33 : void ConfigurationHelper::writeRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG    ,
     102             :                                            const ::rtl::OUString&                            sRelPath,
     103             :                                            const ::rtl::OUString&                            sKey    ,
     104             :                                            const css::uno::Any&                              aValue  )
     105             : {
     106          33 :     css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
     107             : 
     108          33 :     css::uno::Reference< css::beans::XPropertySet > xProps;
     109          33 :     xAccess->getByHierarchicalName(sRelPath) >>= xProps;
     110          33 :     if (!xProps.is())
     111             :     {
     112           0 :         ::rtl::OUStringBuffer sMsg(256);
     113           0 :         sMsg.appendAscii("The requested path \"");
     114           0 :         sMsg.append     (sRelPath               );
     115           0 :         sMsg.appendAscii("\" does not exists."  );
     116             : 
     117             :         throw css::container::NoSuchElementException(
     118             :                     sMsg.makeStringAndClear(),
     119           0 :                     css::uno::Reference< css::uno::XInterface >());
     120             :     }
     121          33 :     xProps->setPropertyValue(sKey, aValue);
     122          33 : }
     123             : 
     124             : //-----------------------------------------------
     125         488 : css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface > xCFG         ,
     126             :                                                                                        const ::rtl::OUString&                            sRelPathToSet,
     127             :                                                                                        const ::rtl::OUString&                            sSetNode     )
     128             : {
     129         488 :     css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
     130         488 :     css::uno::Reference< css::container::XNameAccess > xSet;
     131         488 :     xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
     132         488 :     if (!xSet.is())
     133             :     {
     134           0 :         ::rtl::OUStringBuffer sMsg(256);
     135           0 :         sMsg.appendAscii("The requested path \"");
     136           0 :         sMsg.append     (sRelPathToSet          );
     137           0 :         sMsg.appendAscii("\" does not exists."  );
     138             : 
     139             :         throw css::container::NoSuchElementException(
     140             :                     sMsg.makeStringAndClear(),
     141           0 :                     css::uno::Reference< css::uno::XInterface >());
     142             :     }
     143             : 
     144         488 :     css::uno::Reference< css::uno::XInterface > xNode;
     145         488 :     if (xSet->hasByName(sSetNode))
     146         453 :         xSet->getByName(sSetNode) >>= xNode;
     147             :     else
     148             :     {
     149          35 :         css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
     150          35 :         xNode = xNodeFactory->createInstance();
     151          35 :         css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
     152          35 :         xSetReplace->insertByName(sSetNode, css::uno::makeAny(xNode));
     153             :     }
     154             : 
     155         488 :     return xNode;
     156             : }
     157             : 
     158             : //-----------------------------------------------
     159         878 : css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::uno::XComponentContext >&    rxContext,
     160             :                                                  const ::rtl::OUString&                                       sPackage,
     161             :                                                  const ::rtl::OUString&                                       sRelPath,
     162             :                                                  const ::rtl::OUString&                                       sKey    ,
     163             :                                                        sal_Int32                                              eMode   )
     164             : {
     165         878 :     css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
     166         878 :     return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
     167             : }
     168             : 
     169             : //-----------------------------------------------
     170          33 : void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::uno::XComponentContext >&    rxContext,
     171             :                                          const ::rtl::OUString&                                       sPackage,
     172             :                                          const ::rtl::OUString&                                       sRelPath,
     173             :                                          const ::rtl::OUString&                                       sKey    ,
     174             :                                          const css::uno::Any&                                         aValue  ,
     175             :                                                sal_Int32                                              eMode   )
     176             : {
     177          33 :     css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
     178          33 :     ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
     179          33 :     ConfigurationHelper::flush(xCFG);
     180          33 : }
     181             : 
     182             : //-----------------------------------------------
     183         675 : void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
     184             : {
     185         675 :     css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
     186         675 :     xBatch->commitChanges();
     187         675 : }
     188             : 
     189             : } // namespace comphelper
     190             : 
     191             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10