Diffstat (limited to 'libopie2/opiepim/core/opimaccesstemplate.h') (more/less context) (show whitespace changes)
-rw-r--r-- | libopie2/opiepim/core/opimaccesstemplate.h | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/libopie2/opiepim/core/opimaccesstemplate.h b/libopie2/opiepim/core/opimaccesstemplate.h index 55d600a..6f01b46 100644 --- a/libopie2/opiepim/core/opimaccesstemplate.h +++ b/libopie2/opiepim/core/opimaccesstemplate.h | |||
@@ -26,25 +26,24 @@ | |||
26 | If not, write to the Free Software Foundation, | 26 | If not, write to the Free Software Foundation, |
27 | Inc., 59 Temple Place - Suite 330, | 27 | Inc., 59 Temple Place - Suite 330, |
28 | Boston, MA 02111-1307, USA. | 28 | Boston, MA 02111-1307, USA. |
29 | */ | 29 | */ |
30 | #ifndef OPIE_PIM_ACCESS_TEMPLATE_H | 30 | #ifndef OPIE_PIM_ACCESS_TEMPLATE_H |
31 | #define OPIE_PIM_ACCESS_TEMPLATE_H | 31 | #define OPIE_PIM_ACCESS_TEMPLATE_H |
32 | 32 | ||
33 | /* OPIE */ | 33 | /* OPIE */ |
34 | #include <opie2/opimrecord.h> | 34 | #include <opie2/opimrecord.h> |
35 | #include <opie2/opimaccessbackend.h> | 35 | #include <opie2/opimaccessbackend.h> |
36 | #include <opie2/opimrecordlist.h> | 36 | #include <opie2/opimrecordlist.h> |
37 | 37 | ||
38 | #include <opie2/opimcache.h> | ||
39 | #include <opie2/opimtemplatebase.h> | 38 | #include <opie2/opimtemplatebase.h> |
40 | #include <opie2/odebug.h> | 39 | #include <opie2/odebug.h> |
41 | 40 | ||
42 | /* QT */ | 41 | /* QT */ |
43 | #include <qarray.h> | 42 | #include <qarray.h> |
44 | 43 | ||
45 | namespace Opie { | 44 | namespace Opie { |
46 | 45 | ||
47 | class OPimAccessTemplatePrivate; | 46 | class OPimAccessTemplatePrivate; |
48 | /** | 47 | /** |
49 | * Thats the frontend to our OPIE PIM | 48 | * Thats the frontend to our OPIE PIM |
50 | * Library. Either you want to use it's | 49 | * Library. Either you want to use it's |
@@ -119,42 +118,45 @@ public: | |||
119 | 118 | ||
120 | /** | 119 | /** |
121 | * find the OPimRecord uid | 120 | * find the OPimRecord uid |
122 | */ | 121 | */ |
123 | virtual T find( int uid )const; | 122 | virtual T find( int uid )const; |
124 | 123 | ||
125 | /** | 124 | /** |
126 | * read ahead cache find method ;) | 125 | * read ahead cache find method ;) |
127 | */ | 126 | */ |
128 | virtual T find( int uid, const QArray<int>&, | 127 | virtual T find( int uid, const QArray<int>&, |
129 | uint current, typename OTemplateBase<T>::CacheDirection dir = OTemplateBase<T>::Forward )const; | 128 | uint current, typename OTemplateBase<T>::CacheDirection dir = OTemplateBase<T>::Forward )const; |
130 | 129 | ||
130 | |||
131 | /* invalidate cache here */ | 131 | /* invalidate cache here */ |
132 | /** | 132 | /** |
133 | * clears the backend and invalidates the backend | 133 | * clears the backend and invalidates the backend |
134 | */ | 134 | */ |
135 | void clear() ; | 135 | void clear() ; |
136 | 136 | ||
137 | /** | 137 | /** |
138 | * add T to the backend | 138 | * add T to the backend |
139 | * @param t The item to add. | 139 | * @param t The item to add. |
140 | * @return <i>true</i> if added successfully. | 140 | * @return <i>true</i> if added successfully. |
141 | */ | 141 | */ |
142 | virtual bool add( const T& t ) ; | 142 | virtual bool add( const T& t ) ; |
143 | |||
143 | bool add( const OPimRecord& ); | 144 | bool add( const OPimRecord& ); |
144 | // Needed for real generic access (eilers) | 145 | /** |
145 | // Info: Take this if you are working with OPimRecord, which is a generic base class, and | 146 | * Add an Opie PimRecord. |
146 | // you need to add it into any database, you cannot generate a reference to | 147 | * Info: Take this if you are working with OPimRecords and you need to add it into any database. |
147 | // it and casting may be not approriate, too. | 148 | * But take care that the accessing database is compatible to the real type of OPimRecord !! |
148 | // But take care that the accessing database is compatible to the real type of OPimRecord !! | 149 | * Otherwise this access will be rejected ! |
150 | */ | ||
149 | bool add( const OPimRecord* ); | 151 | bool add( const OPimRecord* ); |
150 | 152 | ||
151 | 153 | ||
152 | /* only the uid matters */ | 154 | /* only the uid matters */ |
153 | /** | 155 | /** |
154 | * remove T from the backend | 156 | * remove T from the backend |
155 | * @param t The item to remove | 157 | * @param t The item to remove |
156 | * @return <i>true</i> if successful. | 158 | * @return <i>true</i> if successful. |
157 | */ | 159 | */ |
158 | virtual bool remove( const T& t ); | 160 | virtual bool remove( const T& t ); |
159 | 161 | ||
160 | /** | 162 | /** |
@@ -167,40 +169,42 @@ public: | |||
167 | 169 | ||
168 | /** | 170 | /** |
169 | * replace T from backend | 171 | * replace T from backend |
170 | * @param t The item to replace | 172 | * @param t The item to replace |
171 | * @return <i>true</i> if successful. | 173 | * @return <i>true</i> if successful. |
172 | */ | 174 | */ |
173 | virtual bool replace( const T& t) ; | 175 | virtual bool replace( const T& t) ; |
174 | 176 | ||
175 | void setReadAhead( uint count ); | 177 | void setReadAhead( uint count ); |
176 | /** | 178 | /** |
177 | * @internal | 179 | * @internal |
178 | */ | 180 | */ |
181 | virtual T cacheFind( int uid )const; | ||
179 | void cache( const T& )const; | 182 | void cache( const T& )const; |
180 | void setSaneCacheSize( int ); | 183 | void setSaneCacheSize( int ); |
181 | 184 | ||
182 | QArray<int> records()const; | 185 | QArray<int> records()const; |
183 | protected: | 186 | protected: |
184 | /** | 187 | /** |
185 | * invalidate the cache | 188 | * invalidate the cache |
186 | */ | 189 | */ |
187 | void invalidateCache(); | 190 | void invalidateCache(); |
188 | 191 | ||
189 | void setBackEnd( BackEnd* end ); | 192 | void setBackEnd( BackEnd* end ); |
190 | /** | 193 | /** |
191 | * returns the backend | 194 | * returns the backend |
192 | */ | 195 | */ |
193 | BackEnd* backEnd(); | 196 | BackEnd* backEnd(); |
194 | BackEnd* m_backEnd; | 197 | BackEnd* m_backEnd; |
198 | |||
195 | Cache m_cache; | 199 | Cache m_cache; |
196 | 200 | ||
197 | private: | 201 | private: |
198 | OPimAccessTemplatePrivate *d; | 202 | OPimAccessTemplatePrivate *d; |
199 | 203 | ||
200 | }; | 204 | }; |
201 | 205 | ||
202 | template <class T> | 206 | template <class T> |
203 | OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) | 207 | OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) |
204 | : OTemplateBase<T>(), m_backEnd( end ) | 208 | : OTemplateBase<T>(), m_backEnd( end ) |
205 | { | 209 | { |
206 | if (end ) | 210 | if (end ) |
@@ -209,25 +213,25 @@ OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) | |||
209 | template <class T> | 213 | template <class T> |
210 | OPimAccessTemplate<T>::~OPimAccessTemplate() { | 214 | OPimAccessTemplate<T>::~OPimAccessTemplate() { |
211 | owarn << "~OPimAccessTemplate<T>" << oendl; | 215 | owarn << "~OPimAccessTemplate<T>" << oendl; |
212 | delete m_backEnd; | 216 | delete m_backEnd; |
213 | } | 217 | } |
214 | template <class T> | 218 | template <class T> |
215 | bool OPimAccessTemplate<T>::load() { | 219 | bool OPimAccessTemplate<T>::load() { |
216 | invalidateCache(); | 220 | invalidateCache(); |
217 | return m_backEnd->load(); | 221 | return m_backEnd->load(); |
218 | } | 222 | } |
219 | template <class T> | 223 | template <class T> |
220 | bool OPimAccessTemplate<T>::reload() { | 224 | bool OPimAccessTemplate<T>::reload() { |
221 | invalidateCache(); // zecke: I think this should be added (se) | 225 | invalidateCache(); |
222 | return m_backEnd->reload(); | 226 | return m_backEnd->reload(); |
223 | } | 227 | } |
224 | template <class T> | 228 | template <class T> |
225 | bool OPimAccessTemplate<T>::save() { | 229 | bool OPimAccessTemplate<T>::save() { |
226 | return m_backEnd->save(); | 230 | return m_backEnd->save(); |
227 | } | 231 | } |
228 | template <class T> | 232 | template <class T> |
229 | typename OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const { | 233 | typename OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const { |
230 | QArray<int> ints = m_backEnd->allRecords(); | 234 | QArray<int> ints = m_backEnd->allRecords(); |
231 | List lis(ints, this ); | 235 | List lis(ints, this ); |
232 | return lis; | 236 | return lis; |
233 | } | 237 | } |
@@ -242,28 +246,40 @@ QArray<int> OPimAccessTemplate<T>::records()const { | |||
242 | return m_backEnd->allRecords(); | 246 | return m_backEnd->allRecords(); |
243 | } | 247 | } |
244 | template <class T> | 248 | template <class T> |
245 | typename OPimAccessTemplate<T>::List | 249 | typename OPimAccessTemplate<T>::List |
246 | OPimAccessTemplate<T>::queryByExample( const T& t, int settings, const QDateTime& d ) { | 250 | OPimAccessTemplate<T>::queryByExample( const T& t, int settings, const QDateTime& d ) { |
247 | QArray<int> ints = m_backEnd->queryByExample( t, settings, d ); | 251 | QArray<int> ints = m_backEnd->queryByExample( t, settings, d ); |
248 | 252 | ||
249 | List lis(ints, this ); | 253 | List lis(ints, this ); |
250 | return lis; | 254 | return lis; |
251 | } | 255 | } |
252 | template <class T> | 256 | template <class T> |
253 | T OPimAccessTemplate<T>::find( int uid ) const{ | 257 | T OPimAccessTemplate<T>::find( int uid ) const{ |
258 | // First search in cache.. | ||
259 | if ( m_cache.contains( uid ) ) | ||
260 | return m_cache.find( uid ); | ||
261 | |||
254 | T t = m_backEnd->find( uid ); | 262 | T t = m_backEnd->find( uid ); |
255 | cache( t ); | 263 | cache( t ); |
264 | |||
256 | return t; | 265 | return t; |
257 | } | 266 | } |
267 | |||
268 | template <class T> | ||
269 | T OPimAccessTemplate<T>::cacheFind( int uid ) const | ||
270 | { | ||
271 | return m_cache.find( uid ); | ||
272 | } | ||
273 | |||
258 | template <class T> | 274 | template <class T> |
259 | T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar, | 275 | T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar, |
260 | uint current, typename OTemplateBase<T>::CacheDirection dir )const { | 276 | uint current, typename OTemplateBase<T>::CacheDirection dir )const { |
261 | /* | 277 | /* |
262 | * better do T.isEmpty() | 278 | * better do T.isEmpty() |
263 | * after a find this way we would | 279 | * after a find this way we would |
264 | * avoid two finds in QCache... | 280 | * avoid two finds in QCache... |
265 | */ | 281 | */ |
266 | // owarn << "find it now " << uid << oendl; | 282 | // owarn << "find it now " << uid << oendl; |
267 | if (m_cache.contains( uid ) ) { | 283 | if (m_cache.contains( uid ) ) { |
268 | return m_cache.find( uid ); | 284 | return m_cache.find( uid ); |
269 | } | 285 | } |
@@ -281,35 +297,39 @@ template <class T> | |||
281 | bool OPimAccessTemplate<T>::add( const T& t ) { | 297 | bool OPimAccessTemplate<T>::add( const T& t ) { |
282 | cache( t ); | 298 | cache( t ); |
283 | return m_backEnd->add( t ); | 299 | return m_backEnd->add( t ); |
284 | } | 300 | } |
285 | 301 | ||
286 | template <class T> | 302 | template <class T> |
287 | bool OPimAccessTemplate<T>::add( const OPimRecord& rec) { | 303 | bool OPimAccessTemplate<T>::add( const OPimRecord& rec) { |
288 | /* same type */ | 304 | /* same type */ |
289 | T tempInstance; | 305 | T tempInstance; |
290 | if ( rec.rtti() == tempInstance.rtti() ) { | 306 | if ( rec.rtti() == tempInstance.rtti() ) { |
291 | const T& t = static_cast<const T&>(rec); | 307 | const T& t = static_cast<const T&>(rec); |
292 | return add(t); | 308 | return add(t); |
309 | } else { | ||
310 | owarn << "Adding not possible: Objecttype mismatch" << oendl; | ||
293 | } | 311 | } |
294 | return false; | 312 | return false; |
295 | } | 313 | } |
296 | 314 | ||
297 | template <class T> | 315 | template <class T> |
298 | bool OPimAccessTemplate<T>::add( const OPimRecord* rec) { | 316 | bool OPimAccessTemplate<T>::add( const OPimRecord* rec) { |
299 | /* same type, but pointer */ | 317 | /* same type, but pointer */ |
300 | T tempInstance; | 318 | T tempInstance; |
301 | if ( rec -> rtti() == tempInstance.rtti() ) { | 319 | if ( rec -> rtti() == tempInstance.rtti() ) { |
302 | const T* t = static_cast<const T*>(rec); | 320 | const T* t = static_cast<const T*>(rec); |
303 | return add( *t ); | 321 | return add( *t ); |
322 | } else { | ||
323 | owarn << "Adding not possible: Objecttype mismatch" << oendl; | ||
304 | } | 324 | } |
305 | return false; | 325 | return false; |
306 | } | 326 | } |
307 | 327 | ||
308 | template <class T> | 328 | template <class T> |
309 | bool OPimAccessTemplate<T>::remove( const T& t ) { | 329 | bool OPimAccessTemplate<T>::remove( const T& t ) { |
310 | return remove( t.uid() ); | 330 | return remove( t.uid() ); |
311 | } | 331 | } |
312 | template <class T> | 332 | template <class T> |
313 | bool OPimAccessTemplate<T>::remove( int uid ) { | 333 | bool OPimAccessTemplate<T>::remove( int uid ) { |
314 | m_cache.remove( uid ); | 334 | m_cache.remove( uid ); |
315 | return m_backEnd->remove( uid ); | 335 | return m_backEnd->remove( uid ); |