LCOV - code coverage report
Current view: top level - basic/source/basmgr - vbahelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 51 69 73.9 %
Date: 2015-06-13 12:38:46 Functions: 11 14 78.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             : 
      21             : #include <basic/vbahelper.hxx>
      22             : 
      23             : #include <map>
      24             : #include <vector>
      25             : #include <com/sun/star/container/XEnumeration.hpp>
      26             : #include <com/sun/star/frame/Desktop.hpp>
      27             : #include <com/sun/star/frame/XModel2.hpp>
      28             : #include <com/sun/star/frame/ModuleManager.hpp>
      29             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      30             : #include <comphelper/processfactory.hxx>
      31             : #include <cppuhelper/implbase1.hxx>
      32             : #include <rtl/instance.hxx>
      33             : 
      34             : namespace basic {
      35             : namespace vba {
      36             : 
      37             : using namespace ::com::sun::star;
      38             : 
      39             : 
      40             : 
      41             : namespace {
      42             : 
      43             : /** Create an instance of a module manager.
      44             :  */
      45          54 : uno::Reference< frame::XModuleManager2 > lclCreateModuleManager()
      46             : {
      47          54 :     uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext(), uno::UNO_QUERY_THROW );
      48          54 :     return frame::ModuleManager::create(xContext);
      49             : }
      50             : 
      51             : 
      52             : 
      53             : /** Implementation of an enumeration of all open documents of the same type.
      54             :  */
      55         108 : class DocumentsEnumeration : public ::cppu::WeakImplHelper1< container::XEnumeration >
      56             : {
      57             : public:
      58             :     explicit DocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel );
      59             :     virtual sal_Bool SAL_CALL hasMoreElements() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
      60             :     virtual uno::Any SAL_CALL nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
      61             : private:
      62             :     typedef ::std::vector< uno::Reference< frame::XModel > > ModelVector;
      63             :     ModelVector maModels;
      64             :     ModelVector::iterator maModelIt;
      65             : };
      66             : 
      67          54 : DocumentsEnumeration::DocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel )
      68             : {
      69             :     try
      70             :     {
      71          54 :         uno::Reference< frame::XModuleManager2 > xModuleManager( lclCreateModuleManager() );
      72         108 :         OUString aIdentifier = xModuleManager->identify( rxModel );
      73         108 :         uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( ::comphelper::getProcessComponentContext() );
      74         108 :         uno::Reference< container::XEnumerationAccess > xComponentsEA( xDesktop->getComponents(), uno::UNO_SET_THROW );
      75         108 :         uno::Reference< container::XEnumeration > xEnumeration( xComponentsEA->createEnumeration(), uno::UNO_SET_THROW );
      76         164 :         while( xEnumeration->hasMoreElements() )
      77             :         {
      78          56 :             uno::Reference< frame::XModel > xCurrModel( xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
      79          56 :             if( xModuleManager->identify( xCurrModel ) == aIdentifier )
      80          56 :                 maModels.push_back( xCurrModel );
      81         110 :         }
      82             :     }
      83           0 :     catch(const uno::Exception& )
      84             :     {
      85             :     }
      86          54 :     maModelIt = maModels.begin();
      87          54 : }
      88             : 
      89         110 : sal_Bool SAL_CALL DocumentsEnumeration::hasMoreElements() throw (uno::RuntimeException, std::exception)
      90             : {
      91         110 :     return maModelIt != maModels.end();
      92             : }
      93             : 
      94          56 : uno::Any SAL_CALL DocumentsEnumeration::nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
      95             : {
      96          56 :     if( maModelIt == maModels.end() )
      97           0 :         throw container::NoSuchElementException();
      98          56 :     return uno::Any( *maModelIt++ );
      99             : }
     100             : 
     101             : 
     102             : 
     103             : /** Locks or unlocks the controllers of the specified document model.
     104             :  */
     105          28 : void lclLockControllers( const uno::Reference< frame::XModel >& rxModel, bool bLockControllers )
     106             : {
     107          28 :     if( rxModel.is() ) try
     108             :     {
     109          28 :         if( bLockControllers )
     110           0 :             rxModel->lockControllers();
     111             :         else
     112          28 :             rxModel->unlockControllers();
     113             :     }
     114           1 :     catch(const uno::Exception& )
     115             :     {
     116             :     }
     117          28 : }
     118             : 
     119             : 
     120             : 
     121             : /** Enables or disables the container windows of all controllers of the
     122             :     specified document model.
     123             :  */
     124          28 : void lclEnableContainerWindows( const uno::Reference< frame::XModel >& rxModel, bool bEnableWindows )
     125             : {
     126             :     try
     127             :     {
     128          28 :         uno::Reference< frame::XModel2 > xModel2( rxModel, uno::UNO_QUERY_THROW );
     129          56 :         uno::Reference< container::XEnumeration > xControllersEnum( xModel2->getControllers(), uno::UNO_SET_THROW );
     130             :         // iterate over all controllers
     131          84 :         while( xControllersEnum->hasMoreElements() )
     132             :         {
     133             :             try
     134             :             {
     135          28 :                 uno::Reference< frame::XController > xController( xControllersEnum->nextElement(), uno::UNO_QUERY_THROW );
     136          56 :                 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
     137          56 :                 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
     138          56 :                 xWindow->setEnable( bEnableWindows );
     139             :             }
     140           0 :             catch(const uno::Exception& )
     141             :             {
     142             :             }
     143          28 :         }
     144             :     }
     145           0 :     catch(const uno::Exception& )
     146             :     {
     147             :     }
     148          28 : }
     149             : 
     150             : 
     151             : 
     152             : typedef void (*ModifyDocumentFunc)( const uno::Reference< frame::XModel >&, bool );
     153             : 
     154             : /** Implementation iterating over all documents that have the same type as the
     155             :     specified model, and calling the passed functor.
     156             :  */
     157          54 : void lclIterateDocuments( ModifyDocumentFunc pModifyDocumentFunc, const uno::Reference< frame::XModel >& rxModel, bool bModificator )
     158             : {
     159          54 :     uno::Reference< container::XEnumeration > xDocumentsEnum( new DocumentsEnumeration( rxModel ) );
     160             :     // iterate over all open documents
     161         164 :     while( xDocumentsEnum->hasMoreElements() ) try
     162             :     {
     163          56 :         uno::Reference< frame::XModel > xCurrModel( xDocumentsEnum->nextElement(), uno::UNO_QUERY_THROW );
     164          56 :         pModifyDocumentFunc( xCurrModel, bModificator );
     165             :     }
     166           0 :     catch(const uno::Exception& )
     167             :     {
     168          54 :     }
     169          54 : }
     170             : 
     171             : 
     172             : 
     173           0 : struct CurrDirPool
     174             : {
     175             :     ::osl::Mutex maMutex;
     176             :     ::std::map< OUString, OUString > maCurrDirs;
     177             : };
     178             : 
     179             : struct StaticCurrDirPool : public ::rtl::Static< CurrDirPool, StaticCurrDirPool > {};
     180             : 
     181             : } // namespace
     182             : 
     183             : 
     184             : 
     185          27 : void lockControllersOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, bool bLockControllers )
     186             : {
     187          27 :     lclIterateDocuments( &lclLockControllers, rxModel, bLockControllers );
     188          27 : }
     189             : 
     190             : 
     191             : 
     192          27 : void enableContainerWindowsOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, bool bEnableWindows )
     193             : {
     194          27 :     lclIterateDocuments( &lclEnableContainerWindows, rxModel, bEnableWindows );
     195          27 : }
     196             : 
     197             : 
     198             : 
     199           0 : void registerCurrentDirectory( const uno::Reference< frame::XModel >& rxModel, const OUString& rPath )
     200             : {
     201           0 :     if( !rPath.isEmpty() )
     202             :     {
     203           0 :         CurrDirPool& rPool = StaticCurrDirPool::get();
     204           0 :         ::osl::MutexGuard aGuard( rPool.maMutex );
     205             :         try
     206             :         {
     207           0 :             uno::Reference< frame::XModuleManager2 > xModuleManager( lclCreateModuleManager() );
     208           0 :             OUString aIdentifier = xModuleManager->identify( rxModel );
     209           0 :             if( !aIdentifier.isEmpty() )
     210           0 :                 rPool.maCurrDirs[ aIdentifier ] = rPath;
     211             :         }
     212           0 :         catch(const uno::Exception& )
     213             :         {
     214           0 :         }
     215             :     }
     216           0 : }
     217             : 
     218             : 
     219             : 
     220             : } // namespace vba
     221             : } // namespace basic
     222             : 
     223             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11