LCOV - code coverage report
Current view: top level - sd/source/ui/tools - ConfigurationAccess.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 53 64 82.8 %
Date: 2014-04-11 Functions: 7 8 87.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 "tools/ConfigurationAccess.hxx"
      22             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      23             : #include <com/sun/star/beans/PropertyValue.hpp>
      24             : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
      25             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      26             : #include <com/sun/star/util/XChangesBatch.hpp>
      27             : #include <comphelper/processfactory.hxx>
      28             : #include <tools/diagnose_ex.h>
      29             : 
      30             : using namespace ::com::sun::star;
      31             : using namespace ::com::sun::star::uno;
      32             : 
      33             : namespace sd { namespace tools {
      34             : 
      35          73 : ConfigurationAccess::ConfigurationAccess (
      36             :     const Reference<XComponentContext>& rxContext,
      37             :     const OUString& rsRootName,
      38             :     const WriteMode eMode)
      39          73 :     : mxRoot()
      40             : {
      41             :     Reference<lang::XMultiServiceFactory> xProvider =
      42          73 :            configuration::theDefaultProvider::get( rxContext );
      43          73 :     Initialize(xProvider, rsRootName, eMode);
      44          73 : }
      45             : 
      46             : 
      47             : 
      48             : 
      49          73 : ConfigurationAccess::ConfigurationAccess (
      50             :     const OUString& rsRootName,
      51             :     const WriteMode eMode)
      52          73 :     : mxRoot()
      53             : {
      54             :     Reference<lang::XMultiServiceFactory> xProvider =
      55          73 :         configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
      56          73 :     Initialize(xProvider, rsRootName, eMode);
      57          73 : }
      58             : 
      59             : 
      60             : 
      61             : 
      62         146 : void ConfigurationAccess::Initialize (
      63             :     const Reference<lang::XMultiServiceFactory>& rxProvider,
      64             :     const OUString& rsRootName,
      65             :     const WriteMode eMode)
      66             : {
      67             :     try
      68             :     {
      69         146 :         Sequence<Any> aCreationArguments(3);
      70         292 :         aCreationArguments[0] = makeAny(beans::PropertyValue(
      71             :             "nodepath",
      72             :             0,
      73             :             makeAny(rsRootName),
      74         146 :             beans::PropertyState_DIRECT_VALUE));
      75         292 :         aCreationArguments[1] = makeAny(beans::PropertyValue(
      76             :             "depth",
      77             :             0,
      78             :             makeAny((sal_Int32)-1),
      79         146 :             beans::PropertyState_DIRECT_VALUE));
      80         292 :         aCreationArguments[2] = makeAny(beans::PropertyValue(
      81             :             "lazywrite",
      82             :             0,
      83             :             makeAny(true),
      84         146 :             beans::PropertyState_DIRECT_VALUE));
      85         292 :         OUString sAccessService;
      86         146 :         if (eMode == READ_ONLY)
      87         146 :             sAccessService = "com.sun.star.configuration.ConfigurationAccess";
      88             :         else
      89           0 :             sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
      90             : 
      91         438 :         mxRoot = rxProvider->createInstanceWithArguments(
      92             :             sAccessService,
      93         438 :             aCreationArguments);
      94             :     }
      95           0 :     catch (Exception&)
      96             :     {
      97             :         DBG_UNHANDLED_EXCEPTION();
      98             :     }
      99         146 : }
     100             : 
     101             : 
     102             : 
     103             : 
     104         146 : Any ConfigurationAccess::GetConfigurationNode (
     105             :     const OUString& sPathToNode)
     106             : {
     107             :     return GetConfigurationNode(
     108             :         Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
     109         146 :         sPathToNode);
     110             : }
     111             : 
     112             : 
     113             : 
     114             : 
     115         146 : Any ConfigurationAccess::GetConfigurationNode (
     116             :     const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
     117             :     const OUString& sPathToNode)
     118             : {
     119         146 :     if (sPathToNode.isEmpty())
     120           0 :         return Any(rxNode);
     121             : 
     122             :     try
     123             :     {
     124         146 :         if (rxNode.is())
     125             :         {
     126         146 :             return rxNode->getByHierarchicalName(sPathToNode);
     127             :         }
     128             :     }
     129           0 :     catch (const Exception& rException)
     130             :     {
     131             :         OSL_TRACE ("caught exception while getting configuration node %s: %s",
     132             :             OUStringToOString(sPathToNode, RTL_TEXTENCODING_UTF8).getStr(),
     133             :             OUStringToOString(rException.Message, RTL_TEXTENCODING_UTF8).getStr());
     134             :     }
     135             : 
     136           0 :     return Any();
     137             : }
     138             : 
     139             : 
     140             : 
     141             : 
     142           0 : void ConfigurationAccess::CommitChanges (void)
     143             : {
     144           0 :     Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
     145           0 :     if (xConfiguration.is())
     146           0 :         xConfiguration->commitChanges();
     147           0 : }
     148             : 
     149             : 
     150             : 
     151             : 
     152         146 : void ConfigurationAccess::ForAll (
     153             :     const Reference<container::XNameAccess>& rxContainer,
     154             :     const ::std::vector<OUString>& rArguments,
     155             :     const Functor& rFunctor)
     156             : {
     157         146 :     if (rxContainer.is())
     158             :     {
     159         146 :         ::std::vector<Any> aValues(rArguments.size());
     160         292 :         Sequence<OUString> aKeys (rxContainer->getElementNames());
     161         511 :         for (sal_Int32 nItemIndex=0; nItemIndex < aKeys.getLength(); ++nItemIndex)
     162             :         {
     163         365 :             const OUString& rsKey (aKeys[nItemIndex]);
     164         365 :             Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
     165         365 :             if (xSetItem.is())
     166             :             {
     167             :                 // Get from the current item of the container the children
     168             :                 // that match the names in the rArguments list.
     169        1022 :                 for (sal_uInt32 nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
     170         657 :                     aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
     171             :             }
     172         365 :             rFunctor(rsKey, aValues);
     173         511 :         }
     174             :     }
     175         146 : }
     176             : 
     177             : 
     178             : 
     179             : 
     180         292 : void ConfigurationAccess::FillList(
     181             :     const Reference<container::XNameAccess>& rxContainer,
     182             :     const OUString& rsArgument,
     183             :     ::std::vector<OUString>& rList)
     184             : {
     185             :     try
     186             :     {
     187         292 :         if (rxContainer.is())
     188             :         {
     189         292 :             Sequence<OUString> aKeys (rxContainer->getElementNames());
     190         292 :             rList.resize(aKeys.getLength());
     191        1606 :             for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
     192             :             {
     193             :                 Reference<container::XNameAccess> xSetItem (
     194        1314 :                     rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY);
     195        1314 :                 if (xSetItem.is())
     196             :                 {
     197        1314 :                     xSetItem->getByName(rsArgument) >>= rList[nItemIndex];
     198             :                 }
     199        1606 :             }
     200             :         }
     201             :     }
     202           0 :     catch (RuntimeException&)
     203             :     {}
     204         292 : }
     205             : 
     206             : 
     207             : } } // end of namespace sd::tools
     208             : 
     209             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10