kaddrbook.cpp
00001 // -*- mode: C++; c-file-style: "gnu" -*- 00002 // kaddrbook.cpp 00003 // Author: Stefan Taferner <taferner@kde.org> 00004 // This code is under GPL 00005 00006 #include <config.h> 00007 00008 #include "kaddrbook.h" 00009 00010 #ifdef TDEPIM_NEW_DISTRLISTS 00011 #include "distributionlist.h" 00012 #else 00013 #include <tdeabc/distributionlist.h> 00014 #endif 00015 00016 #include <tdeapplication.h> 00017 #include <kdebug.h> 00018 #include <tdelocale.h> 00019 #include <tdemessagebox.h> 00020 #include <tdeversion.h> 00021 #include <tdeabc/resource.h> 00022 #include <tdeabc/stdaddressbook.h> 00023 #include <tdeabc/vcardconverter.h> 00024 #include <tdeabc/errorhandler.h> 00025 #include <tderesources/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 TDEABC::Addressee::parseEmailAddress( addr, name, email ); 00040 00041 TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( true ); 00042 00043 // force a reload of the address book file so that changes that were made 00044 // by other programs are loaded 00045 ab->asyncLoad(); 00046 00047 // if we have to reload the address book then we should also wait until 00048 // it's completely reloaded 00049 #if KDE_IS_VERSION(3,4,89) 00050 // This ugly hack will be removed in 4.0 00051 while ( !ab->loadingHasFinished() ) { 00052 TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput ); 00053 00054 // use sleep here to reduce cpu usage 00055 usleep( 100 ); 00056 } 00057 #endif 00058 00059 TDEABC::Addressee::List addressees = ab->findByEmail( email ); 00060 00061 if ( addressees.count() > 0 ) { 00062 if ( kapp->dcopClient()->isApplicationRegistered( "kaddressbook" ) ){ 00063 //make sure kaddressbook is loaded, otherwise showContactEditor 00064 //won't work as desired, see bug #87233 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 //TODO: Enable the better message at the next string unfreeze 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(), "notInAddressBook" ); 00082 } 00083 } 00084 00085 //----------------------------------------------------------------------------- 00086 void KAddrBookExternal::addEmail( const TQString& addr, TQWidget *parent) { 00087 TQString email; 00088 TQString name; 00089 00090 TDEABC::Addressee::parseEmailAddress( addr, name, email ); 00091 00092 TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( true ); 00093 00094 ab->setErrorHandler( new TDEABC::GuiErrorHandler( parent ) ); 00095 00096 // force a reload of the address book file so that changes that were made 00097 // by other programs are loaded 00098 ab->asyncLoad(); 00099 00100 // if we have to reload the address book then we should also wait until 00101 // it's completely reloaded 00102 #if KDE_IS_VERSION(3,4,89) 00103 // This ugly hack will be removed in 4.0 00104 while ( !ab->loadingHasFinished() ) { 00105 TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput ); 00106 00107 // use sleep here to reduce cpu usage 00108 usleep( 100 ); 00109 } 00110 #endif 00111 00112 TDEABC::Addressee::List addressees = ab->findByEmail( email ); 00113 00114 if ( addressees.isEmpty() ) { 00115 TDEABC::Addressee a; 00116 a.setNameFromString( name ); 00117 a.insertEmail( email, true ); 00118 00119 { 00120 TDEConfig 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(), "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(), 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 TDEABC::Addressee& addressee, TQWidget *parent ) 00177 { 00178 TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( true ); 00179 bool inserted = false; 00180 00181 ab->setErrorHandler( new TDEABC::GuiErrorHandler( parent ) ); 00182 00183 TDEABC::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(), "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 TDEABC::Addressee& addr ) 00208 { 00209 TDEABC::AddressBook *addressBook = TDEABC::StdAddressBook::self( true ); 00210 TDEABC::Resource *tdeabcResource = selectResourceForSaving( addressBook ); 00211 if( !tdeabcResource ) 00212 return false; 00213 TDEABC::Ticket *ticket = addressBook->requestSaveTicket( tdeabcResource ); 00214 bool saved = false; 00215 if ( ticket ) { 00216 TDEABC::Addressee addressee( addr ); 00217 addressee.setResource( tdeabcResource ); 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(); 00233 00234 const TQString lowerListName = listName.lower(); 00235 TDEABC::AddressBook *addressBook = TDEABC::StdAddressBook::self( true ); 00236 #ifdef TDEPIM_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 TDEABC::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(); 00255 } 00256 00257 TDEABC::Resource* KAddrBookExternal::selectResourceForSaving( TDEABC::AddressBook *addressBook ) 00258 { 00259 #if KDE_IS_VERSION(3,4,89) 00260 // This ugly hack will be removed in 4.0 00261 while ( !addressBook->loadingHasFinished() ) { 00262 TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput ); 00263 00264 // use sleep here to reduce cpu usage 00265 usleep( 100 ); 00266 } 00267 #endif 00268 00269 // Select a resource 00270 TQPtrList<TDEABC::Resource> tdeabcResources = addressBook->resources(); 00271 00272 TQPtrList<KRES::Resource> kresResources; 00273 TQPtrListIterator<TDEABC::Resource> resIt( tdeabcResources ); 00274 TDEABC::Resource *tdeabcResource; 00275 while ( ( tdeabcResource = resIt.current() ) != 0 ) { 00276 ++resIt; 00277 if ( !tdeabcResource->readOnly() ) { 00278 KRES::Resource *res = static_cast<KRES::Resource*>( tdeabcResource ); 00279 if ( res ) 00280 kresResources.append( res ); 00281 } 00282 } 00283 00284 return static_cast<TDEABC::Resource*>( KRES::SelectDialog::getResource( kresResources, 0 ) ); 00285 }