summaryrefslogtreecommitdiff
path: root/sw/source/ui/envelp/labelcfg.cxx
blob: 28d407484a8006a0286fa8df29cccb410f3d2389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <swtypes.hxx>
#include <labelcfg.hxx>
#include <labimp.hxx>
#include <comphelper/string.hxx>
#include <rtl/bootstrap.hxx>
#include <unotools/configpaths.hxx>
#include <xmlreader/xmlreader.hxx>

#include <unomid.h>

using namespace utl;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;

static inline void lcl_assertEndingItem(xmlreader::XmlReader& reader)
{
#if OSL_DEBUG_LEVEL > 0
    int nsId;
    xmlreader::Span name;
    assert(reader.nextItem(xmlreader::XmlReader::TEXT_NONE, &name, &nsId)
            == xmlreader::XmlReader::RESULT_END);
#else
    (void)reader;
#endif
}

static inline OUString lcl_getValue(xmlreader::XmlReader& reader,
                                    const xmlreader::Span& span)
{
    int nsId;
    xmlreader::Span name;
    xmlreader::XmlReader::Result res;
    res = reader.nextItem(xmlreader::XmlReader::TEXT_NONE, &name, &nsId);
    assert(res == xmlreader::XmlReader::RESULT_BEGIN && name.equals(span));
    res = reader.nextItem(xmlreader::XmlReader::TEXT_RAW, &name, &nsId);
    assert(res == xmlreader::XmlReader::RESULT_TEXT);
    (void) res; (void) span;
    OUString sTmp = name.convertFromUtf8();
    lcl_assertEndingItem(reader);
    return sTmp;
}

SwLabelConfig::SwLabelConfig() :
    ConfigItem("Office.Labels/Manufacturer")
{
    OUString uri("$BRAND_BASE_DIR/share/labels/labels.xml");
    rtl::Bootstrap::expandMacros(uri);
    xmlreader::XmlReader reader(uri);
    int nsId;
    xmlreader::Span name;
    xmlreader::XmlReader::Result res;
    OUString sManufacturer;
    OUString sName;
    OUString sMeasure;

    res = reader.nextItem(
            xmlreader::XmlReader::TEXT_NONE, &name, &nsId);
    assert(res == xmlreader::XmlReader::RESULT_BEGIN &&
                name.equals(RTL_CONSTASCII_STRINGPARAM("manufacturers")));
    res = reader.nextItem(
            xmlreader::XmlReader::TEXT_NONE, &name, &nsId);
    while (res != xmlreader::XmlReader::RESULT_END)
    {
        // Opening manufacturer
        assert(res == xmlreader::XmlReader::RESULT_BEGIN &&
                name.equals(RTL_CONSTASCII_STRINGPARAM("manufacturer")));
        // Get the name
        reader.nextAttribute(&nsId, &name);
        assert(nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
                name.equals(RTL_CONSTASCII_STRINGPARAM("name")));
        sManufacturer = reader.getAttributeValue(false).convertFromUtf8();

        for(;;) {
            // Opening label or ending manufacturer
            res = reader.nextItem(
                    xmlreader::XmlReader::TEXT_NONE, &name, &nsId);
            if (res == xmlreader::XmlReader::RESULT_END)
                break;
            assert(res == xmlreader::XmlReader::RESULT_BEGIN &&
                    name.equals(RTL_CONSTASCII_STRINGPARAM("label")));
            // Get name value
            sName = lcl_getValue(reader,
                    xmlreader::Span(RTL_CONSTASCII_STRINGPARAM("name")));
            // Get measure value
            sMeasure = lcl_getValue(reader,
                    xmlreader::Span(RTL_CONSTASCII_STRINGPARAM("measure")));
            // Ending label mark
            lcl_assertEndingItem(reader);
            m_aLabels[sManufacturer][sName] = sMeasure;
        }
        // Get next manufacturer or end
        res = reader.nextItem(
                xmlreader::XmlReader::TEXT_NONE, &name, &nsId);
    };
    res = reader.nextItem(
            xmlreader::XmlReader::TEXT_NONE, &name, &nsId);
    assert(res == xmlreader::XmlReader::RESULT_DONE);

    FillManufacturers();
}

SwLabelConfig::~SwLabelConfig()
{
}

// the config item is not writable ?:
void SwLabelConfig::Commit() {}

void SwLabelConfig::Notify( const ::com::sun::star::uno::Sequence< rtl::OUString >& ) {}

void SwLabelConfig::FillManufacturers()
{
    m_aManufacturers.clear();
    for (std::map< OUString, std::map<OUString, OUString> >::iterator it =
            m_aLabels.begin(); it != m_aLabels.end(); ++it)
        m_aManufacturers.push_back( it->first );

    const com::sun::star::uno::Sequence<rtl::OUString>& rMan = GetNodeNames(OUString());
    const rtl::OUString* pMan = rMan.getConstArray();
    for(sal_Int32 nMan = 0; nMan < rMan.getLength(); nMan++)
        if (m_aLabels.find( pMan[nMan] ) == m_aLabels.end())
            m_aManufacturers.push_back( pMan[nMan] );
}

static Sequence<OUString> lcl_CreatePropertyNames(const OUString& rPrefix)
{
    Sequence<OUString> aProperties(2);
    OUString* pProperties = aProperties.getArray();
    for(sal_Int32 nProp = 0; nProp < 2; nProp++)
        pProperties[nProp] = rPrefix;

    pProperties[ 0] += "Name";
    pProperties[ 1] += "Measure";
    return aProperties;
}

static SwLabRec* lcl_CreateSwLabRec(const OUString& rType, const OUString& rMeasure, const OUString& rManufacturer)
{
    SwLabRec* pNewRec = new SwLabRec;
    pNewRec->aMake = rManufacturer;
    pNewRec->lPWidth = 0;
    pNewRec->lPHeight = 0;
    pNewRec->aType = rType;
    //all values are contained as colon-separated 1/100 mm values
    //except for the continuous flag ('C'/'S')
    String sMeasure(rMeasure);
    sal_uInt16 nTokenCount = comphelper::string::getTokenCount(sMeasure, ';');
    for(sal_uInt16 i = 0; i < nTokenCount; i++)
    {
        String sToken(sMeasure.GetToken(i, ';' ));
        int nVal = sToken.ToInt32();
        switch(i)
        {
            case  0 : pNewRec->bCont = sToken.GetChar(0) == 'C'; break;
            case  1 : pNewRec->lHDist    = MM100_TO_TWIP(nVal);  break;
            case  2 : pNewRec->lVDist    = MM100_TO_TWIP(nVal);  break;
            case  3 : pNewRec->lWidth    = MM100_TO_TWIP(nVal);  break;
            case  4 : pNewRec->lHeight   = MM100_TO_TWIP(nVal);  break;
            case  5 : pNewRec->lLeft     = MM100_TO_TWIP(nVal);  break;
            case  6 : pNewRec->lUpper    = MM100_TO_TWIP(nVal);  break;
            case  7 : pNewRec->nCols     = nVal;                 break;
            case  8 : pNewRec->nRows     = nVal;                 break;
            case  9 : pNewRec->lPWidth   = MM100_TO_TWIP(nVal);  break;
            case 10 : pNewRec->lPHeight  = MM100_TO_TWIP(nVal);  break;
        }
    }
    // lines added for compatibility with custom label defintions saved before patch 44516
    if (pNewRec->lPWidth == 0 || pNewRec->lPHeight == 0)
    {
        // old style definition (no paper dimensions), calculate probable values
        pNewRec->lPWidth = 2 * pNewRec->lLeft + (pNewRec->nCols - 1) * pNewRec->lHDist + pNewRec->lWidth;
        pNewRec->lPHeight = ( pNewRec->bCont ? pNewRec->nRows * pNewRec->lVDist : 2 * pNewRec->lUpper + (pNewRec->nRows - 1) * pNewRec->lVDist + pNewRec->lHeight );
    }
    return pNewRec;
}

static Sequence<PropertyValue> lcl_CreateProperties(
    Sequence<OUString>& rPropNames, const SwLabRec& rRec)
{
    const OUString* pNames = rPropNames.getConstArray();
    Sequence<PropertyValue> aRet(rPropNames.getLength());
    PropertyValue* pValues = aRet.getArray();
    OUString sColon(";");

    for(sal_Int32 nProp = 0; nProp < rPropNames.getLength(); nProp++)
    {
        pValues[nProp].Name = pNames[nProp];
        switch(nProp)
        {
            case 0: pValues[nProp].Value <<= OUString(rRec.aType); break;
            case 1:
            {
                OUString sTmp;
                sTmp += rRec.bCont ? OUString("C") : OUString("S");           sTmp += sColon;
                sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lHDist) );       sTmp += sColon;
                sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lVDist));        sTmp += sColon;
                sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lWidth)  );      sTmp += sColon;
                sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lHeight) );      sTmp += sColon;
                sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lLeft)   );      sTmp += sColon;
                sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lUpper)  );      sTmp += sColon;
                sTmp += OUString::valueOf(rRec.nCols   );                     sTmp += sColon;
                sTmp += OUString::valueOf(rRec.nRows   );                     sTmp += sColon;
                sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lPWidth)  );     sTmp += sColon;
                sTmp += OUString::valueOf(TWIP_TO_MM100(rRec.lPHeight)  );
                pValues[nProp].Value <<= sTmp;
            }
            break;
        }
    }
    return aRet;
}

void    SwLabelConfig::FillLabels(const OUString& rManufacturer, SwLabRecs& rLabArr)
{
    OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
    const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
    const OUString* pLabels = aLabels.getConstArray();
    for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
    {
        OUString sPrefix(sManufacturer);
        sPrefix += "/";
        sPrefix += pLabels[nLabel];
        sPrefix += "/";
        Sequence<OUString> aPropNames = lcl_CreatePropertyNames(sPrefix);
        Sequence<Any>   aValues = GetProperties(aPropNames);
        const Any* pValues = aValues.getConstArray();
        OUString sType;
        OUString sMeasure;
        if (aValues.getLength() >= 1)
            if(pValues[0].hasValue())
                pValues[0] >>= sType;
        if (aValues.getLength() >= 2)
            if(pValues[1].hasValue())
                pValues[1] >>= sMeasure;
        // Remove default value if we have one from configuration
        if(m_aLabels.find(rManufacturer) != m_aLabels.end())
            m_aLabels[rManufacturer].erase(sType);
        rLabArr.push_back( lcl_CreateSwLabRec(sType, sMeasure, rManufacturer) );
    }
    // Add default labels
    if (m_aLabels.find(rManufacturer) == m_aLabels.end())
        return;
    for (std::map<OUString, OUString>::iterator it =
            m_aLabels[rManufacturer].begin();
            it != m_aLabels[rManufacturer].end(); ++it)
        rLabArr.push_back( lcl_CreateSwLabRec(it->first, it->second, rManufacturer) );
}

sal_Bool    SwLabelConfig::HasLabel(const rtl::OUString& rManufacturer, const rtl::OUString& rType)
{
    if (m_aLabels.find(rManufacturer) != m_aLabels.end())
        if (m_aLabels[rManufacturer].find(rType) != m_aLabels[rManufacturer].end())
            return true;

    bool bFound = false;
    for (size_t nNode = 0; nNode < m_aManufacturers.size() && !bFound; nNode++)
    {
        if (m_aManufacturers[nNode] == rManufacturer)
            bFound = true;
    }
    if(bFound)
    {
        OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
        const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
        const OUString* pLabels = aLabels.getConstArray();
        for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
        {
            OUString sPrefix(sManufacturer);
            sPrefix += "/";
            sPrefix += pLabels[nLabel];
            sPrefix += "/";
            Sequence<OUString> aProperties(1);
            aProperties.getArray()[0] = sPrefix;
            aProperties.getArray()[0] += "Name";
            Sequence<Any>   aValues = GetProperties(aProperties);
            const Any* pValues = aValues.getConstArray();
            if(pValues[0].hasValue())
            {
                OUString sTmp;
                pValues[0] >>= sTmp;
                if(rType == sTmp)
                    return sal_True;
            }
        }
    }
    return sal_False;
}

static bool lcl_Exists(const OUString& rNode, const Sequence<OUString>& rLabels)
{
    const OUString* pLabels = rLabels.getConstArray();
    for(sal_Int32 i = 0; i < rLabels.getLength(); i++)
        if(pLabels[i] == rNode)
            return true;
    return false;
}

void SwLabelConfig::SaveLabel(  const rtl::OUString& rManufacturer,
        const rtl::OUString& rType, const SwLabRec& rRec)
{
    bool bFound = false;
    for (size_t nNode = 0; nNode < m_aManufacturers.size() && !bFound; nNode++)
    {
        if (m_aManufacturers[nNode] == rManufacturer)
            bFound = true;
    }
    if(!bFound)
    {
        if(!AddNode(OUString(), rManufacturer))
        {
            OSL_FAIL("New configuration node could not be created");
            return ;
        }
        else
        {
            FillManufacturers();
        }
    }

    OUString sManufacturer(wrapConfigurationElementName(rManufacturer));
    const Sequence<OUString> aLabels = GetNodeNames(sManufacturer);
    const OUString* pLabels = aLabels.getConstArray();
    OUString sFoundNode;
    for(sal_Int32 nLabel = 0; nLabel < aLabels.getLength(); nLabel++)
    {
        OUString sPrefix(sManufacturer);
        sPrefix += "/";
        sPrefix += pLabels[nLabel];
        sPrefix += "/";
        Sequence<OUString> aProperties(1);
        aProperties.getArray()[0] = sPrefix;
        aProperties.getArray()[0] += "Name";
        Sequence<Any>   aValues = GetProperties(aProperties);
        const Any* pValues = aValues.getConstArray();
        if(pValues[0].hasValue())
        {
            OUString sTmp;
            pValues[0] >>= sTmp;
            if(rType == sTmp)
            {
                sFoundNode = pLabels[nLabel];
                break;
            }
        }
    }
    // if not found - generate a unique node name
    if(sFoundNode.isEmpty())
    {
        sal_Int32 nIndex = aLabels.getLength();
        OUString sPrefix("Label");
        sFoundNode = sPrefix;
        sFoundNode += OUString::valueOf(nIndex);
        while(lcl_Exists(sFoundNode, aLabels))
        {
            sFoundNode = sPrefix;
            sFoundNode += OUString::valueOf(nIndex++);
        }
    }
    OUString sPrefix(wrapConfigurationElementName(rManufacturer));
    sPrefix += "/";
    sPrefix += sFoundNode;
    sPrefix += "/";
    Sequence<OUString> aPropNames = lcl_CreatePropertyNames(sPrefix);
    Sequence<PropertyValue> aPropValues = lcl_CreateProperties(aPropNames, rRec);
    SetSetProperties(wrapConfigurationElementName(rManufacturer), aPropValues);

}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */