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