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