Branch data 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/XDesktop.hpp>
27 : : #include <com/sun/star/frame/XModel2.hpp>
28 : : #include <com/sun/star/frame/XModuleManager.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 : 8 : uno::Reference< frame::XModuleManager > lclCreateModuleManager()
46 : : {
47 : 8 : uno::Reference< frame::XModuleManager > xModuleManager;
48 : : try
49 : : {
50 [ + - ][ + - ]: 8 : uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
51 [ + - ][ + - ]: 8 : xModuleManager.set( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ModuleManager" ) ) ), uno::UNO_QUERY );
[ + - ][ # # ]
[ + - ]
52 : : }
53 [ # # ]: 0 : catch(const uno::Exception& )
54 : : {
55 : : }
56 : 8 : return xModuleManager;
57 : : }
58 : :
59 : : // ----------------------------------------------------------------------------
60 : :
61 : : /** Implementation of an enumeration of all open documents of the same type.
62 : : */
63 [ - + ]: 16 : class DocumentsEnumeration : public ::cppu::WeakImplHelper1< container::XEnumeration >
64 : : {
65 : : public:
66 : : DocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel );
67 : : virtual sal_Bool SAL_CALL hasMoreElements() throw (uno::RuntimeException);
68 : : virtual uno::Any SAL_CALL nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException);
69 : : private:
70 : : typedef ::std::vector< uno::Reference< frame::XModel > > ModelVector;
71 : : ModelVector maModels;
72 : : ModelVector::iterator maModelIt;
73 : : };
74 : :
75 [ + - ]: 8 : DocumentsEnumeration::DocumentsEnumeration( const uno::Reference< frame::XModel >& rxModel )
76 : : {
77 : : try
78 : : {
79 [ + - ][ + - ]: 8 : uno::Reference< frame::XModuleManager > xModuleManager( lclCreateModuleManager(), uno::UNO_SET_THROW );
80 [ + - ][ + - ]: 8 : ::rtl::OUString aIdentifier = xModuleManager->identify( rxModel );
81 [ + - ][ + - ]: 8 : uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
82 [ + - ][ + - ]: 8 : uno::Reference< frame::XDesktop > xDesktop( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ) ), uno::UNO_QUERY_THROW );
[ + - ][ + - ]
83 [ + - ][ + - ]: 8 : uno::Reference< container::XEnumerationAccess > xComponentsEA( xDesktop->getComponents(), uno::UNO_SET_THROW );
[ + - ]
84 [ + - ][ + - ]: 8 : uno::Reference< container::XEnumeration > xEnumeration( xComponentsEA->createEnumeration(), uno::UNO_SET_THROW );
[ + - ]
85 [ + - ][ + - ]: 16 : while( xEnumeration->hasMoreElements() )
[ + + ]
86 : : {
87 [ + - ][ + - ]: 8 : uno::Reference< frame::XModel > xCurrModel( xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
[ + - ]
88 [ + - ][ + - ]: 8 : if( xModuleManager->identify( xCurrModel ) == aIdentifier )
[ + - ]
89 [ + - ]: 8 : maModels.push_back( xCurrModel );
90 [ # # ]: 16 : }
91 : : }
92 [ # # ]: 0 : catch(const uno::Exception& )
93 : : {
94 : : }
95 : 8 : maModelIt = maModels.begin();
96 : 8 : }
97 : :
98 : 16 : sal_Bool SAL_CALL DocumentsEnumeration::hasMoreElements() throw (uno::RuntimeException)
99 : : {
100 [ + - ]: 16 : return maModelIt != maModels.end();
101 : : }
102 : :
103 : 8 : uno::Any SAL_CALL DocumentsEnumeration::nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
104 : : {
105 [ + - ][ - + ]: 8 : if( maModelIt == maModels.end() )
106 [ # # ]: 0 : throw container::NoSuchElementException();
107 [ + - ]: 8 : return uno::Any( *maModelIt++ );
108 : : }
109 : :
110 : : // ----------------------------------------------------------------------------
111 : :
112 : : /** Locks or unlocks the controllers of the specified document model.
113 : : */
114 : 4 : void lclLockControllers( const uno::Reference< frame::XModel >& rxModel, sal_Bool bLockControllers )
115 : : {
116 [ + - ]: 4 : if( rxModel.is() ) try
117 : : {
118 [ - + ]: 4 : if( bLockControllers )
119 [ # # ][ # # ]: 0 : rxModel->lockControllers();
120 : : else
121 [ + - ][ + - ]: 4 : rxModel->unlockControllers();
122 : : }
123 : 0 : catch(const uno::Exception& )
124 : : {
125 : : }
126 [ # # ]: 4 : }
127 : :
128 : : // ----------------------------------------------------------------------------
129 : :
130 : : /** Enables or disables the container windows of all controllers of the
131 : : specified document model.
132 : : */
133 : 4 : void lclEnableContainerWindows( const uno::Reference< frame::XModel >& rxModel, sal_Bool bEnableWindows )
134 : : {
135 : : try
136 : : {
137 [ + - ]: 4 : uno::Reference< frame::XModel2 > xModel2( rxModel, uno::UNO_QUERY_THROW );
138 [ + - ][ + - ]: 4 : uno::Reference< container::XEnumeration > xControllersEnum( xModel2->getControllers(), uno::UNO_SET_THROW );
[ + - ]
139 : : // iterate over all controllers
140 [ + - ][ + - ]: 8 : while( xControllersEnum->hasMoreElements() )
[ + + ]
141 : : {
142 : : try
143 : : {
144 [ + - ][ + - ]: 4 : uno::Reference< frame::XController > xController( xControllersEnum->nextElement(), uno::UNO_QUERY_THROW );
[ + - ]
145 [ + - ][ + - ]: 4 : uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
[ + - ]
146 [ + - ][ + - ]: 4 : uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
[ + - ]
147 [ + - ][ # # ]: 4 : xWindow->setEnable( bEnableWindows );
[ + - ]
148 : : }
149 [ # # ]: 0 : catch(const uno::Exception& )
150 : : {
151 : : }
152 [ # # ]: 4 : }
153 : : }
154 : 0 : catch(const uno::Exception& )
155 : : {
156 : : }
157 : 4 : }
158 : :
159 : : // ----------------------------------------------------------------------------
160 : :
161 : : typedef void (*ModifyDocumentFunc)( const uno::Reference< frame::XModel >&, sal_Bool );
162 : :
163 : : /** Implementation iterating over all documents that have the same type as the
164 : : specified model, and calling the passed functor.
165 : : */
166 : 8 : void lclIterateDocuments( ModifyDocumentFunc pModifyDocumentFunc, const uno::Reference< frame::XModel >& rxModel, sal_Bool bModificator )
167 : : {
168 [ + - ][ + - ]: 8 : uno::Reference< container::XEnumeration > xDocumentsEnum( new DocumentsEnumeration( rxModel ) );
[ + - ]
169 : : // iterate over all open documents
170 [ + - ][ + - ]: 16 : while( xDocumentsEnum->hasMoreElements() ) try
[ + + ]
171 : : {
172 [ + - ][ + - ]: 8 : uno::Reference< frame::XModel > xCurrModel( xDocumentsEnum->nextElement(), uno::UNO_QUERY_THROW );
[ + - ]
173 [ # # ][ + - ]: 8 : pModifyDocumentFunc( xCurrModel, bModificator );
174 : : }
175 [ # # ]: 0 : catch(const uno::Exception& )
176 : : {
177 : 8 : }
178 : 8 : }
179 : :
180 : : // ----------------------------------------------------------------------------
181 : :
182 [ # # ]: 0 : struct CurrDirPool
183 : : {
184 : : ::osl::Mutex maMutex;
185 : : ::std::map< ::rtl::OUString, ::rtl::OUString > maCurrDirs;
186 : : };
187 : :
188 : : struct StaticCurrDirPool : public ::rtl::Static< CurrDirPool, StaticCurrDirPool > {};
189 : :
190 : : } // namespace
191 : :
192 : : // ============================================================================
193 : :
194 : 4 : void lockControllersOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, sal_Bool bLockControllers )
195 : : {
196 : 4 : lclIterateDocuments( &lclLockControllers, rxModel, bLockControllers );
197 : 4 : }
198 : :
199 : : // ============================================================================
200 : :
201 : 4 : void enableContainerWindowsOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, sal_Bool bEnableWindows )
202 : : {
203 : 4 : lclIterateDocuments( &lclEnableContainerWindows, rxModel, bEnableWindows );
204 : 4 : }
205 : :
206 : : // ============================================================================
207 : :
208 : 0 : void registerCurrentDirectory( const uno::Reference< frame::XModel >& rxModel, const ::rtl::OUString& rPath )
209 : : {
210 [ # # ]: 0 : if( !rPath.isEmpty() )
211 : : {
212 [ # # ]: 0 : CurrDirPool& rPool = StaticCurrDirPool::get();
213 [ # # ]: 0 : ::osl::MutexGuard aGuard( rPool.maMutex );
214 : : try
215 : : {
216 [ # # ][ # # ]: 0 : uno::Reference< frame::XModuleManager > xModuleManager( lclCreateModuleManager(), uno::UNO_SET_THROW );
217 [ # # ][ # # ]: 0 : ::rtl::OUString aIdentifier = xModuleManager->identify( rxModel );
218 [ # # ]: 0 : if( !aIdentifier.isEmpty() )
219 [ # # ][ # # ]: 0 : rPool.maCurrDirs[ aIdentifier ] = rPath;
220 : : }
221 [ # # ]: 0 : catch(const uno::Exception& )
222 : : {
223 [ # # ]: 0 : }
224 : : }
225 : 0 : }
226 : :
227 : : // ============================================================================
228 : :
229 : : } // namespace vba
230 : : } // namespace basic
231 : :
232 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|