kaddressbook

distributionlisteditor.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2007 Klaralvdalens Datakonsult AB <frank@kdab.net>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018     As a special exception, permission is given to link this program
00019     with any edition of Qt, and distribute the resulting executable,
00020     without including the source code for Qt in the source distribution.
00021 */
00022 
00023 #include "distributionlisteditor.h"
00024 #include "distributionlisteditor_p.h"
00025 
00026 #include <libkdepim/addresseelineedit.h>
00027 #include <libkdepim/distributionlist.h>
00028 #include <libemailfunctions/email.h>
00029 
00030 #include <kabc/addressbook.h>
00031 
00032 #include <kapplication.h>
00033 #include <kdialogbase.h>
00034 #include <kglobal.h> 
00035 #include <kiconloader.h>
00036 #include <klineedit.h>
00037 #include <klocale.h>
00038 #include <kmessagebox.h>
00039 
00040 #include <qlabel.h>
00041 #include <qlayout.h>
00042 #include <qsignalmapper.h>
00043 #include <qtoolbutton.h>
00044 
00045 class KPIM::DistributionListEditor::EditorWidgetPrivate
00046 {
00047 public:
00048     QScrollView* scrollView;
00049     QSignalMapper* mapper;
00050     KABC::AddressBook* addressBook;
00051     QString distListUid;
00052     QLabel* nameLabel;
00053     QLabel* memberListLabel;
00054     KLineEdit* nameLineEdit;
00055     QWidget* memberListWidget;
00056     QVBoxLayout* addresseeLayout;
00057     QValueList<KPIM::DistributionListEditor::Line*> addressees;
00058     KPIM::DistributionList distributionList;
00059     void addLineForEntry( const KPIM::DistributionList::Entry& entry );
00060     int lastLineId;
00061 };
00062 
00063 
00064 KPIM::DistributionListEditor::Line::Line( KABC::AddressBook* book, QWidget* parent ) : QWidget( parent ), m_addressBook( book )
00065 {
00066     Q_ASSERT( m_addressBook );
00067     QBoxLayout* layout = new QHBoxLayout( this );
00068     layout->setSpacing( KDialog::spacingHint() );
00069     m_lineEdit = new KPIM::DistributionListEditor::LineEdit( this );
00070     connect( m_lineEdit, SIGNAL( textChanged( const QString& ) ),
00071              this, SLOT( textChanged( const QString& ) ) );
00072     layout->addWidget( m_lineEdit );
00073     QToolButton *button = new QToolButton( this );
00074     button->setIconSet( KApplication::reverseLayout() ? SmallIconSet("locationbar_erase") : SmallIconSet( "clear_left" ) );
00075  
00076 
00077     layout->addWidget( button );
00078     connect( button, SIGNAL( clicked() ), m_lineEdit, SLOT( clear() ) );
00079 }
00080 
00081 void KPIM::DistributionListEditor::Line::textChanged( const QString& text )
00082 {
00083     if ( text.isEmpty() )
00084         emit cleared();
00085     emit textChanged();
00086 }
00087 
00088 void KPIM::DistributionListEditor::Line::setEntry( const KPIM::DistributionList::Entry& entry )
00089 {
00090     m_uid = entry.addressee.uid();
00091     m_initialText = entry.addressee.fullEmail( entry.email );
00092     m_lineEdit->setText( m_initialText ); 
00093 }
00094 
00095 KABC::Addressee KPIM::DistributionListEditor::Line::findAddressee( const QString& name, const QString& email ) const
00096 {
00097     if ( name.isEmpty() && email.isEmpty() )
00098         return KABC::Addressee();
00099 
00100     typedef KABC::Addressee::List List;
00101     const List byEmail = m_addressBook->findByEmail( email );
00102     if ( !byEmail.isEmpty() )
00103     {        
00104         const List::ConstIterator end = byEmail.end();
00105         for ( List::ConstIterator it = byEmail.begin(); it != end; ++it )
00106         {
00107             if ( (*it).formattedName() == name )
00108                 return *it;
00109         }
00110         return byEmail.first();
00111     }
00112     // no entry found, create new addressee:
00113     KABC::Addressee addressee;
00114     addressee.setUid( KApplication::randomString( 10 ) );
00115     addressee.setFormattedName( name );
00116     addressee.setEmails( email );
00117     m_addressBook->insertAddressee( addressee );
00118     return addressee;
00119 }
00120 
00121 KPIM::DistributionList::Entry KPIM::DistributionListEditor::Line::entry() const
00122 {
00123     const QString text = m_lineEdit->text();
00124     QString name;
00125     QString email;
00126     KPIM::getNameAndMail(m_lineEdit->text(), name, email );
00127 
00128     KPIM::DistributionList::Entry res;
00129     if ( !m_uid.isNull() )
00130     {
00131         const KABC::Addressee addr = m_addressBook->findByUid( m_uid );
00132         if ( m_initialText == text || addr.formattedName() == name )
00133             res.addressee = addr;
00134     }
00135     if ( res.addressee.isEmpty() )
00136         res.addressee = findAddressee( name, email ); 
00137     res.email = res.addressee.preferredEmail() != email ? email : QString();
00138     return res;
00139 }
00140 
00141 
00142 KPIM::DistributionListEditor::LineEdit::LineEdit( QWidget* parent ) : KPIM::AddresseeLineEdit( parent )
00143 {
00144 }
00145 
00146 
00147 KPIM::DistributionListEditor::EditorWidget::EditorWidget( KABC::AddressBook* book,  QWidget* parent ) 
00148     : KDialogBase( parent, /*name=*/0, /*modal=*/ true, /*caption=*/QString(), KDialogBase::Ok|KDialogBase::Cancel ), d( new DistributionListEditor::EditorWidgetPrivate )
00149 {
00150     d->addressBook = book;
00151     Q_ASSERT( d->addressBook );
00152     d->lastLineId = 0;
00153     d->mapper = new QSignalMapper( this );
00154     connect( d->mapper, SIGNAL( mapped( int ) ), 
00155              this, SLOT( lineTextChanged( int ) ) ); 
00156     setCaption( i18n( "Edit Distribution List" ) );
00157     QWidget* main = new QWidget( this );
00158     QGridLayout* mainLayout = new QGridLayout( main );
00159     mainLayout->setMargin( KDialog::marginHint() );
00160     mainLayout->setSpacing( KDialog::spacingHint() );
00161 
00162     QHBoxLayout* nameLayout = new QHBoxLayout;
00163     nameLayout->setSpacing( KDialog::spacingHint() );
00164 
00165     d->nameLabel = new QLabel( main );
00166     d->nameLabel->setText( i18n( "Name:" ) );
00167     nameLayout->addWidget( d->nameLabel );
00168 
00169     d->nameLineEdit = new KLineEdit( main );
00170     nameLayout->addWidget( d->nameLineEdit );
00171 
00172     mainLayout->addLayout( nameLayout, 0, 0 );
00173 
00174     d->memberListLabel = new QLabel( main );
00175     d->memberListLabel->setText( i18n( "Distribution list members:" ) );
00176     mainLayout->addWidget( d->memberListLabel, 1, 0 );
00177 
00178     d->scrollView = new QScrollView( main );
00179     d->scrollView->setFrameShape( QFrame::NoFrame );
00180     mainLayout->addWidget( d->scrollView, 2, 0 );
00181     d->memberListWidget = new QWidget( d->scrollView->viewport() );
00182     d->memberListWidget->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
00183     QVBoxLayout* memberLayout = new QVBoxLayout( d->memberListWidget );
00184     d->addresseeLayout = new QVBoxLayout;
00185     d->addresseeLayout->setSpacing( KDialog::spacingHint() );
00186     memberLayout->addItem( d->addresseeLayout );
00187     memberLayout->addStretch();
00188     d->scrollView->addChild( d->memberListWidget );
00189     d->scrollView->setResizePolicy( QScrollView::AutoOneFit );
00190     
00191     setMainWidget( main );
00192 
00193     d->addLineForEntry( KPIM::DistributionList::Entry() );
00194 }
00195 
00196 KPIM::DistributionListEditor::EditorWidget::~EditorWidget()
00197 {
00198     delete d;
00199 }
00200 
00201 void KPIM::DistributionListEditor::EditorWidget::lineTextChanged( int id )
00202 {
00203     if ( id != d->lastLineId )
00204         return;
00205     d->addLineForEntry( KPIM::DistributionList::Entry() );
00206     d->scrollView->updateContents();
00207 }
00208 
00209 void KPIM::DistributionListEditor::EditorWidget::setDistributionList( const KPIM::DistributionList& list )
00210 {
00211     d->distListUid = list.uid();
00212     d->nameLineEdit->setText( list.name() );
00213 
00214     using KPIM::DistributionListEditor::Line;
00215     typedef QValueList<Line*>::ConstIterator ListIterator;
00216     for ( ListIterator it = d->addressees.begin(), end = d->addressees.end(); it != end; ++it )
00217     {
00218         delete *it;
00219     }
00220     d->addressees.clear();
00221 
00222     typedef KPIM::DistributionList::Entry Entry;
00223     const Entry::List entries = list.entries( d->addressBook );
00224 
00225     for ( Entry::List::ConstIterator it = entries.begin(), end = entries.end(); it != end; ++it )
00226     {
00227         d->addLineForEntry( *it );
00228     }
00229     d->addLineForEntry( Entry() );
00230 }
00231 
00232 void KPIM::DistributionListEditor::EditorWidgetPrivate::addLineForEntry( const KPIM::DistributionList::Entry& entry )
00233 {  
00234     KPIM::DistributionListEditor::Line* line = new KPIM::DistributionListEditor::Line( addressBook, memberListWidget );
00235     line->setEntry( entry );
00236     addresseeLayout->addWidget( line );
00237     addressees.append( line );
00238     QObject::connect( line, SIGNAL( textChanged() ), 
00239                       mapper, SLOT( map() ) );
00240     mapper->setMapping( line, ++lastLineId );
00241     line->setShown( true );
00242 }
00243 
00244 void KPIM::DistributionListEditor::EditorWidget::slotOk()
00245 {
00246     const QString name = d->nameLineEdit->text();
00247     const KPIM::DistributionList existing = KPIM::DistributionList::findByName( d->addressBook, name );
00248     if ( !existing.isEmpty() && existing.uid() != d->distListUid )
00249     {
00250         KMessageBox::error( this, i18n( "A distribution list with the name %1 already exists. Please choose another name." ).arg( name ), i18n( "Name in Use" ) ); 
00251         return;
00252     }
00253 
00254     KPIM::DistributionList list;
00255     list.setUid( d->distListUid.isNull() ? KApplication::randomString( 10 ) :d->distListUid );
00256     list.setName( name );
00257     typedef QValueList<KPIM::DistributionListEditor::Line*>::ConstIterator ListIterator;
00258     for ( ListIterator it = d->addressees.begin(), end = d->addressees.end(); it != end; ++it )
00259     { 
00260         const KPIM::DistributionList::Entry entry = (*it)->entry();
00261         if ( entry.addressee.isEmpty() )
00262             continue;
00263         list.insertEntry( entry.addressee, entry.email );
00264     }
00265     d->distributionList = list;
00266     accept();
00267 }
00268 
00269 KPIM::DistributionList KPIM::DistributionListEditor::EditorWidget::distributionList() const
00270 {
00271     return d->distributionList;
00272 }
00273 
00274 #include "distributionlisteditor.moc"
00275 #include "distributionlisteditor_p.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys