libtdepim

kaddrbook.cpp
1// kaddrbook.cpp
2// Author: Stefan Taferner <taferner@kde.org>
3// This code is under GPL
4
5#include <config.h>
6
7#include "kaddrbook.h"
8
9#ifdef TDEPIM_NEW_DISTRLISTS
10#include "distributionlist.h"
11#else
12#include <tdeabc/distributionlist.h>
13#endif
14
15#include <tdeapplication.h>
16#include <kdebug.h>
17#include <tdelocale.h>
18#include <tdemessagebox.h>
19#include <tdeabc/resource.h>
20#include <tdeabc/stdaddressbook.h>
21#include <tdeabc/vcardconverter.h>
22#include <tdeabc/errorhandler.h>
23#include <tderesources/selectdialog.h>
24#include <dcopref.h>
25#include <dcopclient.h>
26
27#include <tqeventloop.h>
28#include <tqregexp.h>
29
30#include <unistd.h>
31
32//-----------------------------------------------------------------------------
33void KAddrBookExternal::openEmail( const TQString &addr, TQWidget *parent ) {
34 TQString email;
35 TQString name;
36
37 TDEABC::Addressee::parseEmailAddress( addr, name, email );
38
39 TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( true );
40
41 // force a reload of the address book file so that changes that were made
42 // by other programs are loaded
43 ab->asyncLoad();
44
45 // if we have to reload the address book then we should also wait until
46 // it's completely reloaded
47 // This ugly hack will be removed in 4.0
48 while ( !ab->loadingHasFinished() ) {
49 TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput );
50
51 // use sleep here to reduce cpu usage
52 usleep( 100 );
53 }
54 // End of ugly hack
55
56 TDEABC::Addressee::List addressees = ab->findByEmail( email );
57
58 if ( addressees.count() > 0 ) {
59 if ( tdeApp->dcopClient()->isApplicationRegistered( "kaddressbook" ) ){
60 //make sure kaddressbook is loaded, otherwise showContactEditor
61 //won't work as desired, see bug #87233
62 DCOPRef call ( "kaddressbook", "kaddressbook" );
63 call.send( "newInstance()" );
64 } else {
65 tdeApp->startServiceByDesktopName( "kaddressbook" );
66 }
67
68 DCOPRef call( "kaddressbook", "KAddressBookIface" );
69 call.send( "showContactEditor(TQString)", addressees.first().uid() );
70 } else {
71 //TODO: Enable the better message at the next string unfreeze
72#if 0
73 TQString text = i18n("<qt>The email address <b>%1</b> cannot be "
74 "found in your addressbook.</qt>").arg( email );
75#else
76 TQString text = email + " " + i18n( "is not in address book" );
77#endif
78 KMessageBox::information( parent, text, TQString(), "notInAddressBook" );
79 }
80}
81
82//-----------------------------------------------------------------------------
83void KAddrBookExternal::addEmail( const TQString& addr, TQWidget *parent) {
84 TQString email;
85 TQString name;
86
87 TDEABC::Addressee::parseEmailAddress( addr, name, email );
88
89 TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( true );
90
91 ab->setErrorHandler( new TDEABC::GuiErrorHandler( parent ) );
92
93 // force a reload of the address book file so that changes that were made
94 // by other programs are loaded
95 ab->asyncLoad();
96
97 // if we have to reload the address book then we should also wait until
98 // it's completely reloaded
99 // This ugly hack will be removed in 4.0
100 while ( !ab->loadingHasFinished() ) {
101 TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput );
102
103 // use sleep here to reduce cpu usage
104 usleep( 100 );
105 }
106 // End of ugly hack
107
108 TDEABC::Addressee::List addressees = ab->findByEmail( email );
109
110 if ( addressees.isEmpty() ) {
111 TDEABC::Addressee a;
112 a.setNameFromString( name );
113 a.insertEmail( email, true );
114
115 {
116 TDEConfig config( "kaddressbookrc" );
117 config.setGroup( "General" );
118 int type = config.readNumEntry( "FormattedNameType", 1 );
119
120 TQString name;
121 switch ( type ) {
122 case 1:
123 name = a.givenName() + " " + a.familyName();
124 break;
125 case 2:
126 name = a.assembledName();
127 break;
128 case 3:
129 name = a.familyName() + ", " + a.givenName();
130 break;
131 case 4:
132 name = a.familyName() + " " + a.givenName();
133 break;
134 case 5:
135 name = a.organization();
136 break;
137 default:
138 name = "";
139 break;
140 }
141 name.simplifyWhiteSpace();
142
143 a.setFormattedName( name );
144 }
145
146 if ( KAddrBookExternal::addAddressee( a ) ) {
147 TQString text = i18n("<qt>The email address <b>%1</b> was added to your "
148 "addressbook; you can add more information to this "
149 "entry by opening the addressbook.</qt>").arg( addr );
150 KMessageBox::information( parent, text, TQString(), "addedtokabc" );
151 }
152 } else {
153 TQString text = i18n("<qt>The email address <b>%1</b> is already in your "
154 "addressbook.</qt>").arg( addr );
155 KMessageBox::information( parent, text, TQString(),
156 "alreadyInAddressBook" );
157 }
158 ab->setErrorHandler( 0 );
159}
160
161void KAddrBookExternal::openAddressBook(TQWidget *) {
162 tdeApp->startServiceByDesktopName( "kaddressbook" );
163}
164
165void KAddrBookExternal::addNewAddressee( TQWidget* )
166{
167 tdeApp->startServiceByDesktopName("kaddressbook");
168 DCOPRef call("kaddressbook", "KAddressBookIface");
169 call.send("newContact()");
170}
171
172bool KAddrBookExternal::addVCard( const TDEABC::Addressee& addressee, TQWidget *parent )
173{
174 TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( true );
175 bool inserted = false;
176
177 ab->setErrorHandler( new TDEABC::GuiErrorHandler( parent ) );
178
179 TDEABC::Addressee::List addressees =
180 ab->findByEmail( addressee.preferredEmail() );
181
182 if ( addressees.isEmpty() ) {
183 if ( KAddrBookExternal::addAddressee( addressee ) ) {
184 TQString text = i18n("The VCard was added to your addressbook; "
185 "you can add more information to this "
186 "entry by opening the addressbook.");
187 KMessageBox::information( parent, text, TQString(), "addedtokabc" );
188 inserted = true;
189 }
190 } else {
191 TQString text = i18n("The VCard's primary email address is already in "
192 "your addressbook; however, you may save the VCard "
193 "into a file and import it into the addressbook "
194 "manually.");
195 KMessageBox::information( parent, text );
196 inserted = true;
197 }
198
199 ab->setErrorHandler( 0 );
200 return inserted;
201}
202
203bool KAddrBookExternal::addAddressee( const TDEABC::Addressee& addr )
204{
205 TDEABC::AddressBook *addressBook = TDEABC::StdAddressBook::self( true );
206 TDEABC::Resource *tdeabcResource = selectResourceForSaving( addressBook );
207 if( !tdeabcResource )
208 return false;
209 TDEABC::Ticket *ticket = addressBook->requestSaveTicket( tdeabcResource );
210 bool saved = false;
211 if ( ticket ) {
212 TDEABC::Addressee addressee( addr );
213 addressee.setResource( tdeabcResource );
214 addressBook->insertAddressee( addressee );
215 saved = addressBook->save( ticket );
216 if ( !saved )
217 addressBook->releaseSaveTicket( ticket );
218 }
219
220 addressBook->emitAddressBookChanged();
221
222 return saved;
223}
224
225TQString KAddrBookExternal::expandDistributionList( const TQString& listName )
226{
227 if ( listName.isEmpty() )
228 return TQString();
229
230 const TQString lowerListName = listName.lower();
231 TDEABC::AddressBook *addressBook = TDEABC::StdAddressBook::self( true );
232#ifdef TDEPIM_NEW_DISTRLISTS
233 KPIM::DistributionList distrList = KPIM::DistributionList::findByName( addressBook, lowerListName, false );
234 if ( !distrList.isEmpty() ) {
235 return distrList.emails( addressBook ).join( ", " );
236 }
237#else
238 TDEABC::DistributionListManager manager( addressBook );
239 manager.load();
240 const TQStringList listNames = manager.listNames();
241
242 for ( TQStringList::ConstIterator it = listNames.begin();
243 it != listNames.end(); ++it) {
244 if ( (*it).lower() == lowerListName ) {
245 const TQStringList addressList = manager.list( *it )->emails();
246 return addressList.join( ", " );
247 }
248 }
249#endif
250 return TQString();
251}
252
253TDEABC::Resource* KAddrBookExternal::selectResourceForSaving( TDEABC::AddressBook *addressBook )
254{
255 // This ugly hack will be removed in 4.0
256 while ( !addressBook->loadingHasFinished() ) {
257 TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput );
258
259 // use sleep here to reduce cpu usage
260 usleep( 100 );
261 }
262 // End of ugly hack
263
264 // Select a resource
265 TQPtrList<TDEABC::Resource> tdeabcResources = addressBook->resources();
266
267 TQPtrList<KRES::Resource> kresResources;
268 TQPtrListIterator<TDEABC::Resource> resIt( tdeabcResources );
269 TDEABC::Resource *tdeabcResource;
270 while ( ( tdeabcResource = resIt.current() ) != 0 ) {
271 ++resIt;
272 if ( !tdeabcResource->readOnly() ) {
273 KRES::Resource *res = static_cast<KRES::Resource*>( tdeabcResource );
274 if ( res )
275 kresResources.append( res );
276 }
277 }
278
279 return static_cast<TDEABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) );
280}
Distribution list of email addresses.
TQStringList emails(TDEABC::AddressBook *book) const
Return list of email addresses, which belong to this distributon list.