Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <vbahelper/helperdecl.hxx>
21 : #include <tools/urlobj.hxx>
22 : #include <comphelper/unwrapargs.hxx>
23 : #include <comphelper/servicehelper.hxx>
24 :
25 : #include <com/sun/star/util/XModifiable.hpp>
26 : #include <com/sun/star/util/XProtectable.hpp>
27 : #include <com/sun/star/sheet/XSpreadsheetView.hpp>
28 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
29 : #include <com/sun/star/frame/XStorable.hpp>
30 : #include <com/sun/star/frame/XFrame.hpp>
31 : #include <com/sun/star/beans/XPropertySet.hpp>
32 : #include <ooo/vba/excel/XlFileFormat.hpp>
33 : #include <ooo/vba/excel/XApplication.hpp>
34 :
35 : #include "scextopt.hxx"
36 : #include "vbaworksheet.hxx"
37 : #include "vbaworksheets.hxx"
38 : #include "vbaworkbook.hxx"
39 : #include "vbawindows.hxx"
40 : #include "vbastyles.hxx"
41 : #include "excelvbahelper.hxx"
42 : #include "vbapalette.hxx"
43 : #include <osl/file.hxx>
44 : #include <stdio.h>
45 : #include "vbanames.hxx"
46 : #include "nameuno.hxx"
47 : #include "docoptio.hxx"
48 : #include "unonames.hxx"
49 :
50 : // Much of the impl. for the equivalend UNO module is
51 : // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
52 :
53 : using namespace ::ooo::vba;
54 : using namespace ::com::sun::star;
55 :
56 0 : uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
57 :
58 0 : void ScVbaWorkbook::initColorData( const uno::Sequence< sal_Int32 >& sColors )
59 : {
60 0 : const sal_Int32* pSource = sColors.getConstArray();
61 0 : sal_Int32* pDest = ColorData.getArray();
62 0 : const sal_Int32* pEnd = pSource + sColors.getLength();
63 0 : for ( ; pSource != pEnd; ++pSource, ++pDest )
64 0 : *pDest = *pSource;
65 0 : }
66 :
67 :
68 : void SAL_CALL
69 0 : ScVbaWorkbook::ResetColors( ) throw (::script::BasicErrorException, ::uno::RuntimeException, std::exception)
70 : {
71 0 : uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_QUERY_THROW );
72 0 : sal_Int32 nLen = xIndexAccess->getCount();
73 0 : ColorData.realloc( nLen );
74 :
75 0 : uno::Sequence< sal_Int32 > dDefaultColors( nLen );
76 0 : sal_Int32* pDest = dDefaultColors.getArray();
77 0 : for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
78 0 : xIndexAccess->getByIndex( index ) >>= (*pDest);
79 0 : initColorData( dDefaultColors );
80 0 : }
81 :
82 : ::uno::Any SAL_CALL
83 0 : ScVbaWorkbook::Colors( const ::uno::Any& Index ) throw (::script::BasicErrorException, ::uno::RuntimeException, std::exception)
84 : {
85 0 : uno::Any aRet;
86 0 : if ( Index.getValue() )
87 : {
88 0 : sal_Int32 nIndex = 0;
89 0 : Index >>= nIndex;
90 0 : aRet = uno::makeAny( XLRGBToOORGB( ColorData[ --nIndex ] ) );
91 : }
92 : else
93 0 : aRet = uno::makeAny( ColorData );
94 0 : return aRet;
95 : }
96 :
97 0 : bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat, uno::Sequence< beans::PropertyValue >& rProps )
98 : {
99 0 : bool bRes = false;
100 0 : for ( sal_Int32 index = 0; index < rProps.getLength(); ++index )
101 : {
102 0 : if ( rProps[ index ].Name == "FilterName" )
103 : {
104 0 : switch( nFormat )
105 : {
106 : case excel::XlFileFormat::xlCSV:
107 0 : rProps[ index ].Value = uno::Any( OUString("Text - txt - csv (StarCalc)") );
108 0 : break;
109 : case excel::XlFileFormat::xlDBF4:
110 0 : rProps[ index ].Value = uno::Any( OUString("DBF") );
111 0 : break;
112 : case excel::XlFileFormat::xlDIF:
113 0 : rProps[ index ].Value = uno::Any( OUString("DIF") );
114 0 : break;
115 : case excel::XlFileFormat::xlWK3:
116 0 : rProps[ index ].Value = uno::Any( OUString("Lotus") );
117 0 : break;
118 : case excel::XlFileFormat::xlExcel4Workbook:
119 0 : rProps[ index ].Value = uno::Any( OUString("MS Excel 4.0") );
120 0 : break;
121 : case excel::XlFileFormat::xlExcel5:
122 0 : rProps[ index ].Value = uno::Any( OUString("MS Excel 5.0/95") );
123 0 : break;
124 : case excel::XlFileFormat::xlHtml:
125 0 : rProps[ index ].Value = uno::Any( OUString("HTML (StarCalc)") );
126 0 : break;
127 : case excel::XlFileFormat::xlExcel9795:
128 : default:
129 0 : rProps[ index ].Value = uno::Any( OUString("MS Excel 97") );
130 0 : break;
131 : }
132 0 : bRes = true;
133 0 : break;
134 : }
135 : }
136 0 : return bRes;
137 : }
138 :
139 : ::sal_Int32 SAL_CALL
140 0 : ScVbaWorkbook::getFileFormat( ) throw (::uno::RuntimeException, std::exception)
141 : {
142 0 : sal_Int32 aFileFormat = 0;
143 0 : OUString aFilterName;
144 0 : uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
145 :
146 : // #FIXME - seems suspect should we not walk through the properties
147 : // to find the FilterName
148 0 : if ( aArgs[0].Name == "FilterName" ) {
149 0 : aArgs[0].Value >>= aFilterName;
150 : } else {
151 0 : aArgs[1].Value >>= aFilterName;
152 : }
153 :
154 0 : if (aFilterName == "Text - txt - csv (StarCalc)") {
155 0 : aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
156 : }
157 :
158 0 : if ( aFilterName == "DBF" ) {
159 0 : aFileFormat = excel::XlFileFormat::xlDBF4;
160 : }
161 :
162 0 : if ( aFilterName == "DIF" ) {
163 0 : aFileFormat = excel::XlFileFormat::xlDIF;
164 : }
165 :
166 0 : if ( aFilterName == "Lotus" ) {
167 0 : aFileFormat = excel::XlFileFormat::xlWK3;
168 : }
169 :
170 0 : if ( aFilterName == "MS Excel 4.0" ) {
171 0 : aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
172 : }
173 :
174 0 : if ( aFilterName == "MS Excel 5.0/95" ) {
175 0 : aFileFormat = excel::XlFileFormat::xlExcel5;
176 : }
177 :
178 0 : if ( aFilterName == "MS Excel 97" ) {
179 0 : aFileFormat = excel::XlFileFormat::xlExcel9795;
180 : }
181 :
182 0 : if (aFilterName == "HTML (StarCalc)") {
183 0 : aFileFormat = excel::XlFileFormat::xlHtml;
184 : }
185 :
186 0 : if ( aFilterName == "calc_StarOffice_XML_Calc_Template" ) {
187 0 : aFileFormat = excel::XlFileFormat::xlTemplate;
188 : }
189 :
190 0 : if (aFilterName == "StarOffice XML (Calc)") {
191 0 : aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
192 : }
193 0 : if ( aFilterName == "calc8" ) {
194 0 : aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
195 : }
196 :
197 0 : return aFileFormat;
198 : }
199 :
200 : void
201 0 : ScVbaWorkbook::init()
202 : {
203 0 : if ( !ColorData.getLength() )
204 0 : ResetColors();
205 0 : }
206 :
207 0 : ScVbaWorkbook::ScVbaWorkbook( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, css::uno::Reference< css::frame::XModel > xModel ) : ScVbaWorkbook_BASE( xParent, xContext, xModel )
208 : {
209 0 : init();
210 0 : }
211 :
212 0 : ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
213 0 : uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
214 : {
215 0 : init();
216 0 : }
217 :
218 : uno::Reference< excel::XWorksheet >
219 0 : ScVbaWorkbook::getActiveSheet() throw (uno::RuntimeException, std::exception)
220 : {
221 0 : uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
222 0 : uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
223 0 : uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
224 : // #162503# return the original sheet module wrapper object, instead of a new instance
225 0 : uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
226 0 : if( xWorksheet.is() ) return xWorksheet;
227 : // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
228 0 : return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
229 : }
230 :
231 : uno::Any SAL_CALL
232 0 : ScVbaWorkbook::Sheets( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
233 : {
234 0 : return Worksheets( aIndex );
235 : }
236 :
237 : uno::Any SAL_CALL
238 0 : ScVbaWorkbook::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
239 : {
240 0 : uno::Reference< frame::XModel > xModel( getModel() );
241 0 : uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
242 0 : uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
243 0 : uno::Reference< XCollection > xWorkSheets( new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
244 0 : if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
245 : {
246 0 : return uno::Any( xWorkSheets );
247 : }
248 : // pass on to collection
249 0 : return uno::Any( xWorkSheets->Item( aIndex, uno::Any() ) );
250 : }
251 : uno::Any SAL_CALL
252 0 : ScVbaWorkbook::Windows( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
253 : {
254 :
255 0 : uno::Reference< excel::XWindows > xWindows( new ScVbaWindows( getParent(), mxContext ) );
256 0 : if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
257 0 : return uno::Any( xWindows );
258 0 : return uno::Any( xWindows->Item( aIndex, uno::Any() ) );
259 : }
260 :
261 : void SAL_CALL
262 0 : ScVbaWorkbook::Activate() throw (uno::RuntimeException, std::exception)
263 : {
264 0 : VbaDocumentBase::Activate();
265 0 : }
266 :
267 : void
268 0 : ScVbaWorkbook::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
269 : {
270 0 : VbaDocumentBase::Protect( aPassword );
271 0 : }
272 :
273 : sal_Bool
274 0 : ScVbaWorkbook::getProtectStructure() throw (uno::RuntimeException, std::exception)
275 : {
276 0 : uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
277 0 : return xProt->isProtected();
278 : }
279 :
280 0 : sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed() throw (uno::RuntimeException, std::exception)
281 : {
282 0 : uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
283 0 : ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
284 0 : return pDoc->GetDocOptions().IsCalcAsShown();
285 : }
286 :
287 0 : void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed ) throw (uno::RuntimeException, std::exception)
288 : {
289 0 : uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
290 0 : ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
291 0 : ScDocOptions aOpt = pDoc->GetDocOptions();
292 0 : aOpt.SetCalcAsShown( _precisionAsDisplayed );
293 0 : pDoc->SetDocOptions( aOpt );
294 0 : }
295 :
296 : void
297 0 : ScVbaWorkbook::SaveCopyAs( const OUString& sFileName ) throw ( uno::RuntimeException, std::exception)
298 : {
299 0 : OUString aURL;
300 0 : osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
301 0 : uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
302 0 : uno::Sequence< beans::PropertyValue > storeProps(1);
303 0 : storeProps[0].Name = "FilterName";
304 0 : storeProps[0].Value <<= OUString( "MS Excel 97" );
305 0 : xStor->storeToURL( aURL, storeProps );
306 0 : }
307 :
308 : void SAL_CALL
309 0 : ScVbaWorkbook::SaveAs( const uno::Any& FileName, const uno::Any& FileFormat, const uno::Any& /*Password*/, const uno::Any& /*WriteResPassword*/, const uno::Any& /*ReadOnlyRecommended*/, const uno::Any& /*CreateBackup*/, const uno::Any& /*AccessMode*/, const uno::Any& /*ConflictResolution*/, const uno::Any& /*AddToMru*/, const uno::Any& /*TextCodepage*/, const uno::Any& /*TextVisualLayout*/, const uno::Any& /*Local*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
310 : {
311 0 : OUString sFileName;
312 0 : FileName >>= sFileName;
313 0 : OUString sURL;
314 0 : osl::FileBase::getFileURLFromSystemPath( sFileName, sURL );
315 : // detect if there is no path if there is no path then we need
316 : // to use the current current folder
317 0 : INetURLObject aURL( sURL );
318 0 : sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
319 0 : if( sURL.isEmpty() )
320 : {
321 : // need to add cur dir ( of this workbook ) or else the 'Work' dir
322 0 : sURL = getModel()->getURL();
323 :
324 0 : if ( sURL.isEmpty() )
325 : {
326 : // not path available from 'this' document
327 : // need to add the 'document'/work directory then
328 0 : uno::Reference< excel::XApplication > xApplication ( Application(),uno::UNO_QUERY_THROW );
329 0 : OUString sWorkPath = xApplication->getDefaultFilePath();
330 0 : OUString sWorkURL;
331 0 : osl::FileBase::getFileURLFromSystemPath( sWorkPath, sWorkURL );
332 0 : aURL.SetURL( sWorkURL );
333 : }
334 : else
335 : {
336 0 : aURL.SetURL( sURL );
337 0 : aURL.Append( sFileName );
338 : }
339 0 : sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
340 :
341 : }
342 :
343 0 : sal_Int32 nFileFormat = excel::XlFileFormat::xlExcel9795;
344 0 : FileFormat >>= nFileFormat;
345 :
346 0 : uno::Sequence< beans::PropertyValue > storeProps(1);
347 0 : storeProps[0].Name = "FilterName" ;
348 :
349 0 : setFilterPropsFromFormat( nFileFormat, storeProps );
350 :
351 0 : uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
352 0 : OUString sFilterName;
353 0 : storeProps[0].Value >>= sFilterName;
354 0 : xStor->storeAsURL( sURL, storeProps );
355 0 : }
356 :
357 : css::uno::Any SAL_CALL
358 0 : ScVbaWorkbook::Styles( const uno::Any& Item ) throw (uno::RuntimeException, std::exception)
359 : {
360 : // quick look and Styles object doesn't seem to have a valid parent
361 : // or a least the object browser just shows an object that has no
362 : // variables ( therefore... leave as NULL for now )
363 0 : uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
364 0 : if ( Item.hasValue() )
365 0 : return dStyles->Item( Item, uno::Any() );
366 0 : return uno::makeAny( dStyles );
367 : }
368 :
369 : uno::Any SAL_CALL
370 0 : ScVbaWorkbook::Names( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
371 : {
372 0 : uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
373 0 : uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
374 0 : uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue("NamedRanges"), uno::UNO_QUERY_THROW );
375 0 : uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
376 0 : if ( aIndex.hasValue() )
377 0 : return uno::Any( xNames->Item( aIndex, uno::Any() ) );
378 0 : return uno::Any( xNames );
379 : }
380 :
381 : OUString
382 0 : ScVbaWorkbook::getServiceImplName()
383 : {
384 0 : return OUString("ScVbaWorkbook");
385 : }
386 :
387 : uno::Sequence< OUString >
388 0 : ScVbaWorkbook::getServiceNames()
389 : {
390 0 : static uno::Sequence< OUString > aServiceNames;
391 0 : if ( aServiceNames.getLength() == 0 )
392 : {
393 0 : aServiceNames.realloc( 1 );
394 0 : aServiceNames[ 0 ] = "ooo.vba.excel.Workbook";
395 : }
396 0 : return aServiceNames;
397 : }
398 :
399 : OUString SAL_CALL
400 0 : ScVbaWorkbook::getCodeName() throw (css::uno::RuntimeException, std::exception)
401 : {
402 0 : uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
403 0 : return xModelProp->getPropertyValue("CodeName").get< OUString >();
404 : }
405 :
406 : sal_Int64
407 0 : ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId ) throw(css::uno::RuntimeException, std::exception)
408 : {
409 0 : if (rId.getLength() == 16 &&
410 0 : 0 == memcmp( ScVbaWorksheet::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16 ))
411 : {
412 0 : return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
413 : }
414 0 : return 0;
415 : }
416 :
417 : namespace workbook
418 : {
419 : namespace sdecl = comphelper::service_decl;
420 0 : sdecl::vba_service_class_<ScVbaWorkbook, sdecl::with_args<true> > serviceImpl;
421 0 : extern sdecl::ServiceDecl const serviceDecl(
422 : serviceImpl,
423 : "ScVbaWorkbook",
424 : "ooo.vba.excel.Workbook" );
425 0 : }
426 :
427 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|