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