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 &email, const QString &addr, QWidget *) {
00035   //QString email = KMMessage::getEmailAddr(addr);
00036   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00037   KABC::Addressee::List addresseeList = addressBook->findByEmail(email);
00038   if ( kapp->dcopClient()->isApplicationRegistered( "kaddressbook" ) ){
00039     //make sure kaddressbook is loaded, otherwise showContactEditor
00040     //won't work as desired, see bug #87233
00041     DCOPRef call ( "kaddressbook", "kaddressbook" );
00042     call.send( "newInstance()" );
00043   }
00044   else
00045     kapp->startServiceByDesktopName( "kaddressbook" );
00046 
00047   DCOPRef call( "kaddressbook", "KAddressBookIface" );
00048   if( !addresseeList.isEmpty() ) {
00049     call.send( "showContactEditor(QString)", addresseeList.first().uid() );
00050   }
00051   else {
00052     call.send( "addEmail(QString)", addr );
00053   }
00054 }
00055 
00056 //-----------------------------------------------------------------------------
00057 void KAddrBookExternal::addEmail( const QString& addr, QWidget *parent) {
00058   QString email;
00059   QString name;
00060 
00061   KABC::Addressee::parseEmailAddress( addr, name, email );
00062 
00063   KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00064 
00065   // force a reload of the address book file so that changes that were made
00066   // by other programs are loaded
00067   ab->asyncLoad();
00068 
00069   // if we have to reload the address book then we should also wait until
00070   // it's completely reloaded
00071 #if KDE_IS_VERSION(3,4,89)
00072   // This ugly hack will be removed in 4.0
00073   while ( !ab->loadingHasFinished() ) {
00074     QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00075 
00076     // use sleep here to reduce cpu usage
00077     usleep( 100 );
00078   }
00079 #endif
00080 
00081   KABC::Addressee::List addressees = ab->findByEmail( email );
00082 
00083   if ( addressees.isEmpty() ) {
00084     KABC::Addressee a;
00085     a.setNameFromString( name );
00086     a.insertEmail( email, true );
00087 
00088     if ( !KAddrBookExternal::addAddressee( a ) ) {
00089       KMessageBox::error( parent, i18n("Cannot save to addressbook.") );
00090     } else {
00091       QString text = i18n("<qt>The email address <b>%1</b> was added to your "
00092                           "addressbook; you can add more information to this "
00093                           "entry by opening the addressbook.</qt>").arg( addr );
00094       KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00095     }
00096   } else {
00097     QString text = i18n("<qt>The email address <b>%1</b> is already in your "
00098                         "addressbook.</qt>").arg( addr );
00099     KMessageBox::information( parent, text, QString::null,
00100                               "alreadyInAddressBook" );
00101   }
00102 }
00103 
00104 void KAddrBookExternal::openAddressBook(QWidget *) {
00105   kapp->startServiceByDesktopName( "kaddressbook" );
00106 }
00107 
00108 void KAddrBookExternal::addNewAddressee( QWidget* )
00109 {
00110   kapp->startServiceByDesktopName("kaddressbook");
00111   DCOPRef call("kaddressbook", "KAddressBookIface");
00112   call.send("newContact()");
00113 }
00114 
00115 bool KAddrBookExternal::addVCard( const KABC::Addressee& addressee, QWidget *parent )
00116 {
00117   KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00118   bool inserted = false;
00119 
00120   KABC::Addressee::List addressees =
00121       ab->findByEmail( addressee.preferredEmail() );
00122 
00123   if ( addressees.isEmpty() ) {
00124     if ( !KAddrBookExternal::addAddressee( addressee ) ) {
00125       KMessageBox::error( parent, i18n("Cannot save to addressbook.") );
00126       inserted = false;
00127     } else {
00128       QString text = i18n("The VCard was added to your addressbook; "
00129                           "you can add more information to this "
00130                           "entry by opening the addressbook.");
00131       KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00132       inserted = true;
00133     }
00134   } else {
00135     QString text = i18n("The VCard's primary email address is already in "
00136                         "your addressbook; however, you may save the VCard "
00137                         "into a file and import it into the addressbook "
00138                         "manually.");
00139     KMessageBox::information( parent, text );
00140     inserted = true;
00141   }
00142 
00143   return inserted;
00144 }
00145 
00146 bool KAddrBookExternal::addAddressee( const KABC::Addressee& addr )
00147 {
00148   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00149 
00150 #if KDE_IS_VERSION(3,4,89)
00151   // This ugly hack will be removed in 4.0
00152   while ( !addressBook->loadingHasFinished() ) {
00153     QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00154 
00155     // use sleep here to reduce cpu usage
00156     usleep( 100 );
00157   }
00158 #endif
00159 
00160   // Select a resource
00161   QPtrList<KABC::Resource> kabcResources = addressBook->resources();
00162 
00163   QPtrList<KRES::Resource> kresResources;
00164   QPtrListIterator<KABC::Resource> resIt( kabcResources );
00165   KABC::Resource *kabcResource;
00166   while ( ( kabcResource = resIt.current() ) != 0 ) {
00167     ++resIt;
00168     if ( !kabcResource->readOnly() ) {
00169       KRES::Resource *res = static_cast<KRES::Resource*>( kabcResource );
00170       if ( res )
00171         kresResources.append( res );
00172     }
00173   }
00174 
00175   kabcResource = static_cast<KABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) );
00176 
00177   KABC::Ticket *ticket = addressBook->requestSaveTicket( kabcResource );
00178   bool saved = false;
00179   if ( ticket ) {
00180     KABC::Addressee addressee( addr );
00181     addressee.setResource( kabcResource );
00182     addressBook->insertAddressee( addressee );
00183     saved = addressBook->save( ticket );
00184     if ( !saved )
00185       addressBook->releaseSaveTicket( ticket );
00186   }
00187 
00188   addressBook->emitAddressBookChanged();
00189 
00190   return saved;
00191 }
00192 
00193 QString KAddrBookExternal::expandDistributionList( const QString& listName )
00194 {
00195   if ( listName.isEmpty() )
00196     return QString::null;
00197 
00198   const QString lowerListName = listName.lower();
00199   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00200 #ifdef KDEPIM_NEW_DISTRLISTS
00201   KPIM::DistributionList distrList = KPIM::DistributionList::findByName( addressBook, lowerListName, false );
00202   if ( !distrList.isEmpty() ) {
00203     return distrList.emails( addressBook ).join( ", " );
00204   }
00205 #else
00206   KABC::DistributionListManager manager( addressBook );
00207   manager.load();
00208   const QStringList listNames = manager.listNames();
00209 
00210   for ( QStringList::ConstIterator it = listNames.begin();
00211         it != listNames.end(); ++it) {
00212     if ( (*it).lower() == lowerListName ) {
00213       const QStringList addressList = manager.list( *it )->emails();
00214       return addressList.join( ", " );
00215     }
00216   }
00217 #endif
00218   return QString::null;
00219 }
KDE Home | KDE Accessibility Home | Description of Access Keys