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