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