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