xref: /trunk/main/sw/source/ui/envelp/labelcfg.cxx (revision ffd38472365e95f6a578737bc9a5eb0fac624a86)
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  ---------------------------------------------------------------------------*/
43 SwLabelConfig::SwLabelConfig() :
44     ConfigItem(C2U("Office.Labels/Manufacturer"))
45 {
46     aNodeNames = GetNodeNames(OUString());
47 }
48 /* -----------------------------06.09.00 16:50--------------------------------
49 
50  ---------------------------------------------------------------------------*/
51 SwLabelConfig::~SwLabelConfig()
52 {
53 }
54 /* -----------------------------06.09.00 16:43--------------------------------
55 
56  ---------------------------------------------------------------------------*/
57 void    SwLabelConfig::Commit()
58 {
59     // the config item is not writable yet
60 }
61 
62 void SwLabelConfig::Notify( const ::com::sun::star::uno::Sequence< rtl::OUString >& ) {}
63 
64 /* -----------------------------15.01.01 11:42--------------------------------
65 
66  ---------------------------------------------------------------------------*/
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 //-----------------------------------------------------------------------------
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                             case 9 : pNewRec->lPaperWidth = MM100_TO_TWIP(nVal);break;
115                             case 10: pNewRec->lPaperHeight = MM100_TO_TWIP(nVal);break;
116                         }
117                     }
118                 }
119                 break;
120             }
121         }
122     }
123     return pNewRec;
124 }
125 //-----------------------------------------------------------------------------
126 Sequence<PropertyValue> lcl_CreateProperties(
127     Sequence<OUString>& rPropNames, const SwLabRec& rRec)
128 {
129     const OUString* pNames = rPropNames.getConstArray();
130     Sequence<PropertyValue> aRet(rPropNames.getLength());
131     PropertyValue* pValues = aRet.getArray();
132     OUString sColon(C2U(";"));
133 
134     for(sal_Int32 nProp = 0; nProp < rPropNames.getLength(); nProp++)
135     {
136         pValues[nProp].Name = pNames[nProp];
137         switch(nProp)
138         {
139             case 0: pValues[nProp].Value <<= OUString(rRec.aType); break;
140             case 1:
141             {
142                 OUString sTmp;
143                 sTmp += C2U( rRec.bCont ? "C" : "S");                            sTmp += sColon;
144                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lHDist) );           sTmp += sColon;
145                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lVDist));            sTmp += sColon;
146                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lWidth)  );          sTmp += sColon;
147                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lHeight) );          sTmp += sColon;
148                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lLeft)   );          sTmp += sColon;
149                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lUpper)  );          sTmp += sColon;
150                 sTmp += OUString::valueOf(rRec.nCols   );sTmp += sColon;
151                 sTmp += OUString::valueOf(rRec.nRows   );sTmp += sColon;
152                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lPaperWidth));sTmp += sColon;
153                 sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lPaperHeight));
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