kmaddrbook.cpp
00001
00002
00003
00004
00005
00006 #include <config.h>
00007 #include <unistd.h>
00008
00009 #include "kmaddrbook.h"
00010 #include "kcursorsaver.h"
00011 #include "kmmessage.h"
00012 #include "kmkernel.h"
00013
00014 #include <kapplication.h>
00015 #include <kdebug.h>
00016 #include <klocale.h>
00017 #include <kmessagebox.h>
00018 #include <kabc/stdaddressbook.h>
00019 #include <kabc/distributionlist.h>
00020 #include <kabc/vcardconverter.h>
00021 #include <dcopref.h>
00022 #if !KDE_IS_VERSION( 3, 1, 92 )
00023 #include <kstandarddirs.h>
00024 #include <kprocess.h>
00025 #include <krun.h>
00026 #endif
00027
00028 #include <qregexp.h>
00029
00030 void KabcBridge::addresses(QStringList& result)
00031 {
00032 KCursorSaver busy(KBusyPtr::busy());
00033
00034 KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00035 KABC::AddressBook::Iterator it;
00036 for( it = addressBook->begin(); it != addressBook->end(); ++it ) {
00037 QStringList emails = (*it).emails();
00038 QString n = (*it).prefix() + " " +
00039 (*it).givenName() + " " +
00040 (*it).additionalName() + " " +
00041 (*it).familyName() + " " +
00042 (*it).suffix();
00043 n = n.simplifyWhiteSpace();
00044
00045 QRegExp needQuotes("[^ 0-9A-Za-z\\x0080-\\xFFFF]");
00046 QString endQuote = "\" ";
00047 QStringList::ConstIterator mit;
00048 QString addr, email;
00049
00050 for ( mit = emails.begin(); mit != emails.end(); ++mit ) {
00051 email = *mit;
00052 if (!email.isEmpty()) {
00053 if (n.isEmpty() || (email.find( '<' ) != -1))
00054 addr = QString::null;
00055 else {
00056 if (n.find(needQuotes) != -1)
00057 addr = '"' + n + endQuote;
00058 else
00059 addr = n + ' ';
00060 }
00061
00062 if (!addr.isEmpty() && (email.find( '<' ) == -1)
00063 && (email.find( '>' ) == -1)
00064 && (email.find( ',' ) == -1))
00065 addr += '<' + email + '>';
00066 else
00067 addr += email;
00068 addr = addr.stripWhiteSpace();
00069 result.append( addr );
00070 }
00071 }
00072 }
00073 KABC::DistributionListManager manager( addressBook );
00074 manager.load();
00075
00076 QStringList names = manager.listNames();
00077 QStringList::Iterator jt;
00078 for ( jt = names.begin(); jt != names.end(); ++jt)
00079 result.append( *jt );
00080 result.sort();
00081 }
00082
00083 QStringList KabcBridge::addresses()
00084 {
00085 QStringList entries;
00086 KABC::AddressBook::ConstIterator it;
00087
00088 KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00089 for( it = addressBook->begin(); it != addressBook->end(); ++it ) {
00090 entries += (*it).fullEmail();
00091 }
00092 return entries;
00093 }
00094
00095
00096 QString KabcBridge::expandNickName( const QString& nickName )
00097 {
00098 if ( nickName.isEmpty() )
00099 return QString();
00100
00101 QString lowerNickName = nickName.lower();
00102 KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00103 for( KABC::AddressBook::ConstIterator it = addressBook->begin();
00104 it != addressBook->end(); ++it ) {
00105 if ( (*it).nickName().lower() == lowerNickName )
00106 return (*it).fullEmail();
00107 }
00108 return QString();
00109 }
00110
00111
00112 QString KabcBridge::expandDistributionList( const QString& listName )
00113 {
00114 if ( listName.isEmpty() )
00115 return QString();
00116
00117 QString lowerListName = listName.lower();
00118 KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00119 KABC::DistributionListManager manager( addressBook );
00120 manager.load();
00121 QStringList listNames = manager.listNames();
00122
00123 for ( QStringList::Iterator it = listNames.begin();
00124 it != listNames.end(); ++it) {
00125 if ( (*it).lower() == lowerListName ) {
00126 QStringList addressList = manager.list( *it )->emails();
00127 return addressList.join( ", " );
00128 }
00129 }
00130 return QString();
00131 }
00132
00133
00134 void KMAddrBookExternal::openEmail( const QString &addr, QWidget *) {
00135 #if KDE_IS_VERSION( 3, 1, 92 )
00136 QString email = KMMessage::getEmailAddr(addr);
00137 KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00138 KABC::Addressee::List addresseeList = addressBook->findByEmail(email);
00139 kapp->startServiceByDesktopName( "kaddressbook" );
00140 DCOPRef call( "kaddressbook", "KAddressBookIface" );
00141 if( !addresseeList.isEmpty() ) {
00142 call.send( "showContactEditor(QString)", addresseeList.first().uid() );
00143 }
00144 else {
00145 call.send( "addEmail(QString)", addr );
00146 }
00147 #else
00148 if ( checkForAddressBook() ) {
00149 KRun::runCommand( "kaddressbook -a " + KProcess::quote(addr) );
00150 }
00151 #endif
00152 }
00153
00154
00155 void KMAddrBookExternal::addEmail( const QString& addr, QWidget *parent) {
00156 QString email;
00157 QString name;
00158
00159 KABC::Addressee::parseEmailAddress( addr, name, email );
00160
00161 KABC::AddressBook *ab = KABC::StdAddressBook::self();
00162
00163
00164
00165 ab->load();
00166
00167 KABC::Addressee::List addressees = ab->findByEmail( email );
00168
00169 if ( addressees.isEmpty() ) {
00170 KABC::Addressee a;
00171 a.setNameFromString( name );
00172 a.insertEmail( email, true );
00173
00174 KABC::Ticket *t = ab->requestSaveTicket();
00175 bool saved = false;
00176 if ( t ) {
00177 ab->insertAddressee(a);
00178 saved = ab->save( t );
00179 if ( !saved )
00180 ab->releaseSaveTicket( t );
00181 }
00182 if ( !saved ) {
00183 KMessageBox::error( parent, i18n("Can't save to addressbook.") );
00184 } else {
00185 QString text = i18n("<qt>The email address <b>%1</b> was added to your "
00186 "addressbook. You can add more information to this "
00187 "entry by opening the addressbook.</qt>").arg( addr );
00188 KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00189 }
00190 } else {
00191 QString text = i18n("<qt>The email address <b>%1</b> is already in your "
00192 "addressbook.</qt>").arg( addr );
00193 KMessageBox::information( parent, text );
00194 }
00195 }
00196
00197 void KMAddrBookExternal::openAddressBook(QWidget *) {
00198 #if KDE_IS_VERSION( 3, 1, 92 )
00199 kapp->startServiceByDesktopName( "kaddressbook" );
00200 #else
00201 if ( checkForAddressBook() ) {
00202 KRun::runCommand( "kaddressbook" );
00203 }
00204 #endif
00205 }
00206
00207 #if !KDE_IS_VERSION( 3, 1, 92 )
00208 bool KMAddrBookExternal::checkForAddressBook()
00209 {
00210 if ( KStandardDirs::findExe( "kaddressbook" ).isEmpty() ) {
00211 KMessageBox::information( 0,
00212 i18n("No external address book application found. You might want to "
00213 "install KAddressBook from the kdepim module.") );
00214 return false;
00215 } else {
00216 return true;
00217 }
00218 }
00219 #endif
00220
00221 void KMAddrBookExternal::addNewAddressee( QWidget* )
00222 {
00223 #if KDE_IS_VERSION( 3, 1, 92 )
00224 kapp->startServiceByDesktopName("kaddressbook");
00225 sleep(2);
00226 DCOPRef call("kaddressbook", "KAddressBookIface");
00227 call.send("newContact()");
00228 #else
00229 if ( checkForAddressBook() ) {
00230 KRun::runCommand( "kaddressbook --editor-only --new-contact" );
00231 }
00232 #endif
00233 }
00234
00235 bool KMAddrBookExternal::addVCard( const KABC::Addressee& addressee, QWidget *parent )
00236 {
00237 KABC::AddressBook *ab = KABC::StdAddressBook::self();
00238 bool inserted = false;
00239
00240 KABC::Addressee::List addressees =
00241 ab->findByEmail( addressee.preferredEmail() );
00242
00243 if ( addressees.isEmpty() ) {
00244 KABC::Ticket *t = ab->requestSaveTicket();
00245 bool saved = false;
00246 if ( t ) {
00247 ab->insertAddressee( addressee );
00248 saved = ab->save( t );
00249 if ( !saved )
00250 ab->releaseSaveTicket( t );
00251 }
00252 if ( !saved ) {
00253 KMessageBox::error( parent, i18n("Can't save to addressbook.") );
00254 inserted = false;
00255 } else {
00256 QString text = i18n("The VCard was added to your addressbook. "
00257 "You can add more information to this "
00258 "entry by opening the addressbook.");
00259 KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00260 inserted = true;
00261 }
00262 } else {
00263 QString text = i18n("The VCard's primary email address is already in "
00264 "your addressbook. However you may save the VCard "
00265 "into a file and import it into the addressbook "
00266 "manually.");
00267 KMessageBox::information( parent, text );
00268 inserted = true;
00269 }
00270
00271 return inserted;
00272 }
This file is part of the documentation for kmail Library Version 3.2.2.