1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_basic.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <basic/vbahelper.hxx> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <map> 34*cdf0e10cSrcweir #include <vector> 35*cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp> 36*cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp> 37*cdf0e10cSrcweir #include <com/sun/star/frame/XModel2.hpp> 38*cdf0e10cSrcweir #include <com/sun/star/frame/XModuleManager.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 40*cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 41*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 42*cdf0e10cSrcweir #include <rtl/instance.hxx> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir namespace basic { 45*cdf0e10cSrcweir namespace vba { 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir using namespace ::com::sun::star; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir // ============================================================================ 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir namespace { 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir /** Create an instance of a module manager. 54*cdf0e10cSrcweir */ 55*cdf0e10cSrcweir uno::Reference< frame::XModuleManager > lclCreateModuleManager() 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir uno::Reference< frame::XModuleManager > xModuleManager; 58*cdf0e10cSrcweir try 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW ); 61*cdf0e10cSrcweir xModuleManager.set( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ModuleManager" ) ) ), uno::UNO_QUERY ); 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir catch( uno::Exception& ) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir return xModuleManager; 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir /** Implementation of an enumeration of all open documents of the same type. 72*cdf0e10cSrcweir */ 73*cdf0e10cSrcweir class DocumentsEnumeration : public ::cppu::WeakImplHelper1< container::XEnumeration > 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir public: 76*cdf0e10cSrcweir DocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel ); 77*cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasMoreElements() throw (uno::RuntimeException); 78*cdf0e10cSrcweir virtual uno::Any SAL_CALL nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException); 79*cdf0e10cSrcweir private: 80*cdf0e10cSrcweir typedef ::std::vector< uno::Reference< frame::XModel > > ModelVector; 81*cdf0e10cSrcweir ModelVector maModels; 82*cdf0e10cSrcweir ModelVector::iterator maModelIt; 83*cdf0e10cSrcweir }; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir DocumentsEnumeration::DocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel ) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir try 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir uno::Reference< frame::XModuleManager > xModuleManager( lclCreateModuleManager(), uno::UNO_SET_THROW ); 90*cdf0e10cSrcweir ::rtl::OUString aIdentifier = xModuleManager->identify( rxModel ); 91*cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW ); 92*cdf0e10cSrcweir uno::Reference< frame::XDesktop > xDesktop( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ) ), uno::UNO_QUERY_THROW ); 93*cdf0e10cSrcweir uno::Reference< container::XEnumerationAccess > xComponentsEA( xDesktop->getComponents(), uno::UNO_SET_THROW ); 94*cdf0e10cSrcweir uno::Reference< container::XEnumeration > xEnumeration( xComponentsEA->createEnumeration(), uno::UNO_SET_THROW ); 95*cdf0e10cSrcweir while( xEnumeration->hasMoreElements() ) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir uno::Reference< frame::XModel > xCurrModel( xEnumeration->nextElement(), uno::UNO_QUERY_THROW ); 98*cdf0e10cSrcweir if( xModuleManager->identify( xCurrModel ) == aIdentifier ) 99*cdf0e10cSrcweir maModels.push_back( xCurrModel ); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir catch( uno::Exception& ) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir maModelIt = maModels.begin(); 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir sal_Bool SAL_CALL DocumentsEnumeration::hasMoreElements() throw (uno::RuntimeException) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir return maModelIt != maModels.end(); 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir uno::Any SAL_CALL DocumentsEnumeration::nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir if( maModelIt == maModels.end() ) 116*cdf0e10cSrcweir throw container::NoSuchElementException(); 117*cdf0e10cSrcweir return uno::Any( *maModelIt++ ); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir /** Locks or unlocks the controllers of the specified document model. 123*cdf0e10cSrcweir */ 124*cdf0e10cSrcweir void lclLockControllers( const uno::Reference< frame::XModel >& rxModel, sal_Bool bLockControllers ) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir if( rxModel.is() ) try 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir if( bLockControllers ) 129*cdf0e10cSrcweir rxModel->lockControllers(); 130*cdf0e10cSrcweir else 131*cdf0e10cSrcweir rxModel->unlockControllers(); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir catch( uno::Exception& ) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir /** Enables or disables the container windows of all controllers of the 141*cdf0e10cSrcweir specified document model. 142*cdf0e10cSrcweir */ 143*cdf0e10cSrcweir void lclEnableContainerWindows( const uno::Reference< frame::XModel >& rxModel, sal_Bool bEnableWindows ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir try 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir uno::Reference< frame::XModel2 > xModel2( rxModel, uno::UNO_QUERY_THROW ); 148*cdf0e10cSrcweir uno::Reference< container::XEnumeration > xControllersEnum( xModel2->getControllers(), uno::UNO_SET_THROW ); 149*cdf0e10cSrcweir // iterate over all controllers 150*cdf0e10cSrcweir while( xControllersEnum->hasMoreElements() ) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir try 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir uno::Reference< frame::XController > xController( xControllersEnum->nextElement(), uno::UNO_QUERY_THROW ); 155*cdf0e10cSrcweir uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW ); 156*cdf0e10cSrcweir uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW ); 157*cdf0e10cSrcweir xWindow->setEnable( bEnableWindows ); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir catch( uno::Exception& ) 160*cdf0e10cSrcweir { 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir catch( uno::Exception& ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir typedef void (*ModifyDocumentFunc)( const uno::Reference< frame::XModel >&, sal_Bool ); 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir /** Implementation iterating over all documents that have the same type as the 174*cdf0e10cSrcweir specified model, and calling the passed functor. 175*cdf0e10cSrcweir */ 176*cdf0e10cSrcweir void lclIterateDocuments( ModifyDocumentFunc pModifyDocumentFunc, const uno::Reference< frame::XModel >& rxModel, sal_Bool bModificator ) 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir uno::Reference< container::XEnumeration > xDocumentsEnum( new DocumentsEnumeration( rxModel ) ); 179*cdf0e10cSrcweir // iterate over all open documents 180*cdf0e10cSrcweir while( xDocumentsEnum->hasMoreElements() ) try 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir uno::Reference< frame::XModel > xCurrModel( xDocumentsEnum->nextElement(), uno::UNO_QUERY_THROW ); 183*cdf0e10cSrcweir pModifyDocumentFunc( xCurrModel, bModificator ); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir catch( uno::Exception& ) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir struct CurrDirPool 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir ::osl::Mutex maMutex; 195*cdf0e10cSrcweir ::std::map< ::rtl::OUString, ::rtl::OUString > maCurrDirs; 196*cdf0e10cSrcweir }; 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir struct StaticCurrDirPool : public ::rtl::Static< CurrDirPool, StaticCurrDirPool > {}; 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir } // namespace 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir // ============================================================================ 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir uno::Reference< container::XEnumeration > createDocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel ) 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir return new DocumentsEnumeration( rxModel ); 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir // ============================================================================ 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir void lockControllersOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, sal_Bool bLockControllers ) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir lclIterateDocuments( &lclLockControllers, rxModel, bLockControllers ); 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir // ============================================================================ 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir void enableContainerWindowsOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, sal_Bool bEnableWindows ) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir lclIterateDocuments( &lclEnableContainerWindows, rxModel, bEnableWindows ); 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir // ============================================================================ 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir void registerCurrentDirectory( const uno::Reference< frame::XModel >& rxModel, const ::rtl::OUString& rPath ) 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir if( rPath.getLength() > 0 ) 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir CurrDirPool& rPool = StaticCurrDirPool::get(); 230*cdf0e10cSrcweir ::osl::MutexGuard aGuard( rPool.maMutex ); 231*cdf0e10cSrcweir try 232*cdf0e10cSrcweir { 233*cdf0e10cSrcweir uno::Reference< frame::XModuleManager > xModuleManager( lclCreateModuleManager(), uno::UNO_SET_THROW ); 234*cdf0e10cSrcweir ::rtl::OUString aIdentifier = xModuleManager->identify( rxModel ); 235*cdf0e10cSrcweir if( aIdentifier.getLength() > 0 ) 236*cdf0e10cSrcweir rPool.maCurrDirs[ aIdentifier ] = rPath; 237*cdf0e10cSrcweir } 238*cdf0e10cSrcweir catch( uno::Exception& ) 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir // ============================================================================ 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir ::rtl::OUString getCurrentDirectory( const uno::Reference< frame::XModel >& rxModel ) 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir ::rtl::OUString aPath; 249*cdf0e10cSrcweir CurrDirPool& rPool = StaticCurrDirPool::get(); 250*cdf0e10cSrcweir ::osl::MutexGuard aGuard( rPool.maMutex ); 251*cdf0e10cSrcweir try 252*cdf0e10cSrcweir { 253*cdf0e10cSrcweir uno::Reference< frame::XModuleManager > xModuleManager( lclCreateModuleManager(), uno::UNO_SET_THROW ); 254*cdf0e10cSrcweir ::rtl::OUString aIdentifier = xModuleManager->identify( rxModel ); 255*cdf0e10cSrcweir aPath = rPool.maCurrDirs[ aIdentifier ]; 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir catch( uno::Exception& ) 258*cdf0e10cSrcweir { 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir return aPath; 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir // ============================================================================ 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir } // namespace vba 266*cdf0e10cSrcweir } // namespace basic 267