LibreOffice Module svx (master) 1
xtable.cxx
Go to the documentation of this file.
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 <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>
30
31using namespace com::sun::star;
32
33XColorEntry::XColorEntry(const Color& rColor, const OUString& rName)
34: XPropertyEntry(rName),
35 aColor(rColor)
36{
37}
38
39XLineEndEntry::XLineEndEntry(basegfx::B2DPolyPolygon _aB2DPolyPolygon, const OUString& rName)
40: XPropertyEntry(rName),
41 aB2DPolyPolygon(std::move(_aB2DPolyPolygon))
42{
43}
44
46: XPropertyEntry(rOther),
47 aB2DPolyPolygon(rOther.aB2DPolyPolygon)
48{
49}
50
51XDashEntry::XDashEntry(const XDash& rDash, const OUString& rName)
52: XPropertyEntry(rName),
53 aDash(rDash)
54{
55}
56
58: XPropertyEntry(rOther),
59aDash(rOther.aDash)
60{
61}
62
63XHatchEntry::XHatchEntry(const XHatch& rHatch, const OUString& rName)
64: XPropertyEntry(rName),
65 aHatch(rHatch)
66{
67}
68
70: XPropertyEntry(rOther),
71 aHatch(rOther.aHatch)
72{
73}
74
75XGradientEntry::XGradientEntry(const basegfx::BGradient& rGradient, const OUString& rName)
76: XPropertyEntry(rName),
77 aGradient(rGradient)
78{
79}
80
82: XPropertyEntry(rOther),
83 aGradient(rOther.aGradient)
84{
85}
86
87XBitmapEntry::XBitmapEntry(const GraphicObject& rGraphicObject, const OUString& rName)
88: XPropertyEntry(rName),
89 maGraphicObject(rGraphicObject)
90{
91}
92
94: XPropertyEntry(rOther),
95 maGraphicObject(rOther.maGraphicObject)
96{
97}
98
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 )
108{
109// fprintf (stderr, "Create type %d count %d\n", (int)meType, count++);
110}
111
113{
114 return (nIndex >= 0 && o3tl::make_unsigned(nIndex) < maList.size());
115}
116
117
119{
120}
121
123{
124 if( mbListDirty )
125 {
126 if( !const_cast<XPropertyList*>(this)->Load() )
127 const_cast<XPropertyList*>(this)->Create();
128 }
129 return maList.size();
130}
131
133{
134 if( mbListDirty )
135 {
136 if( !const_cast<XPropertyList*>(this)->Load() )
137 const_cast<XPropertyList*>(this)->Create();
138 }
139 if (!isValidIdx(nIndex))
140 return nullptr;
141
142 return maList[nIndex].get();
143}
144
145tools::Long XPropertyList::GetIndex(std::u16string_view rName) const
146{
147 if( mbListDirty )
148 {
149 if( !const_cast<XPropertyList*>(this)->Load() )
150 const_cast<XPropertyList*>(this)->Create();
151 }
152
153 for( tools::Long i = 0, n = maList.size(); i < n; ++i ) {
154 if (rName == maList[ i ]->GetName()) {
155 return i;
156 }
157 }
158 return -1;
159}
160
162{
163 BitmapEx aRetval;
164 if (!isValidIdx(nIndex))
165 return aRetval;
166
167 XPropertyEntry* pEntry = maList[nIndex].get();
168 aRetval = pEntry->GetUiBitmap();
169
170 if(aRetval.IsEmpty())
171 {
172 aRetval = const_cast< XPropertyList* >(this)->CreateBitmapForUI(nIndex);
173 pEntry->SetUiBitmap(aRetval);
174 }
175 return aRetval;
176}
177
178void XPropertyList::Insert(std::unique_ptr<XPropertyEntry> pEntry, tools::Long nIndex)
179{
180 if (!pEntry)
181 {
182 assert(!"empty XPropertyEntry not allowed in XPropertyList");
183 return;
184 }
185
186 if (isValidIdx(nIndex)) {
187 maList.insert( maList.begin()+nIndex, std::move(pEntry) );
188 } else {
189 maList.push_back( std::move(pEntry) );
190 }
191}
192
193void XPropertyList::Replace(std::unique_ptr<XPropertyEntry> pEntry, tools::Long nIndex)
194{
195 if (!pEntry)
196 {
197 assert(!"empty XPropertyEntry not allowed in XPropertyList");
198 return;
199 }
200 if (!isValidIdx(nIndex))
201 {
202 assert(!"trying to replace invalid entry in XPropertyList");
203 return;
204 }
205
206 maList[nIndex] = std::move(pEntry);
207}
208
210{
211 if (!isValidIdx(nIndex))
212 {
213 assert(!"trying to remove invalid entry in XPropertyList");
214 return;
215 }
216
217 maList.erase(maList.begin() + nIndex);
218}
219
220void XPropertyList::SetName( const OUString& rString )
221{
222 if(!rString.isEmpty())
223 {
224 maName = rString;
225 }
226}
227
229{
230 if( mbListDirty )
231 {
232 mbListDirty = false;
233 std::stack<OUString> aDirs;
234
235 sal_Int32 nIndex = 0;
236 do
237 {
238 aDirs.push(maPath.getToken(0, ';', nIndex));
239 }
240 while (nIndex >= 0);
241
242 //try all entries palette path list working back to front until one
243 //succeeds
244 while (!aDirs.empty())
245 {
246 OUString aPath(aDirs.top());
247 aDirs.pop();
248
249 INetURLObject aURL(aPath);
250
251 if( INetProtocol::NotValid == aURL.GetProtocol() )
252 {
253 DBG_ASSERT( aPath.isEmpty(), "invalid URL" );
254 return false;
255 }
256
257 aURL.Append( maName );
258
259 if( aURL.getExtension().isEmpty() )
260 aURL.setExtension( GetDefaultExt() );
261
263 maReferer, uno::Reference < embed::XStorage >(),
264 createInstance(), nullptr );
265 if (bRet)
266 return bRet;
267 }
268 }
269 return false;
270}
271
272bool XPropertyList::LoadFrom( const uno::Reference < embed::XStorage > &xStorage,
273 const OUString &rURL, const OUString &rReferer )
274{
275 if( !mbListDirty )
276 return false;
277 mbListDirty = false;
278 return SvxXMLXTableImport::load( rURL, rReferer, xStorage, createInstance(), &mbEmbedInDocument );
279}
280
282{
283 //save to the last path in the palette path list
284 OUString aLastDir;
285 sal_Int32 nIndex = 0;
286 do
287 {
288 aLastDir = maPath.getToken(0, ';', nIndex);
289 }
290 while (nIndex >= 0);
291
292 INetURLObject aURL(aLastDir);
293
294 if( INetProtocol::NotValid == aURL.GetProtocol() )
295 {
296 DBG_ASSERT( aLastDir.isEmpty(), "invalid URL" );
297 return false;
298 }
299
300 aURL.Append( maName );
301
302 if( aURL.getExtension().isEmpty() )
303 aURL.setExtension( GetDefaultExt() );
304
307 uno::Reference< embed::XStorage >(), nullptr );
308}
309
310bool XPropertyList::SaveTo( const uno::Reference< embed::XStorage > &xStorage,
311 const OUString &rURL, OUString *pOptName )
312{
313 return SvxXMLXTableExportComponent::save( rURL, createInstance(), xStorage, pOptName );
314}
315
317 const OUString& rPath,
318 const OUString& rReferer )
319{
320 XPropertyListRef pRet;
321
322 switch (aType) {
324 pRet = XPropertyListRef(new XColorList(rPath, rReferer));
325 break;
327 pRet = XPropertyListRef(new XLineEndList(rPath, rReferer));
328 break;
330 pRet = XPropertyListRef(new XDashList(rPath, rReferer));
331 break;
333 pRet = XPropertyListRef(new XHatchList(rPath, rReferer));
334 break;
336 pRet = XPropertyListRef(new XGradientList(rPath, rReferer));
337 break;
339 pRet = XPropertyListRef(new XBitmapList(rPath, rReferer));
340 break;
342 pRet = XPropertyListRef(new XPatternList(rPath, rReferer));
343 break;
344 default:
345 OSL_FAIL("unknown xproperty type");
346 break;
347 }
348 OSL_ASSERT( !pRet.is() || pRet->meType == aType );
349
350 return pRet;
351}
352
355 std::u16string_view rURLStr )
356{
357 INetURLObject aURL( rURLStr );
358 INetURLObject aPathURL( aURL );
359
360 aPathURL.removeSegment();
361 aPathURL.removeFinalSlash();
362
365 pList->SetName( aURL.getName() );
366
367 return pList;
368}
369
370struct {
372 const char *pExt;
373} const pExtnMap[] = {
374 { XPropertyListType::Color, "soc" },
376 { XPropertyListType::Dash, "sod" },
377 { XPropertyListType::Hatch, "soh" },
379 { XPropertyListType::Bitmap, "sob" },
382
384{
385 for (const auto & i : pExtnMap)
386 {
387 if( i.t == t )
388 return OUString::createFromAscii( i.pExt );
389 }
390 return OUString();
391}
392
393/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Any maPath
OUString maName
bool IsEmpty() const
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool removeFinalSlash()
bool removeSegment(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true)
static bool save(const OUString &rURL, const css::uno::Reference< css::container::XNameContainer > &xTable, const css::uno::Reference< css::embed::XStorage > &xStorage, OUString *pOptName)
Definition: xmlxtexp.cxx:198
static bool load(const OUString &rPath, const OUString &rReferer, const css::uno::Reference< css::embed::XStorage > &xStorage, const css::uno::Reference< css::container::XNameContainer > &xTable, bool *bOptLoadedFromStorage) noexcept
Definition: xmlxtimp.cxx:418
XBitmapEntry(const GraphicObject &rGraphicObject, const OUString &rName)
Definition: xtable.cxx:87
XColorEntry(const Color &rColor, const OUString &rName)
Definition: xtable.cxx:33
XDashEntry(const XDash &rDash, const OUString &rName)
Definition: xtable.cxx:51
Definition: xdash.hxx:32
XGradientEntry(const basegfx::BGradient &rGradient, const OUString &rName)
Definition: xtable.cxx:75
XHatchEntry(const XHatch &rHatch, const OUString &rName)
Definition: xtable.cxx:63
XLineEndEntry(basegfx::B2DPolyPolygon aB2DPolyPolygon, const OUString &rName)
Definition: xtable.cxx:39
void SetUiBitmap(const BitmapEx &rUiBitmap)
const BitmapEx & GetUiBitmap() const
virtual ~XPropertyList() override
Definition: xtable.cxx:118
bool LoadFrom(const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &rURL, const OUString &rReferer)
Definition: xtable.cxx:272
bool Save()
Definition: xtable.cxx:281
BitmapEx GetUiBitmap(tools::Long nIndex) const
Definition: xtable.cxx:161
static XPropertyListRef CreatePropertyList(XPropertyListType t, const OUString &rPath, const OUString &rReferer)
Definition: xtable.cxx:316
bool SaveTo(const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &rURL, OUString *pOptName)
Definition: xtable.cxx:310
bool isValidIdx(tools::Long nIndex) const
Definition: xtable.cxx:112
XPropertyList(XPropertyListType t, OUString aPath, OUString aReferer)
Definition: xtable.cxx:99
virtual css::uno::Reference< css::container::XNameContainer > createInstance()=0
bool mbListDirty
Definition: xtable.hxx:163
std::vector< std::unique_ptr< XPropertyEntry > > maList
Definition: xtable.hxx:161
tools::Long GetIndex(std::u16string_view rName) const
Definition: xtable.cxx:145
virtual BitmapEx CreateBitmapForUI(tools::Long nIndex)=0
bool Load()
Definition: xtable.cxx:228
OUString maName
Definition: xtable.hxx:157
OUString maReferer
Definition: xtable.hxx:159
tools::Long Count() const
Definition: xtable.cxx:122
OUString maPath
Definition: xtable.hxx:158
static XPropertyListRef CreatePropertyListFromURL(XPropertyListType t, std::u16string_view rUrl)
Definition: xtable.cxx:354
bool mbEmbedInDocument
Definition: xtable.hxx:164
virtual bool Create()=0
const OUString & GetName() const
Definition: xtable.hxx:186
void SetName(const OUString &rString)
Definition: xtable.cxx:220
void Insert(std::unique_ptr< XPropertyEntry > pEntry, tools::Long nIndex=std::numeric_limits< tools::Long >::max())
Definition: xtable.cxx:178
OUString GetDefaultExt() const
Definition: xtable.hxx:197
void Replace(std::unique_ptr< XPropertyEntry > pEntry, tools::Long nIndex)
Definition: xtable.cxx:193
void Remove(tools::Long nIndex)
Definition: xtable.cxx:209
XPropertyEntry * Get(tools::Long nIndex) const
Definition: xtable.cxx:132
#define DBG_ASSERT(sCon, aError)
URL aURL
sal_Int32 nIndex
sal_Int64 n
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
long Long
ResultType type
RedlineType meType
const char * pExt
Definition: xtable.cxx:372
struct @2 pExtnMap[]
XPropertyListType t
Definition: xtable.cxx:371
rtl::Reference< class XPropertyList > XPropertyListRef
Definition: xtable.hxx:143
XPropertyListType
Definition: xtable.hxx:131