summaryrefslogtreecommitdiffstats
path: root/kabc/plugins/evolution/dbwrapper.cpp
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commitce4a32fe52ef09d8f5ff1dd22c001110902b60a2 (patch)
tree5ac38a06f3dde268dc7927dc155896926aaf7012 /kabc/plugins/evolution/dbwrapper.cpp
downloadtdelibs-ce4a32fe52ef09d8f5ff1dd22c001110902b60a2.tar.gz
tdelibs-ce4a32fe52ef09d8f5ff1dd22c001110902b60a2.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdelibs@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kabc/plugins/evolution/dbwrapper.cpp')
-rw-r--r--kabc/plugins/evolution/dbwrapper.cpp187
1 files changed, 187 insertions, 0 deletions
diff --git a/kabc/plugins/evolution/dbwrapper.cpp b/kabc/plugins/evolution/dbwrapper.cpp
new file mode 100644
index 000000000..cc566344c
--- /dev/null
+++ b/kabc/plugins/evolution/dbwrapper.cpp
@@ -0,0 +1,187 @@
+#include <db.h>
+
+#include <qfile.h>
+
+#include "dbwrapper.h"
+
+
+using namespace Evolution;
+
+struct DBIterator::Data {
+ DBWrapper *wrapper;
+ QString key;
+ QString data;
+ DBC* cursor;
+ bool atEnd;
+};
+
+DBIterator::DBIterator( DBWrapper* wra) {
+ data = new Data;
+ data->wrapper = wra;
+ data->atEnd = false;
+ data->cursor = 0l;
+}
+DBIterator::DBIterator( const DBIterator& copy ) {
+ data = new Data;
+ data->wrapper = copy.data->wrapper;
+ data->key = copy.data->key;
+ data->data = copy.data->data;
+ data->atEnd = copy.data->atEnd;
+ if (copy.data->cursor )
+ copy.data->cursor->c_dup(copy.data->cursor, &data->cursor, 0 );
+ else
+ data->cursor = 0l;
+}
+DBIterator::~DBIterator() {
+ if (data->cursor)
+ data->cursor->c_close(data->cursor);
+ delete data;
+}
+DBIterator& DBIterator::operator=( const DBIterator& rhs ) {
+ if ( *this == rhs )
+ return *this;
+ if (data->cursor)
+ data->cursor->c_close(data->cursor);
+ delete data;
+ data = new Data;
+ data->wrapper = rhs.data->wrapper;
+ data->key = rhs.data->key;
+ data->data = rhs.data->data;
+ data->atEnd = rhs.data->atEnd;
+ if ( rhs.data->cursor )
+ rhs.data->cursor->c_dup(rhs.data->cursor, &data->cursor, 0 );
+ else
+ data->cursor = 0l;
+
+ return *this;
+}
+QString DBIterator::key()const{
+ return data->key;
+}
+QString DBIterator::value()const {
+ return data->data;
+}
+QString DBIterator::operator*() {
+ return data->data;
+}
+DBIterator& DBIterator::operator++() {
+ DBT key, val;
+ ::memset(&key, 0, sizeof(key) );
+ ::memset(&val, 0, sizeof(val) );
+ if ( data->cursor )
+ if ( data->cursor->c_get(data->cursor, &key, &val,DB_NEXT ) != 0 )
+ data->atEnd = true;
+ data->key = QString::fromUtf8( (char*)key.data, key.size );
+ data->data = QString::fromUtf8( (char*)val.data, val.size );
+ return *this;
+}
+DBIterator& DBIterator::operator--() {
+ DBT key, val;
+ ::memset(&key, 0, sizeof(key) );
+ ::memset(&val, 0, sizeof(val) );
+ if ( data->cursor )
+ if ( data->cursor->c_get(data->cursor, &key, &val,DB_PREV ) != 0 )
+ data->atEnd = true;
+ data->key = QString::fromUtf8( (char*)key.data, key.size );
+ data->data = QString::fromUtf8( (char*)val.data, val.size );
+ return *this;
+}
+bool DBIterator::operator==( const DBIterator& rhs ) {
+ if ( data->atEnd && data->atEnd == rhs.data->atEnd ) return true;
+
+ return false;
+}
+bool DBIterator::operator!=( const DBIterator& rhs ) {
+ return !this->operator==(rhs );
+}
+struct DBWrapper::Data {
+ DB* db;
+ bool only;
+};
+DBWrapper::DBWrapper() {
+ data = new Data;
+ (void)db_create(&data->db, NULL, 0 );
+ data->only = false;
+}
+DBWrapper::~DBWrapper() {
+ data->db->close(data->db, 0 );
+ delete data;
+}
+bool DBWrapper::open( const QString& file, bool on) {
+ data->only = on;
+ return !data->db->open(data->db, QFile::encodeName( file ), NULL, DB_HASH, 0, 0666 );
+}
+bool DBWrapper::save() {
+ return true;
+}
+DBIterator DBWrapper::begin() {
+ DBIterator it(this);
+ DBC* cursor;
+ DBT key, val;
+ int ret;
+ ret = data->db->cursor(data->db, NULL, &cursor, 0 );
+ if (ret ) {
+ it.data->atEnd = true;
+ return it;
+ }
+
+ ::memset(&key, 0, sizeof(key) );
+ ::memset(&val, 0, sizeof(val) );
+ ret = cursor->c_get(cursor, &key, &val, DB_FIRST );
+ if (ret ) {
+ it.data->atEnd = true;
+ return it;
+ }
+
+ it.data->cursor = cursor;
+ it.data->key = QString::fromUtf8((char*)key.data, key.size );
+ it.data->data = QString::fromUtf8((char*)val.data, val.size );
+
+ return it;
+}
+DBIterator DBWrapper::end() {
+ DBIterator it(this);
+ it.data->atEnd = true;
+
+ return it;
+}
+bool DBWrapper::find( const QString& _key, QString& _val ) {
+ DBT key, val;
+ ::memset(&key, 0, sizeof(key) );
+ ::memset(&val, 0, sizeof(val) );
+
+ QCString db_key = _key.local8Bit();
+ key.data = db_key.data();
+ key.size = db_key.size();
+
+ int ret = data->db->get(data->db, NULL, &key, &val, 0 );
+ if (!ret) {
+ _val = QString::fromUtf8( (char*)val.data, val.size );
+ qWarning("key: %s val: %sXXX", (char*)key.data, (char*)val.data );
+ return true;
+ }
+ return false;
+}
+bool DBWrapper::add( const QString& _key, const QString& _val ) {
+ QCString db_key = _key.local8Bit();
+ QCString db_val = _val.local8Bit();
+ DBT key, val;
+ ::memset(&key, 0, sizeof(key) );
+ ::memset(&val, 0, sizeof(val) );
+
+ key.data = db_key.data();
+ key.size = db_key.size();
+ val.data = db_val.data();
+ val.size = db_val.size();
+
+ return !data->db->put(data->db, NULL, &key, &val, 0 );
+}
+bool DBWrapper::remove( const QString& _key ) {
+ QCString db_key = _key.local8Bit();
+ DBT key;
+ memset(&key, 0, sizeof(key) );
+ key.data = db_key.data();
+ key.size = db_key.size();
+
+ return !data->db->del(data->db, NULL, &key, 0 );
+}