LCOV - code coverage report
Current view: top level - framework/source/accelerators - documentacceleratorconfiguration.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 40 55 72.7 %
Date: 2014-11-03 Functions: 9 13 69.2 %
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 <accelerators/acceleratorconfiguration.hxx>
      21             : #include <accelerators/istoragelistener.hxx>
      22             : #include <accelerators/presethandler.hxx>
      23             : 
      24             : #include <xml/acceleratorconfigurationreader.hxx>
      25             : #include <xml/acceleratorconfigurationwriter.hxx>
      26             : #include <xml/saxnamespacefilter.hxx>
      27             : 
      28             : #include <acceleratorconst.h>
      29             : 
      30             : #include <com/sun/star/lang/XServiceInfo.hpp>
      31             : #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
      32             : 
      33             : #include <cppuhelper/implbase1.hxx>
      34             : #include <cppuhelper/supportsservice.hxx>
      35             : #include <comphelper/sequenceashashmap.hxx>
      36             : #include <i18nlangtag/languagetag.hxx>
      37             : #include <rtl/ref.hxx>
      38             : #include <vcl/svapp.hxx>
      39             : 
      40             : using namespace framework;
      41             : 
      42             : #define RESOURCETYPE_ACCELERATOR "accelerator"
      43             : 
      44             : namespace {
      45             : 
      46             : /**
      47             :     implements a read/write access to a document
      48             :     based accelerator configuration.
      49             :  */
      50             : 
      51             : typedef ::cppu::ImplInheritanceHelper1<
      52             :              XMLBasedAcceleratorConfiguration,
      53             :              css::lang::XServiceInfo> DocumentAcceleratorConfiguration_BASE;
      54             : 
      55             : class DocumentAcceleratorConfiguration : public DocumentAcceleratorConfiguration_BASE
      56             : {
      57             : private:
      58             : 
      59             :     /** points to the root storage of the outside document,
      60             :         where we can read/save our configuration data. */
      61             :     css::uno::Reference< css::embed::XStorage > m_xDocumentRoot;
      62             : 
      63             : public:
      64             : 
      65             :     /** initialize this instance and fill the internal cache.
      66             : 
      67             :         @param  xSMGR
      68             :                 reference to an uno service manager, which is used internally.
      69             :      */
      70             :     DocumentAcceleratorConfiguration(
      71             :             const css::uno::Reference< css::uno::XComponentContext >& xContext,
      72             :             const css::uno::Sequence< css::uno::Any >& lArguments);
      73             : 
      74             :     virtual ~DocumentAcceleratorConfiguration();
      75             : 
      76           0 :     virtual OUString SAL_CALL getImplementationName()
      77             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
      78             :     {
      79           0 :         return OUString("com.sun.star.comp.framework.DocumentAcceleratorConfiguration");
      80             :     }
      81             : 
      82           0 :     virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
      83             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
      84             :     {
      85           0 :         return cppu::supportsService(this, ServiceName);
      86             :     }
      87             : 
      88           0 :     virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
      89             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
      90             :     {
      91           0 :         css::uno::Sequence< OUString > aSeq(1);
      92           0 :         aSeq[0] = OUString("com.sun.star.ui.DocumentAcceleratorConfiguration");
      93           0 :         return aSeq;
      94             :     }
      95             : 
      96             :     // XUIConfigurationStorage
      97             :     virtual void SAL_CALL setStorage(const css::uno::Reference< css::embed::XStorage >& xStorage)
      98             :         throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      99             : 
     100             :     virtual sal_Bool SAL_CALL hasStorage()
     101             :         throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     102             : 
     103             :     /** read all data into the cache. */
     104             :     void fillCache();
     105             : 
     106             : private:
     107             : 
     108             :     /** forget all currently cached data AND(!)
     109             :         forget all currently used storages. */
     110             :     void clearCache();
     111             : };
     112             : 
     113        1462 : DocumentAcceleratorConfiguration::DocumentAcceleratorConfiguration(
     114             :         const css::uno::Reference< css::uno::XComponentContext >& xContext,
     115             :         const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& lArguments)
     116        1462 :     : DocumentAcceleratorConfiguration_BASE(xContext)
     117             : {
     118             :     {
     119        1462 :         SolarMutexGuard g;
     120        2924 :         css::uno::Reference<css::embed::XStorage> xRoot;
     121        1462 :         if (lArguments.getLength() == 1 && (lArguments[0] >>= xRoot))
     122             :         {
     123        1462 :             m_xDocumentRoot = xRoot;
     124             :         }
     125             :         else
     126             :         {
     127           0 :             ::comphelper::SequenceAsHashMap lArgs(lArguments);
     128           0 :             m_xDocumentRoot = lArgs.getUnpackedValueOrDefault(
     129             :                 OUString("DocumentRoot"),
     130           0 :                 css::uno::Reference< css::embed::XStorage >());
     131        1462 :         }
     132             :     }
     133        1462 : }
     134             : 
     135        4374 : DocumentAcceleratorConfiguration::~DocumentAcceleratorConfiguration()
     136             : {
     137        1458 :     m_aPresetHandler.removeStorageListener(this);
     138        2916 : }
     139             : 
     140           8 : void SAL_CALL DocumentAcceleratorConfiguration::setStorage(const css::uno::Reference< css::embed::XStorage >& xStorage)
     141             :     throw(css::uno::RuntimeException, std::exception)
     142             : {
     143             :     // Attention! xStorage must be accepted too, if it's NULL !
     144             : 
     145             :     bool bForgetOldStorages;
     146             :     {
     147           8 :         SolarMutexGuard g;
     148           8 :         bForgetOldStorages = m_xDocumentRoot.is();
     149           8 :         m_xDocumentRoot = xStorage;
     150             :     }
     151             : 
     152           8 :     if (bForgetOldStorages)
     153           8 :         clearCache();
     154             : 
     155           8 :     if (xStorage.is())
     156           8 :         fillCache();
     157           8 : }
     158             : 
     159           0 : sal_Bool SAL_CALL DocumentAcceleratorConfiguration::hasStorage()
     160             :     throw(css::uno::RuntimeException, std::exception)
     161             : {
     162           0 :     SolarMutexGuard g;
     163           0 :     return m_xDocumentRoot.is();
     164             : }
     165             : 
     166        1470 : void DocumentAcceleratorConfiguration::fillCache()
     167             : {
     168        1470 :     css::uno::Reference< css::embed::XStorage > xDocumentRoot;
     169             :     {
     170        1470 :         SolarMutexGuard g;
     171        1470 :         xDocumentRoot = m_xDocumentRoot;
     172             :     }
     173             : 
     174             :     // Sometimes we must live without a document root.
     175             :     // E.g. if the document is readonly ...
     176        1470 :     if (!xDocumentRoot.is())
     177        1470 :         return;
     178             : 
     179             :     // get current office locale ... but dont cache it.
     180             :     // Otherwise we must be listener on the configuration layer
     181             :     // which seems to superflous for this small implementation .-)
     182        2940 :     LanguageTag aLanguageTag( impl_ts_getLocale());
     183             : 
     184             :     // May be the current document does not contain any
     185             :     // accelerator config? Handle it gracefully :-)
     186             :     try
     187             :     {
     188             :         // Note: The used preset class is threadsafe by itself ... and live if we live!
     189             :         // We do not need any mutex here.
     190             : 
     191             :         // open the folder, where the configuration exists
     192             :         m_aPresetHandler.connectToResource(
     193             :             PresetHandler::E_DOCUMENT,
     194             :             RESOURCETYPE_ACCELERATOR,
     195             :             OUString(),
     196             :             xDocumentRoot,
     197        1470 :             aLanguageTag);
     198             : 
     199        1470 :         DocumentAcceleratorConfiguration::reload();
     200        1470 :         m_aPresetHandler.addStorageListener(this);
     201             :     }
     202           0 :     catch(const css::uno::Exception&)
     203        1470 :     {}
     204             : }
     205             : 
     206           8 : void DocumentAcceleratorConfiguration::clearCache()
     207             : {
     208           8 :     m_aPresetHandler.forgetCachedStorages();
     209           8 : }
     210             : 
     211             : } // namespace framework
     212             : 
     213             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     214        1462 : com_sun_star_comp_framework_DocumentAcceleratorConfiguration_get_implementation(
     215             :     css::uno::XComponentContext *context,
     216             :     css::uno::Sequence<css::uno::Any> const &arguments)
     217             : {
     218        1462 :     DocumentAcceleratorConfiguration *inst = new DocumentAcceleratorConfiguration(context, arguments);
     219        1462 :     css::uno::XInterface *acquired_inst = cppu::acquire(inst);
     220             : 
     221        1462 :     inst->fillCache();
     222             : 
     223        1462 :     return acquired_inst;
     224         951 : }
     225             : 
     226             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10