• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

libkdepim

kaddrbookexternal.cpp

Go to the documentation of this file.
00001 /*
00002   Simple Addressbook for KMail
00003   Copyright Stefan Taferner <taferner@kde.org>
00004 
00005   This program is free software; you can redistribute it and/or modify
00006   it under the terms of the GNU General Public License as published by
00007   the Free Software Foundation; either version 2 of the License, or
00008   (at your option) any later version.
00009 
00010   This program is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013   GNU General Public License for more details.
00014 
00015   You should have received a copy of the GNU General Public License along
00016   with this program; if not, write to the Free Software Foundation, Inc.,
00017   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kaddrbookexternal.h"
00021 #include "kaddressbookcore_interface.h"
00022 
00023 #ifdef KDEPIM_NEW_DISTRLISTS
00024 #include "distributionlist.h"
00025 #else
00026 #include <kabc/distributionlist.h>
00027 #endif
00028 #include <kabc/resource.h>
00029 #include <kabc/stdaddressbook.h>
00030 #include <kabc/vcardconverter.h>
00031 #include <kabc/errorhandler.h>
00032 #include <kresources/selectdialog.h>
00033 
00034 #include <kdefakes.h> // usleep
00035 #include <KDebug>
00036 #include <KLocale>
00037 #include <KMessageBox>
00038 #include <KToolInvocation>
00039 
00040 #include <QApplication>
00041 #include <QEventLoop>
00042 #include <QList>
00043 #include <QRegExp>
00044 #include <QtDBus/QDBusInterface>
00045 #include <QtDBus/QDBusConnection>
00046 
00047 #include <unistd.h>
00048 
00049 using namespace KPIM;
00050 
00051 //-----------------------------------------------------------------------------
00052 void KAddrBookExternal::openEmail( const QString &email, const QString &addr, QWidget *) {
00053   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00054   KABC::Addressee::List addresseeList = addressBook->findByEmail(email);
00055 
00056   // If KAddressbook is running, talk to it, otherwise start it.
00057   QDBusInterface abinterface( "org.kde.KAddressbook", "/kaddressbook_PimApplication",
00058                               "org.kde.KUniqueApplication" );
00059 
00060   if ( abinterface.isValid() ) {
00061     //make sure kaddressbook is loaded, otherwise showContactEditor
00062     //won't work as desired, see bug #87233
00063     abinterface.call("newInstance", QByteArray(), QByteArray());
00064   } else {
00065     KToolInvocation::startServiceByDesktopName( "kaddressbook" );
00066   }
00067 
00068   OrgKdeKAddressbookCoreInterface interface( "org.kde.KAddressbook",
00069                                              "/KAddressbook",
00070                                              QDBusConnection::sessionBus() );
00071   if( !addresseeList.isEmpty() ) {
00072     interface.showContactEditor( addresseeList.first().uid() );
00073   } else {
00074     interface.addEmail( addr );
00075   }
00076 }
00077 
00078 //-----------------------------------------------------------------------------
00079 void KAddrBookExternal::addEmail( const QString &addr, QWidget *parent )
00080 {
00081   QString email;
00082   QString name;
00083 
00084   KABC::Addressee::parseEmailAddress( addr, name, email );
00085   KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00086   ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );
00087 
00088   // force a reload of the address book file so that changes that were made
00089   // by other programs are loaded
00090   ab->asyncLoad();
00091 
00092   KABC::Addressee::List addressees = ab->findByEmail( email );
00093 
00094   if ( addressees.isEmpty() ) {
00095     KABC::Addressee a;
00096     a.setNameFromString( name );
00097     a.insertEmail( email, true );
00098 
00099     if ( KAddrBookExternal::addAddressee( a ) ) {
00100       QString text = i18n( "<qt>The email address <b>%1</b> was added to your "
00101                            "addressbook; you can add more information to this "
00102                            "entry by opening the addressbook.</qt>", addr );
00103       KMessageBox::information( parent, text, QString(), "addedtokabc" );
00104     }
00105   } else {
00106     QString text =
00107       i18n( "<qt>The email address <b>%1</b> is already in your addressbook.</qt>", addr );
00108     KMessageBox::information( parent, text, QString(), "alreadyInAddressBook" );
00109   }
00110   ab->setErrorHandler( 0 );
00111 }
00112 
00113 void KAddrBookExternal::openAddressBook( QWidget * )
00114 {
00115   KToolInvocation::startServiceByDesktopName( "kaddressbook" );
00116 }
00117 
00118 void KAddrBookExternal::addNewAddressee( QWidget * )
00119 {
00120   KToolInvocation::startServiceByDesktopName( "kaddressbook" );
00121   OrgKdeKAddressbookCoreInterface interface( "org.kde.KAddressbook",
00122                                              "/KAddressbook",
00123                                              QDBusConnection::sessionBus() );
00124   interface.newContact();
00125 }
00126 
00127 bool KAddrBookExternal::addVCard( const KABC::Addressee &addressee, QWidget *parent )
00128 {
00129   KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00130   bool inserted = false;
00131 
00132   ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );
00133 
00134   KABC::Addressee::List addressees =
00135     ab->findByEmail( addressee.preferredEmail() );
00136 
00137   if ( addressees.isEmpty() ) {
00138     if ( KAddrBookExternal::addAddressee( addressee ) ) {
00139       QString text = i18n( "The VCard was added to your addressbook; "
00140                            "you can add more information to this "
00141                            "entry by opening the addressbook." );
00142       KMessageBox::information( parent, text, QString(), "addedtokabc" );
00143       inserted = true;
00144     }
00145   } else {
00146     QString text = i18n( "The VCard's primary email address is already in "
00147                          "your addressbook; however, you may save the VCard "
00148                          "into a file and import it into the addressbook manually." );
00149     KMessageBox::information( parent, text );
00150     inserted = true;
00151   }
00152 
00153   ab->setErrorHandler( 0 );
00154   return inserted;
00155 }
00156 
00157 bool KAddrBookExternal::addAddressee( const KABC::Addressee &addr )
00158 {
00159   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00160 
00161   // PORT. FIXME: This ugly hack will be removed in 4.0
00162   while ( !addressBook->loadingHasFinished() ) {
00163     qApp->processEvents( QEventLoop::ExcludeUserInputEvents );
00164 
00165     // use sleep here to reduce cpu usage
00166     usleep( 100 );
00167   }
00168 
00169   // Select a resource
00170   QList<KABC::Resource*> kabcResources = addressBook->resources();
00171   QList<KRES::Resource*> kresResources;
00172   QListIterator<KABC::Resource*> resIt( kabcResources );
00173   KABC::Resource *kabcResource;
00174   while ( resIt.hasNext() ) {
00175     kabcResource = resIt.next();
00176     if ( !kabcResource->readOnly() ) {
00177       KRES::Resource *res = static_cast<KRES::Resource*>( kabcResource );
00178       if ( res ) {
00179         kresResources.append( res );
00180       }
00181     }
00182   }
00183 
00184   kabcResource =
00185     static_cast<KABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) );
00186 
00187   KABC::Ticket *ticket = addressBook->requestSaveTicket( kabcResource );
00188   bool saved = false;
00189   if ( ticket ) {
00190     KABC::Addressee addressee( addr );
00191     addressee.setResource( kabcResource );
00192     addressBook->insertAddressee( addressee );
00193     saved = addressBook->save( ticket );
00194     if ( !saved ) {
00195       addressBook->releaseSaveTicket( ticket );
00196     }
00197   }
00198 
00199   addressBook->emitAddressBookChanged();
00200 
00201   return saved;
00202 }
00203 
00204 QString KAddrBookExternal::expandDistributionList( const QString &listName )
00205 {
00206   if ( listName.isEmpty() ) {
00207     return QString();
00208   }
00209 
00210   const QString lowerListName = listName.toLower();
00211   KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00212 #ifdef KDEPIM_NEW_DISTRLISTS
00213   KPIM::DistributionList distrList =
00214     KPIM::DistributionList::findByName( addressBook, lowerListName, false );
00215   if ( !distrList.isEmpty() ) {
00216     return distrList.emails( addressBook ).join( ", " );
00217   }
00218 #else
00219   KABC::DistributionList* list = addressBook->findDistributionListByName( listName, Qt::CaseInsensitive );
00220 
00221   if ( list ) {
00222     return list->emails().join( ", " );
00223   }
00224 #endif
00225   return QString();
00226 }

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal