summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/orecordlist.h
blob: 8f0011bf756ac7a0b0f48637455889b8a51c9c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
                             This file is part of the Opie Project
                             Copyright (C) The Main Author <main-author@whereever.org>
              =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
 :     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.
*/

#ifndef OPIE_RECORD_LIST_H
#define OPIE_RECORD_LIST_H

#include <qarray.h>

#include <opie2/otemplatebase.h>
#include <opie2/opimrecord.h>

namespace Opie {

class ORecordListIteratorPrivate;
/**
 * Our List Iterator
 * it behaves like STL or Qt
 *
 * for(it = list.begin(); it != list.end(); ++it )
 *   doSomeCoolStuff( (*it) );
 */
template <class T> class ORecordList;
template <class T = OPimRecord>
class ORecordListIterator {
    friend class ORecordList<T>;
public:
    typedef OTemplateBase<T> Base;

    /**
     * The c'tor used internally from
     * ORecordList
     */
    ORecordListIterator( const QArray<int>, const Base* );

    /**
     * The standard c'tor
     */
    ORecordListIterator();
    ~ORecordListIterator();

    ORecordListIterator( const ORecordListIterator& );
    ORecordListIterator &operator=(const ORecordListIterator& );

    /**
     * a * operator ;)
     * use it like this T = (*it);
     */
    T operator*();
    ORecordListIterator &operator++();
    ORecordListIterator &operator--();

    bool operator==( const ORecordListIterator& it );
    bool operator!=( const ORecordListIterator& it );

    /**
     * the current item
     */
    uint current()const;

    /**
     * the number of items
     */
    uint count()const;

    /**
     * sets the current item
     */
    void setCurrent( uint cur );

private:
    QArray<int> m_uids;
    uint m_current;
    const Base* m_temp;
    bool m_end : 1;
    T m_record;
    bool m_direction :1;

    /* d pointer for future versions */
    ORecordListIteratorPrivate *d;
};

class ORecordListPrivate;
/**
 * The recordlist used as a return type
 * from OPimAccessTemplate
 */
template <class T = OPimRecord >
class ORecordList {
public:
    typedef OTemplateBase<T> Base;
    typedef ORecordListIterator<T> Iterator;

    /**
     * c'tor
     */
    ORecordList () {
    }
ORecordList( const QArray<int>& ids,
                 const Base* );
    ~ORecordList();

    /**
     * the first iterator
     */
    Iterator begin();

    /**
     * the end
     */
    Iterator end();

    /**
     * the number of items in the list
     */
     uint count()const;

    T operator[]( uint i );
    int uidAt(uint i );

   /**
    * Remove the contact with given uid
    */
    bool remove( int uid );

    /*
      ConstIterator begin()const;
      ConstIterator end()const;
    */
private:
    QArray<int> m_ids;
    const Base* m_acc;
    ORecordListPrivate *d;
};

/* ok now implement it  */
template <class T>
ORecordListIterator<T>::ORecordListIterator() {
    m_current = 0;
    m_temp = 0l;
    m_end = true;
    m_record = T();
    /* forward */
    m_direction = TRUE;
}
template <class T>
ORecordListIterator<T>::~ORecordListIterator() {
/* nothing to delete */
}

template <class T>
ORecordListIterator<T>::ORecordListIterator( const ORecordListIterator<T>& it) {
//    qWarning("ORecordListIterator copy c'tor");
    m_uids = it.m_uids;
    m_current = it.m_current;
    m_temp = it.m_temp;
    m_end = it.m_end;
    m_record = it.m_record;
    m_direction = it.m_direction;
}

template <class T>
ORecordListIterator<T> &ORecordListIterator<T>::operator=( const ORecordListIterator<T>& it) {
    m_uids = it.m_uids;
    m_current = it.m_current;
    m_temp = it.m_temp;
    m_end = it.m_end;
    m_record = it.m_record;

    return *this;
}

template <class T>
T ORecordListIterator<T>::operator*() {
	//qWarning("operator* %d %d", m_current,  m_uids[m_current] );
    if (!m_end )
        m_record = m_temp->find( m_uids[m_current], m_uids, m_current,
                                 m_direction ? Base::Forward :
                                 Base::Reverse  );
    else
        m_record = T();

    return m_record;
}

template <class T>
ORecordListIterator<T> &ORecordListIterator<T>::operator++() {
    m_direction = true;
    if (m_current < m_uids.count() ) {
        m_end = false;
        ++m_current;
    }else
        m_end = true;

    return *this;
}
template <class T>
ORecordListIterator<T> &ORecordListIterator<T>::operator--() {
    m_direction = false;
    if ( m_current > 0 ) {
        --m_current;
        m_end = false;
    }  else
        m_end = true;

    return *this;
}

template <class T>
bool ORecordListIterator<T>::operator==( const ORecordListIterator<T>& it ) {

    /* if both are at we're the same.... */
    if ( m_end == it.m_end ) return true;

    if ( m_uids != it.m_uids ) return false;
    if ( m_current != it.m_current ) return false;
    if ( m_temp != it.m_temp ) return false;

    return true;
}
template <class T>
bool ORecordListIterator<T>::operator!=( const ORecordListIterator<T>& it ) {
    return !(*this == it );
}
template <class T>
ORecordListIterator<T>::ORecordListIterator( const QArray<int> uids,
                                  const Base* t )
    : m_uids( uids ), m_current( 0 ),  m_temp( t ), m_end( false ),
      m_direction( false )
{
    /* if the list is empty we're already at the end of the list */
    if (uids.count() == 0 )
        m_end = true;
}
template <class T>
uint ORecordListIterator<T>::current()const {
    return m_current;
}
template <class T>
void ORecordListIterator<T>::setCurrent( uint cur ) {
    if( cur < m_uids.count() ) {
	m_end = false;
	m_current= cur;
    }
}
template <class T>
uint ORecordListIterator<T>::count()const {
    return m_uids.count();
}
template <class T>
ORecordList<T>::ORecordList( const QArray<int>& ids,
                             const Base* acc )
    : m_ids( ids ), m_acc( acc )
{
}
template <class T>
ORecordList<T>::~ORecordList() {
/* nothing to do here */
}
template <class T>
typename ORecordList<T>::Iterator ORecordList<T>::begin() {
    Iterator it( m_ids, m_acc );
    return it;
}
template <class T>
typename ORecordList<T>::Iterator ORecordList<T>::end() {
    Iterator it( m_ids, m_acc );
    it.m_end = true;
    it.m_current = m_ids.count();

    return it;
}
template <class T>
uint ORecordList<T>::count()const {
return m_ids.count();
}
template <class T>
T ORecordList<T>::operator[]( uint i ) {
    if ( i >= m_ids.count() )
        return T();
    /* forward */
    return m_acc->find( m_ids[i], m_ids, i );
}
template <class T>
int ORecordList<T>::uidAt( uint i ) {
    return m_ids[i];
}

template <class T>
bool ORecordList<T>::remove( int uid ) {
	QArray<int> copy( m_ids.count() );
	int counter = 0;
	bool ret_val = false;

	for (uint i = 0; i < m_ids.count(); i++){
		if ( m_ids[i] != uid ){
			copy[counter++] = m_ids[i];

		}else
			ret_val = true;
	}

	copy.resize( counter );
	m_ids = copy;


	return ret_val;
}

}
#endif