22 #include "emaileditwidget.h"
24 #include "autoqpointer_p.h"
26 #include <QtCore/QEvent>
27 #include <QtCore/QString>
28 #include <QGridLayout>
30 #include <QPushButton>
31 #include <QToolButton>
33 #include <kabc/addressee.h>
34 #include <kacceleratormanager.h>
35 #include <kinputdialog.h>
36 #include <klineedit.h>
37 #include <KListWidget>
38 #include <klocalizedstring.h>
39 #include <kmessagebox.h>
40 #include <kpimutils/email.h>
42 class EmailAddressExtracter :
public QObject
45 EmailAddressExtracter( KLineEdit *lineEdit )
46 : QObject( lineEdit ), mLineEdit( lineEdit )
48 lineEdit->installEventFilter(
this );
51 virtual bool eventFilter( QObject *watched, QEvent *event )
53 if ( watched == mLineEdit && event->type() == QEvent::FocusOut ) {
54 const QString fullEmailAddress = mLineEdit->text();
55 const QString extractedEmailAddress = KPIMUtils::extractEmailAddress( fullEmailAddress );
56 mLineEdit->setText( extractedEmailAddress );
59 return QObject::eventFilter( watched, event );
66 class EmailItem :
public QListWidgetItem
69 EmailItem(
const QString &text, QListWidget *parent,
bool preferred )
70 : QListWidgetItem( text, parent ), mPreferred( preferred )
75 void setPreferred(
bool preferred ) { mPreferred = preferred; format(); }
76 bool preferred()
const {
return mPreferred; }
82 f.setBold( mPreferred );
90 EmailEditWidget::EmailEditWidget( QWidget *parent )
93 QHBoxLayout *layout =
new QHBoxLayout(
this );
94 layout->setMargin( 0 );
95 layout->setSpacing( KDialog::spacingHint() );
97 mEmailEdit =
new KLineEdit;
98 new EmailAddressExtracter( mEmailEdit );
99 connect( mEmailEdit, SIGNAL(textChanged(QString)),
100 SLOT(textChanged(QString)) );
101 layout->addWidget( mEmailEdit );
103 mEditButton =
new QToolButton;
104 mEditButton->setText( QLatin1String(
"..." ) );
105 connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
106 layout->addWidget( mEditButton );
107 setFocusProxy( mEditButton );
108 setFocusPolicy( Qt::StrongFocus );
111 EmailEditWidget::~EmailEditWidget()
115 void EmailEditWidget::setReadOnly(
bool readOnly )
117 mEmailEdit->setReadOnly( readOnly );
118 mEditButton->setEnabled( !readOnly );
121 void EmailEditWidget::loadContact(
const KABC::Addressee &contact )
123 mEmailList = contact.emails();
125 if ( !mEmailList.isEmpty() ) {
126 mEmailEdit->setText( mEmailList.first() );
128 mEmailEdit->setText( QString() );
132 void EmailEditWidget::storeContact( KABC::Addressee &contact )
const
134 QStringList emails( mEmailList );
137 if ( !emails.isEmpty() ) {
138 emails.removeFirst();
142 if ( !mEmailEdit->text().isEmpty() ) {
143 emails.prepend( mEmailEdit->text().toLower() );
146 contact.setEmails( emails );
149 void EmailEditWidget::edit()
154 if ( dlg->changed() ) {
155 mEmailList = dlg->emails();
156 if ( !mEmailList.isEmpty() ) {
157 mEmailEdit->setText( mEmailList.first() );
159 mEmailEdit->setText( QString() );
165 void EmailEditWidget::textChanged(
const QString &text )
167 if ( !mEmailList.isEmpty() ) {
168 mEmailList.removeFirst();
171 mEmailList.prepend( text );
174 EmailEditDialog::EmailEditDialog(
const QStringList &list, QWidget *parent )
177 setCaption( i18n(
"Edit Email Addresses" ) );
178 setButtons( KDialog::Ok | KDialog::Cancel );
179 setDefaultButton( KDialog::Cancel );
181 QWidget *page =
new QWidget(
this );
182 setMainWidget( page );
184 QGridLayout *topLayout =
new QGridLayout( page );
185 topLayout->setSpacing( spacingHint() );
186 topLayout->setMargin( 0 );
188 mEmailListBox =
new KListWidget( page );
189 mEmailListBox->setSelectionMode( QAbstractItemView::SingleSelection );
192 mEmailListBox->setMinimumHeight( mEmailListBox->sizeHint().height() + 30 );
193 connect( mEmailListBox, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
194 SLOT(selectionChanged()) );
195 connect( mEmailListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
197 topLayout->addWidget( mEmailListBox, 0, 0, 5, 2 );
199 mAddButton =
new QPushButton( i18n(
"Add..." ), page );
200 connect( mAddButton, SIGNAL(clicked()), SLOT(add()) );
201 topLayout->addWidget( mAddButton, 0, 2 );
203 mEditButton =
new QPushButton( i18n(
"Edit..." ), page );
204 mEditButton->setEnabled(
false );
205 connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
206 topLayout->addWidget( mEditButton, 1, 2 );
208 mRemoveButton =
new QPushButton( i18n(
"Remove" ), page );
209 mRemoveButton->setEnabled(
false );
210 connect( mRemoveButton, SIGNAL(clicked()), SLOT(
remove()) );
211 topLayout->addWidget( mRemoveButton, 2, 2 );
213 mStandardButton =
new QPushButton( i18n(
"Set as Standard" ), page );
214 mStandardButton->setEnabled(
false );
215 connect( mStandardButton, SIGNAL(clicked()), SLOT(standard()) );
216 topLayout->addWidget( mStandardButton, 3, 2 );
218 topLayout->setRowStretch( 4, 1 );
220 QStringList items = list;
221 if ( items.removeAll( QLatin1String(
"" ) ) > 0 ) {
227 QStringList::ConstIterator it;
228 bool preferred =
true;
229 for ( it = items.constBegin(); it != items.constEnd(); ++it ) {
230 new EmailItem( *it, mEmailListBox, preferred );
235 KAcceleratorManager::manage(
this );
240 EmailEditDialog::~EmailEditDialog()
245 void EmailEditDialog::readConfig()
247 KConfigGroup group( KGlobal::config(),
"EmailEditDialog" );
248 const QSize sizeDialog = group.readEntry(
"Size", QSize(400,200) );
249 if ( sizeDialog.isValid() ) {
250 resize( sizeDialog );
254 void EmailEditDialog::writeConfig()
256 KConfigGroup group( KGlobal::config(),
"EmailEditDialog" );
257 group.writeEntry(
"Size", size() );
260 QStringList EmailEditDialog::emails()
const
264 for (
int i = 0; i < mEmailListBox->count(); ++i ) {
265 EmailItem *item =
static_cast<EmailItem*
>( mEmailListBox->item( i ) );
266 if ( item->preferred() ) {
267 emails.prepend( item->text() );
269 emails.append( item->text() );
276 void EmailEditDialog::add()
280 QString email = KInputDialog::getText( i18n(
"Add Email" ), i18n(
"New Email:" ),
281 QString(), &ok,
this );
287 email = KPIMUtils::extractEmailAddress( email.toLower() );
290 for (
int i = 0; i < mEmailListBox->count(); ++i ) {
291 if ( mEmailListBox->item( i )->text() == email ) {
296 new EmailItem( email, mEmailListBox, ( mEmailListBox->count() == 0 ) );
301 void EmailEditDialog::edit()
305 QListWidgetItem *item = mEmailListBox->currentItem();
307 QString email = KInputDialog::getText( i18n(
"Edit Email" ),
308 i18nc(
"@label:textbox Inputfield for an email address",
"Email:" ),
309 item->text(), &ok, this );
315 email = KPIMUtils::extractEmailAddress( email.toLower() );
318 for (
int i = 0; i < mEmailListBox->count(); ++i ) {
319 if ( mEmailListBox->item( i )->text() == email ) {
324 EmailItem *eitem =
static_cast<EmailItem*
>( item );
325 eitem->setText( email );
330 void EmailEditDialog::remove()
332 const QString address = mEmailListBox->currentItem()->text();
334 const QString text = i18n(
"<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>", address );
335 const QString caption = i18n(
"Confirm Remove" );
337 if ( KMessageBox::warningContinueCancel(
this, text, caption, KGuiItem( i18n(
"&Delete" ), QLatin1String(
"edit-delete" ) ) ) == KMessageBox::Continue ) {
338 EmailItem *item =
static_cast<EmailItem*
>( mEmailListBox->currentItem() );
340 const bool preferred = item->preferred();
341 mEmailListBox->takeItem( mEmailListBox->currentRow() );
343 item =
dynamic_cast<EmailItem*
>( mEmailListBox->item( 0 ) );
345 item->setPreferred(
true );
353 bool EmailEditDialog::changed()
const
358 void EmailEditDialog::standard()
360 for (
int i = 0; i < mEmailListBox->count(); ++i ) {
361 EmailItem *item =
static_cast<EmailItem*
>( mEmailListBox->item( i ) );
362 if ( i == mEmailListBox->currentRow() ) {
363 item->setPreferred(
true );
365 item->setPreferred(
false );
372 void EmailEditDialog::selectionChanged()
374 int index = mEmailListBox->currentRow();
375 bool value = ( index >= 0 );
377 mRemoveButton->setEnabled( value );
378 mEditButton->setEnabled( value );
379 mStandardButton->setEnabled( value );
A QPointer which when destructed, deletes the object it points to.