kaddressbook
distributionlisteditor.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "distributionlisteditor.h"
00024 #include "distributionlisteditor_p.h"
00025
00026 #include <libkdepim/addresseelineedit.h>
00027 #include <libkdepim/distributionlist.h>
00028
00029 #include <kpimutils/email.h>
00030
00031 #include <kabc/addressbook.h>
00032 #include <kglobal.h>
00033 #include <kiconloader.h>
00034 #include <klineedit.h>
00035 #include <klocale.h>
00036 #include <kmessagebox.h>
00037 #include <KRandom>
00038
00039 #include <QtCore/QSignalMapper>
00040 #include <QtGui/QApplication>
00041 #include <QtGui/QLabel>
00042 #include <QtGui/QLayout>
00043 #include <QtGui/QScrollArea>
00044
00045 class KPIM::DistributionListEditor::EditorWidgetPrivate
00046 {
00047 public:
00048 QScrollArea* 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 QList<KPIM::DistributionListEditor::Line*> addressees;
00058 KPIM::DistributionList distributionList;
00059 KPIM::DistributionListEditor::Line* 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 m_lineEdit->setClearButtonShown( true );
00071 connect( m_lineEdit, SIGNAL( textChanged( const QString& ) ),
00072 this, SLOT( textChanged( const QString& ) ) );
00073 layout->addWidget( m_lineEdit );
00074 }
00075
00076 void KPIM::DistributionListEditor::Line::textChanged( const QString& text )
00077 {
00078 if ( text.isEmpty() )
00079 emit cleared();
00080 emit textChanged();
00081 }
00082
00083 void KPIM::DistributionListEditor::Line::setFocusToLineEdit()
00084 {
00085 m_lineEdit->setFocus();
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
00113 KABC::Addressee addressee;
00114 addressee.setUid( KRandom::randomString( 10 ) );
00115 addressee.setFormattedName( name );
00116 addressee.setEmails( QStringList( 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 KPIMUtils::extractEmailAddressAndName(m_lineEdit->text(), email, name );
00127 KPIM::DistributionList::Entry res;
00128 if ( !m_uid.isNull() )
00129 {
00130 const KABC::Addressee addr = m_addressBook->findByUid( m_uid );
00131 if ( m_initialText == text || addr.formattedName() == name )
00132 res.addressee = addr;
00133 }
00134 if ( res.addressee.isEmpty() )
00135 res.addressee = findAddressee( name, email );
00136 res.email = res.addressee.preferredEmail() != email ? email : QString();
00137 return res;
00138 }
00139
00140
00141 KPIM::DistributionListEditor::LineEdit::LineEdit( QWidget* parent ) : KPIM::AddresseeLineEdit( parent )
00142 {
00143 }
00144
00145 void KPIM::DistributionListEditor::EditorWidget::slotButtonClicked( int button )
00146 {
00147 if ( button == Ok )
00148 saveList();
00149
00150 KDialog::slotButtonClicked( button );
00151 }
00152
00153 KPIM::DistributionListEditor::EditorWidget::EditorWidget( KABC::AddressBook* book, QWidget* parent )
00154 : KDialog( parent ), d( new DistributionListEditor::EditorWidgetPrivate )
00155 {
00156 d->addressBook = book;
00157 Q_ASSERT( d->addressBook );
00158 d->lastLineId = 0;
00159 d->mapper = new QSignalMapper( this );
00160 connect( d->mapper, SIGNAL( mapped( int ) ),
00161 this, SLOT( lineTextChanged( int ) ) );
00162 showButton( Ok, true );
00163 showButton( Cancel, true );
00164 setModal( true );
00165 setWindowTitle( i18n( "Edit Distribution List" ) );
00166 QWidget* main = new QWidget( this );
00167 QVBoxLayout* mainLayout = new QVBoxLayout( main );
00168 mainLayout->setMargin( KDialog::marginHint() );
00169 mainLayout->setSpacing( KDialog::spacingHint() );
00170
00171 QWidget* nameWidget = new QWidget;
00172 QHBoxLayout* nameLayout = new QHBoxLayout( nameWidget );
00173 nameLayout->setSpacing( KDialog::spacingHint() );
00174 d->nameLabel = new QLabel;
00175 d->nameLabel->setText( i18n( "Name:" ) );
00176 nameLayout->addWidget( d->nameLabel );
00177
00178 d->nameLineEdit = new KLineEdit;
00179 d->nameLabel->setBuddy( d->nameLineEdit );
00180 nameLayout->addWidget( d->nameLineEdit );
00181
00182 mainLayout->addWidget( nameWidget );
00183 mainLayout->addSpacing( 30 );
00184
00185 d->memberListLabel = new QLabel;
00186 d->memberListLabel->setText( i18n( "Distribution list members:" ) );
00187 mainLayout->addWidget( d->memberListLabel );
00188
00189 d->scrollView = new QScrollArea;
00190 d->scrollView->setFrameShape( QFrame::NoFrame );
00191 d->scrollView->setWidgetResizable( true );
00192 mainLayout->addWidget( d->scrollView );
00193
00194 d->memberListWidget = new QWidget( this );
00195 d->scrollView->setWidget( d->memberListWidget );
00196 d->memberListWidget->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
00197 QVBoxLayout* memberLayout = new QVBoxLayout( d->memberListWidget );
00198 QWidget* addresseeWidget = new QWidget;
00199 d->addresseeLayout = new QVBoxLayout( addresseeWidget );
00200 memberLayout->addWidget( addresseeWidget );
00201 memberLayout->addStretch();
00202
00203 setMainWidget( main );
00204
00205 const QSize hint = sizeHint();
00206 resize( hint.width() * 1.5, hint.height() * 1.5 );
00207 }
00208
00209 KPIM::DistributionListEditor::EditorWidget::~EditorWidget()
00210 {
00211 delete d;
00212 }
00213
00214 void KPIM::DistributionListEditor::EditorWidget::lineTextChanged( int id )
00215 {
00216 if ( id != d->lastLineId )
00217 return;
00218 d->addLineForEntry( KPIM::DistributionList::Entry() );
00219 }
00220
00221 void KPIM::DistributionListEditor::EditorWidget::setDistributionList( const KPIM::DistributionList& list )
00222 {
00223 d->distListUid = list.uid();
00224 d->nameLineEdit->setText( list.name() );
00225
00226 using KPIM::DistributionListEditor::Line;
00227 qDeleteAll( d->addressees );
00228 d->addressees.clear();
00229
00230 typedef KPIM::DistributionList::Entry Entry;
00231 const Entry::List entries = list.entries( d->addressBook );
00232
00233 Q_FOREACH( const KPIM::DistributionList::Entry& i, entries )
00234 d->addLineForEntry( i );
00235 KPIM::DistributionListEditor::Line* const last = d->addLineForEntry( Entry() );
00236 last->setFocusToLineEdit();
00237 }
00238
00239 KPIM::DistributionListEditor::Line* KPIM::DistributionListEditor::EditorWidgetPrivate::addLineForEntry( const KPIM::DistributionList::Entry& entry )
00240 {
00241 KPIM::DistributionListEditor::Line* line = new KPIM::DistributionListEditor::Line( addressBook );
00242 line->setEntry( entry );
00243 addresseeLayout->addWidget( line );
00244 addressees.append( line );
00245 QObject::connect( line, SIGNAL( textChanged() ),
00246 mapper, SLOT( map() ) );
00247 mapper->setMapping( line, ++lastLineId );
00248 line->setVisible( true );
00249 return line;
00250 }
00251
00252 void KPIM::DistributionListEditor::EditorWidget::saveList()
00253 {
00254 const QString name = d->nameLineEdit->text();
00255 const KPIM::DistributionList existing = KPIM::DistributionList::findByName( d->addressBook, name );
00256 if ( !existing.isEmpty() && existing.uid() != d->distListUid )
00257 {
00258 KMessageBox::error( this, i18n( "A distribution list with the name %1 already exists. Please choose another name.", name ), i18n( "Name in Use" ) );
00259 return;
00260 }
00261
00262 KPIM::DistributionList list;
00263 list.setUid( d->distListUid.isNull() ? KRandom::randomString( 10 ) :d->distListUid );
00264 list.setName( name );
00265 foreach ( const KPIM::DistributionListEditor::Line* i, d->addressees )
00266 {
00267 const KPIM::DistributionList::Entry entry = i->entry();
00268 if ( entry.addressee.isEmpty() )
00269 continue;
00270 list.insertEntry( entry.addressee, entry.email );
00271 }
00272 d->distributionList = list;
00273 }
00274
00275 KPIM::DistributionList KPIM::DistributionListEditor::EditorWidget::distributionList() const
00276 {
00277 return d->distributionList;
00278 }
00279
00280 #include "distributionlisteditor.moc"
00281 #include "distributionlisteditor_p.moc"