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

Generated by: LCOV version 1.10