xref: /trunk/main/sc/source/ui/vba/vbaworkbooks.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
26cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
29cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
30cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
31cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
33cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
34cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
35cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
36cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
37cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp>
38cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
39cdf0e10cSrcweir #include <com/sun/star/beans/PropertyVetoException.hpp>
40cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
42cdf0e10cSrcweir #include <com/sun/star/document/XTypeDetection.hpp>
43cdf0e10cSrcweir #include <com/sun/star/uri/XUriReference.hpp>
44cdf0e10cSrcweir #include <com/sun/star/uri/XUriReferenceFactory.hpp>
45cdf0e10cSrcweir #include <com/sun/star/script/vba/VBAEventId.hpp>
46cdf0e10cSrcweir #include <com/sun/star/script/vba/XVBACompatibility.hpp>
47cdf0e10cSrcweir #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
48cdf0e10cSrcweir #include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
49cdf0e10cSrcweir #include <com/sun/star/script/ModuleInfo.hpp>
50cdf0e10cSrcweir #include <com/sun/star/script/ModuleType.hpp>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include <sfx2/objsh.hxx>
53cdf0e10cSrcweir #include <tools/urlobj.hxx>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #include "vbaglobals.hxx"
56cdf0e10cSrcweir #include "vbaworkbook.hxx"
57cdf0e10cSrcweir #include "vbaworkbooks.hxx"
58cdf0e10cSrcweir #include <vbahelper/vbahelper.hxx>
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #include <hash_map>
61cdf0e10cSrcweir #include <vector>
62cdf0e10cSrcweir #include <osl/file.hxx>
63cdf0e10cSrcweir using namespace ::ooo::vba;
64cdf0e10cSrcweir using namespace ::com::sun::star;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir const sal_Int16 CUSTOM_CHAR = 5;
67cdf0e10cSrcweir 
setUpDocumentModules(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc)68cdf0e10cSrcweir void setUpDocumentModules( const uno::Reference< sheet::XSpreadsheetDocument >& xDoc )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
71cdf0e10cSrcweir     ScDocShell* pShell = excel::getDocShell( xModel );
72cdf0e10cSrcweir     if ( pShell )
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         String aPrjName( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) );
75cdf0e10cSrcweir         pShell->GetBasicManager()->SetName( aPrjName );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         /*  Set library container to VBA compatibility mode. This will create
78cdf0e10cSrcweir             the VBA Globals object and store it in the Basic manager of the
79cdf0e10cSrcweir             document. */
80cdf0e10cSrcweir         uno::Reference<script::XLibraryContainer> xLibContainer = pShell->GetBasicContainer();
81cdf0e10cSrcweir         uno::Reference<script::vba::XVBACompatibility> xVBACompat( xLibContainer, uno::UNO_QUERY_THROW );
82cdf0e10cSrcweir         xVBACompat->setVBACompatibilityMode( sal_True );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         if( xLibContainer.is() )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             if( !xLibContainer->hasByName( aPrjName ) )
87cdf0e10cSrcweir                 xLibContainer->createLibrary( aPrjName );
88cdf0e10cSrcweir             uno::Any aLibAny = xLibContainer->getByName( aPrjName );
89cdf0e10cSrcweir             uno::Reference< container::XNameContainer > xLib;
90cdf0e10cSrcweir             aLibAny >>= xLib;
91cdf0e10cSrcweir             if( xLib.is()  )
92cdf0e10cSrcweir             {
93cdf0e10cSrcweir                 uno::Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( xLib, uno::UNO_QUERY_THROW );
94cdf0e10cSrcweir                 uno::Reference< lang::XMultiServiceFactory> xSF( pShell->GetModel(), uno::UNO_QUERY_THROW);
95cdf0e10cSrcweir                 uno::Reference< container::XNameAccess > xVBACodeNamedObjectAccess( xSF->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.VBAObjectModuleObjectProvider"))), uno::UNO_QUERY_THROW );
96cdf0e10cSrcweir                 // set up the module info for the workbook and sheets in the nealy created
97cdf0e10cSrcweir                 // spreadsheet
98cdf0e10cSrcweir                 ScDocument* pDoc = pShell->GetDocument();
99cdf0e10cSrcweir                 String sCodeName = pDoc->GetCodeName();
100cdf0e10cSrcweir                 if ( sCodeName.Len() == 0 )
101cdf0e10cSrcweir                 {
102cdf0e10cSrcweir                     sCodeName = String( RTL_CONSTASCII_USTRINGPARAM("ThisWorkbook") );
103cdf0e10cSrcweir                     pDoc->SetCodeName( sCodeName );
104cdf0e10cSrcweir                 }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir                 std::vector< rtl::OUString > sDocModuleNames;
107cdf0e10cSrcweir                 sDocModuleNames.push_back( sCodeName );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir                 uno::Reference<container::XNameAccess > xSheets( xDoc->getSheets(), uno::UNO_QUERY_THROW );
110cdf0e10cSrcweir                 uno::Sequence< rtl::OUString > sSheets( xSheets->getElementNames() );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir                 for ( sal_Int32 index=0; index < sSheets.getLength() ; ++index )
113cdf0e10cSrcweir                 {
114cdf0e10cSrcweir                     sDocModuleNames.push_back( sSheets[ index ] );
115cdf0e10cSrcweir                 }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir                 std::vector<rtl::OUString>::iterator it_end = sDocModuleNames.end();
118cdf0e10cSrcweir 
119cdf0e10cSrcweir                 for ( std::vector<rtl::OUString>::iterator it = sDocModuleNames.begin(); it != it_end; ++it )
120cdf0e10cSrcweir                 {
121cdf0e10cSrcweir                     script::ModuleInfo sModuleInfo;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir                     sModuleInfo.ModuleObject.set( xVBACodeNamedObjectAccess->getByName( *it ), uno::UNO_QUERY );
124cdf0e10cSrcweir                     sModuleInfo.ModuleType = script::ModuleType::DOCUMENT;
125cdf0e10cSrcweir                     xVBAModuleInfo->insertModuleInfo( *it, sModuleInfo );
126cdf0e10cSrcweir                     if( xLib->hasByName( *it ) )
127cdf0e10cSrcweir                         xLib->replaceByName( *it, uno::makeAny( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Option VBASupport 1\n") ) ) );
128cdf0e10cSrcweir                     else
129cdf0e10cSrcweir                         xLib->insertByName( *it, uno::makeAny( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Option VBASupport 1\n" ) ) ) );
130cdf0e10cSrcweir                 }
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir static uno::Any
getWorkbook(uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const uno::Reference<XHelperInterface> & xParent)137cdf0e10cSrcweir getWorkbook( uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< sheet::XSpreadsheetDocument > &xDoc, const uno::Reference< XHelperInterface >& xParent )
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	// FIXME: fine as long as ScVbaWorkbook is stateless ...
140cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
141cdf0e10cSrcweir 	if( !xModel.is() )
142cdf0e10cSrcweir 		return uno::Any();
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	uno::Reference< excel::XWorkbook > xWb( getVBADocument( xModel ), uno::UNO_QUERY );
145cdf0e10cSrcweir     if ( xWb.is() )
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         OSL_TRACE(" *** Returning Module uno Object *** ");
148cdf0e10cSrcweir         return uno::Any( xWb );
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	ScVbaWorkbook *pWb = new ScVbaWorkbook( xParent, xContext, xModel );
152cdf0e10cSrcweir 	return uno::Any( uno::Reference< excel::XWorkbook > (pWb) );
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir class WorkBookEnumImpl : public EnumerationHelperImpl
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	uno::Any m_aApplication;
158cdf0e10cSrcweir public:
WorkBookEnumImpl(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XEnumeration> & xEnumeration,const uno::Any & aApplication)159cdf0e10cSrcweir 	WorkBookEnumImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Any& aApplication ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_aApplication( aApplication ) {}
160cdf0e10cSrcweir 
nextElement()161cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		uno::Reference< sheet::XSpreadsheetDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
164cdf0e10cSrcweir 		return getWorkbook( m_xContext, xDoc, m_xParent );
165cdf0e10cSrcweir 	}
166cdf0e10cSrcweir 
167cdf0e10cSrcweir };
168cdf0e10cSrcweir 
ScVbaWorkbooks(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<css::uno::XComponentContext> & xContext)169cdf0e10cSrcweir ScVbaWorkbooks::ScVbaWorkbooks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWorkbooks_BASE( xParent, xContext, VbaDocumentsBase::EXCEL_DOCUMENT )
170cdf0e10cSrcweir {
171cdf0e10cSrcweir }
172cdf0e10cSrcweir // XEnumerationAccess
173cdf0e10cSrcweir uno::Type
getElementType()174cdf0e10cSrcweir ScVbaWorkbooks::getElementType() throw (uno::RuntimeException)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir 	return excel::XWorkbook::static_type(0);
177cdf0e10cSrcweir }
178cdf0e10cSrcweir uno::Reference< container::XEnumeration >
createEnumeration()179cdf0e10cSrcweir ScVbaWorkbooks::createEnumeration() throw (uno::RuntimeException)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir 	// #FIXME its possible the WorkBookEnumImpl here doens't reflect
182cdf0e10cSrcweir 	// the state of this object ( although it should ) would be
183cdf0e10cSrcweir 	// safer to create an enumeration based on this objects state
184cdf0e10cSrcweir 	// rather than one effectively based of the desktop component
185cdf0e10cSrcweir     uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
186cdf0e10cSrcweir 	return new WorkBookEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration(), Application() );
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir uno::Any
createCollectionObject(const css::uno::Any & aSource)190cdf0e10cSrcweir ScVbaWorkbooks::createCollectionObject( const css::uno::Any& aSource )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir 	uno::Reference< sheet::XSpreadsheetDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
193cdf0e10cSrcweir 	return getWorkbook( mxContext, xDoc, mxParent );
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
197cdf0e10cSrcweir uno::Any SAL_CALL
Add(const uno::Any & Template)198cdf0e10cSrcweir ScVbaWorkbooks::Add( const uno::Any& Template ) throw (uno::RuntimeException)
199cdf0e10cSrcweir {
200cdf0e10cSrcweir     uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc;
201cdf0e10cSrcweir     sal_Int32 nWorkbookType = 0;
202cdf0e10cSrcweir     ::rtl::OUString aTemplateFileName;
203cdf0e10cSrcweir     if( Template >>= nWorkbookType )
204cdf0e10cSrcweir     {
205cdf0e10cSrcweir         // nWorkbookType is a constant from XlWBATemplate (added in Excel 2007)
206cdf0e10cSrcweir         // TODO: create chart-sheet if supported by Calc
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
209cdf0e10cSrcweir         // create a document with one sheet only
210cdf0e10cSrcweir         uno::Reference< sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), uno::UNO_SET_THROW );
211cdf0e10cSrcweir         uno::Reference< container::XIndexAccess > xSheetsIA( xSheets, uno::UNO_QUERY_THROW );
212cdf0e10cSrcweir         while( xSheetsIA->getCount() > 1 )
213cdf0e10cSrcweir         {
214cdf0e10cSrcweir             uno::Reference< container::XNamed > xSheetName( xSheetsIA->getByIndex( xSheetsIA->getCount() - 1 ), uno::UNO_QUERY_THROW );
215cdf0e10cSrcweir             xSheets->removeByName( xSheetName->getName() );
216cdf0e10cSrcweir         }
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir     else if( Template >>= aTemplateFileName )
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         // TODO: create document from template
221cdf0e10cSrcweir         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir     else if( !Template.hasValue() )
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         // regular spreadsheet document with configured number of sheets
226cdf0e10cSrcweir         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir     else
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         // illegal argument
231cdf0e10cSrcweir         throw uno::RuntimeException();
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     // need to set up the document modules ( and vba mode ) here
235cdf0e10cSrcweir     setUpDocumentModules( xSpreadDoc );
236cdf0e10cSrcweir     if( xSpreadDoc.is() )
237cdf0e10cSrcweir         return getWorkbook( mxContext, xSpreadDoc, mxParent );
238cdf0e10cSrcweir     return uno::Any();
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir void SAL_CALL
Close()242cdf0e10cSrcweir ScVbaWorkbooks::Close() throw (uno::RuntimeException)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     closeDocuments();
245cdf0e10cSrcweir }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir bool
isTextFile(const rtl::OUString & sType)248cdf0e10cSrcweir ScVbaWorkbooks::isTextFile( const rtl::OUString& sType )
249cdf0e10cSrcweir {
250cdf0e10cSrcweir 	// will return true if the file is
251cdf0e10cSrcweir 	// a) a variant of a text file
252cdf0e10cSrcweir 	// b) a csv file
253cdf0e10cSrcweir 	// c) unknown
254cdf0e10cSrcweir 	// returning true basically means treat this like a csv file
255cdf0e10cSrcweir 	const static rtl::OUString txtType( RTL_CONSTASCII_USTRINGPARAM("writer_Text" ) );
256cdf0e10cSrcweir 	const static rtl::OUString csvType( RTL_CONSTASCII_USTRINGPARAM("calc_Text_txt_csv_StarCalc" ) );
257cdf0e10cSrcweir 	const static rtl::OUString encodedTxtType( RTL_CONSTASCII_USTRINGPARAM("writer_Text_encoded" ) );
258cdf0e10cSrcweir 	return sType.equals( txtType ) || sType.equals( csvType ) || ( sType.getLength() == 0 ) || sType.equals( encodedTxtType );
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir bool
isSpreadSheetFile(const rtl::OUString & sType)262cdf0e10cSrcweir ScVbaWorkbooks::isSpreadSheetFile( const rtl::OUString& sType )
263cdf0e10cSrcweir {
264cdf0e10cSrcweir 	// include calc_QPro etc. ? ( not for the moment anyway )
265cdf0e10cSrcweir 	if ( sType.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("calc_MS"))) == 0
266cdf0e10cSrcweir 	|| sType.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("calc8"))) == 0
267cdf0e10cSrcweir 	|| sType.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("calc_StarOffice"))) == 0 )
268cdf0e10cSrcweir 		return true;
269cdf0e10cSrcweir 	return false;
270cdf0e10cSrcweir }
271cdf0e10cSrcweir 
272cdf0e10cSrcweir rtl::OUString
getFileFilterType(const rtl::OUString & rFileName)273cdf0e10cSrcweir ScVbaWorkbooks::getFileFilterType( const rtl::OUString& rFileName )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir 	uno::Reference< document::XTypeDetection > xTypeDetect( mxContext->getServiceManager()->createInstanceWithContext(::rtl::OUString::createFromAscii("com.sun.star.document.TypeDetection"), mxContext), uno::UNO_QUERY_THROW );
276cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aMediaDesc(1);
277cdf0e10cSrcweir 	aMediaDesc[ 0 ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ("URL" ) );
278cdf0e10cSrcweir 	aMediaDesc[ 0 ].Value <<= rFileName;
279cdf0e10cSrcweir 	rtl::OUString sType = xTypeDetect->queryTypeByDescriptor( aMediaDesc, sal_True );
280cdf0e10cSrcweir 	return sType;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir // #TODO# #FIXME# can any of the unused params below be used?
284cdf0e10cSrcweir uno::Any SAL_CALL
Open(const rtl::OUString & rFileName,const uno::Any &,const uno::Any & ReadOnly,const uno::Any & Format,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any & Delimiter,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)285cdf0e10cSrcweir ScVbaWorkbooks::Open( const rtl::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)
286cdf0e10cSrcweir {
287cdf0e10cSrcweir 	// we need to detect if this is a URL, if not then assume its a file path
288cdf0e10cSrcweir     rtl::OUString aURL;
289cdf0e10cSrcweir     INetURLObject aObj;
290cdf0e10cSrcweir 	aObj.SetURL( rFileName );
291cdf0e10cSrcweir 	bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
292cdf0e10cSrcweir 	if ( bIsURL )
293cdf0e10cSrcweir 		aURL = rFileName;
294cdf0e10cSrcweir 	else
295cdf0e10cSrcweir 		osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > sProps(0);
298cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	rtl::OUString sType = getFileFilterType( aURL );
301cdf0e10cSrcweir 	// A text file means it needs to be processed as a csv file
302cdf0e10cSrcweir 	if ( isTextFile( sType ) )
303cdf0e10cSrcweir 	{
304cdf0e10cSrcweir 		// Values for format
305cdf0e10cSrcweir 		// 1 Tabs
306cdf0e10cSrcweir 		// 2 Commas
307cdf0e10cSrcweir 		// 3 Spaces
308cdf0e10cSrcweir 		// 4 Semicolons
309cdf0e10cSrcweir 		// 5 Nothing
310cdf0e10cSrcweir 		// 6 Custom character (see the Delimiter argument
311cdf0e10cSrcweir 		// no format means use the current delimiter
312cdf0e10cSrcweir 		sProps.realloc( 3 );
313cdf0e10cSrcweir 		sProps[ nIndex ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FilterOptions" ) );
314cdf0e10cSrcweir 		sal_Int16 delims[] = { 0 /*default not used*/, 9/*tab*/, 44/*comma*/, 32/*space*/, 59/*semicolon*/ };
315cdf0e10cSrcweir 		static rtl::OUString sRestOfFormat( RTL_CONSTASCII_USTRINGPARAM(",34,0,1" ) );
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 		rtl::OUString sFormat;
318cdf0e10cSrcweir 		sal_Int16 nFormat = 0; // default indicator
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 		if ( Format.hasValue() )
322cdf0e10cSrcweir 		{
323cdf0e10cSrcweir 			Format >>= nFormat; // val of nFormat overwritten if extracted
324cdf0e10cSrcweir 			// validate param
325cdf0e10cSrcweir 			if ( nFormat < 1 || nFormat > 6 )
326cdf0e10cSrcweir 				throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Illegal value for Format" ) ), uno::Reference< uno::XInterface >() );
327cdf0e10cSrcweir 		}
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 		sal_Int16 nDelim = getCurrentDelim();
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 		if (  nFormat > 0 && nFormat < CUSTOM_CHAR )
332cdf0e10cSrcweir 		{
333cdf0e10cSrcweir 			nDelim =  delims[ nFormat ];
334cdf0e10cSrcweir 		}
335cdf0e10cSrcweir 		else if ( nFormat > CUSTOM_CHAR )
336cdf0e10cSrcweir 		{
337cdf0e10cSrcweir 			// Need to check Delimiter param
338cdf0e10cSrcweir 			if ( !Delimiter.hasValue() )
339cdf0e10cSrcweir 				throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Expected value for Delimiter" ) ), uno::Reference< uno::XInterface >() );
340cdf0e10cSrcweir 			rtl::OUString sStr;
341cdf0e10cSrcweir 			Delimiter >>= sStr;
342cdf0e10cSrcweir 			String aUniStr( sStr );
343cdf0e10cSrcweir 			if ( aUniStr.Len() )
344cdf0e10cSrcweir 				nDelim = aUniStr.GetChar(0);
345cdf0e10cSrcweir 			else
346cdf0e10cSrcweir 				throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Incorrect value for Delimiter" ) ), uno::Reference< uno::XInterface >() );
347cdf0e10cSrcweir 		}
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 		getCurrentDelim() = nDelim; //set new current
350cdf0e10cSrcweir 
351cdf0e10cSrcweir 		sFormat = rtl::OUString::valueOf( (sal_Int32)nDelim ) + sRestOfFormat;
352cdf0e10cSrcweir 		sProps[ nIndex++ ].Value <<= sFormat;
353cdf0e10cSrcweir 		sProps[ nIndex ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FilterName") );
354cdf0e10cSrcweir 		sProps[ nIndex++ ].Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Text - txt - csv (StarCalc)") );
355cdf0e10cSrcweir 		// Ensure WORKAROUND_CSV_TXT_BUG_i60158 gets called in typedetection.cxx so
356cdf0e10cSrcweir 		// csv is forced for deep detected 'writerxxx' types
357cdf0e10cSrcweir 		sProps[ nIndex ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("DocumentService") );
358cdf0e10cSrcweir 		sProps[ nIndex ].Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sheet.SpreadsheetDocument") );
359cdf0e10cSrcweir 	}
360cdf0e10cSrcweir 	else if ( !isSpreadSheetFile( sType ) )
361cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Bad Format")), uno::Reference< uno::XInterface >() );
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 	uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( openDocument( rFileName, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
364cdf0e10cSrcweir 	uno::Any aRet = getWorkbook( mxContext, xSpreadDoc, mxParent );
365cdf0e10cSrcweir 	uno::Reference< excel::XWorkbook > xWBook( aRet, uno::UNO_QUERY );
366cdf0e10cSrcweir 	if ( xWBook.is() )
367cdf0e10cSrcweir 		xWBook->Activate();
368cdf0e10cSrcweir 	return aRet;
369cdf0e10cSrcweir }
370cdf0e10cSrcweir 
371cdf0e10cSrcweir rtl::OUString&
getServiceImplName()372cdf0e10cSrcweir ScVbaWorkbooks::getServiceImplName()
373cdf0e10cSrcweir {
374cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWorkbooks") );
375cdf0e10cSrcweir 	return sImplName;
376cdf0e10cSrcweir }
377cdf0e10cSrcweir 
378cdf0e10cSrcweir css::uno::Sequence<rtl::OUString>
getServiceNames()379cdf0e10cSrcweir ScVbaWorkbooks::getServiceNames()
380cdf0e10cSrcweir {
381cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > sNames;
382cdf0e10cSrcweir 	if ( sNames.getLength() == 0 )
383cdf0e10cSrcweir 	{
384cdf0e10cSrcweir 		sNames.realloc( 1 );
385cdf0e10cSrcweir 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Workbooks") );
386cdf0e10cSrcweir 	}
387cdf0e10cSrcweir 	return sNames;
388cdf0e10cSrcweir }
389