LibreOffice Module sc (master)  1
xmlsubti.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 "xmlsubti.hxx"
21 #include "xmlstyli.hxx"
22 #include "xmlimprt.hxx"
23 #include <document.hxx>
24 #include "XMLConverter.hxx"
25 #include <docuno.hxx>
27 #include <sheetdata.hxx>
28 #include <tabprotection.hxx>
29 #include <tokenarray.hxx>
30 #include <documentimport.hxx>
31 
32 #include <sal/log.hxx>
33 #include <osl/diagnose.h>
34 
35 #include <comphelper/base64.hxx>
37 #include <com/sun/star/sheet/XSpreadsheet.hpp>
38 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
39 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
40 
41 #include <memory>
42 
43 using namespace com::sun::star;
44 
46  meHash1(PASSHASH_SHA1),
47  meHash2(PASSHASH_UNSPECIFIED),
48  mbProtected(false),
49  mbSelectProtectedCells(true),
50  mbSelectUnprotectedCells(true),
51  mbInsertColumns(false),
52  mbInsertRows(false),
53  mbDeleteColumns(false),
54  mbDeleteRows(false)
55 {
56 }
57 
59  : rImport(rTempImport),
60  aFixupOLEs(rTempImport),
61  maCurrentCellPos(ScAddress::INITIALIZE_INVALID),
62  nCurrentColCount(0),
63  nCurrentDrawPage( -1 ),
64  nCurrentXShapes( -1 )
65 {
66 }
67 
69 {
70 }
71 
72 namespace {
73 
74 uno::Reference<sheet::XSpreadsheet> getCurrentSheet(const uno::Reference<frame::XModel>& xModel, SCTAB nSheet)
75 {
76  uno::Reference<sheet::XSpreadsheet> xSheet;
77  uno::Reference<sheet::XSpreadsheetDocument> xSpreadDoc(xModel, uno::UNO_QUERY);
78  if (!xSpreadDoc.is())
79  return xSheet;
80 
81  uno::Reference <sheet::XSpreadsheets> xSheets(xSpreadDoc->getSheets());
82  if (!xSheets.is())
83  return xSheet;
84 
85  uno::Reference <container::XIndexAccess> xIndex(xSheets, uno::UNO_QUERY);
86  if (!xIndex.is())
87  return xSheet;
88 
89  xSheet.set(xIndex->getByIndex(nSheet), uno::UNO_QUERY);
90  return xSheet;
91 }
92 
93 }
94 
95 void ScMyTables::NewSheet(const OUString& sTableName, const OUString& sStyleName,
96  const ScXMLTabProtectionData& rProtectData)
97 {
98  if (!rImport.GetModel().is())
99  return;
100 
101  nCurrentColCount = 0;
102  sCurrentSheetName = sTableName;
103  //reset cols and rows for new sheet, but increment tab
107 
108  maProtectionData = rProtectData;
110 
111  // The document contains one sheet when created. So for the first
112  // sheet, we only need to set its name.
113  if (maCurrentCellPos.Tab() > 0)
114  pDoc->AppendTabOnLoad(sTableName);
115  else
116  pDoc->SetTabNameOnLoad(maCurrentCellPos.Tab(), sTableName);
117 
118  xCurrentSheet = getCurrentSheet(rImport.GetModel(), maCurrentCellPos.Tab());
119  if (xCurrentSheet.is())
120  {
121  // We need to set the current cell range here regardless of
122  // presence of style name.
123  SetTableStyle(sStyleName);
124  }
125 }
126 
127 void ScMyTables::SetTableStyle(const OUString& sStyleName)
128 {
129  //these uno calls are a bit difficult to remove, XMLTableStyleContext::FillPropertySet uses
130  //SvXMLImportPropertyMapper::FillPropertySet
131  if ( sStyleName.isEmpty() )
132  return;
133 
134  // #i57869# All table style properties for all sheets are now applied here,
135  // before importing the contents.
136  // This is needed for the background color.
137  // Sheet visibility has special handling in ScDocFunc::SetTableVisible to
138  // allow hiding the first sheet.
139  // RTL layout is only remembered, not actually applied, so the shapes can
140  // be loaded before mirroring.
141 
142  if ( !xCurrentSheet.is() )
143  return;
144 
145  uno::Reference <beans::XPropertySet> xProperties(xCurrentSheet, uno::UNO_QUERY);
146  if ( !xProperties.is() )
147  return;
148 
150  if ( pStyles )
151  {
152  XMLTableStyleContext* pStyle = const_cast<XMLTableStyleContext*>(static_cast<const XMLTableStyleContext *>(pStyles->FindStyleChildContext(
153  XmlStyleFamily::TABLE_TABLE, sStyleName, true)));
154  if ( pStyle )
155  {
156  pStyle->FillPropertySet(xProperties);
157 
158  ScSheetSaveData* pSheetData = comphelper::getUnoTunnelImplementation<ScModelObj>(rImport.GetModel())->GetSheetSaveData();
159  pSheetData->AddTableStyle( sStyleName, ScAddress( 0, 0, maCurrentCellPos.Tab() ) );
160  }
161  }
162 }
163 
165 {
167  maCurrentCellPos.SetCol(-1); //reset columns for new row
168 }
169 
170 void ScMyTables::SetRowStyle(const OUString& rCellStyleName)
171 {
172  rImport.GetStylesImportHelper()->SetRowStyle(rCellStyleName);
173 }
174 
175 void ScMyTables::AddColumn(bool bIsCovered)
176 {
178  //here only need to set column style if this is the first row and
179  //the cell is not covered.
180  if(maCurrentCellPos.Row() == 0 && !bIsCovered)
182 }
183 
185 {
187 
190 
192 
194  return;
195 
196  uno::Sequence<sal_Int8> aHash;
198 
199  std::unique_ptr<ScTableProtection> pProtect(new ScTableProtection);
200  pProtect->setProtected(maProtectionData.mbProtected);
201  pProtect->setPasswordHash(aHash, maProtectionData.meHash1, maProtectionData.meHash2);
209 }
210 
211 void ScMyTables::AddColStyle(const sal_Int32 nRepeat, const OUString& rCellStyleName)
212 {
213  rImport.GetStylesImportHelper()->AddColumnStyle(rCellStyleName, nCurrentColCount, nRepeat);
214  nCurrentColCount += nRepeat;
215  SAL_WARN_IF(nCurrentColCount > MAXCOLCOUNT, "sc", "more columns than fit into SCCOL");
216  nCurrentColCount = std::min<sal_Int32>( nCurrentColCount, MAXCOLCOUNT );
217 }
218 
219 uno::Reference< drawing::XDrawPage > const & ScMyTables::GetCurrentXDrawPage()
220 {
221  if( (maCurrentCellPos.Tab() != nCurrentDrawPage) || !xDrawPage.is() )
222  {
223  uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier( xCurrentSheet, uno::UNO_QUERY );
224  if( xDrawPageSupplier.is() )
225  xDrawPage.set(xDrawPageSupplier->getDrawPage());
226  nCurrentDrawPage = sal::static_int_cast<sal_Int16>(maCurrentCellPos.Tab());
227  }
228  return xDrawPage;
229 }
230 
231 uno::Reference< drawing::XShapes > const & ScMyTables::GetCurrentXShapes()
232 {
233  if( (maCurrentCellPos.Tab() != nCurrentXShapes) || !xShapes.is() )
234  {
236  rImport.GetShapeImport()->startPage(xShapes);
237  rImport.GetShapeImport()->pushGroupForPostProcessing ( xShapes );
238  nCurrentXShapes = sal::static_int_cast<sal_Int16>(maCurrentCellPos.Tab());
239  }
240  return xShapes;
241 }
242 
244 {
245  return (maCurrentCellPos.Tab() == nCurrentDrawPage) && xDrawPage.is();
246 }
247 
249 {
250  return (maCurrentCellPos.Tab() == nCurrentXShapes) && xShapes.is();
251 }
252 
253 void ScMyTables::AddOLE(const uno::Reference <drawing::XShape>& rShape,
254  const OUString &rRangeList)
255 {
256  aFixupOLEs.AddOLE(rShape, rRangeList);
257 }
258 
260  const SCCOL nStartColumn, const SCROW nStartRow, const SCCOL nEndColumn, const SCROW nEndRow,
261  const OUString& rFormula, const OUString& rFormulaNmsp, const formula::FormulaGrammar::Grammar eGrammar)
262 {
263  OSL_ENSURE(nEndRow >= nStartRow, "wrong row order");
264  OSL_ENSURE(nEndColumn >= nStartColumn, "wrong column order");
265  ScRange aScRange(
266  nStartColumn, nStartRow, maCurrentCellPos.Tab(),
267  nEndColumn, nEndRow, maCurrentCellPos.Tab()
268  );
269 
270  maMatrixRangeList.push_back(aScRange);
271 
272  ScDocumentImport& rDoc = rImport.GetDoc();
273  ScTokenArray aCode(&rDoc.getDoc());
274  aCode.AssignXMLString( rFormula,
275  ((eGrammar == formula::FormulaGrammar::GRAM_EXTERNAL) ? rFormulaNmsp : OUString()));
276  rDoc.setMatrixCells(aScRange, aCode, eGrammar);
277  rDoc.getDoc().IncXMLImportedFormulaCount( rFormula.getLength() );
278 }
279 
280 bool ScMyTables::IsPartOfMatrix(const ScAddress& rScAddress) const
281 {
282  if (!maMatrixRangeList.empty())
283  return maMatrixRangeList.In(rScAddress);
284  return false;
285 }
286 
288 {
289  return std::min<sal_Int32>(nCurrentColCount, rImport.GetDocument()->MaxCol());
290 }
291 
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void NewSheet(const OUString &sTableName, const OUString &sStyleName, const ScXMLTabProtectionData &rProtectData)
Definition: xmlsubti.cxx:95
void InsertCol(const sal_Int32 nCol, const sal_Int32 nTab)
void IncXMLImportedFormulaCount(sal_uLong nVal)
Definition: document.hxx:2363
SCROW Row() const
Definition: address.hxx:262
void SetRowStyle(const OUString &rCellStyleName)
Definition: xmlsubti.cxx:170
SC_DLLPUBLIC void SetTabProtection(SCTAB nTab, const ScTableProtection *pProtect)
Definition: documen3.cxx:1888
ScDocument * GetDocument()
Definition: xmlimprt.hxx:296
sheet protection state container
bool HasDrawPage() const
Definition: xmlsubti.cxx:243
void AssignXMLString(const OUString &rText, const OUString &rFormulaNmsp)
Assign XML string placeholder to the array.
Definition: token.cxx:2254
ScXMLTabProtectionData maProtectionData
Definition: xmlsubti.hxx:63
static ScDocument * GetScDocument(const css::uno::Reference< css::frame::XModel > &xModel)
void SetTableStyle(const OUString &sStyleName)
Definition: xmlsubti.cxx:127
ScMyStylesImportHelper * GetStylesImportHelper()
Definition: xmlimprt.hxx:354
const SCCOL MAXCOLCOUNT
Definition: address.hxx:64
Accessor class to ScDocument.
ScPasswordHash meHash1
Definition: xmlsubti.hxx:37
void SetRowStyle(const OUString &rStyleName)
css::uno::Reference< css::drawing::XShapes > xShapes
Definition: xmlsubti.hxx:59
void AddColumn(bool bIsCovered)
Definition: xmlsubti.cxx:175
ScRangeList maMatrixRangeList
Definition: xmlsubti.hxx:62
bool In(const ScRange &) const
Definition: rangelst.cxx:1089
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override
Definition: xmlstyli.cxx:527
SCCOL GetCurrentColCount() const
Definition: xmlsubti.cxx:287
void push_back(const ScRange &rRange)
Definition: rangelst.cxx:1144
SCTAB Tab() const
Definition: address.hxx:271
void SetRow(SCROW nRowP)
Definition: address.hxx:275
bool HasXShapes() const
Definition: xmlsubti.cxx:248
void SetCol(SCCOL nColP)
Definition: address.hxx:279
OUString sCurrentSheetName
Definition: xmlsubti.hxx:60
bool empty() const
Definition: rangelst.hxx:89
void setMatrixCells(const ScRange &rRange, const ScTokenArray &rArray, formula::FormulaGrammar::Grammar eGrammar)
void SetTabNameOnLoad(SCTAB nTab, const OUString &rName)
Definition: document.cxx:483
void SetTab(SCTAB nTabP)
Definition: address.hxx:283
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:875
ScMyOLEFixer aFixupOLEs
Definition: xmlsubti.hxx:55
void AddTableStyle(const OUString &rName, const ScAddress &rCellPos)
Definition: sheetdata.cxx:54
static void decode(css::uno::Sequence< sal_Int8 > &aPass, const OUString &sBuffer)
sal_Int16 SCCOL
Definition: types.hxx:22
void AddOLE(const css::uno::Reference< css::drawing::XShape > &rShape, const OUString &rRangeList)
sal_Int32 nCurrentColCount
Definition: xmlsubti.hxx:64
css::uno::Reference< css::drawing::XDrawPage > xDrawPage
Definition: xmlsubti.hxx:58
Use this class to manage solar mutex locking instead of calling LockSolarMutex() and UnlockSolarMutex...
Definition: xmlimprt.hxx:392
bool IsPartOfMatrix(const ScAddress &rScAddress) const
Definition: xmlsubti.cxx:280
sal_Int16 nCurrentDrawPage
Definition: xmlsubti.hxx:65
void AddMatrixRange(const SCCOL nStartColumn, const SCROW nStartRow, const SCCOL nEndColumn, const SCROW nEndRow, const OUString &rFormula, const OUString &rFormulaNmsp, const formula::FormulaGrammar::Grammar)
Definition: xmlsubti.cxx:259
SCCOL Col() const
Definition: address.hxx:267
void AddColStyle(const sal_Int32 nRepeat, const OUString &rCellStyleName)
Definition: xmlsubti.cxx:211
void DeleteTable()
Definition: xmlsubti.cxx:184
sal_Int32 SCROW
Definition: types.hxx:18
ScXMLImport & rImport
Definition: xmlsubti.hxx:53
ScMyTables(ScXMLImport &rImport)
Definition: xmlsubti.cxx:58
void AppendTabOnLoad(const OUString &rName)
Definition: document.cxx:471
#define SAL_WARN_IF(condition, area, stream)
css::uno::Reference< css::drawing::XShapes > const & GetCurrentXShapes()
Definition: xmlsubti.cxx:231
ScAddress maCurrentCellPos
Definition: xmlsubti.hxx:61
void AddColumnStyle(const OUString &rStyleName, const sal_Int32 nColumn, const sal_Int32 nRepeat)
const css::uno::Reference< css::frame::XModel > & GetModel() const
const SvXMLStyleContext * FindStyleChildContext(XmlStyleFamily nFamily, const OUString &rName, bool bCreateIndex=false) const
css::uno::Reference< css::sheet::XSpreadsheet > xCurrentSheet
Definition: xmlsubti.hxx:57
SvXMLStylesContext * GetAutoStyles()
sal_Int16 nCurrentXShapes
Definition: xmlsubti.hxx:66
void AddOLE(const css::uno::Reference< css::drawing::XShape > &rShape, const OUString &rRangeList)
Definition: xmlsubti.cxx:253
ScPasswordHash meHash2
Definition: xmlsubti.hxx:38
void AddRow()
Definition: xmlsubti.cxx:164
void RemoveAll()
Definition: rangelst.cxx:1108
ScDocumentImport & GetDoc()
Definition: xmlimprt.cxx:722
sal_Int16 SCTAB
Definition: types.hxx:23
void SetStylesToRangesFinished()
Definition: xmlimprt.cxx:1334
css::uno::Reference< css::drawing::XDrawPage > const & GetCurrentXDrawPage()
Definition: xmlsubti.cxx:219
rtl::Reference< XMLShapeImportHelper > const & GetShapeImport()
ScDocument & getDoc()