fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / vba / vbaworkbooks.cxx
blob424fdcc06ca221dd1b3907dbe17a029ac5ccdeff
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <comphelper/processfactory.hxx>
21 #include <cppuhelper/implbase1.hxx>
22 #include <cppuhelper/implbase3.hxx>
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>
48 #include <sfx2/objsh.hxx>
49 #include <tools/urlobj.hxx>
51 #include "vbaglobals.hxx"
52 #include "vbaworkbook.hxx"
53 #include "vbaworkbooks.hxx"
54 #include <vbahelper/vbahelper.hxx>
56 #include <vector>
57 #include <osl/file.hxx>
58 using namespace ::ooo::vba;
59 using namespace ::com::sun::star;
61 const sal_Int16 CUSTOM_CHAR = 5;
63 static uno::Any
64 getWorkbook( uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< sheet::XSpreadsheetDocument > &xDoc, const uno::Reference< XHelperInterface >& xParent )
66 // FIXME: fine as long as ScVbaWorkbook is stateless ...
67 uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
68 if( !xModel.is() )
69 return uno::Any();
71 uno::Reference< excel::XWorkbook > xWb( getVBADocument( xModel ), uno::UNO_QUERY );
72 if ( xWb.is() )
74 OSL_TRACE(" *** Returning Module uno Object *** ");
75 return uno::Any( xWb );
78 ScVbaWorkbook *pWb = new ScVbaWorkbook( xParent, xContext, xModel );
79 return uno::Any( uno::Reference< excel::XWorkbook > (pWb) );
82 class WorkBookEnumImpl : public EnumerationHelperImpl
84 public:
85 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 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
89 uno::Reference< sheet::XSpreadsheetDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
90 return getWorkbook( m_xContext, xDoc, m_xParent );
95 ScVbaWorkbooks::ScVbaWorkbooks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWorkbooks_BASE( xParent, xContext, VbaDocumentsBase::EXCEL_DOCUMENT )
98 // XEnumerationAccess
99 uno::Type
100 ScVbaWorkbooks::getElementType() throw (uno::RuntimeException)
102 return cppu::UnoType<excel::XWorkbook>::get();
104 uno::Reference< container::XEnumeration >
105 ScVbaWorkbooks::createEnumeration() throw (uno::RuntimeException)
107 // #FIXME its possible the WorkBookEnumImpl here doesn't reflect
108 // the state of this object ( although it should ) would be
109 // safer to create an enumeration based on this objects state
110 // rather than one effectively based of the desktop component
111 uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
112 return new WorkBookEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration() );
115 uno::Any
116 ScVbaWorkbooks::createCollectionObject( const css::uno::Any& aSource )
118 uno::Reference< sheet::XSpreadsheetDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
119 return getWorkbook( mxContext, xDoc, mxParent );
122 uno::Any SAL_CALL
123 ScVbaWorkbooks::Add( const uno::Any& Template ) throw (uno::RuntimeException, std::exception)
125 uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc;
126 sal_Int32 nWorkbookType = 0;
127 OUString aTemplateFileName;
128 if( Template >>= nWorkbookType )
130 // nWorkbookType is a constant from XlWBATemplate (added in Excel 2007)
131 // TODO: create chart-sheet if supported by Calc
133 xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
134 // create a document with one sheet only
135 uno::Reference< sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), uno::UNO_SET_THROW );
136 uno::Reference< container::XIndexAccess > xSheetsIA( xSheets, uno::UNO_QUERY_THROW );
137 while( xSheetsIA->getCount() > 1 )
139 uno::Reference< container::XNamed > xSheetName( xSheetsIA->getByIndex( xSheetsIA->getCount() - 1 ), uno::UNO_QUERY_THROW );
140 xSheets->removeByName( xSheetName->getName() );
143 else if( Template >>= aTemplateFileName )
145 // TODO: create document from template
146 xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
148 else if( !Template.hasValue() )
150 // regular spreadsheet document with configured number of sheets
151 xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
153 else
155 // illegal argument
156 throw uno::RuntimeException();
159 // need to set up the document modules ( and vba mode ) here
160 excel::setUpDocumentModules( xSpreadDoc );
161 if( xSpreadDoc.is() )
162 return getWorkbook( mxContext, xSpreadDoc, mxParent );
163 return uno::Any();
166 void SAL_CALL
167 ScVbaWorkbooks::Close() throw (uno::RuntimeException, std::exception)
171 bool
172 ScVbaWorkbooks::isTextFile( const OUString& sType )
174 // will return true if the file is
175 // a) a variant of a text file
176 // b) a csv file
177 // c) unknown
178 // returning true basically means treat this like a csv file
179 return sType == "generic_Text" || sType.isEmpty();
182 bool
183 ScVbaWorkbooks::isSpreadSheetFile( const OUString& sType )
185 // include calc_QPro etc. ? ( not for the moment anyway )
186 if ( sType.startsWith( "calc_MS" )
187 || sType.startsWith( "calc8" )
188 || sType.startsWith( "calc_StarOffice" ) )
189 return true;
190 return false;
193 OUString
194 ScVbaWorkbooks::getFileFilterType( const OUString& rFileName )
196 uno::Reference< document::XTypeDetection > xTypeDetect( mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxContext), uno::UNO_QUERY_THROW );
197 uno::Sequence< beans::PropertyValue > aMediaDesc(1);
198 aMediaDesc[ 0 ].Name = "URL";
199 aMediaDesc[ 0 ].Value <<= rFileName;
200 OUString sType = xTypeDetect->queryTypeByDescriptor( aMediaDesc, sal_True );
201 return sType;
204 // #TODO# #FIXME# can any of the unused params below be used?
205 uno::Any SAL_CALL
206 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)
208 // we need to detect if this is a URL, if not then assume it's a file path
209 OUString aURL;
210 INetURLObject aObj;
211 aObj.SetURL( rFileName );
212 bool bIsURL = aObj.GetProtocol() != INetProtocol::NotValid;
213 if ( bIsURL )
214 aURL = rFileName;
215 else
216 osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
218 uno::Sequence< beans::PropertyValue > sProps(0);
220 OUString sType = getFileFilterType( aURL );
221 // A text file means it needs to be processed as a csv file
222 if ( isTextFile( sType ) )
224 sal_Int32 nIndex = 0;
225 // Values for format
226 // 1 Tabs
227 // 2 Commas
228 // 3 Spaces
229 // 4 Semicolons
230 // 5 Nothing
231 // 6 Custom character (see the Delimiter argument
232 // no format means use the current delimiter
233 sProps.realloc( 3 );
234 sProps[ nIndex ].Name = "FilterOptions";
235 sal_Int16 delims[] = { 0 /*default not used*/, 9/*tab*/, 44/*comma*/, 32/*space*/, 59/*semicolon*/ };
236 static const char sRestOfFormat[] = ",34,0,1";
238 OUString sFormat;
239 sal_Int16 nFormat = 0; // default indicator
241 if ( Format.hasValue() )
243 Format >>= nFormat; // val of nFormat overwritten if extracted
244 // validate param
245 if ( nFormat < 1 || nFormat > 6 )
246 throw uno::RuntimeException("Illegal value for Format" );
249 sal_Int16 nDelim = getCurrentDelim();
251 if ( nFormat > 0 && nFormat < CUSTOM_CHAR )
253 nDelim = delims[ nFormat ];
255 else if ( nFormat > CUSTOM_CHAR )
257 // Need to check Delimiter param
258 if ( !Delimiter.hasValue() )
259 throw uno::RuntimeException("Expected value for Delimiter" );
260 OUString sStr;
261 Delimiter >>= sStr;
262 if ( !sStr.isEmpty() )
263 nDelim = sStr[0];
264 else
265 throw uno::RuntimeException("Incorrect value for Delimiter" );
268 getCurrentDelim() = nDelim; //set new current
270 sFormat = OUString::number( nDelim ) + sRestOfFormat;
271 sProps[ nIndex++ ].Value <<= sFormat;
272 sProps[ nIndex ].Name = "FilterName";
273 sProps[ nIndex++ ].Value <<= OUString( "Text - txt - csv (StarCalc)" );
274 // Ensure WORKAROUND_CSV_TXT_BUG_i60158 gets called in typedetection.cxx so
275 // csv is forced for deep detected 'writerxxx' types
276 sProps[ nIndex ].Name = "DocumentService";
277 sProps[ nIndex ].Value <<= OUString("com.sun.star.sheet.SpreadsheetDocument");
279 else if ( !isSpreadSheetFile( sType ) )
280 throw uno::RuntimeException("Bad Format" );
282 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( openDocument( rFileName, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
283 uno::Any aRet = getWorkbook( mxContext, xSpreadDoc, mxParent );
284 uno::Reference< excel::XWorkbook > xWBook( aRet, uno::UNO_QUERY );
285 if ( xWBook.is() )
286 xWBook->Activate();
287 return aRet;
290 OUString
291 ScVbaWorkbooks::getServiceImplName()
293 return OUString("ScVbaWorkbooks");
296 css::uno::Sequence<OUString>
297 ScVbaWorkbooks::getServiceNames()
299 static uno::Sequence< OUString > sNames;
300 if ( sNames.getLength() == 0 )
302 sNames.realloc( 1 );
303 sNames[0] = "ooo.vba.excel.Workbooks";
305 return sNames;
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */