summaryrefslogtreecommitdiff
path: root/core/pim/datebook/holiday/national/nationalcfg.cpp
Side-by-side diff
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 @@
+#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;
+}