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