Line data Source code
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 "global.hxx"
22 : #include "xmlstyli.hxx"
23 : #include "xmlimprt.hxx"
24 : #include "document.hxx"
25 : #include "markdata.hxx"
26 : #include "XMLConverter.hxx"
27 : #include "docuno.hxx"
28 : #include "cellsuno.hxx"
29 : #include "XMLStylesImportHelper.hxx"
30 : #include "sheetdata.hxx"
31 : #include "tabprotection.hxx"
32 : #include "tokenarray.hxx"
33 : #include "convuno.hxx"
34 : #include <svx/svdpage.hxx>
35 :
36 : #include <sax/tools/converter.hxx>
37 : #include <xmloff/xmltkmap.hxx>
38 : #include <xmloff/nmspmap.hxx>
39 : #include <xmloff/xmlerror.hxx>
40 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
41 : #include <com/sun/star/sheet/XSheetCellRange.hpp>
42 : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
43 : #include <com/sun/star/sheet/CellInsertMode.hpp>
44 : #include <com/sun/star/sheet/XCellRangeMovement.hpp>
45 : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
46 : #include <com/sun/star/container/XNamed.hpp>
47 : #include <com/sun/star/util/XProtectable.hpp>
48 : #include <com/sun/star/sheet/XArrayFormulaRange.hpp>
49 :
50 : #include <memory>
51 :
52 : using ::std::auto_ptr;
53 : using namespace com::sun::star;
54 :
55 :
56 470 : ScXMLTabProtectionData::ScXMLTabProtectionData() :
57 : meHash1(PASSHASH_SHA1),
58 : meHash2(PASSHASH_UNSPECIFIED),
59 : mbProtected(false),
60 : mbSelectProtectedCells(true),
61 470 : mbSelectUnprotectedCells(true)
62 : {
63 470 : }
64 :
65 302 : ScMyTables::ScMyTables(ScXMLImport& rTempImport)
66 : : rImport(rTempImport),
67 : aFixupOLEs(rTempImport),
68 : maCurrentCellPos(ScAddress::INITIALIZE_INVALID),
69 : nCurrentColCount(0),
70 : nCurrentDrawPage( -1 ),
71 302 : nCurrentXShapes( -1 )
72 : {
73 302 : }
74 :
75 302 : ScMyTables::~ScMyTables()
76 : {
77 302 : }
78 :
79 : namespace {
80 :
81 168 : uno::Reference<sheet::XSpreadsheet> getCurrentSheet(const uno::Reference<frame::XModel>& xModel, SCTAB nSheet)
82 : {
83 168 : uno::Reference<sheet::XSpreadsheet> xSheet;
84 336 : uno::Reference<sheet::XSpreadsheetDocument> xSpreadDoc(xModel, uno::UNO_QUERY);
85 168 : if (!xSpreadDoc.is())
86 0 : return xSheet;
87 :
88 336 : uno::Reference <sheet::XSpreadsheets> xSheets(xSpreadDoc->getSheets());
89 168 : if (!xSheets.is())
90 0 : return xSheet;
91 :
92 336 : uno::Reference <container::XIndexAccess> xIndex(xSheets, uno::UNO_QUERY);
93 168 : if (!xIndex.is())
94 0 : return xSheet;
95 :
96 168 : xSheet.set(xIndex->getByIndex(nSheet), uno::UNO_QUERY);
97 168 : return xSheet;
98 : }
99 :
100 : }
101 :
102 168 : void ScMyTables::NewSheet(const OUString& sTableName, const OUString& sStyleName,
103 : const ScXMLTabProtectionData& rProtectData)
104 : {
105 168 : if (rImport.GetModel().is())
106 : {
107 168 : nCurrentColCount = 0;
108 168 : sCurrentSheetName = sTableName;
109 : //reset cols and rows for new sheet, but increment tab
110 168 : maCurrentCellPos.SetCol(-1);
111 168 : maCurrentCellPos.SetRow(-1);
112 168 : maCurrentCellPos.SetTab(maCurrentCellPos.Tab() + 1);
113 :
114 168 : maProtectionData = rProtectData;
115 168 : ScDocument *pDoc = ScXMLConverter::GetScDocument(rImport.GetModel());
116 :
117 : // The document contains one sheet when created. So for the first
118 : // sheet, we only need to set its name.
119 168 : if (maCurrentCellPos.Tab() > 0)
120 93 : pDoc->AppendTabOnLoad(sTableName);
121 : else
122 75 : pDoc->SetTabNameOnLoad(maCurrentCellPos.Tab(), sTableName);
123 :
124 168 : rImport.SetTableStyle(sStyleName);
125 168 : xCurrentSheet = getCurrentSheet(rImport.GetModel(), maCurrentCellPos.Tab());
126 168 : if (xCurrentSheet.is())
127 : {
128 : // We need to set the current cell range here regardless of
129 : // presence of style name.
130 168 : xCurrentCellRange.set(xCurrentSheet, uno::UNO_QUERY);
131 168 : SetTableStyle(sStyleName);
132 : }
133 : }
134 168 : }
135 :
136 168 : void ScMyTables::SetTableStyle(const OUString& sStyleName)
137 : {
138 : //these uno calls are a bit difficult to remove, XMLTableStyleContext::FillPropertySet uses
139 : //SvXMLImportPropertyMapper::FillPropertySet
140 168 : if ( !sStyleName.isEmpty() )
141 : {
142 : // #i57869# All table style properties for all sheets are now applied here,
143 : // before importing the contents.
144 : // This is needed for the background color.
145 : // Sheet visibility has special handling in ScDocFunc::SetTableVisible to
146 : // allow hiding the first sheet.
147 : // RTL layout is only remembered, not actually applied, so the shapes can
148 : // be loaded before mirroring.
149 :
150 168 : if ( xCurrentSheet.is() )
151 : {
152 168 : xCurrentCellRange.set(xCurrentSheet, uno::UNO_QUERY);
153 168 : uno::Reference <beans::XPropertySet> xProperties(xCurrentSheet, uno::UNO_QUERY);
154 168 : if ( xProperties.is() )
155 : {
156 168 : XMLTableStylesContext *pStyles = (XMLTableStylesContext *)rImport.GetAutoStyles();
157 168 : if ( pStyles )
158 : {
159 : XMLTableStyleContext* pStyle = (XMLTableStyleContext *)pStyles->FindStyleChildContext(
160 168 : XML_STYLE_FAMILY_TABLE_TABLE, sStyleName, true);
161 168 : if ( pStyle )
162 : {
163 168 : pStyle->FillPropertySet(xProperties);
164 :
165 168 : ScSheetSaveData* pSheetData = ScModelObj::getImplementation(rImport.GetModel())->GetSheetSaveData();
166 168 : pSheetData->AddTableStyle( sStyleName, ScAddress( 0, 0, maCurrentCellPos.Tab() ) );
167 : }
168 : }
169 168 : }
170 : }
171 : }
172 168 : }
173 :
174 19924177 : void ScMyTables::AddRow()
175 : {
176 19924177 : maCurrentCellPos.SetRow(maCurrentCellPos.Row() + 1);
177 19924177 : maCurrentCellPos.SetCol(-1); //reset columns for new row
178 19924177 : }
179 :
180 1994 : void ScMyTables::SetRowStyle(const OUString& rCellStyleName)
181 : {
182 1994 : rImport.GetStylesImportHelper()->SetRowStyle(rCellStyleName);
183 1994 : }
184 :
185 19952553 : void ScMyTables::AddColumn(bool bIsCovered)
186 : {
187 19952553 : maCurrentCellPos.SetCol( maCurrentCellPos.Col() + 1 );
188 : //here only need to set column style if this is the first row and
189 : //the cell is not covered.
190 19952553 : if(maCurrentCellPos.Row() == 0 && !bIsCovered)
191 1054 : rImport.GetStylesImportHelper()->InsertCol(maCurrentCellPos.Col(), maCurrentCellPos.Tab(), rImport.GetDocument());
192 19952553 : }
193 :
194 168 : void ScMyTables::DeleteTable()
195 : {
196 168 : ScXMLImport::MutexGuard aGuard(rImport);
197 :
198 168 : rImport.GetStylesImportHelper()->SetStylesToRanges();
199 168 : rImport.SetStylesToRangesFinished();
200 :
201 168 : maMatrixRangeList.RemoveAll();
202 :
203 168 : if (rImport.GetDocument() && maProtectionData.mbProtected)
204 : {
205 0 : uno::Sequence<sal_Int8> aHash;
206 0 : ::sax::Converter::decodeBase64(aHash, maProtectionData.maPassword);
207 :
208 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
209 0 : auto_ptr<ScTableProtection> pProtect(new ScTableProtection);
210 : SAL_WNODEPRECATED_DECLARATIONS_POP
211 0 : pProtect->setProtected(maProtectionData.mbProtected);
212 0 : pProtect->setPasswordHash(aHash, maProtectionData.meHash1, maProtectionData.meHash2);
213 0 : pProtect->setOption(ScTableProtection::SELECT_LOCKED_CELLS, maProtectionData.mbSelectProtectedCells);
214 0 : pProtect->setOption(ScTableProtection::SELECT_UNLOCKED_CELLS, maProtectionData.mbSelectUnprotectedCells);
215 0 : rImport.GetDocument()->SetTabProtection(maCurrentCellPos.Tab(), pProtect.get());
216 168 : }
217 168 : }
218 :
219 259 : void ScMyTables::AddColStyle(const sal_Int32 nRepeat, const OUString& rCellStyleName)
220 : {
221 259 : rImport.GetStylesImportHelper()->AddColumnStyle(rCellStyleName, nCurrentColCount, nRepeat);
222 259 : nCurrentColCount += nRepeat;
223 : SAL_WARN_IF(nCurrentColCount > MAXCOLCOUNT, "sc", "more columns than fit into SCCOL");
224 259 : nCurrentColCount = std::min<sal_Int32>( nCurrentColCount, MAXCOLCOUNT );
225 259 : }
226 :
227 51 : uno::Reference< drawing::XDrawPage > ScMyTables::GetCurrentXDrawPage()
228 : {
229 51 : if( (maCurrentCellPos.Tab() != nCurrentDrawPage) || !xDrawPage.is() )
230 : {
231 46 : uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier( xCurrentSheet, uno::UNO_QUERY );
232 46 : if( xDrawPageSupplier.is() )
233 46 : xDrawPage.set(xDrawPageSupplier->getDrawPage());
234 46 : nCurrentDrawPage = sal::static_int_cast<sal_Int16>(maCurrentCellPos.Tab());
235 : }
236 51 : return xDrawPage;
237 : }
238 :
239 209 : uno::Reference< drawing::XShapes > ScMyTables::GetCurrentXShapes()
240 : {
241 209 : if( (maCurrentCellPos.Tab() != nCurrentXShapes) || !xShapes.is() )
242 : {
243 33 : xShapes.set(GetCurrentXDrawPage(), uno::UNO_QUERY);
244 33 : rImport.GetShapeImport()->startPage(xShapes);
245 33 : rImport.GetShapeImport()->pushGroupForSorting ( xShapes );
246 33 : nCurrentXShapes = sal::static_int_cast<sal_Int16>(maCurrentCellPos.Tab());
247 33 : return xShapes;
248 : }
249 : else
250 176 : return xShapes;
251 : }
252 :
253 168 : bool ScMyTables::HasDrawPage()
254 : {
255 168 : return !((maCurrentCellPos.Tab() != nCurrentDrawPage) || !xDrawPage.is());
256 : }
257 :
258 46 : bool ScMyTables::HasXShapes()
259 : {
260 46 : return !((maCurrentCellPos.Tab() != nCurrentXShapes) || !xShapes.is());
261 : }
262 :
263 19 : void ScMyTables::AddOLE(uno::Reference <drawing::XShape>& rShape,
264 : const OUString &rRangeList)
265 : {
266 19 : aFixupOLEs.AddOLE(rShape, rRangeList);
267 19 : }
268 :
269 31 : void ScMyTables::AddMatrixRange(
270 : const SCCOL nStartColumn, const SCROW nStartRow, const SCCOL nEndColumn, const SCROW nEndRow,
271 : const OUString& rFormula, const OUString& rFormulaNmsp, const formula::FormulaGrammar::Grammar eGrammar)
272 : {
273 : OSL_ENSURE(nEndRow >= nStartRow, "wrong row order");
274 : OSL_ENSURE(nEndColumn >= nStartColumn, "wrong column order");
275 : ScRange aScRange(
276 31 : nStartColumn, nStartRow, maCurrentCellPos.Tab(),
277 31 : nEndColumn, nEndRow, maCurrentCellPos.Tab()
278 62 : );
279 :
280 31 : maMatrixRangeList.Append(aScRange);
281 :
282 31 : ScDocument* pDoc = rImport.GetDocument();
283 31 : ScMarkData aMark;
284 31 : aMark.SetMarkArea( aScRange );
285 31 : aMark.SelectTable( aScRange.aStart.Tab(), sal_True );
286 62 : boost::scoped_ptr<ScTokenArray> pCode(new ScTokenArray);
287 31 : pCode->AddStringXML( rFormula );
288 31 : if( (eGrammar == formula::FormulaGrammar::GRAM_EXTERNAL) && !rFormulaNmsp.isEmpty() )
289 0 : pCode->AddStringXML( rFormulaNmsp );
290 : pDoc->InsertMatrixFormula(
291 : nStartColumn, nStartRow,
292 : nEndColumn, nEndRow,
293 31 : aMark, EMPTY_OUSTRING, pCode.get(), eGrammar, false );
294 62 : pDoc->IncXMLImportedFormulaCount( rFormula.getLength() );
295 31 : }
296 :
297 2729 : bool ScMyTables::IsPartOfMatrix(const ScAddress& rScAddress) const
298 : {
299 2729 : if (!maMatrixRangeList.empty())
300 1121 : return maMatrixRangeList.In(rScAddress);
301 1608 : return false;
302 93 : }
303 :
304 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|