Item dialog shows correct category in dropdown
[shopper.git] / src / xmlList.cc
blobe392aa67a1ae6ca37c4aa0789f94c490a856ea7a
1 /* Shopper
2 * Copyright (C) 2008 David Greaves <david@dgreaves.com>
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 <QXmlAttributes>
24 #include <QXmlParseException>
25 #include <QXmlSimpleReader>
26 #include <QXmlInputSource>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <stdexcept>
31 #include <list>
32 #include <set>
33 #include <signal.h>
34 #include <cstdlib>
35 #include "shopperList.h"
38 using namespace std;
40 namespace Shopper
42 void abort (QString msg){
43 #ifndef DEBUG_SHOPPER
44 Q_UNUSED(msg)
45 #endif
46 DEBUG(msg);
47 exit(1); // FIXME
50 ListParser::ListParser(List *list):
51 l(list),
52 active_cat_id(0)
56 void ListParser::start_list_el(const QXmlAttributes & attrs)
58 // zero the id counters
59 Category::id_master = 0;
60 Item::id_master = 0;
61 QString tmp("name");
62 for (int at = 0; at != attrs.count(); at++) {
63 QString at_name = attrs.localName(at);
64 QString at_value = attrs.value(at);
65 if (at_name.toLower() == "name") {
66 l->name = at_value;
67 } else if (at_name.toLower() == "state") { // save as int? or human readable?
68 bool ok;
69 l->state = sState(at_value.toInt(&ok));
70 if (!ok) {
71 abort("Converting state failed\n");
73 } else {
74 DEBUG("<list> Unknown attribute : " << at_name << "='" <<at_value<<"'\n");
78 void ListParser::start_cat_el(const QXmlAttributes & attrs)
80 if (l == 0) {
81 DEBUG("<category> Not inside <list>\n");
82 exit(1); // FIXME throw?
84 c = new Category();
85 for (int at = 0; at != attrs.count(); at++) {
86 QString at_name = attrs.localName(at);
87 QString at_value = attrs.value(at);
88 if (at_name.toLower() == "name") {
89 c->name = at_value;
90 } else if (at_name.toLower() == "active") { // is this the category active?
91 if (at_value.toLower() == "true" or at_value == "1")
92 l->active_category = c;
93 } else {
94 DEBUG("<category> Unknown attribute : " << at_name << "='" <<at_value<<"'\n");
97 l->add(*c);
99 void ListParser::start_item_el(const QXmlAttributes & attrs)
101 if (c == 0) {
102 DEBUG("<item> Not inside <category>\n");
103 exit(1); // FIXME
106 i = new Item();
107 i->category=c; // current category
108 for (int at = 0; at != attrs.count(); at++) {
109 QString at_name = attrs.localName(at);
110 QString at_value = attrs.value(at);
111 if (at_name.toLower() == "desc") {
112 i->desc = at_value;
113 } else if (at_name.toLower() == "note") {
114 i->note = at_value;
115 } else if (at_name.toLower() == "wanted") {
116 i->set_wanted(at_value.toLower() == "true" or at_value == "1");
117 } else if (at_name.toLower() == "bought") {
118 i->set_bought(at_value.toLower() == "true" or at_value == "1");
119 } else {
120 DEBUG("<item> Unknown attribute : " << at_name << "='" <<at_value<<"'\n");
123 l->add(*i);
126 bool ListParser::startElement (const QString & namespaceURI,
127 const QString & el,
128 const QString & qName,
129 const QXmlAttributes & attrs)
131 Q_UNUSED(namespaceURI)
132 Q_UNUSED(qName)
133 DEBUG("adding " << el << "\n");
134 if (el.toLower() == "list") {
135 start_list_el(attrs);
136 } else if (el.toLower() == "category") {
137 start_cat_el(attrs);
138 } else if (el.toLower() == "item") {
139 start_item_el(attrs);
140 } else {
141 throw ;
143 return true;
146 bool ListParser::endElement (const QString & namespaceURI,
147 const QString & el,
148 const QString & qName)
150 Q_UNUSED(namespaceURI)
151 Q_UNUSED(qName)
152 DEBUG("done " << el << "\n");
153 if (el.toLower() == "list") {
154 l->resequence(); // Ensure the id's are sensible FIXME : not needed?
155 l = 0; // No current list
156 } else if (el.toLower() == "category") {
157 // add the created cat to list
158 c->dbg();
159 c->items.sort(cmpItem);
160 c=0; // No current category
161 } else if (el.toLower() == "item") {
162 // add the created item to list
163 i->dbg();
164 DEBUG("Assigned " << i->desc << " to category " << i->category->name << "\n");
165 i = 0; // No current item
166 } else {
167 return false;
169 return true;
171 bool ListParser::fatalError ( const QXmlParseException & exception )
173 Q_UNUSED(exception)
174 DEBUG("Markup error\n");
175 return true;
178 void ListParser::from_string(QString xml)
180 QXmlSimpleReader xmlReader;
181 xmlReader.setContentHandler(this);
182 xmlReader.setErrorHandler(this);
183 QXmlInputSource source;
184 source.setData(xml);
185 bool ok = xmlReader.parse(&source);
186 if (!ok)
187 std::cout << "Parsing failed." << std::endl;
188 DEBUG("Parsing EXIT\n");
191 ////////////////////////////////////////////////////////////////
192 XMLWriter::XMLWriter(const Shopper::List *list) :
193 l(list)
197 ostream& XMLWriter::write(ostream &out)
199 DEBUG("in write\n");
200 out << "<list name='"<< qPrintable(l->name)
201 << "' state='" << l->state
202 << "'>\n";
203 for (List::pCategoryIter c = l->categories.begin(); c != l->categories.end(); c++) {
204 out << " <category"
205 << ((l->active_category == *c) ? " active='1' " : "")
206 << " name='" << qPrintable((*c)->name)
207 << "' id='" << QString::number((*c)->id)
208 << "'>\n";
209 for (Category::pItemIter i = (*c)->items.begin(); i != (*c)->items.end(); i++) {
210 out << " <item"
211 << " wanted='" << (*i)->wanted
212 << "' bought='" << (*i)->bought
213 << "' desc='" << qPrintable((*i)->desc) // FIXME: Escape quotes etc
214 << "' note='" << qPrintable((*i)->note) // FIXME: Escape quotes etc
215 << "'/>\n";
217 out << " </category>\n";
219 out << "</list>\n";
220 DEBUG("done write\n");
221 return (out);