tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / vba / vbaworkbooks.cxx
blob509324d857f7857b0bcece3414b88526f32e6aee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
26 #include <tools/urlobj.hxx>
28 #include "excelvbahelper.hxx"
29 #include "vbaworkbook.hxx"
30 #include "vbaworkbooks.hxx"
31 #include <vbahelper/vbahelper.hxx>
33 #include <comphelper/propertyvalue.hxx>
34 #include <o3tl/string_view.hxx>
35 #include <osl/file.hxx>
36 #include <rtl/ref.hxx>
38 using namespace ::ooo::vba;
39 using namespace ::com::sun::star;
41 const sal_Int16 CUSTOM_CHAR = 5;
43 static uno::Any
44 getWorkbook( const uno::Reference< uno::XComponentContext >& xContext,
45 const uno::Reference< sheet::XSpreadsheetDocument > &xDoc,
46 const uno::Reference< XHelperInterface >& xParent )
48 // FIXME: fine as long as ScVbaWorkbook is stateless ...
49 uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
50 if( !xModel.is() )
51 return uno::Any();
53 uno::Reference< excel::XWorkbook > xWb( getVBADocument( xModel ), uno::UNO_QUERY );
54 if ( xWb.is() )
56 return uno::Any( xWb );
59 rtl::Reference<ScVbaWorkbook> pWb = new ScVbaWorkbook( xParent, xContext, xModel );
60 return uno::Any( uno::Reference< excel::XWorkbook > (pWb) );
63 namespace {
65 class WorkBookEnumImpl : public EnumerationHelperImpl
67 public:
68 /// @throws uno::RuntimeException
69 WorkBookEnumImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ) {}
71 virtual uno::Any SAL_CALL nextElement( ) override
73 uno::Reference< sheet::XSpreadsheetDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
74 return getWorkbook( m_xContext, xDoc, m_xParent );
81 ScVbaWorkbooks::ScVbaWorkbooks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWorkbooks_BASE( xParent, xContext, VbaDocumentsBase::EXCEL_DOCUMENT )
84 // XEnumerationAccess
85 uno::Type
86 ScVbaWorkbooks::getElementType()
88 return cppu::UnoType<excel::XWorkbook>::get();
90 uno::Reference< container::XEnumeration >
91 ScVbaWorkbooks::createEnumeration()
93 // #FIXME it's possible the WorkBookEnumImpl here doesn't reflect
94 // the state of this object ( although it should ) would be
95 // safer to create an enumeration based on this objects state
96 // rather than one effectively based of the desktop component
97 uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
98 return new WorkBookEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration() );
101 uno::Any
102 ScVbaWorkbooks::createCollectionObject( const css::uno::Any& aSource )
104 uno::Reference< sheet::XSpreadsheetDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
105 return getWorkbook( mxContext, xDoc, mxParent );
108 uno::Any SAL_CALL
109 ScVbaWorkbooks::Add( const uno::Any& Template )
111 uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc;
112 sal_Int32 nWorkbookType = 0;
113 OUString aTemplateFileName;
114 if( Template >>= nWorkbookType )
116 // nWorkbookType is a constant from XlWBATemplate (added in Excel 2007)
117 // TODO: create chart-sheet if supported by Calc
119 xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
120 // create a document with one sheet only
121 uno::Reference< sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), uno::UNO_SET_THROW );
122 uno::Reference< container::XIndexAccess > xSheetsIA( xSheets, uno::UNO_QUERY_THROW );
123 while( xSheetsIA->getCount() > 1 )
125 uno::Reference< container::XNamed > xSheetName( xSheetsIA->getByIndex( xSheetsIA->getCount() - 1 ), uno::UNO_QUERY_THROW );
126 xSheets->removeByName( xSheetName->getName() );
129 else if( Template >>= aTemplateFileName )
131 // TODO: create document from template
132 xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
134 else if( !Template.hasValue() )
136 // regular spreadsheet document with configured number of sheets
137 xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
139 else
141 // illegal argument
142 throw uno::RuntimeException();
145 // need to set up the document modules ( and vba mode ) here
146 excel::setUpDocumentModules( xSpreadDoc );
147 if (!xSpreadDoc.is())
148 return uno::Any();
150 uno::Any aRet = getWorkbook( mxContext, xSpreadDoc, mxParent );
151 uno::Reference< excel::XWorkbook > xWBook( aRet, uno::UNO_QUERY );
152 if (xWBook.is())
153 xWBook->Activate();
154 return aRet;
157 void SAL_CALL
158 ScVbaWorkbooks::Close()
162 bool
163 ScVbaWorkbooks::isTextFile( std::u16string_view sType )
165 // will return true if the file is
166 // a) a variant of a text file
167 // b) a csv file
168 // c) unknown
169 // returning true basically means treat this like a csv file
170 return sType == u"generic_Text" || sType.empty();
173 bool
174 ScVbaWorkbooks::isSpreadSheetFile( std::u16string_view sType )
176 // include calc_QPro etc. ? ( not for the moment anyway )
177 return o3tl::starts_with( sType, u"calc_MS" )
178 || o3tl::starts_with( sType, u"MS Excel" )
179 || o3tl::starts_with( sType, u"calc8" )
180 || o3tl::starts_with( sType, u"calc_StarOffice" );
183 OUString
184 ScVbaWorkbooks::getFileFilterType( const OUString& rFileName )
186 uno::Reference< document::XTypeDetection > xTypeDetect( mxContext->getServiceManager()->createInstanceWithContext(u"com.sun.star.document.TypeDetection"_ustr, mxContext), uno::UNO_QUERY_THROW );
187 uno::Sequence aMediaDesc{ comphelper::makePropertyValue(u"URL"_ustr, rFileName) };
188 OUString sType = xTypeDetect->queryTypeByDescriptor( aMediaDesc, true );
189 return sType;
192 // #TODO# #FIXME# can any of the unused params below be used?
193 uno::Any SAL_CALL
194 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*/ )
196 // we need to detect if this is a URL, if not then assume it's a file path
197 OUString aURL;
198 INetURLObject aObj;
199 aObj.SetURL( rFileName );
200 bool bIsURL = aObj.GetProtocol() != INetProtocol::NotValid;
201 if ( bIsURL )
202 aURL = rFileName;
203 else
204 osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
206 uno::Sequence< beans::PropertyValue > sProps;
208 OUString sType = getFileFilterType( aURL );
209 // A text file means it needs to be processed as a csv file
210 if ( isTextFile( sType ) )
212 // Values for format
213 // 1 Tabs
214 // 2 Commas
215 // 3 Spaces
216 // 4 Semicolons
217 // 5 Nothing
218 // 6 Custom character (see the Delimiter argument
219 // no format means use the current delimiter
220 sal_Int16 const delims[] { 0 /*default not used*/, 9/*tab*/, 44/*comma*/, 32/*space*/, 59/*semicolon*/ };
222 OUString sFormat;
223 sal_Int16 nFormat = 0; // default indicator
225 if ( Format.hasValue() )
227 Format >>= nFormat; // val of nFormat overwritten if extracted
228 // validate param
229 if ( nFormat < 1 || nFormat > 6 )
230 throw uno::RuntimeException(u"Illegal value for Format"_ustr );
233 sal_Int16 nDelim = getCurrentDelim();
235 if ( nFormat > 0 && nFormat < CUSTOM_CHAR )
237 nDelim = delims[ nFormat ];
239 else if ( nFormat > CUSTOM_CHAR )
241 // Need to check Delimiter param
242 if ( !Delimiter.hasValue() )
243 throw uno::RuntimeException(u"Expected value for Delimiter"_ustr );
244 OUString sStr;
245 Delimiter >>= sStr;
246 if ( sStr.isEmpty() )
247 throw uno::RuntimeException(u"Incorrect value for Delimiter"_ustr );
249 nDelim = sStr[0];
253 getCurrentDelim() = nDelim; //set new current
255 sFormat = OUString::number( nDelim ) + ",34,0,1";
257 sProps = { comphelper::makePropertyValue(u"FilterOptions"_ustr, sFormat),
258 comphelper::makePropertyValue(u"FilterName"_ustr, SC_TEXT_CSV_FILTER_NAME),
259 // Ensure WORKAROUND_CSV_TXT_BUG_i60158 gets called in typedetection.cxx so
260 // csv is forced for deep detected 'writerxxx' types
261 comphelper::makePropertyValue(
262 u"DocumentService"_ustr, u"com.sun.star.sheet.SpreadsheetDocument"_ustr) };
264 else if ( !isSpreadSheetFile( sType ) )
265 throw uno::RuntimeException(u"Bad Format"_ustr );
267 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( openDocument( rFileName, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
268 uno::Any aRet = getWorkbook( mxContext, xSpreadDoc, mxParent );
269 uno::Reference< excel::XWorkbook > xWBook( aRet, uno::UNO_QUERY );
270 if ( xWBook.is() )
271 xWBook->Activate();
272 return aRet;
275 OUString
276 ScVbaWorkbooks::getServiceImplName()
278 return u"ScVbaWorkbooks"_ustr;
281 css::uno::Sequence<OUString>
282 ScVbaWorkbooks::getServiceNames()
284 static uno::Sequence< OUString > const sNames
286 u"ooo.vba.excel.Workbooks"_ustr
288 return sNames;
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */