File: InfoVariable.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 261,632 kB
  • sloc: cpp: 650,836; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 17
file content (119 lines) | stat: -rw-r--r-- 3,676 bytes parent folder | download | duplicates (3)
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
/* This file is part of the KDE project
 * Copyright (C) 2007 Pierre Ducroquet <pinaraf@gmail.com>
 * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "InfoVariable.h"

#include "VariablesDebug.h"

#include <KoProperties.h>
#include <KoShapeSavingContext.h>
#include <KoXmlReader.h>
#include <KoXmlWriter.h>

#include <QGlobalStatic>

static const struct {
    KoInlineObject::Property property;
    const char * tag;
    const char * saveTag;
} propertyData[] = {
    { KoInlineObject::AuthorName, "creator", "text:creator" },
    { KoInlineObject::DocumentURL, "file-name", "text:file-name" },
    { KoInlineObject::Title, "title", "text:title" },
    { KoInlineObject::Subject, "subject", "text:subject" },
    { KoInlineObject::Keywords, "keywords", "text:keywords" },
    //{ KoInlineObject::Description, "description", "text:description" }
    { KoInlineObject::Comments, "comments", "text:comments" }
};

static const unsigned int numPropertyData = sizeof(propertyData) / sizeof(*propertyData);

QStringList InfoVariable::tags()
{
    QStringList tagList;
    for (unsigned int i = 0; i < numPropertyData; ++i) {
        tagList << propertyData[i].tag;
    }
    return tagList;
}

InfoVariable::InfoVariable()
        : KoVariable(true),
        m_type(KoInlineObject::AuthorName)
{
}

void InfoVariable::readProperties(const KoProperties *props)
{
    m_type = (Property) props->property("vartype").value<int>();
}

void InfoVariable::propertyChanged(Property property, const QVariant &value)
{
    if (property == m_type) {
        setValue(value.toString());
    }
}

typedef QMap<KoInlineObject::Property, const char*> SaveMap;

Q_GLOBAL_STATIC(SaveMap, s_saveInfo)

void InfoVariable::saveOdf(KoShapeSavingContext & context)
{
    KoXmlWriter *writer = &context.xmlWriter();

    if (!s_saveInfo.exists() ) {
        for (unsigned int i = 0; i < numPropertyData; ++i) {
            s_saveInfo->insert(propertyData[i].property, propertyData[i].saveTag);
        }
    }
    const char *nodeName = s_saveInfo->value(m_type, 0);
    if (nodeName) {
        writer->startElement(nodeName, false);
        writer->addTextNode(value());
        writer->endElement();
    }
}

typedef QMap<QString, KoInlineObject::Property> LoadMap;

Q_GLOBAL_STATIC(LoadMap, s_loadInfo)

bool InfoVariable::loadOdf(const KoXmlElement & element, KoShapeLoadingContext & /*context*/)
{
    if (!s_loadInfo.exists() ) {
        for (unsigned int i = 0; i < numPropertyData; ++i) {
            s_loadInfo->insert(propertyData[i].tag, propertyData[i].property);
        }
    }

    const QString localName(element.localName());
    m_type = s_loadInfo->value(localName);

    for(KoXmlNode node = element.firstChild(); !node.isNull(); node = node.nextSibling() ) {
        if (node.isText()) {
            setValue(node.toText().data());
            break;
        }
    }

    return true;
}