| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# resourcesql.cpp

    00001 /*
    00002     This file is part of libkabc.
    00003     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public License
    00016     along with this library; see the file COPYING.LIB.  If not, write to
    00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <qsqldatabase.h>
    00022 #include <qsqlcursor.h>
    00023
    00024 #include <kdebug.h>
    00025 #include <kglobal.h>
    00026 #include <klineedit.h>
    00027 #include <klocale.h>
    00028
    00029 #include "resourcesql.h"
    00030 #include "resourcesqlconfig.h"
    00031
    00032 using namespace KABC;
    00033
    00034 extern "C"
    00035 {
    00036   KDE_EXPORT void *init_kabc_sql()
    00037   {
    00038     return new KRES::PluginFactory<ResourceSql,ResourceSqlConfig>();
    00039   }
    00040 }
    00041
    00042 ResourceSql::ResourceSql( AddressBook *ab, const KConfig *config )
    00043   : Resource( ab ), mDb( 0 )
    00044 {
    00045   QString user, password, db, host;
    00046
    00047   user = config->readEntry( "SqlUser" );
    00048   password = cryptStr( config->readEntry( "SqlPassword " ) );
    00049   db = config->readEntry( "SqlName" );
    00050   host = config->readEntry( "SqlHost" );
    00051
    00052   init( user, password, db, host );
    00053 }
    00054
    00055 ResourceSql::ResourceSql( AddressBook *ab, const QString &user,
    00056     const QString &password, const QString &db, const QString &host )
    00057   : Resource( ab ), mDb( 0 )
    00058 {
    00059   init( user, password, db, host );
    00060 }
    00061
    00062 void ResourceSql::init( const QString &user, const QString &password,
    00063     const QString &db, const QString &host )
    00064 {
    00065   mUser = user;
    00066   mPassword = password;
    00067   mDbName = db;
    00068   mHost = host;
    00069 }
    00070
    00071 Ticket *ResourceSql::requestSaveTicket()
    00072 {
    00073   if ( !addressBook() ) {
    00074     kdDebug(5700) << "no addressbook" << endl;
    00075     return 0;
    00076   }
    00077
    00078   return createTicket( this );
    00079 }
    00080
    00081 bool ResourceSql::open()
    00082 {
    00083   QStringList drivers = QSqlDatabase::drivers();
    00084   for ( QStringList::Iterator it = drivers.begin(); it != drivers.end(); ++it ) {
    00085     kdDebug(5700) << "Driver: " << (*it) << endl;
    00086   }
    00087
    00088   mDb = QSqlDatabase::addDatabase( "QMYSQL3" );
    00089
    00090   if ( !mDb ) {
    00091     kdDebug(5700) << "Error. Unable to connect to database." << endl;
    00092     return false;
    00093   }
    00094
    00095   mDb->setDatabaseName( mDbName );
    00096   mDb->setUserName( mUser );
    00097   mDb->setPassword( mPassword );
    00098   mDb->setHostName( mHost );
    00099
    00100   if ( !mDb->open() ) {
    00101     kdDebug(5700) << "Error. Unable to open database '" << mDbName << "'." << endl;
    00102     return false;
    00103   }
    00104
    00105   return true;
    00106 }
    00107
    00108 void ResourceSql::close()
    00109 {
    00110   mDb->close();
    00111 }
    00112
    00113 bool ResourceSql::load()
    00114 {
    00115   QSqlQuery query( "select addressId, name, familyName, givenName, "
    00116       "additionalName, prefix, suffix, nickname, birthday, "
    00117       "mailer, timezone, geo_latitude, geo_longitude, title, "
    00118       "role, organization, note, productId, revision, "
    00119       "sortString, url from kaddressbook_main_" + mUser );
    00120
    00121   while ( query.next() ) {
    00122     QString addrId = query.value(0).toString();
    00123
    00124     Addressee addr;
    00125     addr.setResource( this );
    00126     addr.setUid( addrId );
    00127     addr.setName( query.value(1).toString() );
    00128     addr.setFamilyName( query.value(2).toString() );
    00129     addr.setGivenName( query.value(3).toString() );
    00130     addr.setAdditionalName( query.value(4).toString() );
    00131     addr.setPrefix( query.value(5).toString() );
    00132     addr.setSuffix( query.value(6).toString() );
    00133     addr.setNickName( query.value(7).toString() );
    00134     addr.setBirthday( query.value(8).toDateTime() );
    00135     addr.setMailer( query.value(9).toString() );
    00136     addr.setTimeZone( TimeZone( query.value(10).toInt() ) );
    00137     addr.setGeo( Geo( query.value(11).toDouble(), query.value(12).toDouble() ) );
    00138     addr.setTitle( query.value(13).toString() );
    00139     addr.setRole( query.value(14).toString() );
    00140     addr.setOrganization( query.value(15).toString() );
    00141     addr.setNote( query.value(16).toString() );
    00142     addr.setProductId( query.value(17).toString() );
    00143     addr.setRevision( query.value(18).toDateTime() );
    00144     addr.setSortString( query.value(19).toString() );
    00145     addr.setUrl( query.value(20).toString() );
    00146
    00147     // emails
    00148     {
    00149       QSqlQuery emailsQuery( "select email, preferred from kaddressbook_emails "
    00150           "where addressId = '" + addrId + "'" );
    00151       while ( emailsQuery.next() )
    00152         addr.insertEmail( emailsQuery.value( 0 ).toString(),
    00153       emailsQuery.value( 1 ).toInt() );
    00154     }
    00155
    00156     // phones
    00157     {
    00158       QSqlQuery phonesQuery( "select number, type from kaddressbook_phones "
    00159           "where addressId = '" + addrId + "'" );
    00160       while ( phonesQuery.next() )
    00161         addr.insertPhoneNumber( PhoneNumber( phonesQuery.value( 0 ).toString(),
    00162       phonesQuery.value( 1 ).toInt() ) );
    00163     }
    00164
    00165     // addresses
    00166     {
    00167       QSqlQuery addressesQuery( "select postOfficeBox, extended, street, "
    00168           "locality, region, postalCode, country, label, type "
    00169           "from kaddressbook_addresses where addressId = '" + addrId + "'" );
    00170       while ( addressesQuery.next() ) {
    00171         Address a;
    00172         a.setPostOfficeBox( addressesQuery.value(0).toString() );
    00173         a.setExtended( addressesQuery.value(1).toString() );
    00174         a.setStreet( addressesQuery.value(2).toString() );
    00175         a.setLocality( addressesQuery.value(3).toString() );
    00176         a.setRegion( addressesQuery.value(4).toString() );
    00177         a.setPostalCode( addressesQuery.value(5).toString() );
    00178         a.setCountry( addressesQuery.value(6).toString() );
    00179         a.setLabel( addressesQuery.value(7).toString() );
    00180         a.setType( addressesQuery.value(8).toInt() );
    00181
    00182         addr.insertAddress( a );
    00183       }
    00184     }
    00185
    00186     // categories
    00187     {
    00188       QSqlQuery categoriesQuery( "select category from kaddressbook_categories "
    00189           "where addressId = '" + addrId + "'" );
    00190       while ( categoriesQuery.next() )
    00191         addr.insertCategory( categoriesQuery.value( 0 ).toString() );
    00192     }
    00193
    00194     // customs
    00195     {
    00196       QSqlQuery customsQuery( "select app, name, value from kaddressbook_customs "
    00197           "where addressId = '" + addrId + "'" );
    00198       while ( customsQuery.next() )
    00199         addr.insertCustom( customsQuery.value( 0 ).toString(),
    00200       customsQuery.value( 1 ).toString(),
    00201       customsQuery.value( 2 ).toString());
    00202     }
    00203
    00204     addressBook()->insertAddressee( addr );
    00205   }
    00206
    00207   return true;
    00208 }
    00209
    00210 bool ResourceSql::save( Ticket * )
    00211 {
    00212   // we have to delete all entries for this user and reinsert them
    00213   QSqlQuery query( "select addressId from kaddressbook_main_" + mUser );
    00214
    00215   while ( query.next() ) {
    00216     QString addrId = query.value( 0 ).toString();
    00217     QSqlQuery q;
    00218
    00219     q.exec( "DELETE FROM kaddressbook_emails WHERE addressId = '" + addrId + "'" );
    00220     q.exec( "DELETE FROM kaddressbook_phones WHERE addressId = '" + addrId + "'" );
    00221     q.exec( "DELETE FROM kaddressbook_addresses WHERE addressId = '" + addrId + "'" );
    00222     q.exec( "DELETE FROM kaddressbook_categories WHERE addressId = '" + addrId + "'" );
    00223     q.exec( "DELETE FROM kaddressbook_customs WHERE addressId = '" + addrId + "'" );
    00224
    00225     q.exec( "DELETE FROM kaddressbook_main_" + mUser + " WHERE addressId = '" + addrId + "'" );
    00226   }
    00227
    00228   // let's start...
    00229   AddressBook::Iterator it;
    00230   for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
    00231     if ( (*it).resource() != this && (*it).resource() != 0 ) // save only my and new entries
    00232       continue;
    00233
    00234     QString uid = (*it).uid();
    00235
    00236     query.exec( "INSERT INTO kaddressbook_main_" + mUser + " VALUES ('" +
    00237         (*it).uid() + "','" +
    00238         (*it).name() + "','" +
    00239         (*it).familyName() + "','" +
    00240         (*it).givenName() + "','" +
    00241         (*it).additionalName() + "','" +
    00242         (*it).prefix() + "','" +
    00243         (*it).suffix() + "','" +
    00244         (*it).nickName() + "','" +
    00245         (*it).birthday().toString( Qt::ISODate ) + "','" +
    00246         (*it).mailer() + "','" +
    00247         QString::number( (*it).timeZone().offset() ) + "','" +
    00248         QString::number( (*it).geo().latitude() ) + "','" +
    00249         QString::number( (*it).geo().longitude() ) + "','" +
    00250         (*it).title() + "','" +
    00251         (*it).role() + "','" +
    00252         (*it).organization() + "','" +
    00253         (*it).note() + "','" +
    00254         (*it).productId() + "','" +
    00255         (*it).revision().toString( Qt::ISODate ) + "','" +
    00256         (*it).sortString() + "','" +
    00257         (*it).url().url() + "')"
    00258     );
    00259
    00260     // emails
    00261     {
    00262       QStringList emails = (*it).emails();
    00263       QStringList::ConstIterator it;
    00264       bool preferred = true;
    00265       for( it = emails.begin(); it != emails.end(); ++it ) {
    00266         query.exec("INSERT INTO kaddressbook_emails VALUES ('" +
    00267             uid + "','" +
    00268             (*it) + "','" +
    00269             QString::number(preferred) + "')");
    00270         preferred = false;
    00271       }
    00272     }
    00273
    00274     // phonenumbers
    00275     {
    00276       PhoneNumber::List phoneNumberList = (*it).phoneNumbers();
    00277       PhoneNumber::List::ConstIterator it;
    00278       for( it = phoneNumberList.begin(); it != phoneNumberList.end(); ++it ) {
    00279         query.exec("INSERT INTO kaddressbook_phones VALUES ('" +
    00280             uid + "','" +
    00281             (*it).number() + "','" +
    00282             QString::number( (*it).type() ) + "')");
    00283       }
    00284     }
    00285
    00286     // postal addresses
    00287     {
    00288       Address::List addressList = (*it).addresses();
    00289       Address::List::ConstIterator it;
    00290       for( it = addressList.begin(); it != addressList.end(); ++it ) {
    00291         query.exec("INSERT INTO kaddressbook_addresses VALUES ('" +
    00292             uid + "','" +
    00293             (*it).postOfficeBox() + "','" +
    00294             (*it).extended() + "','" +
    00295             (*it).street() + "','" +
    00296             (*it).locality() + "','" +
    00297             (*it).region() + "','" +
    00298             (*it).postalCode() + "','" +
    00299             (*it).country() + "','" +
    00300             (*it).label() + "','" +
    00301             QString::number( (*it).type() ) + "')");
    00302       }
    00303     }
    00304
    00305     // categories
    00306     {
    00307       QStringList categories = (*it).categories();
    00308       QStringList::ConstIterator it;
    00309       for( it = categories.begin(); it != categories.end(); ++it )
    00310         query.exec("INSERT INTO kaddressbook_categories VALUES ('" +
    00311             uid + "','" +
    00312             (*it) + "')");
    00313     }
    00314
    00315     // customs
    00316     {
    00317       QStringList list = (*it).customs();
    00318       QStringList::ConstIterator it;
    00319       for( it = list.begin(); it != list.end(); ++it ) {
    00320         int dashPos = (*it).find( '-' );
    00321         int colonPos = (*it).find( ':' );
    00322         QString app = (*it).left( dashPos );
    00323         QString name = (*it).mid( dashPos + 1, colonPos - dashPos - 1 );
    00324         QString value = (*it).right( (*it).length() - colonPos - 1 );
    00325
    00326         query.exec("INSERT INTO kaddressbook_categories VALUES ('" +
    00327             uid + "','" + app + "','" + name + "','" + value + "')");
    00328       }
    00329     }
    00330   }
    00331
    00332   return true;
    00333 }
    00334
    00335 QString ResourceSql::identifier() const
    00336 {
    00337   return mHost + "_" + mDbName;
    00338 }

This file is part of the documentation for kabc Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:31 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003