kmail

distributionlistdialog.cpp

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 "distributionlistdialog.h"
00023 
00024 #include <libemailfunctions/email.h>
00025 #include <kabc/stdaddressbook.h>
00026 #include <kabc/distributionlist.h>
00027 
00028 #include <klistview.h>
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 #include <kmessagebox.h>
00032 #include <kinputdialog.h>
00033 
00034 #include <qlayout.h>
00035 #include <qlabel.h>
00036 #include <qlineedit.h>
00037 
00038 class DistributionListItem : public QCheckListItem
00039 {
00040   public:
00041     DistributionListItem( QListView *list )
00042       : QCheckListItem( list, QString::null, CheckBox )
00043     {
00044     }
00045 
00046     void setAddressee( const KABC::Addressee &a, const QString &email )
00047     {
00048       mIsTransient = false;
00049       init( a, email );
00050     }
00051 
00052     void setTransientAddressee( const KABC::Addressee &a, const QString &email )
00053     {
00054       mIsTransient = true;
00055       init( a, email );
00056     }
00057 
00058     void init( const KABC::Addressee &a, const QString &email )
00059     {
00060       mAddressee = a;
00061       mEmail = email;
00062       setText( 1, mAddressee.realName() );
00063       setText( 2, mEmail );
00064     }
00065 
00066     KABC::Addressee addressee() const
00067     {
00068       return mAddressee;
00069     }
00070     
00071     QString email() const
00072     {
00073       return mEmail;
00074     }
00075     
00076     bool isTransient() const
00077     {
00078       return mIsTransient;
00079     }
00080     
00081   private:
00082     KABC::Addressee mAddressee;
00083     QString mEmail;
00084     bool mIsTransient;
00085 };
00086 
00087 
00088 DistributionListDialog::DistributionListDialog( QWidget *parent )
00089   : KDialogBase( Plain, i18n("Save Distribution List"), User1 | Cancel,
00090                  User1, parent, 0, false, false, i18n("Save List") )
00091 {
00092   QFrame *topFrame = plainPage();
00093   
00094   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00095   topLayout->setSpacing( spacingHint() );
00096   
00097   QBoxLayout *titleLayout = new QHBoxLayout( topLayout );
00098   
00099   QLabel *label = new QLabel( i18n("Name:"), topFrame );
00100   titleLayout->addWidget( label );
00101   
00102   mTitleEdit = new QLineEdit( topFrame );
00103   titleLayout->addWidget( mTitleEdit );
00104   mTitleEdit->setFocus();
00105   
00106   mRecipientsList = new KListView( topFrame );
00107   mRecipientsList->addColumn( QString::null );
00108   mRecipientsList->addColumn( i18n("Name") );
00109   mRecipientsList->addColumn( i18n("Email") );
00110   topLayout->addWidget( mRecipientsList );
00111 }
00112 
00113 void DistributionListDialog::setRecipients( const Recipient::List &recipients )
00114 {
00115   Recipient::List::ConstIterator it;
00116   for( it = recipients.begin(); it != recipients.end(); ++it ) {
00117     QStringList emails = KPIM::splitEmailAddrList( (*it).email() );
00118     QStringList::ConstIterator it2;
00119     for( it2 = emails.begin(); it2 != emails.end(); ++it2 ) {
00120       QString name;
00121       QString email;
00122       KABC::Addressee::parseEmailAddress( *it2, name, email );
00123       if ( !email.isEmpty() ) {
00124         DistributionListItem *item = new DistributionListItem( mRecipientsList );
00125         KABC::Addressee::List addressees =
00126           KABC::StdAddressBook::self( true )->findByEmail( email );
00127         if ( addressees.isEmpty() ) {
00128           KABC::Addressee a;
00129           a.setNameFromString( name );
00130           a.insertEmail( name );
00131           item->setTransientAddressee( a, email );
00132           item->setOn( true );
00133         } else {
00134           KABC::Addressee::List::ConstIterator it3;
00135           for( it3 = addressees.begin(); it3 != addressees.end(); ++it3 ) {
00136             item->setAddressee( *it3, email );
00137             if ( it3 == addressees.begin() ) item->setOn( true );
00138           }
00139         }
00140       }
00141     }
00142   }
00143 }
00144 
00145 void DistributionListDialog::slotUser1()
00146 {
00147   bool isEmpty = true;
00148 
00149   KABC::StdAddressBook *ab = KABC::StdAddressBook::self( true );
00150 
00151   QListViewItem *i = mRecipientsList->firstChild();
00152   while( i ) {
00153     DistributionListItem *item = static_cast<DistributionListItem *>( i );
00154     if ( item->isOn() ) {
00155       isEmpty = false;
00156       break;
00157     }
00158     i = i->nextSibling();
00159   }
00160 
00161   if ( isEmpty ) {
00162     KMessageBox::information( this,
00163                               i18n("There are no recipients in your list. "
00164                                    "First select some recipients, "
00165                                    "then try again.") );
00166     return;
00167   }
00168 
00169   KABC::DistributionListManager manager( ab );
00170   manager.load();
00171 
00172   QString name = mTitleEdit->text();
00173 
00174   if ( name.isEmpty() ) {
00175     bool ok = false;
00176     name = KInputDialog::getText( i18n("New Distribution List"),
00177       i18n("Please enter name:"), QString::null, &ok, this );
00178     if ( !ok || name.isEmpty() )
00179       return;
00180   }
00181 
00182   if ( manager.list( name ) ) {
00183     KMessageBox::information( this,
00184       i18n( "<qt>Distribution list with the given name <b>%1</b> "
00185         "already exists. Please select a different name.</qt>" ).arg( name ) );
00186     return;
00187   }
00188 
00189   KABC::DistributionList *dlist = new KABC::DistributionList( &manager, name );
00190   i = mRecipientsList->firstChild();
00191   while( i ) {
00192     DistributionListItem *item = static_cast<DistributionListItem *>( i );
00193     if ( item->isOn() ) {
00194       kdDebug() << "  " << item->addressee().fullEmail() << endl;
00195       if ( item->isTransient() ) {
00196         // FIXME: Adding to the address book doesn't seem to work.
00197         ab->insertAddressee( item->addressee() );
00198       }
00199       if ( item->email() == item->addressee().preferredEmail() ) {
00200         dlist->insertEntry( item->addressee() );
00201       } else {
00202         dlist->insertEntry( item->addressee(), item->email() );
00203       }
00204     }
00205     i = i->nextSibling();
00206   }
00207 
00208   manager.save();
00209 
00210   close();
00211 }
KDE Home | KDE Accessibility Home | Description of Access Keys