#ifndef OPIE_PIM_ACCESS_TEMPLATE_H #define OPIE_PIM_ACCESS_TEMPLATE_H #include #include #include #include #include "opimcache.h" #include "otemplatebase.h" 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 plugins */ template class OPimAccessTemplate : public OTemplateBase { public: enum Access { Random = 0, SortedAccess }; typedef ORecordList List; typedef OPimAccessBackend BackEnd; typedef OPimCache 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 */ virtual T find( int uid )const; /** * read ahead cache find method ;) */ virtual T find( int uid, const QArray&, uint current, typename OTemplateBase::CacheDirection dir = OTemplateBase::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 true if added successfully. */ virtual bool add( const T& t ) ; bool add( const OPimRecord& ); /* only the uid matters */ /** * remove T from the backend * @param t The item to remove * @return true if successful. */ virtual bool remove( const T& t ); /** * remove the OPimRecord with uid * @param uid The ID of the item to remove * @return true if successful. */ bool remove( int uid ); bool remove( const OPimRecord& ); /** * replace T from backend * @param t The item to replace * @return true if successful. */ virtual bool replace( const T& t) ; void setReadAhead( uint count ); /** * @internal */ void cache( const T& )const; void setSaneCacheSize( int ); QArray 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 OPimAccessTemplate::OPimAccessTemplate( BackEnd* end ) : OTemplateBase(), m_backEnd( end ) { if (end ) end->setFrontend( this ); } template OPimAccessTemplate::~OPimAccessTemplate() { qWarning("~OPimAccessTemplate"); delete m_backEnd; } template bool OPimAccessTemplate::load() { invalidateCache(); return m_backEnd->load(); } template bool OPimAccessTemplate::reload() { invalidateCache(); // zecke: I think this should be added (se) return m_backEnd->reload(); } template bool OPimAccessTemplate::save() { return m_backEnd->save(); } template typename OPimAccessTemplate::List OPimAccessTemplate::allRecords()const { QArray ints = m_backEnd->allRecords(); List lis(ints, this ); return lis; } template typename OPimAccessTemplate::List OPimAccessTemplate::matchRegexp( const QRegExp &r )const { QArray ints = m_backEnd->matchRegexp( r ); List lis(ints, this ); return lis; } template QArray OPimAccessTemplate::records()const { return m_backEnd->allRecords(); } template typename OPimAccessTemplate::List OPimAccessTemplate::queryByExample( const T& t, int settings, const QDateTime& d ) { QArray ints = m_backEnd->queryByExample( t, settings, d ); List lis(ints, this ); return lis; } template T OPimAccessTemplate::find( int uid ) const{ T t = m_backEnd->find( uid ); cache( t ); return t; } template T OPimAccessTemplate::find( int uid, const QArray& ar, uint current, typename OTemplateBase::CacheDirection dir )const { /* * better do T.isEmpty() * after a find this way we would * avoid two finds in QCache... */ // qWarning("find it now %d", uid ); if (m_cache.contains( uid ) ) { return m_cache.find( uid ); } T t = m_backEnd->find( uid, ar, current, dir ); cache( t ); return t; } template void OPimAccessTemplate::clear() { invalidateCache(); m_backEnd->clear(); } template bool OPimAccessTemplate::add( const T& t ) { cache( t ); return m_backEnd->add( t ); } template bool OPimAccessTemplate::add( const OPimRecord& rec) { /* same type */ if ( rec.rtti() == T::rtti() ) { const T &t = static_cast(rec); return add(t); } return false; } template bool OPimAccessTemplate::remove( const T& t ) { return remove( t.uid() ); } template bool OPimAccessTemplate::remove( int uid ) { m_cache.remove( uid ); return m_backEnd->remove( uid ); } template bool OPimAccessTemplate::remove( const OPimRecord& rec) { return remove( rec.uid() ); } template bool OPimAccessTemplate::replace( const T& t ) { m_cache.replace( t ); return m_backEnd->replace( t ); } template void OPimAccessTemplate::invalidateCache() { m_cache.invalidate(); } template typename OPimAccessTemplate::BackEnd* OPimAccessTemplate::backEnd() { return m_backEnd; } template bool OPimAccessTemplate::wasChangedExternally()const { return false; } template void OPimAccessTemplate::setBackEnd( BackEnd* end ) { m_backEnd = end; if (m_backEnd ) m_backEnd->setFrontend( this ); } template void OPimAccessTemplate::cache( const T& t ) const{ /* hacky we need to work around the const*/ ((OPimAccessTemplate*)this)->m_cache.add( t ); } template void OPimAccessTemplate::setSaneCacheSize( int size ) { m_cache.setSize( size ); } template void OPimAccessTemplate::setReadAhead( uint count ) { m_backEnd->setReadAhead( count ); } #endif