lib: added DBData and DBLoader
[barry.git] / src / m_desktop.h
blob4f70b5c515f74e26636c8f4e3c171a625ad7661b
1 ///
2 /// \file m_desktop.h
3 /// Mode class for the Desktop mode
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 __BARRY_M_DESKTOP_H__
23 #define __BARRY_M_DESKTOP_H__
25 #include "dll.h"
26 #include "m_mode_base.h"
27 #include "data.h"
28 #include "socket.h"
29 #include "record.h"
31 namespace Barry {
33 // forward declarations
34 class Parser;
35 class Builder;
36 class IConverter;
38 namespace Mode {
40 class DBLoader;
43 // Desktop class
45 /// The main interface class to the device databases.
46 ///
47 /// To use this class, use the following steps:
48 ///
49 /// - Create a Controller object (see Controller class for more details)
50 /// - Create this Mode::Desktop object, passing in the Controller
51 /// object during construction
52 /// - Call Open() to open database socket and finish constructing.
53 /// - Call GetDBDB() to get the device's database database
54 /// - Call GetDBID() to get a database ID by name
55 /// - Call LoadDatabase() to retrieve and store a database
56 ///
57 class BXEXPORT Desktop : public Mode
59 friend class DBLoader;
61 public:
62 enum CommandType { Unknown, DatabaseAccess };
64 private:
65 // packet data
66 Data m_command, m_response;
68 CommandTable m_commandTable;
69 DatabaseDatabase m_dbdb;
71 // external objects (optional, can be null)
72 const IConverter *m_ic;
74 protected:
75 void LoadCommandTable();
76 void LoadDBDB();
78 //////////////////////////////////
79 // overrides
81 virtual void OnOpen();
83 public:
84 Desktop(Controller &con);
85 Desktop(Controller &con, const IConverter &ic);
86 ~Desktop();
88 //////////////////////////////////
89 // meta access
91 /// Returns DatabaseDatabase object for this connection.
92 /// Must call Open() first, which loads the DBDB.
93 const DatabaseDatabase& GetDBDB() const { return m_dbdb; }
94 unsigned int GetDBID(const std::string &name) const;
95 unsigned int GetDBCommand(CommandType ct);
97 void SetIConverter(const IConverter &ic);
99 //////////////////////////////////
100 // Desktop mode - database specific
102 // dirty flag related functions, for sync operations
103 void GetRecordStateTable(unsigned int dbId, RecordStateTable &result);
104 void AddRecord(unsigned int dbId, Builder &build); // RecordId is
105 // retrieved from build, and duplicate IDs are allowed,
106 // but *not* recommended!
107 void GetRecord(unsigned int dbId, unsigned int stateTableIndex, Parser &parser);
108 void SetRecord(unsigned int dbId, unsigned int stateTableIndex, Builder &build);
109 void ClearDirty(unsigned int dbId, unsigned int stateTableIndex);
110 void DeleteRecord(unsigned int dbId, unsigned int stateTableIndex);
112 // pure load/save operations
113 void LoadDatabase(unsigned int dbId, Parser &parser);
114 void ClearDatabase(unsigned int dbId);
115 void SaveDatabase(unsigned int dbId, Builder &builder);
117 template <class RecordT, class StorageT> void LoadDatabaseByType(StorageT &store);
118 template <class RecordT, class StorageT> void SaveDatabaseByType(StorageT &store);
120 template <class StorageT> void LoadDatabaseByName(const std::string &name, StorageT &store);
121 template <class StorageT> void SaveDatabaseByName(const std::string &name, StorageT &store);
123 template <class RecordT> void AddRecordByType(uint32_t recordId, const RecordT &rec);
127 // used to hold internal-only state
128 struct DBLoaderData;
131 // DBLoader
133 /// Database Loader operation class. Encapsulates the load / save
134 /// logic of Desktop::LoadDatabase() and someday Desktop::SaveDatabase()
135 /// in such a way that the loading of individual records is
136 /// controllable by the user, instead of using the parser callback mechanism.
138 /// This class can be reused to load / save multiple databases, but
139 /// do not call Desktop members while a load operation is in progress.
141 class BXEXPORT DBLoader
143 DBLoaderData *m_loader;
144 Desktop &m_desktop;
145 DBData &m_data;
146 bool m_loading;
147 std::string m_dbName;
149 public:
150 DBLoader(Desktop &desktop, DBData &data);
151 ~DBLoader();
153 /// Do not call Desktop members if this is true.
154 bool IsBusy() const { return m_loading; }
156 // caller-controllable load/save operations... if
157 // these functions return true, then new data has
158 // just been loaded into the data object passed to
159 // the constructor
160 bool StartDBLoad(unsigned int dbId);
161 bool GetNextRecord();
164 }} // namespace Barry::Mode
166 #endif