00001
00002
00003
00004
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 <kabc/errorhandler.h>
00025 #include <kresources/selectdialog.h>
00026 #include <dcopref.h>
00027 #include <dcopclient.h>
00028
00029 #include <qeventloop.h>
00030 #include <qregexp.h>
00031
00032 #include <unistd.h>
00033
00034
00035 void KAddrBookExternal::openEmail( const QString &addr, QWidget *parent ) {
00036 QString email;
00037 QString name;
00038
00039 KABC::Addressee::parseEmailAddress( addr, name, email );
00040
00041 KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00042
00043
00044
00045 ab->asyncLoad();
00046
00047
00048
00049 #if KDE_IS_VERSION(3,4,89)
00050
00051 while ( !ab->loadingHasFinished() ) {
00052 QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00053
00054
00055 usleep( 100 );
00056 }
00057 #endif
00058
00059 KABC::Addressee::List addressees = ab->findByEmail( email );
00060
00061 if ( addressees.count() > 0 ) {
00062 if ( kapp->dcopClient()->isApplicationRegistered( "kaddressbook" ) ){
00063
00064
00065 DCOPRef call ( "kaddressbook", "kaddressbook" );
00066 call.send( "newInstance()" );
00067 } else {
00068 kapp->startServiceByDesktopName( "kaddressbook" );
00069 }
00070
00071 DCOPRef call( "kaddressbook", "KAddressBookIface" );
00072 call.send( "showContactEditor(QString)", addressees.first().uid() );
00073 } else {
00074
00075 #if 0
00076 QString text = i18n("<qt>The email address <b>%1</b> cannot be "
00077 "found in your addressbook.</qt>").arg( email );
00078 #else
00079 QString text = email + " " + i18n( "is not in address book" );
00080 #endif
00081 KMessageBox::information( parent, text, QString::null, "notInAddressBook" );
00082 }
00083 }
00084
00085
00086 void KAddrBookExternal::addEmail( const QString& addr, QWidget *parent) {
00087 QString email;
00088 QString name;
00089
00090 KABC::Addressee::parseEmailAddress( addr, name, email );
00091
00092 KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00093
00094 ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );
00095
00096
00097
00098 ab->asyncLoad();
00099
00100
00101
00102 #if KDE_IS_VERSION(3,4,89)
00103
00104 while ( !ab->loadingHasFinished() ) {
00105 QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00106
00107
00108 usleep( 100 );
00109 }
00110 #endif
00111
00112 KABC::Addressee::List addressees = ab->findByEmail( email );
00113
00114 if ( addressees.isEmpty() ) {
00115 KABC::Addressee a;
00116 a.setNameFromString( name );
00117 a.insertEmail( email, true );
00118
00119 {
00120 KConfig config( "kaddressbookrc" );
00121 config.setGroup( "General" );
00122 int type = config.readNumEntry( "FormattedNameType", 1 );
00123
00124 QString name;
00125 switch ( type ) {
00126 case 1:
00127 name = a.givenName() + " " + a.familyName();
00128 break;
00129 case 2:
00130 name = a.assembledName();
00131 break;
00132 case 3:
00133 name = a.familyName() + ", " + a.givenName();
00134 break;
00135 case 4:
00136 name = a.familyName() + " " + a.givenName();
00137 break;
00138 case 5:
00139 name = a.organization();
00140 break;
00141 default:
00142 name = "";
00143 break;
00144 }
00145 name.simplifyWhiteSpace();
00146
00147 a.setFormattedName( name );
00148 }
00149
00150 if ( KAddrBookExternal::addAddressee( a ) ) {
00151 QString text = i18n("<qt>The email address <b>%1</b> was added to your "
00152 "addressbook; you can add more information to this "
00153 "entry by opening the addressbook.</qt>").arg( addr );
00154 KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00155 }
00156 } else {
00157 QString text = i18n("<qt>The email address <b>%1</b> is already in your "
00158 "addressbook.</qt>").arg( addr );
00159 KMessageBox::information( parent, text, QString::null,
00160 "alreadyInAddressBook" );
00161 }
00162 ab->setErrorHandler( 0 );
00163 }
00164
00165 void KAddrBookExternal::openAddressBook(QWidget *) {
00166 kapp->startServiceByDesktopName( "kaddressbook" );
00167 }
00168
00169 void KAddrBookExternal::addNewAddressee( QWidget* )
00170 {
00171 kapp->startServiceByDesktopName("kaddressbook");
00172 DCOPRef call("kaddressbook", "KAddressBookIface");
00173 call.send("newContact()");
00174 }
00175
00176 bool KAddrBookExternal::addVCard( const KABC::Addressee& addressee, QWidget *parent )
00177 {
00178 KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00179 bool inserted = false;
00180
00181 ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );
00182
00183 KABC::Addressee::List addressees =
00184 ab->findByEmail( addressee.preferredEmail() );
00185
00186 if ( addressees.isEmpty() ) {
00187 if ( KAddrBookExternal::addAddressee( addressee ) ) {
00188 QString text = i18n("The VCard was added to your addressbook; "
00189 "you can add more information to this "
00190 "entry by opening the addressbook.");
00191 KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00192 inserted = true;
00193 }
00194 } else {
00195 QString text = i18n("The VCard's primary email address is already in "
00196 "your addressbook; however, you may save the VCard "
00197 "into a file and import it into the addressbook "
00198 "manually.");
00199 KMessageBox::information( parent, text );
00200 inserted = true;
00201 }
00202
00203 ab->setErrorHandler( 0 );
00204 return inserted;
00205 }
00206
00207 bool KAddrBookExternal::addAddressee( const KABC::Addressee& addr )
00208 {
00209 KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00210
00211 #if KDE_IS_VERSION(3,4,89)
00212
00213 while ( !addressBook->loadingHasFinished() ) {
00214 QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00215
00216
00217 usleep( 100 );
00218 }
00219 #endif
00220
00221
00222 QPtrList<KABC::Resource> kabcResources = addressBook->resources();
00223
00224 QPtrList<KRES::Resource> kresResources;
00225 QPtrListIterator<KABC::Resource> resIt( kabcResources );
00226 KABC::Resource *kabcResource;
00227 while ( ( kabcResource = resIt.current() ) != 0 ) {
00228 ++resIt;
00229 if ( !kabcResource->readOnly() ) {
00230 KRES::Resource *res = static_cast<KRES::Resource*>( kabcResource );
00231 if ( res )
00232 kresResources.append( res );
00233 }
00234 }
00235
00236 kabcResource = static_cast<KABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) );
00237 if( !kabcResource )
00238 return false;
00239 KABC::Ticket *ticket = addressBook->requestSaveTicket( kabcResource );
00240 bool saved = false;
00241 if ( ticket ) {
00242 KABC::Addressee addressee( addr );
00243 addressee.setResource( kabcResource );
00244 addressBook->insertAddressee( addressee );
00245 saved = addressBook->save( ticket );
00246 if ( !saved )
00247 addressBook->releaseSaveTicket( ticket );
00248 }
00249
00250 addressBook->emitAddressBookChanged();
00251
00252 return saved;
00253 }
00254
00255 QString KAddrBookExternal::expandDistributionList( const QString& listName )
00256 {
00257 if ( listName.isEmpty() )
00258 return QString::null;
00259
00260 const QString lowerListName = listName.lower();
00261 KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00262 #ifdef KDEPIM_NEW_DISTRLISTS
00263 KPIM::DistributionList distrList = KPIM::DistributionList::findByName( addressBook, lowerListName, false );
00264 if ( !distrList.isEmpty() ) {
00265 return distrList.emails( addressBook ).join( ", " );
00266 }
00267 #else
00268 KABC::DistributionListManager manager( addressBook );
00269 manager.load();
00270 const QStringList listNames = manager.listNames();
00271
00272 for ( QStringList::ConstIterator it = listNames.begin();
00273 it != listNames.end(); ++it) {
00274 if ( (*it).lower() == lowerListName ) {
00275 const QStringList addressList = manager.list( *it )->emails();
00276 return addressList.join( ", " );
00277 }
00278 }
00279 #endif
00280 return QString::null;
00281 }