Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / basic / source / basmgr / vbahelper.cxx
blobd5c24667f39a0a25e4ac183f7d87301f07480cba
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <basic/vbahelper.hxx>
23 #include <map>
24 #include <vector>
25 #include <com/sun/star/container/XEnumeration.hpp>
26 #include <com/sun/star/frame/XDesktop.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>
34 namespace basic {
35 namespace vba {
37 using namespace ::com::sun::star;
39 // ============================================================================
41 namespace {
43 /** Create an instance of a module manager.
45 uno::Reference< frame::XModuleManager2 > lclCreateModuleManager()
47 uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext(), uno::UNO_QUERY_THROW );
48 return frame::ModuleManager::create(xContext);
51 // ----------------------------------------------------------------------------
53 /** Implementation of an enumeration of all open documents of the same type.
55 class DocumentsEnumeration : public ::cppu::WeakImplHelper1< container::XEnumeration >
57 public:
58 DocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel );
59 virtual sal_Bool SAL_CALL hasMoreElements() throw (uno::RuntimeException);
60 virtual uno::Any SAL_CALL nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException);
61 private:
62 typedef ::std::vector< uno::Reference< frame::XModel > > ModelVector;
63 ModelVector maModels;
64 ModelVector::iterator maModelIt;
67 DocumentsEnumeration::DocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel )
69 try
71 uno::Reference< frame::XModuleManager2 > xModuleManager( lclCreateModuleManager() );
72 ::rtl::OUString aIdentifier = xModuleManager->identify( rxModel );
73 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
74 uno::Reference< frame::XDesktop > xDesktop( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ) ), uno::UNO_QUERY_THROW );
75 uno::Reference< container::XEnumerationAccess > xComponentsEA( xDesktop->getComponents(), uno::UNO_SET_THROW );
76 uno::Reference< container::XEnumeration > xEnumeration( xComponentsEA->createEnumeration(), uno::UNO_SET_THROW );
77 while( xEnumeration->hasMoreElements() )
79 uno::Reference< frame::XModel > xCurrModel( xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
80 if( xModuleManager->identify( xCurrModel ) == aIdentifier )
81 maModels.push_back( xCurrModel );
84 catch(const uno::Exception& )
87 maModelIt = maModels.begin();
90 sal_Bool SAL_CALL DocumentsEnumeration::hasMoreElements() throw (uno::RuntimeException)
92 return maModelIt != maModels.end();
95 uno::Any SAL_CALL DocumentsEnumeration::nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
97 if( maModelIt == maModels.end() )
98 throw container::NoSuchElementException();
99 return uno::Any( *maModelIt++ );
102 // ----------------------------------------------------------------------------
104 /** Locks or unlocks the controllers of the specified document model.
106 void lclLockControllers( const uno::Reference< frame::XModel >& rxModel, sal_Bool bLockControllers )
108 if( rxModel.is() ) try
110 if( bLockControllers )
111 rxModel->lockControllers();
112 else
113 rxModel->unlockControllers();
115 catch(const uno::Exception& )
120 // ----------------------------------------------------------------------------
122 /** Enables or disables the container windows of all controllers of the
123 specified document model.
125 void lclEnableContainerWindows( const uno::Reference< frame::XModel >& rxModel, sal_Bool bEnableWindows )
129 uno::Reference< frame::XModel2 > xModel2( rxModel, uno::UNO_QUERY_THROW );
130 uno::Reference< container::XEnumeration > xControllersEnum( xModel2->getControllers(), uno::UNO_SET_THROW );
131 // iterate over all controllers
132 while( xControllersEnum->hasMoreElements() )
136 uno::Reference< frame::XController > xController( xControllersEnum->nextElement(), uno::UNO_QUERY_THROW );
137 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
138 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
139 xWindow->setEnable( bEnableWindows );
141 catch(const uno::Exception& )
146 catch(const uno::Exception& )
151 // ----------------------------------------------------------------------------
153 typedef void (*ModifyDocumentFunc)( const uno::Reference< frame::XModel >&, sal_Bool );
155 /** Implementation iterating over all documents that have the same type as the
156 specified model, and calling the passed functor.
158 void lclIterateDocuments( ModifyDocumentFunc pModifyDocumentFunc, const uno::Reference< frame::XModel >& rxModel, sal_Bool bModificator )
160 uno::Reference< container::XEnumeration > xDocumentsEnum( new DocumentsEnumeration( rxModel ) );
161 // iterate over all open documents
162 while( xDocumentsEnum->hasMoreElements() ) try
164 uno::Reference< frame::XModel > xCurrModel( xDocumentsEnum->nextElement(), uno::UNO_QUERY_THROW );
165 pModifyDocumentFunc( xCurrModel, bModificator );
167 catch(const uno::Exception& )
172 // ----------------------------------------------------------------------------
174 struct CurrDirPool
176 ::osl::Mutex maMutex;
177 ::std::map< ::rtl::OUString, ::rtl::OUString > maCurrDirs;
180 struct StaticCurrDirPool : public ::rtl::Static< CurrDirPool, StaticCurrDirPool > {};
182 } // namespace
184 // ============================================================================
186 void lockControllersOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, sal_Bool bLockControllers )
188 lclIterateDocuments( &lclLockControllers, rxModel, bLockControllers );
191 // ============================================================================
193 void enableContainerWindowsOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, sal_Bool bEnableWindows )
195 lclIterateDocuments( &lclEnableContainerWindows, rxModel, bEnableWindows );
198 // ============================================================================
200 void registerCurrentDirectory( const uno::Reference< frame::XModel >& rxModel, const ::rtl::OUString& rPath )
202 if( !rPath.isEmpty() )
204 CurrDirPool& rPool = StaticCurrDirPool::get();
205 ::osl::MutexGuard aGuard( rPool.maMutex );
208 uno::Reference< frame::XModuleManager2 > xModuleManager( lclCreateModuleManager() );
209 ::rtl::OUString aIdentifier = xModuleManager->identify( rxModel );
210 if( !aIdentifier.isEmpty() )
211 rPool.maCurrDirs[ aIdentifier ] = rPath;
213 catch(const uno::Exception& )
219 // ============================================================================
221 } // namespace vba
222 } // namespace basic
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */