LibreOffice Module basic (master) 1
vbahelper.cxx
Go to the documentation of this file.
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 <mutex>
25#include <vector>
26#include <com/sun/star/container/XEnumeration.hpp>
27#include <com/sun/star/frame/Desktop.hpp>
28#include <com/sun/star/frame/XModel2.hpp>
29#include <com/sun/star/frame/ModuleManager.hpp>
31
32namespace basic::vba {
33
34using namespace ::com::sun::star;
35
36
37namespace {
38
41uno::Reference< frame::XModuleManager2 > lclCreateModuleManager()
42{
43 uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext(), uno::UNO_SET_THROW );
44 return frame::ModuleManager::create(xContext);
45}
46
47typedef std::vector<uno::Reference<frame::XModel>> ModelVector;
48
49ModelVector CreateDocumentsEnumeration(
51{
52 ModelVector models;
53 try
54 {
55 uno::Reference< frame::XModuleManager2 > xModuleManager( lclCreateModuleManager() );
56 OUString aIdentifier = xModuleManager->identify( rxModel );
57 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( ::comphelper::getProcessComponentContext() );
58 uno::Reference< container::XEnumerationAccess > xComponentsEA( xDesktop->getComponents(), uno::UNO_SET_THROW );
59 uno::Reference< container::XEnumeration > xEnumeration( xComponentsEA->createEnumeration(), uno::UNO_SET_THROW );
60 while( xEnumeration->hasMoreElements() )
61 {
62 uno::Reference< frame::XModel > xCurrModel( xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
63 if( xModuleManager->identify( xCurrModel ) == aIdentifier )
64 models.push_back( xCurrModel );
65 }
66 }
67 catch(const uno::Exception& )
68 {
69 }
70 return models;
71}
72
75void lclLockControllers( const uno::Reference< frame::XModel >& rxModel, bool bLockControllers )
76{
77 if( rxModel.is() ) try
78 {
79 if( bLockControllers )
80 rxModel->lockControllers();
81 else
82 rxModel->unlockControllers();
83 }
84 catch(const uno::Exception& )
85 {
86 }
87}
88
89
93void lclEnableContainerWindows( const uno::Reference< frame::XModel >& rxModel, bool bEnableWindows )
94{
95 try
96 {
97 uno::Reference< frame::XModel2 > xModel2( rxModel, uno::UNO_QUERY_THROW );
98 uno::Reference< container::XEnumeration > xControllersEnum( xModel2->getControllers(), uno::UNO_SET_THROW );
99 // iterate over all controllers
100 while( xControllersEnum->hasMoreElements() )
101 {
102 try
103 {
104 uno::Reference< frame::XController > xController( xControllersEnum->nextElement(), uno::UNO_QUERY_THROW );
105 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
106 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
107 xWindow->setEnable( bEnableWindows );
108 }
109 catch(const uno::Exception& )
110 {
111 }
112 }
113 }
114 catch(const uno::Exception& )
115 {
116 }
117}
118
119
120typedef void (*ModifyDocumentFunc)( const uno::Reference< frame::XModel >&, bool );
121
125void lclIterateDocuments( ModifyDocumentFunc pModifyDocumentFunc, const uno::Reference< frame::XModel >& rxModel, bool bModificator )
126{
127 ModelVector models(CreateDocumentsEnumeration(rxModel));
128 // iterate over all open documents
129 for (auto const& xCurrModel : models)
130 {
131 try
132 {
133 pModifyDocumentFunc(xCurrModel, bModificator);
134 }
135 catch (const uno::Exception&)
136 {
137 }
138 }
139}
140
141
142struct CurrDirPool
143{
144 std::mutex maMutex;
145 std::map< OUString, OUString > maCurrDirs;
146};
147
148} // namespace
149
150
151void lockControllersOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, bool bLockControllers )
152{
153 lclIterateDocuments( &lclLockControllers, rxModel, bLockControllers );
154}
155
156
158{
159 lclIterateDocuments( &lclEnableContainerWindows, rxModel, bEnableWindows );
160}
161
162
163void registerCurrentDirectory( const uno::Reference< frame::XModel >& rxModel, const OUString& rPath )
164{
165 if( rPath.isEmpty() )
166 return;
167
168 static CurrDirPool StaticCurrDirPool;
169
170 CurrDirPool& rPool = StaticCurrDirPool;
171 std::unique_lock aGuard( rPool.maMutex );
172 try
173 {
174 uno::Reference< frame::XModuleManager2 > xModuleManager( lclCreateModuleManager() );
175 OUString aIdentifier = xModuleManager->identify( rxModel );
176 if( !aIdentifier.isEmpty() )
177 rPool.maCurrDirs[ aIdentifier ] = rPath;
178 }
179 catch(const uno::Exception& )
180 {
181 }
182}
183
184
185} // namespace
186
187/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void enableContainerWindowsOfAllDocuments(const uno::Reference< frame::XModel > &rxModel, bool bEnableWindows)
Definition: vbahelper.cxx:157
void lockControllersOfAllDocuments(const uno::Reference< frame::XModel > &rxModel, bool bLockControllers)
Definition: vbahelper.cxx:151
void registerCurrentDirectory(const uno::Reference< frame::XModel > &rxModel, const OUString &rPath)
Definition: vbahelper.cxx:163
Reference< XController > xController
Reference< XFrame > xFrame
std::mutex maMutex
Definition: vbahelper.cxx:144
std::map< OUString, OUString > maCurrDirs
Definition: vbahelper.cxx:145