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 .
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>
30 using namespace com::sun::star
;
32 XColorEntry::XColorEntry(const Color
& rColor
, const OUString
& rName
)
33 : XPropertyEntry(rName
),
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
),
56 XDashEntry::XDashEntry(const XDashEntry
& rOther
)
57 : XPropertyEntry(rOther
),
62 XHatchEntry::XHatchEntry(const XHatch
& rHatch
, const OUString
& rName
)
63 : XPropertyEntry(rName
),
68 XHatchEntry::XHatchEntry(const XHatchEntry
& rOther
)
69 : XPropertyEntry(rOther
),
74 XGradientEntry::XGradientEntry(const XGradient
& rGradient
, const OUString
& rName
)
75 : XPropertyEntry(rName
),
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
102 maName ( "standard" ),
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
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
135 if( !const_cast<XPropertyList
*>(this)->Load() )
136 const_cast<XPropertyList
*>(this)->Create();
138 if (!isValidIdx(nIndex
))
141 return maList
[nIndex
].get();
144 tools::Long
XPropertyList::GetIndex(std::u16string_view rName
) const
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()) {
160 BitmapEx
XPropertyList::GetUiBitmap( tools::Long nIndex
) const
163 if (!isValidIdx(nIndex
))
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
);
177 void XPropertyList::Insert(std::unique_ptr
<XPropertyEntry
> pEntry
, tools::Long nIndex
)
181 assert(!"empty XPropertyEntry not allowed in XPropertyList");
185 if (isValidIdx(nIndex
)) {
186 maList
.insert( maList
.begin()+nIndex
, std::move(pEntry
) );
188 maList
.push_back( std::move(pEntry
) );
192 void XPropertyList::Replace(std::unique_ptr
<XPropertyEntry
> pEntry
, tools::Long nIndex
)
196 assert(!"empty XPropertyEntry not allowed in XPropertyList");
199 if (!isValidIdx(nIndex
))
201 assert(!"trying to replace invalid entry in XPropertyList");
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");
216 maList
.erase(maList
.begin() + nIndex
);
219 void XPropertyList::SetName( const OUString
& rString
)
221 if(!rString
.isEmpty())
227 bool XPropertyList::Load()
232 std::stack
<OUString
> aDirs
;
234 sal_Int32 nIndex
= 0;
237 aDirs
.push(maPath
.getToken(0, ';', nIndex
));
241 //try all entries palette path list working back to front until one
243 while (!aDirs
.empty())
245 OUString
aPath(aDirs
.top());
248 INetURLObject
aURL(aPath
);
250 if( INetProtocol::NotValid
== aURL
.GetProtocol() )
252 DBG_ASSERT( aPath
.isEmpty(), "invalid URL" );
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 );
271 bool XPropertyList::LoadFrom( const uno::Reference
< embed::XStorage
> &xStorage
,
272 const OUString
&rURL
, const OUString
&rReferer
)
277 return SvxXMLXTableImport::load( rURL
, rReferer
, xStorage
, createInstance(), &mbEmbedInDocument
);
280 bool XPropertyList::Save()
282 //save to the last path in the palette path list
284 sal_Int32 nIndex
= 0;
287 aLastDir
= maPath
.getToken(0, ';', nIndex
);
291 INetURLObject
aURL(aLastDir
);
293 if( INetProtocol::NotValid
== aURL
.GetProtocol() )
295 DBG_ASSERT( aLastDir
.isEmpty(), "invalid URL" );
299 aURL
.Append( maName
);
301 if( aURL
.getExtension().isEmpty() )
302 aURL
.setExtension( GetDefaultExt() );
304 return SvxXMLXTableExportComponent::save( aURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
),
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
;
322 case XPropertyListType::Color
:
323 pRet
= XPropertyListRef(new XColorList(rPath
, rReferer
));
325 case XPropertyListType::LineEnd
:
326 pRet
= XPropertyListRef(new XLineEndList(rPath
, rReferer
));
328 case XPropertyListType::Dash
:
329 pRet
= XPropertyListRef(new XDashList(rPath
, rReferer
));
331 case XPropertyListType::Hatch
:
332 pRet
= XPropertyListRef(new XHatchList(rPath
, rReferer
));
334 case XPropertyListType::Gradient
:
335 pRet
= XPropertyListRef(new XGradientList(rPath
, rReferer
));
337 case XPropertyListType::Bitmap
:
338 pRet
= XPropertyListRef(new XBitmapList(rPath
, rReferer
));
340 case XPropertyListType::Pattern
:
341 pRet
= XPropertyListRef(new XPatternList(rPath
, rReferer
));
344 OSL_FAIL("unknown xproperty type");
347 OSL_ASSERT( !pRet
.is() || pRet
->meType
== aType
);
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() );
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
)
387 return OUString::createFromAscii( i
.pExt
);
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */