1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
30 #include <rtl/instance.hxx>
35 using namespace ::com::sun::star
;
40 /** Create an instance of a module manager.
42 uno::Reference
< frame::XModuleManager2
> lclCreateModuleManager()
44 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext(), uno::UNO_SET_THROW
);
45 return frame::ModuleManager::create(xContext
);
48 typedef std::vector
<uno::Reference
<frame::XModel
>> ModelVector
;
50 ModelVector
CreateDocumentsEnumeration(
51 const uno::Reference
< frame::XModel
>& rxModel
)
56 uno::Reference
< frame::XModuleManager2
> xModuleManager( lclCreateModuleManager() );
57 OUString aIdentifier
= xModuleManager
->identify( rxModel
);
58 uno::Reference
< frame::XDesktop2
> xDesktop
= frame::Desktop::create( ::comphelper::getProcessComponentContext() );
59 uno::Reference
< container::XEnumerationAccess
> xComponentsEA( xDesktop
->getComponents(), uno::UNO_SET_THROW
);
60 uno::Reference
< container::XEnumeration
> xEnumeration( xComponentsEA
->createEnumeration(), uno::UNO_SET_THROW
);
61 while( xEnumeration
->hasMoreElements() )
63 uno::Reference
< frame::XModel
> xCurrModel( xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
64 if( xModuleManager
->identify( xCurrModel
) == aIdentifier
)
65 models
.push_back( xCurrModel
);
68 catch(const uno::Exception
& )
74 /** Locks or unlocks the controllers of the specified document model.
76 void lclLockControllers( const uno::Reference
< frame::XModel
>& rxModel
, bool bLockControllers
)
78 if( rxModel
.is() ) try
80 if( bLockControllers
)
81 rxModel
->lockControllers();
83 rxModel
->unlockControllers();
85 catch(const uno::Exception
& )
91 /** Enables or disables the container windows of all controllers of the
92 specified document model.
94 void lclEnableContainerWindows( const uno::Reference
< frame::XModel
>& rxModel
, bool bEnableWindows
)
98 uno::Reference
< frame::XModel2
> xModel2( rxModel
, uno::UNO_QUERY_THROW
);
99 uno::Reference
< container::XEnumeration
> xControllersEnum( xModel2
->getControllers(), uno::UNO_SET_THROW
);
100 // iterate over all controllers
101 while( xControllersEnum
->hasMoreElements() )
105 uno::Reference
< frame::XController
> xController( xControllersEnum
->nextElement(), uno::UNO_QUERY_THROW
);
106 uno::Reference
< frame::XFrame
> xFrame( xController
->getFrame(), uno::UNO_SET_THROW
);
107 uno::Reference
< awt::XWindow
> xWindow( xFrame
->getContainerWindow(), uno::UNO_SET_THROW
);
108 xWindow
->setEnable( bEnableWindows
);
110 catch(const uno::Exception
& )
115 catch(const uno::Exception
& )
121 typedef void (*ModifyDocumentFunc
)( const uno::Reference
< frame::XModel
>&, bool );
123 /** Implementation iterating over all documents that have the same type as the
124 specified model, and calling the passed functor.
126 void lclIterateDocuments( ModifyDocumentFunc pModifyDocumentFunc
, const uno::Reference
< frame::XModel
>& rxModel
, bool bModificator
)
128 ModelVector
models(CreateDocumentsEnumeration(rxModel
));
129 // iterate over all open documents
130 for (auto const& xCurrModel
: models
)
134 pModifyDocumentFunc(xCurrModel
, bModificator
);
136 catch (const uno::Exception
&)
145 ::osl::Mutex maMutex
;
146 std::map
< OUString
, OUString
> maCurrDirs
;
149 struct StaticCurrDirPool
: public ::rtl::Static
< CurrDirPool
, StaticCurrDirPool
> {};
154 void lockControllersOfAllDocuments( const uno::Reference
< frame::XModel
>& rxModel
, bool bLockControllers
)
156 lclIterateDocuments( &lclLockControllers
, rxModel
, bLockControllers
);
160 void enableContainerWindowsOfAllDocuments( const uno::Reference
< frame::XModel
>& rxModel
, bool bEnableWindows
)
162 lclIterateDocuments( &lclEnableContainerWindows
, rxModel
, bEnableWindows
);
166 void registerCurrentDirectory( const uno::Reference
< frame::XModel
>& rxModel
, const OUString
& rPath
)
168 if( !rPath
.isEmpty() )
170 CurrDirPool
& rPool
= StaticCurrDirPool::get();
171 ::osl::MutexGuard
aGuard( rPool
.maMutex
);
174 uno::Reference
< frame::XModuleManager2
> xModuleManager( lclCreateModuleManager() );
175 OUString aIdentifier
= xModuleManager
->identify( rxModel
);
176 if( !aIdentifier
.isEmpty() )
177 rPool
.maCurrDirs
[ aIdentifier
] = rPath
;
179 catch(const uno::Exception
& )
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */