LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basic / source / basmgr / vbahelper.cxx
blob7fa101a8259e633935e5bcb93188a148ffb0d69f
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/Desktop.hpp>
27 #include <com/sun/star/frame/XModel2.hpp>
28 #include <com/sun/star/frame/ModuleManager.hpp>
29 #include <comphelper/processfactory.hxx>
31 namespace basic::vba {
33 using namespace ::com::sun::star;
36 namespace {
38 /** Create an instance of a module manager.
40 uno::Reference< frame::XModuleManager2 > lclCreateModuleManager()
42 uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext(), uno::UNO_SET_THROW );
43 return frame::ModuleManager::create(xContext);
46 typedef std::vector<uno::Reference<frame::XModel>> ModelVector;
48 ModelVector CreateDocumentsEnumeration(
49 const uno::Reference< frame::XModel >& rxModel)
51 ModelVector models;
52 try
54 uno::Reference< frame::XModuleManager2 > xModuleManager( lclCreateModuleManager() );
55 OUString aIdentifier = xModuleManager->identify( rxModel );
56 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( ::comphelper::getProcessComponentContext() );
57 uno::Reference< container::XEnumerationAccess > xComponentsEA( xDesktop->getComponents(), uno::UNO_SET_THROW );
58 uno::Reference< container::XEnumeration > xEnumeration( xComponentsEA->createEnumeration(), uno::UNO_SET_THROW );
59 while( xEnumeration->hasMoreElements() )
61 uno::Reference< frame::XModel > xCurrModel( xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
62 if( xModuleManager->identify( xCurrModel ) == aIdentifier )
63 models.push_back( xCurrModel );
66 catch(const uno::Exception& )
69 return models;
72 /** Locks or unlocks the controllers of the specified document model.
74 void lclLockControllers( const uno::Reference< frame::XModel >& rxModel, bool bLockControllers )
76 if( rxModel.is() ) try
78 if( bLockControllers )
79 rxModel->lockControllers();
80 else
81 rxModel->unlockControllers();
83 catch(const uno::Exception& )
89 /** Enables or disables the container windows of all controllers of the
90 specified document model.
92 void lclEnableContainerWindows( const uno::Reference< frame::XModel >& rxModel, bool bEnableWindows )
94 try
96 uno::Reference< frame::XModel2 > xModel2( rxModel, uno::UNO_QUERY_THROW );
97 uno::Reference< container::XEnumeration > xControllersEnum( xModel2->getControllers(), uno::UNO_SET_THROW );
98 // iterate over all controllers
99 while( xControllersEnum->hasMoreElements() )
103 uno::Reference< frame::XController > xController( xControllersEnum->nextElement(), uno::UNO_QUERY_THROW );
104 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
105 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
106 xWindow->setEnable( bEnableWindows );
108 catch(const uno::Exception& )
113 catch(const uno::Exception& )
119 typedef void (*ModifyDocumentFunc)( const uno::Reference< frame::XModel >&, bool );
121 /** Implementation iterating over all documents that have the same type as the
122 specified model, and calling the passed functor.
124 void lclIterateDocuments( ModifyDocumentFunc pModifyDocumentFunc, const uno::Reference< frame::XModel >& rxModel, bool bModificator )
126 ModelVector models(CreateDocumentsEnumeration(rxModel));
127 // iterate over all open documents
128 for (auto const& xCurrModel : models)
132 pModifyDocumentFunc(xCurrModel, bModificator);
134 catch (const uno::Exception&)
141 struct CurrDirPool
143 ::osl::Mutex maMutex;
144 std::map< OUString, OUString > maCurrDirs;
147 } // namespace
150 void lockControllersOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, bool bLockControllers )
152 lclIterateDocuments( &lclLockControllers, rxModel, bLockControllers );
156 void enableContainerWindowsOfAllDocuments( const uno::Reference< frame::XModel >& rxModel, bool bEnableWindows )
158 lclIterateDocuments( &lclEnableContainerWindows, rxModel, bEnableWindows );
162 void registerCurrentDirectory( const uno::Reference< frame::XModel >& rxModel, const OUString& rPath )
164 if( rPath.isEmpty() )
165 return;
167 static CurrDirPool StaticCurrDirPool;
169 CurrDirPool& rPool = StaticCurrDirPool;
170 ::osl::MutexGuard aGuard( rPool.maMutex );
173 uno::Reference< frame::XModuleManager2 > xModuleManager( lclCreateModuleManager() );
174 OUString aIdentifier = xModuleManager->identify( rxModel );
175 if( !aIdentifier.isEmpty() )
176 rPool.maCurrDirs[ aIdentifier ] = rPath;
178 catch(const uno::Exception& )
184 } // namespace
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */