21 #include <tqsqldatabase.h>
22 #include <tqsqlcursor.h>
25 #include <tdeglobal.h>
26 #include <klineedit.h>
29 #include "resourcesql.h"
30 #include "resourcesqlconfig.h"
36 TDE_EXPORT
void *init_tdeabc_sql()
38 return new KRES::PluginFactory<ResourceSql,ResourceSqlConfig>();
43 : Resource( ab ), mDb( 0 )
45 TQString user, password, db, host;
48 password = cryptStr( config->
readEntry(
"SqlPassword " ) );
52 init( user, password, db, host );
55 ResourceSql::ResourceSql(
AddressBook *ab,
const TQString &user,
56 const TQString &password,
const TQString &db,
const TQString &host )
57 : Resource( ab ), mDb( 0 )
59 init( user, password, db, host );
62 void ResourceSql::init(
const TQString &user,
const TQString &password,
63 const TQString &db,
const TQString &host )
71 Ticket *ResourceSql::requestSaveTicket()
73 if ( !addressBook() ) {
78 return createTicket(
this );
81 void ResourceSql::releaseSaveTicket(
Ticket *ticket )
86 bool ResourceSql::open()
88 TQStringList drivers = TQSqlDatabase::drivers();
89 for ( TQStringList::Iterator it = drivers.begin(); it != drivers.end(); ++it ) {
93 mDb = TQSqlDatabase::addDatabase(
"QMYSQL3" );
96 kdDebug(5700) <<
"Error. Unable to connect to database." <<
endl;
100 mDb->setDatabaseName( mDbName );
101 mDb->setUserName( mUser );
102 mDb->setPassword( mPassword );
103 mDb->setHostName( mHost );
105 if ( !mDb->open() ) {
106 kdDebug(5700) <<
"Error. Unable to open database '" << mDbName <<
"'." <<
endl;
113 void ResourceSql::close()
118 bool ResourceSql::load()
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 );
126 while ( query.next() ) {
127 TQString addrId = query.value(0).toString();
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() );
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() );
163 TQSqlQuery phonesQuery(
"select number, type from kaddressbook_phones "
164 "where addressId = '" + addrId +
"'" );
165 while ( phonesQuery.next() )
167 phonesQuery.value( 1 ).toInt() ) );
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() ) {
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() );
183 a.
setCountry( addressesQuery.value(6).toString() );
184 a.
setLabel( addressesQuery.value(7).toString() );
185 a.
setType( addressesQuery.value(8).toInt() );
193 TQSqlQuery categoriesQuery(
"select category from kaddressbook_categories "
194 "where addressId = '" + addrId +
"'" );
195 while ( categoriesQuery.next() )
201 TQSqlQuery customsQuery(
"select app, name, value from kaddressbook_customs "
202 "where addressId = '" + addrId +
"'" );
203 while ( customsQuery.next() )
205 customsQuery.value( 1 ).toString(),
206 customsQuery.value( 2 ).toString());
209 addressBook()->insertAddressee( addr );
215 bool ResourceSql::save(
Ticket * )
218 TQSqlQuery query(
"select addressId from kaddressbook_main_" + mUser );
220 while ( query.next() ) {
221 TQString addrId = query.value( 0 ).toString();
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 +
"'" );
230 q.exec(
"DELETE FROM kaddressbook_main_" + mUser +
" WHERE addressId = '" + addrId +
"'" );
235 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
236 if ( (*it).resource() !=
this && (*it).resource() != 0 )
239 TQString uid = (*it).uid();
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() +
"')"
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 ('" +
274 TQString::number(preferred) +
"')");
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 ('" +
286 (*it).number() +
"','" +
287 TQString::number( (*it).type() ) +
"')");
294 Address::List::ConstIterator it;
295 for( it = addressList.begin(); it != addressList.end(); ++it ) {
296 query.exec(
"INSERT INTO kaddressbook_addresses VALUES ('" +
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() ) +
"')");
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 ('" +
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 );
331 query.exec(
"INSERT INTO kaddressbook_categories VALUES ('" +
332 uid +
"','" + app +
"','" + name +
"','" + value +
"')");
340 TQString ResourceSql::identifier()
const
342 return mHost +
"_" + mDbName;
Postal address information.
void setPostalCode(const TQString &)
Sets the postal code.
void setExtended(const TQString &)
Sets the extended address information.
void setLocality(const TQString &)
Sets the locality, e.g.
void setType(int type)
Sets the type of address.
TQValueList< Address > List
List of addresses.
void setRegion(const TQString &)
Sets the region, e.g.
void setLabel(const TQString &)
Sets the delivery label.
void setStreet(const TQString &)
Sets the street (including number).
void setCountry(const TQString &)
Sets the country.
void setPostOfficeBox(const TQString &)
Sets the post office box.
void setUid(const TQString &uid)
Set unique identifier.
void insertPhoneNumber(const PhoneNumber &phoneNumber)
Insert a phone number.
void insertCustom(const TQString &app, const TQString &name, const TQString &value)
Insert custom entry.
void insertCategory(const TQString &)
Insert category.
void insertEmail(const TQString &email, bool preferred=false)
Insert an email address.
void insertAddress(const Address &address)
Insert an address.
void setResource(Resource *resource)
Set resource where the addressee is from.
Helper class for handling coordinated save of address books.
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
static data, shared by ALL addressee objects
TQString name(StdAccel id)