LibreOffice Module sw (master)  1
labelcfg.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 <config_folders.h>
22 
23 #include <swtypes.hxx>
24 #include <labelcfg.hxx>
25 #include <rtl/bootstrap.hxx>
26 #include <tools/UnitConversion.hxx>
27 #include <unotools/configpaths.hxx>
28 #include <xmlreader/xmlreader.hxx>
29 #include <comphelper/sequence.hxx>
30 #include <osl/diagnose.h>
31 
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 
34 using namespace utl;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::beans;
37 
39 {
40  int nsId;
43  res = reader.nextItem(xmlreader::XmlReader::Text::NONE, &name, &nsId);
44  assert(res == xmlreader::XmlReader::Result::End);
45  (void) res;
46 }
47 
48 static OUString lcl_getValue(xmlreader::XmlReader& reader,
49  const xmlreader::Span& span)
50 {
51  int nsId;
54  res = reader.nextItem(xmlreader::XmlReader::Text::NONE, &name, &nsId);
55  assert(res == xmlreader::XmlReader::Result::Begin && name == span);
56  res = reader.nextItem(xmlreader::XmlReader::Text::Raw, &name, &nsId);
58  (void) res; (void) span;
59  OUString sTmp = name.convertFromUtf8();
60  lcl_assertEndingItem(reader);
61  return sTmp;
62 }
63 
64 static Sequence<OUString> lcl_CreatePropertyNames(const OUString& rPrefix)
65 {
66  return { OUString::Concat(rPrefix) + "Name", OUString::Concat(rPrefix) + "Measure" };
67 }
68 
70  ConfigItem("Office.Labels/Manufacturer")
71 {
72  OUString uri("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/labels/labels.xml");
73  rtl::Bootstrap::expandMacros(uri);
74  xmlreader::XmlReader reader(uri);
75  int nsId;
78  OUString sManufacturer;
79  OUString sName;
80  OUString sMeasure;
81 
82  // fill m_aLabels and m_aManufacturers with the predefined labels
83  res = reader.nextItem(
84  xmlreader::XmlReader::Text::NONE, &name, &nsId);
85  assert(
87  && name == "manufacturers");
88  res = reader.nextItem(
89  xmlreader::XmlReader::Text::NONE, &name, &nsId);
91  {
92  // Opening manufacturer
93  assert(
95  && name == "manufacturer");
96  // Get the name
97  (void)reader.nextAttribute(&nsId, &name);
98  assert(
100  && name == "name");
101  sManufacturer = reader.getAttributeValue(false).convertFromUtf8();
102 
103  for(;;) {
104  // Opening label or ending manufacturer
105  res = reader.nextItem(
106  xmlreader::XmlReader::Text::NONE, &name, &nsId);
108  break;
109  assert(
111  && name == "label");
112  // Get name value
113  sName = lcl_getValue(reader, xmlreader::Span("name"));
114  // Get measure value
115  sMeasure = lcl_getValue(reader, xmlreader::Span("measure"));
116  // Ending label mark
117  lcl_assertEndingItem(reader);
118  if ( m_aLabels.find( sManufacturer ) == m_aLabels.end() )
119  m_aManufacturers.push_back( sManufacturer );
120  m_aLabels[sManufacturer][sName].m_aMeasure = sMeasure;
121  m_aLabels[sManufacturer][sName].m_bPredefined = true;
122  }
123  // Get next manufacturer or end
124  res = reader.nextItem(
125  xmlreader::XmlReader::Text::NONE, &name, &nsId);
126  };
127  res = reader.nextItem(
128  xmlreader::XmlReader::Text::NONE, &name, &nsId);
129  assert(res == xmlreader::XmlReader::Result::Done);
130 
131  // add to m_aLabels and m_aManufacturers the custom labels
132  const Sequence<OUString>& rMan = GetNodeNames( OUString() );
133  for ( const OUString& rManufacturer : rMan )
134  {
135  const Sequence<OUString> aLabels = GetNodeNames( rManufacturer );
136  for( const OUString& rLabel : aLabels )
137  {
138  OUString sPrefix = rManufacturer + "/" + rLabel + "/";
140  Sequence<Any> aValues = GetProperties( aPropNames );
141  const Any* pValues = aValues.getConstArray();
142  if (aValues.getLength() >= 1)
143  if(pValues[0].hasValue())
144  pValues[0] >>= sName;
145  if (aValues.getLength() >= 2)
146  if(pValues[1].hasValue())
147  pValues[1] >>= sMeasure;
148  if ( m_aLabels.find( rManufacturer ) == m_aLabels.end() )
149  m_aManufacturers.push_back( rManufacturer );
150  m_aLabels[rManufacturer][sName].m_aMeasure = sMeasure;
151  m_aLabels[rManufacturer][sName].m_bPredefined = false;
152  }
153  }
154 }
155 
157 {
158 }
159 
160 // the config item is not writable ?:
162 
163 void SwLabelConfig::Notify( const css::uno::Sequence< OUString >& ) {}
164 
165 static std::unique_ptr<SwLabRec> lcl_CreateSwLabRec(const OUString& rType, const OUString& rMeasure, const OUString& rManufacturer)
166 {
167  std::unique_ptr<SwLabRec> pNewRec(new SwLabRec);
168  pNewRec->m_aMake = rManufacturer;
169  pNewRec->m_nPWidth = 0;
170  pNewRec->m_nPHeight = 0;
171  pNewRec->m_aType = rType;
172  //all values are contained as colon-separated 1/100 mm values
173  //except for the continuous flag ('C'/'S') and nCols, nRows (sal_Int32)
174  sal_Int32 nTok{0};
175  sal_Int32 nIdx{rMeasure.isEmpty() ? -1 : 0};
176  while (nIdx>=0)
177  {
178  const OUString sToken(rMeasure.getToken(0, ';', nIdx));
179  int nVal = sToken.toInt32();
180  switch(nTok++)
181  {
182  case 0 : pNewRec->m_bCont = sToken[0] == 'C'; break;
183  case 1 : pNewRec->m_nHDist = o3tl::toTwips(nVal, o3tl::Length::mm100); break;
184  case 2 : pNewRec->m_nVDist = o3tl::toTwips(nVal, o3tl::Length::mm100); break;
185  case 3 : pNewRec->m_nWidth = o3tl::toTwips(nVal, o3tl::Length::mm100); break;
186  case 4 : pNewRec->m_nHeight = o3tl::toTwips(nVal, o3tl::Length::mm100); break;
187  case 5 : pNewRec->m_nLeft = o3tl::toTwips(nVal, o3tl::Length::mm100); break;
188  case 6 : pNewRec->m_nUpper = o3tl::toTwips(nVal, o3tl::Length::mm100); break;
189  case 7 : pNewRec->m_nCols = nVal; break;
190  case 8 : pNewRec->m_nRows = nVal; break;
191  case 9 : pNewRec->m_nPWidth = o3tl::toTwips(nVal, o3tl::Length::mm100); break;
192  case 10 : pNewRec->m_nPHeight = o3tl::toTwips(nVal, o3tl::Length::mm100); break;
193  }
194  }
195  // lines added for compatibility with custom label definitions saved before patch fdo#44516
196  if (pNewRec->m_nPWidth == 0 || pNewRec->m_nPHeight == 0)
197  {
198  // old style definition (no paper dimensions), calculate probable values
199  pNewRec->m_nPWidth = 2 * pNewRec->m_nLeft + (pNewRec->m_nCols - 1) * pNewRec->m_nHDist + pNewRec->m_nWidth;
200  pNewRec->m_nPHeight = ( pNewRec->m_bCont ? pNewRec->m_nRows * pNewRec->m_nVDist : 2 * pNewRec->m_nUpper + (pNewRec->m_nRows - 1) * pNewRec->m_nVDist + pNewRec->m_nHeight );
201  }
202  return pNewRec;
203 }
204 
205 static Sequence<PropertyValue> lcl_CreateProperties(
206  Sequence<OUString> const & rPropNames, OUString& rMeasure, const SwLabRec& rRec)
207 {
208  const OUString* pNames = rPropNames.getConstArray();
209  Sequence<PropertyValue> aRet(rPropNames.getLength());
210  PropertyValue* pValues = aRet.getArray();
211  OUString sColon(";");
212 
213  for(sal_Int32 nProp = 0; nProp < rPropNames.getLength(); nProp++)
214  {
215  pValues[nProp].Name = pNames[nProp];
216  switch(nProp)
217  {
218  case 0: pValues[nProp].Value <<= rRec.m_aType; break;
219  case 1:
220  {
221  rMeasure.clear();
222  rMeasure += rRec.m_bCont ? OUStringLiteral( u"C" ) : OUStringLiteral( u"S" ); rMeasure += sColon;
223  rMeasure += OUString::number( convertTwipToMm100( rRec.m_nHDist ) ); rMeasure += sColon;
224  rMeasure += OUString::number( convertTwipToMm100( rRec.m_nVDist ) ); rMeasure += sColon;
225  rMeasure += OUString::number( convertTwipToMm100( rRec.m_nWidth ) ); rMeasure += sColon;
226  rMeasure += OUString::number( convertTwipToMm100( rRec.m_nHeight ) ); rMeasure += sColon;
227  rMeasure += OUString::number( convertTwipToMm100( rRec.m_nLeft ) ); rMeasure += sColon;
228  rMeasure += OUString::number( convertTwipToMm100( rRec.m_nUpper ) ); rMeasure += sColon;
229  rMeasure += OUString::number( rRec.m_nCols ); rMeasure += sColon;
230  rMeasure += OUString::number( rRec.m_nRows ); rMeasure += sColon;
231  rMeasure += OUString::number( convertTwipToMm100( rRec.m_nPWidth ) ); rMeasure += sColon;
232  rMeasure += OUString::number( convertTwipToMm100( rRec.m_nPHeight ) );
233  pValues[nProp].Value <<= rMeasure;
234  }
235  break;
236  }
237  }
238  return aRet;
239 }
240 
241 // function fills SwLabDlg with label definitions for manufacturer rManufacturer
242 void SwLabelConfig::FillLabels(const OUString& rManufacturer, SwLabRecs& rLabArr)
243 {
244  if (m_aLabels.find(rManufacturer) == m_aLabels.end())
245  return;
246  for (const auto& rEntry : m_aLabels[rManufacturer])
247  rLabArr.push_back( lcl_CreateSwLabRec(rEntry.first, rEntry.second.m_aMeasure, rManufacturer) );
248 }
249 
250 bool SwLabelConfig::HasLabel(const OUString& rManufacturer, const OUString& rType)
251 {
252  return ( ( m_aLabels.find(rManufacturer) != m_aLabels.end() ) &&
253  ( m_aLabels[rManufacturer].find(rType) != m_aLabels[rManufacturer].end() ) );
254 }
255 
256 // label is always saved as a custom label
257 // predefined labels can NOT be overwritten by custom labels with same manufacturer/name
258 void SwLabelConfig::SaveLabel( const OUString& rManufacturer,
259  const OUString& rType, const SwLabRec& rRec )
260 {
261  OUString sFoundNode;
262  bool bManufacturerNodeFound;
263  if ( m_aLabels.find( rManufacturer ) == m_aLabels.end() ||
264  !GetNodeNames( rManufacturer ).hasElements() )
265  {
266  bManufacturerNodeFound = false;
267  // manufacturer node does not exist, add (and also to m_aManufacturers)
268  if ( !AddNode( OUString(), rManufacturer ) )
269  {
270  OSL_FAIL("New configuration node could not be created");
271  return ;
272  }
273  m_aManufacturers.push_back( rManufacturer );
274  }
275  else
276  bManufacturerNodeFound = true;
277 
278  if ( !bManufacturerNodeFound ||
279  m_aLabels[rManufacturer].find( rType ) == m_aLabels[rManufacturer].end() )
280  {
281  // type does not yet exist, add to config
282  const Sequence<OUString> aLabels = GetNodeNames( rManufacturer );
283  sal_Int32 nIndex = aLabels.getLength();
284  OUString sPrefix( "Label" );
285  sFoundNode = sPrefix + OUString::number( nIndex );
286  while ( comphelper::findValue(aLabels, sFoundNode) != -1 )
287  {
288  sFoundNode = sPrefix + OUString::number(nIndex++);
289  }
290  }
291  else
292  {
293  // get the appropriate node
294  OUString sManufacturer( wrapConfigurationElementName( rManufacturer ) );
295  const Sequence<OUString> aLabels = GetNodeNames( sManufacturer );
296  for (const OUString& rLabel : aLabels)
297  {
298  OUString sPrefix = sManufacturer + "/" + rLabel + "/";
299  Sequence<OUString> aProperties { sPrefix };
300  aProperties.getArray()[0] += "Name";
301  Sequence<Any> aValues = GetProperties( aProperties );
302  const Any* pValues = aValues.getConstArray();
303  if ( pValues[0].hasValue() )
304  {
305  OUString sTmp;
306  pValues[0] >>= sTmp;
307  if ( rType == sTmp )
308  {
309  sFoundNode = rLabel;
310  break;
311  }
312  }
313  }
314  }
315 
316  OUString sPrefix = wrapConfigurationElementName( rManufacturer ) +
317  "/" + sFoundNode + "/";
319  OUString sMeasure;
320  Sequence<PropertyValue> aPropValues = lcl_CreateProperties( aPropNames, sMeasure, rRec );
321  SetSetProperties( wrapConfigurationElementName( rManufacturer ), aPropValues );
322 
323  //update m_aLabels
324  m_aLabels[rManufacturer][rType].m_aMeasure = sMeasure;
325  m_aLabels[rManufacturer][rType].m_bPredefined = false;
326 }
327 
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
static bool SetSetProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const OUString &rNode, const css::uno::Sequence< css::beans::PropertyValue > &rValues)
static css::uno::Sequence< OUString > GetNodeNames(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const OUString &rNode, ConfigNameFormat eFormat)
sal_Int32 m_nRows
Definition: labrec.hxx:50
constexpr auto toTwips(N number, Length from)
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
tools::Long m_nPWidth
Definition: labrec.hxx:47
bool HasLabel(const OUString &rManufacturer, const OUString &rType)
Definition: labelcfg.cxx:250
tools::Long m_nHDist
Definition: labrec.hxx:41
virtual void Notify(const css::uno::Sequence< OUString > &aPropertyNames) override
Definition: labelcfg.cxx:163
std::vector< OUString > m_aManufacturers
Definition: labelcfg.hxx:38
static void lcl_assertEndingItem(xmlreader::XmlReader &reader)
Definition: labelcfg.cxx:38
constexpr auto convertTwipToMm100(N n)
std::map< OUString, std::map< OUString, SwLabelMeasure > > m_aLabels
Definition: labelcfg.hxx:39
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)
constexpr OStringLiteral sColon
PropertiesInfo aProperties
bool m_bCont
Definition: labrec.hxx:51
tools::Long m_nPHeight
Definition: labrec.hxx:48
virtual void ImplCommit() override
Definition: labelcfg.cxx:161
const char * sName
static css::uno::Sequence< css::uno::Any > GetProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, bool bAllLocales)
static Sequence< OUString > lcl_CreatePropertyNames(const OUString &rPrefix)
Definition: labelcfg.cxx:64
tools::Long m_nHeight
Definition: labrec.hxx:44
OUString wrapConfigurationElementName(OUString const &_sElementName)
tools::Long m_nUpper
Definition: labrec.hxx:46
float u
const PropertyStruct aPropNames[]
static Sequence< PropertyValue > lcl_CreateProperties(Sequence< OUString > const &rPropNames, OUString &rMeasure, const SwLabRec &rRec)
Definition: labelcfg.cxx:205
OUString sPrefix
static OUString lcl_getValue(xmlreader::XmlReader &reader, const xmlreader::Span &span)
Definition: labelcfg.cxx:48
void FillLabels(const OUString &rManufacturer, SwLabRecs &rLabArr)
Definition: labelcfg.cxx:242
enumrange< T >::Iterator end(enumrange< T >)
sal_Int32 m_nCols
Definition: labrec.hxx:49
OUString m_aType
Definition: labrec.hxx:40
tools::Long m_nVDist
Definition: labrec.hxx:42
const PropertyValue * pValues
bool AddNode(const OUString &rNode, const OUString &rNewNode)
tools::Long m_nWidth
Definition: labrec.hxx:43
Span getAttributeValue(bool fullyNormalize)
rtl::OUString convertFromUtf8() const
static std::unique_ptr< SwLabRec > lcl_CreateSwLabRec(const OUString &rType, const OUString &rMeasure, const OUString &rManufacturer)
Definition: labelcfg.cxx:165
const char * name
tools::Long m_nLeft
Definition: labrec.hxx:45
std::vector< std::unique_ptr< SwLabRec > > SwLabRecs
Definition: labrec.hxx:54
void SaveLabel(const OUString &rManufacturer, const OUString &rType, const SwLabRec &rRec)
Definition: labelcfg.cxx:258
bool nextAttribute(int *nsId, Span *localName)
Result nextItem(Text reportText, Span *data, int *nsId)
virtual ~SwLabelConfig() override
Definition: labelcfg.cxx:156
exports com.sun.star. uri
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo