1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: labelcfg.cxx,v $
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>
38 #include <unotools/configpathes.hxx>
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");
82 //-----------------------------------------------------------------------------
83 SwLabRec
* lcl_CreateSwLabRec(Sequence
<Any
>& rValues
, const OUString
& rManufacturer
)
85 SwLabRec
* pNewRec
= new SwLabRec
;
86 const Any
* pValues
= rValues
.getConstArray();
88 pNewRec
->aMake
= rManufacturer
;
89 for(sal_Int32 nProp
= 0; nProp
< rValues
.getLength(); nProp
++)
91 if(pValues
[nProp
].hasValue())
95 case 0: pValues
[nProp
] >>= sTmp
; pNewRec
->aType
= sTmp
; break;
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();
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;
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
];
141 case 0: pValues
[nProp
].Value
<<= OUString(rRec
.aType
); break;
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
;
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
);
171 sPrefix
+= pLabels
[nLabel
];
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
)
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
);
200 sPrefix
+= pLabels
[nLabel
];
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())
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
)
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
)
242 if(!AddNode(OUString(), rManufacturer
))
244 DBG_ERROR("New configuration node could not be created");
249 aNodeNames
= GetNodeNames(OUString());
253 OUString
sManufacturer(wrapConfigurationElementName(rManufacturer
));
254 const Sequence
<OUString
> aLabels
= GetNodeNames(sManufacturer
);
255 const OUString
* pLabels
= aLabels
.getConstArray();
257 for(sal_Int32 nLabel
= 0; nLabel
< aLabels
.getLength(); nLabel
++)
259 OUString
sPrefix(sManufacturer
);
261 sPrefix
+= pLabels
[nLabel
];
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())
274 sFoundNode
= pLabels
[nLabel
];
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
));
294 sPrefix
+= sFoundNode
;
296 Sequence
<OUString
> aPropNames
= lcl_CreatePropertyNames(sPrefix
);
297 Sequence
<PropertyValue
> aPropValues
= lcl_CreateProperties(aPropNames
, rRec
);
298 SetSetProperties(wrapConfigurationElementName(rManufacturer
), aPropValues
);