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>
41 using namespace com::sun::star
;
43 ScXMLTabProtectionData::ScXMLTabProtectionData() :
44 meHash1(PASSHASH_SHA1
),
45 meHash2(PASSHASH_UNSPECIFIED
),
47 mbSelectProtectedCells(true),
48 mbSelectUnprotectedCells(true),
49 mbInsertColumns(false),
51 mbDeleteColumns(false),
53 mbUseAutoFilter(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())
101 nCurrentColCount
= 0;
102 sCurrentSheetName
= sTableName
;
103 //reset cols and rows for new sheet, but increment tab
104 maCurrentCellPos
.SetCol(-1);
105 maCurrentCellPos
.SetRow(-1);
106 maCurrentCellPos
.SetTab(maCurrentCellPos
.Tab() + 1);
108 maProtectionData
= rProtectData
;
109 ScDocument
*pDoc
= ScXMLConverter::GetScDocument(rImport
.GetModel());
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
);
116 pDoc
->SetTabNameOnLoad(maCurrentCellPos
.Tab(), sTableName
);
118 xCurrentSheet
= getCurrentSheet(rImport
.GetModel(), maCurrentCellPos
.Tab());
119 if (xCurrentSheet
.is())
121 // We need to set the current cell range here regardless of
122 // presence of style name.
123 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() )
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.
142 if ( !xCurrentSheet
.is() )
145 uno::Reference
<beans::XPropertySet
> xProperties(xCurrentSheet
, uno::UNO_QUERY
);
146 if ( !xProperties
.is() )
149 XMLTableStylesContext
*pStyles
= static_cast<XMLTableStylesContext
*>(rImport
.GetAutoStyles());
152 XMLTableStyleContext
* pStyle
= const_cast<XMLTableStyleContext
*>(static_cast<const XMLTableStyleContext
*>(pStyles
->FindStyleChildContext(
153 XmlStyleFamily::TABLE_TABLE
, sStyleName
, true)));
156 pStyle
->FillPropertySet(xProperties
);
158 ScSheetSaveData
* pSheetData
= rImport
.GetScModel()->GetSheetSaveData();
159 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::AddColumns(sal_Int32 nRepeat
)
186 maCurrentCellPos
.SetCol( maCurrentCellPos
.Col() + nRepeat
);
189 void ScMyTables::DeleteTable()
191 ScXMLImport::MutexGuard
aGuard(rImport
);
193 rImport
.GetStylesImportHelper()->SetStylesToRanges();
194 rImport
.SetStylesToRangesFinished();
196 maMatrixRangeList
.RemoveAll();
198 if (!(rImport
.GetDocument() && maProtectionData
.mbProtected
))
201 uno::Sequence
<sal_Int8
> aHash
;
202 ::comphelper::Base64::decode(aHash
, maProtectionData
.maPassword
);
204 ScTableProtection aProtect
;
205 aProtect
.setProtected(maProtectionData
.mbProtected
);
206 aProtect
.setPasswordHash(aHash
, maProtectionData
.meHash1
, maProtectionData
.meHash2
);
207 aProtect
.setOption(ScTableProtection::SELECT_LOCKED_CELLS
, maProtectionData
.mbSelectProtectedCells
);
208 aProtect
.setOption(ScTableProtection::SELECT_UNLOCKED_CELLS
, maProtectionData
.mbSelectUnprotectedCells
);
209 aProtect
.setOption(ScTableProtection::INSERT_COLUMNS
, maProtectionData
.mbInsertColumns
);
210 aProtect
.setOption(ScTableProtection::INSERT_ROWS
, maProtectionData
.mbInsertRows
);
211 aProtect
.setOption(ScTableProtection::DELETE_COLUMNS
, maProtectionData
.mbDeleteColumns
);
212 aProtect
.setOption(ScTableProtection::DELETE_ROWS
, maProtectionData
.mbDeleteRows
);
213 aProtect
.setOption(ScTableProtection::AUTOFILTER
, maProtectionData
.mbUseAutoFilter
);
214 aProtect
.setOption(ScTableProtection::PIVOT_TABLES
, maProtectionData
.mbUsePivot
);
215 rImport
.GetDocument()->SetTabProtection(maCurrentCellPos
.Tab(), &aProtect
);
218 void ScMyTables::AddColStyle(const sal_Int32 nRepeat
, const OUString
& rCellStyleName
)
220 rImport
.GetStylesImportHelper()->AddColumnStyle(rCellStyleName
, nCurrentColCount
, nRepeat
);
221 nCurrentColCount
+= nRepeat
;
222 if (ScDocument
* pDoc
= rImport
.GetDocument())
224 SAL_WARN_IF(nCurrentColCount
> pDoc
->GetSheetLimits().GetMaxColCount(),
225 "sc", "more columns than fit into SCCOL");
226 nCurrentColCount
= std::min
<sal_Int32
>( nCurrentColCount
, pDoc
->GetSheetLimits().GetMaxColCount() );
230 uno::Reference
< drawing::XDrawPage
> const & ScMyTables::GetCurrentXDrawPage()
232 if( (maCurrentCellPos
.Tab() != nCurrentDrawPage
) || !xDrawPage
.is() )
234 uno::Reference
<drawing::XDrawPageSupplier
> xDrawPageSupplier( xCurrentSheet
, uno::UNO_QUERY
);
235 if( xDrawPageSupplier
.is() )
236 xDrawPage
.set(xDrawPageSupplier
->getDrawPage());
237 nCurrentDrawPage
= sal::static_int_cast
<sal_Int16
>(maCurrentCellPos
.Tab());
242 uno::Reference
< drawing::XShapes
> const & ScMyTables::GetCurrentXShapes()
244 if( (maCurrentCellPos
.Tab() != nCurrentXShapes
) || !xShapes
.is() )
246 xShapes
= GetCurrentXDrawPage();
247 rImport
.GetShapeImport()->startPage(xShapes
);
248 rImport
.GetShapeImport()->pushGroupForPostProcessing ( xShapes
);
249 nCurrentXShapes
= sal::static_int_cast
<sal_Int16
>(maCurrentCellPos
.Tab());
254 bool ScMyTables::HasDrawPage() const
256 return (maCurrentCellPos
.Tab() == nCurrentDrawPage
) && xDrawPage
.is();
259 bool ScMyTables::HasXShapes() const
261 return (maCurrentCellPos
.Tab() == nCurrentXShapes
) && xShapes
.is();
264 void ScMyTables::AddOLE(const uno::Reference
<drawing::XShape
>& rShape
,
265 const OUString
&rRangeList
)
267 aFixupOLEs
.AddOLE(rShape
, rRangeList
);
270 void ScMyTables::AddMatrixRange(
271 const SCCOL nStartColumn
, const SCROW nStartRow
, const SCCOL nEndColumn
, const SCROW nEndRow
,
272 const OUString
& rFormula
, const OUString
& rFormulaNmsp
, const formula::FormulaGrammar::Grammar eGrammar
)
274 OSL_ENSURE(nEndRow
>= nStartRow
, "wrong row order");
275 OSL_ENSURE(nEndColumn
>= nStartColumn
, "wrong column order");
277 nStartColumn
, nStartRow
, maCurrentCellPos
.Tab(),
278 nEndColumn
, nEndRow
, maCurrentCellPos
.Tab()
281 maMatrixRangeList
.push_back(aScRange
);
283 ScDocumentImport
& rDoc
= rImport
.GetDoc();
284 ScTokenArray
aCode(rDoc
.getDoc());
285 aCode
.AssignXMLString( rFormula
,
286 ((eGrammar
== formula::FormulaGrammar::GRAM_EXTERNAL
) ? rFormulaNmsp
: OUString()));
287 rDoc
.setMatrixCells(aScRange
, aCode
, eGrammar
);
288 rDoc
.getDoc().IncXMLImportedFormulaCount( rFormula
.getLength() );
291 bool ScMyTables::IsPartOfMatrix(const ScAddress
& rScAddress
) const
293 if (!maMatrixRangeList
.empty())
294 return maMatrixRangeList
.Contains(ScRange(rScAddress
));
298 SCCOL
ScMyTables::GetCurrentColCount() const
300 ScDocument
* pDoc
= rImport
.GetDocument();
302 return nCurrentColCount
;
303 return std::min
<sal_Int32
>(nCurrentColCount
, pDoc
->MaxCol());
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */