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 <tqeventloop.h>
00030 #include <tqregexp.h>
00031
00032 #include <unistd.h>
00033
00034
00035 void KAddrBookExternal::openEmail( const TQString &addr, TQWidget *parent ) {
00036 TQString email;
00037 TQString 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 TQApplication::eventLoop()->processEvents( TQEventLoop::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(TQString)", addressees.first().uid() );
00073 } else {
00074
00075 #if 0
00076 TQString text = i18n("<qt>The email address <b>%1</b> cannot be "
00077 "found in your addressbook.</qt>").arg( email );
00078 #else
00079 TQString text = email + " " + i18n( "is not in address book" );
00080 #endif
00081 KMessageBox::information( parent, text, TQString::null, "notInAddressBook" );
00082 }
00083 }
00084
00085
00086 void KAddrBookExternal::addEmail( const TQString& addr, TQWidget *parent) {
00087 TQString email;
00088 TQString 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 TQApplication::eventLoop()->processEvents( TQEventLoop::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 TQString 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 TQString 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, TQString::null, "addedtokabc" );
00155 }
00156 } else {
00157 TQString text = i18n("<qt>The email address <b>%1</b> is already in your "
00158 "addressbook.</qt>").arg( addr );
00159 KMessageBox::information( parent, text, TQString::null,
00160 "alreadyInAddressBook" );
00161 }
00162 ab->setErrorHandler( 0 );
00163 }
00164
00165 void KAddrBookExternal::openAddressBook(TQWidget *) {
00166 kapp->startServiceByDesktopName( "kaddressbook" );
00167 }
00168
00169 void KAddrBookExternal::addNewAddressee( TQWidget* )
00170 {
00171 kapp->startServiceByDesktopName("kaddressbook");
00172 DCOPRef call("kaddressbook", "KAddressBookIface");
00173 call.send("newContact()");
00174 }
00175
00176 bool KAddrBookExternal::addVCard( const KABC::Addressee& addressee, TQWidget *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 TQString 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, TQString::null, "addedtokabc" );
00192 inserted = true;
00193 }
00194 } else {
00195 TQString 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 KABC::Resource *kabcResource = selectResourceForSaving( addressBook );
00211 if( !kabcResource )
00212 return false;
00213 KABC::Ticket *ticket = addressBook->requestSaveTicket( kabcResource );
00214 bool saved = false;
00215 if ( ticket ) {
00216 KABC::Addressee addressee( addr );
00217 addressee.setResource( kabcResource );
00218 addressBook->insertAddressee( addressee );
00219 saved = addressBook->save( ticket );
00220 if ( !saved )
00221 addressBook->releaseSaveTicket( ticket );
00222 }
00223
00224 addressBook->emitAddressBookChanged();
00225
00226 return saved;
00227 }
00228
00229 TQString KAddrBookExternal::expandDistributionList( const TQString& listName )
00230 {
00231 if ( listName.isEmpty() )
00232 return TQString::null;
00233
00234 const TQString lowerListName = listName.lower();
00235 KABC::AddressBook *addressBook = KABC::StdAddressBook::self( true );
00236 #ifdef KDEPIM_NEW_DISTRLISTS
00237 KPIM::DistributionList distrList = KPIM::DistributionList::findByName( addressBook, lowerListName, false );
00238 if ( !distrList.isEmpty() ) {
00239 return distrList.emails( addressBook ).join( ", " );
00240 }
00241 #else
00242 KABC::DistributionListManager manager( addressBook );
00243 manager.load();
00244 const TQStringList listNames = manager.listNames();
00245
00246 for ( TQStringList::ConstIterator it = listNames.begin();
00247 it != listNames.end(); ++it) {
00248 if ( (*it).lower() == lowerListName ) {
00249 const TQStringList addressList = manager.list( *it )->emails();
00250 return addressList.join( ", " );
00251 }
00252 }
00253 #endif
00254 return TQString::null;
00255 }
00256
00257 KABC::Resource* KAddrBookExternal::selectResourceForSaving( KABC::AddressBook *addressBook )
00258 {
00259 #if KDE_IS_VERSION(3,4,89)
00260
00261 while ( !addressBook->loadingHasFinished() ) {
00262 TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput );
00263
00264
00265 usleep( 100 );
00266 }
00267 #endif
00268
00269
00270 TQPtrList<KABC::Resource> kabcResources = addressBook->resources();
00271
00272 TQPtrList<KRES::Resource> kresResources;
00273 TQPtrListIterator<KABC::Resource> resIt( kabcResources );
00274 KABC::Resource *kabcResource;
00275 while ( ( kabcResource = resIt.current() ) != 0 ) {
00276 ++resIt;
00277 if ( !kabcResource->readOnly() ) {
00278 KRES::Resource *res = static_cast<KRES::Resource*>( kabcResource );
00279 if ( res )
00280 kresResources.append( res );
00281 }
00282 }
00283
00284 return static_cast<KABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) );
00285 }