26#include <com/sun/star/container/XEnumerationAccess.hpp>
27#include <com/sun/star/lang/XComponent.hpp>
28#include <com/sun/star/frame/Desktop.hpp>
29#include <com/sun/star/frame/XModel.hpp>
30#include <com/sun/star/frame/XFrame.hpp>
31#include <com/sun/star/frame/FrameSearchFlag.hpp>
32#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
33#include <com/sun/star/document/MacroExecMode.hpp>
34#include <com/sun/star/lang/XServiceInfo.hpp>
37#include <osl/file.hxx>
38#include <unordered_map>
41#include <ooo/vba/XApplicationBase.hpp>
46typedef std::unordered_map< OUString,
49typedef std::vector < uno::Reference< frame::XModel > >
Documents;
56class DocumentsEnumImpl :
public ::cppu::WeakImplHelper< container::XEnumeration >
58 uno::Reference< uno::XComponentContext >
m_xContext;
60 Documents::const_iterator m_it;
64 DocumentsEnumImpl( uno::Reference< uno::XComponentContext > xContext,
Documents&& docs ) :
m_xContext(std::move( xContext )), m_documents( std::move(docs) )
66 m_it = m_documents.begin();
69 explicit DocumentsEnumImpl( uno::Reference< uno::XComponentContext > xContext ) :
m_xContext(std::move( xContext ))
71 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(
m_xContext );
72 uno::Reference< container::XEnumeration > xComponents = xDesktop->getComponents()->createEnumeration();
73 while( xComponents->hasMoreElements() )
75 uno::Reference< frame::XModel > xNext( xComponents->nextElement(), uno::UNO_QUERY );
77 m_documents.push_back( xNext );
79 m_it = m_documents.begin();
82 virtual sal_Bool SAL_CALL hasMoreElements( )
override
84 return m_it != m_documents.end();
87 virtual uno::Any SAL_CALL nextElement( )
override
89 if ( !hasMoreElements() )
91 throw container::NoSuchElementException();
93 return css::uno::Any( *(m_it++) );
105typedef ::cppu::WeakImplHelper< container::XEnumerationAccess
106 , css::container::XIndexAccess
107 , css::container::XNameAccess
114 uno::Reference< uno::XComponentContext >
m_xContext;
121 uno::Reference< container::XEnumeration > xEnum =
new DocumentsEnumImpl(
m_xContext );
123 while( xEnum->hasMoreElements() )
125 uno::Reference< lang::XServiceInfo > xServiceInfo( xEnum->nextElement(), uno::UNO_QUERY );
126 if ( xServiceInfo.is()
130 uno::Reference< frame::XModel >
xModel( xServiceInfo, uno::UNO_QUERY_THROW );
131 m_documents.push_back(
xModel );
140 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( )
override
142 return new DocumentsEnumImpl(
m_xContext, std::vector(m_documents) );
145 virtual ::sal_Int32 SAL_CALL getCount( )
override
147 return m_documents.size();
149 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32
Index )
override
153 throw lang::IndexOutOfBoundsException();
154 return css::uno::Any( m_documents[
Index ] );
158 virtual uno::Type SAL_CALL getElementType( )
override
163 virtual sal_Bool SAL_CALL hasElements( )
override
165 return (!m_documents.empty());
169 virtual uno::Any SAL_CALL getByName(
const OUString&
aName )
override
171 NameIndexHash::const_iterator it = namesToIndices.find(
aName );
172 if ( it == namesToIndices.end() )
173 throw container::NoSuchElementException();
174 return css::uno::Any( m_documents[ it->second ] );
178 virtual uno::Sequence< OUString > SAL_CALL getElementNames( )
override
183 virtual sal_Bool SAL_CALL hasByName(
const OUString&
aName )
override
185 NameIndexHash::const_iterator it = namesToIndices.find(
aName );
186 return (it != namesToIndices.end());
199void lclSetupComponent(
const uno::Reference< lang::XComponent >& rxComponent,
bool bScreenUpdating,
bool bInteractive )
201 if( !bScreenUpdating )
try
203 uno::Reference< frame::XModel >( rxComponent, uno::UNO_QUERY_THROW )->lockControllers();
205 catch( uno::Exception& )
209 if( !bInteractive )
try
211 uno::Reference< frame::XModel >
xModel( rxComponent, uno::UNO_QUERY_THROW );
212 uno::Reference< frame::XController >
xController(
xModel->getCurrentController(), uno::UNO_SET_THROW );
213 uno::Reference< frame::XFrame >
xFrame(
xController->getFrame(), uno::UNO_SET_THROW );
214 uno::Reference< awt::XWindow >(
xFrame->getContainerWindow(), uno::UNO_SET_THROW )->setEnable(
false );
216 catch( uno::Exception& )
226 uno::Reference< XApplicationBase > xApplication(
Application(), uno::UNO_QUERY );
227 bool bScreenUpdating = !xApplication.is() || xApplication->getScreenUpdating();
228 bool bInteractive =
true;
232 bInteractive = !xApplication.is() || xApplication->getInteractive();
234 catch(
const uno::Exception& )
238 uno::Reference< frame::XDesktop2 > xLoader = frame::Desktop::create(
mxContext);
241 sURL =
"private:factory/swriter";
243 sURL =
"private:factory/scalc";
245 throw uno::RuntimeException(
"Not implemented" );
253 uno::Reference< lang::XComponent > xComponent = xLoader->loadComponentFromURL(
255 aMediaDesc.getAsConstPropertyValueList() );
258 lclSetupComponent( xComponent, bScreenUpdating, bInteractive );
267 uno::Reference< XApplicationBase > xApplication(
Application(), uno::UNO_QUERY );
268 bool bScreenUpdating = !xApplication.is() || xApplication->getScreenUpdating();
269 bool bInteractive =
true;
273 bInteractive = !xApplication.is() || xApplication->getInteractive();
275 catch(
const uno::Exception& )
283 bool bIsURL = aObj.
GetProtocol() != INetProtocol::NotValid;
287 osl::FileBase::getFileURLFromSystemPath( rFileName,
aURL );
288 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(
mxContext );
290 uno::Sequence< beans::PropertyValue > sProps( rProps );
291 sProps.realloc( sProps.getLength() + 1 );
292 auto pProps = sProps.getArray();
293 pProps[ sProps.getLength() - 1 ].Name =
"MacroExecutionMode";
294 pProps[ sProps.getLength() - 1 ].Value <<= document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
298 bool bIsReadOnly =
false;
302 sProps.realloc( sProps.getLength() + 1 );
303 pProps = sProps.getArray();
304 pProps[ sProps.getLength() - 1 ].Name =
"ReadOnly";
305 pProps[ sProps.getLength() - 1 ].Value <<=
true;
309 uno::Reference< lang::XComponent > xComponent = xDesktop->loadComponentFromURL(
aURL,
311 frame::FrameSearchFlag::CREATE,
315 lclSetupComponent( xComponent, bScreenUpdating, bInteractive );
Reference< XComponentContext > m_xContext
INetProtocol GetProtocol() const
bool SetURL(std::u16string_view rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Any SAL_CALL Application() override
static OUString getNameFromModel(const css::uno::Reference< css::frame::XModel > &xModel)
VbaDocumentsBase(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, DOCUMENT_TYPE eDocType)
css::uno::Any openDocument(const OUString &Filename, const css::uno::Any &ReadOnly, const css::uno::Sequence< css::beans::PropertyValue > &rProps)
css::uno::Any createDocument()
css::uno::Type const & get()
std::vector< DocumentDescriptor > Documents
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Reference< XController > xController
Reference< XFrame > xFrame
Reference< XModel > xModel
::cppu::WeakImplHelper< container::XEnumerationAccess, css::container::XIndexAccess, css::container::XNameAccess > DocumentsAccessImpl_BASE
std::unordered_map< OUString, sal_Int32 > NameIndexHash
std::vector< uno::Reference< frame::XModel > > Documents
std::unordered_map< OUString, SCTAB > NameIndexHash