summaryrefslogtreecommitdiff
path: root/core/pim/datebook/holiday/national/nationalcfg.cpp
blob: a29325113f72cdf36d87599fe15a3ee1c877032d (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
#include "nationalcfg.h"

#include <opie2/odebug.h>

#include <qfile.h>

static QString _key_desc="description";
static QString _key_doc="nationaldays";
static QString _key_list="entries";
static QString _key_entry="entry";
static QString _content_name="name";
static QString _content_date="date";

NHcfg::NHcfg()
    :QXmlDefaultHandler(),err(""),_path("")
{
}

NHcfg::~NHcfg()
{
}

bool NHcfg::load(const QString&aPath)
{
    _path=aPath;
    stage = 0;
    _content.clear();
    odebug << "Start loading file "<<_path<<oendl;
    QFile *f=new QFile(_path);
    if (!f) {
        oerr << "Could not open file" << oendl;
        return false;
    }
    odebug << "Source" << oendl;
    QXmlInputSource is(*f);
    odebug << "Reader" << oendl;
    QXmlSimpleReader reader;
    odebug << "Handler" << oendl;
    reader.setContentHandler(this);
    odebug << "Error handler" << oendl;
    reader.setErrorHandler(this);

    err = "";
    odebug << "parse it" << oendl;
    bool ret = reader.parse(is);
    odebug << "Errors: " << err << oendl;
    return ret;
}

const tholidaylist&NHcfg::days()const
{
    return _content;
}

bool NHcfg::warning(const QXmlParseException& e)
{
        QString tmp;

        tmp.sprintf("%d: warning: %s\n", e.lineNumber(),
                (const char*) e.message().utf8());

        err += tmp;

        return true;
}

bool NHcfg::error(const QXmlParseException& e)
{
        QString tmp;

        tmp.sprintf("%d: error: %s\n", e.lineNumber(),
                (const char*) e.message().utf8());

        err += tmp;

        return true;
}

bool NHcfg::fatalError(const QXmlParseException& e)
{
        QString tmp;

        tmp.sprintf("%d: fatal error: %s\n", e.lineNumber(),
                (const char*) e.message().utf8());

        err += tmp;

        return false;
}

bool NHcfg::startElement(const QString&, const QString&,const QString& name, const QXmlAttributes& attr)
{
    bool ret = false;
    odebug << "startElement: " << name << oendl;
    if (name==_key_doc) {
        stage = 1;
        return true;
    }
    if (stage == 0) {
        err = "This is not a national holiday config file";
        return false;
    }
    if (name==_key_desc) {
        stage = 2;
        ret = setName(attr);
        return ret;
    }
    if (stage<2) {return false;}
    if (name==_key_list) {stage=3;return true;}
    if (stage<3) {return false;}
    return parsevalue(name,attr);
}

bool NHcfg::parsevalue(const QString&name,const QXmlAttributes&attr)
{
    int nindex = attr.index(_content_name);
    int dindex = attr.index(_content_date);
    if (name != _key_entry) {err = "Not a valid entry"; return false;}
    if (dindex == -1 || nindex == -1) {err = QString("Listentry %i is invalid").arg(1);return false;}
    QString txt = attr.value(nindex);
    QString dstring = attr.value(dindex);
    QStringList e = QStringList::split("-",dstring);
    if (e.count()!=2){err=QString("Datestring %1 is invalid").arg(dstring);return false;}
    QDate d(0,e[0].toInt(),e[1].toInt());
    odebug << "Found entry \"" << txt<<"\" on "<<d<<oendl;
    _content[d].append(txt);
    return true;
}

bool NHcfg::endElement(const QString&, const QString&,const QString& name)
{
    return true;
}

const QString&NHcfg::errorString()const
{
    return err;
}

bool NHcfg::setName(const QXmlAttributes&attr)
{
    int nindx = attr.index("value");
    if (nindx==-1) {
        return false;
    }
    _contentname = attr.value(nindx);
    odebug << "Contentname = " << _contentname<<oendl;
    return true;
}