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 :
34 : #include "scextopt.hxx"
35 : #include "vbaworksheet.hxx"
36 : #include "vbaworksheets.hxx"
37 : #include "vbaworkbook.hxx"
38 : #include "vbawindows.hxx"
39 : #include "vbastyles.hxx"
40 : #include "excelvbahelper.hxx"
41 : #include "vbapalette.hxx"
42 : #include <osl/file.hxx>
43 : #include <stdio.h>
44 : #include "vbanames.hxx"
45 : #include "nameuno.hxx"
46 : #include "docoptio.hxx"
47 : #include "unonames.hxx"
48 :
49 : // Much of the impl. for the equivalend UNO module is
50 : // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
51 :
52 : using namespace ::ooo::vba;
53 : using namespace ::com::sun::star;
54 :
55 0 : uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
56 :
57 0 : void ScVbaWorkbook::initColorData( const uno::Sequence< sal_Int32 >& sColors )
58 : {
59 0 : const sal_Int32* pSource = sColors.getConstArray();
60 0 : sal_Int32* pDest = ColorData.getArray();
61 0 : const sal_Int32* pEnd = pSource + sColors.getLength();
62 0 : for ( ; pSource != pEnd; ++pSource, ++pDest )
63 0 : *pDest = *pSource;
64 0 : }
65 :
66 :
67 : void SAL_CALL
68 0 : ScVbaWorkbook::ResetColors( ) throw (::script::BasicErrorException, ::uno::RuntimeException)
69 : {
70 0 : uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_QUERY_THROW );
71 0 : sal_Int32 nLen = xIndexAccess->getCount();
72 0 : ColorData.realloc( nLen );
73 :
74 0 : uno::Sequence< sal_Int32 > dDefaultColors( nLen );
75 0 : sal_Int32* pDest = dDefaultColors.getArray();
76 0 : for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
77 0 : xIndexAccess->getByIndex( index ) >>= (*pDest);
78 0 : initColorData( dDefaultColors );
79 0 : }
80 :
81 : ::uno::Any SAL_CALL
82 0 : ScVbaWorkbook::Colors( const ::uno::Any& Index ) throw (::script::BasicErrorException, ::uno::RuntimeException)
83 : {
84 0 : uno::Any aRet;
85 0 : if ( Index.getValue() )
86 : {
87 0 : sal_Int32 nIndex = 0;
88 0 : Index >>= nIndex;
89 0 : aRet = uno::makeAny( XLRGBToOORGB( ColorData[ --nIndex ] ) );
90 : }
91 : else
92 0 : aRet = uno::makeAny( ColorData );
93 0 : return aRet;
94 : }
95 :
96 : ::sal_Int32 SAL_CALL
97 0 : ScVbaWorkbook::getFileFormat( ) throw (::uno::RuntimeException)
98 : {
99 0 : sal_Int32 aFileFormat = 0;
100 0 : rtl::OUString aFilterName;
101 0 : uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
102 :
103 : // #FIXME - seems suspect should we not walk through the properties
104 : // to find the FilterName
105 0 : if ( aArgs[0].Name == "FilterName" ) {
106 0 : aArgs[0].Value >>= aFilterName;
107 : } else {
108 0 : aArgs[1].Value >>= aFilterName;
109 : }
110 :
111 0 : if (aFilterName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Text - txt - csv (StarCalc)"))) {
112 0 : aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
113 : }
114 :
115 0 : if ( aFilterName == "DBF" ) {
116 0 : aFileFormat = excel::XlFileFormat::xlDBF4;
117 : }
118 :
119 0 : if ( aFilterName == "DIF" ) {
120 0 : aFileFormat = excel::XlFileFormat::xlDIF;
121 : }
122 :
123 0 : if ( aFilterName == "Lotus" ) {
124 0 : aFileFormat = excel::XlFileFormat::xlWK3;
125 : }
126 :
127 0 : if ( aFilterName == "MS Excel 4.0" ) {
128 0 : aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
129 : }
130 :
131 0 : if ( aFilterName == "MS Excel 5.0/95" ) {
132 0 : aFileFormat = excel::XlFileFormat::xlExcel5;
133 : }
134 :
135 0 : if ( aFilterName == "MS Excel 97" ) {
136 0 : aFileFormat = excel::XlFileFormat::xlExcel9795;
137 : }
138 :
139 0 : if (aFilterName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("HTML (StarCalc)"))) {
140 0 : aFileFormat = excel::XlFileFormat::xlHtml;
141 : }
142 :
143 0 : if ( aFilterName == "calc_StarOffice_XML_Calc_Template" ) {
144 0 : aFileFormat = excel::XlFileFormat::xlTemplate;
145 : }
146 :
147 0 : if (aFilterName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("StarOffice XML (Calc)"))) {
148 0 : aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
149 : }
150 0 : if ( aFilterName == "calc8" ) {
151 0 : aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
152 : }
153 :
154 0 : return aFileFormat;
155 : }
156 :
157 : void
158 0 : ScVbaWorkbook::init()
159 : {
160 0 : if ( !ColorData.getLength() )
161 0 : ResetColors();
162 0 : }
163 :
164 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 )
165 : {
166 0 : init();
167 0 : }
168 :
169 0 : ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
170 0 : uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
171 : {
172 0 : init();
173 0 : }
174 :
175 : uno::Reference< excel::XWorksheet >
176 0 : ScVbaWorkbook::getActiveSheet() throw (uno::RuntimeException)
177 : {
178 0 : uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
179 0 : uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
180 0 : uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
181 : // #162503# return the original sheet module wrapper object, instead of a new instance
182 0 : uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
183 0 : if( xWorksheet.is() ) return xWorksheet;
184 : // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
185 0 : return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
186 : }
187 :
188 : uno::Any SAL_CALL
189 0 : ScVbaWorkbook::Sheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
190 : {
191 0 : return Worksheets( aIndex );
192 : }
193 :
194 : uno::Any SAL_CALL
195 0 : ScVbaWorkbook::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
196 : {
197 0 : uno::Reference< frame::XModel > xModel( getModel() );
198 0 : uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
199 0 : uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
200 0 : uno::Reference< XCollection > xWorkSheets( new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
201 0 : if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
202 : {
203 0 : return uno::Any( xWorkSheets );
204 : }
205 : // pass on to collection
206 0 : return uno::Any( xWorkSheets->Item( aIndex, uno::Any() ) );
207 : }
208 : uno::Any SAL_CALL
209 0 : ScVbaWorkbook::Windows( const uno::Any& aIndex ) throw (uno::RuntimeException)
210 : {
211 :
212 0 : uno::Reference< excel::XWindows > xWindows( new ScVbaWindows( getParent(), mxContext ) );
213 0 : if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
214 0 : return uno::Any( xWindows );
215 0 : return uno::Any( xWindows->Item( aIndex, uno::Any() ) );
216 : }
217 :
218 : void SAL_CALL
219 0 : ScVbaWorkbook::Activate() throw (uno::RuntimeException)
220 : {
221 0 : VbaDocumentBase::Activate();
222 0 : }
223 :
224 : void
225 0 : ScVbaWorkbook::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
226 : {
227 0 : VbaDocumentBase::Protect( aPassword );
228 0 : }
229 :
230 : ::sal_Bool
231 0 : ScVbaWorkbook::getProtectStructure() throw (uno::RuntimeException)
232 : {
233 0 : uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
234 0 : return xProt->isProtected();
235 : }
236 :
237 0 : ::sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed() throw (uno::RuntimeException)
238 : {
239 0 : uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
240 0 : ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
241 0 : return pDoc->GetDocOptions().IsCalcAsShown();
242 : }
243 :
244 0 : void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed ) throw (uno::RuntimeException)
245 : {
246 0 : uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
247 0 : ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
248 0 : ScDocOptions aOpt = pDoc->GetDocOptions();
249 0 : aOpt.SetCalcAsShown( _precisionAsDisplayed );
250 0 : pDoc->SetDocOptions( aOpt );
251 0 : }
252 :
253 : void
254 0 : ScVbaWorkbook::SaveCopyAs( const rtl::OUString& sFileName ) throw ( uno::RuntimeException)
255 : {
256 0 : rtl::OUString aURL;
257 0 : osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
258 0 : uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
259 0 : uno::Sequence< beans::PropertyValue > storeProps(1);
260 0 : storeProps[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ) );
261 0 : storeProps[0].Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MS Excel 97" ) );
262 0 : xStor->storeToURL( aURL, storeProps );
263 0 : }
264 :
265 : css::uno::Any SAL_CALL
266 0 : ScVbaWorkbook::Styles( const uno::Any& Item ) throw (uno::RuntimeException)
267 : {
268 : // quick look and Styles object doesn't seem to have a valid parent
269 : // or a least the object browser just shows an object that has no
270 : // variables ( therefore... leave as NULL for now )
271 0 : uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
272 0 : if ( Item.hasValue() )
273 0 : return dStyles->Item( Item, uno::Any() );
274 0 : return uno::makeAny( dStyles );
275 : }
276 :
277 : uno::Any SAL_CALL
278 0 : ScVbaWorkbook::Names( const uno::Any& aIndex ) throw (uno::RuntimeException)
279 : {
280 0 : uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
281 0 : uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
282 0 : uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("NamedRanges") ) ), uno::UNO_QUERY_THROW );
283 0 : uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
284 0 : if ( aIndex.hasValue() )
285 0 : return uno::Any( xNames->Item( aIndex, uno::Any() ) );
286 0 : return uno::Any( xNames );
287 : }
288 :
289 : rtl::OUString
290 0 : ScVbaWorkbook::getServiceImplName()
291 : {
292 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ScVbaWorkbook"));
293 : }
294 :
295 : uno::Sequence< rtl::OUString >
296 0 : ScVbaWorkbook::getServiceNames()
297 : {
298 0 : static uno::Sequence< rtl::OUString > aServiceNames;
299 0 : if ( aServiceNames.getLength() == 0 )
300 : {
301 0 : aServiceNames.realloc( 1 );
302 0 : aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Workbook" ) );
303 : }
304 0 : return aServiceNames;
305 : }
306 :
307 : ::rtl::OUString SAL_CALL
308 0 : ScVbaWorkbook::getCodeName() throw (css::uno::RuntimeException)
309 : {
310 0 : uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
311 0 : return xModelProp->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CodeName" ) ) ).get< ::rtl::OUString >();
312 : }
313 :
314 : sal_Int64
315 0 : ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId ) throw(css::uno::RuntimeException)
316 : {
317 0 : if (rId.getLength() == 16 &&
318 0 : 0 == memcmp( ScVbaWorksheet::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16 ))
319 : {
320 0 : return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
321 : }
322 0 : return 0;
323 : }
324 :
325 : namespace workbook
326 : {
327 : namespace sdecl = comphelper::service_decl;
328 0 : sdecl::vba_service_class_<ScVbaWorkbook, sdecl::with_args<true> > serviceImpl;
329 0 : extern sdecl::ServiceDecl const serviceDecl(
330 : serviceImpl,
331 : "ScVbaWorkbook",
332 : "ooo.vba.excel.Workbook" );
333 0 : }
334 :
335 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|