summaryrefslogtreecommitdiff
path: root/noncore/unsupported/libopie/pim/opimaccesstemplate.h
Unidiff
Diffstat (limited to 'noncore/unsupported/libopie/pim/opimaccesstemplate.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/unsupported/libopie/pim/opimaccesstemplate.h302
1 files changed, 302 insertions, 0 deletions
diff --git a/noncore/unsupported/libopie/pim/opimaccesstemplate.h b/noncore/unsupported/libopie/pim/opimaccesstemplate.h
new file mode 100644
index 0000000..ecbeb68
--- a/dev/null
+++ b/noncore/unsupported/libopie/pim/opimaccesstemplate.h
@@ -0,0 +1,302 @@
1#ifndef OPIE_PIM_ACCESS_TEMPLATE_H
2#define OPIE_PIM_ACCESS_TEMPLATE_H
3
4#include <qarray.h>
5
6#include <opie/opimrecord.h>
7#include <opie/opimaccessbackend.h>
8#include <opie/orecordlist.h>
9
10#include "opimcache.h"
11#include "otemplatebase.h"
12
13class OPimAccessTemplatePrivate;
14/**
15 * Thats the frontend to our OPIE PIM
16 * Library. Either you want to use it's
17 * interface or you want to implement
18 * your own Access lib
19 * Just create a OPimRecord and inherit from
20 * the plugins
21 */
22
23template <class T = OPimRecord >
24class OPimAccessTemplate : public OTemplateBase<T> {
25public:
26 enum Access {
27 Random = 0,
28 SortedAccess
29 };
30 typedef ORecordList<T> List;
31 typedef OPimAccessBackend<T> BackEnd;
32 typedef OPimCache<T> Cache;
33
34 /**
35 * c'tor BackEnd
36 * enum Access a small hint on how to handle the backend
37 */
38 OPimAccessTemplate( BackEnd* end);
39
40 virtual ~OPimAccessTemplate();
41
42 /**
43 * load from the backend
44 */
45 bool load();
46
47 /** Reload database.
48 * You should execute this function if the external database
49 * was changed.
50 * This function will load the external database and afterwards
51 * rejoin the local changes. Therefore the local database will be set consistent.
52 */
53 virtual bool reload();
54
55 /** Save contacts database.
56 * Save is more a "commit". After calling this function, all changes are public available.
57 * @return true if successful
58 */
59 bool save();
60
61 /**
62 * if the resource was changed externally
63 * You should use the signal handling instead of polling possible changes !
64 * zecke: Do you implement a signal for otodoaccess ?
65 */
66 bool wasChangedExternally()const;
67
68 /**
69 * return a List of records
70 * you can iterate over them
71 */
72 virtual List allRecords()const;
73
74 /**
75 * return a List of records
76 * that match the regex
77 */
78 virtual List matchRegexp( const QRegExp &r ) const;
79
80 /**
81 * queryByExample.
82 * @see otodoaccess, ocontactaccess
83 */
84 virtual List queryByExample( const T& t, int querySettings, const QDateTime& d = QDateTime() );
85
86 /**
87 * find the OPimRecord uid
88 */
89 virtual T find( int uid )const;
90
91 /**
92 * read ahead cache find method ;)
93 */
94 virtual T find( int uid, const QArray<int>&,
95 uint current, typename OTemplateBase<T>::CacheDirection dir = OTemplateBase<T>::Forward )const;
96
97 /* invalidate cache here */
98 /**
99 * clears the backend and invalidates the backend
100 */
101 void clear() ;
102
103 /**
104 * add T to the backend
105 * @param t The item to add.
106 * @return <i>true</i> if added successfully.
107 */
108 virtual bool add( const T& t ) ;
109 bool add( const OPimRecord& );
110
111 /* only the uid matters */
112 /**
113 * remove T from the backend
114 * @param t The item to remove
115 * @return <i>true</i> if successful.
116 */
117 virtual bool remove( const T& t );
118
119 /**
120 * remove the OPimRecord with uid
121 * @param uid The ID of the item to remove
122 * @return <i>true</i> if successful.
123 */
124 bool remove( int uid );
125 bool remove( const OPimRecord& );
126
127 /**
128 * replace T from backend
129 * @param t The item to replace
130 * @return <i>true</i> if successful.
131 */
132 virtual bool replace( const T& t) ;
133
134 void setReadAhead( uint count );
135 /**
136 * @internal
137 */
138 void cache( const T& )const;
139 void setSaneCacheSize( int );
140
141 QArray<int> records()const;
142protected:
143 /**
144 * invalidate the cache
145 */
146 void invalidateCache();
147
148 void setBackEnd( BackEnd* end );
149 /**
150 * returns the backend
151 */
152 BackEnd* backEnd();
153 BackEnd* m_backEnd;
154 Cache m_cache;
155
156private:
157 OPimAccessTemplatePrivate *d;
158
159};
160
161template <class T>
162OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end )
163 : OTemplateBase<T>(), m_backEnd( end )
164{
165 if (end )
166 end->setFrontend( this );
167}
168template <class T>
169OPimAccessTemplate<T>::~OPimAccessTemplate() {
170 qWarning("~OPimAccessTemplate<T>");
171 delete m_backEnd;
172}
173template <class T>
174bool OPimAccessTemplate<T>::load() {
175 invalidateCache();
176 return m_backEnd->load();
177}
178template <class T>
179bool OPimAccessTemplate<T>::reload() {
180 invalidateCache(); // zecke: I think this should be added (se)
181 return m_backEnd->reload();
182}
183template <class T>
184bool OPimAccessTemplate<T>::save() {
185 return m_backEnd->save();
186}
187template <class T>
188typename OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const {
189 QArray<int> ints = m_backEnd->allRecords();
190 List lis(ints, this );
191 return lis;
192}
193template <class T>
194typename OPimAccessTemplate<T>::List OPimAccessTemplate<T>::matchRegexp( const QRegExp &r )const {
195 QArray<int> ints = m_backEnd->matchRegexp( r );
196 List lis(ints, this );
197 return lis;
198}
199template <class T>
200QArray<int> OPimAccessTemplate<T>::records()const {
201 return m_backEnd->allRecords();
202}
203template <class T>
204typename OPimAccessTemplate<T>::List
205OPimAccessTemplate<T>::queryByExample( const T& t, int settings, const QDateTime& d ) {
206 QArray<int> ints = m_backEnd->queryByExample( t, settings, d );
207
208 List lis(ints, this );
209 return lis;
210}
211template <class T>
212T OPimAccessTemplate<T>::find( int uid ) const{
213 T t = m_backEnd->find( uid );
214 cache( t );
215 return t;
216}
217template <class T>
218T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar,
219 uint current, typename OTemplateBase<T>::CacheDirection dir )const {
220 /*
221 * better do T.isEmpty()
222 * after a find this way we would
223 * avoid two finds in QCache...
224 */
225 // qWarning("find it now %d", uid );
226 if (m_cache.contains( uid ) ) {
227 return m_cache.find( uid );
228 }
229
230 T t = m_backEnd->find( uid, ar, current, dir );
231 cache( t );
232 return t;
233}
234template <class T>
235void OPimAccessTemplate<T>::clear() {
236 invalidateCache();
237 m_backEnd->clear();
238}
239template <class T>
240bool OPimAccessTemplate<T>::add( const T& t ) {
241 cache( t );
242 return m_backEnd->add( t );
243}
244template <class T>
245bool OPimAccessTemplate<T>::add( const OPimRecord& rec) {
246 /* same type */
247 if ( rec.rtti() == T::rtti() ) {
248 const T &t = static_cast<const T&>(rec);
249 return add(t);
250 }
251 return false;
252}
253template <class T>
254bool OPimAccessTemplate<T>::remove( const T& t ) {
255 return remove( t.uid() );
256}
257template <class T>
258bool OPimAccessTemplate<T>::remove( int uid ) {
259 m_cache.remove( uid );
260 return m_backEnd->remove( uid );
261}
262template <class T>
263bool OPimAccessTemplate<T>::remove( const OPimRecord& rec) {
264 return remove( rec.uid() );
265}
266template <class T>
267bool OPimAccessTemplate<T>::replace( const T& t ) {
268 m_cache.replace( t );
269 return m_backEnd->replace( t );
270}
271template <class T>
272void OPimAccessTemplate<T>::invalidateCache() {
273 m_cache.invalidate();
274}
275template <class T>
276typename OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() {
277 return m_backEnd;
278}
279template <class T>
280bool OPimAccessTemplate<T>::wasChangedExternally()const {
281 return false;
282}
283template <class T>
284void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) {
285 m_backEnd = end;
286 if (m_backEnd )
287 m_backEnd->setFrontend( this );
288}
289template <class T>
290void OPimAccessTemplate<T>::cache( const T& t ) const{
291 /* hacky we need to work around the const*/
292 ((OPimAccessTemplate<T>*)this)->m_cache.add( t );
293}
294template <class T>
295void OPimAccessTemplate<T>::setSaneCacheSize( int size ) {
296 m_cache.setSize( size );
297}
298template <class T>
299void OPimAccessTemplate<T>::setReadAhead( uint count ) {
300 m_backEnd->setReadAhead( count );
301}
302#endif