kmail

distributionlistdialog.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of KMail.
00003 
00004     Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010     
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015     
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <config.h> // for KDEPIM_NEW_DISTRLISTS
00023 
00024 #include "distributionlistdialog.h"
00025 
00026 #include <libemailfunctions/email.h>
00027 #include <kabc/resource.h>
00028 #include <kabc/stdaddressbook.h>
00029 #include <kabc/distributionlist.h>
00030 
00031 #ifdef KDEPIM_NEW_DISTRLISTS
00032 #include <libkdepim/distributionlist.h>
00033 #endif
00034 
00035 #include <klistview.h>
00036 #include <klocale.h>
00037 #include <kdebug.h>
00038 #include <kmessagebox.h>
00039 #include <kinputdialog.h>
00040 
00041 #include <qlayout.h>
00042 #include <qlabel.h>
00043 #include <qlineedit.h>
00044 
00045 class DistributionListItem : public QCheckListItem
00046 {
00047   public:
00048     DistributionListItem( QListView *list )
00049       : QCheckListItem( list, QString::null, CheckBox )
00050     {
00051     }
00052 
00053     void setAddressee( const KABC::Addressee &a, const QString &email )
00054     {
00055       mIsTransient = false;
00056       init( a, email );
00057     }
00058 
00059     void setTransientAddressee( const KABC::Addressee &a, const QString &email )
00060     {
00061       mIsTransient = true;
00062       init( a, email );
00063     }
00064 
00065     void init( const KABC::Addressee &a, const QString &email )
00066     {
00067       mAddressee = a;
00068       mEmail = email;
00069       setText( 1, mAddressee.realName() );
00070       setText( 2, mEmail );
00071     }
00072 
00073     KABC::Addressee addressee() const
00074     {
00075       return mAddressee;
00076     }
00077     
00078     QString email() const
00079     {
00080       return mEmail;
00081     }
00082     
00083     bool isTransient() const
00084     {
00085       return mIsTransient;
00086     }
00087     
00088   private:
00089     KABC::Addressee mAddressee;
00090     QString mEmail;
00091     bool mIsTransient;
00092 };
00093 
00094 
00095 DistributionListDialog::DistributionListDialog( QWidget *parent )
00096   : KDialogBase( Plain, i18n("Save Distribution List"), User1 | Cancel,
00097                  User1, parent, 0, false, false, i18n("Save List") )
00098 {
00099   QFrame *topFrame = plainPage();
00100   
00101   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00102   topLayout->setSpacing( spacingHint() );
00103   
00104   QBoxLayout *titleLayout = new QHBoxLayout( topLayout );
00105   
00106   QLabel *label = new QLabel( i18n("Name:"), topFrame );
00107   titleLayout->addWidget( label );
00108   
00109   mTitleEdit = new QLineEdit( topFrame );
00110   titleLayout->addWidget( mTitleEdit );
00111   mTitleEdit->setFocus();
00112   
00113   mRecipientsList = new KListView( topFrame );
00114   mRecipientsList->addColumn( QString::null );
00115   mRecipientsList->addColumn( i18n("Name") );
00116   mRecipientsList->addColumn( i18n("Email") );
00117   topLayout->addWidget( mRecipientsList );
00118 }
00119 
00120 void DistributionListDialog::setRecipients( const Recipient::List &recipients )
00121 {
00122   Recipient::List::ConstIterator it;
00123   for( it = recipients.begin(); it != recipients.end(); ++it ) {
00124     QStringList emails = KPIM::splitEmailAddrList( (*it).email() );
00125     QStringList::ConstIterator it2;
00126     for( it2 = emails.begin(); it2 != emails.end(); ++it2 ) {
00127       QString name;
00128       QString email;
00129       KABC::Addressee::parseEmailAddress( *it2, name, email );
00130       if ( !email.isEmpty() ) {
00131         DistributionListItem *item = new DistributionListItem( mRecipientsList );
00132         KABC::Addressee::List addressees =
00133           KABC::StdAddressBook::self( true )->findByEmail( email );
00134         if ( addressees.isEmpty() ) {
00135           KABC::Addressee a;
00136           a.setNameFromString( name );
00137           a.insertEmail( email );
00138           item->setTransientAddressee( a, email );
00139           item->setOn( true );
00140         } else {
00141           KABC::Addressee::List::ConstIterator it3;
00142           for( it3 = addressees.begin(); it3 != addressees.end(); ++it3 ) {
00143             item->setAddressee( *it3, email );
00144             if ( it3 == addressees.begin() ) item->setOn( true );
00145           }
00146         }
00147       }
00148     }
00149   }
00150 }
00151 
00152 void DistributionListDialog::slotUser1()
00153 {
00154   bool isEmpty = true;
00155 
00156   KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00157 
00158   QListViewItem *i = mRecipientsList->firstChild();
00159   while( i ) {
00160     DistributionListItem *item = static_cast<DistributionListItem *>( i );
00161     if ( item->isOn() ) {
00162       isEmpty = false;
00163       break;
00164     }
00165     i = i->nextSibling();
00166   }
00167 
00168   if ( isEmpty ) {
00169     KMessageBox::information( this,
00170                               i18n("There are no recipients in your list. "
00171                                    "First select some recipients, "
00172                                    "then try again.") );
00173     return;
00174   }
00175 
00176 #ifndef KDEPIM_NEW_DISTRLISTS
00177   KABC::DistributionListManager manager( ab );
00178   manager.load();
00179 #endif
00180 
00181   QString name = mTitleEdit->text();
00182 
00183   if ( name.isEmpty() ) {
00184     bool ok = false;
00185     name = KInputDialog::getText( i18n("New Distribution List"),
00186       i18n("Please enter name:"), QString::null, &ok, this );
00187     if ( !ok || name.isEmpty() )
00188       return;
00189   }
00190 
00191 #ifdef KDEPIM_NEW_DISTRLISTS
00192   if ( !KPIM::DistributionList::findByName( ab, name ).isEmpty() ) {
00193 #else
00194   if ( manager.list( name ) ) {
00195 #endif
00196     KMessageBox::information( this,
00197       i18n( "<qt>Distribution list with the given name <b>%1</b> "
00198         "already exists. Please select a different name.</qt>" ).arg( name ) );
00199     return;
00200   }
00201 
00202 #ifdef KDEPIM_NEW_DISTRLISTS
00203   KPIM::DistributionList dlist;
00204   dlist.setName( name );
00205 
00206   i = mRecipientsList->firstChild();
00207   while( i ) {
00208     DistributionListItem *item = static_cast<DistributionListItem *>( i );
00209     if ( item->isOn() ) {
00210       kdDebug() << "  " << item->addressee().fullEmail() << endl;
00211       if ( item->isTransient() ) {
00212         ab->insertAddressee( item->addressee() );
00213       }
00214       if ( item->email() == item->addressee().preferredEmail() ) {
00215         dlist.insertEntry( item->addressee() );
00216       } else {
00217         dlist.insertEntry( item->addressee(), item->email() );
00218       }
00219     }
00220     i = i->nextSibling();
00221   }
00222 
00223   ab->insertAddressee( dlist );
00224 #else
00225   KABC::DistributionList *dlist = new KABC::DistributionList( &manager, name );
00226   i = mRecipientsList->firstChild();
00227   while( i ) {
00228     DistributionListItem *item = static_cast<DistributionListItem *>( i );
00229     if ( item->isOn() ) {
00230       kdDebug() << "  " << item->addressee().fullEmail() << endl;
00231       if ( item->isTransient() ) {
00232         ab->insertAddressee( item->addressee() );
00233       }
00234       if ( item->email() == item->addressee().preferredEmail() ) {
00235         dlist->insertEntry( item->addressee() );
00236       } else {
00237         dlist->insertEntry( item->addressee(), item->email() );
00238       }
00239     }
00240     i = i->nextSibling();
00241   }
00242 #endif
00243 
00244   // FIXME: Ask the user which resource to save to instead of the default
00245   bool saveError = true;
00246   KABC::Ticket *ticket = ab->requestSaveTicket( 0 /*default resource */ );
00247   if ( ticket )
00248     if ( ab->save( ticket ) )
00249       saveError = false;
00250     else
00251       ab->releaseSaveTicket( ticket );
00252 
00253   if ( saveError )
00254     kdWarning(5006) << k_funcinfo << " Couldn't save new addresses in the distribution list just created to the address book" << endl;
00255 
00256 #ifndef KDEPIM_NEW_DISTRLISTS
00257   manager.save();
00258 #endif
00259 
00260   close();
00261 }