kaddressbook Library API Documentation

emaileditwidget.cpp

00001 /*                                                                      
00002     This file is part of KAddressBook.                                  
00003     Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
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., 675 Mass Ave, Cambridge, MA 02139, USA.           
00018                                                                         
00019     As a special exception, permission is given to link this program    
00020     with any edition of Qt, and distribute the resulting executable,    
00021     without including the source code for Qt in the source distribution.
00022 */                                                                      
00023 
00024 #include <qlayout.h>
00025 #include <qlabel.h>
00026 #include <qtoolbutton.h>
00027 #include <qtooltip.h>
00028 #include <qpushbutton.h>
00029 #include <qcheckbox.h>
00030 #include <qstring.h>
00031 
00032 #include <kaccelmanager.h>
00033 #include <kconfig.h>
00034 #include <klineedit.h>
00035 #include <kcombobox.h>
00036 #include <klocale.h>
00037 #include <kdebug.h>
00038 #include <kiconloader.h>
00039 #include <kmessagebox.h>
00040 #include <kdialog.h>
00041 
00042 #include "emaileditwidget.h"
00043 
00044 EmailEditWidget::EmailEditWidget(QWidget *parent, const char *name)
00045   : QWidget(parent, name)
00046 {
00047   QGridLayout *topLayout = new QGridLayout(this, 2, 2);
00048   topLayout->setSpacing( KDialog::spacingHint() );
00049 
00050   QLabel *label = new QLabel(i18n("Email:"), this);
00051   topLayout->addWidget(label, 0, 0);
00052 
00053   mEmailEdit = new KLineEdit(this);
00054   connect(mEmailEdit, SIGNAL( textChanged(const QString &) ), SLOT( textChanged(const QString&) ) );
00055   connect(mEmailEdit, SIGNAL( textChanged(const QString &) ), SIGNAL( modified() ) );
00056   label->setBuddy( mEmailEdit );
00057   topLayout->addWidget(mEmailEdit, 0, 1);
00058 
00059   QPushButton *editButton = new QPushButton(i18n("Edit Email Addresses..."), this);
00060   connect(editButton, SIGNAL(clicked()), SLOT(edit()));
00061   topLayout->addMultiCellWidget(editButton, 1, 1, 0, 1);
00062 
00063   topLayout->activate();
00064 }
00065 
00066 EmailEditWidget::~EmailEditWidget()
00067 {
00068 }
00069     
00070 void EmailEditWidget::setEmails(const QStringList &list)
00071 {
00072   mEmailList = list;
00073 
00074   bool blocked = mEmailEdit->signalsBlocked();
00075   mEmailEdit->blockSignals( true );
00076   if ( list.count() > 0 )
00077     mEmailEdit->setText( list[ 0 ] );
00078   else
00079     mEmailEdit->setText( "" );
00080   mEmailEdit->blockSignals( blocked );
00081 }
00082 
00083 QStringList EmailEditWidget::emails()
00084 {
00085   if ( mEmailEdit->text().isEmpty() ) {
00086     if ( mEmailList.count() > 0 )
00087       mEmailList.remove( mEmailList.begin() );
00088   } else {
00089     if ( mEmailList.count() > 0 )
00090       mEmailList.remove( mEmailList.begin() );
00091 
00092     mEmailList.prepend( mEmailEdit->text() );
00093   }
00094 
00095   return mEmailList;
00096 }
00097 
00098 void EmailEditWidget::edit()
00099 {
00100   EmailEditDialog dlg( mEmailList, this );
00101   
00102   if ( dlg.exec() ) {
00103     if ( dlg.changed() ) {
00104       mEmailList = dlg.emails();
00105       mEmailEdit->setText( mEmailList[ 0 ] );
00106       emit modified();
00107     }
00108   }
00109 }
00110 
00111 void EmailEditWidget::textChanged( const QString &text )
00112 {
00113   if ( mEmailList.count() > 0 )
00114     mEmailList.remove( mEmailList.begin() );
00115 
00116   mEmailList.prepend( text );
00117 }
00118 
00119 
00120 EmailEditDialog::EmailEditDialog( const QStringList &list, QWidget *parent, const char *name )
00121   : KDialogBase( KDialogBase::Plain, i18n( "Edit Email Addresses" ),
00122                 KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok,
00123                 parent, name, true)
00124 {
00125   QWidget *page = plainPage();
00126 
00127   QGridLayout *topLayout = new QGridLayout( page, 4, 3 );
00128 
00129   QLabel *label = new QLabel(i18n("Email address:"), page);
00130   topLayout->addWidget(label, 0, 0);
00131 
00132   mEmailEdit = new KLineEdit(page);
00133   label->setBuddy( mEmailEdit );
00134   topLayout->addWidget(mEmailEdit, 0, 1);
00135   connect(mEmailEdit, SIGNAL(returnPressed()), SLOT(add()));
00136   connect(mEmailEdit, SIGNAL(textChanged(const QString&)), SLOT(emailChanged()));
00137 
00138   mAddButton = new QPushButton( i18n("Add"), page );
00139   mAddButton->setEnabled( false );
00140   connect(mAddButton, SIGNAL(clicked()), SLOT(add()));
00141   topLayout->addWidget(mAddButton, 0, 2);
00142 
00143   mEmailListBox = new QListBox( page );
00144 
00145   // Make sure there is room for the scrollbar
00146   mEmailListBox->setMinimumHeight(mEmailListBox->sizeHint().height() + 30);
00147   connect(mEmailListBox, SIGNAL(highlighted(int)), 
00148           SLOT(selectionChanged(int)));
00149   topLayout->addMultiCellWidget(mEmailListBox, 1, 3, 0, 1);
00150   
00151   mEditButton = new QPushButton(i18n("Change"), page);
00152   connect(mEditButton, SIGNAL(clicked()), SLOT(edit()));
00153   topLayout->addWidget(mEditButton, 1, 2);
00154 
00155   mRemoveButton = new QPushButton(i18n("Remove"), page);
00156   connect(mRemoveButton, SIGNAL(clicked()), SLOT(remove()));
00157   topLayout->addWidget(mRemoveButton, 2, 2);
00158 
00159   mStandardButton = new QPushButton(i18n("Set Standard"), page);
00160   connect(mStandardButton, SIGNAL(clicked()), SLOT(standard()));
00161   topLayout->addWidget(mStandardButton, 3, 2);
00162 
00163   topLayout->activate();
00164   
00165   QStringList items = list;
00166   if ( items.remove( "" ) > 0 )
00167     mChanged = true;
00168   else
00169     mChanged = false;
00170   mEmailListBox->insertStringList( items );
00171 
00172   // set default state
00173   selectionChanged(-1);
00174 
00175   KAcceleratorManager::manage( this );
00176 }
00177 
00178 EmailEditDialog::~EmailEditDialog()
00179 {
00180 }
00181     
00182 QStringList EmailEditDialog::emails() const
00183 {
00184   QStringList emails;
00185   
00186   for (unsigned int i = 0; i < mEmailListBox->count(); ++i)
00187     emails << mEmailListBox->text(i);
00188   
00189   return emails;
00190 }
00191 
00192 void EmailEditDialog::add()
00193 {
00194   mEmailListBox->insertItem(mEmailEdit->text());
00195 
00196   mEmailEdit->clear();
00197   mEmailEdit->setFocus();
00198 
00199   mChanged = true;
00200 }
00201 
00202 void EmailEditDialog::edit()
00203 {
00204   mEmailEdit->setText(mEmailListBox->currentText());
00205   mEmailEdit->setFocus();
00206 }
00207 
00208 void EmailEditDialog::remove()
00209 {
00210   QString address = mEmailListBox->currentText();
00211   
00212   QString text = i18n("Are you sure that you want to remove the email address \"%1\"?").arg( address );
00213   
00214   QString caption = i18n("Confirm Remove");
00215   
00216   if (KMessageBox::questionYesNo(this, text, caption) == KMessageBox::Yes) {
00217     mEmailListBox->removeItem(mEmailListBox->currentItem());
00218     mChanged = true;
00219   }
00220 }
00221 
00222 bool EmailEditDialog::changed() const
00223 {
00224   return mChanged;
00225 }
00226 
00227 void EmailEditDialog::standard()
00228 {
00229   QString text = mEmailListBox->currentText();
00230   mEmailListBox->removeItem(mEmailListBox->currentItem());
00231   mEmailListBox->insertItem(text, 0);
00232   mEmailListBox->setSelected(0, true);
00233 
00234   mChanged = true;
00235 }
00236 
00237 void EmailEditDialog::selectionChanged(int index)
00238 {
00239   bool value = (index >= 0); // An item is selected
00240 
00241   mRemoveButton->setEnabled(value);
00242   mEditButton->setEnabled(value);
00243   mStandardButton->setEnabled(value);
00244 }
00245 
00246 void EmailEditDialog::emailChanged()
00247 {
00248   mAddButton->setEnabled( !mEmailEdit->text().isEmpty() );
00249 }
00250 
00251 #include "emaileditwidget.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:09 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001