LCOV - code coverage report
Current view: top level - framework/source/services - modulemanager.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 63 113 55.8 %
Date: 2014-11-03 Functions: 10 19 52.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 <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/sequenceasvector.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         286 : ModuleManager::ModuleManager(const css::uno::Reference< css::uno::XComponentContext >& xContext)
     146         286 :     : 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         286 :             css::uno::UNO_QUERY_THROW );
     152         286 : }
     153             : 
     154         548 : ModuleManager::~ModuleManager()
     155             : {
     156         548 : }
     157             : 
     158           0 : OUString ModuleManager::getImplementationName()
     159             :     throw (css::uno::RuntimeException, std::exception)
     160             : {
     161           0 :     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           0 : css::uno::Sequence< OUString > ModuleManager::getSupportedServiceNames()
     171             :     throw (css::uno::RuntimeException, std::exception)
     172             : {
     173           0 :     css::uno::Sequence< OUString > s(1);
     174           0 :     s[0] = "com.sun.star.frame.ModuleManager";
     175           0 :     return s;
     176             : }
     177             : 
     178      629319 : 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      629319 :     css::uno::Reference< css::frame::XFrame >      xFrame     (xModule, css::uno::UNO_QUERY);
     185     1258638 :     css::uno::Reference< css::awt::XWindow >       xWindow    (xModule, css::uno::UNO_QUERY);
     186     1258638 :     css::uno::Reference< css::frame::XController > xController(xModule, css::uno::UNO_QUERY);
     187     1258638 :     css::uno::Reference< css::frame::XModel >      xModel     (xModule, css::uno::UNO_QUERY);
     188             : 
     189      629319 :     if (
     190      706118 :         (!xFrame.is()     ) &&
     191      153598 :         (!xWindow.is()    ) &&
     192      782901 :         (!xController.is()) &&
     193       76783 :         (!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         488 :                 1);
     200             :     }
     201             : 
     202      628831 :     if (xFrame.is())
     203             :     {
     204      552520 :         xController = xFrame->getController();
     205      552520 :         xWindow     = xFrame->getComponentWindow();
     206             :     }
     207      628831 :     if (xController.is())
     208      504704 :         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      628831 :     OUString sModule;
     216      628831 :     if (xModel.is())
     217      580581 :         sModule = implts_identify(xModel);
     218       48250 :     else if (xController.is())
     219         418 :         sModule = implts_identify(xController);
     220       47832 :     else if (xWindow.is())
     221        4922 :         sModule = implts_identify(xWindow);
     222             : 
     223      628831 :     if (sModule.isEmpty())
     224             :         throw css::frame::UnknownModuleException(
     225             :                 OUString("Cant find suitable module for the given component."),
     226       48188 :                 static_cast< ::cppu::OWeakObject* >(this));
     227             : 
     228     1209962 :     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             :     try
     239             :     {
     240           0 :         ::comphelper::SequenceAsHashMap lProps(aValue);
     241           0 :         if (lProps.empty() )
     242             :         {
     243             :             throw css::lang::IllegalArgumentException(
     244             :                     OUString("No properties given to replace part of module."),
     245             :                     static_cast< cppu::OWeakObject * >(this),
     246           0 :                     2);
     247             :         }
     248             : 
     249             :         // get access to the element
     250             :         // Note: Dont use impl_getConfig() method here. Because it creates a readonly access only, further
     251             :         // it cache it as a member of this module manager instance. If we change some props there ... but dont
     252             :         // flush changes (because an error occurred) we will read them later. If we use a different config access
     253             :         // we can close it without a flush ... and our read data wont be affected .-)
     254             :         css::uno::Reference< css::uno::XInterface >         xCfg      = ::comphelper::ConfigurationHelper::openConfig(
     255             :                                                                             m_xContext,
     256             :                                                                             "/org.openoffice.Setup/Office/Factories",
     257           0 :                                                                             ::comphelper::ConfigurationHelper::E_STANDARD);
     258           0 :         css::uno::Reference< css::container::XNameAccess >  xModules (xCfg, css::uno::UNO_QUERY_THROW);
     259           0 :         css::uno::Reference< css::container::XNameReplace > xModule;
     260             : 
     261           0 :         xModules->getByName(sName) >>= xModule;
     262           0 :         if (!xModule.is())
     263             :         {
     264             :             throw css::uno::RuntimeException(
     265             :                     OUString("Was not able to get write access to the requested module entry inside configuration."),
     266           0 :                     static_cast< cppu::OWeakObject * >(this));
     267             :         }
     268             : 
     269           0 :         ::comphelper::SequenceAsHashMap::const_iterator pProp;
     270           0 :         for (  pProp  = lProps.begin();
     271           0 :                pProp != lProps.end();
     272             :              ++pProp                  )
     273             :         {
     274           0 :             const OUString& sPropName  = pProp->first;
     275           0 :             const css::uno::Any&   aPropValue = pProp->second;
     276             : 
     277             :             // let "NoSuchElementException" out ! We support the same API ...
     278             :             // and without a flush() at the end all changed data before will be ignored !
     279           0 :             xModule->replaceByName(sPropName, aPropValue);
     280             :         }
     281             : 
     282           0 :         ::comphelper::ConfigurationHelper::flush(xCfg);
     283             :     }
     284           0 :     catch (const css::beans::IllegalTypeException& e)
     285             :     {
     286           0 :         css::uno::Any a(e);
     287             :         throw css::lang::WrappedTargetException(
     288             :                 OUString("ModuleManager::replaceByName: IllegalType exception"),
     289           0 :                 css::uno::Reference<css::uno::XInterface>(*this), a);
     290             :     }
     291           0 : }
     292             : 
     293       35692 : css::uno::Any SAL_CALL ModuleManager::getByName(const OUString& sName)
     294             :     throw(css::container::NoSuchElementException,
     295             :           css::lang::WrappedTargetException     ,
     296             :           css::uno::RuntimeException, std::exception            )
     297             : {
     298             :     // get access to the element
     299       35692 :     css::uno::Reference< css::container::XNameAccess > xModule;
     300       35692 :     m_xCFG->getByName(sName) >>= xModule;
     301       35692 :     if (!xModule.is())
     302             :     {
     303             :         throw css::uno::RuntimeException(
     304             :                 OUString("Was not able to get write access to the requested module entry inside configuration."),
     305           0 :                 static_cast< cppu::OWeakObject * >(this));
     306             :     }
     307             : 
     308             :     // convert it to seq< PropertyValue >
     309       71384 :     const css::uno::Sequence< OUString > lPropNames = xModule->getElementNames();
     310       71384 :     comphelper::SequenceAsHashMap lProps;
     311             : 
     312       35692 :     lProps[OUString("ooSetupFactoryModuleIdentifier")] <<= sName;
     313      642456 :     for (sal_Int32 i = 0; i < lPropNames.getLength(); ++i)
     314             :     {
     315      606764 :         const OUString& sPropName = lPropNames[i];
     316      606764 :         lProps[sPropName] = xModule->getByName(sPropName);
     317             :     }
     318             : 
     319       71384 :     return css::uno::makeAny(lProps.getAsConstPropertyValueList());
     320             : }
     321             : 
     322        4226 : css::uno::Sequence< OUString > SAL_CALL ModuleManager::getElementNames()
     323             :     throw(css::uno::RuntimeException, std::exception)
     324             : {
     325        4226 :     return m_xCFG->getElementNames();
     326             : }
     327             : 
     328           0 : sal_Bool SAL_CALL ModuleManager::hasByName(const OUString& sName)
     329             :     throw(css::uno::RuntimeException, std::exception)
     330             : {
     331           0 :     return m_xCFG->hasByName(sName);
     332             : }
     333             : 
     334           0 : css::uno::Type SAL_CALL ModuleManager::getElementType()
     335             :     throw(css::uno::RuntimeException, std::exception)
     336             : {
     337           0 :     return ::getCppuType((const css::uno::Sequence< css::beans::PropertyValue >*)0);
     338             : }
     339             : 
     340           0 : sal_Bool SAL_CALL ModuleManager::hasElements()
     341             :     throw(css::uno::RuntimeException, std::exception)
     342             : {
     343           0 :     return m_xCFG->hasElements();
     344             : }
     345             : 
     346           0 : css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByQuery(const OUString&)
     347             :     throw(css::uno::RuntimeException, std::exception)
     348             : {
     349           0 :     return css::uno::Reference< css::container::XEnumeration >();
     350             : }
     351             : 
     352           0 : css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties)
     353             :     throw(css::uno::RuntimeException, std::exception)
     354             : {
     355           0 :     ::comphelper::SequenceAsHashMap lSearchProps(lProperties);
     356           0 :     const css::uno::Sequence< OUString > lModules = getElementNames();
     357           0 :     ::comphelper::SequenceAsVector< css::uno::Any > lResult;
     358             : 
     359           0 :     for (sal_Int32 i = 0; i < lModules.getLength(); ++i)
     360             :     {
     361             :         try
     362             :         {
     363           0 :             ::comphelper::SequenceAsHashMap lModuleProps = getByName(lModules[i]);
     364           0 :             if (lModuleProps.match(lSearchProps))
     365           0 :                 lResult.push_back(css::uno::makeAny(lModuleProps.getAsConstPropertyValueList()));
     366             :         }
     367           0 :         catch(const css::uno::Exception&)
     368             :         {
     369             :         }
     370             :     }
     371             : 
     372           0 :     ::comphelper::OAnyEnumeration*                      pEnum = new ::comphelper::OAnyEnumeration(lResult.getAsConstList());
     373           0 :     css::uno::Reference< css::container::XEnumeration > xEnum(static_cast< css::container::XEnumeration* >(pEnum), css::uno::UNO_QUERY_THROW);
     374           0 :     return xEnum;
     375             : }
     376             : 
     377      585921 : OUString ModuleManager::implts_identify(const css::uno::Reference< css::uno::XInterface >& xComponent)
     378             : {
     379             :     // Search for an optional (!) interface XModule first.
     380             :     // Its used to overrule an existing service name. Used e.g. by our database form designer
     381             :     // which uses a writer module internally.
     382      585921 :     css::uno::Reference< css::frame::XModule > xModule(xComponent, css::uno::UNO_QUERY);
     383      585921 :     if (xModule.is())
     384      577185 :         return xModule->getIdentifier();
     385             : 
     386             :     // detect modules in a generic way ...
     387             :     // comparing service names with configured entries ...
     388       17472 :     css::uno::Reference< css::lang::XServiceInfo > xInfo(xComponent, css::uno::UNO_QUERY);
     389        8736 :     if (!xInfo.is())
     390        4922 :         return OUString();
     391             : 
     392        7628 :     const css::uno::Sequence< OUString > lKnownModules = getElementNames();
     393       65250 :     for (sal_Int32 i = 0; i < lKnownModules.getLength(); ++i)
     394             :     {
     395       64894 :         if (xInfo->supportsService(lKnownModules[i]))
     396        3458 :             return lKnownModules[i];
     397             :     }
     398             : 
     399      586277 :     return OUString();
     400             : }
     401             : 
     402         286 : struct Instance {
     403         286 :     explicit Instance(
     404             :         css::uno::Reference<css::uno::XComponentContext> const & context):
     405         286 :         instance(static_cast<cppu::OWeakObject *>(new ModuleManager(context)))
     406             :     {
     407         286 :     }
     408             : 
     409             :     css::uno::Reference<css::uno::XInterface> instance;
     410             : };
     411             : 
     412             : struct Singleton:
     413             :     public rtl::StaticWithArg<
     414             :         Instance, css::uno::Reference<css::uno::XComponentContext>, Singleton>
     415             : {};
     416             : 
     417             : }
     418             : 
     419             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     420      590480 : com_sun_star_comp_framework_ModuleManager_get_implementation(
     421             :     css::uno::XComponentContext *context,
     422             :     css::uno::Sequence<css::uno::Any> const &)
     423             : {
     424             :     return cppu::acquire(static_cast<cppu::OWeakObject *>(
     425      590480 :                 Singleton::get(context).instance.get()));
     426             : }
     427             : 
     428             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10