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