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

akonadi/contact

  • sources
  • kde-4.14
  • kdepimlibs
  • akonadi
  • contact
  • editor
emaileditwidget.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "emaileditwidget.h"
23 
24 #include "autoqpointer_p.h"
25 
26 #include <QtCore/QEvent>
27 #include <QtCore/QString>
28 #include <QGridLayout>
29 #include <QPushButton>
30 #include <QToolButton>
31 
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>
40 
41 class EmailAddressExtracter : public QObject
42 {
43 public:
44  EmailAddressExtracter(KLineEdit *lineEdit)
45  : QObject(lineEdit)
46  , mLineEdit(lineEdit)
47  {
48  lineEdit->installEventFilter(this);
49  }
50 
51  virtual bool eventFilter(QObject *watched, QEvent *event)
52  {
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);
57  }
58 
59  return QObject::eventFilter(watched, event);
60  }
61 
62 private:
63  KLineEdit *mLineEdit;
64 };
65 
66 class EmailItem : public QListWidgetItem
67 {
68 public:
69  EmailItem(const QString &text, QListWidget *parent, bool preferred)
70  : QListWidgetItem(text, parent)
71  , mPreferred(preferred)
72  {
73  format();
74  }
75 
76  void setEmail(const KABC::Email &email)
77  {
78  mEmail = email;
79  }
80 
81  KABC::Email email() const
82  {
83  return mEmail;
84  }
85 
86  void setPreferred(bool preferred)
87  {
88  mPreferred = preferred;
89  format();
90  }
91  bool preferred() const
92  {
93  return mPreferred;
94  }
95 
96 private:
97  void format()
98  {
99  QFont f = font();
100  f.setBold(mPreferred);
101  setFont(f);
102  }
103 
104 private:
105  KABC::Email mEmail;
106  bool mPreferred;
107 };
108 
109 EmailEditWidget::EmailEditWidget(QWidget *parent)
110  : QWidget(parent)
111 {
112  QHBoxLayout *layout = new QHBoxLayout(this);
113  layout->setMargin(0);
114  layout->setSpacing(KDialog::spacingHint());
115 
116  mEmailEdit = new KLineEdit;
117  mEmailEdit->setTrapReturnKey(true);
118  mEmailEdit->setObjectName(QLatin1String("emailedit"));
119  new EmailAddressExtracter(mEmailEdit);
120  connect(mEmailEdit, SIGNAL(textChanged(QString)),
121  SLOT(textChanged(QString)));
122  layout->addWidget(mEmailEdit);
123 
124  mEditButton = new QToolButton;
125  mEditButton->setObjectName(QLatin1String("editbutton"));
126  mEditButton->setText(QLatin1String("..."));
127  connect(mEditButton, SIGNAL(clicked()), SLOT(edit()));
128  layout->addWidget(mEditButton);
129  setFocusProxy(mEditButton);
130  setFocusPolicy(Qt::StrongFocus);
131 }
132 
133 EmailEditWidget::~EmailEditWidget()
134 {
135 }
136 
137 void EmailEditWidget::setReadOnly(bool readOnly)
138 {
139  mEmailEdit->setReadOnly(readOnly);
140  mEditButton->setEnabled(!readOnly);
141 }
142 
143 void EmailEditWidget::loadContact(const KABC::Addressee &contact)
144 {
145  mList = contact.emailList();
146 
147  if (mList.isEmpty()) {
148  mEmailEdit->setText(QString());
149  } else {
150  mEmailEdit->setText(mList.first().mail());
151  }
152 }
153 
154 void EmailEditWidget::storeContact(KABC::Addressee &contact) const
155 {
156  contact.setEmailList(mList);
157 }
158 
159 void EmailEditWidget::edit()
160 {
161  AutoQPointer<EmailEditDialog> dlg = new EmailEditDialog(this);
162  dlg->setEmailList(mList);
163  if (dlg->exec()) {
164  if (dlg->changed()) {
165  mList = dlg->emailList();
166  if (!mList.isEmpty()) {
167  mEmailEdit->setText(mList.first().mail());
168  } else {
169  mEmailEdit->setText(QString());
170  }
171  }
172  }
173 }
174 
175 void EmailEditWidget::textChanged(const QString &text)
176 {
177  if (mList.isEmpty()) {
178  mList.append(KABC::Email(text));
179  } else {
180  mList.first().setEmail(text);
181  }
182 }
183 
184 EmailEditDialog::EmailEditDialog(QWidget *parent)
185  : KDialog(parent)
186 {
187  setCaption(i18n("Edit Email Addresses"));
188  setButtons(KDialog::Ok | KDialog::Cancel);
189  setDefaultButton(KDialog::Cancel);
190 
191  QWidget *page = new QWidget(this);
192  setMainWidget(page);
193 
194  QGridLayout *topLayout = new QGridLayout(page);
195  topLayout->setSpacing(spacingHint());
196  topLayout->setMargin(0);
197 
198  mEmailListBox = new KListWidget(page);
199  mEmailListBox->setObjectName(QLatin1String("emailListBox"));
200  mEmailListBox->setSelectionMode(QAbstractItemView::SingleSelection);
201 
202  // Make sure there is room for the scrollbar
203  mEmailListBox->setMinimumHeight(mEmailListBox->sizeHint().height() + 30);
204  connect(mEmailListBox, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
205  SLOT(selectionChanged()));
206  connect(mEmailListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
207  SLOT(edit()));
208  topLayout->addWidget(mEmailListBox, 0, 0, 5, 2);
209 
210  mAddButton = new QPushButton(i18n("Add..."), page);
211  mAddButton->setObjectName(QLatin1String("add"));
212  connect(mAddButton, SIGNAL(clicked()), SLOT(add()));
213  topLayout->addWidget(mAddButton, 0, 2);
214 
215  mEditButton = new QPushButton(i18n("Edit..."), page);
216  mEditButton->setObjectName(QLatin1String("edit"));
217  mEditButton->setEnabled(false);
218  connect(mEditButton, SIGNAL(clicked()), SLOT(edit()));
219  topLayout->addWidget(mEditButton, 1, 2);
220 
221  mRemoveButton = new QPushButton(i18n("Remove"), page);
222  mRemoveButton->setObjectName(QLatin1String("remove"));
223  mRemoveButton->setEnabled(false);
224  connect(mRemoveButton, SIGNAL(clicked()), SLOT(remove()));
225  topLayout->addWidget(mRemoveButton, 2, 2);
226 
227  mStandardButton = new QPushButton(i18n("Set as Standard"), page);
228  mStandardButton->setObjectName(QLatin1String("standard"));
229  mStandardButton->setEnabled(false);
230  connect(mStandardButton, SIGNAL(clicked()), SLOT(standard()));
231  topLayout->addWidget(mStandardButton, 3, 2);
232 
233  topLayout->setRowStretch(4, 1);
234 
235  // set default state
236  KAcceleratorManager::manage(this);
237 
238  readConfig();
239 }
240 
241 EmailEditDialog::~EmailEditDialog()
242 {
243  writeConfig();
244 }
245 
246 void EmailEditDialog::readConfig()
247 {
248  KConfigGroup group(KGlobal::config(), "EmailEditDialog");
249  const QSize sizeDialog = group.readEntry("Size", QSize(400, 200));
250  if (sizeDialog.isValid()) {
251  resize(sizeDialog);
252  }
253 }
254 
255 void EmailEditDialog::writeConfig()
256 {
257  KConfigGroup group(KGlobal::config(), "EmailEditDialog");
258  group.writeEntry("Size", size());
259 }
260 
261 KABC::Email::List EmailEditDialog::emailList() const
262 {
263  KABC::Email::List lst;
264 
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()) {
270  lst.prepend(email);
271  } else {
272  lst.append(email);
273  }
274  }
275 
276  return lst;
277 
278 }
279 
280 
281 void EmailEditDialog::add()
282 {
283  bool ok = false;
284 
285  QString email = KInputDialog::getText(i18n("Add Email"), i18n("New Email:"),
286  QString(), &ok, this);
287 
288  if (!ok) {
289  return;
290  }
291 
292  email = KPIMUtils::extractEmailAddress(email.toLower());
293 
294  // check if item already available, ignore if so...
295  for (int i = 0; i < mEmailListBox->count(); ++i) {
296  if (mEmailListBox->item(i)->text() == email) {
297  return;
298  }
299  }
300 
301  new EmailItem(email, mEmailListBox, (mEmailListBox->count() == 0));
302 
303  mChanged = true;
304 }
305 
306 void EmailEditDialog::edit()
307 {
308  bool ok = false;
309 
310  QListWidgetItem *item = mEmailListBox->currentItem();
311 
312  QString email = KInputDialog::getText(i18n("Edit Email"),
313  i18nc("@label:textbox Inputfield for an email address", "Email:"),
314  item->text(), &ok, this);
315 
316  if (!ok) {
317  return;
318  }
319 
320  email = KPIMUtils::extractEmailAddress(email.toLower());
321 
322  // check if item already available, ignore if so...
323  for (int i = 0; i < mEmailListBox->count(); ++i) {
324  if (mEmailListBox->item(i)->text() == email) {
325  return;
326  }
327  }
328 
329  EmailItem *eitem = static_cast<EmailItem *>(item);
330  eitem->setText(email);
331 
332  mChanged = true;
333 }
334 
335 void EmailEditDialog::remove()
336 {
337  const QString address = mEmailListBox->currentItem()->text();
338 
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");
341 
342  if (KMessageBox::warningContinueCancel(this, text, caption, KGuiItem(i18n("&Delete"), QLatin1String("edit-delete"))) == KMessageBox::Continue) {
343  EmailItem *item = static_cast<EmailItem *>(mEmailListBox->currentItem());
344 
345  const bool preferred = item->preferred();
346  mEmailListBox->takeItem(mEmailListBox->currentRow());
347  if (preferred) {
348  item = dynamic_cast<EmailItem *>(mEmailListBox->item(0));
349  if (item) {
350  item->setPreferred(true);
351  }
352  }
353 
354  mChanged = true;
355  }
356 }
357 
358 bool EmailEditDialog::changed() const
359 {
360  return mChanged;
361 }
362 
363 void EmailEditDialog::setEmailList(const KABC::Email::List &list)
364 {
365  QStringList emails;
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();
372  preferred = false;
373  }
374  }
375 }
376 
377 void EmailEditDialog::standard()
378 {
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);
383  } else {
384  item->setPreferred(false);
385  }
386  }
387 
388  mChanged = true;
389 }
390 
391 void EmailEditDialog::selectionChanged()
392 {
393  const int index = mEmailListBox->currentRow();
394  const bool value = (index >= 0); // An item is selected
395 
396  mRemoveButton->setEnabled(value);
397  mEditButton->setEnabled(value);
398  mStandardButton->setEnabled(value);
399 }
QWidget::layout
QLayout * layout() const
QEvent
QWidget
QSize::isValid
bool isValid() const
QEvent::type
Type type() const
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QWidget::setFocusPolicy
void setFocusPolicy(Qt::FocusPolicy policy)
QFont
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QListWidgetItem
QHBoxLayout
QGridLayout
QListWidget
QGridLayout::setSpacing
void setSpacing(int spacing)
QFont::setBold
void setBold(bool enable)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QGridLayout::setRowStretch
void setRowStretch(int row, int stretch)
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
QObject
QListWidgetItem::font
QFont font() const
QObject::setObjectName
void setObjectName(const QString &name)
QWidget::setFocusProxy
void setFocusProxy(QWidget *w)
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
QString
QLayout::setMargin
void setMargin(int margin)
QStringList
QString::toLower
QString toLower() const
QToolButton
QSize
AutoQPointer
A QPointer which when destructed, deletes the object it points to.
Definition: autoqpointer_p.h:34
QLatin1String
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QWidget::setCaption
void setCaption(const QString &c)
QPushButton
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QListWidgetItem::setFont
void setFont(const QFont &font)
QListWidgetItem::text
QString text() const
QBoxLayout::setSpacing
void setSpacing(int spacing)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



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