22 #include "emaileditwidget.h"
24 #include "autoqpointer_p.h"
26 #include <QtCore/QEvent>
27 #include <QtCore/QString>
28 #include <QGridLayout>
29 #include <QPushButton>
30 #include <QToolButton>
32 #include <kabc/addressee.h>
33 #include <kacceleratormanager.h>
34 #include <kinputdialog.h>
35 #include <klineedit.h>
36 #include <KListWidget>
37 #include <klocalizedstring.h>
38 #include <kmessagebox.h>
39 #include <kpimutils/email.h>
41 class EmailAddressExtracter :
public QObject
44 EmailAddressExtracter(KLineEdit *lineEdit)
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);
71 , mPreferred(preferred)
76 void setEmail(
const KABC::Email &email)
81 KABC::Email email()
const
86 void setPreferred(
bool preferred)
88 mPreferred = preferred;
91 bool preferred()
const
109 EmailEditWidget::EmailEditWidget(
QWidget *parent)
116 mEmailEdit =
new KLineEdit;
117 mEmailEdit->setTrapReturnKey(
true);
119 new EmailAddressExtracter(mEmailEdit);
127 connect(mEditButton, SIGNAL(clicked()), SLOT(edit()));
133 EmailEditWidget::~EmailEditWidget()
137 void EmailEditWidget::setReadOnly(
bool readOnly)
139 mEmailEdit->setReadOnly(readOnly);
140 mEditButton->setEnabled(!readOnly);
143 void EmailEditWidget::loadContact(
const KABC::Addressee &contact)
145 mList = contact.emailList();
147 if (mList.isEmpty()) {
148 mEmailEdit->setText(
QString());
150 mEmailEdit->setText(mList.first().mail());
154 void EmailEditWidget::storeContact(KABC::Addressee &contact)
const
156 contact.setEmailList(mList);
159 void EmailEditWidget::edit()
162 dlg->setEmailList(mList);
164 if (dlg->changed()) {
165 mList = dlg->emailList();
166 if (!mList.isEmpty()) {
167 mEmailEdit->setText(mList.first().mail());
169 mEmailEdit->setText(
QString());
175 void EmailEditWidget::textChanged(
const QString &text)
177 if (mList.isEmpty()) {
178 mList.append(KABC::Email(text));
180 mList.first().setEmail(text);
184 EmailEditDialog::EmailEditDialog(
QWidget *parent)
188 setButtons(KDialog::Ok | KDialog::Cancel);
189 setDefaultButton(KDialog::Cancel);
198 mEmailListBox =
new KListWidget(page);
200 mEmailListBox->setSelectionMode(QAbstractItemView::SingleSelection);
203 mEmailListBox->setMinimumHeight(mEmailListBox->sizeHint().height() + 30);
205 SLOT(selectionChanged()));
208 topLayout->
addWidget(mEmailListBox, 0, 0, 5, 2);
210 mAddButton =
new QPushButton(i18n(
"Add..."), page);
212 connect(mAddButton, SIGNAL(clicked()), SLOT(add()));
215 mEditButton =
new QPushButton(i18n(
"Edit..."), page);
217 mEditButton->setEnabled(
false);
218 connect(mEditButton, SIGNAL(clicked()), SLOT(edit()));
221 mRemoveButton =
new QPushButton(i18n(
"Remove"), page);
223 mRemoveButton->setEnabled(
false);
224 connect(mRemoveButton, SIGNAL(clicked()), SLOT(
remove()));
225 topLayout->
addWidget(mRemoveButton, 2, 2);
227 mStandardButton =
new QPushButton(i18n(
"Set as Standard"), page);
229 mStandardButton->setEnabled(
false);
230 connect(mStandardButton, SIGNAL(clicked()), SLOT(standard()));
231 topLayout->
addWidget(mStandardButton, 3, 2);
236 KAcceleratorManager::manage(
this);
241 EmailEditDialog::~EmailEditDialog()
246 void EmailEditDialog::readConfig()
248 KConfigGroup group(KGlobal::config(),
"EmailEditDialog");
249 const QSize sizeDialog = group.readEntry(
"Size",
QSize(400, 200));
255 void EmailEditDialog::writeConfig()
257 KConfigGroup group(KGlobal::config(),
"EmailEditDialog");
258 group.writeEntry(
"Size", size());
261 KABC::Email::List EmailEditDialog::emailList()
const
263 KABC::Email::List lst;
265 for (
int i = 0; i < mEmailListBox->count(); ++i) {
266 EmailItem *item =
static_cast<EmailItem *
>(mEmailListBox->item(i));
267 KABC::Email email = item->email();
268 email.setEmail(item->text());
269 if (item->preferred()) {
281 void EmailEditDialog::add()
285 QString email = KInputDialog::getText(i18n(
"Add Email"), i18n(
"New Email:"),
292 email = KPIMUtils::extractEmailAddress(email.
toLower());
295 for (
int i = 0; i < mEmailListBox->count(); ++i) {
296 if (mEmailListBox->item(i)->text() == email) {
301 new EmailItem(email, mEmailListBox, (mEmailListBox->count() == 0));
306 void EmailEditDialog::edit()
312 QString email = KInputDialog::getText(i18n(
"Edit Email"),
313 i18nc(
"@label:textbox Inputfield for an email address",
"Email:"),
314 item->
text(), &ok,
this);
320 email = KPIMUtils::extractEmailAddress(email.
toLower());
323 for (
int i = 0; i < mEmailListBox->count(); ++i) {
324 if (mEmailListBox->item(i)->text() == email) {
329 EmailItem *eitem =
static_cast<EmailItem *
>(item);
330 eitem->setText(email);
335 void EmailEditDialog::remove()
337 const QString address = mEmailListBox->currentItem()->text();
339 const QString text = i18n(
"<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>", address);
340 const QString caption = i18n(
"Confirm Remove");
342 if (KMessageBox::warningContinueCancel(
this, text, caption, KGuiItem(i18n(
"&Delete"),
QLatin1String(
"edit-delete"))) == KMessageBox::Continue) {
343 EmailItem *item =
static_cast<EmailItem *
>(mEmailListBox->currentItem());
345 const bool preferred = item->preferred();
346 mEmailListBox->takeItem(mEmailListBox->currentRow());
348 item =
dynamic_cast<EmailItem *
>(mEmailListBox->item(0));
350 item->setPreferred(
true);
358 bool EmailEditDialog::changed()
const
363 void EmailEditDialog::setEmailList(
const KABC::Email::List &list)
366 bool preferred =
true;
367 Q_FOREACH(
const KABC::Email &email, list) {
368 if (!emails.
contains(email.mail())) {
369 EmailItem *emailItem =
new EmailItem(email.mail(), mEmailListBox, preferred);
370 emailItem->setEmail(email);
371 emails << email.mail();
377 void EmailEditDialog::standard()
379 for (
int i = 0; i < mEmailListBox->count(); ++i) {
380 EmailItem *item =
static_cast<EmailItem *
>(mEmailListBox->item(i));
381 if (i == mEmailListBox->currentRow()) {
382 item->setPreferred(
true);
384 item->setPreferred(
false);
391 void EmailEditDialog::selectionChanged()
393 const int index = mEmailListBox->currentRow();
394 const bool value = (index >= 0);
396 mRemoveButton->setEnabled(value);
397 mEditButton->setEnabled(value);
398 mStandardButton->setEnabled(value);
bool contains(const QString &str, Qt::CaseSensitivity cs) const
void setSpacing(int spacing)
void setBold(bool enable)
void setRowStretch(int row, int stretch)
void installEventFilter(QObject *filterObj)
void setObjectName(const QString &name)
virtual bool eventFilter(QObject *watched, QEvent *event)
void setMargin(int margin)
A QPointer which when destructed, deletes the object it points to.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setSpacing(int spacing)