1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
21 #include <com/sun/star/container/XEnumerationAccess.hpp>
22 #include <com/sun/star/frame/XModel.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <com/sun/star/document/XTypeDetection.hpp>
26 #include <tools/urlobj.hxx>
28 #include "excelvbahelper.hxx"
29 #include "vbaworkbook.hxx"
30 #include "vbaworkbooks.hxx"
31 #include <vbahelper/vbahelper.hxx>
33 #include <osl/file.hxx>
34 using namespace ::ooo::vba
;
35 using namespace ::com::sun::star
;
37 const sal_Int16 CUSTOM_CHAR
= 5;
40 getWorkbook( const uno::Reference
< uno::XComponentContext
>& xContext
,
41 const uno::Reference
< sheet::XSpreadsheetDocument
> &xDoc
,
42 const uno::Reference
< XHelperInterface
>& xParent
)
44 // FIXME: fine as long as ScVbaWorkbook is stateless ...
45 uno::Reference
< frame::XModel
> xModel( xDoc
, uno::UNO_QUERY
);
49 uno::Reference
< excel::XWorkbook
> xWb( getVBADocument( xModel
), uno::UNO_QUERY
);
52 return uno::Any( xWb
);
55 ScVbaWorkbook
*pWb
= new ScVbaWorkbook( xParent
, xContext
, xModel
);
56 return uno::Any( uno::Reference
< excel::XWorkbook
> (pWb
) );
59 class WorkBookEnumImpl
: public EnumerationHelperImpl
62 /// @throws uno::RuntimeException
63 WorkBookEnumImpl( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< container::XEnumeration
>& xEnumeration
) : EnumerationHelperImpl( xParent
, xContext
, xEnumeration
) {}
65 virtual uno::Any SAL_CALL
nextElement( ) override
67 uno::Reference
< sheet::XSpreadsheetDocument
> xDoc( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
68 return getWorkbook( m_xContext
, xDoc
, m_xParent
);
73 ScVbaWorkbooks::ScVbaWorkbooks( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< css::uno::XComponentContext
>& xContext
) : ScVbaWorkbooks_BASE( xParent
, xContext
, VbaDocumentsBase::EXCEL_DOCUMENT
)
78 ScVbaWorkbooks::getElementType()
80 return cppu::UnoType
<excel::XWorkbook
>::get();
82 uno::Reference
< container::XEnumeration
>
83 ScVbaWorkbooks::createEnumeration()
85 // #FIXME its possible the WorkBookEnumImpl here doesn't reflect
86 // the state of this object ( although it should ) would be
87 // safer to create an enumeration based on this objects state
88 // rather than one effectively based of the desktop component
89 uno::Reference
< container::XEnumerationAccess
> xEnumerationAccess( m_xIndexAccess
, uno::UNO_QUERY_THROW
);
90 return new WorkBookEnumImpl( mxParent
, mxContext
, xEnumerationAccess
->createEnumeration() );
94 ScVbaWorkbooks::createCollectionObject( const css::uno::Any
& aSource
)
96 uno::Reference
< sheet::XSpreadsheetDocument
> xDoc( aSource
, uno::UNO_QUERY_THROW
);
97 return getWorkbook( mxContext
, xDoc
, mxParent
);
101 ScVbaWorkbooks::Add( const uno::Any
& Template
)
103 uno::Reference
< sheet::XSpreadsheetDocument
> xSpreadDoc
;
104 sal_Int32 nWorkbookType
= 0;
105 OUString aTemplateFileName
;
106 if( Template
>>= nWorkbookType
)
108 // nWorkbookType is a constant from XlWBATemplate (added in Excel 2007)
109 // TODO: create chart-sheet if supported by Calc
111 xSpreadDoc
.set( createDocument(), uno::UNO_QUERY_THROW
);
112 // create a document with one sheet only
113 uno::Reference
< sheet::XSpreadsheets
> xSheets( xSpreadDoc
->getSheets(), uno::UNO_SET_THROW
);
114 uno::Reference
< container::XIndexAccess
> xSheetsIA( xSheets
, uno::UNO_QUERY_THROW
);
115 while( xSheetsIA
->getCount() > 1 )
117 uno::Reference
< container::XNamed
> xSheetName( xSheetsIA
->getByIndex( xSheetsIA
->getCount() - 1 ), uno::UNO_QUERY_THROW
);
118 xSheets
->removeByName( xSheetName
->getName() );
121 else if( Template
>>= aTemplateFileName
)
123 // TODO: create document from template
124 xSpreadDoc
.set( createDocument(), uno::UNO_QUERY_THROW
);
126 else if( !Template
.hasValue() )
128 // regular spreadsheet document with configured number of sheets
129 xSpreadDoc
.set( createDocument(), uno::UNO_QUERY_THROW
);
134 throw uno::RuntimeException();
137 // need to set up the document modules ( and vba mode ) here
138 excel::setUpDocumentModules( xSpreadDoc
);
139 if( xSpreadDoc
.is() )
140 return getWorkbook( mxContext
, xSpreadDoc
, mxParent
);
145 ScVbaWorkbooks::Close()
150 ScVbaWorkbooks::isTextFile( const OUString
& sType
)
152 // will return true if the file is
153 // a) a variant of a text file
156 // returning true basically means treat this like a csv file
157 return sType
== "generic_Text" || sType
.isEmpty();
161 ScVbaWorkbooks::isSpreadSheetFile( const OUString
& sType
)
163 // include calc_QPro etc. ? ( not for the moment anyway )
164 return sType
.startsWith( "calc_MS" )
165 || sType
.startsWith( "MS Excel" )
166 || sType
.startsWith( "calc8" )
167 || sType
.startsWith( "calc_StarOffice" );
171 ScVbaWorkbooks::getFileFilterType( const OUString
& rFileName
)
173 uno::Reference
< document::XTypeDetection
> xTypeDetect( mxContext
->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxContext
), uno::UNO_QUERY_THROW
);
174 uno::Sequence
< beans::PropertyValue
> aMediaDesc(1);
175 aMediaDesc
[ 0 ].Name
= "URL";
176 aMediaDesc
[ 0 ].Value
<<= rFileName
;
177 OUString sType
= xTypeDetect
->queryTypeByDescriptor( aMediaDesc
, true );
181 // #TODO# #FIXME# can any of the unused params below be used?
183 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*/ )
185 // we need to detect if this is a URL, if not then assume it's a file path
188 aObj
.SetURL( rFileName
);
189 bool bIsURL
= aObj
.GetProtocol() != INetProtocol::NotValid
;
193 osl::FileBase::getFileURLFromSystemPath( rFileName
, aURL
);
195 uno::Sequence
< beans::PropertyValue
> sProps(0);
197 OUString sType
= getFileFilterType( aURL
);
198 // A text file means it needs to be processed as a csv file
199 if ( isTextFile( sType
) )
201 sal_Int32 nIndex
= 0;
208 // 6 Custom character (see the Delimiter argument
209 // no format means use the current delimiter
211 sProps
[ nIndex
].Name
= "FilterOptions";
212 sal_Int16
const delims
[] { 0 /*default not used*/, 9/*tab*/, 44/*comma*/, 32/*space*/, 59/*semicolon*/ };
215 sal_Int16 nFormat
= 0; // default indicator
217 if ( Format
.hasValue() )
219 Format
>>= nFormat
; // val of nFormat overwritten if extracted
221 if ( nFormat
< 1 || nFormat
> 6 )
222 throw uno::RuntimeException("Illegal value for Format" );
225 sal_Int16 nDelim
= getCurrentDelim();
227 if ( nFormat
> 0 && nFormat
< CUSTOM_CHAR
)
229 nDelim
= delims
[ nFormat
];
231 else if ( nFormat
> CUSTOM_CHAR
)
233 // Need to check Delimiter param
234 if ( !Delimiter
.hasValue() )
235 throw uno::RuntimeException("Expected value for Delimiter" );
238 if ( sStr
.isEmpty() )
239 throw uno::RuntimeException("Incorrect value for Delimiter" );
245 getCurrentDelim() = nDelim
; //set new current
247 sFormat
= OUString::number( nDelim
) + ",34,0,1";
248 sProps
[ nIndex
++ ].Value
<<= sFormat
;
249 sProps
[ nIndex
].Name
= "FilterName";
250 sProps
[ nIndex
++ ].Value
<<= OUString( SC_TEXT_CSV_FILTER_NAME
);
251 // Ensure WORKAROUND_CSV_TXT_BUG_i60158 gets called in typedetection.cxx so
252 // csv is forced for deep detected 'writerxxx' types
253 sProps
[ nIndex
].Name
= "DocumentService";
254 sProps
[ nIndex
].Value
<<= OUString("com.sun.star.sheet.SpreadsheetDocument");
256 else if ( !isSpreadSheetFile( sType
) )
257 throw uno::RuntimeException("Bad Format" );
259 uno::Reference
<sheet::XSpreadsheetDocument
> xSpreadDoc( openDocument( rFileName
, ReadOnly
, sProps
), uno::UNO_QUERY_THROW
);
260 uno::Any aRet
= getWorkbook( mxContext
, xSpreadDoc
, mxParent
);
261 uno::Reference
< excel::XWorkbook
> xWBook( aRet
, uno::UNO_QUERY
);
268 ScVbaWorkbooks::getServiceImplName()
270 return "ScVbaWorkbooks";
273 css::uno::Sequence
<OUString
>
274 ScVbaWorkbooks::getServiceNames()
276 static uno::Sequence
< OUString
> const sNames
278 "ooo.vba.excel.Workbooks"
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */