From ce4a32fe52ef09d8f5ff1dd22c001110902b60a2 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: 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 --- kabc/plugins/sql/Makefile.am | 20 ++ kabc/plugins/sql/resourcesql.cpp | 338 +++++++++++++++++++++++++++++++++ kabc/plugins/sql/resourcesql.h | 63 ++++++ kabc/plugins/sql/resourcesqlconfig.cpp | 95 +++++++++ kabc/plugins/sql/resourcesqlconfig.h | 51 +++++ kabc/plugins/sql/sql.desktop | 10 + 6 files changed, 577 insertions(+) create mode 100644 kabc/plugins/sql/Makefile.am create mode 100644 kabc/plugins/sql/resourcesql.cpp create mode 100644 kabc/plugins/sql/resourcesql.h create mode 100644 kabc/plugins/sql/resourcesqlconfig.cpp create mode 100644 kabc/plugins/sql/resourcesqlconfig.h create mode 100644 kabc/plugins/sql/sql.desktop (limited to 'kabc/plugins/sql') diff --git a/kabc/plugins/sql/Makefile.am b/kabc/plugins/sql/Makefile.am new file mode 100644 index 000000000..93c86f061 --- /dev/null +++ b/kabc/plugins/sql/Makefile.am @@ -0,0 +1,20 @@ +INCLUDES = -I$(top_srcdir)/kabc -I$(top_builddir)/kabc $(all_includes) + +# these are the headers for your project +noinst_HEADERS = resourcesql.h resourcesqlconfig.h + +kde_module_LTLIBRARIES = kabc_sql.la + +kabc_sql_la_SOURCES = resourcesql.cpp resourcesqlconfig.cpp + +kabc_sql_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN) +kabc_sql_la_LIBADD = ../../libkabc.la ../../../kdeui/libkdeui.la + +METASOURCES = AUTO + +messages: rc.cpp + $(XGETTEXT) *.cpp -o $(podir)/kabc_sql.pot + +linkdir = $(kde_datadir)/kresources/contact +link_DATA = sql.desktop +EXTRA_DIST = $(link_DATA) diff --git a/kabc/plugins/sql/resourcesql.cpp b/kabc/plugins/sql/resourcesql.cpp new file mode 100644 index 000000000..2e6eb6902 --- /dev/null +++ b/kabc/plugins/sql/resourcesql.cpp @@ -0,0 +1,338 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include + +#include +#include +#include +#include + +#include "resourcesql.h" +#include "resourcesqlconfig.h" + +using namespace KABC; + +extern "C" +{ + KDE_EXPORT void *init_kabc_sql() + { + return new KRES::PluginFactory(); + } +} + +ResourceSql::ResourceSql( AddressBook *ab, const KConfig *config ) + : Resource( ab ), mDb( 0 ) +{ + QString user, password, db, host; + + user = config->readEntry( "SqlUser" ); + password = cryptStr( config->readEntry( "SqlPassword " ) ); + db = config->readEntry( "SqlName" ); + host = config->readEntry( "SqlHost" ); + + init( user, password, db, host ); +} + +ResourceSql::ResourceSql( AddressBook *ab, const QString &user, + const QString &password, const QString &db, const QString &host ) + : Resource( ab ), mDb( 0 ) +{ + init( user, password, db, host ); +} + +void ResourceSql::init( const QString &user, const QString &password, + const QString &db, const QString &host ) +{ + mUser = user; + mPassword = password; + mDbName = db; + mHost = host; +} + +Ticket *ResourceSql::requestSaveTicket() +{ + if ( !addressBook() ) { + kdDebug(5700) << "no addressbook" << endl; + return 0; + } + + return createTicket( this ); +} + +bool ResourceSql::open() +{ + QStringList drivers = QSqlDatabase::drivers(); + for ( QStringList::Iterator it = drivers.begin(); it != drivers.end(); ++it ) { + kdDebug(5700) << "Driver: " << (*it) << endl; + } + + mDb = QSqlDatabase::addDatabase( "QMYSQL3" ); + + if ( !mDb ) { + kdDebug(5700) << "Error. Unable to connect to database." << endl; + return false; + } + + mDb->setDatabaseName( mDbName ); + mDb->setUserName( mUser ); + mDb->setPassword( mPassword ); + mDb->setHostName( mHost ); + + if ( !mDb->open() ) { + kdDebug(5700) << "Error. Unable to open database '" << mDbName << "'." << endl; + return false; + } + + return true; +} + +void ResourceSql::close() +{ + mDb->close(); +} + +bool ResourceSql::load() +{ + QSqlQuery query( "select addressId, name, familyName, givenName, " + "additionalName, prefix, suffix, nickname, birthday, " + "mailer, timezone, geo_latitude, geo_longitude, title, " + "role, organization, note, productId, revision, " + "sortString, url from kaddressbook_main_" + mUser ); + + while ( query.next() ) { + QString addrId = query.value(0).toString(); + + Addressee addr; + addr.setResource( this ); + addr.setUid( addrId ); + addr.setName( query.value(1).toString() ); + addr.setFamilyName( query.value(2).toString() ); + addr.setGivenName( query.value(3).toString() ); + addr.setAdditionalName( query.value(4).toString() ); + addr.setPrefix( query.value(5).toString() ); + addr.setSuffix( query.value(6).toString() ); + addr.setNickName( query.value(7).toString() ); + addr.setBirthday( query.value(8).toDateTime() ); + addr.setMailer( query.value(9).toString() ); + addr.setTimeZone( TimeZone( query.value(10).toInt() ) ); + addr.setGeo( Geo( query.value(11).toDouble(), query.value(12).toDouble() ) ); + addr.setTitle( query.value(13).toString() ); + addr.setRole( query.value(14).toString() ); + addr.setOrganization( query.value(15).toString() ); + addr.setNote( query.value(16).toString() ); + addr.setProductId( query.value(17).toString() ); + addr.setRevision( query.value(18).toDateTime() ); + addr.setSortString( query.value(19).toString() ); + addr.setUrl( query.value(20).toString() ); + + // emails + { + QSqlQuery emailsQuery( "select email, preferred from kaddressbook_emails " + "where addressId = '" + addrId + "'" ); + while ( emailsQuery.next() ) + addr.insertEmail( emailsQuery.value( 0 ).toString(), + emailsQuery.value( 1 ).toInt() ); + } + + // phones + { + QSqlQuery phonesQuery( "select number, type from kaddressbook_phones " + "where addressId = '" + addrId + "'" ); + while ( phonesQuery.next() ) + addr.insertPhoneNumber( PhoneNumber( phonesQuery.value( 0 ).toString(), + phonesQuery.value( 1 ).toInt() ) ); + } + + // addresses + { + QSqlQuery addressesQuery( "select postOfficeBox, extended, street, " + "locality, region, postalCode, country, label, type " + "from kaddressbook_addresses where addressId = '" + addrId + "'" ); + while ( addressesQuery.next() ) { + Address a; + a.setPostOfficeBox( addressesQuery.value(0).toString() ); + a.setExtended( addressesQuery.value(1).toString() ); + a.setStreet( addressesQuery.value(2).toString() ); + a.setLocality( addressesQuery.value(3).toString() ); + a.setRegion( addressesQuery.value(4).toString() ); + a.setPostalCode( addressesQuery.value(5).toString() ); + a.setCountry( addressesQuery.value(6).toString() ); + a.setLabel( addressesQuery.value(7).toString() ); + a.setType( addressesQuery.value(8).toInt() ); + + addr.insertAddress( a ); + } + } + + // categories + { + QSqlQuery categoriesQuery( "select category from kaddressbook_categories " + "where addressId = '" + addrId + "'" ); + while ( categoriesQuery.next() ) + addr.insertCategory( categoriesQuery.value( 0 ).toString() ); + } + + // customs + { + QSqlQuery customsQuery( "select app, name, value from kaddressbook_customs " + "where addressId = '" + addrId + "'" ); + while ( customsQuery.next() ) + addr.insertCustom( customsQuery.value( 0 ).toString(), + customsQuery.value( 1 ).toString(), + customsQuery.value( 2 ).toString()); + } + + addressBook()->insertAddressee( addr ); + } + + return true; +} + +bool ResourceSql::save( Ticket * ) +{ + // we have to delete all entries for this user and reinsert them + QSqlQuery query( "select addressId from kaddressbook_main_" + mUser ); + + while ( query.next() ) { + QString addrId = query.value( 0 ).toString(); + QSqlQuery q; + + q.exec( "DELETE FROM kaddressbook_emails WHERE addressId = '" + addrId + "'" ); + q.exec( "DELETE FROM kaddressbook_phones WHERE addressId = '" + addrId + "'" ); + q.exec( "DELETE FROM kaddressbook_addresses WHERE addressId = '" + addrId + "'" ); + q.exec( "DELETE FROM kaddressbook_categories WHERE addressId = '" + addrId + "'" ); + q.exec( "DELETE FROM kaddressbook_customs WHERE addressId = '" + addrId + "'" ); + + q.exec( "DELETE FROM kaddressbook_main_" + mUser + " WHERE addressId = '" + addrId + "'" ); + } + + // let's start... + AddressBook::Iterator it; + for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { + if ( (*it).resource() != this && (*it).resource() != 0 ) // save only my and new entries + continue; + + QString uid = (*it).uid(); + + query.exec( "INSERT INTO kaddressbook_main_" + mUser + " VALUES ('" + + (*it).uid() + "','" + + (*it).name() + "','" + + (*it).familyName() + "','" + + (*it).givenName() + "','" + + (*it).additionalName() + "','" + + (*it).prefix() + "','" + + (*it).suffix() + "','" + + (*it).nickName() + "','" + + (*it).birthday().toString( Qt::ISODate ) + "','" + + (*it).mailer() + "','" + + QString::number( (*it).timeZone().offset() ) + "','" + + QString::number( (*it).geo().latitude() ) + "','" + + QString::number( (*it).geo().longitude() ) + "','" + + (*it).title() + "','" + + (*it).role() + "','" + + (*it).organization() + "','" + + (*it).note() + "','" + + (*it).productId() + "','" + + (*it).revision().toString( Qt::ISODate ) + "','" + + (*it).sortString() + "','" + + (*it).url().url() + "')" + ); + + // emails + { + QStringList emails = (*it).emails(); + QStringList::ConstIterator it; + bool preferred = true; + for( it = emails.begin(); it != emails.end(); ++it ) { + query.exec("INSERT INTO kaddressbook_emails VALUES ('" + + uid + "','" + + (*it) + "','" + + QString::number(preferred) + "')"); + preferred = false; + } + } + + // phonenumbers + { + PhoneNumber::List phoneNumberList = (*it).phoneNumbers(); + PhoneNumber::List::ConstIterator it; + for( it = phoneNumberList.begin(); it != phoneNumberList.end(); ++it ) { + query.exec("INSERT INTO kaddressbook_phones VALUES ('" + + uid + "','" + + (*it).number() + "','" + + QString::number( (*it).type() ) + "')"); + } + } + + // postal addresses + { + Address::List addressList = (*it).addresses(); + Address::List::ConstIterator it; + for( it = addressList.begin(); it != addressList.end(); ++it ) { + query.exec("INSERT INTO kaddressbook_addresses VALUES ('" + + uid + "','" + + (*it).postOfficeBox() + "','" + + (*it).extended() + "','" + + (*it).street() + "','" + + (*it).locality() + "','" + + (*it).region() + "','" + + (*it).postalCode() + "','" + + (*it).country() + "','" + + (*it).label() + "','" + + QString::number( (*it).type() ) + "')"); + } + } + + // categories + { + QStringList categories = (*it).categories(); + QStringList::ConstIterator it; + for( it = categories.begin(); it != categories.end(); ++it ) + query.exec("INSERT INTO kaddressbook_categories VALUES ('" + + uid + "','" + + (*it) + "')"); + } + + // customs + { + QStringList list = (*it).customs(); + QStringList::ConstIterator it; + for( it = list.begin(); it != list.end(); ++it ) { + int dashPos = (*it).find( '-' ); + int colonPos = (*it).find( ':' ); + QString app = (*it).left( dashPos ); + QString name = (*it).mid( dashPos + 1, colonPos - dashPos - 1 ); + QString value = (*it).right( (*it).length() - colonPos - 1 ); + + query.exec("INSERT INTO kaddressbook_categories VALUES ('" + + uid + "','" + app + "','" + name + "','" + value + "')"); + } + } + } + + return true; +} + +QString ResourceSql::identifier() const +{ + return mHost + "_" + mDbName; +} diff --git a/kabc/plugins/sql/resourcesql.h b/kabc/plugins/sql/resourcesql.h new file mode 100644 index 000000000..c0b61b69b --- /dev/null +++ b/kabc/plugins/sql/resourcesql.h @@ -0,0 +1,63 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KABC_RESOURCESQL_H +#define KABC_RESOURCESQL_H + +#include + +#include "addressbook.h" +#include "resource.h" + +class QSqlDatabase; + +namespace KABC { + +class ResourceSql : public Resource +{ +public: + ResourceSql( AddressBook *ab, const QString &user, const QString &password, + const QString &db, const QString &host ); + ResourceSql( AddressBook *ab, const KConfig * ); + + bool open(); + void close(); + + Ticket *requestSaveTicket(); + + bool load(); + bool save( Ticket * ticket ); + + QString identifier() const; + +private: + void init(const QString &user, const QString &password, + const QString &db, const QString &host ); + + QString mUser; + QString mPassword; + QString mDbName; + QString mHost; + + QSqlDatabase *mDb; +}; + +} +#endif diff --git a/kabc/plugins/sql/resourcesqlconfig.cpp b/kabc/plugins/sql/resourcesqlconfig.cpp new file mode 100644 index 000000000..d9abffa30 --- /dev/null +++ b/kabc/plugins/sql/resourcesqlconfig.cpp @@ -0,0 +1,95 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include + +#include +#include + +#include "resource.h" +#include "resourcesqlconfig.h" + +using namespace KABC; + +ResourceSqlConfig::ResourceSqlConfig( QWidget* parent, const char* name ) + : ResourceConfigWidget( parent, name ) +{ + resize( 290, 170 ); + + QGridLayout *mainLayout = new QGridLayout( this, 4, 2 ); + + QLabel *label = new QLabel( i18n( "Username:" ), this ); + mUser = new KLineEdit( this ); + + mainLayout->addWidget( label, 0, 0 ); + mainLayout->addWidget( mUser, 0, 1 ); + + label = new QLabel( i18n( "Password:" ), this ); + mPassword = new KLineEdit( this ); + mPassword->setEchoMode( KLineEdit::Password ); + + mainLayout->addWidget( label, 1, 0 ); + mainLayout->addWidget( mPassword, 1, 1 ); + + label = new QLabel( i18n( "Host:" ), this ); + mHost = new KLineEdit( this ); + + mainLayout->addWidget( label, 2, 0 ); + mainLayout->addWidget( mHost, 2, 1 ); + + label = new QLabel( i18n( "Port:" ), this ); + QVBox *box = new QVBox(this); + mPort = new QSpinBox(0, 65535, 1, box ); + mPort->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred)); + mPort->setValue(389); + new QWidget(box, "dummy"); + + mainLayout->addWidget( label, 3, 0 ); + mainLayout->addWidget( box, 3, 1 ); + + label = new QLabel( i18n( "Database:" ), this ); + mDbName = new KLineEdit( this ); + + mainLayout->addWidget( label, 4, 0 ); + mainLayout->addWidget( mDbName, 4, 1 ); +} + +void ResourceSqlConfig::loadSettings( KConfig *config ) +{ + mUser->setText( config->readEntry( "SqlUser" ) ); + mPassword->setText( KABC::Resource::cryptStr( config->readEntry( "SqlPassword" ) ) ); + mDbName->setText( config->readEntry( "SqlName" ) ); + mHost->setText( config->readEntry( "SqlHost" ) ); + mPort->setValue( config->readNumEntry( "SqlPort" ) ); +} + +void ResourceSqlConfig::saveSettings( KConfig *config ) +{ + config->writeEntry( "SqlUser", mUser->text() ); + config->writeEntry( "SqlPassword", KABC::Resource::cryptStr( mPassword->text() ) ); + config->writeEntry( "SqlName", mDbName->text() ); + config->writeEntry( "SqlHost", mHost->text() ); + config->writeEntry( "SqlPort", mPort->value() ); +} + +#include "resourcesqlconfig.moc" diff --git a/kabc/plugins/sql/resourcesqlconfig.h b/kabc/plugins/sql/resourcesqlconfig.h new file mode 100644 index 000000000..d7230870e --- /dev/null +++ b/kabc/plugins/sql/resourcesqlconfig.h @@ -0,0 +1,51 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef RESOURCESQLCONFIG_H +#define RESOURCESQLCONFIG_H + +#include "resourceconfigwidget.h" + +class KLineEdit; +class QSpinBox; + +namespace KABC { + +class ResourceSqlConfig : public ResourceConfigWidget +{ + Q_OBJECT + +public: + ResourceSqlConfig( QWidget* parent = 0, const char* name = 0 ); + +public slots: + void loadSettings( KConfig *config ); + void saveSettings( KConfig *config ); + +private: + KLineEdit* mUser; + KLineEdit* mPassword; + KLineEdit* mDbName; + KLineEdit* mHost; + QSpinBox* mPort; +}; + +} +#endif diff --git a/kabc/plugins/sql/sql.desktop b/kabc/plugins/sql/sql.desktop new file mode 100644 index 000000000..69ca3e3a7 --- /dev/null +++ b/kabc/plugins/sql/sql.desktop @@ -0,0 +1,10 @@ +[Misc] +Name=SQL +Name[bn]=এস-কিউ-এল (SQL) +Name[hi]=एसक्यूएल (SQL) +Name[ss]=I-SQL +Name[te]=ఏస్క్యుఎల్ + +[Plugin] +Type=sql +X-KDE-Library=kabc_sql -- cgit v1.2.1