From 44c98b497ff1cb8cddba10b56435f136a583cce2 Mon Sep 17 00:00:00 2001 From: eilers Date: Fri, 27 Sep 2002 17:11:44 +0000 Subject: 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 ! --- (limited to 'libopie2/opiepim/core') diff --git a/libopie2/opiepim/core/ocontactaccess.cpp b/libopie2/opiepim/core/ocontactaccess.cpp new file mode 100644 index 0000000..2d808f7 --- a/dev/null +++ b/libopie2/opiepim/core/ocontactaccess.cpp @@ -0,0 +1,174 @@ +/* + * 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.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 +#include +#include +#include +#include +#include + +//#include +#include + +#include +#include +#include +#include + +#include "ocontactaccessbackend_xml.h" + + +OContactAccess::OContactAccess ( const QString appname, const QString filename, + OContactAccessBackend* end, bool autosync ): + OPimAccessTemplate( end ), + m_changed ( false ) +{ + /* 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 ) { + end = new OContactAccessBackend_XML( appname, filename ); + } + m_backEnd = 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(); + delete m_backEnd; +} + +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() ) + reload(); + + if ( m_changed ){ + bool status = m_backEnd->save(); + if ( !status ) return false; + + m_changed = false; + /* 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/libopie2/opiepim/core/ocontactaccess.h b/libopie2/opiepim/core/ocontactaccess.h new file mode 100644 index 0000000..54f7f07 --- a/dev/null +++ b/libopie2/opiepim/core/ocontactaccess.h @@ -0,0 +1,197 @@ +/* + * 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.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 + +#include + +#include +#include + +#include "ocontact.h" +#include "ocontactaccessbackend.h" +#include "opimaccesstemplate.h" + +/** Class to access the contacts database. + * This is just a frontend for the real database handling which is + * done by the backend. + */ +class OContactAccess: public QObject, public OPimAccessTemplate +{ + Q_OBJECT + + + /* class Iterator{ + friend OContactAccess; + public: + Iterator(); + Iterator ( const Iterator& copy ); + + bool operator== ( const Iterator& it ); + bool operator!= ( const Iterator& it ); + Iterator& operator= ( const Iterator& it ); + Iterator& operator++ (); // prefix + Iterator operator++ ( int ); // postfix + Iterator& operator-- (); // prefix + Iterator operator-- ( int ); // postfix + Contact operator*() const; + Contact operator->() const; + + Iterator begin(); + Iterator end(); + + uint count() const; + + private: + QValueList m_uids; + int m_cur_position; + bool m_end_reached; + OContactAccess *m_db; + + }; + + */ + + public: + /** Create Database with contacts (addressbook). + * @param appname Name of application which wants access to the database + * (i.e. "todolist") + * @param filename The name of the database file. If not set, the default one + * is used. + * @param backend Pointer to an alternative Backend. If not set, we will use + * the default backend. + * @param handlesync If true the database stores the current state + * automatically if it receives the signals flush() and reload() + * which are used before and after synchronisation. If the application wants + * to react itself, it should be disabled by setting it to false + * @see OContactBackend + */ + OContactAccess (const QString appname, const QString filename = 0l, + OContactAccessBackend* backend = 0l, bool handlesync = true); + ~OContactAccess (); + + /** Constants for query. + * Use this constants to set the query parameters. + * Note: query_IgnoreCase just make sense with one of the other attributes ! + * @see queryByExample() + * - why not enum - zecke? + * -> Had some implementation problems .. Will use enum soon ! .. (se) + */ + static const int query_WildCards = 0x0001; + static const int query_IgnoreCase = 0x0002; + static const int query_RegExp = 0x0004; + static const int query_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 true if the given settings are correct and possible. + */ + bool hasQuerySettings ( int querySettings ) const; + + /** Add Contact to database. + * @param newcontact The contact to add. + * @return true if added successfully. + */ + bool add (const OContact& newcontact); + + /** Replace contact. + * Replaces given contact with contact with the user id uid. + * @param uid The user ID + * @param contact The new contact + * @return true if successful. + */ + bool replace ( const OContact& contact ); + + /** Remove contact. + * Removes contact with the user id uid. + * @param The contact to remove + * @return true if successful. + */ + bool remove ( const OContact& t ); + + /** Remove contact. + * Removes contact with the user id uid. + * @param The user id of the contact to remove + * @return true 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 -- cgit v0.9.0.2