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 .
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>
31 using namespace com::sun::star
;
33 XColorEntry::XColorEntry(const Color
& rColor
, const OUString
& rName
)
34 : XPropertyEntry(rName
),
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
),
57 XDashEntry::XDashEntry(const XDashEntry
& rOther
)
58 : XPropertyEntry(rOther
),
63 XHatchEntry::XHatchEntry(const XHatch
& rHatch
, const OUString
& rName
)
64 : XPropertyEntry(rName
),
69 XHatchEntry::XHatchEntry(const XHatchEntry
& rOther
)
70 : XPropertyEntry(rOther
),
75 XGradientEntry::XGradientEntry(const basegfx::BGradient
& rGradient
, const OUString
& rName
)
76 : XPropertyEntry(rName
),
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
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
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
136 if( !const_cast<XPropertyList
*>(this)->Load() )
137 const_cast<XPropertyList
*>(this)->Create();
139 if (!isValidIdx(nIndex
))
142 return maList
[nIndex
].get();
145 tools::Long
XPropertyList::GetIndex(std::u16string_view rName
) const
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()) {
161 BitmapEx
XPropertyList::GetUiBitmap( tools::Long nIndex
) const
164 if (!isValidIdx(nIndex
))
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
);
178 void XPropertyList::Insert(std::unique_ptr
<XPropertyEntry
> pEntry
, tools::Long nIndex
)
182 assert(!"empty XPropertyEntry not allowed in XPropertyList");
186 if (isValidIdx(nIndex
)) {
187 maList
.insert( maList
.begin()+nIndex
, std::move(pEntry
) );
189 maList
.push_back( std::move(pEntry
) );
193 void XPropertyList::Replace(std::unique_ptr
<XPropertyEntry
> pEntry
, tools::Long nIndex
)
197 assert(!"empty XPropertyEntry not allowed in XPropertyList");
200 if (!isValidIdx(nIndex
))
202 assert(!"trying to replace invalid entry in XPropertyList");
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");
217 maList
.erase(maList
.begin() + nIndex
);
220 void XPropertyList::SetName( const OUString
& rString
)
222 if(!rString
.isEmpty())
228 bool XPropertyList::Load()
233 std::stack
<OUString
> aDirs
;
235 sal_Int32 nIndex
= 0;
238 aDirs
.push(maPath
.getToken(0, ';', nIndex
));
242 //try all entries palette path list working back to front until one
244 while (!aDirs
.empty())
246 OUString
aPath(aDirs
.top());
249 INetURLObject
aURL(aPath
);
251 if( INetProtocol::NotValid
== aURL
.GetProtocol() )
253 DBG_ASSERT( aPath
.isEmpty(), "invalid URL" );
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 );
272 bool XPropertyList::LoadFrom( const uno::Reference
< embed::XStorage
> &xStorage
,
273 const OUString
&rURL
, const OUString
&rReferer
)
278 return SvxXMLXTableImport::load( rURL
, rReferer
, xStorage
, createInstance(), &mbEmbedInDocument
);
281 bool XPropertyList::Save()
283 //save to the last path in the palette path list
285 sal_Int32 nIndex
= 0;
288 aLastDir
= maPath
.getToken(0, ';', nIndex
);
292 INetURLObject
aURL(aLastDir
);
294 if( INetProtocol::NotValid
== aURL
.GetProtocol() )
296 DBG_ASSERT( aLastDir
.isEmpty(), "invalid URL" );
300 aURL
.Append( maName
);
302 if( aURL
.getExtension().isEmpty() )
303 aURL
.setExtension( GetDefaultExt() );
305 return SvxXMLXTableExportComponent::save( aURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
),
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
;
323 case XPropertyListType::Color
:
324 pRet
= XPropertyListRef(new XColorList(rPath
, rReferer
));
326 case XPropertyListType::LineEnd
:
327 pRet
= XPropertyListRef(new XLineEndList(rPath
, rReferer
));
329 case XPropertyListType::Dash
:
330 pRet
= XPropertyListRef(new XDashList(rPath
, rReferer
));
332 case XPropertyListType::Hatch
:
333 pRet
= XPropertyListRef(new XHatchList(rPath
, rReferer
));
335 case XPropertyListType::Gradient
:
336 pRet
= XPropertyListRef(new XGradientList(rPath
, rReferer
));
338 case XPropertyListType::Bitmap
:
339 pRet
= XPropertyListRef(new XBitmapList(rPath
, rReferer
));
341 case XPropertyListType::Pattern
:
342 pRet
= XPropertyListRef(new XPatternList(rPath
, rReferer
));
345 OSL_FAIL("unknown xproperty type");
348 OSL_ASSERT( !pRet
.is() || pRet
->meType
== aType
);
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() );
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
)
388 return OUString::createFromAscii( i
.pExt
);
393 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */