LCOV - code coverage report
Current view: top level - sd/source/ui/framework/module - ModuleController.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 81 88 92.0 %
Date: 2014-04-11 Functions: 20 20 100.0 %
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             : 
      21             : #include "framework/ModuleController.hxx"
      22             : 
      23             : #include "tools/ConfigurationAccess.hxx"
      24             : #include <comphelper/processfactory.hxx>
      25             : #include <boost/bind.hpp>
      26             : #include <boost/unordered_map.hpp>
      27             : 
      28             : #include <tools/diagnose_ex.h>
      29             : 
      30             : using namespace ::com::sun::star;
      31             : using namespace ::com::sun::star::uno;
      32             : using namespace ::com::sun::star::drawing::framework;
      33             : using ::sd::tools::ConfigurationAccess;
      34             : 
      35             : namespace sd { namespace framework {
      36             : 
      37             : static const sal_uInt32 snFactoryPropertyCount (2);
      38             : static const sal_uInt32 snStartupPropertyCount (1);
      39             : 
      40             : 
      41             : 
      42             : 
      43          73 : class ModuleController::ResourceToFactoryMap
      44             :     : public ::boost::unordered_map<
      45             :     OUString,
      46             :     OUString,
      47             :     OUStringHash>
      48             : {
      49             : public:
      50          73 :     ResourceToFactoryMap (void) {}
      51             : };
      52             : 
      53             : 
      54          73 : class ModuleController::LoadedFactoryContainer
      55             :     : public ::boost::unordered_map<
      56             :     OUString,
      57             :     WeakReference<XInterface>,
      58             :     OUStringHash>
      59             : {
      60             : public:
      61          73 :     LoadedFactoryContainer (void) {}
      62             : };
      63             : 
      64             : 
      65             : 
      66             : 
      67             : 
      68          73 : Reference<XInterface> SAL_CALL ModuleController_createInstance (
      69             :     const Reference<XComponentContext>& rxContext)
      70             : {
      71          73 :     return Reference<XInterface>(ModuleController::CreateInstance(rxContext), UNO_QUERY);
      72             : }
      73             : 
      74             : 
      75             : 
      76             : 
      77          26 : OUString ModuleController_getImplementationName (void) throw(RuntimeException)
      78             : {
      79          26 :     return OUString("com.sun.star.comp.Draw.framework.module.ModuleController");
      80             : }
      81             : 
      82             : 
      83             : 
      84             : 
      85          11 : Sequence<OUString> SAL_CALL ModuleController_getSupportedServiceNames (void)
      86             :     throw (RuntimeException)
      87             : {
      88          11 :     static const OUString sServiceName("com.sun.star.drawing.framework.ModuleController");
      89          11 :     return Sequence<OUString>(&sServiceName, 1);
      90             : }
      91             : 
      92             : 
      93             : 
      94             : 
      95             : //===== ModuleController ======================================================
      96             : 
      97          73 : Reference<XModuleController> ModuleController::CreateInstance (
      98             :     const Reference<XComponentContext>& rxContext)
      99             : {
     100          73 :     return new ModuleController(rxContext);
     101             : }
     102             : 
     103             : 
     104             : 
     105             : 
     106          73 : ModuleController::ModuleController (const Reference<XComponentContext>& rxContext) throw()
     107             :     : ModuleControllerInterfaceBase(MutexOwner::maMutex),
     108             :       mxController(),
     109           0 :       mpResourceToFactoryMap(new ResourceToFactoryMap()),
     110          73 :       mpLoadedFactories(new LoadedFactoryContainer())
     111             : {
     112             :     (void)rxContext;
     113          73 :     LoadFactories(rxContext);
     114          73 : }
     115             : 
     116             : 
     117             : 
     118             : 
     119         134 : ModuleController::~ModuleController (void) throw()
     120             : {
     121         134 : }
     122             : 
     123             : 
     124             : 
     125             : 
     126          73 : void SAL_CALL ModuleController::disposing (void)
     127             : {
     128             :     // Break the cyclic reference back to DrawController object
     129          73 :     mpLoadedFactories.reset();
     130          73 :     mpResourceToFactoryMap.reset();
     131          73 :     mxController.clear();
     132          73 : }
     133             : 
     134             : 
     135             : 
     136             : 
     137          73 : void ModuleController::LoadFactories (const Reference<XComponentContext>& rxContext)
     138             : {
     139             :     try
     140             :     {
     141             :         ConfigurationAccess aConfiguration (
     142             :             rxContext,
     143             :             "/org.openoffice.Office.Impress/",
     144          73 :             ConfigurationAccess::READ_ONLY);
     145             :         Reference<container::XNameAccess> xFactories (
     146             :             aConfiguration.GetConfigurationNode("MultiPaneGUI/Framework/ResourceFactories"),
     147         146 :             UNO_QUERY);
     148         146 :         ::std::vector<OUString> aProperties (snFactoryPropertyCount);
     149          73 :         aProperties[0] = "ServiceName";
     150          73 :         aProperties[1] = "ResourceList";
     151             :         ConfigurationAccess::ForAll(
     152             :             xFactories,
     153             :             aProperties,
     154         146 :             ::boost::bind(&ModuleController::ProcessFactory, this, _2));
     155             :     }
     156           0 :     catch (Exception&)
     157             :     {
     158             :         DBG_UNHANDLED_EXCEPTION();
     159             :     }
     160          73 : }
     161             : 
     162             : 
     163             : 
     164             : 
     165         292 : void ModuleController::ProcessFactory (const ::std::vector<Any>& rValues)
     166             : {
     167             :     OSL_ASSERT(rValues.size() == snFactoryPropertyCount);
     168             : 
     169             :     // Get the service name of the factory.
     170         292 :     OUString sServiceName;
     171         292 :     rValues[0] >>= sServiceName;
     172             : 
     173             :     // Get all resource URLs that are created by the factory.
     174         584 :     Reference<container::XNameAccess> xResources (rValues[1], UNO_QUERY);
     175         584 :     ::std::vector<OUString> aURLs;
     176             :     tools::ConfigurationAccess::FillList(
     177             :         xResources,
     178             :         "URL",
     179         292 :         aURLs);
     180             : 
     181             :     SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ModuleController::adding factory " <<
     182             :         OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
     183             : 
     184             :     // Add the resource URLs to the map.
     185         292 :     ::std::vector<OUString>::const_iterator iResource;
     186        1606 :     for (iResource=aURLs.begin(); iResource!=aURLs.end(); ++iResource)
     187             :     {
     188        1314 :         (*mpResourceToFactoryMap)[*iResource] = sServiceName;
     189             :         SAL_INFO("sd.fwk", OSL_THIS_FUNC << ":    " <<
     190             :             OUStringToOString(*iResource, RTL_TEXTENCODING_UTF8).getStr());
     191         292 :     }
     192         292 : }
     193             : 
     194             : 
     195             : 
     196             : 
     197          73 : void ModuleController::InstantiateStartupServices (void)
     198             : {
     199             :     try
     200             :     {
     201             :         tools::ConfigurationAccess aConfiguration (
     202             :             "/org.openoffice.Office.Impress/",
     203          73 :             tools::ConfigurationAccess::READ_ONLY);
     204             :         Reference<container::XNameAccess> xFactories (
     205             :             aConfiguration.GetConfigurationNode("MultiPaneGUI/Framework/StartupServices"),
     206         146 :             UNO_QUERY);
     207         146 :         ::std::vector<OUString> aProperties (snStartupPropertyCount);
     208          73 :         aProperties[0] = "ServiceName";
     209             :         tools::ConfigurationAccess::ForAll(
     210             :             xFactories,
     211             :             aProperties,
     212         146 :             ::boost::bind(&ModuleController::ProcessStartupService, this, _2));
     213             :     }
     214           0 :     catch (Exception&)
     215             :     {
     216             :         OSL_TRACE("ERROR in ModuleController::InstantiateStartupServices");
     217             :     }
     218          73 : }
     219             : 
     220             : 
     221             : 
     222             : 
     223          73 : void ModuleController::ProcessStartupService (const ::std::vector<Any>& rValues)
     224             : {
     225             :     OSL_ASSERT(rValues.size() == snStartupPropertyCount);
     226             : 
     227             :     try
     228             :     {
     229             :         // Get the service name of the startup service.
     230          73 :         OUString sServiceName;
     231          73 :         rValues[0] >>= sServiceName;
     232             : 
     233             :         // Instantiate service.
     234             :         Reference<uno::XComponentContext> xContext =
     235         146 :             ::comphelper::getProcessComponentContext();
     236             : 
     237             :         // Create the startup service.
     238         146 :         Sequence<Any> aArguments(1);
     239          73 :         aArguments[0] <<= mxController;
     240             :         // Note that when the new object will be destroyed at the end of
     241             :         // this scope when it does not register itself anywhere.
     242             :         // Typically it will add itself as ConfigurationChangeListener
     243             :         // at the configuration controller.
     244          73 :         xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName, aArguments, xContext);
     245             : 
     246             :         SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ModuleController::created startup service " <<
     247          73 :             OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
     248             :     }
     249           0 :     catch (Exception&)
     250             :     {
     251             :         OSL_TRACE("ERROR in ModuleController::ProcessStartupServices");
     252             :     }
     253          73 : }
     254             : 
     255             : 
     256             : 
     257             : 
     258             : //----- XModuleController -----------------------------------------------------
     259             : 
     260         319 : void SAL_CALL ModuleController::requestResource (const OUString& rsResourceURL)
     261             :     throw (RuntimeException, std::exception)
     262             : {
     263         319 :     ResourceToFactoryMap::const_iterator iFactory (mpResourceToFactoryMap->find(rsResourceURL));
     264         319 :     if (iFactory != mpResourceToFactoryMap->end())
     265             :     {
     266             :         // Check that the factory has already been loaded and not been
     267             :         // destroyed in the meantime.
     268         167 :         Reference<XInterface> xFactory;
     269             :         LoadedFactoryContainer::const_iterator iLoadedFactory (
     270         167 :             mpLoadedFactories->find(iFactory->second));
     271         167 :         if (iLoadedFactory != mpLoadedFactories->end())
     272           0 :             xFactory = Reference<XInterface>(iLoadedFactory->second, UNO_QUERY);
     273         167 :         if ( ! xFactory.is())
     274             :         {
     275             :             // Create a new instance of the factory.
     276             :             Reference<uno::XComponentContext> xContext =
     277         167 :                 ::comphelper::getProcessComponentContext();
     278             : 
     279             :             // Create the factory service.
     280         334 :             Sequence<Any> aArguments(1);
     281         167 :             aArguments[0] <<= mxController;
     282             :             OSL_TRACE("creating resource %s",
     283             :                 OUStringToOString(iFactory->second, RTL_TEXTENCODING_ASCII_US).getStr());
     284             :             try
     285             :             {
     286         501 :                 xFactory = xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
     287         167 :                     iFactory->second,
     288             :                     aArguments,
     289         334 :                     xContext);
     290             :             }
     291           0 :             catch (const Exception&)
     292             :             {
     293             :                 OSL_TRACE("caught exception while creating factory.");
     294             :             }
     295             : 
     296             :             // Remember that this factory has been instanced.
     297         334 :             (*mpLoadedFactories)[iFactory->second] = xFactory;
     298         167 :         }
     299             :     }
     300         319 : }
     301             : 
     302             : 
     303             : 
     304             : 
     305             : //----- XInitialization -------------------------------------------------------
     306             : 
     307          73 : void SAL_CALL ModuleController::initialize (const Sequence<Any>& aArguments)
     308             :     throw (Exception, RuntimeException, std::exception)
     309             : {
     310          73 :     if (aArguments.getLength() > 0)
     311             :     {
     312             :         try
     313             :         {
     314             :             // Get the XController from the first argument.
     315          73 :             mxController = Reference<frame::XController>(aArguments[0], UNO_QUERY_THROW);
     316             : 
     317          73 :             InstantiateStartupServices();
     318             :         }
     319           0 :         catch (RuntimeException&)
     320             :         {}
     321             :     }
     322          73 : }
     323             : 
     324             : 
     325          48 : } } // end of namespace sd::framework
     326             : 
     327             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10