LCOV - code coverage report
Current view: top level - framework/source/services - modulemanager.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 69 110 62.7 %
Date: 2015-06-13 12:38:46 Functions: 12 19 63.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 <sal/config.h>
      21             : 
      22             : #include <com/sun/star/frame/XFrame.hpp>
      23             : #include <com/sun/star/frame/XController.hpp>
      24             : #include <com/sun/star/frame/XModel.hpp>
      25             : #include <com/sun/star/frame/XModule.hpp>
      26             : #include <com/sun/star/lang/XServiceInfo.hpp>
      27             : #include <com/sun/star/frame/XModuleManager2.hpp>
      28             : #include <com/sun/star/container/XNameReplace.hpp>
      29             : #include <com/sun/star/container/XContainerQuery.hpp>
      30             : #include <com/sun/star/uno/XComponentContext.hpp>
      31             : 
      32             : #include <cppuhelper/implbase3.hxx>
      33             : #include <cppuhelper/supportsservice.hxx>
      34             : #include <comphelper/configurationhelper.hxx>
      35             : #include <comphelper/sequenceashashmap.hxx>
      36             : #include <comphelper/sequence.hxx>
      37             : #include <comphelper/enumhelper.hxx>
      38             : 
      39             : #include <boost/noncopyable.hpp>
      40             : 
      41             : namespace {
      42             : 
      43             : class ModuleManager:
      44             :     public cppu::WeakImplHelper3<
      45             :         css::lang::XServiceInfo,
      46             :         css::frame::XModuleManager2,
      47             :         css::container::XContainerQuery >,
      48             :     private boost::noncopyable
      49             : {
      50             : private:
      51             : 
      52             :     /** the global uno service manager.
      53             :         Must be used to create own needed services.
      54             :      */
      55             :     css::uno::Reference< css::uno::XComponentContext > m_xContext;
      56             : 
      57             :     /** points to the underlying configuration.
      58             :         This ModuleManager does not cache - it calls directly the
      59             :         configuration API!
      60             :       */
      61             :     css::uno::Reference< css::container::XNameAccess > m_xCFG;
      62             : 
      63             : public:
      64             : 
      65             :     ModuleManager(const css::uno::Reference< css::uno::XComponentContext >& xContext);
      66             : 
      67             :     virtual ~ModuleManager();
      68             : 
      69             :     // XServiceInfo
      70             :     virtual OUString SAL_CALL getImplementationName()
      71             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      72             : 
      73             :     virtual sal_Bool SAL_CALL supportsService(
      74             :         OUString const & ServiceName)
      75             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      76             : 
      77             :     virtual css::uno::Sequence< OUString > SAL_CALL
      78             :     getSupportedServiceNames() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      79             : 
      80             :     // XModuleManager
      81             :     virtual OUString SAL_CALL identify(const css::uno::Reference< css::uno::XInterface >& xModule)
      82             :         throw(css::lang::IllegalArgumentException,
      83             :               css::frame::UnknownModuleException,
      84             :               css::uno::RuntimeException, std::exception         ) SAL_OVERRIDE;
      85             : 
      86             :     // XNameReplace
      87             :     virtual void SAL_CALL replaceByName(const OUString& sName ,
      88             :                                         const css::uno::Any&   aValue)
      89             :         throw (css::lang::IllegalArgumentException   ,
      90             :                css::container::NoSuchElementException,
      91             :                css::lang::WrappedTargetException     ,
      92             :                css::uno::RuntimeException, std::exception            ) SAL_OVERRIDE;
      93             : 
      94             :     // XNameAccess
      95             :     virtual css::uno::Any SAL_CALL getByName(const OUString& sName)
      96             :         throw(css::container::NoSuchElementException,
      97             :               css::lang::WrappedTargetException     ,
      98             :               css::uno::RuntimeException, std::exception            ) SAL_OVERRIDE;
      99             : 
     100             :     virtual css::uno::Sequence< OUString > SAL_CALL getElementNames()
     101             :         throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     102             : 
     103             :     virtual sal_Bool SAL_CALL hasByName(const OUString& sName)
     104             :         throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     105             : 
     106             :     // XElementAccess
     107             :     virtual css::uno::Type SAL_CALL getElementType()
     108             :         throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     109             : 
     110             :     virtual sal_Bool SAL_CALL hasElements()
     111             :         throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     112             : 
     113             :     // XContainerQuery
     114             :     virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createSubSetEnumerationByQuery(const OUString& sQuery)
     115             :         throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     116             : 
     117             :     virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties)
     118             :         throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     119             : 
     120             : private:
     121             : 
     122             :     /** @short  makes the real identification of the module.
     123             : 
     124             :         @descr  It checks for the optional but preferred interface
     125             :                 XModule first. If this module does not exists at the
     126             :                 given component it tries to use XServiceInfo instead.
     127             : 
     128             :                 Note: This method try to locate a suitable module name.
     129             :                 Nothing else. Selecting the right component and throwing suitable
     130             :                 exceptions must be done outside.
     131             : 
     132             :         @see    identify()
     133             : 
     134             :         @param  xComponent
     135             :                 the module for identification.
     136             : 
     137             :         @return The identifier of the given module.
     138             :                 Can be empty if given component is not a real module !
     139             : 
     140             :         @threadsafe
     141             :      */
     142             :     OUString implts_identify(const css::uno::Reference< css::uno::XInterface >& xComponent);
     143             : };
     144             : 
     145         194 : ModuleManager::ModuleManager(const css::uno::Reference< css::uno::XComponentContext >& xContext)
     146         194 :     : m_xContext(xContext)
     147             : {
     148             :     m_xCFG.set( comphelper::ConfigurationHelper::openConfig(
     149             :                 m_xContext, "/org.openoffice.Setup/Office/Factories",
     150             :                 comphelper::ConfigurationHelper::E_READONLY),
     151         194 :             css::uno::UNO_QUERY_THROW );
     152         194 : }
     153             : 
     154         364 : ModuleManager::~ModuleManager()
     155             : {
     156         364 : }
     157             : 
     158           1 : OUString ModuleManager::getImplementationName()
     159             :     throw (css::uno::RuntimeException, std::exception)
     160             : {
     161           1 :     return OUString("com.sun.star.comp.framework.ModuleManager");
     162             : }
     163             : 
     164           0 : sal_Bool ModuleManager::supportsService(OUString const & ServiceName)
     165             :     throw (css::uno::RuntimeException, std::exception)
     166             : {
     167           0 :     return cppu::supportsService(this, ServiceName);
     168             : }
     169             : 
     170           1 : css::uno::Sequence< OUString > ModuleManager::getSupportedServiceNames()
     171             :     throw (css::uno::RuntimeException, std::exception)
     172             : {
     173           1 :     css::uno::Sequence< OUString > s(1);
     174           1 :     s[0] = "com.sun.star.frame.ModuleManager";
     175           1 :     return s;
     176             : }
     177             : 
     178      374320 : OUString SAL_CALL ModuleManager::identify(const css::uno::Reference< css::uno::XInterface >& xModule)
     179             :     throw(css::lang::IllegalArgumentException,
     180             :           css::frame::UnknownModuleException,
     181             :           css::uno::RuntimeException, std::exception         )
     182             : {
     183             :     // valid parameter?
     184      374320 :     css::uno::Reference< css::frame::XFrame >      xFrame     (xModule, css::uno::UNO_QUERY);
     185      748640 :     css::uno::Reference< css::awt::XWindow >       xWindow    (xModule, css::uno::UNO_QUERY);
     186      748640 :     css::uno::Reference< css::frame::XController > xController(xModule, css::uno::UNO_QUERY);
     187      748640 :     css::uno::Reference< css::frame::XModel >      xModel     (xModule, css::uno::UNO_QUERY);
     188             : 
     189      374320 :     if (
     190      425823 :         (!xFrame.is()     ) &&
     191      103006 :         (!xWindow.is()    ) &&
     192      477319 :         (!xController.is()) &&
     193       51496 :         (!xModel.is()     )
     194             :        )
     195             :     {
     196             :         throw css::lang::IllegalArgumentException(
     197             :                 OUString("Given module is not a frame nor a window, controller or model."),
     198             :                 static_cast< ::cppu::OWeakObject* >(this),
     199         351 :                 1);
     200             :     }
     201             : 
     202      373969 :     if (xFrame.is())
     203             :     {
     204      322817 :         xController = xFrame->getController();
     205      322817 :         xWindow     = xFrame->getComponentWindow();
     206             :     }
     207      373969 :     if (xController.is())
     208      294266 :         xModel = xController->getModel();
     209             : 
     210             :     // modules are implemented by the deepest component in hierarchy ...
     211             :     // Means: model -> controller -> window
     212             :     // No fallbacks to higher components are allowed !
     213             :     // Note : A frame provides access to module components only ... but it's not a module by himself.
     214             : 
     215      373969 :     OUString sModule;
     216      373969 :     if (xModel.is())
     217      345202 :         sModule = implts_identify(xModel);
     218       28767 :     else if (xController.is())
     219         209 :         sModule = implts_identify(xController);
     220       28558 :     else if (xWindow.is())
     221        2888 :         sModule = implts_identify(xWindow);
     222             : 
     223      373969 :     if (sModule.isEmpty())
     224             :         throw css::frame::UnknownModuleException(
     225             :                 OUString("Can not find suitable module for the given component."),
     226       28736 :                 static_cast< ::cppu::OWeakObject* >(this));
     227             : 
     228      719553 :     return sModule;
     229             : }
     230             : 
     231           0 : void SAL_CALL ModuleManager::replaceByName(const OUString& sName ,
     232             :                                            const css::uno::Any&   aValue)
     233             :     throw (css::lang::IllegalArgumentException   ,
     234             :            css::container::NoSuchElementException,
     235             :            css::lang::WrappedTargetException     ,
     236             :            css::uno::RuntimeException, std::exception            )
     237             : {
     238           0 :     ::comphelper::SequenceAsHashMap lProps(aValue);
     239           0 :     if (lProps.empty() )
     240             :     {
     241             :         throw css::lang::IllegalArgumentException(
     242             :                 OUString("No properties given to replace part of module."),
     243             :                 static_cast< cppu::OWeakObject * >(this),
     244           0 :                 2);
     245             :     }
     246             : 
     247             :     // get access to the element
     248             :     // Note: Dont use impl_getConfig() method here. Because it creates a readonly access only, further
     249             :     // it cache it as a member of this module manager instance. If we change some props there ... but dont
     250             :     // flush changes (because an error occurred) we will read them later. If we use a different config access
     251             :     // we can close it without a flush ... and our read data wont be affected .-)
     252             :     css::uno::Reference< css::uno::XInterface >         xCfg      = ::comphelper::ConfigurationHelper::openConfig(
     253             :                                                                         m_xContext,
     254             :                                                                         "/org.openoffice.Setup/Office/Factories",
     255           0 :                                                                         ::comphelper::ConfigurationHelper::E_STANDARD);
     256           0 :     css::uno::Reference< css::container::XNameAccess >  xModules (xCfg, css::uno::UNO_QUERY_THROW);
     257           0 :     css::uno::Reference< css::container::XNameReplace > xModule  ;
     258             : 
     259           0 :     xModules->getByName(sName) >>= xModule;
     260           0 :     if (!xModule.is())
     261             :     {
     262             :         throw css::uno::RuntimeException(
     263             :                 OUString("Was not able to get write access to the requested module entry inside configuration."),
     264           0 :                 static_cast< cppu::OWeakObject * >(this));
     265             :     }
     266             : 
     267           0 :     ::comphelper::SequenceAsHashMap::const_iterator pProp;
     268           0 :     for (  pProp  = lProps.begin();
     269           0 :            pProp != lProps.end()  ;
     270             :          ++pProp                  )
     271             :     {
     272           0 :         const OUString& sPropName  = pProp->first;
     273           0 :         const css::uno::Any&   aPropValue = pProp->second;
     274             : 
     275             :         // let "NoSuchElementException" out ! We support the same API ...
     276             :         // and without a flush() at the end all changed data before will be ignored !
     277           0 :         xModule->replaceByName(sPropName, aPropValue);
     278             :     }
     279             : 
     280           0 :     ::comphelper::ConfigurationHelper::flush(xCfg);
     281           0 : }
     282             : 
     283       21111 : css::uno::Any SAL_CALL ModuleManager::getByName(const OUString& sName)
     284             :     throw(css::container::NoSuchElementException,
     285             :           css::lang::WrappedTargetException     ,
     286             :           css::uno::RuntimeException, std::exception            )
     287             : {
     288             :     // get access to the element
     289       21111 :     css::uno::Reference< css::container::XNameAccess > xModule;
     290       21111 :     m_xCFG->getByName(sName) >>= xModule;
     291       21111 :     if (!xModule.is())
     292             :     {
     293             :         throw css::uno::RuntimeException(
     294             :                 OUString("Was not able to get write access to the requested module entry inside configuration."),
     295           0 :                 static_cast< cppu::OWeakObject * >(this));
     296             :     }
     297             : 
     298             :     // convert it to seq< PropertyValue >
     299       42222 :     const css::uno::Sequence< OUString > lPropNames = xModule->getElementNames();
     300       42222 :     comphelper::SequenceAsHashMap lProps;
     301             : 
     302       21111 :     lProps[OUString("ooSetupFactoryModuleIdentifier")] <<= sName;
     303      379998 :     for (sal_Int32 i = 0; i < lPropNames.getLength(); ++i)
     304             :     {
     305      358887 :         const OUString& sPropName = lPropNames[i];
     306      358887 :         lProps[sPropName] = xModule->getByName(sPropName);
     307             :     }
     308             : 
     309       42222 :     return css::uno::makeAny(lProps.getAsConstPropertyValueList());
     310             : }
     311             : 
     312        2194 : css::uno::Sequence< OUString > SAL_CALL ModuleManager::getElementNames()
     313             :     throw(css::uno::RuntimeException, std::exception)
     314             : {
     315        2194 :     return m_xCFG->getElementNames();
     316             : }
     317             : 
     318           0 : sal_Bool SAL_CALL ModuleManager::hasByName(const OUString& sName)
     319             :     throw(css::uno::RuntimeException, std::exception)
     320             : {
     321           0 :     return m_xCFG->hasByName(sName);
     322             : }
     323             : 
     324           0 : css::uno::Type SAL_CALL ModuleManager::getElementType()
     325             :     throw(css::uno::RuntimeException, std::exception)
     326             : {
     327           0 :     return cppu::UnoType<css::uno::Sequence< css::beans::PropertyValue >>::get();
     328             : }
     329             : 
     330           0 : sal_Bool SAL_CALL ModuleManager::hasElements()
     331             :     throw(css::uno::RuntimeException, std::exception)
     332             : {
     333           0 :     return m_xCFG->hasElements();
     334             : }
     335             : 
     336           0 : css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByQuery(const OUString&)
     337             :     throw(css::uno::RuntimeException, std::exception)
     338             : {
     339           0 :     return css::uno::Reference< css::container::XEnumeration >();
     340             : }
     341             : 
     342           0 : css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties)
     343             :     throw(css::uno::RuntimeException, std::exception)
     344             : {
     345           0 :     ::comphelper::SequenceAsHashMap lSearchProps(lProperties);
     346           0 :     const css::uno::Sequence< OUString > lModules = getElementNames();
     347           0 :     ::std::vector< css::uno::Any > lResult;
     348             : 
     349           0 :     for (sal_Int32 i = 0; i < lModules.getLength(); ++i)
     350             :     {
     351             :         try
     352             :         {
     353           0 :             ::comphelper::SequenceAsHashMap lModuleProps = getByName(lModules[i]);
     354           0 :             if (lModuleProps.match(lSearchProps))
     355           0 :                 lResult.push_back(css::uno::makeAny(lModuleProps.getAsConstPropertyValueList()));
     356             :         }
     357           0 :         catch(const css::uno::Exception&)
     358             :         {
     359             :         }
     360             :     }
     361             : 
     362             :     ::comphelper::OAnyEnumeration*                      pEnum =
     363           0 :                  new ::comphelper::OAnyEnumeration(comphelper::containerToSequence(lResult));
     364           0 :     css::uno::Reference< css::container::XEnumeration > xEnum(static_cast< css::container::XEnumeration* >(pEnum), css::uno::UNO_QUERY_THROW);
     365           0 :     return xEnum;
     366             : }
     367             : 
     368      348299 : OUString ModuleManager::implts_identify(const css::uno::Reference< css::uno::XInterface >& xComponent)
     369             : {
     370             :     // Search for an optional (!) interface XModule first.
     371             :     // Its used to overrule an existing service name. Used e.g. by our database form designer
     372             :     // which uses a writer module internally.
     373      348299 :     css::uno::Reference< css::frame::XModule > xModule(xComponent, css::uno::UNO_QUERY);
     374      348299 :     if (xModule.is())
     375      343489 :         return xModule->getIdentifier();
     376             : 
     377             :     // detect modules in a generic way ...
     378             :     // comparing service names with configured entries ...
     379        9620 :     css::uno::Reference< css::lang::XServiceInfo > xInfo(xComponent, css::uno::UNO_QUERY);
     380        4810 :     if (!xInfo.is())
     381        2888 :         return OUString();
     382             : 
     383        3844 :     const css::uno::Sequence< OUString > lKnownModules = getElementNames();
     384       31854 :     for (sal_Int32 i = 0; i < lKnownModules.getLength(); ++i)
     385             :     {
     386       31676 :         if (xInfo->supportsService(lKnownModules[i]))
     387        1744 :             return lKnownModules[i];
     388             :     }
     389             : 
     390      348477 :     return OUString();
     391             : }
     392             : 
     393         194 : struct Instance {
     394         194 :     explicit Instance(
     395             :         css::uno::Reference<css::uno::XComponentContext> const & context):
     396         194 :         instance(static_cast<cppu::OWeakObject *>(new ModuleManager(context)))
     397             :     {
     398         194 :     }
     399             : 
     400             :     css::uno::Reference<css::uno::XInterface> instance;
     401             : };
     402             : 
     403             : struct Singleton:
     404             :     public rtl::StaticWithArg<
     405             :         Instance, css::uno::Reference<css::uno::XComponentContext>, Singleton>
     406             : {};
     407             : 
     408             : }
     409             : 
     410             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     411      357909 : com_sun_star_comp_framework_ModuleManager_get_implementation(
     412             :     css::uno::XComponentContext *context,
     413             :     css::uno::Sequence<css::uno::Any> const &)
     414             : {
     415             :     return cppu::acquire(static_cast<cppu::OWeakObject *>(
     416      357909 :                 Singleton::get(context).instance.get()));
     417             : }
     418             : 
     419             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11