libtdepim

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