9#ifdef TDEPIM_NEW_DISTRLISTS 
   10#include "distributionlist.h" 
   12#include <tdeabc/distributionlist.h> 
   15#include <tdeapplication.h> 
   18#include <tdemessagebox.h> 
   19#include <tdeversion.h> 
   20#include <tdeabc/resource.h> 
   21#include <tdeabc/stdaddressbook.h> 
   22#include <tdeabc/vcardconverter.h> 
   23#include <tdeabc/errorhandler.h> 
   24#include <tderesources/selectdialog.h> 
   26#include <dcopclient.h> 
   28#include <tqeventloop.h> 
   34void KAddrBookExternal::openEmail( 
const TQString &addr, TQWidget *parent ) {
 
   38  TDEABC::Addressee::parseEmailAddress( addr, name, email );
 
   40  TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( 
true );
 
   48#if KDE_IS_VERSION(3,4,89) 
   50  while ( !ab->loadingHasFinished() ) {
 
   51    TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput );
 
   58  TDEABC::Addressee::List addressees = ab->findByEmail( email );
 
   60  if ( addressees.count() > 0 ) {
 
   61    if ( tdeApp->dcopClient()->isApplicationRegistered( 
"kaddressbook" ) ){
 
   64      DCOPRef call ( 
"kaddressbook", 
"kaddressbook" );
 
   65      call.send( 
"newInstance()" );
 
   67      tdeApp->startServiceByDesktopName( 
"kaddressbook" );
 
   70    DCOPRef call( 
"kaddressbook", 
"KAddressBookIface" );
 
   71    call.send( 
"showContactEditor(TQString)", addressees.first().uid() );
 
   75    TQString text = i18n(
"<qt>The email address <b>%1</b> cannot be " 
   76                        "found in your addressbook.</qt>").arg( email );
 
   78    TQString text = email + 
" " + i18n( 
"is not in address book" );
 
   80    KMessageBox::information( parent, text, TQString(), 
"notInAddressBook" );
 
   85void KAddrBookExternal::addEmail( 
const TQString& addr, TQWidget *parent) {
 
   89  TDEABC::Addressee::parseEmailAddress( addr, name, email );
 
   91  TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( 
true );
 
   93  ab->setErrorHandler( 
new TDEABC::GuiErrorHandler( parent ) );
 
  101#if KDE_IS_VERSION(3,4,89) 
  103  while ( !ab->loadingHasFinished() ) {
 
  104    TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput );
 
  111  TDEABC::Addressee::List addressees = ab->findByEmail( email );
 
  113  if ( addressees.isEmpty() ) {
 
  115    a.setNameFromString( name );
 
  116    a.insertEmail( email, 
true );
 
  119      TDEConfig config( 
"kaddressbookrc" );
 
  120      config.setGroup( 
"General" );
 
  121      int type = config.readNumEntry( 
"FormattedNameType", 1 );
 
  126          name = a.givenName() + 
" " + a.familyName();
 
  129          name = a.assembledName();
 
  132          name = a.familyName() + 
", " + a.givenName();
 
  135          name = a.familyName() + 
" " + a.givenName();
 
  138          name = a.organization();
 
  144      name.simplifyWhiteSpace();
 
  146      a.setFormattedName( name );
 
  149    if ( KAddrBookExternal::addAddressee( a ) ) {
 
  150      TQString text = i18n(
"<qt>The email address <b>%1</b> was added to your " 
  151                          "addressbook; you can add more information to this " 
  152                          "entry by opening the addressbook.</qt>").arg( addr );
 
  153      KMessageBox::information( parent, text, TQString(), 
"addedtokabc" );
 
  156    TQString text = i18n(
"<qt>The email address <b>%1</b> is already in your " 
  157                        "addressbook.</qt>").arg( addr );
 
  158    KMessageBox::information( parent, text, TQString(),
 
  159                              "alreadyInAddressBook" );
 
  161  ab->setErrorHandler( 0 );
 
  164void KAddrBookExternal::openAddressBook(TQWidget *) {
 
  165  tdeApp->startServiceByDesktopName( 
"kaddressbook" );
 
  168void KAddrBookExternal::addNewAddressee( TQWidget* )
 
  170  tdeApp->startServiceByDesktopName(
"kaddressbook");
 
  171  DCOPRef call(
"kaddressbook", 
"KAddressBookIface");
 
  172  call.send(
"newContact()");
 
  175bool KAddrBookExternal::addVCard( 
const TDEABC::Addressee& addressee, TQWidget *parent )
 
  177  TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( 
true );
 
  178  bool inserted = 
false;
 
  180  ab->setErrorHandler( 
new TDEABC::GuiErrorHandler( parent ) );
 
  182  TDEABC::Addressee::List addressees =
 
  183      ab->findByEmail( addressee.preferredEmail() );
 
  185  if ( addressees.isEmpty() ) {
 
  186    if ( KAddrBookExternal::addAddressee( addressee ) ) {
 
  187      TQString text = i18n(
"The VCard was added to your addressbook; " 
  188                          "you can add more information to this " 
  189                          "entry by opening the addressbook.");
 
  190      KMessageBox::information( parent, text, TQString(), 
"addedtokabc" );
 
  194    TQString text = i18n(
"The VCard's primary email address is already in " 
  195                        "your addressbook; however, you may save the VCard " 
  196                        "into a file and import it into the addressbook " 
  198    KMessageBox::information( parent, text );
 
  202  ab->setErrorHandler( 0 );
 
  206bool KAddrBookExternal::addAddressee( 
const TDEABC::Addressee& addr )
 
  208  TDEABC::AddressBook *addressBook = TDEABC::StdAddressBook::self( 
true );
 
  209  TDEABC::Resource *tdeabcResource = selectResourceForSaving( addressBook );
 
  210  if( !tdeabcResource ) 
 
  212  TDEABC::Ticket *ticket = addressBook->requestSaveTicket( tdeabcResource );
 
  215    TDEABC::Addressee addressee( addr );
 
  216    addressee.setResource( tdeabcResource );
 
  217    addressBook->insertAddressee( addressee );
 
  218    saved = addressBook->save( ticket );
 
  220      addressBook->releaseSaveTicket( ticket );
 
  223  addressBook->emitAddressBookChanged();
 
  228TQString KAddrBookExternal::expandDistributionList( 
const TQString& listName )
 
  230  if ( listName.isEmpty() )
 
  233  const TQString lowerListName = listName.lower();
 
  234  TDEABC::AddressBook *addressBook = TDEABC::StdAddressBook::self( 
true );
 
  235#ifdef TDEPIM_NEW_DISTRLISTS 
  236  KPIM::DistributionList distrList = KPIM::DistributionList::findByName( addressBook, lowerListName, 
false );
 
  237  if ( !distrList.isEmpty() ) {
 
  238    return distrList.
emails( addressBook ).join( 
", " );
 
  241  TDEABC::DistributionListManager manager( addressBook );
 
  243  const TQStringList listNames = manager.listNames();
 
  245  for ( TQStringList::ConstIterator it = listNames.begin();
 
  246        it != listNames.end(); ++it) {
 
  247    if ( (*it).lower() == lowerListName ) {
 
  248      const TQStringList addressList = manager.list( *it )->emails();
 
  249      return addressList.join( 
", " );
 
  256TDEABC::Resource* KAddrBookExternal::selectResourceForSaving( TDEABC::AddressBook *addressBook )
 
  258#if KDE_IS_VERSION(3,4,89) 
  260  while ( !addressBook->loadingHasFinished() ) {
 
  261    TQApplication::eventLoop()->processEvents( TQEventLoop::ExcludeUserInput );
 
  269  TQPtrList<TDEABC::Resource> tdeabcResources = addressBook->resources();
 
  271  TQPtrList<KRES::Resource> kresResources;
 
  272  TQPtrListIterator<TDEABC::Resource> resIt( tdeabcResources );
 
  273  TDEABC::Resource *tdeabcResource;
 
  274  while ( ( tdeabcResource = resIt.current() ) != 0 ) {
 
  276    if ( !tdeabcResource->readOnly() ) {
 
  277      KRES::Resource *res = 
static_cast<KRES::Resource*
>( tdeabcResource );
 
  279        kresResources.append( res );
 
  283  return static_cast<TDEABC::Resource*
>( KRES::SelectDialog::getResource( kresResources, 0 ) );
 
Distribution list of email addresses.
TQStringList emails(TDEABC::AddressBook *book) const
Return list of email addresses, which belong to this distributon list.