• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kabc
 

kabc

  • kabc
  • plugins
  • sql
resourcesql.cpp
1 /*
2  This file is part of libkabc.
3  Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <tqsqldatabase.h>
22 #include <tqsqlcursor.h>
23 
24 #include <kdebug.h>
25 #include <kglobal.h>
26 #include <klineedit.h>
27 #include <klocale.h>
28 
29 #include "resourcesql.h"
30 #include "resourcesqlconfig.h"
31 
32 using namespace KABC;
33 
34 extern "C"
35 {
36  KDE_EXPORT void *init_kabc_sql()
37  {
38  return new KRES::PluginFactory<ResourceSql,ResourceSqlConfig>();
39  }
40 }
41 
42 ResourceSql::ResourceSql( AddressBook *ab, const KConfig *config )
43  : Resource( ab ), mDb( 0 )
44 {
45  TQString user, password, db, host;
46 
47  user = config->readEntry( "SqlUser" );
48  password = cryptStr( config->readEntry( "SqlPassword " ) );
49  db = config->readEntry( "SqlName" );
50  host = config->readEntry( "SqlHost" );
51 
52  init( user, password, db, host );
53 }
54 
55 ResourceSql::ResourceSql( AddressBook *ab, const TQString &user,
56  const TQString &password, const TQString &db, const TQString &host )
57  : Resource( ab ), mDb( 0 )
58 {
59  init( user, password, db, host );
60 }
61 
62 void ResourceSql::init( const TQString &user, const TQString &password,
63  const TQString &db, const TQString &host )
64 {
65  mUser = user;
66  mPassword = password;
67  mDbName = db;
68  mHost = host;
69 }
70 
71 Ticket *ResourceSql::requestSaveTicket()
72 {
73  if ( !addressBook() ) {
74  kdDebug(5700) << "no addressbook" << endl;
75  return 0;
76  }
77 
78  return createTicket( this );
79 }
80 
81 bool ResourceSql::open()
82 {
83  TQStringList drivers = TQSqlDatabase::drivers();
84  for ( TQStringList::Iterator it = drivers.begin(); it != drivers.end(); ++it ) {
85  kdDebug(5700) << "Driver: " << (*it) << endl;
86  }
87 
88  mDb = TQSqlDatabase::addDatabase( "QMYSQL3" );
89 
90  if ( !mDb ) {
91  kdDebug(5700) << "Error. Unable to connect to database." << endl;
92  return false;
93  }
94 
95  mDb->setDatabaseName( mDbName );
96  mDb->setUserName( mUser );
97  mDb->setPassword( mPassword );
98  mDb->setHostName( mHost );
99 
100  if ( !mDb->open() ) {
101  kdDebug(5700) << "Error. Unable to open database '" << mDbName << "'." << endl;
102  return false;
103  }
104 
105  return true;
106 }
107 
108 void ResourceSql::close()
109 {
110  mDb->close();
111 }
112 
113 bool ResourceSql::load()
114 {
115  TQSqlQuery query( "select addressId, name, familyName, givenName, "
116  "additionalName, prefix, suffix, nickname, birthday, "
117  "mailer, timezone, geo_latitude, geo_longitude, title, "
118  "role, organization, note, productId, revision, "
119  "sortString, url from kaddressbook_main_" + mUser );
120 
121  while ( query.next() ) {
122  TQString addrId = query.value(0).toString();
123 
124  Addressee addr;
125  addr.setResource( this );
126  addr.setUid( addrId );
127  addr.setName( query.value(1).toString() );
128  addr.setFamilyName( query.value(2).toString() );
129  addr.setGivenName( query.value(3).toString() );
130  addr.setAdditionalName( query.value(4).toString() );
131  addr.setPrefix( query.value(5).toString() );
132  addr.setSuffix( query.value(6).toString() );
133  addr.setNickName( query.value(7).toString() );
134  addr.setBirthday( query.value(8).toDateTime() );
135  addr.setMailer( query.value(9).toString() );
136  addr.setTimeZone( TimeZone( query.value(10).toInt() ) );
137  addr.setGeo( Geo( query.value(11).toDouble(), query.value(12).toDouble() ) );
138  addr.setTitle( query.value(13).toString() );
139  addr.setRole( query.value(14).toString() );
140  addr.setOrganization( query.value(15).toString() );
141  addr.setNote( query.value(16).toString() );
142  addr.setProductId( query.value(17).toString() );
143  addr.setRevision( query.value(18).toDateTime() );
144  addr.setSortString( query.value(19).toString() );
145  addr.setUrl( query.value(20).toString() );
146 
147  // emails
148  {
149  TQSqlQuery emailsQuery( "select email, preferred from kaddressbook_emails "
150  "where addressId = '" + addrId + "'" );
151  while ( emailsQuery.next() )
152  addr.insertEmail( emailsQuery.value( 0 ).toString(),
153  emailsQuery.value( 1 ).toInt() );
154  }
155 
156  // phones
157  {
158  TQSqlQuery phonesQuery( "select number, type from kaddressbook_phones "
159  "where addressId = '" + addrId + "'" );
160  while ( phonesQuery.next() )
161  addr.insertPhoneNumber( PhoneNumber( phonesQuery.value( 0 ).toString(),
162  phonesQuery.value( 1 ).toInt() ) );
163  }
164 
165  // addresses
166  {
167  TQSqlQuery addressesQuery( "select postOfficeBox, extended, street, "
168  "locality, region, postalCode, country, label, type "
169  "from kaddressbook_addresses where addressId = '" + addrId + "'" );
170  while ( addressesQuery.next() ) {
171  Address a;
172  a.setPostOfficeBox( addressesQuery.value(0).toString() );
173  a.setExtended( addressesQuery.value(1).toString() );
174  a.setStreet( addressesQuery.value(2).toString() );
175  a.setLocality( addressesQuery.value(3).toString() );
176  a.setRegion( addressesQuery.value(4).toString() );
177  a.setPostalCode( addressesQuery.value(5).toString() );
178  a.setCountry( addressesQuery.value(6).toString() );
179  a.setLabel( addressesQuery.value(7).toString() );
180  a.setType( addressesQuery.value(8).toInt() );
181 
182  addr.insertAddress( a );
183  }
184  }
185 
186  // categories
187  {
188  TQSqlQuery categoriesQuery( "select category from kaddressbook_categories "
189  "where addressId = '" + addrId + "'" );
190  while ( categoriesQuery.next() )
191  addr.insertCategory( categoriesQuery.value( 0 ).toString() );
192  }
193 
194  // customs
195  {
196  TQSqlQuery customsQuery( "select app, name, value from kaddressbook_customs "
197  "where addressId = '" + addrId + "'" );
198  while ( customsQuery.next() )
199  addr.insertCustom( customsQuery.value( 0 ).toString(),
200  customsQuery.value( 1 ).toString(),
201  customsQuery.value( 2 ).toString());
202  }
203 
204  addressBook()->insertAddressee( addr );
205  }
206 
207  return true;
208 }
209 
210 bool ResourceSql::save( Ticket * )
211 {
212  // we have to delete all entries for this user and reinsert them
213  TQSqlQuery query( "select addressId from kaddressbook_main_" + mUser );
214 
215  while ( query.next() ) {
216  TQString addrId = query.value( 0 ).toString();
217  TQSqlQuery q;
218 
219  q.exec( "DELETE FROM kaddressbook_emails WHERE addressId = '" + addrId + "'" );
220  q.exec( "DELETE FROM kaddressbook_phones WHERE addressId = '" + addrId + "'" );
221  q.exec( "DELETE FROM kaddressbook_addresses WHERE addressId = '" + addrId + "'" );
222  q.exec( "DELETE FROM kaddressbook_categories WHERE addressId = '" + addrId + "'" );
223  q.exec( "DELETE FROM kaddressbook_customs WHERE addressId = '" + addrId + "'" );
224 
225  q.exec( "DELETE FROM kaddressbook_main_" + mUser + " WHERE addressId = '" + addrId + "'" );
226  }
227 
228  // let's start...
229  AddressBook::Iterator it;
230  for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
231  if ( (*it).resource() != this && (*it).resource() != 0 ) // save only my and new entries
232  continue;
233 
234  TQString uid = (*it).uid();
235 
236  query.exec( "INSERT INTO kaddressbook_main_" + mUser + " VALUES ('" +
237  (*it).uid() + "','" +
238  (*it).name() + "','" +
239  (*it).familyName() + "','" +
240  (*it).givenName() + "','" +
241  (*it).additionalName() + "','" +
242  (*it).prefix() + "','" +
243  (*it).suffix() + "','" +
244  (*it).nickName() + "','" +
245  (*it).birthday().toString( Qt::ISODate ) + "','" +
246  (*it).mailer() + "','" +
247  TQString::number( (*it).timeZone().offset() ) + "','" +
248  TQString::number( (*it).geo().latitude() ) + "','" +
249  TQString::number( (*it).geo().longitude() ) + "','" +
250  (*it).title() + "','" +
251  (*it).role() + "','" +
252  (*it).organization() + "','" +
253  (*it).note() + "','" +
254  (*it).productId() + "','" +
255  (*it).revision().toString( Qt::ISODate ) + "','" +
256  (*it).sortString() + "','" +
257  (*it).url().url() + "')"
258  );
259 
260  // emails
261  {
262  TQStringList emails = (*it).emails();
263  TQStringList::ConstIterator it;
264  bool preferred = true;
265  for( it = emails.begin(); it != emails.end(); ++it ) {
266  query.exec("INSERT INTO kaddressbook_emails VALUES ('" +
267  uid + "','" +
268  (*it) + "','" +
269  TQString::number(preferred) + "')");
270  preferred = false;
271  }
272  }
273 
274  // phonenumbers
275  {
276  PhoneNumber::List phoneNumberList = (*it).phoneNumbers();
277  PhoneNumber::List::ConstIterator it;
278  for( it = phoneNumberList.begin(); it != phoneNumberList.end(); ++it ) {
279  query.exec("INSERT INTO kaddressbook_phones VALUES ('" +
280  uid + "','" +
281  (*it).number() + "','" +
282  TQString::number( (*it).type() ) + "')");
283  }
284  }
285 
286  // postal addresses
287  {
288  Address::List addressList = (*it).addresses();
289  Address::List::ConstIterator it;
290  for( it = addressList.begin(); it != addressList.end(); ++it ) {
291  query.exec("INSERT INTO kaddressbook_addresses VALUES ('" +
292  uid + "','" +
293  (*it).postOfficeBox() + "','" +
294  (*it).extended() + "','" +
295  (*it).street() + "','" +
296  (*it).locality() + "','" +
297  (*it).region() + "','" +
298  (*it).postalCode() + "','" +
299  (*it).country() + "','" +
300  (*it).label() + "','" +
301  TQString::number( (*it).type() ) + "')");
302  }
303  }
304 
305  // categories
306  {
307  TQStringList categories = (*it).categories();
308  TQStringList::ConstIterator it;
309  for( it = categories.begin(); it != categories.end(); ++it )
310  query.exec("INSERT INTO kaddressbook_categories VALUES ('" +
311  uid + "','" +
312  (*it) + "')");
313  }
314 
315  // customs
316  {
317  TQStringList list = (*it).customs();
318  TQStringList::ConstIterator it;
319  for( it = list.begin(); it != list.end(); ++it ) {
320  int dashPos = (*it).find( '-' );
321  int colonPos = (*it).find( ':' );
322  TQString app = (*it).left( dashPos );
323  TQString name = (*it).mid( dashPos + 1, colonPos - dashPos - 1 );
324  TQString value = (*it).right( (*it).length() - colonPos - 1 );
325 
326  query.exec("INSERT INTO kaddressbook_categories VALUES ('" +
327  uid + "','" + app + "','" + name + "','" + value + "')");
328  }
329  }
330  }
331 
332  return true;
333 }
334 
335 TQString ResourceSql::identifier() const
336 {
337  return mHost + "_" + mDbName;
338 }

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kabc

Skip menu "kabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kabc by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |