Kitlist  1.1.0
xmldao.cpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of Kitlist, a program to maintain a simple list
4  of items and assign items to one or more categories.
5 
6  Copyright (C) 2008,2009 Frank Dean
7 
8  Kitlist is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Kitlist is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Kitlist. If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #include <algorithm>
24 #include <iostream>
25 #include <sstream>
26 
27 #include "xmldao.hpp"
28 #include "kitparser.hpp"
29 
30 using namespace std;
31 
32 
47  KitModel* retval = new KitModel();
48  try {
49  if (m_filename.size() > 0) {
50  KitParser parser(*retval);
51  parser.set_validate();
52  parser.set_substitute_entities();
53  parser.parse_file(m_filename);
54  // Clear the dirty, deleted and new flags etc.
55  }
56  ItemContainer* items = retval->get_all_items();
57  ItemIter i = max_element(items->begin(), items->end(), ItemCompareId());
58  if (i != items->end()) {
59  Item* max_item = (*i);
60  m_max_item_id = max_item->get_id();
61  } else {
62  m_max_item_id = -1;
63  }
64  delete items;
65  CategoryContainer* categories = retval->get_categories();
66  CategoryIter cat = std::max_element(categories->begin(), categories->end(), CategoryCompareId());
67  if (cat != categories->end()) {
68  Category* max_category = (*cat);
69  m_max_category_id = max_category->get_id();
70  } else {
71  m_max_category_id = -1;
72  }
73  delete categories;
74  retval->reset();
75  } catch (const xmlpp::exception& ex) {
76  std::cout << "libxml++ exception: " << ex.what() << std::endl;
77  }
78  return retval;
79 }
80 
81 
88  assert(m_items_node != NULL);
89  if (!item.is_deleted()) {
90  xmlpp::Element* itemNode = m_items_node->add_child("item");
91  ostringstream os;
92  os << item.get_id();
93  itemNode->set_attribute("id", os.str());
94  itemNode->set_attribute("checked", item.get_checked() ? "true" : "false");
95  itemNode->set_child_text(item.get_description());
96  }
97  return false;
98 }
99 
100 
107  assert(m_cat_items_node != NULL);
108  if (!((ModelItem&) item).is_deleted()) {
109  xmlpp::Element* itemNode = m_cat_items_node->add_child("category-item");
110  ostringstream os;
111  os << item.get_id();
112  itemNode->set_attribute("id", os.str());
113  }
114  return false;
115 }
116 
117 
124  assert(m_categories_node != NULL);
125  if (!category.is_deleted()) {
126  xmlpp::Element* catNode = m_categories_node->add_child("category");
127  ostringstream os;
128  os << category.get_id();
129  catNode->set_attribute("id", os.str());
130  xmlpp::Element* catNameNode = catNode->add_child("category-name");
131  catNameNode->set_child_text(category.get_name());
132  m_cat_items_node = catNode->add_child("category-items");
133  category.foreach_item( sigc::mem_fun(*this, &XmlDao::add_category_item_to_dom) );
134  }
135  return false;
136 }
137 
138 
143  return ++m_max_item_id;
144 }
145 
146 
151  return ++m_max_category_id;
152 }
153 
154 
164  xmlpp::Document document;
165  xmlpp::Element* rootNode = document.create_root_node("kitlist");
166  m_items_node = rootNode->add_child("items");
167  model->foreach_item( sigc::mem_fun(*this, &XmlDao::add_item_to_dom) );
168  m_categories_node = rootNode->add_child("categories");
169  model->foreach_category( sigc::mem_fun(*this, &XmlDao::add_category_to_dom) );
170  document.write_to_file_formatted(m_filename);
171 #ifdef XML_DAO
172  // Remove deleted items from categories etc.
173  model->purge();
174  // Clear the dirty, deleted and new flags etc.
175  model->reset();
176 #endif
177 }
long get_id()
Definition: item.hpp:46
std::string get_name()
Definition: category.hpp:47
long get_next_category_id()
Definition: xmldao.cpp:150
Represents a Category combined with GuiState attributes.
Definition: kitmodel.hpp:94
SaxParser implementation for reading the KitModel from an XML document.
Definition: kitparser.hpp:35
ItemContainer::iterator ItemIter
Definition: item.hpp:92
bool add_category_item_to_dom(Item &item)
Adds the passed item to the current category&#39;s node.
Definition: xmldao.cpp:106
CategoryContainer::iterator CategoryIter
Definition: category.hpp:85
void foreach_category(const SlotForeachCategory &slot)
Calls the provided slot for each category in the map.
Definition: kitmodel.cpp:254
STL namespace.
Comparator used for comparing Items by id.
Definition: item.hpp:72
std::vector< Item * > ItemContainer
Definition: item.hpp:91
void foreach_item(const SlotForeachItem &slot)
Executes a callback function for each associated item.
Definition: category.cpp:55
void save_model(KitModel *model)
Saves the model as an XML document.
Definition: xmldao.cpp:163
KitModel * get_model()
Loads the data model from the previously set filename.
Definition: xmldao.cpp:46
bool add_item_to_dom(ModelItem &item)
Adds the passed item to the current items&#39; node.
Definition: xmldao.cpp:87
virtual ItemContainer * get_all_items()
Returns a list of all items.
Definition: kitmodel.cpp:327
Represents an Item.
Definition: item.hpp:37
bool add_category_to_dom(ModelCategory &item)
Adds the passed item to the current categories&#39; node.
Definition: xmldao.cpp:123
bool is_deleted()
Definition: kitmodel.hpp:47
Represents a Category.
Definition: category.hpp:37
std::string get_description()
Definition: item.hpp:48
Represents an Item combined with GuiState attributes.
Definition: kitmodel.hpp:60
Holds a rich graph of objects representing the application&#39;s data model.
Definition: kitmodel.hpp:135
bool get_checked()
Definition: item.hpp:50
virtual CategoryContainer * get_categories()
Returns a list of all categories.
Definition: kitmodel.cpp:300
long get_id()
Definition: category.hpp:45
void foreach_item(const SlotForeachModelItem &slot)
Calls the provided slot for each item in the map.
Definition: kitmodel.cpp:230
std::vector< Category * > CategoryContainer
Definition: category.hpp:84
virtual void purge()
Purges deleted categories and items from the model.
Definition: kitmodel.cpp:486
virtual void reset()
Resets all contained objects to their default states.
Definition: kitmodel.cpp:466
long get_next_item_id()
Returns the next unused unique id for items.
Definition: xmldao.cpp:142
Comparator used for comparing Categories by id.
Definition: category.hpp:77

Copyright 2008-2021 Frank Dean