summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/opimaccesstemplate.h
blob: 7ab1ea5e4440fc70fc2cbd8c99974f1b5869dff8 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
                             This file is part of the Opie Project
                             Copyright (C) Holger Freyther <zecke@handhelds.org>
                 Copyright (C) Stefan Eilers <eilers.stefan@epost.de>
              =.             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_PIM_ACCESS_TEMPLATE_H
#define OPIE_PIM_ACCESS_TEMPLATE_H

/* OPIE */
#include <opie2/opimrecord.h>
#include <opie2/opimaccessbackend.h>
#include <opie2/opimrecordlist.h>

#include <opie2/opimtemplatebase.h>
#include <opie2/odebug.h>

/* QT */
#include <qarray.h>

namespace Opie {

class OPimAccessTemplatePrivate;
/**
 * Thats the frontend to our OPIE PIM
 * Library. Either you want to use it's
 * interface or you want to implement
 * your own Access lib
 * Just create a OPimRecord and inherit from
 * the templates
 */

template <class T = OPimRecord >
class OPimAccessTemplate : public OTemplateBase<T> {
public:
    enum Access {
        Random = 0,
        SortedAccess
    };
    typedef OPimRecordList<T> List;
    typedef OPimAccessBackend<T> BackEnd;
    typedef OPimCache<T> Cache;

    /**
     * c'tor BackEnd
     * enum Access a small hint on how to handle the backend
     */
    OPimAccessTemplate( BackEnd* end);

    virtual ~OPimAccessTemplate();

    /**
     * load from the backend
     */
    bool load();

    /** Reload database.
     * You should execute this function if the external database
     * was changed.
     * This function will load the external database and afterwards
     * rejoin the local changes. Therefore the local database will be set consistent.
     */
    virtual bool reload();

    /** Save contacts database.
     * Save is more a "commit". After calling this function, all changes are public available.
     * @return true if successful
     */
    bool save();

    /**
     * if the resource was changed externally
     * You should use the signal handling instead of polling possible changes !
     * zecke: Do you implement a signal for otodoaccess ?
     */
    bool wasChangedExternally()const;

    /**
     * return a List of records
     * you can iterate over them
     */
    virtual List allRecords()const;

     /**
     * return a List of records
     * that match the regex
     */
    virtual List matchRegexp(  const QRegExp &r ) const;

    /**
     * queryByExample.
     * @see otodoaccess, ocontactaccess
     */
    virtual List queryByExample( const T& t, int querySettings, const QDateTime& d = QDateTime() );

    /**
     * find the OPimRecord uid
     */
    T find( int uid )const;

    /**
     * read ahead cache find method ;)
     */
    T find( int uid, const QArray<int>&,
                    uint current, typename OTemplateBase<T>::CacheDirection dir = OTemplateBase<T>::Forward )const;


    /* invalidate cache here */
    /**
     * clears the backend and invalidates the backend
     */
    void clear() ;

    /**
     * add T to the backend
     * @param t The item to add.
     * @return <i>true</i> if added successfully.
     */
     virtual bool add( const T& t ) ;

    bool add( const OPimRecord& );
    /** 
     * Add an Opie PimRecord.
     * Info: Take this if  you are working with OPimRecords and you need to add it into any database.
     *       But take care that the accessing database is compatible to the real type of OPimRecord !! 
     *       Otherwise this access will be rejected !
     */
    bool add( const OPimRecord* );


    /* only the uid matters */
    /**
     * remove T from the backend
     * @param t The item to remove
     * @return <i>true</i> if successful.
     */
    virtual bool remove( const T& t );

    /**
     * remove the OPimRecord with uid
     * @param uid The ID of the item to remove
     * @return <i>true</i> if successful.
     */
    bool remove( int uid );
    bool remove( const OPimRecord& );

    /**
     * replace T from backend
     * @param t The item to replace
     * @return <i>true</i> if successful.
     */
    virtual bool replace( const T& t) ;

    void setReadAhead( uint count );
    /**
     * @internal
     */
    virtual T cacheFind( int uid )const;
    void cache( const T& )const;
    void setSaneCacheSize( int );

    QArray<int> records()const;
protected:
    /**
     * invalidate the cache
     */
    void invalidateCache();

    void setBackEnd( BackEnd* end );
    /**
     * returns the backend
     */
    BackEnd* backEnd();
    BackEnd* m_backEnd;

    Cache m_cache;

private:
    OPimAccessTemplatePrivate *d;

};

template <class T>
OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end )
    : OTemplateBase<T>(), m_backEnd( end )
{
    if (end )
        end->setFrontend( this );
}
template <class T>
OPimAccessTemplate<T>::~OPimAccessTemplate() {
    owarn << "~OPimAccessTemplate<T>" << oendl;
    delete m_backEnd;
}
template <class T>
bool OPimAccessTemplate<T>::load() {
    invalidateCache();
    return m_backEnd->load();
}
template <class T>
bool OPimAccessTemplate<T>::reload() {
    invalidateCache();
    return m_backEnd->reload();
}
template <class T>
bool OPimAccessTemplate<T>::save() {
    return m_backEnd->save();
}
template <class T>
typename OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const {
    QArray<int> ints = m_backEnd->allRecords();
    List lis(ints, this );
    return lis;
}
template <class T>
typename OPimAccessTemplate<T>::List OPimAccessTemplate<T>::matchRegexp( const QRegExp &r )const {
    QArray<int> ints = m_backEnd->matchRegexp( r );
    List lis(ints, this );
    return lis;
}
template <class T>
QArray<int> OPimAccessTemplate<T>::records()const {
    return m_backEnd->allRecords();
}
template <class T>
typename OPimAccessTemplate<T>::List
OPimAccessTemplate<T>::queryByExample( const T& t, int settings, const QDateTime& d ) {
    QArray<int> ints = m_backEnd->queryByExample( t, settings, d );

    List lis(ints, this );
    return lis;
}
template <class T>
T OPimAccessTemplate<T>::find( int uid ) const{
    // First search in cache..
    if ( m_cache.contains( uid ) )
	    return m_cache.find( uid );

    T t = m_backEnd->find( uid );
    cache( t );

    return t;
}

template <class T>
T OPimAccessTemplate<T>::cacheFind( int uid ) const
{
	return m_cache.find( uid );
}

template <class T>
T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar,
                               uint current, typename OTemplateBase<T>::CacheDirection dir )const {
    /*
     * better do T.isEmpty()
     * after a find this way we would
     * avoid two finds in QCache...
     */
    // owarn << "find it now " << uid << oendl;
    if ( m_cache.contains( uid ) ) {
        return m_cache.find( uid );
    }

    T t = m_backEnd->find( uid, ar, current, dir );
    cache( t );
    return t;
}
template <class T>
void OPimAccessTemplate<T>::clear() {
    invalidateCache();
    m_backEnd->clear();
}
template <class T>
bool OPimAccessTemplate<T>::add( const T& t ) {
    cache( t );
    return m_backEnd->add( t );
}

template <class T>
bool OPimAccessTemplate<T>::add( const OPimRecord& rec ) {
    /* same type */
    T tempInstance;
    if ( rec.rtti() == tempInstance.rtti() ) {
        const T& t = static_cast<const T&>(rec);
        return add(t);
    } else {
	owarn << "Adding not possible: Objecttype mismatch" << oendl;
    }
    return false;
}

template <class T>
bool OPimAccessTemplate<T>::add( const OPimRecord* rec) {
    /* same type, but pointer  */
    T tempInstance;
    if ( rec -> rtti() == tempInstance.rtti() ) {
        const T* t = static_cast<const T*>(rec);
        return add( *t );
    } else {
	owarn << "Adding not possible: Objecttype mismatch" << oendl;
    }
    return false;
}

template <class T>
bool OPimAccessTemplate<T>::remove( const T& t ) {
    return remove( t.uid() );
}
template <class T>
bool OPimAccessTemplate<T>::remove( int uid ) {
    m_cache.remove( uid );
    return m_backEnd->remove( uid );
}
template <class T>
bool OPimAccessTemplate<T>::remove( const OPimRecord& rec) {
    return remove( rec.uid() );
}
template <class T>
bool OPimAccessTemplate<T>::replace( const T& t ) {
    m_cache.replace( t );
    return m_backEnd->replace( t );
}
template <class T>
void OPimAccessTemplate<T>::invalidateCache() {
    m_cache.invalidate();
}
template <class T>
typename OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() {
    return m_backEnd;
}
template <class T>
bool OPimAccessTemplate<T>::wasChangedExternally()const {
    return false;
}
template <class T>
void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) {
    m_backEnd = end;
    if (m_backEnd )
        m_backEnd->setFrontend( this );
}
template <class T>
void OPimAccessTemplate<T>::cache( const T& t ) const{
    /* hacky we need to work around the const*/
    ((OPimAccessTemplate<T>*)this)->m_cache.add( t );
}
template <class T>
void OPimAccessTemplate<T>::setSaneCacheSize( int size ) {
    m_cache.setSize( size );
}
template <class T>
void OPimAccessTemplate<T>::setReadAhead( uint count ) {
    m_backEnd->setReadAhead( count );
}

}

#endif