LCOV - code coverage report
Current view: top level - sdext/source/presenter - PresenterConfigurationAccess.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 2 122 1.6 %
Date: 2015-06-13 12:38:46 Functions: 2 17 11.8 %
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 "PresenterConfigurationAccess.hxx"
      21             : 
      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 <osl/diagnose.h>
      28             : 
      29             : using namespace ::com::sun::star;
      30             : using namespace ::com::sun::star::uno;
      31             : 
      32             : namespace sdext { namespace presenter {
      33             : 
      34           6 : const OUString PresenterConfigurationAccess::msPresenterScreenRootName =
      35             :     "/org.openoffice.Office.PresenterScreen/";
      36             : 
      37           0 : PresenterConfigurationAccess::PresenterConfigurationAccess (
      38             :     const Reference<XComponentContext>& rxContext,
      39             :     const OUString& rsRootName,
      40             :     WriteMode eMode)
      41             :     : mxRoot(),
      42           0 :       maNode()
      43             : {
      44             :     try
      45             :     {
      46           0 :         if (rxContext.is())
      47             :         {
      48           0 :             Sequence<Any> aCreationArguments(3);
      49           0 :             aCreationArguments[0] = makeAny(beans::PropertyValue(
      50             :                 "nodepath",
      51             :                 0,
      52             :                 makeAny(rsRootName),
      53           0 :                 beans::PropertyState_DIRECT_VALUE));
      54           0 :             aCreationArguments[1] = makeAny(beans::PropertyValue(
      55             :                 "depth",
      56             :                 0,
      57             :                 makeAny((sal_Int32)-1),
      58           0 :                 beans::PropertyState_DIRECT_VALUE));
      59           0 :             aCreationArguments[2] = makeAny(beans::PropertyValue(
      60             :                 "lazywrite",
      61             :                 0,
      62             :                 makeAny(true),
      63           0 :                 beans::PropertyState_DIRECT_VALUE));
      64             : 
      65           0 :             OUString sAccessService;
      66           0 :             if (eMode == READ_ONLY)
      67           0 :                 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
      68             :             else
      69           0 :                 sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
      70             : 
      71             :             Reference<lang::XMultiServiceFactory> xProvider =
      72           0 :                 configuration::theDefaultProvider::get( rxContext );
      73           0 :             mxRoot = xProvider->createInstanceWithArguments(
      74           0 :                 sAccessService, aCreationArguments);
      75           0 :             maNode <<= mxRoot;
      76             :         }
      77             :     }
      78           0 :     catch (const Exception& rException)
      79             :     {
      80             :         OSL_TRACE ("caught exception while opening configuration: %s",
      81             :             OUStringToOString(rException.Message,
      82             :                 RTL_TEXTENCODING_UTF8).getStr());
      83             :     }
      84           0 : }
      85             : 
      86           0 : PresenterConfigurationAccess::~PresenterConfigurationAccess()
      87             : {
      88           0 : }
      89             : 
      90           0 : bool PresenterConfigurationAccess::IsValid() const
      91             : {
      92           0 :     return mxRoot.is();
      93             : }
      94             : 
      95           0 : Any PresenterConfigurationAccess::GetConfigurationNode (const OUString& sPathToNode)
      96             : {
      97             :     return GetConfigurationNode(
      98             :         Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
      99           0 :         sPathToNode);
     100             : }
     101             : 
     102           0 : bool PresenterConfigurationAccess::GoToChild (const OUString& rsPathToNode)
     103             : {
     104           0 :     if ( ! IsValid())
     105           0 :         return false;
     106             : 
     107           0 :     Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY);
     108           0 :     if (xNode.is())
     109             :     {
     110           0 :         maNode = GetConfigurationNode(
     111             :             Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY),
     112           0 :             rsPathToNode);
     113           0 :         if (Reference<XInterface>(maNode, UNO_QUERY).is())
     114           0 :             return true;
     115             :     }
     116             : 
     117           0 :     mxRoot = NULL;
     118           0 :     return false;
     119             : }
     120             : 
     121           0 : bool PresenterConfigurationAccess::GoToChild (const Predicate& rPredicate)
     122             : {
     123           0 :     if ( ! IsValid())
     124           0 :         return false;
     125             : 
     126           0 :     maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate);
     127           0 :     if (Reference<XInterface>(maNode, UNO_QUERY).is())
     128           0 :         return true;
     129             : 
     130           0 :     mxRoot = NULL;
     131           0 :     return false;
     132             : }
     133             : 
     134           0 : bool PresenterConfigurationAccess::SetProperty (
     135             :     const OUString& rsPropertyName,
     136             :     const Any& rValue)
     137             : {
     138           0 :     Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY);
     139           0 :     if (xProperties.is())
     140             :     {
     141           0 :         xProperties->setPropertyValue(rsPropertyName, rValue);
     142           0 :         return true;
     143             :     }
     144             :     else
     145           0 :         return false;
     146             : }
     147             : 
     148           0 : Any PresenterConfigurationAccess::GetConfigurationNode (
     149             :     const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
     150             :     const OUString& sPathToNode)
     151             : {
     152           0 :     if (sPathToNode.isEmpty())
     153           0 :         return Any(rxNode);
     154             : 
     155             :     try
     156             :     {
     157           0 :         if (rxNode.is())
     158             :         {
     159           0 :             return rxNode->getByHierarchicalName(sPathToNode);
     160             :         }
     161             :     }
     162           0 :     catch (const Exception& rException)
     163             :     {
     164             :         OSL_TRACE ("caught exception while getting configuration node %s: %s",
     165             :             OUStringToOString(sPathToNode, RTL_TEXTENCODING_UTF8).getStr(),
     166             :             OUStringToOString(rException.Message, RTL_TEXTENCODING_UTF8).getStr());
     167             :     }
     168             : 
     169           0 :     return Any();
     170             : }
     171             : 
     172           0 : Reference<beans::XPropertySet> PresenterConfigurationAccess::GetNodeProperties (
     173             :     const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
     174             :     const OUString& rsPathToNode)
     175             : {
     176           0 :     return Reference<beans::XPropertySet>(GetConfigurationNode(rxNode, rsPathToNode), UNO_QUERY);
     177             : }
     178             : 
     179           0 : void PresenterConfigurationAccess::CommitChanges()
     180             : {
     181           0 :     Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
     182           0 :     if (xConfiguration.is())
     183           0 :         xConfiguration->commitChanges();
     184           0 : }
     185             : 
     186           0 : void PresenterConfigurationAccess::ForAll (
     187             :     const Reference<container::XNameAccess>& rxContainer,
     188             :     const ::std::vector<OUString>& rArguments,
     189             :     const ItemProcessor& rProcessor)
     190             : {
     191           0 :     if (rxContainer.is())
     192             :     {
     193           0 :         ::std::vector<Any> aValues(rArguments.size());
     194           0 :         Sequence<OUString> aKeys (rxContainer->getElementNames());
     195           0 :         for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
     196             :         {
     197           0 :             bool bHasAllValues (true);
     198           0 :             const OUString& rsKey (aKeys[nItemIndex]);
     199           0 :             Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
     200           0 :             Reference<beans::XPropertySet> xSet (xSetItem, UNO_QUERY);
     201             :             OSL_ASSERT(xSet.is());
     202           0 :             if (xSetItem.is())
     203             :             {
     204             :                 // Get from the current item of the container the children
     205             :                 // that match the names in the rArguments list.
     206           0 :                 for (size_t nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
     207             :                 {
     208           0 :                     if ( ! xSetItem->hasByName(rArguments[nValueIndex]))
     209           0 :                         bHasAllValues = false;
     210             :                     else
     211           0 :                         aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
     212             :                 }
     213             :             }
     214             :             else
     215           0 :                 bHasAllValues = false;
     216           0 :             if (bHasAllValues)
     217           0 :                 rProcessor(rsKey,aValues);
     218           0 :         }
     219             :     }
     220           0 : }
     221             : 
     222           0 : void PresenterConfigurationAccess::ForAll (
     223             :     const Reference<container::XNameAccess>& rxContainer,
     224             :     const PropertySetProcessor& rProcessor)
     225             : {
     226           0 :     if (rxContainer.is())
     227             :     {
     228           0 :         Sequence<OUString> aKeys (rxContainer->getElementNames());
     229           0 :         for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
     230             :         {
     231           0 :             const OUString& rsKey (aKeys[nItemIndex]);
     232           0 :             Reference<beans::XPropertySet> xSet (rxContainer->getByName(rsKey), UNO_QUERY);
     233           0 :             if (xSet.is())
     234           0 :                 rProcessor(rsKey, xSet);
     235           0 :         }
     236             :     }
     237           0 : }
     238             : 
     239           0 : Any PresenterConfigurationAccess::Find (
     240             :     const Reference<container::XNameAccess>& rxContainer,
     241             :     const Predicate& rPredicate)
     242             : {
     243           0 :     if (rxContainer.is())
     244             :     {
     245           0 :         Sequence<OUString> aKeys (rxContainer->getElementNames());
     246           0 :         for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
     247             :         {
     248             :             Reference<beans::XPropertySet> xProperties (
     249           0 :                 rxContainer->getByName(aKeys[nItemIndex]),
     250           0 :                 UNO_QUERY);
     251           0 :             if (xProperties.is())
     252           0 :                 if (rPredicate(aKeys[nItemIndex], xProperties))
     253           0 :                     return Any(xProperties);
     254           0 :         }
     255             :     }
     256           0 :     return Any();
     257             : }
     258             : 
     259           0 : bool PresenterConfigurationAccess::IsStringPropertyEqual (
     260             :     const OUString& rsValue,
     261             :     const OUString& rsPropertyName,
     262             :     const css::uno::Reference<css::beans::XPropertySet>& rxNode)
     263             : {
     264           0 :     OUString sValue;
     265           0 :     if (GetProperty(rxNode, rsPropertyName) >>= sValue)
     266           0 :         return sValue == rsValue;
     267             :     else
     268           0 :         return false;
     269             : }
     270             : 
     271           0 : Any PresenterConfigurationAccess::GetProperty (
     272             :     const Reference<beans::XPropertySet>& rxProperties,
     273             :     const OUString& rsKey)
     274             : {
     275             :     OSL_ASSERT(rxProperties.is());
     276           0 :     if ( ! rxProperties.is())
     277           0 :         return Any();
     278             :     try
     279             :     {
     280           0 :         Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo());
     281           0 :         if (xInfo.is())
     282           0 :             if ( ! xInfo->hasPropertyByName(rsKey))
     283           0 :                 return Any();
     284           0 :         return rxProperties->getPropertyValue(rsKey);
     285             :     }
     286           0 :     catch (beans::UnknownPropertyException&)
     287             :     {
     288             :     }
     289           0 :     return Any();
     290             : }
     291             : 
     292          18 : } } // end of namespace sdext::tools
     293             : 
     294             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11