Kitlist  1.1.0
service.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 "service.hpp"
24 #include <algorithm>
25 #include <cassert>
26 #include <iostream>
27 
28 using namespace std;
29 
30 class FilterItem : public ItemFunctor {
32 public:
33  FilterItem(KitModel& model) : ItemFunctor(), m_model(model) {}
34  bool operator()(Item& item) {
35  return m_model.filter(item.get_checked());
36  }
37 
38 };
39 
40 
44 Service::Service(KitListDao& dao) : m_dao(dao), m_model(0) {
45  m_model = load_model();
46 }
47 
48 
50  if (m_model)
51  delete m_model;
52 }
53 
54 
59  return m_dao.get_model();
60 }
61 
62 
70 void Service::open_as_xml(const Glib::ustring& filename) {
71 #ifdef XML_DAO
72  delete m_model;
73  m_model = ((XmlDao&) m_dao).get_model(filename);
74 #endif
75 }
76 
77 
84  delete m_model;
85 #ifdef XML_DAO
86  ((XmlDao&) m_dao).set_filename("");
88 #else
89  m_model = new KitModel();
90  m_model->reset();
91 #endif
92 }
93 
94 
98 void Service::save() {
99  assert(m_model);
101 }
102 
103 
111 void Service::save_as_xml(const Glib::ustring& filename) {
112 #ifdef XML_DAO
113  ((XmlDao&) m_dao).save_model(m_model, filename);
114 #else
115  // Only want a new DAO when the current DAO isn't XmlDao
116  XmlDao xmldao;
117  xmldao.save_model(m_model, filename);
118 #endif
119 }
120 
121 
126  return m_model->find_item(id);
127 }
128 
129 
134  return m_model->find_category(cat_id);
135 }
136 
137 
144 void Service::copy_items(const ModelItemContainer& items, long cat_id) {
145  assert(m_model);
146  m_model->set_dirty(true);
147  m_model->copy_items(items, cat_id);
148 }
149 
150 
159 Item* Service::create_item(long cat_id) {
160  assert(m_model);
161  m_model->set_dirty(true);
162  ModelItem* item = new ModelItem;
163  item->set_id(get_next_item_id());
164  item->set_new_flag(true);
165  item->set_dirty(true);
166  if (cat_id < 0)
167  m_model->add_item(item);
168  else
169  m_model->add_item(item, cat_id);
170  return item;
171 }
172 
173 
180 bool Service::delete_item(long id) {
181  bool retval = false;
182  assert(m_model);
183  ModelItem* item = m_model->find_item(id);
184  if (item) {
185  m_model->set_dirty(true);
186  item->set_dirty(true);
187  item->set_deleted(true);
188  retval = true;
189  }
190  return retval;
191 }
192 
193 
200 bool Service::delete_category(long cat_id) {
201  bool retval = false;
202  assert(m_model);
203  ModelCategory* category = m_model->find_category(cat_id);
204  if (category) {
205  m_model->set_dirty(true);
206  category->set_dirty(true);
207  category->set_deleted(true);
208  retval = true;
209  }
210  return retval;
211 }
212 
213 
221  assert(m_model);
222  m_model->set_dirty(true);
223  ModelCategory* category = new ModelCategory;
224  category->set_id(get_next_category_id());
225  category->set_new_flag(true);
226  category->set_dirty(true);
227  m_model->add_category(category);
228  return category;
229 }
230 
231 
238 void Service::select_items(ModelItemContainer* items, bool checked) {
239  m_model->set_dirty();
240  for (ModelItemIter i = items->begin(); i != items->end(); ++i) {
241  (*i)->set_dirty();
242  (*i)->set_checked(checked);
243  }
244 }
245 
246 
253  m_model->set_dirty();
254  for (ModelItemIter i = items->begin(); i != items->end(); ++i) {
255  (*i)->set_dirty();
256  (*i)->set_checked(!(*i)->get_checked());
257  }
258 }
259 
260 
264 void Service::set_model_dirty(bool flag) {
265  assert(m_model);
266  if (m_model)
267  m_model->set_dirty(flag);
268 }
269 
270 
282 bool Service::update_item(long id, const string description, bool checked) {
283  bool retval = false;
284  assert(m_model);
285  ModelItem* item = m_model->find_item(id);
286  if (item) {
287  item->set_description(description);
288  item->set_checked(checked);
289  item->set_dirty(true);
290  retval = true;
291  }
292  return retval;
293 }
294 
295 
306  assert(m_model);
307  ItemContainer* retval = 0;
308  if (cat_id == -1) {
309  retval = m_model->get_all_items();
310  } else {
311  ModelCategory* cat = m_model->find_category(cat_id);
312  if (cat) {
313  retval = cat->get_items();
314  }
315  }
316  return retval;
317 }
318 
319 
332  assert(m_model);
333  ItemContainer* retval = 0;
335  if (cat_id == -1) {
336  retval = m_model->get_all_items(fi);
337  } else {
338  ModelCategory* cat = m_model->find_category(cat_id);
339  if (cat) {
340  retval = cat->get_items(fi);
341  }
342  }
343  if (retval)
344  sort(retval->begin(), retval->end(), ItemCompareName());
345  return retval;
346 }
347 
348 
355  assert(m_model);
357  return retval;
358 }
359 
360 
365  return m_dao.get_next_item_id();
366 }
367 
368 
373  return m_dao.get_next_category_id();
374 }
virtual bool filter(bool checked)
Applies the current filter.
Definition: kitmodel.cpp:446
ModelItemContainer::iterator ModelItemIter
Definition: kitmodel.hpp:82
ModelCategory * find_category(long cat_id)
Definition: service.cpp:133
Category * create_category()
Creates a new category.
Definition: service.cpp:220
Represents a Category combined with GuiState attributes.
Definition: kitmodel.hpp:94
virtual void save_model(KitModel *model)=0
Saves the current data model.
virtual void select_items(ModelItemContainer *items, bool checked=true)
Checks or unchecks all the passed items.
Definition: service.cpp:238
virtual void set_dirty(bool dirty=true)
Definition: kitmodel.hpp:176
virtual long get_next_category_id()=0
void set_dirty(bool dirty=true)
Definition: kitmodel.hpp:46
void set_new_flag(bool flag)
Definition: kitmodel.hpp:49
STL namespace.
virtual void add_category(ModelCategory *category)
Add a category to the model.
Definition: kitmodel.cpp:361
virtual ModelItem * find_item(long id)
Finds an item by it&#39;s unique ID.
Definition: kitmodel.cpp:278
std::vector< Item * > ItemContainer
Definition: item.hpp:91
virtual ModelCategory * find_category(long id)
Finds a Category by it&#39;s unique ID.
Definition: kitmodel.cpp:265
KitModel & m_model
Definition: service.cpp:31
void save_model(KitModel *model)
Saves the model as an XML document.
Definition: xmldao.cpp:163
virtual ItemContainer * get_items()
Returns the list of items belonging to the Category.
Definition: kitmodel.cpp:151
~Service()
Definition: service.cpp:49
void open_as_xml(const Glib::ustring &filename)
Loads a new data model from the named XML document.
Definition: service.cpp:70
Comparator used for sorting Items by name.
Definition: item.hpp:61
Service(KitListDao &dao)
Loads the data model from the persistence store.
Definition: service.cpp:44
virtual KitModel * get_model()=0
Loads the data model.
ItemContainer * get_items(long cat_id=-1)
Returns a list of items.
Definition: service.cpp:305
std::vector< ModelItem * > ModelItemContainer
Definition: kitmodel.hpp:81
Item * create_item(long cat_id)
Creates a new ModelItem and associates it with the specified category.
Definition: service.cpp:159
bool delete_category(long cat_id)
Flags a category as deleted.
Definition: service.cpp:200
virtual ItemContainer * get_all_items()
Returns a list of all items.
Definition: kitmodel.cpp:327
void set_deleted(bool deleted)
Definition: kitmodel.hpp:48
virtual long get_next_item_id()=0
KitModel * load_model()
Loads the data model from the persistence store.
Definition: service.cpp:58
Defines the methods that an implementation of this class must implement.
Definition: kitlistdao.hpp:46
void set_id(long id)
Definition: category.hpp:44
Represents an Item.
Definition: item.hpp:37
void save_as_xml(const Glib::ustring &filename)
Saves the model&#39;s state to an XML document.
Definition: service.cpp:111
CategoryContainer * get_categories()
Returns a list of all categories.
Definition: service.cpp:354
bool update_item(long id, const std::string description, bool checked)
Updates the attributes of an item.
Definition: service.cpp:282
void save()
Definition: service.cpp:98
Represents a Category.
Definition: category.hpp:37
void set_id(long id)
Definition: item.hpp:45
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
virtual void toggle_selected_items(ModelItemContainer *items)
Toggles the checked state of all the passed items.
Definition: service.cpp:252
void copy_items(const ModelItemContainer &items, long cat_id)
Copies items to the specified category.
Definition: service.cpp:144
bool delete_item(long id)
Flags an item as deleted.
Definition: service.cpp:180
bool get_checked()
Definition: item.hpp:50
virtual CategoryContainer * get_categories()
Returns a list of all categories.
Definition: kitmodel.cpp:300
Implementation of a KitListDao using XML as the persistence store.
Definition: xmldao.hpp:42
virtual void set_checked(bool checked)
Definition: kitmodel.hpp:64
ItemContainer * get_filtered_items(long cat_id=-1)
Returns a list of items, applying the current filter.
Definition: service.cpp:331
std::vector< Category * > CategoryContainer
Definition: category.hpp:84
virtual void add_item(ModelItem *item)
Adds an item to the model.
Definition: kitmodel.cpp:371
void set_model_dirty(bool flag=true)
Definition: service.cpp:264
KitListDao & m_dao
Reference to the perisitence data access object.
Definition: service.hpp:40
virtual void reset()
Resets all contained objects to their default states.
Definition: kitmodel.cpp:466
void create_default_model()
Creates a default model.
Definition: service.cpp:83
ModelItem * find_item(long id)
Definition: service.cpp:125
void set_description(const std::string description)
Definition: item.hpp:47
Functor for processing items.
Definition: item.hpp:85
long get_next_item_id()
Definition: service.cpp:364
bool operator()(Item &item)
Definition: service.cpp:34
long get_next_category_id()
Definition: service.cpp:372
virtual void copy_items(const ModelItemContainer &items, long cat_id=-1)
Copies items to the specified category.
Definition: kitmodel.cpp:399
KitModel * m_model
The application&#39;s data model.
Definition: service.hpp:41
FilterItem(KitModel &model)
Definition: service.cpp:33

Copyright 2008-2021 Frank Dean