summaryrefslogtreecommitdiff
path: root/noncore/apps/tableviewer/db/datacache.cpp
Unidiff
Diffstat (limited to 'noncore/apps/tableviewer/db/datacache.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/tableviewer/db/datacache.cpp68
1 files changed, 34 insertions, 34 deletions
diff --git a/noncore/apps/tableviewer/db/datacache.cpp b/noncore/apps/tableviewer/db/datacache.cpp
index 6380e1b..de57961 100644
--- a/noncore/apps/tableviewer/db/datacache.cpp
+++ b/noncore/apps/tableviewer/db/datacache.cpp
@@ -13,13 +13,13 @@
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20/* 20/*
21 * This file is used to load the xml files that represent the database. 21 * This file is used to load the xml files that represent the database.
22 * The main requirment for said file is each data entry must contain a key, 22 * The main requirment for said file is each data entry must contain a key,
23 * otherwise any other data headings are allowed. 23 * otherwise any other data headings are allowed.
24 */ 24 */
25 25
@@ -60,16 +60,16 @@ DBStore::DBStore()
60 table_size = INIT_TABLE_SIZE; 60 table_size = INIT_TABLE_SIZE;
61 61
62 current_elem = 0; 62 current_elem = 0;
63 archive = 0; 63 archive = 0;
64} 64}
65 65
66//TODO 66//TODO
67/*! 67/*!
68 Reinitializes the table to empty (include a resize of the master table, 68 Reinitializes the table to empty (include a resize of the master table,
69 which should free some memory) 69 which should free some memory)
70*/ 70*/
71void DBStore::freeTable() 71void DBStore::freeTable()
72{ 72{
73 name = ""; 73 name = "";
74 if(archive) { 74 if(archive) {
75 delete archive; 75 delete archive;
@@ -90,62 +90,62 @@ void DBStore::freeTable()
90DBStore::~DBStore() 90DBStore::~DBStore()
91{ 91{
92 freeTable(); 92 freeTable();
93} 93}
94 94
95/*! 95/*!
96 This function opens the given xml file, loads it and sets up the 96 This function opens the given xml file, loads it and sets up the
97 appropriate data structures. 97 appropriate data structures.
98 98
99 \param file_name A string representing the name of the file to be opened 99 \param file_name A string representing the name of the file to be opened
100 \return true if successful, false otherwise. 100 \return true if successful, false otherwise.
101*/ 101*/
102bool DBStore::openSource(QIODevice *inDev, const QString &source) { 102bool DBStore::openSource(QIODevice *inDev, const QString &source) {
103 103
104 /* first check if db is already open, if contains data.. then clear */ 104 /* first check if db is already open, if contains data.. then clear */
105 if(number_elems > 0) { 105 if(number_elems > 0) {
106 freeTable(); 106 freeTable();
107 } 107 }
108 108
109 if (source == "text/x-xml-tableviewer") { 109 if (source == "text/x-xml-tableviewer") {
110 archive = new DBXml(this); 110 archive = new DBXml(this);
111 } else if (source == "text/csv") { 111 } else if (source == "text/csv") {
112 archive = new DBCsv(this); 112 archive = new DBCsv(this);
113 } else 113 } else
114 return false; 114 return false;
115 115
116 return (archive->openSource(inDev)); 116 return (archive->openSource(inDev));
117} 117}
118 118
119bool DBStore::saveSource(QIODevice *outDev, const QString &source) 119bool DBStore::saveSource(QIODevice *outDev, const QString &source)
120{ 120{
121 /* saving a new file */ 121 /* saving a new file */
122 if(!archive) { 122 if(!archive) {
123 if (source == "text/x-xml-tableviewer") { 123 if (source == "text/x-xml-tableviewer") {
124 archive = new DBXml(this); 124 archive = new DBXml(this);
125 } else if (source == "text/x-xml-tableviewer") { 125 } else if (source == "text/x-xml-tableviewer") {
126 archive = new DBCsv(this); 126 archive = new DBCsv(this);
127 } else 127 } else
128 return false; 128 return false;
129 } 129 }
130 130
131 /* changing file type */ 131 /* changing file type */
132 if(archive->type() != source) { 132 if(archive->type() != source) {
133 delete archive; 133 delete archive;
134 if (source == "text/x-xml-tableviewer") { 134 if (source == "text/x-xml-tableviewer") {
135 archive = new DBXml(this); 135 archive = new DBXml(this);
136 } else if (source == "text/x-xml-tableviewer") { 136 } else if (source == "text/x-xml-tableviewer") {
137 archive = new DBCsv(this); 137 archive = new DBCsv(this);
138 } else 138 } else
139 return false; 139 return false;
140 } 140 }
141 141
142 return (archive->saveSource(outDev)); 142 return (archive->saveSource(outDev));
143} 143}
144 144
145/*! 145/*!
146 This function is used to add new elements to the database. If the database 146 This function is used to add new elements to the database. If the database
147 has already reached the maximum allowable size this function does not alter 147 has already reached the maximum allowable size this function does not alter
148 the database. 148 the database.
149 149
150 \param delm An already allocated and initialized data element to be added 150 \param delm An already allocated and initialized data element to be added
151*/ 151*/
@@ -153,15 +153,15 @@ void DBStore::addItem(DataElem *delem)
153{ 153{
154 addItemInternal(delem); 154 addItemInternal(delem);
155} 155}
156 156
157void DBStore::addItemInternal(DataElem *delem) 157void DBStore::addItemInternal(DataElem *delem)
158{ 158{
159 /* if already full, don't over fill, do a qWarning though */ 159 /* if already full, don't over fill, do a owarn though */
160 if (full) { 160 if (full) {
161 owarn << "Attempted to add items to already full table" << oendl; 161 owarn << "Attempted to add items to already full table" << oendl;
162 return; 162 return;
163 } 163 }
164 164
165 master_table.insert(number_elems, delem); 165 master_table.insert(number_elems, delem);
166 166
167 current_elem = number_elems; 167 current_elem = number_elems;
@@ -182,43 +182,43 @@ void DBStore::addItemInternal(DataElem *delem)
182 } 182 }
183} 183}
184 184
185void DBStore::removeItem(DataElem *r) 185void DBStore::removeItem(DataElem *r)
186{ 186{
187 int position = master_table.findRef(r); 187 int position = master_table.findRef(r);
188 if(position != -1) { 188 if(position != -1) {
189 /* there is at least one item, this is it */ 189 /* there is at least one item, this is it */
190 /* replace this with the last element, decrease the element count */ 190 /* replace this with the last element, decrease the element count */
191 master_table.insert(position, master_table.at(--number_elems)); 191 master_table.insert(position, master_table.at(--number_elems));
192 master_table.remove(number_elems); 192 master_table.remove(number_elems);
193 delete r; 193 delete r;
194 } 194 }
195} 195}
196 196
197/*! 197/*!
198 Sets the name of the database 198 Sets the name of the database
199 199
200 \param n A string representing the new name of the database. 200 \param n A string representing the new name of the database.
201*/ 201*/
202void DBStore::setName(const QString &n) 202void DBStore::setName(const QString &n)
203{ 203{
204 name = n; 204 name = n;
205} 205}
206 206
207/*! 207/*!
208 Gets the name of the database 208 Gets the name of the database
209 209
210 \return A string representing the name of the database. 210 \return A string representing the name of the database.
211*/ 211*/
212QString DBStore::getName() 212QString DBStore::getName()
213{ 213{
214 return name; 214 return name;
215} 215}
216 216
217/*! 217/*!
218 Retrieves a pointer to the key representation of the database for 218 Retrieves a pointer to the key representation of the database for
219 other classes to use as reference. 219 other classes to use as reference.
220 220
221 \return a pointer to the databases key representaion 221 \return a pointer to the databases key representaion
222*/ 222*/
223KeyList *DBStore::getKeys() 223KeyList *DBStore::getKeys()
224{ 224{
@@ -232,21 +232,21 @@ KeyList *DBStore::getKeys()
232void DBStore::setKeys(KeyList *k) 232void DBStore::setKeys(KeyList *k)
233{ 233{
234 kRep = k; 234 kRep = k;
235} 235}
236 236
237/*! 237/*!
238 Sets the current element to the first element of the database 238 Sets the current element to the first element of the database
239*/ 239*/
240void DBStore::first() 240void DBStore::first()
241{ 241{
242 current_elem = 0; 242 current_elem = 0;
243} 243}
244 244
245/*! 245/*!
246 Sets the current element to the last element of the database 246 Sets the current element to the last element of the database
247*/ 247*/
248void DBStore::last() 248void DBStore::last()
249{ 249{
250 current_elem = number_elems - 1; 250 current_elem = number_elems - 1;
251} 251}
252 252
@@ -254,13 +254,13 @@ void DBStore::last()
254 Sets the current element to the next element of the database if 254 Sets the current element to the next element of the database if
255 there exists an element after the current one. 255 there exists an element after the current one.
256*/ 256*/
257bool DBStore::next() 257bool DBStore::next()
258{ 258{
259 unsigned int new_current_elem = current_elem + 1; 259 unsigned int new_current_elem = current_elem + 1;
260 if (current_elem < number_elems) 260 if (current_elem < number_elems)
261 /* was valid before inc (it is possible but unlikely that inc current 261 /* was valid before inc (it is possible but unlikely that inc current
262 elem will change it from invalid to valid) */ 262 elem will change it from invalid to valid) */
263 if (new_current_elem < number_elems) { 263 if (new_current_elem < number_elems) {
264 /* is valid after inc */ 264 /* is valid after inc */
265 current_elem = new_current_elem; 265 current_elem = new_current_elem;
266 return true; 266 return true;
@@ -275,27 +275,27 @@ bool DBStore::next()
275bool DBStore::previous() 275bool DBStore::previous()
276{ 276{
277 unsigned int new_current_elem = current_elem -1; 277 unsigned int new_current_elem = current_elem -1;
278 if (current_elem < number_elems) 278 if (current_elem < number_elems)
279 /* was valid */ 279 /* was valid */
280 if (new_current_elem < number_elems) { 280 if (new_current_elem < number_elems) {
281 /* still is (if was 0, then now -1, but as is unsigned will wrap 281 /* still is (if was 0, then now -1, but as is unsigned will wrap
282 and hence be invalid */ 282 and hence be invalid */
283 current_elem = new_current_elem; 283 current_elem = new_current_elem;
284 return true; 284 return true;
285 } 285 }
286 return false; 286 return false;
287} 287}
288 288
289/*! 289/*!
290 Returns the current data element in the database. Which element is current 290 Returns the current data element in the database. Which element is current
291 is affected by newly added items, findItem, next, previous, first and 291 is affected by newly added items, findItem, next, previous, first and
292 last functions 292 last functions
293 293
294 \return a pointer to the current data element 294 \return a pointer to the current data element
295*/ 295*/
296DataElem *DBStore::getCurrentData() 296DataElem *DBStore::getCurrentData()
297{ 297{
298 if (current_elem >= number_elems) 298 if (current_elem >= number_elems)
299 return NULL; 299 return NULL;
300 return master_table[current_elem]; 300 return master_table[current_elem];
301} 301}