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