summaryrefslogtreecommitdiff
path: root/core/pim/datebook/holiday/national/nationalcfg.cpp
Unidiff
Diffstat (limited to 'core/pim/datebook/holiday/national/nationalcfg.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/pim/datebook/holiday/national/nationalcfg.cpp149
1 files changed, 149 insertions, 0 deletions
diff --git a/core/pim/datebook/holiday/national/nationalcfg.cpp b/core/pim/datebook/holiday/national/nationalcfg.cpp
new file mode 100644
index 0000000..a293251
--- a/dev/null
+++ b/core/pim/datebook/holiday/national/nationalcfg.cpp
@@ -0,0 +1,149 @@
1#include "nationalcfg.h"
2
3#include <opie2/odebug.h>
4
5#include <qfile.h>
6
7static QString _key_desc="description";
8static QString _key_doc="nationaldays";
9static QString _key_list="entries";
10static QString _key_entry="entry";
11static QString _content_name="name";
12static QString _content_date="date";
13
14NHcfg::NHcfg()
15 :QXmlDefaultHandler(),err(""),_path("")
16{
17}
18
19NHcfg::~NHcfg()
20{
21}
22
23bool NHcfg::load(const QString&aPath)
24{
25 _path=aPath;
26 stage = 0;
27 _content.clear();
28 odebug << "Start loading file "<<_path<<oendl;
29 QFile *f=new QFile(_path);
30 if (!f) {
31 oerr << "Could not open file" << oendl;
32 return false;
33 }
34 odebug << "Source" << oendl;
35 QXmlInputSource is(*f);
36 odebug << "Reader" << oendl;
37 QXmlSimpleReader reader;
38 odebug << "Handler" << oendl;
39 reader.setContentHandler(this);
40 odebug << "Error handler" << oendl;
41 reader.setErrorHandler(this);
42
43 err = "";
44 odebug << "parse it" << oendl;
45 bool ret = reader.parse(is);
46 odebug << "Errors: " << err << oendl;
47 return ret;
48}
49
50const tholidaylist&NHcfg::days()const
51{
52 return _content;
53}
54
55bool NHcfg::warning(const QXmlParseException& e)
56{
57 QString tmp;
58
59 tmp.sprintf("%d: warning: %s\n", e.lineNumber(),
60 (const char*) e.message().utf8());
61
62 err += tmp;
63
64 return true;
65}
66
67bool NHcfg::error(const QXmlParseException& e)
68{
69 QString tmp;
70
71 tmp.sprintf("%d: error: %s\n", e.lineNumber(),
72 (const char*) e.message().utf8());
73
74 err += tmp;
75
76 return true;
77}
78
79bool NHcfg::fatalError(const QXmlParseException& e)
80{
81 QString tmp;
82
83 tmp.sprintf("%d: fatal error: %s\n", e.lineNumber(),
84 (const char*) e.message().utf8());
85
86 err += tmp;
87
88 return false;
89}
90
91bool NHcfg::startElement(const QString&, const QString&,const QString& name, const QXmlAttributes& attr)
92{
93 bool ret = false;
94 odebug << "startElement: " << name << oendl;
95 if (name==_key_doc) {
96 stage = 1;
97 return true;
98 }
99 if (stage == 0) {
100 err = "This is not a national holiday config file";
101 return false;
102 }
103 if (name==_key_desc) {
104 stage = 2;
105 ret = setName(attr);
106 return ret;
107 }
108 if (stage<2) {return false;}
109 if (name==_key_list) {stage=3;return true;}
110 if (stage<3) {return false;}
111 return parsevalue(name,attr);
112}
113
114bool NHcfg::parsevalue(const QString&name,const QXmlAttributes&attr)
115{
116 int nindex = attr.index(_content_name);
117 int dindex = attr.index(_content_date);
118 if (name != _key_entry) {err = "Not a valid entry"; return false;}
119 if (dindex == -1 || nindex == -1) {err = QString("Listentry %i is invalid").arg(1);return false;}
120 QString txt = attr.value(nindex);
121 QString dstring = attr.value(dindex);
122 QStringList e = QStringList::split("-",dstring);
123 if (e.count()!=2){err=QString("Datestring %1 is invalid").arg(dstring);return false;}
124 QDate d(0,e[0].toInt(),e[1].toInt());
125 odebug << "Found entry \"" << txt<<"\" on "<<d<<oendl;
126 _content[d].append(txt);
127 return true;
128}
129
130bool NHcfg::endElement(const QString&, const QString&,const QString& name)
131{
132 return true;
133}
134
135const QString&NHcfg::errorString()const
136{
137 return err;
138}
139
140bool NHcfg::setName(const QXmlAttributes&attr)
141{
142 int nindx = attr.index("value");
143 if (nindx==-1) {
144 return false;
145 }
146 _contentname = attr.value(nindx);
147 odebug << "Contentname = " << _contentname<<oendl;
148 return true;
149}