Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / xoutdev / xtable.cxx
blob9818fd1939f4093757cf94a568a118c9422fe93a
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 <utility>
22 #include <xmlxtexp.hxx>
23 #include <xmlxtimp.hxx>
24 #include <o3tl/safeint.hxx>
25 #include <osl/diagnose.h>
26 #include <tools/urlobj.hxx>
27 #include <svx/xtable.hxx>
28 #include <tools/debug.hxx>
29 #include <stack>
31 using namespace com::sun::star;
33 XColorEntry::XColorEntry(const Color& rColor, const OUString& rName)
34 : XPropertyEntry(rName),
35 aColor(rColor)
39 XLineEndEntry::XLineEndEntry(basegfx::B2DPolyPolygon _aB2DPolyPolygon, const OUString& rName)
40 : XPropertyEntry(rName),
41 aB2DPolyPolygon(std::move(_aB2DPolyPolygon))
45 XLineEndEntry::XLineEndEntry(const XLineEndEntry& rOther)
46 : XPropertyEntry(rOther),
47 aB2DPolyPolygon(rOther.aB2DPolyPolygon)
51 XDashEntry::XDashEntry(const XDash& rDash, const OUString& rName)
52 : XPropertyEntry(rName),
53 aDash(rDash)
57 XDashEntry::XDashEntry(const XDashEntry& rOther)
58 : XPropertyEntry(rOther),
59 aDash(rOther.aDash)
63 XHatchEntry::XHatchEntry(const XHatch& rHatch, const OUString& rName)
64 : XPropertyEntry(rName),
65 aHatch(rHatch)
69 XHatchEntry::XHatchEntry(const XHatchEntry& rOther)
70 : XPropertyEntry(rOther),
71 aHatch(rOther.aHatch)
75 XGradientEntry::XGradientEntry(const basegfx::BGradient& rGradient, const OUString& rName)
76 : XPropertyEntry(rName),
77 aGradient(rGradient)
81 XGradientEntry::XGradientEntry(const XGradientEntry& rOther)
82 : XPropertyEntry(rOther),
83 aGradient(rOther.aGradient)
87 XBitmapEntry::XBitmapEntry(const GraphicObject& rGraphicObject, const OUString& rName)
88 : XPropertyEntry(rName),
89 maGraphicObject(rGraphicObject)
93 XBitmapEntry::XBitmapEntry(const XBitmapEntry& rOther)
94 : XPropertyEntry(rOther),
95 maGraphicObject(rOther.maGraphicObject)
99 XPropertyList::XPropertyList(
100 XPropertyListType type,
101 OUString aPath, OUString aReferer
102 ) : meType ( type ),
103 maName ( "standard" ),
104 maPath (std::move( aPath )),
105 maReferer (std::move( aReferer )),
106 mbListDirty ( true ),
107 mbEmbedInDocument( false )
109 // fprintf (stderr, "Create type %d count %d\n", (int)meType, count++);
112 bool XPropertyList::isValidIdx(tools::Long nIndex) const
114 return (nIndex >= 0 && o3tl::make_unsigned(nIndex) < maList.size());
118 XPropertyList::~XPropertyList()
122 tools::Long XPropertyList::Count() const
124 if( mbListDirty )
126 if( !const_cast<XPropertyList*>(this)->Load() )
127 const_cast<XPropertyList*>(this)->Create();
129 return maList.size();
132 XPropertyEntry* XPropertyList::Get( tools::Long nIndex ) const
134 if( mbListDirty )
136 if( !const_cast<XPropertyList*>(this)->Load() )
137 const_cast<XPropertyList*>(this)->Create();
139 if (!isValidIdx(nIndex))
140 return nullptr;
142 return maList[nIndex].get();
145 tools::Long XPropertyList::GetIndex(std::u16string_view rName) const
147 if( mbListDirty )
149 if( !const_cast<XPropertyList*>(this)->Load() )
150 const_cast<XPropertyList*>(this)->Create();
153 for( tools::Long i = 0, n = maList.size(); i < n; ++i ) {
154 if (rName == maList[ i ]->GetName()) {
155 return i;
158 return -1;
161 BitmapEx XPropertyList::GetUiBitmap( tools::Long nIndex ) const
163 BitmapEx aRetval;
164 if (!isValidIdx(nIndex))
165 return aRetval;
167 XPropertyEntry* pEntry = maList[nIndex].get();
168 aRetval = pEntry->GetUiBitmap();
170 if(aRetval.IsEmpty())
172 aRetval = const_cast< XPropertyList* >(this)->CreateBitmapForUI(nIndex);
173 pEntry->SetUiBitmap(aRetval);
175 return aRetval;
178 void XPropertyList::Insert(std::unique_ptr<XPropertyEntry> pEntry, tools::Long nIndex)
180 if (!pEntry)
182 assert(!"empty XPropertyEntry not allowed in XPropertyList");
183 return;
186 if (isValidIdx(nIndex)) {
187 maList.insert( maList.begin()+nIndex, std::move(pEntry) );
188 } else {
189 maList.push_back( std::move(pEntry) );
193 void XPropertyList::Replace(std::unique_ptr<XPropertyEntry> pEntry, tools::Long nIndex)
195 if (!pEntry)
197 assert(!"empty XPropertyEntry not allowed in XPropertyList");
198 return;
200 if (!isValidIdx(nIndex))
202 assert(!"trying to replace invalid entry in XPropertyList");
203 return;
206 maList[nIndex] = std::move(pEntry);
209 void XPropertyList::Remove(tools::Long nIndex)
211 if (!isValidIdx(nIndex))
213 assert(!"trying to remove invalid entry in XPropertyList");
214 return;
217 maList.erase(maList.begin() + nIndex);
220 void XPropertyList::SetName( const OUString& rString )
222 if(!rString.isEmpty())
224 maName = rString;
228 bool XPropertyList::Load()
230 if( mbListDirty )
232 mbListDirty = false;
233 std::stack<OUString> aDirs;
235 sal_Int32 nIndex = 0;
238 aDirs.push(maPath.getToken(0, ';', nIndex));
240 while (nIndex >= 0);
242 //try all entries palette path list working back to front until one
243 //succeeds
244 while (!aDirs.empty())
246 OUString aPath(aDirs.top());
247 aDirs.pop();
249 INetURLObject aURL(aPath);
251 if( INetProtocol::NotValid == aURL.GetProtocol() )
253 DBG_ASSERT( aPath.isEmpty(), "invalid URL" );
254 return false;
257 aURL.Append( maName );
259 if( aURL.getExtension().isEmpty() )
260 aURL.setExtension( GetDefaultExt() );
262 bool bRet = SvxXMLXTableImport::load(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE),
263 maReferer, uno::Reference < embed::XStorage >(),
264 createInstance(), nullptr );
265 if (bRet)
266 return bRet;
269 return false;
272 bool XPropertyList::LoadFrom( const uno::Reference < embed::XStorage > &xStorage,
273 const OUString &rURL, const OUString &rReferer )
275 if( !mbListDirty )
276 return false;
277 mbListDirty = false;
278 return SvxXMLXTableImport::load( rURL, rReferer, xStorage, createInstance(), &mbEmbedInDocument );
281 bool XPropertyList::Save()
283 //save to the last path in the palette path list
284 OUString aLastDir;
285 sal_Int32 nIndex = 0;
288 aLastDir = maPath.getToken(0, ';', nIndex);
290 while (nIndex >= 0);
292 INetURLObject aURL(aLastDir);
294 if( INetProtocol::NotValid == aURL.GetProtocol() )
296 DBG_ASSERT( aLastDir.isEmpty(), "invalid URL" );
297 return false;
300 aURL.Append( maName );
302 if( aURL.getExtension().isEmpty() )
303 aURL.setExtension( GetDefaultExt() );
305 return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ),
306 createInstance(),
307 uno::Reference< embed::XStorage >(), nullptr );
310 bool XPropertyList::SaveTo( const uno::Reference< embed::XStorage > &xStorage,
311 const OUString &rURL, OUString *pOptName )
313 return SvxXMLXTableExportComponent::save( rURL, createInstance(), xStorage, pOptName );
316 XPropertyListRef XPropertyList::CreatePropertyList( XPropertyListType aType,
317 const OUString& rPath,
318 const OUString& rReferer )
320 XPropertyListRef pRet;
322 switch (aType) {
323 case XPropertyListType::Color:
324 pRet = XPropertyListRef(new XColorList(rPath, rReferer));
325 break;
326 case XPropertyListType::LineEnd:
327 pRet = XPropertyListRef(new XLineEndList(rPath, rReferer));
328 break;
329 case XPropertyListType::Dash:
330 pRet = XPropertyListRef(new XDashList(rPath, rReferer));
331 break;
332 case XPropertyListType::Hatch:
333 pRet = XPropertyListRef(new XHatchList(rPath, rReferer));
334 break;
335 case XPropertyListType::Gradient:
336 pRet = XPropertyListRef(new XGradientList(rPath, rReferer));
337 break;
338 case XPropertyListType::Bitmap:
339 pRet = XPropertyListRef(new XBitmapList(rPath, rReferer));
340 break;
341 case XPropertyListType::Pattern:
342 pRet = XPropertyListRef(new XPatternList(rPath, rReferer));
343 break;
344 default:
345 OSL_FAIL("unknown xproperty type");
346 break;
348 OSL_ASSERT( !pRet.is() || pRet->meType == aType );
350 return pRet;
353 XPropertyListRef
354 XPropertyList::CreatePropertyListFromURL( XPropertyListType t,
355 std::u16string_view rURLStr )
357 INetURLObject aURL( rURLStr );
358 INetURLObject aPathURL( aURL );
360 aPathURL.removeSegment();
361 aPathURL.removeFinalSlash();
363 XPropertyListRef pList = XPropertyList::CreatePropertyList(
364 t, aPathURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), "" );
365 pList->SetName( aURL.getName() );
367 return pList;
370 struct {
371 XPropertyListType t;
372 const char *pExt;
373 } const pExtnMap[] = {
374 { XPropertyListType::Color, "soc" },
375 { XPropertyListType::LineEnd, "soe" },
376 { XPropertyListType::Dash, "sod" },
377 { XPropertyListType::Hatch, "soh" },
378 { XPropertyListType::Gradient, "sog" },
379 { XPropertyListType::Bitmap, "sob" },
380 { XPropertyListType::Pattern, "sop"}
383 OUString XPropertyList::GetDefaultExt( XPropertyListType t )
385 for (const auto & i : pExtnMap)
387 if( i.t == t )
388 return OUString::createFromAscii( i.pExt );
390 return OUString();
393 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */