summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/opimaccessbackend.h
blob: 0d112c9d4eb1158a47ed18e3297639146ef41c82 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
                             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_PIM_ACCESS_BACKEND
#define OPIE_PIM_ACCESS_BACKEND

#include <qarray.h>
#include <qdatetime.h>

#include <opie2/opimtemplatebase.h>
#include <opie2/opimrecord.h>
#include <opie2/opimbackendoccurrence.h>

namespace Opie {
class OPimAccessBackendPrivate;

/**
 * OPimAccessBackend is the Backend Interface to be used
 * by OTemplateBase based Frontends.
 * For efficency reasons and to support delayed loading
 * most of the Frontend functions can be implemented
 * by this backend.
 * This allows to utilise the best method on each backend.
 * For example we can use SQL queries instead of self made
 * query which is first more efficent and also uses less memory.
 */
template <class T = OPimRecord>
class OPimAccessBackend {
public:
    typedef OTemplateBase<T> Frontend;

    //@{
    OPimAccessBackend(int access = 0);
    virtual ~OPimAccessBackend();
    //@}

    //@{
    virtual bool load() = 0;
    virtual bool reload() = 0;
    virtual bool save() = 0;
    virtual void clear() = 0;
    //@}


    //@{
    virtual UIDArray allRecords()const = 0;
    virtual UIDArray matchRegexp(const QRegExp &r) const;
    virtual UIDArray queryByExample( const T& t, int settings, const QDateTime& d = QDateTime() )const = 0;
    virtual UIDArray queryByExample( const OPimRecord* rec, int, const QDateTime& d = QDateTime() )const;
    virtual UIDArray sorted( const UIDArray&, bool asc, int sortOrder, int sortFilter,  const QArray<int>& cats )const;
    virtual UIDArray sorted( bool asc, int sortOrder, int sortFilter, const QArray<int>& cats )const;
    virtual OPimBackendOccurrence::List occurrences( const QDate& start, const QDate& end)const;
    virtual OPimBackendOccurrence::List occurrences( const QDateTime& dt )const;
    //@}


    //@{
    virtual T find(UID uid )const  = 0;
    virtual T find(UID uid, const QArray<UID>& items,
                   uint current, typename Frontend::CacheDirection )const ;
    //@}


    //@{
    virtual bool add( const T& t ) = 0;
    virtual bool remove( UID uid ) = 0;
    virtual bool replace( const T& t ) = 0;
    //@}



    void setFrontend( Frontend* front );

    /**
     * set the read ahead count
     */
    void setReadAhead( uint count );
protected:
    //@{
    int access()const;
    void cache( const T& t )const;
    void setSaneCacheSize( int );
    uint readAhead()const;
    //@}

private:
    OPimAccessBackendPrivate *d;
    Frontend* m_front;
    uint m_read;
    int m_acc;

};

template <class T>
OPimAccessBackend<T>::OPimAccessBackend(int acc)
    : m_acc( acc )
{
    m_front = 0l;
}
template <class T>
OPimAccessBackend<T>::~OPimAccessBackend() {

}

/*
 * Slow but default matchRegexp Implementation
 * Create a Big Enough QArray and then iterate
 * over all Records and matchRegexp them.
 * At the end we will resize the array to the actual
 * number of items
 */
template <class T>
UIDArray OPimAccessBackend<T>::matchRegexp( const QRegExp& reg )const {
    UIDArray all_rec = allRecords();
    UIDArray result( all_rec.count() );
    uint used_records = 0, all_rec_count = all_rec.count();

    for ( uint i = 0; i < all_rec_count; ++i )
        if (find( all_rec[i], all_rec, i, Frontend::Forward ).match( reg ) )
            result[used_records++] = all_rec[i];

    /* shrink to fit */
    result.resize( used_records );
    return result;
}

template <class T>
UIDArray OPimAccessBackend<T>::queryByExample( const OPimRecord* rec, int settings,
                                               const QDateTime& datetime )const {
    T* tmp_rec = T::safeCast( rec );
    UIDArray ar;
    if ( tmp_rec )
        ar = queryByExample( *tmp_rec, settings, datetime );

    return ar;
}

template <class T>
UIDArray OPimAccessBackend<T>::sorted( const UIDArray& ids, bool,
                                       int, int, const QArray<int>& ) const {
    return ids;
}

template <class T>
UIDArray OPimAccessBackend<T>::sorted( bool asc, int order, int filter,
                                       const QArray<int>& cats )const {
    return sorted( allRecords(), asc, order, filter, cats );
}

template<class T>
OPimBackendOccurrence::List OPimAccessBackend<T>::occurrences( const QDate&,
                                                               const QDate& )const {
    return OPimBackendOccurrence::List();
}

template<class T>
OPimBackendOccurrence::List OPimAccessBackend<T>::occurrences( const QDateTime& dt )const {
    QDate date = dt.date();
    return occurrences( date, date );
}

template <class T>
void OPimAccessBackend<T>::setFrontend( Frontend* fr ) {
    m_front = fr;
}
template <class T>
void OPimAccessBackend<T>::cache( const T& t )const {
    if ( m_front )
        m_front->cache( t );
}

template <class T>
void OPimAccessBackend<T>::setSaneCacheSize( int size) {
    if ( m_front )
        m_front->setSaneCacheSize( size );
}
template <class T>
T OPimAccessBackend<T>::find( int uid, const QArray<int>&,
                              uint, typename Frontend::CacheDirection ) const{
    qDebug( "*** Lookahead feature not supported. Fallback to default find!!" );
    return find( uid );
}
template <class T>
void OPimAccessBackend<T>::setReadAhead( uint count ) {
    m_read = count;
}
template <class T>
uint OPimAccessBackend<T>::readAhead()const {
    return m_read;
}
template <class T>
int OPimAccessBackend<T>::access()const {
    return m_acc;
}

}

/**
 * \fn template <class T> OPimAccessBackend<T>::OPimAccessBackend(int hint )
 * @param hint The access hint from the frontend
 */

/**
 * \fn template <class T> bool OPimAccessBackend<T>::load()
 *  Opens the DataBase and does necessary
 *  initialisation of internal structures.
 *
 *  @return true If the DataBase could be opened and
 *          Information was successfully loaded
 */

/**
 * \fn template <class T> bool OPimAccessBackend<T>::reload()
 * Reinitialise the DataBase and merges the external changes
 * with your local changes.
 *
 * @return True if the DataBase was reloaded.
 *
 */

/**
 * \fn template <class T> bool OPimAccessBackend<T>::save()
 *
 * Save the changes to storage. In case of memory or
 * disk shortage, return false.
 *
 *
 * @return True if the DataBase could be saved to storage.
 */

/**
 * \fn template <class T> bool OPimAccessBackend<T>::clear()
 * Until a \sa save() changes shouldn't be comitted
 *
 *
 * @return True if the DataBase could be cleared
 * @todo Introduce a 'Commit'
 */

/**
 * \fn template <class T> QArray<UID> OPimAccessBackend<T>::allRecords()const
 *  Return an array of  all available uids in the loaded
 *  DataBase.
 *  @see load
 */

/**
 * \fn template <class T> QArray<UID> OPimAccessBackend<T>::matchRegexp(const QRegExp& r)const
 * Return a List of records  that match the regex \par r.
 *
 * @param r The QRegExp to match.
 */

/**
 * \fn template <class T> QArray<UID> OPimAccessBackend<T>::queryByExample(const T& t, int settings, const QDateTime& d = QDateTime() )
 *
 * Implement QueryByExample. An Example record is filled and with the
 * settings and QDateTime it is determined how the query should be executed.
 * Return a list of UIDs that match the Example
 *
 * @param t The Example record
 * @param settings Gives
 *
 */

/**
 * \fn template<class T> QArray<UID> OPimAccessBackend<T>::sorted(const QArray<UID>& ids, bool asc, int sortOrder, int sortFilter, int cat)
 * \brief Sort the List of records according to the preference
 *
 * Implement sorting in your backend. The default implementation is
 * to return the list as it was passed.
 * The default Backend Implementation should do unaccelerated filtering
 *
 *
 * @param ids        The Records to sort
 * @param asc        Sort ascending or descending
 * @param sortOrder
 * @param sortFilter Sort filter
 * @param cat        The Category to include
 */

/**
 * \fn template <class T> T OPimAccessBackend<T>::find(UID uid)const
 * \brief Find the Record with the UID
 *
 * Find the UID in the database and return the record.
 * @param uid The uid to be searched for
 * @return The record or an empty record (T.isEmpty())
 *
 */

/**
 * \fn template <class T> T OPimAccessBackend<T>::find( UID uid, const QArray<UID>& items, uint current, typename Frontend::CacheDirection d)const
 * \brief find a Record and do a read ahead or read behind
 *
 * @param uid     The UID to search for
 * @param items   The list of items from where your search
 * @param current The index of \param uid
 * @param d       The direction to search for
 *
 * @see find
 */


/**
 * \fn template<class T> bool OPimAccessBackend<T>::add(const T& t)
 *
 * \brief Add the record to the internal database
 *
 * If an record with the same t.uid() is already present internally
 * the behaviour is undefined but the state of the database
 * needs to be stable.
 * For modifying a record use \sa replace.
 *
 *
 * @return true if the record could be added or false if not
 * @todo Eilers your opinion on readd/replace
 */

/**
 * \fn template<class T> bool OPimAccessBackend<T>::remove(UID uid)
 * \brief Remove a record by its UID
 *
 * Remove the records with UID from the internal Database.
 *
 * @return True if the record could be removed.
 *
 */

/**
 * \fn template<class T> bool OPimAccessBackend<T>::replace(const T& t)
 * \brief Take this Record and replace the old version.
 *
 * Take \param t as the new record for t.uid(). It is not described
 * what happens if the record is not present in the database.
 * Normally the record is determined by the UID.
 *
 * @param t The record to use internally.
 */

/**
 * \fn template<class T> void OPimAccessBackend<T>::setFrontend( Frontend* fron)
 * \@aram fron The Frontend that uses this backend
 *
 * This function is called by the frontend and is used
 */

/**
 * \fn template<class T> void OPimAccessBackend<T>::setReadAhead(uint count)
 * \brief Set the number of items to Read-Ahead/Read-Behind
 *
 * @param count The number of records to read ahead
 */

/**
 * \fn template<class T> void OPimAccessBackend<T>::cache( const T& t)const
 * \brief Add the Record to the PIM Cache
 *
 * This will add the Record to the PIM cache, which is owned
 * by the FrontEnd. If no FrontEnd is available the item will
 * not be cached.
 *
 *
 * @param t The Item to be added to the Cache
 */

/**
 * \fn template<class T> void OPimAccessBackend<T>::setSaneCacheSize(int items)
 * \brief Give a hint on the number of too cached items
 *
 * Give the Frontend a hint on the number of items to be cached. Use
 * a prime number for best performance.
 *
 * @param items The number of items to be cached
 */

/**
 * \fn template<class T> uint OPimAccessBackend<T>::readAhead()const
 * \brief Return the number of Items to be ReadAhead
 *
 * @return The number of Items to read ahead/read behind
 */

/**
 * \fn template<class T> QArray<OPimBackendOccurence> OPimAccessBackend<T>::occurrences(const QDateTime& start,const QDateTime& end)
 * \brief Get a List of Occurrences for a period of time
 *
 * Return an Array of OPimBackendOccurence for a period of time. If start == end date
 * return only occurrences for the start date. If end is smaller the start date
 * the result is not defined. You could switch dates or return an empty list.
 *
 * @return Return an array of OPimBackendOccurence for the period specified by the parameters
 * @param start The start of the period.
 * @param  end  The end   of the period.
 *
 */

#endif