• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

emaileditwidget.cpp
00001 /*
00002     This file is part of Akonadi Contact.
00003 
00004     Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     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 the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
00020 */
00021 
00022 #include "emaileditwidget.h"
00023 
00024 #include "autoqpointer_p.h"
00025 
00026 #include <QtCore/QEvent>
00027 #include <QtCore/QString>
00028 #include <QtGui/QCheckBox>
00029 #include <QtGui/QGridLayout>
00030 #include <QtGui/QLabel>
00031 #include <QtGui/QPushButton>
00032 #include <QtGui/QToolButton>
00033 
00034 #include <kabc/addressee.h>
00035 #include <kacceleratormanager.h>
00036 #include <kinputdialog.h>
00037 #include <klineedit.h>
00038 #include <KListWidget>
00039 #include <klocale.h>
00040 #include <kmessagebox.h>
00041 #include <kpimutils/email.h>
00042 
00043 class EmailAddressExtracter : public QObject
00044 {
00045   public:
00046     EmailAddressExtracter( KLineEdit *lineEdit )
00047       : QObject( lineEdit ), mLineEdit( lineEdit )
00048     {
00049       lineEdit->installEventFilter( this );
00050     }
00051 
00052     virtual bool eventFilter( QObject *watched, QEvent *event )
00053     {
00054       if ( watched == mLineEdit && event->type() == QEvent::FocusOut ) {
00055         const QString fullEmailAddress = mLineEdit->text();
00056         const QString extractedEmailAddress = KPIMUtils::extractEmailAddress( fullEmailAddress );
00057         mLineEdit->setText( extractedEmailAddress );
00058       }
00059 
00060       return QObject::eventFilter( watched, event );
00061     }
00062 
00063   private:
00064     KLineEdit *mLineEdit;
00065     bool mIgnoreFocusOutEvent;
00066 };
00067 
00068 class EmailItem : public QListWidgetItem
00069 {
00070   public:
00071     EmailItem( const QString &text, QListWidget *parent, bool preferred )
00072       : QListWidgetItem( text, parent ), mPreferred( preferred )
00073     {
00074       format();
00075     }
00076 
00077     void setPreferred( bool preferred ) { mPreferred = preferred; format(); }
00078     bool preferred() const { return mPreferred; }
00079 
00080   private:
00081     void format()
00082     {
00083       QFont f = font();
00084       f.setBold( mPreferred );
00085       setFont( f );
00086     }
00087 
00088   private:
00089     bool mPreferred;
00090 };
00091 
00092 EmailEditWidget::EmailEditWidget( QWidget *parent )
00093   : QWidget( parent )
00094 {
00095   QHBoxLayout *layout = new QHBoxLayout( this );
00096   layout->setMargin( 0 );
00097   layout->setSpacing( KDialog::spacingHint() );
00098 
00099   mEmailEdit = new KLineEdit;
00100   new EmailAddressExtracter( mEmailEdit );
00101   connect( mEmailEdit, SIGNAL(textChanged(QString)),
00102            SLOT(textChanged(QString)) );
00103   layout->addWidget( mEmailEdit );
00104 
00105   mEditButton = new QToolButton;
00106   mEditButton->setText( QLatin1String( "..." ) );
00107   connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
00108   layout->addWidget( mEditButton );
00109 }
00110 
00111 EmailEditWidget::~EmailEditWidget()
00112 {
00113 }
00114 
00115 void EmailEditWidget::setReadOnly( bool readOnly )
00116 {
00117   mEmailEdit->setReadOnly( readOnly );
00118   mEditButton->setEnabled( !readOnly );
00119 }
00120 
00121 void EmailEditWidget::loadContact( const KABC::Addressee &contact )
00122 {
00123   mEmailList = contact.emails();
00124 
00125   if ( !mEmailList.isEmpty() )
00126     mEmailEdit->setText( mEmailList.first() );
00127   else
00128     mEmailEdit->setText( QString() );
00129 }
00130 
00131 void EmailEditWidget::storeContact( KABC::Addressee &contact ) const
00132 {
00133   QStringList emails( mEmailList );
00134 
00135   // the preferred address is always the first one, remove it...
00136   if ( !emails.isEmpty() )
00137     emails.removeFirst();
00138 
00139   // ... and prepend the one from the line edit
00140   if ( !mEmailEdit->text().isEmpty() )
00141     emails.prepend( mEmailEdit->text() );
00142 
00143   contact.setEmails( emails );
00144 }
00145 
00146 void EmailEditWidget::edit()
00147 {
00148   AutoQPointer<EmailEditDialog> dlg = new EmailEditDialog( mEmailList, this );
00149 
00150   if ( dlg->exec() ) {
00151     if ( dlg->changed() ) {
00152       mEmailList = dlg->emails();
00153       if ( !mEmailList.isEmpty() )
00154         mEmailEdit->setText( mEmailList.first() );
00155       else
00156         mEmailEdit->setText( QString() );
00157     }
00158   }
00159 }
00160 
00161 void EmailEditWidget::textChanged( const QString &text )
00162 {
00163   if ( !mEmailList.isEmpty() )
00164     mEmailList.removeFirst();
00165 
00166   mEmailList.prepend( text );
00167 }
00168 
00169 
00170 EmailEditDialog::EmailEditDialog( const QStringList &list, QWidget *parent )
00171   : KDialog( parent )
00172 {
00173   setCaption( i18n( "Edit Email Addresses" ) );
00174   setButtons( KDialog::Ok | KDialog::Cancel );
00175   setDefaultButton( KDialog::Cancel );
00176 
00177   QWidget *page = new QWidget( this);
00178   setMainWidget( page );
00179 
00180   QGridLayout *topLayout = new QGridLayout( page );
00181   topLayout->setSpacing( spacingHint() );
00182   topLayout->setMargin( 0 );
00183 
00184   mEmailListBox = new KListWidget( page );
00185   mEmailListBox->setSelectionMode( QAbstractItemView::SingleSelection );
00186 
00187   // Make sure there is room for the scrollbar
00188   mEmailListBox->setMinimumHeight( mEmailListBox->sizeHint().height() + 30 );
00189   connect( mEmailListBox, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
00190            SLOT(selectionChanged()) );
00191   connect( mEmailListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
00192            SLOT(edit()) );
00193   topLayout->addWidget( mEmailListBox, 0, 0, 5, 2 );
00194 
00195   mAddButton = new QPushButton( i18n( "Add..." ), page );
00196   connect( mAddButton, SIGNAL(clicked()), SLOT(add()) );
00197   topLayout->addWidget( mAddButton, 0, 2 );
00198 
00199   mEditButton = new QPushButton( i18n( "Edit..." ), page );
00200   mEditButton->setEnabled( false );
00201   connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
00202   topLayout->addWidget( mEditButton, 1, 2 );
00203 
00204   mRemoveButton = new QPushButton( i18n( "Remove" ), page );
00205   mRemoveButton->setEnabled( false );
00206   connect( mRemoveButton, SIGNAL(clicked()), SLOT(remove()) );
00207   topLayout->addWidget( mRemoveButton, 2, 2 );
00208 
00209   mStandardButton = new QPushButton( i18n( "Set as Standard" ), page );
00210   mStandardButton->setEnabled( false );
00211   connect( mStandardButton, SIGNAL(clicked()), SLOT(standard()) );
00212   topLayout->addWidget( mStandardButton, 3, 2 );
00213 
00214   topLayout->setRowStretch( 4, 1 );
00215 
00216   QStringList items = list;
00217   if ( items.removeAll( QLatin1String( "" ) ) > 0 )
00218     mChanged = true;
00219   else
00220     mChanged = false;
00221 
00222   QStringList::ConstIterator it;
00223   bool preferred = true;
00224   for ( it = items.constBegin(); it != items.constEnd(); ++it ) {
00225     new EmailItem( *it, mEmailListBox, preferred );
00226     preferred = false;
00227   }
00228 
00229   // set default state
00230   KAcceleratorManager::manage( this );
00231 
00232   setInitialSize( QSize( 400, 200 ) );
00233 }
00234 
00235 EmailEditDialog::~EmailEditDialog()
00236 {
00237 }
00238 
00239 QStringList EmailEditDialog::emails() const
00240 {
00241   QStringList emails;
00242 
00243   for ( int i = 0; i < mEmailListBox->count(); ++i ) {
00244     EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
00245     if ( item->preferred() )
00246       emails.prepend( item->text() );
00247     else
00248       emails.append( item->text() );
00249   }
00250 
00251   return emails;
00252 }
00253 
00254 void EmailEditDialog::add()
00255 {
00256   bool ok = false;
00257 
00258   QString email = KInputDialog::getText( i18n( "Add Email" ), i18n( "New Email:" ),
00259                                          QString(), &ok, this );
00260 
00261   if ( !ok )
00262     return;
00263 
00264   email = KPIMUtils::extractEmailAddress( email );
00265 
00266   // check if item already available, ignore if so...
00267   for ( int i = 0; i < mEmailListBox->count(); ++i ) {
00268     if ( mEmailListBox->item( i )->text() == email )
00269       return;
00270   }
00271 
00272   new EmailItem( email, mEmailListBox, (mEmailListBox->count() == 0) );
00273 
00274   mChanged = true;
00275 }
00276 
00277 void EmailEditDialog::edit()
00278 {
00279   bool ok = false;
00280 
00281   QListWidgetItem *item = mEmailListBox->currentItem();
00282 
00283   QString email = KInputDialog::getText( i18n( "Edit Email" ),
00284                                          i18nc( "@label:textbox Inputfield for an email address", "Email:" ),
00285                                          item->text(), &ok, this );
00286 
00287   if ( !ok )
00288     return;
00289 
00290   email = KPIMUtils::extractEmailAddress( email );
00291 
00292   // check if item already available, ignore if so...
00293   for ( int i = 0; i < mEmailListBox->count(); ++i ) {
00294     if ( mEmailListBox->item( i )->text() == email )
00295       return;
00296   }
00297 
00298   EmailItem *eitem = static_cast<EmailItem*>( item );
00299   eitem->setText( email );
00300 
00301   mChanged = true;
00302 }
00303 
00304 void EmailEditDialog::remove()
00305 {
00306   const QString address = mEmailListBox->currentItem()->text();
00307 
00308   const QString text = i18n( "<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>", address );
00309   const QString caption = i18n( "Confirm Remove" );
00310 
00311   if ( KMessageBox::warningContinueCancel( this, text, caption, KGuiItem( i18n( "&Delete" ), QLatin1String( "edit-delete" ) ) ) == KMessageBox::Continue ) {
00312     EmailItem *item = static_cast<EmailItem*>( mEmailListBox->currentItem() );
00313 
00314     const bool preferred = item->preferred();
00315     mEmailListBox->takeItem( mEmailListBox->currentRow() );
00316     if ( preferred ) {
00317       item = dynamic_cast<EmailItem*>( mEmailListBox->item( 0 ) );
00318       if ( item )
00319         item->setPreferred( true );
00320     }
00321 
00322     mChanged = true;
00323   }
00324 }
00325 
00326 bool EmailEditDialog::changed() const
00327 {
00328   return mChanged;
00329 }
00330 
00331 void EmailEditDialog::standard()
00332 {
00333   for ( int i = 0; i < mEmailListBox->count(); ++i ) {
00334     EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
00335     if ( i == mEmailListBox->currentRow() )
00336       item->setPreferred( true );
00337     else
00338       item->setPreferred( false );
00339   }
00340 
00341   mChanged = true;
00342 }
00343 
00344 void EmailEditDialog::selectionChanged()
00345 {
00346   int index = mEmailListBox->currentRow();
00347   bool value = ( index >= 0 ); // An item is selected
00348 
00349   mRemoveButton->setEnabled( value );
00350   mEditButton->setEnabled( value );
00351   mStandardButton->setEnabled( value );
00352 }
00353 
00354 #include "emaileditwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal