Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / xml / xmlsubti.cxx
bloba20b74a328a2230b36d23bf96d264bbf7dc24c7e
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 "xmlsubti.hxx"
21 #include "xmlstyli.hxx"
22 #include "xmlimprt.hxx"
23 #include <document.hxx>
24 #include "XMLConverter.hxx"
25 #include <docuno.hxx>
26 #include "XMLStylesImportHelper.hxx"
27 #include <sheetdata.hxx>
28 #include <tabprotection.hxx>
29 #include <tokenarray.hxx>
30 #include <documentimport.hxx>
32 #include <sal/log.hxx>
33 #include <osl/diagnose.h>
35 #include <comphelper/base64.hxx>
36 #include <comphelper/servicehelper.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>
41 using namespace com::sun::star;
43 ScXMLTabProtectionData::ScXMLTabProtectionData() :
44 meHash1(PASSHASH_SHA1),
45 meHash2(PASSHASH_UNSPECIFIED),
46 mbProtected(false),
47 mbSelectProtectedCells(true),
48 mbSelectUnprotectedCells(true),
49 mbInsertColumns(false),
50 mbInsertRows(false),
51 mbDeleteColumns(false),
52 mbDeleteRows(false)
56 ScMyTables::ScMyTables(ScXMLImport& rTempImport)
57 : rImport(rTempImport),
58 aFixupOLEs(rTempImport),
59 maCurrentCellPos(ScAddress::INITIALIZE_INVALID),
60 nCurrentColCount(0),
61 nCurrentDrawPage( -1 ),
62 nCurrentXShapes( -1 )
66 ScMyTables::~ScMyTables()
70 namespace {
72 uno::Reference<sheet::XSpreadsheet> getCurrentSheet(const uno::Reference<frame::XModel>& xModel, SCTAB nSheet)
74 uno::Reference<sheet::XSpreadsheet> xSheet;
75 uno::Reference<sheet::XSpreadsheetDocument> xSpreadDoc(xModel, uno::UNO_QUERY);
76 if (!xSpreadDoc.is())
77 return xSheet;
79 uno::Reference <sheet::XSpreadsheets> xSheets(xSpreadDoc->getSheets());
80 if (!xSheets.is())
81 return xSheet;
83 uno::Reference <container::XIndexAccess> xIndex(xSheets, uno::UNO_QUERY);
84 if (!xIndex.is())
85 return xSheet;
87 xSheet.set(xIndex->getByIndex(nSheet), uno::UNO_QUERY);
88 return xSheet;
93 void ScMyTables::NewSheet(const OUString& sTableName, const OUString& sStyleName,
94 const ScXMLTabProtectionData& rProtectData)
96 if (!rImport.GetModel().is())
97 return;
99 nCurrentColCount = 0;
100 sCurrentSheetName = sTableName;
101 //reset cols and rows for new sheet, but increment tab
102 maCurrentCellPos.SetCol(-1);
103 maCurrentCellPos.SetRow(-1);
104 maCurrentCellPos.SetTab(maCurrentCellPos.Tab() + 1);
106 maProtectionData = rProtectData;
107 ScDocument *pDoc = ScXMLConverter::GetScDocument(rImport.GetModel());
109 // The document contains one sheet when created. So for the first
110 // sheet, we only need to set its name.
111 if (maCurrentCellPos.Tab() > 0)
112 pDoc->AppendTabOnLoad(sTableName);
113 else
114 pDoc->SetTabNameOnLoad(maCurrentCellPos.Tab(), sTableName);
116 xCurrentSheet = getCurrentSheet(rImport.GetModel(), maCurrentCellPos.Tab());
117 if (xCurrentSheet.is())
119 // We need to set the current cell range here regardless of
120 // presence of style name.
121 SetTableStyle(sStyleName);
125 void ScMyTables::SetTableStyle(const OUString& sStyleName)
127 //these uno calls are a bit difficult to remove, XMLTableStyleContext::FillPropertySet uses
128 //SvXMLImportPropertyMapper::FillPropertySet
129 if ( sStyleName.isEmpty() )
130 return;
132 // #i57869# All table style properties for all sheets are now applied here,
133 // before importing the contents.
134 // This is needed for the background color.
135 // Sheet visibility has special handling in ScDocFunc::SetTableVisible to
136 // allow hiding the first sheet.
137 // RTL layout is only remembered, not actually applied, so the shapes can
138 // be loaded before mirroring.
140 if ( !xCurrentSheet.is() )
141 return;
143 uno::Reference <beans::XPropertySet> xProperties(xCurrentSheet, uno::UNO_QUERY);
144 if ( !xProperties.is() )
145 return;
147 XMLTableStylesContext *pStyles = static_cast<XMLTableStylesContext *>(rImport.GetAutoStyles());
148 if ( pStyles )
150 XMLTableStyleContext* pStyle = const_cast<XMLTableStyleContext*>(static_cast<const XMLTableStyleContext *>(pStyles->FindStyleChildContext(
151 XmlStyleFamily::TABLE_TABLE, sStyleName, true)));
152 if ( pStyle )
154 pStyle->FillPropertySet(xProperties);
156 ScSheetSaveData* pSheetData = comphelper::getFromUnoTunnel<ScModelObj>(rImport.GetModel())->GetSheetSaveData();
157 pSheetData->AddTableStyle( sStyleName, ScAddress( 0, 0, maCurrentCellPos.Tab() ) );
162 void ScMyTables::AddRow()
164 maCurrentCellPos.SetRow(maCurrentCellPos.Row() + 1);
165 maCurrentCellPos.SetCol(-1); //reset columns for new row
168 void ScMyTables::SetRowStyle(const OUString& rCellStyleName)
170 rImport.GetStylesImportHelper()->SetRowStyle(rCellStyleName);
173 void ScMyTables::AddColumn(bool bIsCovered)
175 maCurrentCellPos.SetCol( maCurrentCellPos.Col() + 1 );
176 //here only need to set column style if this is the first row and
177 //the cell is not covered.
178 if(maCurrentCellPos.Row() == 0 && !bIsCovered)
179 rImport.GetStylesImportHelper()->InsertCol(maCurrentCellPos.Col(), maCurrentCellPos.Tab());
182 void ScMyTables::DeleteTable()
184 ScXMLImport::MutexGuard aGuard(rImport);
186 rImport.GetStylesImportHelper()->SetStylesToRanges();
187 rImport.SetStylesToRangesFinished();
189 maMatrixRangeList.RemoveAll();
191 if (!(rImport.GetDocument() && maProtectionData.mbProtected))
192 return;
194 uno::Sequence<sal_Int8> aHash;
195 ::comphelper::Base64::decode(aHash, maProtectionData.maPassword);
197 ScTableProtection aProtect;
198 aProtect.setProtected(maProtectionData.mbProtected);
199 aProtect.setPasswordHash(aHash, maProtectionData.meHash1, maProtectionData.meHash2);
200 aProtect.setOption(ScTableProtection::SELECT_LOCKED_CELLS, maProtectionData.mbSelectProtectedCells);
201 aProtect.setOption(ScTableProtection::SELECT_UNLOCKED_CELLS, maProtectionData.mbSelectUnprotectedCells);
202 aProtect.setOption(ScTableProtection::INSERT_COLUMNS, maProtectionData.mbInsertColumns);
203 aProtect.setOption(ScTableProtection::INSERT_ROWS, maProtectionData.mbInsertRows);
204 aProtect.setOption(ScTableProtection::DELETE_COLUMNS, maProtectionData.mbDeleteColumns);
205 aProtect.setOption(ScTableProtection::DELETE_ROWS, maProtectionData.mbDeleteRows);
206 rImport.GetDocument()->SetTabProtection(maCurrentCellPos.Tab(), &aProtect);
209 void ScMyTables::AddColStyle(const sal_Int32 nRepeat, const OUString& rCellStyleName)
211 rImport.GetStylesImportHelper()->AddColumnStyle(rCellStyleName, nCurrentColCount, nRepeat);
212 nCurrentColCount += nRepeat;
213 SAL_WARN_IF(nCurrentColCount > rImport.GetDocument()->GetSheetLimits().GetMaxColCount(),
214 "sc", "more columns than fit into SCCOL");
215 nCurrentColCount = std::min<sal_Int32>( nCurrentColCount, rImport.GetDocument()->GetSheetLimits().GetMaxColCount() );
218 uno::Reference< drawing::XDrawPage > const & ScMyTables::GetCurrentXDrawPage()
220 if( (maCurrentCellPos.Tab() != nCurrentDrawPage) || !xDrawPage.is() )
222 uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier( xCurrentSheet, uno::UNO_QUERY );
223 if( xDrawPageSupplier.is() )
224 xDrawPage.set(xDrawPageSupplier->getDrawPage());
225 nCurrentDrawPage = sal::static_int_cast<sal_Int16>(maCurrentCellPos.Tab());
227 return xDrawPage;
230 uno::Reference< drawing::XShapes > const & ScMyTables::GetCurrentXShapes()
232 if( (maCurrentCellPos.Tab() != nCurrentXShapes) || !xShapes.is() )
234 xShapes = GetCurrentXDrawPage();
235 rImport.GetShapeImport()->startPage(xShapes);
236 rImport.GetShapeImport()->pushGroupForPostProcessing ( xShapes );
237 nCurrentXShapes = sal::static_int_cast<sal_Int16>(maCurrentCellPos.Tab());
239 return xShapes;
242 bool ScMyTables::HasDrawPage() const
244 return (maCurrentCellPos.Tab() == nCurrentDrawPage) && xDrawPage.is();
247 bool ScMyTables::HasXShapes() const
249 return (maCurrentCellPos.Tab() == nCurrentXShapes) && xShapes.is();
252 void ScMyTables::AddOLE(const uno::Reference <drawing::XShape>& rShape,
253 const OUString &rRangeList)
255 aFixupOLEs.AddOLE(rShape, rRangeList);
258 void ScMyTables::AddMatrixRange(
259 const SCCOL nStartColumn, const SCROW nStartRow, const SCCOL nEndColumn, const SCROW nEndRow,
260 const OUString& rFormula, const OUString& rFormulaNmsp, const formula::FormulaGrammar::Grammar eGrammar)
262 OSL_ENSURE(nEndRow >= nStartRow, "wrong row order");
263 OSL_ENSURE(nEndColumn >= nStartColumn, "wrong column order");
264 ScRange aScRange(
265 nStartColumn, nStartRow, maCurrentCellPos.Tab(),
266 nEndColumn, nEndRow, maCurrentCellPos.Tab()
269 maMatrixRangeList.push_back(aScRange);
271 ScDocumentImport& rDoc = rImport.GetDoc();
272 ScTokenArray aCode(rDoc.getDoc());
273 aCode.AssignXMLString( rFormula,
274 ((eGrammar == formula::FormulaGrammar::GRAM_EXTERNAL) ? rFormulaNmsp : OUString()));
275 rDoc.setMatrixCells(aScRange, aCode, eGrammar);
276 rDoc.getDoc().IncXMLImportedFormulaCount( rFormula.getLength() );
279 bool ScMyTables::IsPartOfMatrix(const ScAddress& rScAddress) const
281 if (!maMatrixRangeList.empty())
282 return maMatrixRangeList.Contains(rScAddress);
283 return false;
286 SCCOL ScMyTables::GetCurrentColCount() const
288 return std::min<sal_Int32>(nCurrentColCount, rImport.GetDocument()->MaxCol());
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */