Adding new category now shows and selects newly added category
[shopper.git] / src / xmlList.cc
blob86b89b797bc4f598a85aa4c818e1b46ae511cb54
1 /* Shopper
2 * Copyright (C) 2008 David Greaves
4 * This software is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This software is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this software; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA
21 //#define DEBUG_SHOPPER 1
22 #include "shopper.h" // automake, i8n, gettext
23 #include <QtXml>
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <stdexcept>
28 #include <list>
29 #include <set>
30 #include <signal.h>
31 #include "shopperList.h"
34 using namespace std;
36 namespace Shopper
38 void abort (QString msg){
39 DEBUG(msg);
40 exit(1); // FIXME
43 ListParser::ListParser(List *list):
44 l(list)
48 void ListParser::start_list_el(const QXmlAttributes & attrs)
50 // zero the id counters
51 Category::id_master = 0;
52 Item::id_master = 0;
53 QString tmp("name");
54 for (int at = 0; at != attrs.count(); at++) {
55 QString at_name = attrs.localName(at);
56 QString at_value = attrs.value(at);
57 if (at_name.toLower() == "name") {
58 l->name = at_value;
59 } else if (at_name.toLower() == "state") { // save as int? or human readable?
60 bool ok;
61 l->state = sState(at_value.toInt(&ok));
62 if (!ok) {
63 abort("Converting state failed\n");
65 } else {
66 DEBUG("<list> Unknown attribute : " << at_name << "='" <<at_value<<"'\n");
70 void ListParser::start_cat_el(const QXmlAttributes & attrs)
72 if (l == NULL) {
73 DEBUG("<category> Not inside <list>\n");
74 exit(1); // FIXME throw?
76 c = new Category();
77 for (int at = 0; at != attrs.count(); at++) {
78 QString at_name = attrs.localName(at);
79 QString at_value = attrs.value(at);
80 if (at_name.toLower() == "name") {
81 c->name = at_value;
82 } else if (at_name.toLower() == "active") { // is this the category active?
83 if (at_value.toLower() == "true" or at_value == "1")
84 l->active_category = c;
85 } else {
86 DEBUG("<category> Unknown attribute : " << at_name << "='" <<at_value<<"'\n");
89 l->add(*c);
91 void ListParser::start_item_el(const QXmlAttributes & attrs)
93 if (c == NULL) {
94 DEBUG("<item> Not inside <category>\n");
95 exit(1); // FIXME
98 i = new Item();
99 i->category=c; // current category
100 for (int at = 0; at != attrs.count(); at++) {
101 QString at_name = attrs.localName(at);
102 QString at_value = attrs.value(at);
103 if (at_name.toLower() == "desc") {
104 i->desc = at_value;
105 } else if (at_name.toLower() == "note") {
106 i->note = at_value;
107 } else if (at_name.toLower() == "wanted") {
108 i->set_wanted(at_value.toLower() == "true" or at_value == "1");
109 } else if (at_name.toLower() == "bought") {
110 i->set_bought(at_value.toLower() == "true" or at_value == "1");
111 } else {
112 DEBUG("<item> Unknown attribute : " << at_name << "='" <<at_value<<"'\n");
115 l->add(*i);
118 bool ListParser::startElement (const QString & namespaceURI,
119 const QString & el,
120 const QString & qName,
121 const QXmlAttributes & attrs)
123 DEBUG("adding " << el << "\n");
124 if (el.toLower() == "list") {
125 start_list_el(attrs);
126 } else if (el.toLower() == "category") {
127 start_cat_el(attrs);
128 } else if (el.toLower() == "item") {
129 start_item_el(attrs);
130 } else {
131 throw ;
133 return true;
136 bool ListParser::endElement (const QString & namespaceURI,
137 const QString & el,
138 const QString & qName)
140 DEBUG("done " << el << "\n");
141 if (el.toLower() == "list") {
142 l->resequence(); // Ensure the id's are sensible FIXME : not needed?
143 l = NULL; // No current list
144 } else if (el.toLower() == "category") {
145 // add the created cat to list
146 c->dbg();
147 c=NULL; // No current category
148 } else if (el.toLower() == "item") {
149 // add the created item to list
150 i->dbg();
151 DEBUG("Assigned " << i->desc << " to category " << i->category->name << "\n");
152 i = NULL; // No current item
153 } else {
154 return false;
156 return true;
158 bool ListParser::fatalError ( const QXmlParseException & exception )
160 DEBUG("Markup error\n");
161 return true;
164 void ListParser::from_string(QString xml)
166 QXmlSimpleReader xmlReader;
167 xmlReader.setContentHandler(this);
168 xmlReader.setErrorHandler(this);
169 QXmlInputSource source;
170 source.setData(xml);
171 bool ok = xmlReader.parse(&source);
172 if (!ok)
173 std::cout << "Parsing failed." << std::endl;
174 DEBUG("Parsing EXIT\n");
177 ////////////////////////////////////////////////////////////////
178 XMLWriter::XMLWriter(const Shopper::List *list) :
179 l(list)
183 ostream& XMLWriter::write(ostream &out)
185 DEBUG("in write\n");
186 out << "<list name='"<< qPrintable(l->name)
187 << "' state='" << l->state
188 << "'>\n";
189 for (List::pCategoryIter c = l->categories.begin(); c != l->categories.end(); c++) {
190 out << " <category"
191 << ((l->active_category == *c) ? " active='1' " : "")
192 << " name='" << qPrintable((*c)->name)
193 << "'>\n";
194 for (Category::pItemIter i = (*c)->items.begin(); i != (*c)->items.end(); i++) {
195 out << " <item"
196 << " wanted='" << (*i)->wanted
197 << "' bought='" << (*i)->bought
198 << "' desc='" << qPrintable((*i)->desc) // FIXME: Escape quotes etc
199 << "' note='" << qPrintable((*i)->note) // FIXME: Escape quotes etc
200 << "'/>\n";
202 out << " </category>\n";
204 out << "</list>\n";
205 DEBUG("done write\n");
206 return (out);