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