LCOV - code coverage report
Current view: top level - framework/source/accelerators - documentacceleratorconfiguration.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 39 54 72.2 %
Date: 2015-06-13 12:38:46 Functions: 7 11 63.6 %
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] = "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         588 : 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         588 :     : DocumentAcceleratorConfiguration_BASE(xContext)
     117             : {
     118             :     {
     119         588 :         SolarMutexGuard g;
     120        1176 :         css::uno::Reference<css::embed::XStorage> xRoot;
     121         588 :         if (lArguments.getLength() == 1 && (lArguments[0] >>= xRoot))
     122             :         {
     123         588 :             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         588 :         }
     132             :     }
     133         588 : }
     134             : 
     135        1755 : DocumentAcceleratorConfiguration::~DocumentAcceleratorConfiguration()
     136             : {
     137         585 :     m_aPresetHandler.removeStorageListener(this);
     138        1170 : }
     139             : 
     140           4 : 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           4 :         SolarMutexGuard g;
     148           4 :         bForgetOldStorages = m_xDocumentRoot.is();
     149           4 :         m_xDocumentRoot = xStorage;
     150             :     }
     151             : 
     152           4 :     if (bForgetOldStorages)
     153           4 :         clearCache();
     154             : 
     155           4 :     if (xStorage.is())
     156           4 :         fillCache();
     157           4 : }
     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         592 : void DocumentAcceleratorConfiguration::fillCache()
     167             : {
     168         592 :     css::uno::Reference< css::embed::XStorage > xDocumentRoot;
     169             :     {
     170         592 :         SolarMutexGuard g;
     171         592 :         xDocumentRoot = m_xDocumentRoot;
     172             :     }
     173             : 
     174             :     // Sometimes we must live without a document root.
     175             :     // E.g. if the document is readonly ...
     176         592 :     if (!xDocumentRoot.is())
     177         592 :         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        1184 :     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         592 :             aLanguageTag);
     198             : 
     199         592 :         DocumentAcceleratorConfiguration::reload();
     200         592 :         m_aPresetHandler.addStorageListener(this);
     201             :     }
     202           0 :     catch(const css::uno::Exception&)
     203         592 :     {}
     204             : }
     205             : 
     206           4 : void DocumentAcceleratorConfiguration::clearCache()
     207             : {
     208           4 :     m_aPresetHandler.forgetCachedStorages();
     209           4 : }
     210             : 
     211             : } // namespace framework
     212             : 
     213             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     214         588 : com_sun_star_comp_framework_DocumentAcceleratorConfiguration_get_implementation(
     215             :     css::uno::XComponentContext *context,
     216             :     css::uno::Sequence<css::uno::Any> const &arguments)
     217             : {
     218         588 :     DocumentAcceleratorConfiguration *inst = new DocumentAcceleratorConfiguration(context, arguments);
     219         588 :     css::uno::XInterface *acquired_inst = cppu::acquire(inst);
     220             : 
     221         588 :     inst->fillCache();
     222             : 
     223         588 :     return acquired_inst;
     224             : }
     225             : 
     226             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11