Diffstat (limited to 'noncore/apps/tableviewer/db/datacache.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/tableviewer/db/datacache.cpp | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/noncore/apps/tableviewer/db/datacache.cpp b/noncore/apps/tableviewer/db/datacache.cpp index 7c14eef..6380e1b 100644 --- a/noncore/apps/tableviewer/db/datacache.cpp +++ b/noncore/apps/tableviewer/db/datacache.cpp | |||
@@ -1,249 +1,257 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
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 | ||
26 | #include "datacache.h" | 26 | #include "datacache.h" |
27 | #include "xmlsource.h" | 27 | #include "xmlsource.h" |
28 | #include "csvsource.h" | 28 | #include "csvsource.h" |
29 | #include <stdlib.h> | 29 | |
30 | /* OPIE */ | ||
31 | #include <opie2/odebug.h> | ||
32 | using namespace Opie::Core; | ||
33 | |||
34 | /* QT */ | ||
30 | #include <qheader.h> | 35 | #include <qheader.h> |
31 | 36 | ||
37 | /* STD */ | ||
38 | #include <stdlib.h> | ||
39 | |||
32 | #define INIT_TABLE_SIZE 128 | 40 | #define INIT_TABLE_SIZE 128 |
33 | 41 | ||
34 | /*! | 42 | /*! |
35 | \class DBStore datastore.h | 43 | \class DBStore datastore.h |
36 | 44 | ||
37 | \brief The DBStore class is the class responsible for storing, sorting and | 45 | \brief The DBStore class is the class responsible for storing, sorting and |
38 | searching the data loaded by the application | 46 | searching the data loaded by the application |
39 | 47 | ||
40 | */ | 48 | */ |
41 | 49 | ||
42 | /*! | 50 | /*! |
43 | Constructs a DBStore item | 51 | Constructs a DBStore item |
44 | */ | 52 | */ |
45 | DBStore::DBStore() | 53 | DBStore::DBStore() |
46 | { | 54 | { |
47 | name = ""; | 55 | name = ""; |
48 | number_elems = 0; | 56 | number_elems = 0; |
49 | full = false; | 57 | full = false; |
50 | kRep = new KeyList(); | 58 | kRep = new KeyList(); |
51 | master_table.resize(INIT_TABLE_SIZE); | 59 | master_table.resize(INIT_TABLE_SIZE); |
52 | table_size = INIT_TABLE_SIZE; | 60 | table_size = INIT_TABLE_SIZE; |
53 | 61 | ||
54 | current_elem = 0; | 62 | current_elem = 0; |
55 | archive = 0; | 63 | archive = 0; |
56 | } | 64 | } |
57 | 65 | ||
58 | //TODO | 66 | //TODO |
59 | /*! | 67 | /*! |
60 | 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, |
61 | which should free some memory) | 69 | which should free some memory) |
62 | */ | 70 | */ |
63 | void DBStore::freeTable() | 71 | void DBStore::freeTable() |
64 | { | 72 | { |
65 | name = ""; | 73 | name = ""; |
66 | if(archive) { | 74 | if(archive) { |
67 | delete archive; | 75 | delete archive; |
68 | archive = 0; | 76 | archive = 0; |
69 | } | 77 | } |
70 | kRep->clear(); /* clear the current key list */ | 78 | kRep->clear(); /* clear the current key list */ |
71 | 79 | ||
72 | number_elems = 0; | 80 | number_elems = 0; |
73 | table_size = INIT_TABLE_SIZE; | 81 | table_size = INIT_TABLE_SIZE; |
74 | master_table.resize(table_size); | 82 | master_table.resize(table_size); |
75 | full = false; | 83 | full = false; |
76 | current_elem = 0; | 84 | current_elem = 0; |
77 | } | 85 | } |
78 | 86 | ||
79 | /*! | 87 | /*! |
80 | Removes all items from the DBStore and destroys the DBStore | 88 | Removes all items from the DBStore and destroys the DBStore |
81 | */ | 89 | */ |
82 | DBStore::~DBStore() | 90 | DBStore::~DBStore() |
83 | { | 91 | { |
84 | freeTable(); | 92 | freeTable(); |
85 | } | 93 | } |
86 | 94 | ||
87 | /*! | 95 | /*! |
88 | 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 |
89 | appropriate data structures. | 97 | appropriate data structures. |
90 | 98 | ||
91 | \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 |
92 | \return true if successful, false otherwise. | 100 | \return true if successful, false otherwise. |
93 | */ | 101 | */ |
94 | bool DBStore::openSource(QIODevice *inDev, const QString &source) { | 102 | bool DBStore::openSource(QIODevice *inDev, const QString &source) { |
95 | 103 | ||
96 | /* 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 */ |
97 | if(number_elems > 0) { | 105 | if(number_elems > 0) { |
98 | freeTable(); | 106 | freeTable(); |
99 | } | 107 | } |
100 | 108 | ||
101 | if (source == "text/x-xml-tableviewer") { | 109 | if (source == "text/x-xml-tableviewer") { |
102 | archive = new DBXml(this); | 110 | archive = new DBXml(this); |
103 | } else if (source == "text/csv") { | 111 | } else if (source == "text/csv") { |
104 | archive = new DBCsv(this); | 112 | archive = new DBCsv(this); |
105 | } else | 113 | } else |
106 | return false; | 114 | return false; |
107 | 115 | ||
108 | return (archive->openSource(inDev)); | 116 | return (archive->openSource(inDev)); |
109 | } | 117 | } |
110 | 118 | ||
111 | bool DBStore::saveSource(QIODevice *outDev, const QString &source) | 119 | bool DBStore::saveSource(QIODevice *outDev, const QString &source) |
112 | { | 120 | { |
113 | /* saving a new file */ | 121 | /* saving a new file */ |
114 | if(!archive) { | 122 | if(!archive) { |
115 | if (source == "text/x-xml-tableviewer") { | 123 | if (source == "text/x-xml-tableviewer") { |
116 | archive = new DBXml(this); | 124 | archive = new DBXml(this); |
117 | } else if (source == "text/x-xml-tableviewer") { | 125 | } else if (source == "text/x-xml-tableviewer") { |
118 | archive = new DBCsv(this); | 126 | archive = new DBCsv(this); |
119 | } else | 127 | } else |
120 | return false; | 128 | return false; |
121 | } | 129 | } |
122 | 130 | ||
123 | /* changing file type */ | 131 | /* changing file type */ |
124 | if(archive->type() != source) { | 132 | if(archive->type() != source) { |
125 | delete archive; | 133 | delete archive; |
126 | if (source == "text/x-xml-tableviewer") { | 134 | if (source == "text/x-xml-tableviewer") { |
127 | archive = new DBXml(this); | 135 | archive = new DBXml(this); |
128 | } else if (source == "text/x-xml-tableviewer") { | 136 | } else if (source == "text/x-xml-tableviewer") { |
129 | archive = new DBCsv(this); | 137 | archive = new DBCsv(this); |
130 | } else | 138 | } else |
131 | return false; | 139 | return false; |
132 | } | 140 | } |
133 | 141 | ||
134 | return (archive->saveSource(outDev)); | 142 | return (archive->saveSource(outDev)); |
135 | } | 143 | } |
136 | 144 | ||
137 | /*! | 145 | /*! |
138 | 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 |
139 | 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 |
140 | the database. | 148 | the database. |
141 | 149 | ||
142 | \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 |
143 | */ | 151 | */ |
144 | void DBStore::addItem(DataElem *delem) | 152 | void DBStore::addItem(DataElem *delem) |
145 | { | 153 | { |
146 | addItemInternal(delem); | 154 | addItemInternal(delem); |
147 | } | 155 | } |
148 | 156 | ||
149 | void DBStore::addItemInternal(DataElem *delem) | 157 | void DBStore::addItemInternal(DataElem *delem) |
150 | { | 158 | { |
151 | /* if already full, don't over fill, do a qWarning though */ | 159 | /* if already full, don't over fill, do a qWarning though */ |
152 | if (full) { | 160 | if (full) { |
153 | qWarning("Attempted to add items to already full table"); | 161 | owarn << "Attempted to add items to already full table" << oendl; |
154 | return; | 162 | return; |
155 | } | 163 | } |
156 | 164 | ||
157 | master_table.insert(number_elems, delem); | 165 | master_table.insert(number_elems, delem); |
158 | 166 | ||
159 | current_elem = number_elems; | 167 | current_elem = number_elems; |
160 | number_elems++; | 168 | number_elems++; |
161 | 169 | ||
162 | if(number_elems >= table_size) { | 170 | if(number_elems >= table_size) { |
163 | /* filled current table, double if we can */ | 171 | /* filled current table, double if we can */ |
164 | table_size = table_size << 1; | 172 | table_size = table_size << 1; |
165 | 173 | ||
166 | /* check that the new table size is still valid, i.e. that we didn't | 174 | /* check that the new table size is still valid, i.e. that we didn't |
167 | just shift the 1 bit of the end of the int. */ | 175 | just shift the 1 bit of the end of the int. */ |
168 | if (!table_size) { | 176 | if (!table_size) { |
169 | full = true; | 177 | full = true; |
170 | /* no point in doing antying else. */ | 178 | /* no point in doing antying else. */ |
171 | return; | 179 | return; |
172 | } | 180 | } |
173 | master_table.resize(table_size); | 181 | master_table.resize(table_size); |
174 | } | 182 | } |
175 | } | 183 | } |
176 | 184 | ||
177 | void DBStore::removeItem(DataElem *r) | 185 | void DBStore::removeItem(DataElem *r) |
178 | { | 186 | { |
179 | int position = master_table.findRef(r); | 187 | int position = master_table.findRef(r); |
180 | if(position != -1) { | 188 | if(position != -1) { |
181 | /* there is at least one item, this is it */ | 189 | /* there is at least one item, this is it */ |
182 | /* replace this with the last element, decrease the element count */ | 190 | /* replace this with the last element, decrease the element count */ |
183 | master_table.insert(position, master_table.at(--number_elems)); | 191 | master_table.insert(position, master_table.at(--number_elems)); |
184 | master_table.remove(number_elems); | 192 | master_table.remove(number_elems); |
185 | delete r; | 193 | delete r; |
186 | } | 194 | } |
187 | } | 195 | } |
188 | 196 | ||
189 | /*! | 197 | /*! |
190 | Sets the name of the database | 198 | Sets the name of the database |
191 | 199 | ||
192 | \param n A string representing the new name of the database. | 200 | \param n A string representing the new name of the database. |
193 | */ | 201 | */ |
194 | void DBStore::setName(const QString &n) | 202 | void DBStore::setName(const QString &n) |
195 | { | 203 | { |
196 | name = n; | 204 | name = n; |
197 | } | 205 | } |
198 | 206 | ||
199 | /*! | 207 | /*! |
200 | Gets the name of the database | 208 | Gets the name of the database |
201 | 209 | ||
202 | \return A string representing the name of the database. | 210 | \return A string representing the name of the database. |
203 | */ | 211 | */ |
204 | QString DBStore::getName() | 212 | QString DBStore::getName() |
205 | { | 213 | { |
206 | return name; | 214 | return name; |
207 | } | 215 | } |
208 | 216 | ||
209 | /*! | 217 | /*! |
210 | Retrieves a pointer to the key representation of the database for | 218 | Retrieves a pointer to the key representation of the database for |
211 | other classes to use as reference. | 219 | other classes to use as reference. |
212 | 220 | ||
213 | \return a pointer to the databases key representaion | 221 | \return a pointer to the databases key representaion |
214 | */ | 222 | */ |
215 | KeyList *DBStore::getKeys() | 223 | KeyList *DBStore::getKeys() |
216 | { | 224 | { |
217 | return kRep; | 225 | return kRep; |
218 | } | 226 | } |
219 | 227 | ||
220 | /*! | 228 | /*! |
221 | sets the database's key representation the passed pointer | 229 | sets the database's key representation the passed pointer |
222 | \param a pointer to a key representaton | 230 | \param a pointer to a key representaton |
223 | */ | 231 | */ |
224 | void DBStore::setKeys(KeyList *k) | 232 | void DBStore::setKeys(KeyList *k) |
225 | { | 233 | { |
226 | kRep = k; | 234 | kRep = k; |
227 | } | 235 | } |
228 | 236 | ||
229 | /*! | 237 | /*! |
230 | Sets the current element to the first element of the database | 238 | Sets the current element to the first element of the database |
231 | */ | 239 | */ |
232 | void DBStore::first() | 240 | void DBStore::first() |
233 | { | 241 | { |
234 | current_elem = 0; | 242 | current_elem = 0; |
235 | } | 243 | } |
236 | 244 | ||
237 | /*! | 245 | /*! |
238 | Sets the current element to the last element of the database | 246 | Sets the current element to the last element of the database |
239 | */ | 247 | */ |
240 | void DBStore::last() | 248 | void DBStore::last() |
241 | { | 249 | { |
242 | current_elem = number_elems - 1; | 250 | current_elem = number_elems - 1; |
243 | } | 251 | } |
244 | 252 | ||
245 | /*! | 253 | /*! |
246 | 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 |
247 | there exists an element after the current one. | 255 | there exists an element after the current one. |
248 | */ | 256 | */ |
249 | bool DBStore::next() | 257 | bool DBStore::next() |