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

Generated by: LCOV version 1.11