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