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