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 : #include "vbahelper/vbadocumentsbase.hxx"
21 :
22 : #include <unotools/mediadescriptor.hxx>
23 : #include <comphelper/processfactory.hxx>
24 : #include <cppuhelper/implbase1.hxx>
25 : #include <cppuhelper/implbase3.hxx>
26 : #include <com/sun/star/frame/XDesktop.hpp>
27 : #include <com/sun/star/container/XEnumerationAccess.hpp>
28 : #include <com/sun/star/frame/XComponentLoader.hpp>
29 : #include <com/sun/star/lang/XComponent.hpp>
30 : #include <com/sun/star/frame/Desktop.hpp>
31 : #include <com/sun/star/frame/XModel.hpp>
32 : #include <com/sun/star/frame/XFrame.hpp>
33 : #include <com/sun/star/frame/XTitle.hpp>
34 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
35 : #include <com/sun/star/util/XModifiable.hpp>
36 : #include <com/sun/star/frame/XStorable.hpp>
37 : #include <com/sun/star/lang/DisposedException.hpp>
38 : #include <com/sun/star/beans/PropertyVetoException.hpp>
39 : #include <com/sun/star/util/XCloseable.hpp>
40 : #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
41 : #include <com/sun/star/document/XTypeDetection.hpp>
42 : #include <com/sun/star/document/MacroExecMode.hpp>
43 : #include <com/sun/star/uri/XUriReference.hpp>
44 : #include <com/sun/star/uri/XUriReferenceFactory.hpp>
45 : #include <com/sun/star/lang/XServiceInfo.hpp>
46 : #include <sfx2/objsh.hxx>
47 : #include <tools/urlobj.hxx>
48 : #include <osl/file.hxx>
49 : #include <unordered_map>
50 :
51 : #include "vbahelper/vbahelper.hxx"
52 : #include "vbahelper/vbaapplicationbase.hxx"
53 : #include "vbahelper/vbadocumentbase.hxx"
54 :
55 : using namespace ::ooo::vba;
56 : using namespace ::com::sun::star;
57 :
58 : static const char aSpreadsheetDocument[] = "com.sun.star.sheet.SpreadsheetDocument";
59 : static const char aTextDocument[] = "com.sun.star.text.TextDocument";
60 :
61 : typedef std::unordered_map< OUString,
62 : sal_Int32, OUStringHash,
63 : ::std::equal_to< OUString > > NameIndexHash;
64 :
65 : typedef std::vector < uno::Reference< frame::XModel > > Documents;
66 :
67 : typedef ::cppu::WeakImplHelper1< container::XEnumeration > DocumentsEnumImpl_BASE;
68 :
69 : // #FIXME clearly this is a candidate for some sort of helper base class as
70 : // this is a copy of SelectedSheetsEnum ( vbawindow.cxx )
71 :
72 20 : class DocumentsEnumImpl : public DocumentsEnumImpl_BASE
73 : {
74 : uno::Reference< uno::XComponentContext > m_xContext;
75 : Documents m_documents;
76 : Documents::const_iterator m_it;
77 :
78 : public:
79 0 : DocumentsEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const Documents& docs ) throw ( uno::RuntimeException ) : m_xContext( xContext ), m_documents( docs )
80 : {
81 0 : m_it = m_documents.begin();
82 0 : }
83 10 : explicit DocumentsEnumImpl( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::RuntimeException ) : m_xContext( xContext )
84 : {
85 10 : uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( m_xContext );
86 20 : uno::Reference< container::XEnumeration > mxComponents = xDesktop->getComponents()->createEnumeration();
87 32 : while( mxComponents->hasMoreElements() )
88 : {
89 12 : uno::Reference< frame::XModel > xNext( mxComponents->nextElement(), uno::UNO_QUERY );
90 12 : if ( xNext.is() )
91 12 : m_documents.push_back( xNext );
92 12 : }
93 20 : m_it = m_documents.begin();
94 10 : }
95 : // XEnumeration
96 34 : virtual sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
97 : {
98 34 : return m_it != m_documents.end();
99 : }
100 :
101 12 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
102 : {
103 12 : if ( !hasMoreElements() )
104 : {
105 0 : throw container::NoSuchElementException();
106 : }
107 12 : return makeAny( *(m_it++) );
108 : }
109 : };
110 :
111 : // #FIXME clearly this is also a candidate for some sort of helper base class as
112 : // a very similar one is used in vbawindow ( SelectedSheetsEnumAccess )
113 : // Maybe a template base class that does all of the operations on the hashmap
114 : // and vector only, and the sub-class does everything else
115 : // => ctor, createEnumeration & factory method need be defined ( to be called
116 : // by getByIndex, getByName )
117 : typedef ::cppu::WeakImplHelper3< container::XEnumerationAccess
118 : , com::sun::star::container::XIndexAccess
119 : , com::sun::star::container::XNameAccess
120 : > DocumentsAccessImpl_BASE;
121 :
122 20 : class DocumentsAccessImpl : public DocumentsAccessImpl_BASE
123 : {
124 : uno::Reference< uno::XComponentContext > m_xContext;
125 : Documents m_documents;
126 : NameIndexHash namesToIndices;
127 : VbaDocumentsBase::DOCUMENT_TYPE meDocType;
128 : public:
129 10 : DocumentsAccessImpl( const uno::Reference< uno::XComponentContext >& xContext, VbaDocumentsBase::DOCUMENT_TYPE eDocType ) throw (uno::RuntimeException) :m_xContext( xContext ), meDocType( eDocType )
130 : {
131 10 : uno::Reference< container::XEnumeration > xEnum = new DocumentsEnumImpl( m_xContext );
132 10 : sal_Int32 nIndex=0;
133 32 : while( xEnum->hasMoreElements() )
134 : {
135 12 : uno::Reference< lang::XServiceInfo > xServiceInfo( xEnum->nextElement(), uno::UNO_QUERY );
136 36 : if ( xServiceInfo.is()
137 60 : && ( ( xServiceInfo->supportsService( OUString(aSpreadsheetDocument) ) && meDocType == VbaDocumentsBase::EXCEL_DOCUMENT )
138 12 : || ( xServiceInfo->supportsService( OUString(aTextDocument) ) && meDocType == VbaDocumentsBase::WORD_DOCUMENT ) ) )
139 : {
140 12 : uno::Reference< frame::XModel > xModel( xServiceInfo, uno::UNO_QUERY_THROW ); // that the spreadsheetdocument is a xmodel is a given
141 12 : m_documents.push_back( xModel );
142 24 : OUString sName = VbaDocumentBase::getNameFromModel( xModel );
143 24 : namesToIndices[ sName ] = nIndex++;
144 : }
145 22 : }
146 :
147 10 : }
148 :
149 : //XEnumerationAccess
150 0 : virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
151 : {
152 0 : return new DocumentsEnumImpl( m_xContext, m_documents );
153 : }
154 : // XIndexAccess
155 1 : virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
156 : {
157 1 : return m_documents.size();
158 : }
159 7 : virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw ( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
160 : {
161 7 : if ( Index < 0
162 7 : || static_cast< Documents::size_type >(Index) >= m_documents.size() )
163 0 : throw lang::IndexOutOfBoundsException();
164 7 : return makeAny( m_documents[ Index ] ); // returns xspreadsheetdoc
165 : }
166 :
167 : //XElementAccess
168 0 : virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
169 : {
170 0 : return cppu::UnoType<frame::XModel>::get();
171 : }
172 :
173 0 : virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
174 : {
175 0 : return (!m_documents.empty());
176 : }
177 :
178 : //XNameAccess
179 1 : virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
180 : {
181 1 : NameIndexHash::const_iterator it = namesToIndices.find( aName );
182 1 : if ( it == namesToIndices.end() )
183 0 : throw container::NoSuchElementException();
184 1 : return makeAny( m_documents[ it->second ] );
185 :
186 : }
187 :
188 0 : virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
189 : {
190 0 : uno::Sequence< OUString > names( namesToIndices.size() );
191 0 : OUString* pString = names.getArray();
192 0 : NameIndexHash::const_iterator it = namesToIndices.begin();
193 0 : NameIndexHash::const_iterator it_end = namesToIndices.end();
194 0 : for ( ; it != it_end; ++it, ++pString )
195 0 : *pString = it->first;
196 0 : return names;
197 : }
198 :
199 0 : virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
200 : {
201 0 : NameIndexHash::const_iterator it = namesToIndices.find( aName );
202 0 : return (it != namesToIndices.end());
203 : }
204 :
205 : };
206 :
207 10 : VbaDocumentsBase::VbaDocumentsBase( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext, DOCUMENT_TYPE eDocType ) throw (uno::RuntimeException) : VbaDocumentsBase_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new DocumentsAccessImpl( xContext, eDocType ) ) ), meDocType( eDocType )
208 : {
209 10 : }
210 :
211 : namespace {
212 :
213 1 : void lclSetupComponent( const uno::Reference< lang::XComponent >& rxComponent, bool bScreenUpdating, bool bInteractive )
214 : {
215 1 : if( !bScreenUpdating ) try
216 : {
217 0 : uno::Reference< frame::XModel >( rxComponent, uno::UNO_QUERY_THROW )->lockControllers();
218 : }
219 0 : catch( uno::Exception& )
220 : {
221 : }
222 :
223 1 : if( !bInteractive ) try
224 : {
225 0 : uno::Reference< frame::XModel > xModel( rxComponent, uno::UNO_QUERY_THROW );
226 0 : uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
227 0 : uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
228 0 : uno::Reference< awt::XWindow >( xFrame->getContainerWindow(), uno::UNO_SET_THROW )->setEnable( sal_False );
229 : }
230 0 : catch( uno::Exception& )
231 : {
232 : }
233 1 : }
234 :
235 : } // namespace
236 :
237 1 : uno::Any VbaDocumentsBase::createDocument() throw (uno::RuntimeException, std::exception)
238 : {
239 : // #163808# determine state of Application.ScreenUpdating and Application.Interactive symbols (before new document is opened)
240 1 : uno::Reference< XApplicationBase > xApplication( Application(), uno::UNO_QUERY );
241 1 : bool bScreenUpdating = !xApplication.is() || xApplication->getScreenUpdating();
242 1 : bool bInteractive = !xApplication.is() || xApplication->getInteractive();
243 :
244 2 : uno::Reference< frame::XDesktop2 > xLoader = frame::Desktop::create(mxContext);
245 2 : OUString sURL;
246 1 : if( meDocType == WORD_DOCUMENT )
247 0 : sURL = "private:factory/swriter";
248 1 : else if( meDocType == EXCEL_DOCUMENT )
249 1 : sURL = "private:factory/scalc";
250 : else
251 0 : throw uno::RuntimeException( "Not implemented" );
252 :
253 : // prepare the media descriptor
254 2 : utl::MediaDescriptor aMediaDesc;
255 1 : aMediaDesc[ utl::MediaDescriptor::PROP_MACROEXECUTIONMODE() ] <<= document::MacroExecMode::USE_CONFIG;
256 1 : aMediaDesc.setComponentDataEntry( "ApplyFormDesignMode" , uno::Any( false ) );
257 :
258 : // create the new document
259 1 : uno::Reference< lang::XComponent > xComponent = xLoader->loadComponentFromURL(
260 : sURL , "_blank", 0,
261 2 : aMediaDesc.getAsConstPropertyValueList() );
262 :
263 : // #163808# lock document controllers and container window if specified by application
264 1 : lclSetupComponent( xComponent, bScreenUpdating, bInteractive );
265 :
266 2 : return uno::makeAny( xComponent );
267 : }
268 :
269 : // #TODO# #FIXME# can any of the unused params below be used?
270 0 : uno::Any VbaDocumentsBase::openDocument( const OUString& rFileName, const uno::Any& ReadOnly, const uno::Sequence< beans::PropertyValue >& rProps ) throw (uno::RuntimeException)
271 : {
272 : // #163808# determine state of Application.ScreenUpdating and Application.Interactive symbols (before new document is opened)
273 0 : uno::Reference< XApplicationBase > xApplication( Application(), uno::UNO_QUERY );
274 0 : bool bScreenUpdating = !xApplication.is() || xApplication->getScreenUpdating();
275 0 : bool bInteractive = !xApplication.is() || xApplication->getInteractive();
276 :
277 : // we need to detect if this is a URL, if not then assume it's a file path
278 0 : OUString aURL;
279 0 : INetURLObject aObj;
280 0 : aObj.SetURL( rFileName );
281 0 : bool bIsURL = aObj.GetProtocol() != INetProtocol::NotValid;
282 0 : if ( bIsURL )
283 0 : aURL = rFileName;
284 : else
285 0 : osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
286 0 : uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( mxContext );
287 :
288 0 : uno::Sequence< beans::PropertyValue > sProps( rProps );
289 0 : sProps.realloc( sProps.getLength() + 1 );
290 0 : sProps[ sProps.getLength() - 1 ].Name = "MacroExecutionMode";
291 0 : sProps[ sProps.getLength() - 1 ].Value <<= document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
292 :
293 0 : if ( ReadOnly.hasValue() )
294 : {
295 0 : bool bIsReadOnly = false;
296 0 : ReadOnly >>= bIsReadOnly;
297 0 : if ( bIsReadOnly )
298 : {
299 0 : sProps.realloc( sProps.getLength() + 1 );
300 0 : sProps[ sProps.getLength() - 1 ].Name = "ReadOnly";
301 0 : sProps[ sProps.getLength() - 1 ].Value <<= true;
302 : }
303 : }
304 :
305 0 : uno::Reference< lang::XComponent > xComponent = xDesktop->loadComponentFromURL( aURL,
306 : "_default" ,
307 : frame::FrameSearchFlag::CREATE,
308 0 : sProps);
309 :
310 : // #163808# lock document controllers and container window if specified by application
311 0 : lclSetupComponent( xComponent, bScreenUpdating, bInteractive );
312 :
313 0 : return uno::makeAny( xComponent );
314 : }
315 :
316 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|