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