bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / xoutdev / xtable.cxx
blob6a0744fe0a3d6584964a51f23794a94a1600ba3a
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 <memory>
21 #include <xmlxtexp.hxx>
22 #include <xmlxtimp.hxx>
23 #include <o3tl/safeint.hxx>
24 #include <osl/diagnose.h>
25 #include <tools/urlobj.hxx>
26 #include <svx/xtable.hxx>
27 #include <tools/debug.hxx>
28 #include <stack>
30 using namespace com::sun::star;
32 XColorEntry::XColorEntry(const Color& rColor, const OUString& rName)
33 : XPropertyEntry(rName),
34 aColor(rColor)
38 XLineEndEntry::XLineEndEntry(const basegfx::B2DPolyPolygon& rB2DPolyPolygon, const OUString& rName)
39 : XPropertyEntry(rName),
40 aB2DPolyPolygon(rB2DPolyPolygon)
44 XLineEndEntry::XLineEndEntry(const XLineEndEntry& rOther)
45 : XPropertyEntry(rOther),
46 aB2DPolyPolygon(rOther.aB2DPolyPolygon)
50 XDashEntry::XDashEntry(const XDash& rDash, const OUString& rName)
51 : XPropertyEntry(rName),
52 aDash(rDash)
56 XDashEntry::XDashEntry(const XDashEntry& rOther)
57 : XPropertyEntry(rOther),
58 aDash(rOther.aDash)
62 XHatchEntry::XHatchEntry(const XHatch& rHatch, const OUString& rName)
63 : XPropertyEntry(rName),
64 aHatch(rHatch)
68 XHatchEntry::XHatchEntry(const XHatchEntry& rOther)
69 : XPropertyEntry(rOther),
70 aHatch(rOther.aHatch)
74 XGradientEntry::XGradientEntry(const XGradient& rGradient, const OUString& rName)
75 : XPropertyEntry(rName),
76 aGradient(rGradient)
80 XGradientEntry::XGradientEntry(const XGradientEntry& rOther)
81 : XPropertyEntry(rOther),
82 aGradient(rOther.aGradient)
86 XBitmapEntry::XBitmapEntry(const GraphicObject& rGraphicObject, const OUString& rName)
87 : XPropertyEntry(rName),
88 maGraphicObject(rGraphicObject)
92 XBitmapEntry::XBitmapEntry(const XBitmapEntry& rOther)
93 : XPropertyEntry(rOther),
94 maGraphicObject(rOther.maGraphicObject)
98 XPropertyList::XPropertyList(
99 XPropertyListType type,
100 const OUString& rPath, const OUString& rReferer
101 ) : meType ( type ),
102 maName ( "standard" ),
103 maPath ( rPath ),
104 maReferer ( rReferer ),
105 mbListDirty ( true ),
106 mbEmbedInDocument( false )
108 // fprintf (stderr, "Create type %d count %d\n", (int)meType, count++);
111 bool XPropertyList::isValidIdx(tools::Long nIndex) const
113 return (nIndex >= 0 && o3tl::make_unsigned(nIndex) < maList.size());
117 XPropertyList::~XPropertyList()
121 tools::Long XPropertyList::Count() const
123 if( mbListDirty )
125 if( !const_cast<XPropertyList*>(this)->Load() )
126 const_cast<XPropertyList*>(this)->Create();
128 return maList.size();
131 XPropertyEntry* XPropertyList::Get( tools::Long nIndex ) const
133 if( mbListDirty )
135 if( !const_cast<XPropertyList*>(this)->Load() )
136 const_cast<XPropertyList*>(this)->Create();
138 if (!isValidIdx(nIndex))
139 return nullptr;
141 return maList[nIndex].get();
144 tools::Long XPropertyList::GetIndex(std::u16string_view rName) const
146 if( mbListDirty )
148 if( !const_cast<XPropertyList*>(this)->Load() )
149 const_cast<XPropertyList*>(this)->Create();
152 for( tools::Long i = 0, n = maList.size(); i < n; ++i ) {
153 if (rName == maList[ i ]->GetName()) {
154 return i;
157 return -1;
160 BitmapEx XPropertyList::GetUiBitmap( tools::Long nIndex ) const
162 BitmapEx aRetval;
163 if (!isValidIdx(nIndex))
164 return aRetval;
166 XPropertyEntry* pEntry = maList[nIndex].get();
167 aRetval = pEntry->GetUiBitmap();
169 if(aRetval.IsEmpty())
171 aRetval = const_cast< XPropertyList* >(this)->CreateBitmapForUI(nIndex);
172 pEntry->SetUiBitmap(aRetval);
174 return aRetval;
177 void XPropertyList::Insert(std::unique_ptr<XPropertyEntry> pEntry, tools::Long nIndex)
179 if (!pEntry)
181 assert(!"empty XPropertyEntry not allowed in XPropertyList");
182 return;
185 if (isValidIdx(nIndex)) {
186 maList.insert( maList.begin()+nIndex, std::move(pEntry) );
187 } else {
188 maList.push_back( std::move(pEntry) );
192 void XPropertyList::Replace(std::unique_ptr<XPropertyEntry> pEntry, tools::Long nIndex)
194 if (!pEntry)
196 assert(!"empty XPropertyEntry not allowed in XPropertyList");
197 return;
199 if (!isValidIdx(nIndex))
201 assert(!"trying to replace invalid entry in XPropertyList");
202 return;
205 maList[nIndex] = std::move(pEntry);
208 void XPropertyList::Remove(tools::Long nIndex)
210 if (!isValidIdx(nIndex))
212 assert(!"trying to remove invalid entry in XPropertyList");
213 return;
216 maList.erase(maList.begin() + nIndex);
219 void XPropertyList::SetName( const OUString& rString )
221 if(!rString.isEmpty())
223 maName = rString;
227 bool XPropertyList::Load()
229 if( mbListDirty )
231 mbListDirty = false;
232 std::stack<OUString> aDirs;
234 sal_Int32 nIndex = 0;
237 aDirs.push(maPath.getToken(0, ';', nIndex));
239 while (nIndex >= 0);
241 //try all entries palette path list working back to front until one
242 //succeeds
243 while (!aDirs.empty())
245 OUString aPath(aDirs.top());
246 aDirs.pop();
248 INetURLObject aURL(aPath);
250 if( INetProtocol::NotValid == aURL.GetProtocol() )
252 DBG_ASSERT( aPath.isEmpty(), "invalid URL" );
253 return false;
256 aURL.Append( maName );
258 if( aURL.getExtension().isEmpty() )
259 aURL.setExtension( GetDefaultExt() );
261 bool bRet = SvxXMLXTableImport::load(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE),
262 maReferer, uno::Reference < embed::XStorage >(),
263 createInstance(), nullptr );
264 if (bRet)
265 return bRet;
268 return false;
271 bool XPropertyList::LoadFrom( const uno::Reference < embed::XStorage > &xStorage,
272 const OUString &rURL, const OUString &rReferer )
274 if( !mbListDirty )
275 return false;
276 mbListDirty = false;
277 return SvxXMLXTableImport::load( rURL, rReferer, xStorage, createInstance(), &mbEmbedInDocument );
280 bool XPropertyList::Save()
282 //save to the last path in the palette path list
283 OUString aLastDir;
284 sal_Int32 nIndex = 0;
287 aLastDir = maPath.getToken(0, ';', nIndex);
289 while (nIndex >= 0);
291 INetURLObject aURL(aLastDir);
293 if( INetProtocol::NotValid == aURL.GetProtocol() )
295 DBG_ASSERT( aLastDir.isEmpty(), "invalid URL" );
296 return false;
299 aURL.Append( maName );
301 if( aURL.getExtension().isEmpty() )
302 aURL.setExtension( GetDefaultExt() );
304 return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ),
305 createInstance(),
306 uno::Reference< embed::XStorage >(), nullptr );
309 bool XPropertyList::SaveTo( const uno::Reference< embed::XStorage > &xStorage,
310 const OUString &rURL, OUString *pOptName )
312 return SvxXMLXTableExportComponent::save( rURL, createInstance(), xStorage, pOptName );
315 XPropertyListRef XPropertyList::CreatePropertyList( XPropertyListType aType,
316 const OUString& rPath,
317 const OUString& rReferer )
319 XPropertyListRef pRet;
321 switch (aType) {
322 case XPropertyListType::Color:
323 pRet = XPropertyListRef(new XColorList(rPath, rReferer));
324 break;
325 case XPropertyListType::LineEnd:
326 pRet = XPropertyListRef(new XLineEndList(rPath, rReferer));
327 break;
328 case XPropertyListType::Dash:
329 pRet = XPropertyListRef(new XDashList(rPath, rReferer));
330 break;
331 case XPropertyListType::Hatch:
332 pRet = XPropertyListRef(new XHatchList(rPath, rReferer));
333 break;
334 case XPropertyListType::Gradient:
335 pRet = XPropertyListRef(new XGradientList(rPath, rReferer));
336 break;
337 case XPropertyListType::Bitmap:
338 pRet = XPropertyListRef(new XBitmapList(rPath, rReferer));
339 break;
340 case XPropertyListType::Pattern:
341 pRet = XPropertyListRef(new XPatternList(rPath, rReferer));
342 break;
343 default:
344 OSL_FAIL("unknown xproperty type");
345 break;
347 OSL_ASSERT( !pRet.is() || pRet->meType == aType );
349 return pRet;
352 XPropertyListRef
353 XPropertyList::CreatePropertyListFromURL( XPropertyListType t,
354 const OUString & rURLStr )
356 INetURLObject aURL( rURLStr );
357 INetURLObject aPathURL( aURL );
359 aPathURL.removeSegment();
360 aPathURL.removeFinalSlash();
362 XPropertyListRef pList = XPropertyList::CreatePropertyList(
363 t, aPathURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), "" );
364 pList->SetName( aURL.getName() );
366 return pList;
369 struct {
370 XPropertyListType t;
371 const char *pExt;
372 } const pExtnMap[] = {
373 { XPropertyListType::Color, "soc" },
374 { XPropertyListType::LineEnd, "soe" },
375 { XPropertyListType::Dash, "sod" },
376 { XPropertyListType::Hatch, "soh" },
377 { XPropertyListType::Gradient, "sog" },
378 { XPropertyListType::Bitmap, "sob" },
379 { XPropertyListType::Pattern, "sop"}
382 OUString XPropertyList::GetDefaultExt( XPropertyListType t )
384 for (const auto & i : pExtnMap)
386 if( i.t == t )
387 return OUString::createFromAscii( i.pExt );
389 return OUString();
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */