summaryrefslogtreecommitdiff
path: root/libopie
Side-by-side diff
Diffstat (limited to 'libopie') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/pim/ocontactaccess.cpp57
-rw-r--r--libopie/pim/ocontactaccess.h43
-rw-r--r--libopie/pim/ocontactaccessbackend_xml.h21
-rw-r--r--libopie/pim/opimaccesstemplate.h27
4 files changed, 55 insertions, 93 deletions
diff --git a/libopie/pim/ocontactaccess.cpp b/libopie/pim/ocontactaccess.cpp
index e8c0a45..2ca0283 100644
--- a/libopie/pim/ocontactaccess.cpp
+++ b/libopie/pim/ocontactaccess.cpp
@@ -1,188 +1,153 @@
/*
* Class to manage the Contacts.
*
* Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
*
* =====================================================================
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* =====================================================================
* Info: This class could just work with a change in the header-file
* of the Contact class ! Therefore our libopie only compiles
* with our version of libqpe
* =====================================================================
* ToDo: XML-Backend: Automatic reload if something was changed...
*
*
* =====================================================================
* Version: $Id$
* =====================================================================
* History:
* $Log$
+ * Revision 1.5 2002/10/16 10:52:40 eilers
+ * Added some docu to the interface and now using the cache infrastucture by zecke.. :)
+ *
* Revision 1.4 2002/10/14 16:21:54 eilers
* Some minor interface updates
*
* Revision 1.3 2002/10/07 17:34:24 eilers
* added OBackendFactory for advanced backend access
*
* Revision 1.2 2002/10/02 16:18:11 eilers
* debugged and seems to work almost perfectly ..
*
* Revision 1.1 2002/09/27 17:11:44 eilers
* Added API for accessing the Contact-Database ! It is compiling, but
* please do not expect that anything is working !
* I will debug that stuff in the next time ..
* Please read README_COMPILE for compiling !
*
*
*/
#include "ocontactaccess.h"
#include "obackendfactory.h"
#include <qasciidict.h>
#include <qdatetime.h>
#include <qfile.h>
#include <qregexp.h>
#include <qlist.h>
#include <qcopchannel_qws.h>
//#include <qpe/qcopenvelope_qws.h>
#include <qpe/global.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include "ocontactaccessbackend_xml.h"
OContactAccess::OContactAccess ( const QString appname, const QString ,
OContactAccessBackend* end, bool autosync ):
- OPimAccessTemplate<OContact>( end ),
- m_changed ( false )
+ OPimAccessTemplate<OContact>( end )
{
/* take care of the backend. If there is no one defined, we
* will use the XML-Backend as default (until we have a cute SQL-Backend..).
*/
if( end == 0 ) {
qWarning ("Using BackendFactory !");
end = OBackendFactory<OContactAccessBackend>::Default( "contact", appname );
}
// Set backend locally and in template
m_backEnd = end;
OPimAccessTemplate<OContact>::setBackEnd (end);
/* Connect signal of external db change to function */
QCopChannel *dbchannel = new QCopChannel( "QPE/PIM", this );
connect( dbchannel, SIGNAL(received(const QCString &, const QByteArray &)),
this, SLOT(copMessage( const QCString &, const QByteArray &)) );
if ( autosync ){
QCopChannel *syncchannel = new QCopChannel( "QPE/Sync", this );
connect( syncchannel, SIGNAL(received(const QCString &, const QByteArray &)),
this, SLOT(copMessage( const QCString &, const QByteArray &)) );
}
}
OContactAccess::~OContactAccess ()
{
/* The user may forget to save the changed database, therefore try to
* do it for him..
*/
- if ( m_changed )
- save();
+ save();
// delete m_backEnd; is done by template..
}
-bool OContactAccess::load()
-{
- return ( m_backEnd->load() );
-}
bool OContactAccess::save ()
{
/* If the database was changed externally, we could not save the
* Data. This will remove added items which is unacceptable !
* Therefore: Reload database and merge the data...
*/
- if ( m_backEnd->wasChangedExternally() )
+ if ( OPimAccessTemplate<OContact>::wasChangedExternally() )
reload();
- if ( m_changed ){
- bool status = m_backEnd->save();
- if ( !status ) return false;
+ bool status = OPimAccessTemplate<OContact>::save();
+ if ( !status ) return false;
- m_changed = false;
- /* Now tell everyone that new data is available.
- */
- QCopEnvelope e( "QPE/PIM", "addressbookUpdated()" );
-
- }
+ /* Now tell everyone that new data is available.
+ */
+ QCopEnvelope e( "QPE/PIM", "addressbookUpdated()" );
return true;
}
const uint OContactAccess::querySettings()
{
return ( m_backEnd->querySettings() );
}
bool OContactAccess::hasQuerySettings ( int querySettings ) const
{
return ( m_backEnd->hasQuerySettings ( querySettings ) );
}
-bool OContactAccess::add ( const OContact& newcontact )
-{
- m_changed = true;
- return ( m_backEnd->add ( newcontact ) );
-}
-
-bool OContactAccess::replace ( const OContact& contact )
-{
- m_changed = true;
- return ( m_backEnd->replace ( contact ) );
-}
-
-bool OContactAccess::remove ( const OContact& t )
-{
- m_changed = true;
- return ( m_backEnd->remove ( t.uid() ) );
-}
-
-bool OContactAccess::remove ( int uid )
-{
- m_changed = true;
- return ( m_backEnd->remove ( uid ) );
-}
bool OContactAccess::wasChangedExternally()const
{
return ( m_backEnd->wasChangedExternally() );
}
-bool OContactAccess::reload()
-{
- return ( m_backEnd->reload() );
-}
-
void OContactAccess::copMessage( const QCString &msg, const QByteArray & )
{
if ( msg == "addressbookUpdated()" ){
qWarning ("OContactAccess: Received addressbokUpdated()");
emit signalChanged ( this );
} else if ( msg == "flush()" ) {
qWarning ("OContactAccess: Received flush()");
save ();
} else if ( msg == "reload()" ) {
qWarning ("OContactAccess: Received reload()");
reload ();
emit signalChanged ( this );
}
}
diff --git a/libopie/pim/ocontactaccess.h b/libopie/pim/ocontactaccess.h
index adc66cf..da9c942 100644
--- a/libopie/pim/ocontactaccess.h
+++ b/libopie/pim/ocontactaccess.h
@@ -1,43 +1,46 @@
/*
* Class to manage the Contacts.
*
* Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
* Copyright (c) 2002 by Holger Freyther (zecke@handhelds.org)
*
* =====================================================================
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any later
* version.
* =====================================================================
* ToDo: Define enum for query settings
* =====================================================================
* Version: $Id$
* =====================================================================
* History:
* $Log$
+ * Revision 1.3 2002/10/16 10:52:40 eilers
+ * Added some docu to the interface and now using the cache infrastucture by zecke.. :)
+ *
* Revision 1.2 2002/10/14 16:21:54 eilers
* Some minor interface updates
*
* Revision 1.1 2002/09/27 17:11:44 eilers
* Added API for accessing the Contact-Database ! It is compiling, but
* please do not expect that anything is working !
* I will debug that stuff in the next time ..
* Please read README_COMPILE for compiling !
*
* =====================================================================
*/
#ifndef _OCONTACTACCESS_H
#define _OCONTACTACCESS_H
#include <qobject.h>
#include <qpe/qcopenvelope_qws.h>
#include <qvaluelist.h>
#include <qfileinfo.h>
#include "ocontact.h"
#include "ocontactaccessbackend.h"
#include "opimaccesstemplate.h"
@@ -70,99 +73,59 @@ class OContactAccess: public QObject, public OPimAccessTemplate<OContact>
/** Constants for query.
* Use this constants to set the query parameters.
* Note: <i>query_IgnoreCase</i> just make sense with one of the other attributes !
* @see queryByExample()
*/
enum QuerySettings {
WildCards = 0x0001,
IgnoreCase = 0x0002,
RegExp = 0x0004,
ExactMatch = 0x0008,
};
/** Return all possible settings.
* @return All settings provided by the current backend
* (i.e.: query_WildCards & query_IgnoreCase)
*/
const uint querySettings();
/** Check whether settings are correct.
* @return <i>true</i> if the given settings are correct and possible.
*/
bool hasQuerySettings ( int querySettings ) const;
- /** Add Contact to database.
- * @param newcontact The contact to add.
- * @return <i>true</i> if added successfully.
- */
- bool add (const OContact& newcontact);
-
- /** Replace contact.
- * Replaces given contact with contact with the user id <i>uid</i>.
- * @param uid The user ID
- * @param contact The new contact
- * @return <i>true</i> if successful.
- */
- bool replace ( const OContact& contact );
-
- /** Remove contact.
- * Removes contact with the user id <i>uid</i>.
- * @param The contact to remove
- * @return <i>true</i> if successful.
- */
- bool remove ( const OContact& t );
-
- /** Remove contact.
- * Removes contact with the user id <i>uid</i>.
- * @param The user id of the contact to remove
- * @return <i>true</i> if successful.
- */
- bool remove ( int uid );
-
- /** Load Database *
- */
- bool load();
-
/**
* if the resource was changed externally.
* You should use the signal instead of polling possible changes !
*/
bool wasChangedExternally()const;
- /** 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.
- */
- 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();
signals:
/* Signal is emitted if the database was changed. Therefore
* we may need to reload to stay consistent.
* @param which Pointer to the database who created this event. This pointer
* is useful if an application has to handle multiple databases at the same time.
* @see reload()
*/
void signalChanged ( const OContactAccess *which );
private:
// class OContactAccessPrivate;
// OContactAccessPrivate* d;
OContactAccessBackend *m_backEnd;
bool m_loading:1;
- bool m_changed;
private slots:
void copMessage( const QCString &msg, const QByteArray &data );
};
#endif
diff --git a/libopie/pim/ocontactaccessbackend_xml.h b/libopie/pim/ocontactaccessbackend_xml.h
index 50ea329..12a75ba 100644
--- a/libopie/pim/ocontactaccessbackend_xml.h
+++ b/libopie/pim/ocontactaccessbackend_xml.h
@@ -1,171 +1,182 @@
/*
* XML Backend for the OPIE-Contact Database.
*
* Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
*
* =====================================================================
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* =====================================================================
* ToDo: XML-Backend: Automatic reload if something was changed...
*
*
* =====================================================================
* Version: $Id$
* =====================================================================
* History:
* $Log$
+ * Revision 1.4 2002/10/16 10:52:40 eilers
+ * Added some docu to the interface and now using the cache infrastucture by zecke.. :)
+ *
* Revision 1.3 2002/10/14 16:21:54 eilers
* Some minor interface updates
*
* Revision 1.2 2002/10/07 17:34:24 eilers
* added OBackendFactory for advanced backend access
*
* Revision 1.1 2002/09/27 17:11:44 eilers
* Added API for accessing the Contact-Database ! It is compiling, but
* please do not expect that anything is working !
* I will debug that stuff in the next time ..
* Please read README_COMPILE for compiling !
*
*
*/
#ifndef _OContactAccessBackend_XML_
#define _OContactAccessBackend_XML_
#include <qasciidict.h>
#include <qdatetime.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qregexp.h>
#include <qarray.h>
#include <qpe/global.h>
#include <opie/xmltree.h>
#include "ocontactaccessbackend.h"
#include "ocontactaccess.h"
#include <stdlib.h>
#include <errno.h>
using namespace Opie;
/* the default xml implementation */
class OContactAccessBackend_XML : public OContactAccessBackend {
public:
- OContactAccessBackend_XML ( QString appname, QString filename = 0l )
+ OContactAccessBackend_XML ( QString appname, QString filename = 0l ):
+ m_changed( false )
{
m_appName = appname;
/* Set journalfile name ... */
m_journalName = getenv("HOME");
m_journalName +="/.abjournal" + appname;
/* Expecting to access the default filename if nothing else is set */
if ( filename.isEmpty() ){
m_fileName = Global::applicationFileName( "addressbook","addressbook.xml" );
} else
m_fileName = filename;
/* Load Database now */
load ();
}
bool save() {
+
+ if ( !m_changed )
+ return true;
+
QString strNewFile = m_fileName + ".new";
QFile f( strNewFile );
if ( !f.open( IO_WriteOnly|IO_Raw ) )
return false;
int total_written;
QString out;
out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n"
" <Groups>\n"
" </Groups>\n"
" <Contacts>\n";
//QValueList<Contact>::iterator it;
QValueListConstIterator<OContact> it;
for ( it = m_contactList.begin(); it != m_contactList.end(); ++it ) {
out += "<Contact ";
(*it).save( out );
out += "/>\n";
QCString cstr = out.utf8();
total_written = f.writeBlock( cstr.data(), cstr.length() );
if ( total_written != int(cstr.length()) ) {
f.close();
QFile::remove( strNewFile );
return false;
}
out = "";
}
out += " </Contacts>\n</AddressBook>\n";
QCString cstr = out.utf8();
total_written = f.writeBlock( cstr.data(), cstr.length() );
if ( total_written != int( cstr.length() ) ) {
f.close();
QFile::remove( strNewFile );
return false;
}
f.close();
// move the file over, I'm just going to use the system call
// because, I don't feel like using QDir.
if ( ::rename( strNewFile.latin1(), m_fileName.latin1() ) < 0 ) {
qWarning( "problem renaming file %s to %s, errno: %d",
strNewFile.latin1(), m_journalName.latin1(), errno );
// remove the tmp file...
QFile::remove( strNewFile );
}
/* The journalfile should be removed now... */
removeJournal();
+
+ m_changed = false;
return true;
}
bool load () {
m_contactList.clear();
/* Load XML-File and journal if it exists */
if ( !load ( m_fileName, false ) )
return false;
/* The returncode of the journalfile is ignored due to the
* fact that it does not exist when this class is instantiated !
* But there may such a file exist, if the application crashed.
* Therefore we try to load it to get the changes before the #
* crash happened...
*/
load (m_journalName, true);
return true;
}
void clear () {
m_contactList.clear();
+ m_changed = false;
}
bool wasChangedExternally()
{
QFileInfo fi( m_fileName );
QDateTime lastmod = fi.lastModified ();
return (lastmod != m_readtime);
}
QArray<int> allRecords() const {
QArray<int> uid_list( m_contactList.count() );
uint counter = 0;
QValueListConstIterator<OContact> it;
for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){
uid_list[counter++] = (*it).uid();
}
return ( uid_list );
}
@@ -254,73 +265,80 @@ class OContactAccessBackend_XML : public OContactAccessBackend {
bool hasQuerySettings (uint querySettings) const
{
/* OContactAccess::IgnoreCase may be added with one
* of the other settings, but never used alone.
* The other settings are just valid alone...
*/
switch ( querySettings & ~OContactAccess::IgnoreCase ){
case OContactAccess::RegExp:
return ( true );
case OContactAccess::WildCards:
return ( true );
case OContactAccess::ExactMatch:
return ( true );
default:
return ( false );
}
}
bool add ( const OContact &newcontact )
{
//qWarning("odefaultbackend: ACTION::ADD");
updateJournal (newcontact, OContact::ACTION_ADD);
addContact_p( newcontact );
+
+ m_changed = true;
+
return true;
}
bool replace ( const OContact &contact )
{
+ m_changed = true;
+
bool found = false;
QValueListIterator<OContact> it;
for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){
if ( (*it).uid() == contact.uid() ){
found = true;
break;
}
}
if (found) {
updateJournal (contact, OContact::ACTION_REPLACE);
m_contactList.remove (it);
m_contactList.append (contact);
return true;
} else
return false;
}
bool remove ( int uid )
{
+ m_changed = true;
+
bool found = false;
QValueListIterator<OContact> it;
for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){
if ((*it).uid() == uid){
found = true;
break;
}
}
if (found) {
updateJournal ( *it, OContact::ACTION_REMOVE);
m_contactList.remove (it);
return true;
} else
return false;
}
bool reload(){
/* Reload is the same as load in this implementation */
return ( load() );
}
private:
void addContact_p( const OContact &newcontact ){
m_contactList.append (newcontact);
@@ -532,32 +550,33 @@ class OContactAccessBackend_XML : public OContactAccessBackend {
// get a XML-File which is readable by our parser.
// This is just a cheat, but better than rewrite the parser.
if ( created ){
buf = "<Contacts>";
QCString cstr = buf.utf8();
f.writeBlock( cstr.data(), cstr.length() );
}
buf = "<Contact ";
cnt.save( buf );
buf += " action=\"" + QString::number( (int)action ) + "\" ";
buf += "/>\n";
QCString cstr = buf.utf8();
f.writeBlock( cstr.data(), cstr.length() );
}
void removeJournal()
{
QFile f ( m_journalName );
if ( f.exists() )
f.remove();
}
protected:
+ bool m_changed;
QString m_journalName;
QString m_fileName;
QString m_appName;
QValueList<OContact> m_contactList;
QDateTime m_readtime;
};
#endif
diff --git a/libopie/pim/opimaccesstemplate.h b/libopie/pim/opimaccesstemplate.h
index 3e1f393..50cb1e4 100644
--- a/libopie/pim/opimaccesstemplate.h
+++ b/libopie/pim/opimaccesstemplate.h
@@ -16,154 +16,169 @@
* interface or you want to implement
* your own Access lib
* Just create a OPimRecord and inherit from
* the plugins
*/
template <class T = OPimRecord >
class OPimAccessTemplate : public OTemplateBase<T> {
public:
typedef ORecordList<T> List;
typedef OPimAccessBackend<T> BackEnd;
typedef OPimCache<T> Cache;
/**
* c'tor BackEnd
*/
OPimAccessTemplate( BackEnd* end);
virtual ~OPimAccessTemplate();
/**
* load from the backend
*/
virtual bool load();
- /**
- * reload from the backend
+ /** 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 to the backend
+ /** Save contacts database.
+ * Save is more a "commit". After calling this function, all changes are public available.
+ * @return true if successful
*/
virtual 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;
/**
- * queryByExample)
+ * queryByExample.
* @see otodoaccess, ocontactaccess
*/
virtual List queryByExample( const T& t, int querySettings );
/**
* find the OPimRecord uid
*/
virtual T find( int uid )const;
/**
* read ahead cache find method ;)
*/
virtual T find( int uid, const QArray<int>&,
uint current, CacheDirection dir = Forward )const;
/* invalidate cache here */
/**
* clears the backend and invalidates the backend
*/
virtual void clear() ;
/**
* add T to the backend
+ * @param t The item to add.
+ * @return <i>true</i> if added successfully.
*/
virtual bool add( const T& t ) ;
/* only the uid matters */
/**
* remove T from the backend
+ * @param t The item to remove
+ * @return <i>true</i> if successful.
*/
virtual bool remove( const T& t );
/**
* remove the OPimRecord with uid
+ * @param uid The ID of the item to remove
+ * @return <i>true</i> if successful.
*/
virtual bool remove( int uid );
/**
* replace T from backend
+ * @param t The item to replace
+ * @return <i>true</i> if successful.
*/
virtual bool replace( const T& t) ;
/**
* @internal
*/
void cache( const T& )const;
void setSaneCacheSize( int );
protected:
/**
* invalidate the cache
*/
void invalidateCache();
void setBackEnd( BackEnd* end );
/**
* returns the backend
*/
BackEnd* backEnd();
BackEnd* m_backEnd;
Cache m_cache;
};
template <class T>
OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end )
: OTemplateBase<T>(), m_backEnd( end )
{
if (end )
end->setFrontend( this );
}
template <class T>
OPimAccessTemplate<T>::~OPimAccessTemplate() {
qWarning("~OPimAccessTemplate<T>");
delete m_backEnd;
}
template <class T>
bool OPimAccessTemplate<T>::load() {
invalidateCache();
return m_backEnd->load();
}
template <class T>
bool OPimAccessTemplate<T>::reload() {
- return m_backEnd->reload();
+ invalidateCache(); // zecke: I think this should be added (se)
+ return m_backEnd->reload();
}
template <class T>
bool OPimAccessTemplate<T>::save() {
return m_backEnd->save();
}
template <class T>
OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const {
QArray<int> ints = m_backEnd->allRecords();
List lis(ints, this );
return lis;
}
template <class T>
OPimAccessTemplate<T>::List
OPimAccessTemplate<T>::queryByExample( const T& t, int sortOrder ) {
QArray<int> ints = m_backEnd->queryByExample( t, sortOrder );
List lis(ints, this );
return lis;
}
template <class T>
T OPimAccessTemplate<T>::find( int uid ) const{
T t = m_backEnd->find( uid );
cache( t );
return t;