1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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>
43 using namespace com::sun::star
;
45 ScXMLTabProtectionData::ScXMLTabProtectionData() :
46 meHash1(PASSHASH_SHA1
),
47 meHash2(PASSHASH_UNSPECIFIED
),
49 mbSelectProtectedCells(true),
50 mbSelectUnprotectedCells(true),
51 mbInsertColumns(false),
53 mbDeleteColumns(false),
58 ScMyTables::ScMyTables(ScXMLImport
& rTempImport
)
59 : rImport(rTempImport
),
60 aFixupOLEs(rTempImport
),
61 maCurrentCellPos(ScAddress::INITIALIZE_INVALID
),
63 nCurrentDrawPage( -1 ),
68 ScMyTables::~ScMyTables()
74 uno::Reference
<sheet::XSpreadsheet
> getCurrentSheet(const uno::Reference
<frame::XModel
>& xModel
, SCTAB nSheet
)
76 uno::Reference
<sheet::XSpreadsheet
> xSheet
;
77 uno::Reference
<sheet::XSpreadsheetDocument
> xSpreadDoc(xModel
, uno::UNO_QUERY
);
81 uno::Reference
<sheet::XSpreadsheets
> xSheets(xSpreadDoc
->getSheets());
85 uno::Reference
<container::XIndexAccess
> xIndex(xSheets
, uno::UNO_QUERY
);
89 xSheet
.set(xIndex
->getByIndex(nSheet
), uno::UNO_QUERY
);
95 void ScMyTables::NewSheet(const OUString
& sTableName
, const OUString
& sStyleName
,
96 const ScXMLTabProtectionData
& rProtectData
)
98 if (rImport
.GetModel().is())
100 nCurrentColCount
= 0;
101 sCurrentSheetName
= sTableName
;
102 //reset cols and rows for new sheet, but increment tab
103 maCurrentCellPos
.SetCol(-1);
104 maCurrentCellPos
.SetRow(-1);
105 maCurrentCellPos
.SetTab(maCurrentCellPos
.Tab() + 1);
107 maProtectionData
= rProtectData
;
108 ScDocument
*pDoc
= ScXMLConverter::GetScDocument(rImport
.GetModel());
110 // The document contains one sheet when created. So for the first
111 // sheet, we only need to set its name.
112 if (maCurrentCellPos
.Tab() > 0)
113 pDoc
->AppendTabOnLoad(sTableName
);
115 pDoc
->SetTabNameOnLoad(maCurrentCellPos
.Tab(), sTableName
);
117 xCurrentSheet
= getCurrentSheet(rImport
.GetModel(), maCurrentCellPos
.Tab());
118 if (xCurrentSheet
.is())
120 // We need to set the current cell range here regardless of
121 // presence of style name.
122 SetTableStyle(sStyleName
);
127 void ScMyTables::SetTableStyle(const OUString
& sStyleName
)
129 //these uno calls are a bit difficult to remove, XMLTableStyleContext::FillPropertySet uses
130 //SvXMLImportPropertyMapper::FillPropertySet
131 if ( !sStyleName
.isEmpty() )
133 // #i57869# All table style properties for all sheets are now applied here,
134 // before importing the contents.
135 // This is needed for the background color.
136 // Sheet visibility has special handling in ScDocFunc::SetTableVisible to
137 // allow hiding the first sheet.
138 // RTL layout is only remembered, not actually applied, so the shapes can
139 // be loaded before mirroring.
141 if ( xCurrentSheet
.is() )
143 uno::Reference
<beans::XPropertySet
> xProperties(xCurrentSheet
, uno::UNO_QUERY
);
144 if ( xProperties
.is() )
146 XMLTableStylesContext
*pStyles
= static_cast<XMLTableStylesContext
*>(rImport
.GetAutoStyles());
149 XMLTableStyleContext
* pStyle
= const_cast<XMLTableStyleContext
*>(static_cast<const XMLTableStyleContext
*>(pStyles
->FindStyleChildContext(
150 XML_STYLE_FAMILY_TABLE_TABLE
, sStyleName
, true)));
153 pStyle
->FillPropertySet(xProperties
);
155 ScSheetSaveData
* pSheetData
= comphelper::getUnoTunnelImplementation
<ScModelObj
>(rImport
.GetModel())->GetSheetSaveData();
156 pSheetData
->AddTableStyle( sStyleName
, ScAddress( 0, 0, maCurrentCellPos
.Tab() ) );
164 void ScMyTables::AddRow()
166 maCurrentCellPos
.SetRow(maCurrentCellPos
.Row() + 1);
167 maCurrentCellPos
.SetCol(-1); //reset columns for new row
170 void ScMyTables::SetRowStyle(const OUString
& rCellStyleName
)
172 rImport
.GetStylesImportHelper()->SetRowStyle(rCellStyleName
);
175 void ScMyTables::AddColumn(bool bIsCovered
)
177 maCurrentCellPos
.SetCol( maCurrentCellPos
.Col() + 1 );
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
)
181 rImport
.GetStylesImportHelper()->InsertCol(maCurrentCellPos
.Col(), maCurrentCellPos
.Tab());
184 void ScMyTables::DeleteTable()
186 ScXMLImport::MutexGuard
aGuard(rImport
);
188 rImport
.GetStylesImportHelper()->SetStylesToRanges();
189 rImport
.SetStylesToRangesFinished();
191 maMatrixRangeList
.RemoveAll();
193 if (rImport
.GetDocument() && maProtectionData
.mbProtected
)
195 uno::Sequence
<sal_Int8
> aHash
;
196 ::comphelper::Base64::decode(aHash
, maProtectionData
.maPassword
);
198 std::unique_ptr
<ScTableProtection
> pProtect(new ScTableProtection
);
199 pProtect
->setProtected(maProtectionData
.mbProtected
);
200 pProtect
->setPasswordHash(aHash
, maProtectionData
.meHash1
, maProtectionData
.meHash2
);
201 pProtect
->setOption(ScTableProtection::SELECT_LOCKED_CELLS
, maProtectionData
.mbSelectProtectedCells
);
202 pProtect
->setOption(ScTableProtection::SELECT_UNLOCKED_CELLS
, maProtectionData
.mbSelectUnprotectedCells
);
203 pProtect
->setOption(ScTableProtection::INSERT_COLUMNS
, maProtectionData
.mbInsertColumns
);
204 pProtect
->setOption(ScTableProtection::INSERT_ROWS
, maProtectionData
.mbInsertRows
);
205 pProtect
->setOption(ScTableProtection::DELETE_COLUMNS
, maProtectionData
.mbDeleteColumns
);
206 pProtect
->setOption(ScTableProtection::DELETE_ROWS
, maProtectionData
.mbDeleteRows
);
207 rImport
.GetDocument()->SetTabProtection(maCurrentCellPos
.Tab(), pProtect
.get());
211 void ScMyTables::AddColStyle(const sal_Int32 nRepeat
, const OUString
& rCellStyleName
)
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
);
219 uno::Reference
< drawing::XDrawPage
> const & ScMyTables::GetCurrentXDrawPage()
221 if( (maCurrentCellPos
.Tab() != nCurrentDrawPage
) || !xDrawPage
.is() )
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());
231 uno::Reference
< drawing::XShapes
> const & ScMyTables::GetCurrentXShapes()
233 if( (maCurrentCellPos
.Tab() != nCurrentXShapes
) || !xShapes
.is() )
235 xShapes
= GetCurrentXDrawPage();
236 rImport
.GetShapeImport()->startPage(xShapes
);
237 rImport
.GetShapeImport()->pushGroupForPostProcessing ( xShapes
);
238 nCurrentXShapes
= sal::static_int_cast
<sal_Int16
>(maCurrentCellPos
.Tab());
243 bool ScMyTables::HasDrawPage() const
245 return (maCurrentCellPos
.Tab() == nCurrentDrawPage
) && xDrawPage
.is();
248 bool ScMyTables::HasXShapes() const
250 return (maCurrentCellPos
.Tab() == nCurrentXShapes
) && xShapes
.is();
253 void ScMyTables::AddOLE(const uno::Reference
<drawing::XShape
>& rShape
,
254 const OUString
&rRangeList
)
256 aFixupOLEs
.AddOLE(rShape
, rRangeList
);
259 void ScMyTables::AddMatrixRange(
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
)
263 OSL_ENSURE(nEndRow
>= nStartRow
, "wrong row order");
264 OSL_ENSURE(nEndColumn
>= nStartColumn
, "wrong column order");
266 nStartColumn
, nStartRow
, maCurrentCellPos
.Tab(),
267 nEndColumn
, nEndRow
, maCurrentCellPos
.Tab()
270 maMatrixRangeList
.push_back(aScRange
);
272 ScDocumentImport
& rDoc
= rImport
.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() );
280 bool ScMyTables::IsPartOfMatrix(const ScAddress
& rScAddress
) const
282 if (!maMatrixRangeList
.empty())
283 return maMatrixRangeList
.In(rScAddress
);
287 SCCOL
ScMyTables::GetCurrentColCount() const
289 return std::min
<sal_Int32
>(nCurrentColCount
, rImport
.GetDocument()->MaxCol());
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */