desktop: documented the extra classes in Mode_Browse.h
[barry.git] / desktop / src / Mode_Browse.h
blob4d2bbbcf0f4bca65cd0a06b6d66e7aaf3c5d8b1d
1 ///
2 /// \file Mode_Browse.h
3 /// Mode derived class for database browsing
4 ///
6 /*
7 Copyright (C) 2011-2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRYDESKTOP_MODE_BROWSE_H__
23 #define __BARRYDESKTOP_MODE_BROWSE_H__
25 #include "Mode.h"
26 #include "util.h"
27 #include <barry/barry.h>
28 #include <barry/scoped_lock.h>
29 #include <tr1/memory>
30 #include <list>
32 class GUIDesktopConnector : public Barry::DesktopConnector
34 wxWindow *m_parent;
36 public:
37 GUIDesktopConnector(wxWindow *parent, const char *password,
38 const std::string &locale, const Barry::ProbeResult &result)
39 : Barry::DesktopConnector(password, locale, result)
40 , m_parent(parent)
44 virtual bool PasswordPrompt(const Barry::BadPassword &bp,
45 std::string &password_result);
48 // Holds the "right" to use the desktop, and this right expires with the
49 // scope of this class, and no other code can use it while this object exists.
51 // Is copied around by the below auto_ptr<> typedef.
52 class DesktopInstance
54 private:
55 Barry::scoped_lock m_lock;
57 // external interfaces... owned by caller
58 GUIDesktopConnector &m_gdc;
60 public:
61 explicit DesktopInstance(pthread_mutex_t &mutex,
62 GUIDesktopConnector &gdc)
63 : m_lock(mutex)
64 , m_gdc(gdc)
68 GUIDesktopConnector& Connector() { return m_gdc; }
69 Barry::Mode::Desktop& Desktop() { return m_gdc.GetDesktop(); }
70 Barry::IConverter& IC() { return m_gdc.GetIConverter(); }
73 typedef std::auto_ptr<DesktopInstance> DesktopInstancePtr;
76 // ThreadableDesktop
78 /// Protection class that holds the GUIDesktopConnector (i.e. the main
79 /// desktop instance) and a mutex. To access the desktop object, call
80 /// Get(), which locks access for the caller and returns the
81 /// DesktopInstancePtr smart pointer. The lock is released when
82 /// the DesktopInstancePtr goes out of scope.
83 ///
84 class ThreadableDesktop
86 private:
87 pthread_mutex_t m_mutex;
89 // external interfaces... owned by caller
90 GUIDesktopConnector &m_gdc;
92 public:
93 explicit ThreadableDesktop(GUIDesktopConnector &gdc)
94 : m_gdc(gdc)
96 if( pthread_mutex_init(&m_mutex, NULL) ) {
97 throw Barry::Error("Failed to create mutex for ThreadableDesktop");
101 DesktopInstancePtr Get()
103 return DesktopInstancePtr(
104 new DesktopInstance(m_mutex, m_gdc));
109 // DataCache
111 /// Abstract base class representing a single cached record in a database.
112 /// Does not actually contain the record data (base classes hold that), but
113 /// defines what can be done with such a record cache object. (eg. such as
114 /// editing the record)
116 class DataCache
118 public:
119 typedef Barry::RecordStateTable::IndexType IndexType;
121 private:
122 IndexType m_state_index;
123 uint32_t m_record_id;
125 public:
126 DataCache(IndexType state_index, uint32_t record_id)
127 : m_state_index(state_index)
128 , m_record_id(record_id)
132 virtual ~DataCache() {}
134 virtual IndexType GetStateIndex() const { return m_state_index; }
135 virtual uint32_t GetRecordId() const { return m_record_id; }
136 virtual void SetIds(IndexType state_index, uint32_t record_id)
138 std::cout << "DataCache::SetIds(" << state_index << ", " << record_id << ");" << std::endl;
139 m_state_index = state_index;
140 m_record_id = record_id;
143 // set editable to false if you just want to view record
144 // non-buildable records will always be non-editable regardless
145 virtual bool Edit(wxWindow *parent, bool editable) = 0;
146 virtual std::string GetDescription() const = 0;
148 virtual bool IsBuildable() const { return false; }
150 bool operator< (const DataCache &other)
152 return GetDescription() < other.GetDescription();
158 // DataCachePtr
160 /// Basically a shared_ptr<DataCache>, but with the added operator<()
161 /// overload so that it calls DataCache's operator<(), for sorting
162 /// of objects via this pointer.
164 class DataCachePtr : public std::tr1::shared_ptr<DataCache>
166 public:
167 DataCachePtr()
171 explicit DataCachePtr(DataCache *obj)
172 : std::tr1::shared_ptr<DataCache>(obj)
176 bool operator< (const DataCachePtr &other)
178 return (*this)->operator<( *other );
183 // DBDataCache
185 /// Derived class from DataCache that holds the raw data of a record that
186 /// does not yet have a parser. Unable to do much with such records in
187 /// terms of editing, but still able to be cached in the DBMap.
189 class DBDataCache : public DataCache
191 Barry::DBData m_raw;
193 public:
194 DBDataCache(DataCache::IndexType index, const Barry::DBData &raw);
196 virtual bool Edit(wxWindow *parent, bool editable);
197 virtual std::string GetDescription() const;
200 // returns true if GUI indicates change (i.e. if dialogreturns wxID_OK)
201 #undef HANDLE_PARSER
202 #define HANDLE_PARSER(dbname) \
203 bool EditRecord(wxWindow *parent, bool editable, Barry::dbname &rec);
204 ALL_KNOWN_PARSER_TYPES
207 // RecordCache<>
209 /// Template class derived from DataCache which holds the parsed record data
210 /// for the templated record type. Allows actions such as editing.
212 template <class RecordT>
213 class RecordCache
214 : public DataCache
215 , public Barry::Builder
217 private:
218 RecordT m_rec;
220 public:
221 RecordCache(DataCache::IndexType index, const RecordT &rec)
222 : DataCache(index, rec.GetUniqueId())
223 , m_rec(rec)
225 // if copying from another record, don't copy what
226 // we don't understand
227 m_rec.Unknowns.clear();
230 const RecordT& GetRecord() const { return m_rec; }
232 // hook SetIds() to grab any new record_ids / UniqueIDs
233 virtual void SetIds(IndexType state_index, uint32_t record_id)
235 std::cout << "RecordCache::SetIds(" << state_index << ", " << record_id << ");" << std::endl;
236 DataCache::SetIds(state_index, record_id);
237 m_rec.SetIds(RecordT::GetDefaultRecType(), record_id);
240 virtual bool Edit(wxWindow *parent, bool editable)
242 RecordT copy = m_rec;
243 bool changed = EditRecord(parent, editable, copy);
244 // FIXME - we could, at this point, add a (copy != m_rec)
245 // check here, to prevent writing a record that has not
246 // changed, but that would require using the FieldHandle<>
247 // system (a lot of code), and it's not a critical feature
248 // right now
249 if( changed && editable ) {
250 m_rec = copy;
251 return true;
253 return false;
256 virtual std::string GetDescription() const
258 return m_rec.GetDescription();
261 virtual bool IsBuildable() const
263 return ::IsBuildable<RecordT>();
267 // Barry::Builder overrides
269 virtual bool BuildRecord(Barry::DBData &data, size_t &offset,
270 const Barry::IConverter *ic)
272 Barry::SetDBData<RecordT>(m_rec, data, offset, ic);
273 return true;
276 virtual bool FetchRecord(Barry::DBData &data,
277 const Barry::IConverter *ic)
279 size_t offset = 0;
280 Barry::SetDBData<RecordT>(m_rec, data, offset, ic);
281 return true;
284 virtual bool EndOfFile() const
286 return true;
291 // DBCache
293 /// This is a container class that holds a std::list of DataCache-derived
294 /// objects for all the records of a single database.
296 class DBCache : public Barry::AllRecordStore, public Barry::Parser
298 public:
299 typedef Barry::RecordStateTable::IndexType IndexType;
300 typedef std::list<DataCachePtr> DataList;
301 typedef DataList::iterator iterator;
302 typedef DataList::const_iterator const_iterator;
304 private:
305 ThreadableDesktop &m_tdesktop;
306 std::string m_dbname;
307 unsigned int m_dbid;
308 Barry::RecordStateTable m_state;
309 DataList m_records;
311 // per-record load state
312 IndexType m_index;
314 public:
315 // loads records in constructor
316 DBCache(ThreadableDesktop &tdesktop, const std::string &dbname);
317 virtual ~DBCache();
319 const std::string& GetDBName() { return m_dbname; }
321 iterator Get(int list_offset);
322 const_iterator Get(int list_offset) const;
323 // returns the numeric index of the record, to keep with GUI
324 int GetIndex(iterator record) const;
325 int GetIndex(const_iterator record) const;
327 iterator Add(wxWindow *parent, iterator copy_record);
328 bool Edit(wxWindow *parent, iterator record);
329 bool Delete(wxWindow *parent, iterator record);
331 iterator begin() { return m_records.begin(); }
332 iterator end() { return m_records.end(); }
333 const_iterator begin() const { return m_records.begin(); }
334 const_iterator end() const { return m_records.end(); }
336 // For Barry::AllRecordStore
337 #undef HANDLE_PARSER
338 #define HANDLE_PARSER(tname) \
339 virtual void operator() (const Barry::tname &);
340 ALL_KNOWN_PARSER_TYPES
342 // For Barry::Parser
343 virtual void ParseRecord(const Barry::DBData &data,
344 const Barry::IConverter *ic);
348 // DBMap
350 /// A std::map based container that maps database names with their
351 /// corresponding DBCaches.
353 class DBMap
355 public:
356 typedef std::tr1::shared_ptr<DBCache> DBCachePtr;
357 typedef std::map<std::string, DBCachePtr> MapType;
359 private:
360 ThreadableDesktop &m_tdesktop;
361 MapType m_map;
362 pthread_mutex_t m_map_mutex;
363 pthread_mutex_t m_load_mutex;
365 public:
366 DBMap(ThreadableDesktop &tdesktop);
368 DBCachePtr LoadDBCache(const std::string &dbname);
369 DBCachePtr GetDBCache(const std::string &dbname);
373 // BrowseMode
375 /// The Desktop GUI mode class for browsing databases and manipulating records.
377 class BrowseMode : public wxEvtHandler, public Mode
379 private:
380 DECLARE_EVENT_TABLE() // sets to protected:
382 private:
383 wxWindow *m_parent;
385 // device interface
386 std::auto_ptr<GUIDesktopConnector> m_con;
387 std::auto_ptr<ThreadableDesktop> m_tdesktop;
388 std::auto_ptr<DBMap> m_dbmap;
389 Barry::DatabaseDatabase m_dbdb;
391 // window controls
392 std::auto_ptr<wxBoxSizer> m_top_sizer;
393 std::auto_ptr<wxListCtrl> m_dbdb_list;
394 std::auto_ptr<wxListCtrl> m_record_list;
395 std::auto_ptr<wxCheckBox> m_show_all_checkbox;
396 std::auto_ptr<wxButton> m_add_record_button;
397 std::auto_ptr<wxButton> m_copy_record_button;
398 std::auto_ptr<wxButton> m_edit_record_button;
399 std::auto_ptr<wxButton> m_delete_record_button;
400 std::auto_ptr<wxStaticText> m_load_status_text;
402 // misc supporting data
403 wxString m_device_id_str;
405 // GUI state
406 bool m_buildable; // true if currently displayed db has
407 // a Builder available for it
408 bool m_editable; // true if currently displayed db has
409 // an edit dialog available for it
410 bool m_show_all; // if true, show all databases in list
411 // instead of just the parsable ones
412 std::string m_current_dbname;
413 long m_current_record_item;
415 // thread state
416 pthread_t m_cache_thread;
417 volatile bool m_abort_flag;
419 protected:
420 void CreateControls();
421 int GUItoDBDBIndex(int gui_index);
422 void FillDBDBList();
423 void FillRecordList(const std::string &dbname);
424 void UpdateButtons();
425 void FillCache();
427 // background thread function
428 static void* FillCacheThread(void *bobj);
430 public:
431 BrowseMode(wxWindow *parent, const Barry::ProbeResult &device);
432 ~BrowseMode();
434 void SendStatusEvent(const std::string &dbname);
436 // virtual override events (derived from Mode)
437 wxString GetTitleText() const { return _T("Barry Database Browser"); }
439 // window events
440 void OnDBDBListSelChange(wxListEvent &event);
441 void OnRecordListSelChange(wxListEvent &event);
442 void OnRecordListActivated(wxListEvent &event);
443 void OnShowAll(wxCommandEvent &event);
444 void OnAddRecord(wxCommandEvent &event);
445 void OnCopyRecord(wxCommandEvent &event);
446 void OnEditRecord(wxCommandEvent &event);
447 void OnDeleteRecord(wxCommandEvent &event);
448 void OnStatusEvent(wxCommandEvent &event);
451 #endif