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