LCOV - code coverage report
Current view: top level - libreoffice/sw/source/ui/envelp - labelcfg.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 172 0.6 %
Date: 2012-12-27 Functions: 2 14 14.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <swtypes.hxx>
      21             : #include <labelcfg.hxx>
      22             : #include <labimp.hxx>
      23             : #include <comphelper/string.hxx>
      24             : #include <unotools/configpaths.hxx>
      25             : 
      26             : #include <unomid.h>
      27             : 
      28             : using namespace utl;
      29             : using namespace ::com::sun::star::uno;
      30             : using namespace ::com::sun::star::beans;
      31             : 
      32             : using ::rtl::OUString;
      33             : 
      34           0 : SwLabelConfig::SwLabelConfig() :
      35           0 :     ConfigItem("Office.Labels/Manufacturer")
      36             : {
      37           0 :     aNodeNames = GetNodeNames(OUString());
      38           0 : }
      39             : 
      40           0 : SwLabelConfig::~SwLabelConfig()
      41             : {
      42           0 : }
      43             : 
      44           0 : void    SwLabelConfig::Commit()
      45             : {
      46             :     // the config item is not writable yet
      47           0 : }
      48             : 
      49           0 : void SwLabelConfig::Notify( const ::com::sun::star::uno::Sequence< rtl::OUString >& ) {}
      50             : 
      51           0 : static Sequence<OUString> lcl_CreatePropertyNames(const OUString& rPrefix)
      52             : {
      53           0 :     Sequence<OUString> aProperties(2);
      54           0 :     OUString* pProperties = aProperties.getArray();
      55           0 :     for(sal_Int32 nProp = 0; nProp < 2; nProp++)
      56           0 :         pProperties[nProp] = rPrefix;
      57             : 
      58           0 :     pProperties[ 0] += "Name";
      59           0 :     pProperties[ 1] += "Measure";
      60           0 :     return aProperties;
      61             : }
      62             : 
      63           0 : static SwLabRec* lcl_CreateSwLabRec(Sequence<Any>& rValues, const OUString& rManufacturer)
      64             : {
      65           0 :     SwLabRec* pNewRec = new SwLabRec;
      66           0 :     const Any* pValues = rValues.getConstArray();
      67           0 :     OUString sTmp;
      68           0 :     pNewRec->aMake = rManufacturer;
      69           0 :     pNewRec->lPWidth = 0;
      70           0 :     pNewRec->lPHeight = 0;
      71           0 :     for(sal_Int32 nProp = 0; nProp < rValues.getLength(); nProp++)
      72             :     {
      73           0 :         if(pValues[nProp].hasValue())
      74             :         {
      75           0 :             switch(nProp)
      76             :             {
      77           0 :                 case 0: pValues[nProp] >>= sTmp; pNewRec->aType = sTmp; break;
      78             :                 case 1:
      79             :                 {
      80             :                     //all values are contained as colon-separated 1/100 mm values
      81             :                     //except for the continuous flag ('C'/'S')
      82           0 :                     pValues[nProp] >>= sTmp;
      83           0 :                     String sMeasure(sTmp);
      84           0 :                     sal_uInt16 nTokenCount = comphelper::string::getTokenCount(sMeasure, ';');
      85           0 :                     for(sal_uInt16 i = 0; i < nTokenCount; i++)
      86             :                     {
      87           0 :                         String sToken(sMeasure.GetToken(i, ';' ));
      88           0 :                         int nVal = sToken.ToInt32();
      89           0 :                         switch(i)
      90             :                         {
      91           0 :                             case  0 : pNewRec->bCont = sToken.GetChar(0) == 'C'; break;
      92           0 :                             case  1 : pNewRec->lHDist    = MM100_TO_TWIP(nVal);  break;
      93           0 :                             case  2 : pNewRec->lVDist    = MM100_TO_TWIP(nVal);  break;
      94           0 :                             case  3 : pNewRec->lWidth    = MM100_TO_TWIP(nVal);  break;
      95           0 :                             case  4 : pNewRec->lHeight   = MM100_TO_TWIP(nVal);  break;
      96           0 :                             case  5 : pNewRec->lLeft     = MM100_TO_TWIP(nVal);  break;
      97           0 :                             case  6 : pNewRec->lUpper    = MM100_TO_TWIP(nVal);  break;
      98           0 :                             case  7 : pNewRec->nCols     = nVal;                 break;
      99           0 :                             case  8 : pNewRec->nRows     = nVal;                 break;
     100           0 :                             case  9 : pNewRec->lPWidth   = MM100_TO_TWIP(nVal);  break;
     101           0 :                             case 10 : pNewRec->lPHeight  = MM100_TO_TWIP(nVal);  break;
     102             :                         }
     103           0 :                     }
     104             :                 }
     105           0 :                 break;
     106             :             }
     107             :         }
     108             :     }
     109             :     // lines added for compatibility with custom label defintions saved before patch 44516
     110           0 :     if (pNewRec->lPWidth == 0 || pNewRec->lPHeight == 0)
     111             :     {
     112             :         // old style definition (no paper dimensions), calculate probable values
     113           0 :         pNewRec->lPWidth = 2 * pNewRec->lLeft + (pNewRec->nCols - 1) * pNewRec->lHDist + pNewRec->lWidth;
     114           0 :         pNewRec->lPHeight = ( pNewRec->bCont ? pNewRec->nRows * pNewRec->lVDist : 2 * pNewRec->lUpper + (pNewRec->nRows - 1) * pNewRec->lVDist + pNewRec->lHeight );
     115             :     }
     116           0 :     return pNewRec;
     117             : }
     118             : 
     119           0 : static Sequence<PropertyValue> lcl_CreateProperties(
     120             :     Sequence<OUString>& rPropNames, const SwLabRec& rRec)
     121             : {
     122           0 :     const OUString* pNames = rPropNames.getConstArray();
     123           0 :     Sequence<PropertyValue> aRet(rPropNames.getLength());
     124           0 :     PropertyValue* pValues = aRet.getArray();
     125           0 :     OUString sColon(";");
     126             : 
     127           0 :     for(sal_Int32 nProp = 0; nProp < rPropNames.getLength(); nProp++)
     128             :     {
     129           0 :         pValues[nProp].Name = pNames[nProp];
     130           0 :         switch(nProp)
     131             :         {
     132           0 :             case 0: pValues[nProp].Value <<= OUString(rRec.aType); break;
     133             :             case 1:
     134             :             {
     135           0 :                 OUString sTmp;
     136           0 :                 sTmp += rRec.bCont ? OUString("C") : OUString("S");           sTmp += sColon;
     137           0 :                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lHDist) );       sTmp += sColon;
     138           0 :                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lVDist));        sTmp += sColon;
     139           0 :                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lWidth)  );      sTmp += sColon;
     140           0 :                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lHeight) );      sTmp += sColon;
     141           0 :                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lLeft)   );      sTmp += sColon;
     142           0 :                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lUpper)  );      sTmp += sColon;
     143           0 :                 sTmp += OUString::valueOf(rRec.nCols   );                     sTmp += sColon;
     144           0 :                 sTmp += OUString::valueOf(rRec.nRows   );                     sTmp += sColon;
     145           0 :                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lPWidth)  );     sTmp += sColon;
     146           0 :                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lPHeight)  );
     147           0 :                 pValues[nProp].Value <<= sTmp;
     148             :             }
     149           0 :             break;
     150             :         }
     151             :     }
     152           0 :     return aRet;
     153             : }
     154             : 
     155           0 : void    SwLabelConfig::FillLabels(const OUString& rManufacturer, SwLabRecs& rLabArr)
     156             : {
     157           0 :     OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
     158           0 :     const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
     159           0 :     const OUString* pLabels = aLabels.getConstArray();
     160           0 :     for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
     161             :     {
     162           0 :         OUString sPrefix(sManufacturer);
     163           0 :         sPrefix += "/";
     164           0 :         sPrefix += pLabels[nLabel];
     165           0 :         sPrefix += "/";
     166           0 :         Sequence<OUString> aPropNames = lcl_CreatePropertyNames(sPrefix);
     167           0 :         Sequence<Any>   aValues = GetProperties(aPropNames);
     168           0 :         SwLabRec* pNewRec = lcl_CreateSwLabRec(aValues, rManufacturer);
     169           0 :         rLabArr.push_back( pNewRec );
     170           0 :     }
     171           0 : }
     172             : 
     173           0 : sal_Bool    SwLabelConfig::HasLabel(const rtl::OUString& rManufacturer, const rtl::OUString& rType)
     174             : {
     175           0 :     const OUString* pNode = aNodeNames.getConstArray();
     176           0 :     sal_Bool bFound = sal_False;
     177           0 :     for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength() && !bFound; nNode++)
     178             :     {
     179           0 :         if(pNode[nNode] == rManufacturer)
     180           0 :             bFound = sal_True;
     181             :     }
     182           0 :     if(bFound)
     183             :     {
     184           0 :         OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
     185           0 :         const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
     186           0 :         const OUString* pLabels = aLabels.getConstArray();
     187           0 :         for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
     188             :         {
     189           0 :             OUString sPrefix(sManufacturer);
     190           0 :             sPrefix += "/";
     191           0 :             sPrefix += pLabels[nLabel];
     192           0 :             sPrefix += "/";
     193           0 :             Sequence<OUString> aProperties(1);
     194           0 :             aProperties.getArray()[0] = sPrefix;
     195           0 :             aProperties.getArray()[0] += "Name";
     196           0 :             Sequence<Any>   aValues = GetProperties(aProperties);
     197           0 :             const Any* pValues = aValues.getConstArray();
     198           0 :             if(pValues[0].hasValue())
     199             :             {
     200           0 :                 OUString sTmp;
     201           0 :                 pValues[0] >>= sTmp;
     202           0 :                 if(rType == sTmp)
     203           0 :                     return sal_True;
     204             :             }
     205           0 :         }
     206             :     }
     207           0 :     return sal_False;
     208             : }
     209             : 
     210           0 : static sal_Bool lcl_Exists(const OUString& rNode, const Sequence<OUString>& rLabels)
     211             : {
     212           0 :     const OUString* pLabels = rLabels.getConstArray();
     213           0 :     for(sal_Int32 i = 0; i < rLabels.getLength(); i++)
     214           0 :         if(pLabels[i] == rNode)
     215           0 :             return sal_True;
     216           0 :     return sal_False;
     217             : }
     218             : 
     219           0 : void SwLabelConfig::SaveLabel(  const rtl::OUString& rManufacturer,
     220             :         const rtl::OUString& rType, const SwLabRec& rRec)
     221             : {
     222           0 :     const OUString* pNode = aNodeNames.getConstArray();
     223           0 :     sal_Bool bFound = sal_False;
     224           0 :     for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength() && !bFound; nNode++)
     225             :     {
     226           0 :         if(pNode[nNode] == rManufacturer)
     227           0 :             bFound = sal_True;
     228             :     }
     229           0 :     if(!bFound)
     230             :     {
     231           0 :         if(!AddNode(OUString(), rManufacturer))
     232             :         {
     233             :             OSL_FAIL("New configuration node could not be created");
     234           0 :             return ;
     235             :         }
     236             :         else
     237             :         {
     238           0 :             aNodeNames = GetNodeNames(OUString());
     239             :         }
     240             :     }
     241             : 
     242           0 :     OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
     243           0 :     const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
     244           0 :     const OUString* pLabels = aLabels.getConstArray();
     245           0 :     OUString sFoundNode;
     246           0 :     for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
     247             :     {
     248           0 :         OUString sPrefix(sManufacturer);
     249           0 :         sPrefix += "/";
     250           0 :         sPrefix += pLabels[nLabel];
     251           0 :         sPrefix += "/";
     252           0 :         Sequence<OUString> aProperties(1);
     253           0 :         aProperties.getArray()[0] = sPrefix;
     254           0 :         aProperties.getArray()[0] += "Name";
     255           0 :         Sequence<Any>   aValues = GetProperties(aProperties);
     256           0 :         const Any* pValues = aValues.getConstArray();
     257           0 :         if(pValues[0].hasValue())
     258             :         {
     259           0 :             OUString sTmp;
     260           0 :             pValues[0] >>= sTmp;
     261           0 :             if(rType == sTmp)
     262             :             {
     263           0 :                 sFoundNode = pLabels[nLabel];
     264             :                 break;
     265           0 :             }
     266             :         }
     267           0 :     }
     268             :     // if not found - generate a unique node name
     269           0 :     if(sFoundNode.isEmpty())
     270             :     {
     271           0 :         sal_Int32 nIndex = aLabels.getLength();
     272           0 :         OUString sPrefix("Label");
     273           0 :         sFoundNode = sPrefix;
     274           0 :         sFoundNode += OUString::valueOf(nIndex);
     275           0 :         while(lcl_Exists(sFoundNode, aLabels))
     276             :         {
     277           0 :             sFoundNode = sPrefix;
     278           0 :             sFoundNode += OUString::valueOf(nIndex++);
     279           0 :         }
     280             :     }
     281           0 :     OUString sPrefix(wrapConfigurationElementName(rManufacturer));
     282           0 :     sPrefix += "/";
     283           0 :     sPrefix += sFoundNode;
     284           0 :     sPrefix += "/";
     285           0 :     Sequence<OUString> aPropNames = lcl_CreatePropertyNames(sPrefix);
     286           0 :     Sequence<PropertyValue> aPropValues = lcl_CreateProperties(aPropNames, rRec);
     287           0 :     SetSetProperties(wrapConfigurationElementName(rManufacturer), aPropValues);
     288             : 
     289          30 : }
     290             : 
     291             : 
     292             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10