LibreOffice Module sc (master)  1
vbaworkbooks.cxx
Go to the documentation of this file.
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 <com/sun/star/sheet/XSpreadsheetDocument.hpp>
21 #include <com/sun/star/container/XEnumerationAccess.hpp>
22 #include <com/sun/star/frame/XModel.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <com/sun/star/document/XTypeDetection.hpp>
25 
26 #include <tools/urlobj.hxx>
27 
28 #include "excelvbahelper.hxx"
29 #include "vbaworkbook.hxx"
30 #include "vbaworkbooks.hxx"
31 #include <vbahelper/vbahelper.hxx>
32 
33 #include <osl/file.hxx>
34 #include <rtl/ref.hxx>
35 
36 using namespace ::ooo::vba;
37 using namespace ::com::sun::star;
38 
39 const sal_Int16 CUSTOM_CHAR = 5;
40 
41 static uno::Any
42 getWorkbook( const uno::Reference< uno::XComponentContext >& xContext,
43  const uno::Reference< sheet::XSpreadsheetDocument > &xDoc,
44  const uno::Reference< XHelperInterface >& xParent )
45 {
46  // FIXME: fine as long as ScVbaWorkbook is stateless ...
47  uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
48  if( !xModel.is() )
49  return uno::Any();
50 
51  uno::Reference< excel::XWorkbook > xWb( getVBADocument( xModel ), uno::UNO_QUERY );
52  if ( xWb.is() )
53  {
54  return uno::Any( xWb );
55  }
56 
57  rtl::Reference<ScVbaWorkbook> pWb = new ScVbaWorkbook( xParent, xContext, xModel );
58  return uno::Any( uno::Reference< excel::XWorkbook > (pWb) );
59 }
60 
61 namespace {
62 
63 class WorkBookEnumImpl : public EnumerationHelperImpl
64 {
65 public:
67  WorkBookEnumImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ) {}
68 
69  virtual uno::Any SAL_CALL nextElement( ) override
70  {
71  uno::Reference< sheet::XSpreadsheetDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
72  return getWorkbook( m_xContext, xDoc, m_xParent );
73  }
74 
75 };
76 
77 }
78 
79 ScVbaWorkbooks::ScVbaWorkbooks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWorkbooks_BASE( xParent, xContext, VbaDocumentsBase::EXCEL_DOCUMENT )
80 {
81 }
82 // XEnumerationAccess
85 {
87 }
88 uno::Reference< container::XEnumeration >
90 {
91  // #FIXME it's possible the WorkBookEnumImpl here doesn't reflect
92  // the state of this object ( although it should ) would be
93  // safer to create an enumeration based on this objects state
94  // rather than one effectively based of the desktop component
95  uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
96  return new WorkBookEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration() );
97 }
98 
100 ScVbaWorkbooks::createCollectionObject( const css::uno::Any& aSource )
101 {
102  uno::Reference< sheet::XSpreadsheetDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
103  return getWorkbook( mxContext, xDoc, mxParent );
104 }
105 
106 uno::Any SAL_CALL
107 ScVbaWorkbooks::Add( const uno::Any& Template )
108 {
109  uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc;
110  sal_Int32 nWorkbookType = 0;
111  OUString aTemplateFileName;
112  if( Template >>= nWorkbookType )
113  {
114  // nWorkbookType is a constant from XlWBATemplate (added in Excel 2007)
115  // TODO: create chart-sheet if supported by Calc
116 
117  xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
118  // create a document with one sheet only
119  uno::Reference< sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), uno::UNO_SET_THROW );
120  uno::Reference< container::XIndexAccess > xSheetsIA( xSheets, uno::UNO_QUERY_THROW );
121  while( xSheetsIA->getCount() > 1 )
122  {
123  uno::Reference< container::XNamed > xSheetName( xSheetsIA->getByIndex( xSheetsIA->getCount() - 1 ), uno::UNO_QUERY_THROW );
124  xSheets->removeByName( xSheetName->getName() );
125  }
126  }
127  else if( Template >>= aTemplateFileName )
128  {
129  // TODO: create document from template
130  xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
131  }
132  else if( !Template.hasValue() )
133  {
134  // regular spreadsheet document with configured number of sheets
135  xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
136  }
137  else
138  {
139  // illegal argument
140  throw uno::RuntimeException();
141  }
142 
143  // need to set up the document modules ( and vba mode ) here
144  excel::setUpDocumentModules( xSpreadDoc );
145  if( xSpreadDoc.is() )
146  return getWorkbook( mxContext, xSpreadDoc, mxParent );
147  return uno::Any();
148 }
149 
150 void SAL_CALL
152 {
153 }
154 
155 bool
156 ScVbaWorkbooks::isTextFile( std::u16string_view sType )
157 {
158  // will return true if the file is
159  // a) a variant of a text file
160  // b) a csv file
161  // c) unknown
162  // returning true basically means treat this like a csv file
163  return sType == u"generic_Text" || sType.empty();
164 }
165 
166 bool
167 ScVbaWorkbooks::isSpreadSheetFile( const OUString& sType )
168 {
169  // include calc_QPro etc. ? ( not for the moment anyway )
170  return sType.startsWith( "calc_MS" )
171  || sType.startsWith( "MS Excel" )
172  || sType.startsWith( "calc8" )
173  || sType.startsWith( "calc_StarOffice" );
174 }
175 
176 OUString
177 ScVbaWorkbooks::getFileFilterType( const OUString& rFileName )
178 {
179  uno::Reference< document::XTypeDetection > xTypeDetect( mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxContext), uno::UNO_QUERY_THROW );
180  uno::Sequence< beans::PropertyValue > aMediaDesc(1);
181  aMediaDesc[ 0 ].Name = "URL";
182  aMediaDesc[ 0 ].Value <<= rFileName;
183  OUString sType = xTypeDetect->queryTypeByDescriptor( aMediaDesc, true );
184  return sType;
185 }
186 
187 // #TODO# #FIXME# can any of the unused params below be used?
188 uno::Any SAL_CALL
189 ScVbaWorkbooks::Open( const 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*/ )
190 {
191  // we need to detect if this is a URL, if not then assume it's a file path
192  OUString aURL;
193  INetURLObject aObj;
194  aObj.SetURL( rFileName );
195  bool bIsURL = aObj.GetProtocol() != INetProtocol::NotValid;
196  if ( bIsURL )
197  aURL = rFileName;
198  else
199  osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
200 
201  uno::Sequence< beans::PropertyValue > sProps(0);
202 
203  OUString sType = getFileFilterType( aURL );
204  // A text file means it needs to be processed as a csv file
205  if ( isTextFile( sType ) )
206  {
207  sal_Int32 nIndex = 0;
208  // Values for format
209  // 1 Tabs
210  // 2 Commas
211  // 3 Spaces
212  // 4 Semicolons
213  // 5 Nothing
214  // 6 Custom character (see the Delimiter argument
215  // no format means use the current delimiter
216  sProps.realloc( 3 );
217  sProps[ nIndex ].Name = "FilterOptions";
218  sal_Int16 const delims[] { 0 /*default not used*/, 9/*tab*/, 44/*comma*/, 32/*space*/, 59/*semicolon*/ };
219 
220  OUString sFormat;
221  sal_Int16 nFormat = 0; // default indicator
222 
223  if ( Format.hasValue() )
224  {
225  Format >>= nFormat; // val of nFormat overwritten if extracted
226  // validate param
227  if ( nFormat < 1 || nFormat > 6 )
228  throw uno::RuntimeException("Illegal value for Format" );
229  }
230 
231  sal_Int16 nDelim = getCurrentDelim();
232 
233  if ( nFormat > 0 && nFormat < CUSTOM_CHAR )
234  {
235  nDelim = delims[ nFormat ];
236  }
237  else if ( nFormat > CUSTOM_CHAR )
238  {
239  // Need to check Delimiter param
240  if ( !Delimiter.hasValue() )
241  throw uno::RuntimeException("Expected value for Delimiter" );
242  OUString sStr;
243  Delimiter >>= sStr;
244  if ( sStr.isEmpty() )
245  throw uno::RuntimeException("Incorrect value for Delimiter" );
246 
247  nDelim = sStr[0];
248 
249  }
250 
251  getCurrentDelim() = nDelim; //set new current
252 
253  sFormat = OUString::number( nDelim ) + ",34,0,1";
254  sProps[ nIndex++ ].Value <<= sFormat;
255  sProps[ nIndex ].Name = "FilterName";
256  sProps[ nIndex++ ].Value <<= OUString( SC_TEXT_CSV_FILTER_NAME );
257  // Ensure WORKAROUND_CSV_TXT_BUG_i60158 gets called in typedetection.cxx so
258  // csv is forced for deep detected 'writerxxx' types
259  sProps[ nIndex ].Name = "DocumentService";
260  sProps[ nIndex ].Value <<= OUString("com.sun.star.sheet.SpreadsheetDocument");
261  }
262  else if ( !isSpreadSheetFile( sType ) )
263  throw uno::RuntimeException("Bad Format" );
264 
265  uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( openDocument( rFileName, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
266  uno::Any aRet = getWorkbook( mxContext, xSpreadDoc, mxParent );
267  uno::Reference< excel::XWorkbook > xWBook( aRet, uno::UNO_QUERY );
268  if ( xWBook.is() )
269  xWBook->Activate();
270  return aRet;
271 }
272 
273 OUString
275 {
276  return "ScVbaWorkbooks";
277 }
278 
279 css::uno::Sequence<OUString>
281 {
282  static uno::Sequence< OUString > const sNames
283  {
284  "ooo.vba.excel.Workbooks"
285  };
286  return sNames;
287 }
288 
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
ScVbaWorkbooks(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext)
URL aURL
bool hasValue()
sal_Int32 nIndex
void setUpDocumentModules(const uno::Reference< sheet::XSpreadsheetDocument > &xDoc)
uno::Reference< uno::XComponentContext > mxContext
WeakReference< XInterface > mxParent
virtual css::uno::Sequence< OUString > getServiceNames() override
const sal_Int16 CUSTOM_CHAR
virtual OUString getServiceImplName() override
static sal_Int16 & getCurrentDelim()
virtual void SAL_CALL Close() override
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Any SAL_CALL Add(const css::uno::Any &Template) override
OptionalString sType
float u
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
css::uno::Type const & get()
static uno::Any getWorkbook(const uno::Reference< uno::XComponentContext > &xContext, const uno::Reference< sheet::XSpreadsheetDocument > &xDoc, const uno::Reference< XHelperInterface > &xParent)
virtual css::uno::Any SAL_CALL Open(const OUString &Filename, const css::uno::Any &UpdateLinks, const css::uno::Any &ReadOnly, const css::uno::Any &Format, const css::uno::Any &Password, const css::uno::Any &WriteResPassword, const css::uno::Any &IgnoreReadOnlyRecommended, const css::uno::Any &Origin, const css::uno::Any &Delimiter, const css::uno::Any &Editable, const css::uno::Any &Notify, const css::uno::Any &Converter, const css::uno::Any &AddToMru) override
uno::Reference< XHelperInterface > getVBADocument(const uno::Reference< frame::XModel > &xModel)
INetProtocol GetProtocol() const
#define SC_TEXT_CSV_FILTER_NAME
Definition: global.hxx:61
OUString getFileFilterType(const OUString &rString)
bool SetURL(OUString const &rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
Reference< XModel > xModel
static bool isSpreadSheetFile(const OUString &rString)
cppu::ImplInheritanceHelper< VbaDocumentsBase, ov::excel::XWorkbooks > ScVbaWorkbooks_BASE
static bool isTextFile(std::u16string_view rString)