merge the formfield patch from ooo-build
[ooovba.git] / sw / source / ui / envelp / labelcfg.cxx
blob32ae96881b44af7a6e37e73b61abd2383e739537
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: labelcfg.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <swtypes.hxx>
36 #include <labelcfg.hxx>
37 #include <labimp.hxx>
38 #include <unotools/configpathes.hxx>
40 #include <unomid.h>
42 using namespace utl;
43 using namespace rtl;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::beans;
47 /* -----------------------------15.01.01 11:17--------------------------------
49 ---------------------------------------------------------------------------*/
50 SwLabelConfig::SwLabelConfig() :
51 ConfigItem(C2U("Office.Labels/Manufacturer"))
53 aNodeNames = GetNodeNames(OUString());
55 /* -----------------------------06.09.00 16:50--------------------------------
57 ---------------------------------------------------------------------------*/
58 SwLabelConfig::~SwLabelConfig()
61 /* -----------------------------06.09.00 16:43--------------------------------
63 ---------------------------------------------------------------------------*/
64 void SwLabelConfig::Commit()
66 // the config item is not writable yet
68 /* -----------------------------15.01.01 11:42--------------------------------
70 ---------------------------------------------------------------------------*/
71 Sequence<OUString> lcl_CreatePropertyNames(const OUString& rPrefix)
73 Sequence<OUString> aProperties(2);
74 OUString* pProperties = aProperties.getArray();
75 for(sal_Int32 nProp = 0; nProp < 2; nProp++)
76 pProperties[nProp] = rPrefix;
78 pProperties[ 0] += C2U("Name");
79 pProperties[ 1] += C2U("Measure");
80 return aProperties;
82 //-----------------------------------------------------------------------------
83 SwLabRec* lcl_CreateSwLabRec(Sequence<Any>& rValues, const OUString& rManufacturer)
85 SwLabRec* pNewRec = new SwLabRec;
86 const Any* pValues = rValues.getConstArray();
87 OUString sTmp;
88 pNewRec->aMake = rManufacturer;
89 for(sal_Int32 nProp = 0; nProp < rValues.getLength(); nProp++)
91 if(pValues[nProp].hasValue())
93 switch(nProp)
95 case 0: pValues[nProp] >>= sTmp; pNewRec->aType = sTmp; break;
96 case 1:
98 //all values are contained as colon-separated 1/100 mm values except for the
99 //continuous flag ('C'/'S')
100 pValues[nProp] >>= sTmp;
101 String sMeasure(sTmp);
102 USHORT nTokenCount = sMeasure.GetTokenCount(';');
103 for(USHORT i = 0; i < nTokenCount; i++)
105 String sToken(sMeasure.GetToken(i, ';' ));
106 int nVal = sToken.ToInt32();
107 switch(i)
109 case 0 : pNewRec->bCont = sToken.GetChar(0) == 'C'; break;
110 case 1 : pNewRec->lHDist = MM100_TO_TWIP(nVal);break;
111 case 2 : pNewRec->lVDist = MM100_TO_TWIP(nVal);break;
112 case 3 : pNewRec->lWidth = MM100_TO_TWIP(nVal);break;
113 case 4 : pNewRec->lHeight = MM100_TO_TWIP(nVal); break;
114 case 5 : pNewRec->lLeft = MM100_TO_TWIP(nVal);break;
115 case 6 : pNewRec->lUpper = MM100_TO_TWIP(nVal);break;
116 case 7 : pNewRec->nCols = nVal; break;
117 case 8 : pNewRec->nRows = nVal; break;
121 break;
125 return pNewRec;
127 //-----------------------------------------------------------------------------
128 Sequence<PropertyValue> lcl_CreateProperties(
129 Sequence<OUString>& rPropNames, const SwLabRec& rRec)
131 const OUString* pNames = rPropNames.getConstArray();
132 Sequence<PropertyValue> aRet(rPropNames.getLength());
133 PropertyValue* pValues = aRet.getArray();
134 OUString sColon(C2U(";"));
136 for(sal_Int32 nProp = 0; nProp < rPropNames.getLength(); nProp++)
138 pValues[nProp].Name = pNames[nProp];
139 switch(nProp)
141 case 0: pValues[nProp].Value <<= OUString(rRec.aType); break;
142 case 1:
144 OUString sTmp;
145 sTmp += C2U( rRec.bCont ? "C" : "S"); sTmp += sColon;
146 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lHDist) ); sTmp += sColon;
147 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lVDist)); sTmp += sColon;
148 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lWidth) ); sTmp += sColon;
149 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lHeight) ); sTmp += sColon;
150 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lLeft) ); sTmp += sColon;
151 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lUpper) ); sTmp += sColon;
152 sTmp += OUString::valueOf(rRec.nCols );sTmp += sColon;
153 sTmp += OUString::valueOf(rRec.nRows );
154 pValues[nProp].Value <<= sTmp;
156 break;
159 return aRet;
161 //-----------------------------------------------------------------------------
162 void SwLabelConfig::FillLabels(const OUString& rManufacturer, SwLabRecs& rLabArr)
164 OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
165 const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
166 const OUString* pLabels = aLabels.getConstArray();
167 for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
169 OUString sPrefix(sManufacturer);
170 sPrefix += C2U("/");
171 sPrefix += pLabels[nLabel];
172 sPrefix += C2U("/");
173 Sequence<OUString> aPropNames = lcl_CreatePropertyNames(sPrefix);
174 Sequence<Any> aValues = GetProperties(aPropNames);
175 SwLabRec* pNewRec = lcl_CreateSwLabRec(aValues, rManufacturer);
176 rLabArr.C40_INSERT( SwLabRec, pNewRec, rLabArr.Count() );
179 /* -----------------------------23.01.01 11:36--------------------------------
181 ---------------------------------------------------------------------------*/
182 sal_Bool SwLabelConfig::HasLabel(const rtl::OUString& rManufacturer, const rtl::OUString& rType)
184 const OUString* pNode = aNodeNames.getConstArray();
185 sal_Bool bFound = sal_False;
186 for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength() && !bFound; nNode++)
188 if(pNode[nNode] == rManufacturer)
189 bFound = sal_True;
191 if(bFound)
193 OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
194 const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
195 const OUString* pLabels = aLabels.getConstArray();
196 for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
198 OUString sPrefix(sManufacturer);
199 sPrefix += C2U("/");
200 sPrefix += pLabels[nLabel];
201 sPrefix += C2U("/");
202 Sequence<OUString> aProperties(1);
203 aProperties.getArray()[0] = sPrefix;
204 aProperties.getArray()[0] += C2U("Name");
205 Sequence<Any> aValues = GetProperties(aProperties);
206 const Any* pValues = aValues.getConstArray();
207 if(pValues[0].hasValue())
209 OUString sTmp;
210 pValues[0] >>= sTmp;
211 if(rType == sTmp)
212 return sal_True;
216 return sal_False;
218 /* -----------------------------23.01.01 11:36--------------------------------
220 ---------------------------------------------------------------------------*/
221 sal_Bool lcl_Exists(const OUString& rNode, const Sequence<OUString>& rLabels)
223 const OUString* pLabels = rLabels.getConstArray();
224 for(sal_Int32 i = 0; i < rLabels.getLength(); i++)
225 if(pLabels[i] == rNode)
226 return sal_True;
227 return sal_False;
229 //-----------------------------------------------------------------------------
230 void SwLabelConfig::SaveLabel( const rtl::OUString& rManufacturer,
231 const rtl::OUString& rType, const SwLabRec& rRec)
233 const OUString* pNode = aNodeNames.getConstArray();
234 sal_Bool bFound = sal_False;
235 for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength() && !bFound; nNode++)
237 if(pNode[nNode] == rManufacturer)
238 bFound = sal_True;
240 if(!bFound)
242 if(!AddNode(OUString(), rManufacturer))
244 DBG_ERROR("New configuration node could not be created");
245 return ;
247 else
249 aNodeNames = GetNodeNames(OUString());
253 OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
254 const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
255 const OUString* pLabels = aLabels.getConstArray();
256 OUString sFoundNode;
257 for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
259 OUString sPrefix(sManufacturer);
260 sPrefix += C2U("/");
261 sPrefix += pLabels[nLabel];
262 sPrefix += C2U("/");
263 Sequence<OUString> aProperties(1);
264 aProperties.getArray()[0] = sPrefix;
265 aProperties.getArray()[0] += C2U("Name");
266 Sequence<Any> aValues = GetProperties(aProperties);
267 const Any* pValues = aValues.getConstArray();
268 if(pValues[0].hasValue())
270 OUString sTmp;
271 pValues[0] >>= sTmp;
272 if(rType == sTmp)
274 sFoundNode = pLabels[nLabel];
275 break;
279 // if not found - generate a unique node name
280 if(!sFoundNode.getLength())
282 sal_Int32 nIndex = aLabels.getLength();
283 OUString sPrefix(C2U("Label"));
284 sFoundNode = sPrefix;
285 sFoundNode += OUString::valueOf(nIndex);
286 while(lcl_Exists(sFoundNode, aLabels))
288 sFoundNode = sPrefix;
289 sFoundNode += OUString::valueOf(nIndex++);
292 OUString sPrefix(wrapConfigurationElementName(rManufacturer));
293 sPrefix += C2U("/");
294 sPrefix += sFoundNode;
295 sPrefix += C2U("/");
296 Sequence<OUString> aPropNames = lcl_CreatePropertyNames(sPrefix);
297 Sequence<PropertyValue> aPropValues = lcl_CreateProperties(aPropNames, rRec);
298 SetSetProperties(wrapConfigurationElementName(rManufacturer), aPropValues);