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

Generated by: LCOV version 1.10