Diffstat (limited to 'libopie2/opiepim/core/opimrecordlist.h') (more/less context) (show whitespace changes)
-rw-r--r-- | libopie2/opiepim/core/opimrecordlist.h | 402 |
1 files changed, 402 insertions, 0 deletions
diff --git a/libopie2/opiepim/core/opimrecordlist.h b/libopie2/opiepim/core/opimrecordlist.h new file mode 100644 index 0000000..b23138d --- a/dev/null +++ b/libopie2/opiepim/core/opimrecordlist.h | |||
@@ -0,0 +1,402 @@ | |||
1 | /* | ||
2 | This file is part of the Opie Project | ||
3 | Copyright (C) The Main Author <main-author@whereever.org> | ||
4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
5 | .=l. | ||
6 | .>+-= | ||
7 | _;:, .> :=|. This program is free software; you can | ||
8 | .> <`_, > . <= redistribute it and/or modify it under | ||
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
10 | .="- .-=="i, .._ License as published by the Free Software | ||
11 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
12 | ._= =} : or (at your option) any later version. | ||
13 | .%`+i> _;_. | ||
14 | .i_,=:_. -<s. This program is distributed in the hope that | ||
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
16 | : .. .:, . . . without even the implied warranty of | ||
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
19 | ..}^=.= = ; Library General Public License for more | ||
20 | ++= -. .` .: details. | ||
21 | : = ...= . :.=- | ||
22 | -. .:....=;==+<; You should have received a copy of the GNU | ||
23 | -_. . . )=. = Library General Public License along with | ||
24 | -- :-=` this library; see the file COPYING.LIB. | ||
25 | If not, write to the Free Software Foundation, | ||
26 | Inc., 59 Temple Place - Suite 330, | ||
27 | Boston, MA 02111-1307, USA. | ||
28 | */ | ||
29 | |||
30 | #ifndef ORECORDLIST_H | ||
31 | #define ORECORDLIST_H | ||
32 | |||
33 | /* OPIE */ | ||
34 | #include <opie2/opimtemplatebase.h> | ||
35 | #include <opie2/opimrecord.h> | ||
36 | |||
37 | /* QT */ | ||
38 | #include <qarray.h> | ||
39 | |||
40 | namespace Opie | ||
41 | { | ||
42 | |||
43 | class OPimRecordListIteratorPrivate; | ||
44 | /** | ||
45 | * Our List Iterator | ||
46 | * it behaves like STL or Qt | ||
47 | * | ||
48 | * for(it = list.begin(); it != list.end(); ++it ) | ||
49 | * doSomeCoolStuff( (*it) ); | ||
50 | */ | ||
51 | template <class T> class OPimRecordList; | ||
52 | template <class T = OPimRecord> | ||
53 | class OPimRecordListIterator | ||
54 | { | ||
55 | friend class OPimRecordList<T>; | ||
56 | |||
57 | public: | ||
58 | typedef OTemplateBase<T> Base; | ||
59 | |||
60 | /** | ||
61 | * The c'tor used internally from | ||
62 | * OPimRecordList | ||
63 | */ | ||
64 | OPimRecordListIterator( const QArray<int>, const Base* ); | ||
65 | |||
66 | /** | ||
67 | * The standard c'tor | ||
68 | */ | ||
69 | OPimRecordListIterator(); | ||
70 | ~OPimRecordListIterator(); | ||
71 | |||
72 | OPimRecordListIterator( const OPimRecordListIterator& ); | ||
73 | OPimRecordListIterator &operator=( const OPimRecordListIterator& ); | ||
74 | |||
75 | /** | ||
76 | * a * operator ;) | ||
77 | * use it like this T = (*it); | ||
78 | */ | ||
79 | T operator*(); | ||
80 | OPimRecordListIterator &operator++(); | ||
81 | OPimRecordListIterator &operator--(); | ||
82 | |||
83 | bool operator==( const OPimRecordListIterator& it ); | ||
84 | bool operator!=( const OPimRecordListIterator& it ); | ||
85 | |||
86 | /** | ||
87 | * the current item | ||
88 | */ | ||
89 | uint current() const; | ||
90 | |||
91 | /** | ||
92 | * the number of items | ||
93 | */ | ||
94 | uint count() const; | ||
95 | |||
96 | /** | ||
97 | * sets the current item | ||
98 | */ | ||
99 | void setCurrent( uint cur ); | ||
100 | |||
101 | private: | ||
102 | QArray<int> m_uids; | ||
103 | uint m_current; | ||
104 | const Base* m_temp; | ||
105 | bool m_end : 1; | ||
106 | T m_record; | ||
107 | bool m_direction : 1; | ||
108 | |||
109 | /* d pointer for future versions */ | ||
110 | OPimRecordListIteratorPrivate *d; | ||
111 | }; | ||
112 | |||
113 | |||
114 | class OPimRecordListPrivate; | ||
115 | /** | ||
116 | * The recordlist used as a return type | ||
117 | * from OPimAccessTemplate | ||
118 | */ | ||
119 | template <class T = OPimRecord > | ||
120 | class OPimRecordList | ||
121 | { | ||
122 | public: | ||
123 | typedef OTemplateBase<T> Base; | ||
124 | typedef OPimRecordListIterator<T> Iterator; | ||
125 | |||
126 | /** | ||
127 | * c'tor | ||
128 | */ | ||
129 | OPimRecordList () | ||
130 | {} | ||
131 | OPimRecordList( const QArray<int>& ids, | ||
132 | const Base* ); | ||
133 | ~OPimRecordList(); | ||
134 | |||
135 | /** | ||
136 | * the first iterator | ||
137 | */ | ||
138 | Iterator begin(); | ||
139 | |||
140 | /** | ||
141 | * the end | ||
142 | */ | ||
143 | Iterator end(); | ||
144 | |||
145 | /** | ||
146 | * the number of items in the list | ||
147 | */ | ||
148 | uint count() const; | ||
149 | |||
150 | T operator[] ( uint i ); | ||
151 | int uidAt( uint i ); | ||
152 | |||
153 | /** | ||
154 | * Remove the contact with given uid | ||
155 | */ | ||
156 | bool remove( int uid ); | ||
157 | |||
158 | /* | ||
159 | ConstIterator begin()const; | ||
160 | ConstIterator end()const; | ||
161 | */ | ||
162 | private: | ||
163 | QArray<int> m_ids; | ||
164 | const Base* m_acc; | ||
165 | OPimRecordListPrivate *d; | ||
166 | }; | ||
167 | |||
168 | |||
169 | /* ok now implement it */ | ||
170 | template <class T> | ||
171 | OPimRecordListIterator<T>::OPimRecordListIterator() | ||
172 | { | ||
173 | m_current = 0; | ||
174 | m_temp = 0l; | ||
175 | m_end = true; | ||
176 | m_record = T(); | ||
177 | /* forward */ | ||
178 | m_direction = TRUE; | ||
179 | } | ||
180 | |||
181 | |||
182 | template <class T> | ||
183 | OPimRecordListIterator<T>::~OPimRecordListIterator() | ||
184 | { | ||
185 | /* nothing to delete */ | ||
186 | } | ||
187 | |||
188 | |||
189 | template <class T> | ||
190 | OPimRecordListIterator<T>::OPimRecordListIterator( const OPimRecordListIterator<T>& it ) | ||
191 | { | ||
192 | // qWarning("OPimRecordListIterator copy c'tor"); | ||
193 | m_uids = it.m_uids; | ||
194 | m_current = it.m_current; | ||
195 | m_temp = it.m_temp; | ||
196 | m_end = it.m_end; | ||
197 | m_record = it.m_record; | ||
198 | m_direction = it.m_direction; | ||
199 | } | ||
200 | |||
201 | |||
202 | template <class T> | ||
203 | OPimRecordListIterator<T> &OPimRecordListIterator<T>::operator=( const OPimRecordListIterator<T>& it ) | ||
204 | { | ||
205 | m_uids = it.m_uids; | ||
206 | m_current = it.m_current; | ||
207 | m_temp = it.m_temp; | ||
208 | m_end = it.m_end; | ||
209 | m_record = it.m_record; | ||
210 | |||
211 | return *this; | ||
212 | } | ||
213 | |||
214 | |||
215 | template <class T> | ||
216 | T OPimRecordListIterator<T>::operator*() | ||
217 | { | ||
218 | //qWarning("operator* %d %d", m_current, m_uids[m_current] ); | ||
219 | if ( !m_end ) | ||
220 | m_record = m_temp->find( m_uids[ m_current ], m_uids, m_current, | ||
221 | m_direction ? Base::Forward : | ||
222 | Base::Reverse ); | ||
223 | else | ||
224 | m_record = T(); | ||
225 | |||
226 | return m_record; | ||
227 | } | ||
228 | |||
229 | |||
230 | template <class T> | ||
231 | OPimRecordListIterator<T> &OPimRecordListIterator<T>::operator++() | ||
232 | { | ||
233 | m_direction = true; | ||
234 | if ( m_current < m_uids.count() ) | ||
235 | { | ||
236 | m_end = false; | ||
237 | ++m_current; | ||
238 | } | ||
239 | else | ||
240 | m_end = true; | ||
241 | |||
242 | return *this; | ||
243 | } | ||
244 | |||
245 | |||
246 | template <class T> | ||
247 | OPimRecordListIterator<T> &OPimRecordListIterator<T>::operator--() | ||
248 | { | ||
249 | m_direction = false; | ||
250 | if ( m_current > 0 ) | ||
251 | { | ||
252 | --m_current; | ||
253 | m_end = false; | ||
254 | } | ||
255 | else | ||
256 | m_end = true; | ||
257 | |||
258 | return *this; | ||
259 | } | ||
260 | |||
261 | |||
262 | template <class T> | ||
263 | bool OPimRecordListIterator<T>::operator==( const OPimRecordListIterator<T>& it ) | ||
264 | { | ||
265 | |||
266 | /* if both are at we're the same.... */ | ||
267 | if ( m_end == it.m_end ) return true; | ||
268 | |||
269 | if ( m_uids != it.m_uids ) return false; | ||
270 | if ( m_current != it.m_current ) return false; | ||
271 | if ( m_temp != it.m_temp ) return false; | ||
272 | |||
273 | return true; | ||
274 | } | ||
275 | |||
276 | |||
277 | template <class T> | ||
278 | bool OPimRecordListIterator<T>::operator!=( const OPimRecordListIterator<T>& it ) | ||
279 | { | ||
280 | return !( *this == it ); | ||
281 | } | ||
282 | |||
283 | |||
284 | template <class T> | ||
285 | OPimRecordListIterator<T>::OPimRecordListIterator( const QArray<int> uids, | ||
286 | const Base* t ) | ||
287 | : m_uids( uids ), m_current( 0 ), m_temp( t ), m_end( false ), | ||
288 | m_direction( false ) | ||
289 | { | ||
290 | /* if the list is empty we're already at the end of the list */ | ||
291 | if ( uids.count() == 0 ) | ||
292 | m_end = true; | ||
293 | } | ||
294 | |||
295 | |||
296 | template <class T> | ||
297 | uint OPimRecordListIterator<T>::current() const | ||
298 | { | ||
299 | return m_current; | ||
300 | } | ||
301 | |||
302 | |||
303 | template <class T> | ||
304 | void OPimRecordListIterator<T>::setCurrent( uint cur ) | ||
305 | { | ||
306 | if ( cur < m_uids.count() ) | ||
307 | { | ||
308 | m_end = false; | ||
309 | m_current = cur; | ||
310 | } | ||
311 | } | ||
312 | template <class T> | ||
313 | uint OPimRecordListIterator<T>::count() const | ||
314 | { | ||
315 | return m_uids.count(); | ||
316 | } | ||
317 | |||
318 | |||
319 | template <class T> | ||
320 | OPimRecordList<T>::OPimRecordList( const QArray<int>& ids, | ||
321 | const Base* acc ) | ||
322 | : m_ids( ids ), m_acc( acc ) | ||
323 | {} | ||
324 | |||
325 | |||
326 | template <class T> | ||
327 | OPimRecordList<T>::~OPimRecordList() | ||
328 | { | ||
329 | /* nothing to do here */ | ||
330 | } | ||
331 | |||
332 | |||
333 | template <class T> | ||
334 | typename OPimRecordList<T>::Iterator OPimRecordList<T>::begin() | ||
335 | { | ||
336 | Iterator it( m_ids, m_acc ); | ||
337 | return it; | ||
338 | } | ||
339 | |||
340 | |||
341 | template <class T> | ||
342 | typename OPimRecordList<T>::Iterator OPimRecordList<T>::end() | ||
343 | { | ||
344 | Iterator it( m_ids, m_acc ); | ||
345 | it.m_end = true; | ||
346 | it.m_current = m_ids.count(); | ||
347 | |||
348 | return it; | ||
349 | } | ||
350 | |||
351 | |||
352 | template <class T> | ||
353 | uint OPimRecordList<T>::count() const | ||
354 | { | ||
355 | return m_ids.count(); | ||
356 | } | ||
357 | |||
358 | |||
359 | template <class T> | ||
360 | T OPimRecordList<T>::operator[] ( uint i ) | ||
361 | { | ||
362 | if ( i >= m_ids.count() ) | ||
363 | return T(); | ||
364 | /* forward */ | ||
365 | return m_acc->find( m_ids[ i ], m_ids, i ); | ||
366 | } | ||
367 | |||
368 | |||
369 | template <class T> | ||
370 | int OPimRecordList<T>::uidAt( uint i ) | ||
371 | { | ||
372 | return m_ids[ i ]; | ||
373 | } | ||
374 | |||
375 | |||
376 | template <class T> | ||
377 | bool OPimRecordList<T>::remove( int uid ) | ||
378 | { | ||
379 | QArray<int> copy( m_ids.count() ); | ||
380 | int counter = 0; | ||
381 | bool ret_val = false; | ||
382 | |||
383 | for ( uint i = 0; i < m_ids.count(); i++ ) | ||
384 | { | ||
385 | if ( m_ids[ i ] != uid ) | ||
386 | { | ||
387 | copy[ counter++ ] = m_ids[ i ]; | ||
388 | |||
389 | } | ||
390 | else | ||
391 | ret_val = true; | ||
392 | } | ||
393 | |||
394 | copy.resize( counter ); | ||
395 | m_ids = copy; | ||
396 | |||
397 | |||
398 | return ret_val; | ||
399 | } | ||
400 | |||
401 | } | ||
402 | #endif | ||