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

tdeabc

  • tdeabc
  • plugins
  • sql
resourcesql.cpp
1/*
2 This file is part of libtdeabc.
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 <tdeglobal.h>
26#include <klineedit.h>
27#include <tdelocale.h>
28
29#include "resourcesql.h"
30#include "resourcesqlconfig.h"
31
32using namespace TDEABC;
33
34extern "C"
35{
36 TDE_EXPORT void *init_tdeabc_sql()
37 {
38 return new KRES::PluginFactory<ResourceSql,ResourceSqlConfig>();
39 }
40}
41
42ResourceSql::ResourceSql( AddressBook *ab, const TDEConfig *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
55ResourceSql::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
62void 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
71Ticket *ResourceSql::requestSaveTicket()
72{
73 if ( !addressBook() ) {
74 kdDebug(5700) << "no addressbook" << endl;
75 return 0;
76 }
77
78 return createTicket( this );
79}
80
81void ResourceSql::releaseSaveTicket( Ticket *ticket )
82{
83 delete ticket;
84}
85
86bool ResourceSql::open()
87{
88 TQStringList drivers = TQSqlDatabase::drivers();
89 for ( TQStringList::Iterator it = drivers.begin(); it != drivers.end(); ++it ) {
90 kdDebug(5700) << "Driver: " << (*it) << endl;
91 }
92
93 mDb = TQSqlDatabase::addDatabase( "QMYSQL3" );
94
95 if ( !mDb ) {
96 kdDebug(5700) << "Error. Unable to connect to database." << endl;
97 return false;
98 }
99
100 mDb->setDatabaseName( mDbName );
101 mDb->setUserName( mUser );
102 mDb->setPassword( mPassword );
103 mDb->setHostName( mHost );
104
105 if ( !mDb->open() ) {
106 kdDebug(5700) << "Error. Unable to open database '" << mDbName << "'." << endl;
107 return false;
108 }
109
110 return true;
111}
112
113void ResourceSql::close()
114{
115 mDb->close();
116}
117
118bool ResourceSql::load()
119{
120 TQSqlQuery query( "select addressId, name, familyName, givenName, "
121 "additionalName, prefix, suffix, nickname, birthday, "
122 "mailer, timezone, geo_latitude, geo_longitude, title, "
123 "role, organization, note, productId, revision, "
124 "sortString, url from kaddressbook_main_" + mUser );
125
126 while ( query.next() ) {
127 TQString addrId = query.value(0).toString();
128
129 Addressee addr;
130 addr.setResource( this );
131 addr.setUid( addrId );
132 addr.setName( query.value(1).toString() );
133 addr.setFamilyName( query.value(2).toString() );
134 addr.setGivenName( query.value(3).toString() );
135 addr.setAdditionalName( query.value(4).toString() );
136 addr.setPrefix( query.value(5).toString() );
137 addr.setSuffix( query.value(6).toString() );
138 addr.setNickName( query.value(7).toString() );
139 addr.setBirthday( query.value(8).toDateTime() );
140 addr.setMailer( query.value(9).toString() );
141 addr.setTimeZone( TimeZone( query.value(10).toInt() ) );
142 addr.setGeo( Geo( query.value(11).toDouble(), query.value(12).toDouble() ) );
143 addr.setTitle( query.value(13).toString() );
144 addr.setRole( query.value(14).toString() );
145 addr.setOrganization( query.value(15).toString() );
146 addr.setNote( query.value(16).toString() );
147 addr.setProductId( query.value(17).toString() );
148 addr.setRevision( query.value(18).toDateTime() );
149 addr.setSortString( query.value(19).toString() );
150 addr.setUrl( query.value(20).toString() );
151
152 // emails
153 {
154 TQSqlQuery emailsQuery( "select email, preferred from kaddressbook_emails "
155 "where addressId = '" + addrId + "'" );
156 while ( emailsQuery.next() )
157 addr.insertEmail( emailsQuery.value( 0 ).toString(),
158 emailsQuery.value( 1 ).toInt() );
159 }
160
161 // phones
162 {
163 TQSqlQuery phonesQuery( "select number, type from kaddressbook_phones "
164 "where addressId = '" + addrId + "'" );
165 while ( phonesQuery.next() )
166 addr.insertPhoneNumber( PhoneNumber( phonesQuery.value( 0 ).toString(),
167 phonesQuery.value( 1 ).toInt() ) );
168 }
169
170 // addresses
171 {
172 TQSqlQuery addressesQuery( "select postOfficeBox, extended, street, "
173 "locality, region, postalCode, country, label, type "
174 "from kaddressbook_addresses where addressId = '" + addrId + "'" );
175 while ( addressesQuery.next() ) {
176 Address a;
177 a.setPostOfficeBox( addressesQuery.value(0).toString() );
178 a.setExtended( addressesQuery.value(1).toString() );
179 a.setStreet( addressesQuery.value(2).toString() );
180 a.setLocality( addressesQuery.value(3).toString() );
181 a.setRegion( addressesQuery.value(4).toString() );
182 a.setPostalCode( addressesQuery.value(5).toString() );
183 a.setCountry( addressesQuery.value(6).toString() );
184 a.setLabel( addressesQuery.value(7).toString() );
185 a.setType( addressesQuery.value(8).toInt() );
186
187 addr.insertAddress( a );
188 }
189 }
190
191 // categories
192 {
193 TQSqlQuery categoriesQuery( "select category from kaddressbook_categories "
194 "where addressId = '" + addrId + "'" );
195 while ( categoriesQuery.next() )
196 addr.insertCategory( categoriesQuery.value( 0 ).toString() );
197 }
198
199 // customs
200 {
201 TQSqlQuery customsQuery( "select app, name, value from kaddressbook_customs "
202 "where addressId = '" + addrId + "'" );
203 while ( customsQuery.next() )
204 addr.insertCustom( customsQuery.value( 0 ).toString(),
205 customsQuery.value( 1 ).toString(),
206 customsQuery.value( 2 ).toString());
207 }
208
209 addressBook()->insertAddressee( addr );
210 }
211
212 return true;
213}
214
215bool ResourceSql::save( Ticket * )
216{
217 // we have to delete all entries for this user and reinsert them
218 TQSqlQuery query( "select addressId from kaddressbook_main_" + mUser );
219
220 while ( query.next() ) {
221 TQString addrId = query.value( 0 ).toString();
222 TQSqlQuery q;
223
224 q.exec( "DELETE FROM kaddressbook_emails WHERE addressId = '" + addrId + "'" );
225 q.exec( "DELETE FROM kaddressbook_phones WHERE addressId = '" + addrId + "'" );
226 q.exec( "DELETE FROM kaddressbook_addresses WHERE addressId = '" + addrId + "'" );
227 q.exec( "DELETE FROM kaddressbook_categories WHERE addressId = '" + addrId + "'" );
228 q.exec( "DELETE FROM kaddressbook_customs WHERE addressId = '" + addrId + "'" );
229
230 q.exec( "DELETE FROM kaddressbook_main_" + mUser + " WHERE addressId = '" + addrId + "'" );
231 }
232
233 // let's start...
234 AddressBook::Iterator it;
235 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
236 if ( (*it).resource() != this && (*it).resource() != 0 ) // save only my and new entries
237 continue;
238
239 TQString uid = (*it).uid();
240
241 query.exec( "INSERT INTO kaddressbook_main_" + mUser + " VALUES ('" +
242 (*it).uid() + "','" +
243 (*it).name() + "','" +
244 (*it).familyName() + "','" +
245 (*it).givenName() + "','" +
246 (*it).additionalName() + "','" +
247 (*it).prefix() + "','" +
248 (*it).suffix() + "','" +
249 (*it).nickName() + "','" +
250 (*it).birthday().toString( TQt::ISODate ) + "','" +
251 (*it).mailer() + "','" +
252 TQString::number( (*it).timeZone().offset() ) + "','" +
253 TQString::number( (*it).geo().latitude() ) + "','" +
254 TQString::number( (*it).geo().longitude() ) + "','" +
255 (*it).title() + "','" +
256 (*it).role() + "','" +
257 (*it).organization() + "','" +
258 (*it).note() + "','" +
259 (*it).productId() + "','" +
260 (*it).revision().toString( TQt::ISODate ) + "','" +
261 (*it).sortString() + "','" +
262 (*it).url().url() + "')"
263 );
264
265 // emails
266 {
267 TQStringList emails = (*it).emails();
268 TQStringList::ConstIterator it;
269 bool preferred = true;
270 for( it = emails.begin(); it != emails.end(); ++it ) {
271 query.exec("INSERT INTO kaddressbook_emails VALUES ('" +
272 uid + "','" +
273 (*it) + "','" +
274 TQString::number(preferred) + "')");
275 preferred = false;
276 }
277 }
278
279 // phonenumbers
280 {
281 PhoneNumber::List phoneNumberList = (*it).phoneNumbers();
282 PhoneNumber::List::ConstIterator it;
283 for( it = phoneNumberList.begin(); it != phoneNumberList.end(); ++it ) {
284 query.exec("INSERT INTO kaddressbook_phones VALUES ('" +
285 uid + "','" +
286 (*it).number() + "','" +
287 TQString::number( (*it).type() ) + "')");
288 }
289 }
290
291 // postal addresses
292 {
293 Address::List addressList = (*it).addresses();
294 Address::List::ConstIterator it;
295 for( it = addressList.begin(); it != addressList.end(); ++it ) {
296 query.exec("INSERT INTO kaddressbook_addresses VALUES ('" +
297 uid + "','" +
298 (*it).postOfficeBox() + "','" +
299 (*it).extended() + "','" +
300 (*it).street() + "','" +
301 (*it).locality() + "','" +
302 (*it).region() + "','" +
303 (*it).postalCode() + "','" +
304 (*it).country() + "','" +
305 (*it).label() + "','" +
306 TQString::number( (*it).type() ) + "')");
307 }
308 }
309
310 // categories
311 {
312 TQStringList categories = (*it).categories();
313 TQStringList::ConstIterator it;
314 for( it = categories.begin(); it != categories.end(); ++it )
315 query.exec("INSERT INTO kaddressbook_categories VALUES ('" +
316 uid + "','" +
317 (*it) + "')");
318 }
319
320 // customs
321 {
322 TQStringList list = (*it).customs();
323 TQStringList::ConstIterator it;
324 for( it = list.begin(); it != list.end(); ++it ) {
325 int dashPos = (*it).find( '-' );
326 int colonPos = (*it).find( ':' );
327 TQString app = (*it).left( dashPos );
328 TQString name = (*it).mid( dashPos + 1, colonPos - dashPos - 1 );
329 TQString value = (*it).right( (*it).length() - colonPos - 1 );
330
331 query.exec("INSERT INTO kaddressbook_categories VALUES ('" +
332 uid + "','" + app + "','" + name + "','" + value + "')");
333 }
334 }
335 }
336
337 return true;
338}
339
340TQString ResourceSql::identifier() const
341{
342 return mHost + "_" + mDbName;
343}
TDEABC::AddressBook::Iterator
Address Book Iterator.
Definition: addressbook.h:58
TDEABC::AddressBook
Address Book.
Definition: addressbook.h:44
TDEABC::Address
Postal address information.
Definition: address.h:56
TDEABC::Address::setPostalCode
void setPostalCode(const TQString &)
Sets the postal code.
Definition: address.cpp:221
TDEABC::Address::setExtended
void setExtended(const TQString &)
Sets the extended address information.
Definition: address.cpp:149
TDEABC::Address::setLocality
void setLocality(const TQString &)
Sets the locality, e.g.
Definition: address.cpp:185
TDEABC::Address::setType
void setType(int type)
Sets the type of address.
Definition: address.cpp:100
TDEABC::Address::List
TQValueList< Address > List
List of addresses.
Definition: address.h:64
TDEABC::Address::setRegion
void setRegion(const TQString &)
Sets the region, e.g.
Definition: address.cpp:203
TDEABC::Address::setLabel
void setLabel(const TQString &)
Sets the delivery label.
Definition: address.cpp:257
TDEABC::Address::setStreet
void setStreet(const TQString &)
Sets the street (including number).
Definition: address.cpp:167
TDEABC::Address::setCountry
void setCountry(const TQString &)
Sets the country.
Definition: address.cpp:239
TDEABC::Address::setPostOfficeBox
void setPostOfficeBox(const TQString &)
Sets the post office box.
Definition: address.cpp:131
TDEABC::Addressee
address book entry
Definition: addressee.src.h:75
TDEABC::Addressee::setUid
void setUid(const TQString &uid)
Set unique identifier.
Definition: addressee.src.cpp:166
TDEABC::Addressee::insertPhoneNumber
void insertPhoneNumber(const PhoneNumber &phoneNumber)
Insert a phone number.
Definition: addressee.src.cpp:460
TDEABC::Addressee::insertCustom
void insertCustom(const TQString &app, const TQString &name, const TQString &value)
Insert custom entry.
Definition: addressee.src.cpp:791
TDEABC::Addressee::insertCategory
void insertCategory(const TQString &)
Insert category.
Definition: addressee.src.cpp:753
TDEABC::Addressee::insertEmail
void insertEmail(const TQString &email, bool preferred=false)
Insert an email address.
Definition: addressee.src.cpp:412
TDEABC::Addressee::insertAddress
void insertAddress(const Address &address)
Insert an address.
Definition: addressee.src.cpp:675
TDEABC::Addressee::setResource
void setResource(Resource *resource)
Set resource where the addressee is from.
Definition: addressee.src.cpp:1013
TDEABC::Geo
Geographic position.
Definition: geo.h:36
TDEABC::PhoneNumber
Phonenumber information.
Definition: phonenumber.h:39
TDEABC::Ticket
Helper class for handling coordinated save of address books.
Definition: resource.h:38
TDEABC::TimeZone
Time zone information.
Definition: timezone.h:36
TDEConfigBase::readEntry
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
TDEConfig
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48
TDEStdAccel::name
TQString name(StdAccel id)
tdelocale.h

tdeabc

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

tdeabc

Skip menu "tdeabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeabc by doxygen 1.9.4
This website is maintained by Timothy Pearson.