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 *parent )
00166 {
00167 QString errStr;
00168 if ( kapp->startServiceByDesktopName( "kaddressbook", QString(), &errStr ) > 0 ) {
00169 QString txt;
00170 if ( !errStr.isEmpty() ) {
00171 txt = i18n( "<qt>Unable to open kaddressbook.<p>Error: \"%1\"</qt>" ).arg( errStr );
00172 } else {
00173 txt = i18n( "<qt>Unable to open kaddressbook.<p>Error: unknown.</qt>" );
00174 }
00175 KMessageBox::sorry( parent, txt );
00176 }
00177 }
00178
00179 void KAddrBookExternal::addNewAddressee( QWidget* )
00180 {
00181 kapp->startServiceByDesktopName("kaddressbook");
00182 DCOPRef call("kaddressbook", "KAddressBookIface");
00183 call.send("newContact()");
00184 }
00185
00186 bool KAddrBookExternal::addVCard( const KABC::Addressee& addressee, QWidget *parent )
00187 {
00188 KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00189 bool inserted = false;
00190
00191 ab->setErrorHandler( new KABC::GuiErrorHandler( parent ) );
00192
00193 KABC::Addressee::List addressees =
00194 ab->findByEmail( addressee.preferredEmail() );
00195
00196 if ( addressees.isEmpty() ) {
00197 if ( KAddrBookExternal::addAddressee( addressee ) ) {
00198 QString text = i18n("The VCard was added to your addressbook; "
00199 "you can add more information to this "
00200 "entry by opening the addressbook.");
00201 KMessageBox::information( parent, text, QString::null, "addedtokabc" );
00202 inserted = true;
00203 }
00204 } else {
00205 QString text = i18n("The VCard's primary email address is already in "
00206 "your addressbook; however, you may save the VCard "
00207 "into a file and import it into the addressbook "
00208 "manually.");
00209 KMessageBox::information( parent, text );
00210 inserted = true;
00211 }
00212
00213 ab->setErrorHandler( 0 );
00214 return inserted;
00215 }
00216
00217 bool KAddrBookExternal::addAddressee( const KABC::Addressee& addr )
00218 {
00219 KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00220 KABC::Resource *kabcResource = selectResourceForSaving( addressBook );
00221 if( !kabcResource )
00222 return false;
00223 KABC::Ticket *ticket = addressBook->requestSaveTicket( kabcResource );
00224 bool saved = false;
00225 if ( ticket ) {
00226 KABC::Addressee addressee( addr );
00227 addressee.setResource( kabcResource );
00228 addressBook->insertAddressee( addressee );
00229 saved = addressBook->save( ticket );
00230 if ( !saved )
00231 addressBook->releaseSaveTicket( ticket );
00232 }
00233
00234 addressBook->emitAddressBookChanged();
00235
00236 return saved;
00237 }
00238
00239 QString KAddrBookExternal::expandDistributionList( const QString& listName )
00240 {
00241 if ( listName.isEmpty() )
00242 return QString::null;
00243
00244 const QString lowerListName = listName.lower();
00245 KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00246 #ifdef KDEPIM_NEW_DISTRLISTS
00247 KPIM::DistributionList distrList = KPIM::DistributionList::findByName( addressBook, lowerListName, false );
00248 if ( !distrList.isEmpty() ) {
00249 return distrList.emails( addressBook ).join( ", " );
00250 }
00251 #else
00252 KABC::DistributionListManager manager( addressBook );
00253 manager.load();
00254 const QStringList listNames = manager.listNames();
00255
00256 for ( QStringList::ConstIterator it = listNames.begin();
00257 it != listNames.end(); ++it) {
00258 if ( (*it).lower() == lowerListName ) {
00259 const QStringList addressList = manager.list( *it )->emails();
00260 return addressList.join( ", " );
00261 }
00262 }
00263 #endif
00264 return QString::null;
00265 }
00266
00267 KABC::Resource* KAddrBookExternal::selectResourceForSaving( KABC::AddressBook *addressBook )
00268 {
00269 #if KDE_IS_VERSION(3,4,89)
00270
00271 while ( !addressBook->loadingHasFinished() ) {
00272 QApplication::eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00273
00274
00275 usleep( 100 );
00276 }
00277 #endif
00278
00279
00280 QPtrList<KABC::Resource> kabcResources = addressBook->resources();
00281
00282 QPtrList<KRES::Resource> kresResources;
00283 QPtrListIterator<KABC::Resource> resIt( kabcResources );
00284 KABC::Resource *kabcResource;
00285 while ( ( kabcResource = resIt.current() ) != 0 ) {
00286 ++resIt;
00287 if ( !kabcResource->readOnly() ) {
00288 KRES::Resource *res = static_cast<KRES::Resource*>( kabcResource );
00289 if ( res )
00290 kresResources.append( res );
00291 }
00292 }
00293
00294 return static_cast<KABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) );
00295 }