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