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 : #include <comphelper/processfactory.hxx>
20 :
21 : #include <cppuhelper/implbase1.hxx>
22 : #include <cppuhelper/implbase3.hxx>
23 :
24 : #include <com/sun/star/frame/XDesktop.hpp>
25 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
26 : #include <com/sun/star/container/XEnumerationAccess.hpp>
27 : #include <com/sun/star/frame/XComponentLoader.hpp>
28 : #include <com/sun/star/lang/XComponent.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/util/XModifiable.hpp>
33 : #include <com/sun/star/frame/XStorable.hpp>
34 : #include <com/sun/star/lang/DisposedException.hpp>
35 : #include <com/sun/star/beans/PropertyVetoException.hpp>
36 : #include <com/sun/star/util/XCloseable.hpp>
37 : #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
38 : #include <com/sun/star/document/XTypeDetection.hpp>
39 : #include <com/sun/star/uri/XUriReference.hpp>
40 : #include <com/sun/star/uri/XUriReferenceFactory.hpp>
41 : #include <com/sun/star/script/vba/VBAEventId.hpp>
42 : #include <com/sun/star/script/vba/XVBACompatibility.hpp>
43 : #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
44 : #include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
45 : #include <com/sun/star/script/ModuleInfo.hpp>
46 : #include <com/sun/star/script/ModuleType.hpp>
47 :
48 : #include <sfx2/objsh.hxx>
49 : #include <tools/urlobj.hxx>
50 :
51 : #include "vbaglobals.hxx"
52 : #include "vbaworkbook.hxx"
53 : #include "vbaworkbooks.hxx"
54 : #include <vbahelper/vbahelper.hxx>
55 :
56 : #include <boost/unordered_map.hpp>
57 : #include <vector>
58 : #include <osl/file.hxx>
59 : using namespace ::ooo::vba;
60 : using namespace ::com::sun::star;
61 :
62 : const sal_Int16 CUSTOM_CHAR = 5;
63 :
64 : static uno::Any
65 18 : getWorkbook( uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< sheet::XSpreadsheetDocument > &xDoc, const uno::Reference< XHelperInterface >& xParent )
66 : {
67 : // FIXME: fine as long as ScVbaWorkbook is stateless ...
68 18 : uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
69 18 : if( !xModel.is() )
70 0 : return uno::Any();
71 :
72 36 : uno::Reference< excel::XWorkbook > xWb( getVBADocument( xModel ), uno::UNO_QUERY );
73 18 : if ( xWb.is() )
74 : {
75 : OSL_TRACE(" *** Returning Module uno Object *** ");
76 18 : return uno::Any( xWb );
77 : }
78 :
79 0 : ScVbaWorkbook *pWb = new ScVbaWorkbook( xParent, xContext, xModel );
80 18 : return uno::Any( uno::Reference< excel::XWorkbook > (pWb) );
81 : }
82 :
83 0 : class WorkBookEnumImpl : public EnumerationHelperImpl
84 : {
85 : public:
86 0 : WorkBookEnumImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ) {}
87 :
88 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
89 : {
90 0 : uno::Reference< sheet::XSpreadsheetDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
91 0 : return getWorkbook( m_xContext, xDoc, m_xParent );
92 : }
93 :
94 : };
95 :
96 20 : ScVbaWorkbooks::ScVbaWorkbooks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWorkbooks_BASE( xParent, xContext, VbaDocumentsBase::EXCEL_DOCUMENT )
97 : {
98 20 : }
99 : // XEnumerationAccess
100 : uno::Type
101 0 : ScVbaWorkbooks::getElementType() throw (uno::RuntimeException)
102 : {
103 0 : return cppu::UnoType<excel::XWorkbook>::get();
104 : }
105 : uno::Reference< container::XEnumeration >
106 0 : ScVbaWorkbooks::createEnumeration() throw (uno::RuntimeException)
107 : {
108 : // #FIXME its possible the WorkBookEnumImpl here doens't reflect
109 : // the state of this object ( although it should ) would be
110 : // safer to create an enumeration based on this objects state
111 : // rather than one effectively based of the desktop component
112 0 : uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
113 0 : return new WorkBookEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration() );
114 : }
115 :
116 : uno::Any
117 16 : ScVbaWorkbooks::createCollectionObject( const css::uno::Any& aSource )
118 : {
119 16 : uno::Reference< sheet::XSpreadsheetDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
120 16 : return getWorkbook( mxContext, xDoc, mxParent );
121 : }
122 :
123 : uno::Any SAL_CALL
124 2 : ScVbaWorkbooks::Add( const uno::Any& Template ) throw (uno::RuntimeException, std::exception)
125 : {
126 2 : uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc;
127 2 : sal_Int32 nWorkbookType = 0;
128 4 : OUString aTemplateFileName;
129 2 : if( Template >>= nWorkbookType )
130 : {
131 : // nWorkbookType is a constant from XlWBATemplate (added in Excel 2007)
132 : // TODO: create chart-sheet if supported by Calc
133 :
134 0 : xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
135 : // create a document with one sheet only
136 0 : uno::Reference< sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), uno::UNO_SET_THROW );
137 0 : uno::Reference< container::XIndexAccess > xSheetsIA( xSheets, uno::UNO_QUERY_THROW );
138 0 : while( xSheetsIA->getCount() > 1 )
139 : {
140 0 : uno::Reference< container::XNamed > xSheetName( xSheetsIA->getByIndex( xSheetsIA->getCount() - 1 ), uno::UNO_QUERY_THROW );
141 0 : xSheets->removeByName( xSheetName->getName() );
142 0 : }
143 : }
144 2 : else if( Template >>= aTemplateFileName )
145 : {
146 : // TODO: create document from template
147 0 : xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
148 : }
149 2 : else if( !Template.hasValue() )
150 : {
151 : // regular spreadsheet document with configured number of sheets
152 2 : xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
153 : }
154 : else
155 : {
156 : // illegal argument
157 0 : throw uno::RuntimeException();
158 : }
159 :
160 : // need to set up the document modules ( and vba mode ) here
161 2 : excel::setUpDocumentModules( xSpreadDoc );
162 2 : if( xSpreadDoc.is() )
163 2 : return getWorkbook( mxContext, xSpreadDoc, mxParent );
164 2 : return uno::Any();
165 : }
166 :
167 : void SAL_CALL
168 0 : ScVbaWorkbooks::Close() throw (uno::RuntimeException, std::exception)
169 : {
170 0 : closeDocuments();
171 0 : }
172 :
173 : bool
174 0 : ScVbaWorkbooks::isTextFile( const OUString& sType )
175 : {
176 : // will return true if the file is
177 : // a) a variant of a text file
178 : // b) a csv file
179 : // c) unknown
180 : // returning true basically means treat this like a csv file
181 0 : return sType.equals( "generic_Text" ) || sType.isEmpty();
182 : }
183 :
184 : bool
185 0 : ScVbaWorkbooks::isSpreadSheetFile( const OUString& sType )
186 : {
187 : // include calc_QPro etc. ? ( not for the moment anyway )
188 0 : if ( sType.startsWith( "calc_MS" )
189 0 : || sType.startsWith( "calc8" )
190 0 : || sType.startsWith( "calc_StarOffice" ) )
191 0 : return true;
192 0 : return false;
193 : }
194 :
195 : OUString
196 0 : ScVbaWorkbooks::getFileFilterType( const OUString& rFileName )
197 : {
198 0 : uno::Reference< document::XTypeDetection > xTypeDetect( mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxContext), uno::UNO_QUERY_THROW );
199 0 : uno::Sequence< beans::PropertyValue > aMediaDesc(1);
200 0 : aMediaDesc[ 0 ].Name = "URL";
201 0 : aMediaDesc[ 0 ].Value <<= rFileName;
202 0 : OUString sType = xTypeDetect->queryTypeByDescriptor( aMediaDesc, sal_True );
203 0 : return sType;
204 : }
205 :
206 : // #TODO# #FIXME# can any of the unused params below be used?
207 : uno::Any SAL_CALL
208 0 : ScVbaWorkbooks::Open( const OUString& rFileName, const uno::Any& /*UpdateLinks*/, const uno::Any& ReadOnly, const uno::Any& Format, const uno::Any& /*Password*/, const uno::Any& /*WriteResPassword*/, const uno::Any& /*IgnoreReadOnlyRecommended*/, const uno::Any& /*Origin*/, const uno::Any& Delimiter, const uno::Any& /*Editable*/, const uno::Any& /*Notify*/, const uno::Any& /*Converter*/, const uno::Any& /*AddToMru*/ ) throw (uno::RuntimeException, std::exception)
209 : {
210 : // we need to detect if this is a URL, if not then assume it's a file path
211 0 : OUString aURL;
212 0 : INetURLObject aObj;
213 0 : aObj.SetURL( rFileName );
214 0 : bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
215 0 : if ( bIsURL )
216 0 : aURL = rFileName;
217 : else
218 0 : osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
219 :
220 0 : uno::Sequence< beans::PropertyValue > sProps(0);
221 :
222 0 : OUString sType = getFileFilterType( aURL );
223 : // A text file means it needs to be processed as a csv file
224 0 : if ( isTextFile( sType ) )
225 : {
226 0 : sal_Int32 nIndex = 0;
227 : // Values for format
228 : // 1 Tabs
229 : // 2 Commas
230 : // 3 Spaces
231 : // 4 Semicolons
232 : // 5 Nothing
233 : // 6 Custom character (see the Delimiter argument
234 : // no format means use the current delimiter
235 0 : sProps.realloc( 3 );
236 0 : sProps[ nIndex ].Name = "FilterOptions";
237 0 : sal_Int16 delims[] = { 0 /*default not used*/, 9/*tab*/, 44/*comma*/, 32/*space*/, 59/*semicolon*/ };
238 0 : static OUString sRestOfFormat(",34,0,1" );
239 :
240 0 : OUString sFormat;
241 0 : sal_Int16 nFormat = 0; // default indicator
242 :
243 0 : if ( Format.hasValue() )
244 : {
245 0 : Format >>= nFormat; // val of nFormat overwritten if extracted
246 : // validate param
247 0 : if ( nFormat < 1 || nFormat > 6 )
248 0 : throw uno::RuntimeException("Illegal value for Format" );
249 : }
250 :
251 0 : sal_Int16 nDelim = getCurrentDelim();
252 :
253 0 : if ( nFormat > 0 && nFormat < CUSTOM_CHAR )
254 : {
255 0 : nDelim = delims[ nFormat ];
256 : }
257 0 : else if ( nFormat > CUSTOM_CHAR )
258 : {
259 : // Need to check Delimiter param
260 0 : if ( !Delimiter.hasValue() )
261 0 : throw uno::RuntimeException("Expected value for Delimiter" );
262 0 : OUString sStr;
263 0 : Delimiter >>= sStr;
264 0 : if ( !sStr.isEmpty() )
265 0 : nDelim = sStr[0];
266 : else
267 0 : throw uno::RuntimeException("Incorrect value for Delimiter" );
268 : }
269 :
270 0 : getCurrentDelim() = nDelim; //set new current
271 :
272 0 : sFormat = OUString::number( nDelim ) + sRestOfFormat;
273 0 : sProps[ nIndex++ ].Value <<= sFormat;
274 0 : sProps[ nIndex ].Name = "FilterName";
275 0 : sProps[ nIndex++ ].Value <<= OUString( "Text - txt - csv (StarCalc)" );
276 : // Ensure WORKAROUND_CSV_TXT_BUG_i60158 gets called in typedetection.cxx so
277 : // csv is forced for deep detected 'writerxxx' types
278 0 : sProps[ nIndex ].Name = "DocumentService";
279 0 : sProps[ nIndex ].Value <<= OUString("com.sun.star.sheet.SpreadsheetDocument");
280 : }
281 0 : else if ( !isSpreadSheetFile( sType ) )
282 0 : throw uno::RuntimeException("Bad Format" );
283 :
284 0 : uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( openDocument( rFileName, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
285 0 : uno::Any aRet = getWorkbook( mxContext, xSpreadDoc, mxParent );
286 0 : uno::Reference< excel::XWorkbook > xWBook( aRet, uno::UNO_QUERY );
287 0 : if ( xWBook.is() )
288 0 : xWBook->Activate();
289 0 : return aRet;
290 : }
291 :
292 : OUString
293 0 : ScVbaWorkbooks::getServiceImplName()
294 : {
295 0 : return OUString("ScVbaWorkbooks");
296 : }
297 :
298 : css::uno::Sequence<OUString>
299 0 : ScVbaWorkbooks::getServiceNames()
300 : {
301 0 : static uno::Sequence< OUString > sNames;
302 0 : if ( sNames.getLength() == 0 )
303 : {
304 0 : sNames.realloc( 1 );
305 0 : sNames[0] = "ooo.vba.excel.Workbooks";
306 : }
307 0 : return sNames;
308 12 : }
309 :
310 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|