libkdepim

kaddrbook.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*-
00002 // kaddrbook.cpp
00003 // Author: Stefan Taferner <taferner@kde.org>
00004 // This code is under GPL
00005 
00006 #include <config.h>
00007 
00008 #include "kaddrbook.h"
00009 
00010 #ifdef KDEPIM_NEW_DISTRLISTS
00011 #include "distributionlist.h"
00012 #else
00013 #include <kabc/distributionlist.h>
00014 #endif
00015 
00016 #include <kapplication.h>
00017 #include <kdebug.h>
00018 #include <klocale.h>
00019 #include <kmessagebox.h>
00020 #include <kdeversion.h>
00021 #include <kabc/resource.h>
00022 #include <kabc/stdaddressbook.h>
00023 #include <kabc/vcardconverter.h>
00024 #include <kresources/selectdialog.h>
00025 #include <dcopref.h>
00026 #include <dcopclient.h>
00027 
00028 #include <qeventloop.h>
00029 #include <qregexp.h>
00030 
00031 #include <unistd.h>
00032 
00033 //-----------------------------------------------------------------------------
00034 void KAddrBookExternal::openEmail( const QString &addr, QWidget *parent ) {
00035   QString email;
00036   QString name;
00037 
00038   KABC::Addressee::parseEmailAddress( addr, name, email );
00039 
00040   KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00041 
00042   // force a reload of the address book file so that changes that were made
00043   // by other programs are loaded
00044   ab->asyncLoad();
00045 
00046   // if we have to reload the address book then we should also wait until
00047   // it's completely reloaded
00048 #if KDE_IS_VERSION(3,4,89)
00049   // This ugly hack will be removed in 4.0
00050   while ( !ab->loadingHasFinished() ) {
00051     QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00052 
00053     // use sleep here to reduce cpu usage
00054     usleep( 100 );
00055   }
00056 #endif
00057 
00058   KABC::Addressee::List addressees = ab->findByEmail( email );
00059 
00060   if ( addressees.count() > 0 ) {
00061     if ( kapp->dcopClient()->isApplicationRegistered( "kaddressbook" ) ){
00062       //make sure kaddressbook is loaded, otherwise showContactEditor
00063       //won't work as desired, see bug #87233
00064       DCOPRef call ( "kaddressbook", "kaddressbook" );
00065       call.send( "newInstance()" );
00066     }  else {
00067       kapp->startServiceByDesktopName( "kaddressbook" );
00068     }
00069 
00070     DCOPRef call( "kaddressbook", "KAddressBookIface" );
00071     call.send( "showContactEditor(QString)", addressees.first().uid() );
00072   } else {
00073     //TODO: Enable the better message at the next string unfreeze
00074 #if 0
00075     QString text = i18n("<qt>The email address <b>%1</b> cannot be "
00076                         "found in your addressbook.</qt>").arg( email );
00077 #else
00078     QString text = email + " " + i18n( "is not in address book" );
00079 #endif
00080     KMessageBox::information( parent, text, QString::null, "notInAddressBook" );
00081   }
00082 }
00083 
00084 //-----------------------------------------------------------------------------
00085 void KAddrBookExternal::addEmail( const QString& addr, QWidget *parent) {
00086   QString email;
00087   QString name;
00088 
00089   KABC::Addressee::parseEmailAddress( addr, name, email );
00090 
00091   KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00092 
00093   // force a reload of the address book file so that changes that were made
00094   // by other programs are loaded
00095   ab->asyncLoad();
00096 
00097   // if we have to reload the address book then we should also wait until
00098   // it's completely reloaded
00099 #if KDE_IS_VERSION(3,4,89)
00100   // This ugly hack will be removed in 4.0
00101   while ( !ab->loadingHasFinished() ) {
00102     QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00103 
00104     // use sleep here to reduce cpu usage
00105     usleep( 100 );
00106   }
00107 #endif
00108 
00109   KABC::Addressee::List addressees = ab->findByEmail( email );
00110 
00111   if ( addressees.isEmpty() ) {
00112     KABC::Addressee a;
00113     a.setNameFromString( name );
00114     a.insertEmail( email, true );
00115 
00116     if ( !KAddrBookExternal::addAddressee( a ) ) {
00117       KMessageBox::error( parent, i18n("Cannot save to addressbook.") );
00118     } else {
00119       QString text = i18n("<qt>The email address <b>%1</b> was added to your "
00120                           "addressbook; you can add more information to this "
00121                           "entry by opening the addressbook.</qt>").arg( addr );
00122       KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00123     }
00124   } else {
00125     QString text = i18n("<qt>The email address <b>%1</b> is already in your "
00126                         "addressbook.</qt>").arg( addr );
00127     KMessageBox::information( parent, text, QString::null,
00128                               "alreadyInAddressBook" );
00129   }
00130 }
00131 
00132 void KAddrBookExternal::openAddressBook(QWidget *) {
00133   kapp->startServiceByDesktopName( "kaddressbook" );
00134 }
00135 
00136 void KAddrBookExternal::addNewAddressee( QWidget* )
00137 {
00138   kapp->startServiceByDesktopName("kaddressbook");
00139   DCOPRef call("kaddressbook", "KAddressBookIface");
00140   call.send("newContact()");
00141 }
00142 
00143 bool KAddrBookExternal::addVCard( const KABC::Addressee& addressee, QWidget *parent )
00144 {
00145   KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00146   bool inserted = false;
00147 
00148   KABC::Addressee::List addressees =
00149       ab->findByEmail( addressee.preferredEmail() );
00150 
00151   if ( addressees.isEmpty() ) {
00152     if ( !KAddrBookExternal::addAddressee( addressee ) ) {
00153       KMessageBox::error( parent, i18n("Cannot save to addressbook.") );
00154       inserted = false;
00155     } else {
00156       QString text = i18n("The VCard was added to your addressbook; "
00157                           "you can add more information to this "
00158                           "entry by opening the addressbook.");
00159       KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00160       inserted = true;
00161     }
00162   } else {
00163     QString text = i18n("The VCard's primary email address is already in "
00164                         "your addressbook; however, you may save the VCard "
00165                         "into a file and import it into the addressbook "
00166                         "manually.");
00167     KMessageBox::information( parent, text );
00168     inserted = true;
00169   }
00170 
00171   return inserted;
00172 }
00173 
00174 bool KAddrBookExternal::addAddressee( const KABC::Addressee& addr )
00175 {
00176   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00177 
00178 #if KDE_IS_VERSION(3,4,89)
00179   // This ugly hack will be removed in 4.0
00180   while ( !addressBook->loadingHasFinished() ) {
00181     QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00182 
00183     // use sleep here to reduce cpu usage
00184     usleep( 100 );
00185   }
00186 #endif
00187 
00188   // Select a resource
00189   QPtrList<KABC::Resource> kabcResources = addressBook->resources();
00190 
00191   QPtrList<KRES::Resource> kresResources;
00192   QPtrListIterator<KABC::Resource> resIt( kabcResources );
00193   KABC::Resource *kabcResource;
00194   while ( ( kabcResource = resIt.current() ) != 0 ) {
00195     ++resIt;
00196     if ( !kabcResource->readOnly() ) {
00197       KRES::Resource *res = static_cast<KRES::Resource*>( kabcResource );
00198       if ( res )
00199         kresResources.append( res );
00200     }
00201   }
00202 
00203   kabcResource = static_cast<KABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) );
00204 
00205   KABC::Ticket *ticket = addressBook->requestSaveTicket( kabcResource );
00206   bool saved = false;
00207   if ( ticket ) {
00208     KABC::Addressee addressee( addr );
00209     addressee.setResource( kabcResource );
00210     addressBook->insertAddressee( addressee );
00211     saved = addressBook->save( ticket );
00212     if ( !saved )
00213       addressBook->releaseSaveTicket( ticket );
00214   }
00215 
00216   addressBook->emitAddressBookChanged();
00217 
00218   return saved;
00219 }
00220 
00221 QString KAddrBookExternal::expandDistributionList( const QString& listName )
00222 {
00223   if ( listName.isEmpty() )
00224     return QString::null;
00225 
00226   const QString lowerListName = listName.lower();
00227   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00228 #ifdef KDEPIM_NEW_DISTRLISTS
00229   KPIM::DistributionList distrList = KPIM::DistributionList::findByName( addressBook, lowerListName, false );
00230   if ( !distrList.isEmpty() ) {
00231     return distrList.emails( addressBook ).join( ", " );
00232   }
00233 #else
00234   KABC::DistributionListManager manager( addressBook );
00235   manager.load();
00236   const QStringList listNames = manager.listNames();
00237 
00238   for ( QStringList::ConstIterator it = listNames.begin();
00239         it != listNames.end(); ++it) {
00240     if ( (*it).lower() == lowerListName ) {
00241       const QStringList addressList = manager.list( *it )->emails();
00242       return addressList.join( ", " );
00243     }
00244   }
00245 #endif
00246   return QString::null;
00247 }
KDE Home | KDE Accessibility Home | Description of Access Keys