xref: /trunk/main/sw/source/ui/envelp/labelcfg.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 
32 #include <swtypes.hxx>
33 #include <labelcfg.hxx>
34 #include <labimp.hxx>
35 #include <unotools/configpathes.hxx>
36 
37 #include <unomid.h>
38 
39 using namespace utl;
40 using namespace rtl;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 
44 /* -----------------------------15.01.01 11:17--------------------------------
45 
46  ---------------------------------------------------------------------------*/
47 SwLabelConfig::SwLabelConfig() :
48 	ConfigItem(C2U("Office.Labels/Manufacturer"))
49 {
50 	aNodeNames = GetNodeNames(OUString());
51 }
52 /* -----------------------------06.09.00 16:50--------------------------------
53 
54  ---------------------------------------------------------------------------*/
55 SwLabelConfig::~SwLabelConfig()
56 {
57 }
58 /* -----------------------------06.09.00 16:43--------------------------------
59 
60  ---------------------------------------------------------------------------*/
61 void	SwLabelConfig::Commit()
62 {
63 	// the config item is not writable yet
64 }
65 
66 void SwLabelConfig::Notify( const ::com::sun::star::uno::Sequence< rtl::OUString >& ) {}
67 
68 /* -----------------------------15.01.01 11:42--------------------------------
69 
70  ---------------------------------------------------------------------------*/
71 Sequence<OUString> lcl_CreatePropertyNames(const OUString& rPrefix)
72 {
73     Sequence<OUString> aProperties(2);
74 	OUString* pProperties = aProperties.getArray();
75     for(sal_Int32 nProp = 0; nProp < 2; nProp++)
76 		pProperties[nProp] = rPrefix;
77 
78 	pProperties[ 0] += C2U("Name");
79     pProperties[ 1] += C2U("Measure");
80 	return aProperties;
81 }
82 //-----------------------------------------------------------------------------
83 SwLabRec* lcl_CreateSwLabRec(Sequence<Any>& rValues, const OUString& rManufacturer)
84 {
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++)
90 	{
91 		if(pValues[nProp].hasValue())
92 		{
93 			switch(nProp)
94 			{
95 				case 0: pValues[nProp] >>= sTmp; pNewRec->aType = sTmp; break;
96                 case 1:
97                 {
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                     sal_uInt16 nTokenCount = sMeasure.GetTokenCount(';');
103                     for(sal_uInt16 i = 0; i < nTokenCount; i++)
104                     {
105                         String sToken(sMeasure.GetToken(i, ';' ));
106                         int nVal = sToken.ToInt32();
107                         switch(i)
108                         {
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;
118                         }
119                     }
120                 }
121                 break;
122 			}
123 		}
124 	}
125 	return pNewRec;
126 }
127 //-----------------------------------------------------------------------------
128 Sequence<PropertyValue>	lcl_CreateProperties(
129 	Sequence<OUString>& rPropNames, const SwLabRec& rRec)
130 {
131 	const OUString* pNames = rPropNames.getConstArray();
132 	Sequence<PropertyValue>	aRet(rPropNames.getLength());
133 	PropertyValue* pValues = aRet.getArray();
134     OUString sColon(C2U(";"));
135 
136     for(sal_Int32 nProp = 0; nProp < rPropNames.getLength(); nProp++)
137 	{
138 		pValues[nProp].Name = pNames[nProp];
139 		switch(nProp)
140 		{
141 			case 0: pValues[nProp].Value <<= OUString(rRec.aType); break;
142             case 1:
143             {
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;
155             }
156             break;
157         }
158 	}
159 	return aRet;
160 }
161 //-----------------------------------------------------------------------------
162 void	SwLabelConfig::FillLabels(const OUString& rManufacturer, SwLabRecs& rLabArr)
163 {
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++)
168 	{
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() );
177 	}
178 }
179 /* -----------------------------23.01.01 11:36--------------------------------
180 
181  ---------------------------------------------------------------------------*/
182 sal_Bool	SwLabelConfig::HasLabel(const rtl::OUString& rManufacturer, const rtl::OUString& rType)
183 {
184 	const OUString* pNode = aNodeNames.getConstArray();
185 	sal_Bool bFound = sal_False;
186 	for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength() && !bFound; nNode++)
187 	{
188 		if(pNode[nNode] == rManufacturer)
189 			bFound = sal_True;
190 	}
191 	if(bFound)
192 	{
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++)
197 		{
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())
208 			{
209 				OUString sTmp;
210 				pValues[0] >>= sTmp;
211 				if(rType == sTmp)
212 					return sal_True;
213 			}
214 		}
215 	}
216 	return sal_False;
217 }
218 /* -----------------------------23.01.01 11:36--------------------------------
219 
220  ---------------------------------------------------------------------------*/
221 sal_Bool lcl_Exists(const OUString& rNode, const Sequence<OUString>& rLabels)
222 {
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;
228 }
229 //-----------------------------------------------------------------------------
230 void SwLabelConfig::SaveLabel( 	const rtl::OUString& rManufacturer,
231 		const rtl::OUString& rType,	const SwLabRec& rRec)
232 {
233 	const OUString* pNode = aNodeNames.getConstArray();
234 	sal_Bool bFound = sal_False;
235 	for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength() && !bFound; nNode++)
236 	{
237 		if(pNode[nNode] == rManufacturer)
238 			bFound = sal_True;
239 	}
240 	if(!bFound)
241 	{
242 		if(!AddNode(OUString(), rManufacturer))
243 		{
244 			DBG_ERROR("New configuration node could not be created");
245 			return ;
246 		}
247 		else
248 		{
249 			aNodeNames = GetNodeNames(OUString());
250 		}
251 	}
252 
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++)
258 	{
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())
269 		{
270 			OUString sTmp;
271 			pValues[0] >>= sTmp;
272 			if(rType == sTmp)
273 			{
274 				sFoundNode = pLabels[nLabel];
275 				break;
276 			}
277 		}
278 	}
279 	// if not found - generate a unique node name
280 	if(!sFoundNode.getLength())
281 	{
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))
287 		{
288 			sFoundNode = sPrefix;
289 			sFoundNode += OUString::valueOf(nIndex++);
290 		}
291 	}
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);
299 
300 }
301 
302 
303