libkdenetwork Library API Documentation

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 #include <unistd.h>
00008 
00009 #include "kaddrbook.h"
00010 
00011 #include <kapplication.h>
00012 #include <kdebug.h>
00013 #include <klocale.h>
00014 #include <kmessagebox.h>
00015 #include <kabc/stdaddressbook.h>
00016 #include <kabc/distributionlist.h>
00017 #include <kabc/vcardconverter.h>
00018 #include <dcopref.h>
00019 #include <dcopclient.h> 
00020 
00021 #include <qregexp.h>
00022 
00023 //-----------------------------------------------------------------------------
00024 void KAddrBookExternal::openEmail( const QString &email, const QString &addr, QWidget *) {
00025   //QString email = KMMessage::getEmailAddr(addr);
00026   KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00027   KABC::Addressee::List addresseeList = addressBook->findByEmail(email);
00028   if ( kapp->dcopClient()->isApplicationRegistered( "kaddressbook" ) ){
00029     //make sure kaddressbook is loaded, otherwise showContactEditor
00030     //won't work as desired, see bug #87233
00031     DCOPRef call ( "kaddressbook", "kaddressbook" );
00032     call.send( "newInstance()" );
00033   }
00034   else
00035     kapp->startServiceByDesktopName( "kaddressbook" );
00036     
00037   DCOPRef call( "kaddressbook", "KAddressBookIface" );
00038   if( !addresseeList.isEmpty() ) {
00039     call.send( "showContactEditor(QString)", addresseeList.first().uid() );
00040   }
00041   else {
00042     call.send( "addEmail(QString)", addr );
00043   }
00044 }
00045 
00046 //-----------------------------------------------------------------------------
00047 void KAddrBookExternal::addEmail( const QString& addr, QWidget *parent) {
00048   QString email;
00049   QString name;
00050 
00051   KABC::Addressee::parseEmailAddress( addr, name, email );
00052 
00053   KABC::AddressBook *ab = KABC::StdAddressBook::self();
00054 
00055   // force a reload of the address book file so that changes that were made
00056   // by other programs are loaded
00057   ab->load();
00058 
00059   KABC::Addressee::List addressees = ab->findByEmail( email );
00060 
00061   if ( addressees.isEmpty() ) {
00062     KABC::Addressee a;
00063     a.setNameFromString( name );
00064     a.insertEmail( email, true );
00065 
00066     if ( !KAddrBookExternal::addAddressee( a ) ) {
00067       KMessageBox::error( parent, i18n("Cannot save to addressbook.") );
00068     } else {
00069       QString text = i18n("<qt>The email address <b>%1</b> was added to your "
00070                           "addressbook; you can add more information to this "
00071                           "entry by opening the addressbook.</qt>").arg( addr );
00072       KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00073     }
00074   } else {
00075     QString text = i18n("<qt>The email address <b>%1</b> is already in your "
00076                         "addressbook.</qt>").arg( addr );
00077     KMessageBox::information( parent, text, QString::null,
00078                               "alreadyInAddressBook" );
00079   }
00080 }
00081 
00082 void KAddrBookExternal::openAddressBook(QWidget *) {
00083   kapp->startServiceByDesktopName( "kaddressbook" );
00084 }
00085 
00086 void KAddrBookExternal::addNewAddressee( QWidget* )
00087 {
00088   kapp->startServiceByDesktopName("kaddressbook");
00089   sleep(2);
00090   DCOPRef call("kaddressbook", "KAddressBookIface");
00091   call.send("newContact()");
00092 }
00093 
00094 bool KAddrBookExternal::addVCard( const KABC::Addressee& addressee, QWidget *parent )
00095 {
00096   KABC::AddressBook *ab = KABC::StdAddressBook::self();
00097   bool inserted = false;
00098 
00099   KABC::Addressee::List addressees =
00100       ab->findByEmail( addressee.preferredEmail() );
00101 
00102   if ( addressees.isEmpty() ) {
00103     if ( !KAddrBookExternal::addAddressee( addressee ) ) {
00104       KMessageBox::error( parent, i18n("Cannot save to addressbook.") );
00105       inserted = false;
00106     } else {
00107       QString text = i18n("The VCard was added to your addressbook; "
00108                           "you can add more information to this "
00109                           "entry by opening the addressbook.");
00110       KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00111       inserted = true;
00112     }
00113   } else {
00114     QString text = i18n("The VCard's primary email address is already in "
00115                         "your addressbook; however, you may save the VCard "
00116                         "into a file and import it into the addressbook "
00117                         "manually.");
00118     KMessageBox::information( parent, text );
00119     inserted = true;
00120   }
00121 
00122   return inserted;
00123 }
00124 
00125 bool KAddrBookExternal::addAddressee( const KABC::Addressee& addressee )
00126 {
00127   KABC::AddressBook *ab = KABC::StdAddressBook::self();
00128   KABC::Ticket *t = ab->requestSaveTicket();
00129   bool saved = false;
00130   if ( t ) {
00131     ab->insertAddressee( addressee );
00132     saved = ab->save( t );
00133     if ( !saved )
00134       ab->releaseSaveTicket( t );
00135   }
00136   return saved;
00137 }
00138 
00139 QString KAddrBookExternal::expandDistributionList( const QString& listName )
00140 {
00141   if ( listName.isEmpty() )
00142     return QString::null;
00143 
00144   const QString lowerListName = listName.lower();
00145   KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00146   KABC::DistributionListManager manager( addressBook );
00147   manager.load();
00148   const QStringList listNames = manager.listNames();
00149 
00150   for ( QStringList::ConstIterator it = listNames.begin();
00151         it != listNames.end(); ++it) {
00152     if ( (*it).lower() == lowerListName ) {
00153       const QStringList addressList = manager.list( *it )->emails();
00154       return addressList.join( ", " );
00155     }
00156   }
00157   return QString::null;
00158 }
KDE Logo
This file is part of the documentation for libkdenetwork Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 4 14:39:09 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003