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

akonadi

  • 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 setPreferred(bool preferred)
77  {
78  mPreferred = preferred;
79  format();
80  }
81  bool preferred() const
82  {
83  return mPreferred;
84  }
85 
86 private:
87  void format()
88  {
89  QFont f = font();
90  f.setBold(mPreferred);
91  setFont(f);
92  }
93 
94 private:
95  bool mPreferred;
96 };
97 
98 EmailEditWidget::EmailEditWidget(QWidget *parent)
99  : QWidget(parent)
100 {
101  QHBoxLayout *layout = new QHBoxLayout(this);
102  layout->setMargin(0);
103  layout->setSpacing(KDialog::spacingHint());
104 
105  mEmailEdit = new KLineEdit;
106  new EmailAddressExtracter(mEmailEdit);
107  connect(mEmailEdit, SIGNAL(textChanged(QString)),
108  SLOT(textChanged(QString)));
109  layout->addWidget(mEmailEdit);
110 
111  mEditButton = new QToolButton;
112  mEditButton->setText(QLatin1String("..."));
113  connect(mEditButton, SIGNAL(clicked()), SLOT(edit()));
114  layout->addWidget(mEditButton);
115  setFocusProxy(mEditButton);
116  setFocusPolicy(Qt::StrongFocus);
117 }
118 
119 EmailEditWidget::~EmailEditWidget()
120 {
121 }
122 
123 void EmailEditWidget::setReadOnly(bool readOnly)
124 {
125  mEmailEdit->setReadOnly(readOnly);
126  mEditButton->setEnabled(!readOnly);
127 }
128 
129 void EmailEditWidget::loadContact(const KABC::Addressee &contact)
130 {
131  mEmailList = contact.emails();
132 
133  if (!mEmailList.isEmpty()) {
134  mEmailEdit->setText(mEmailList.first());
135  } else {
136  mEmailEdit->setText(QString());
137  }
138 }
139 
140 void EmailEditWidget::storeContact(KABC::Addressee &contact) const
141 {
142  QStringList emails(mEmailList);
143 
144  // the preferred address is always the first one, remove it...
145  if (!emails.isEmpty()) {
146  emails.removeFirst();
147  }
148 
149  // ... and prepend the one from the line edit
150  if (!mEmailEdit->text().isEmpty()) {
151  emails.prepend(mEmailEdit->text().toLower());
152  }
153 
154  contact.setEmails(emails);
155 }
156 
157 void EmailEditWidget::edit()
158 {
159  AutoQPointer<EmailEditDialog> dlg = new EmailEditDialog(mEmailList, this);
160 
161  if (dlg->exec()) {
162  if (dlg->changed()) {
163  mEmailList = dlg->emails();
164  if (!mEmailList.isEmpty()) {
165  mEmailEdit->setText(mEmailList.first());
166  } else {
167  mEmailEdit->setText(QString());
168  }
169  }
170  }
171 }
172 
173 void EmailEditWidget::textChanged(const QString &text)
174 {
175  if (!mEmailList.isEmpty()) {
176  mEmailList.removeFirst();
177  }
178 
179  mEmailList.prepend(text);
180 }
181 
182 EmailEditDialog::EmailEditDialog(const QStringList &list, QWidget *parent)
183  : KDialog(parent)
184 {
185  setCaption(i18n("Edit Email Addresses"));
186  setButtons(KDialog::Ok | KDialog::Cancel);
187  setDefaultButton(KDialog::Cancel);
188 
189  QWidget *page = new QWidget(this);
190  setMainWidget(page);
191 
192  QGridLayout *topLayout = new QGridLayout(page);
193  topLayout->setSpacing(spacingHint());
194  topLayout->setMargin(0);
195 
196  mEmailListBox = new KListWidget(page);
197  mEmailListBox->setSelectionMode(QAbstractItemView::SingleSelection);
198 
199  // Make sure there is room for the scrollbar
200  mEmailListBox->setMinimumHeight(mEmailListBox->sizeHint().height() + 30);
201  connect(mEmailListBox, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
202  SLOT(selectionChanged()));
203  connect(mEmailListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
204  SLOT(edit()));
205  topLayout->addWidget(mEmailListBox, 0, 0, 5, 2);
206 
207  mAddButton = new QPushButton(i18n("Add..."), page);
208  connect(mAddButton, SIGNAL(clicked()), SLOT(add()));
209  topLayout->addWidget(mAddButton, 0, 2);
210 
211  mEditButton = new QPushButton(i18n("Edit..."), page);
212  mEditButton->setEnabled(false);
213  connect(mEditButton, SIGNAL(clicked()), SLOT(edit()));
214  topLayout->addWidget(mEditButton, 1, 2);
215 
216  mRemoveButton = new QPushButton(i18n("Remove"), page);
217  mRemoveButton->setEnabled(false);
218  connect(mRemoveButton, SIGNAL(clicked()), SLOT(remove()));
219  topLayout->addWidget(mRemoveButton, 2, 2);
220 
221  mStandardButton = new QPushButton(i18n("Set as Standard"), page);
222  mStandardButton->setEnabled(false);
223  connect(mStandardButton, SIGNAL(clicked()), SLOT(standard()));
224  topLayout->addWidget(mStandardButton, 3, 2);
225 
226  topLayout->setRowStretch(4, 1);
227 
228  QStringList items = list;
229  if (items.removeAll(QLatin1String("")) > 0) {
230  mChanged = true;
231  } else {
232  mChanged = false;
233  }
234 
235  QStringList::ConstIterator it;
236  bool preferred = true;
237  QStringList::ConstIterator end(items.constEnd());
238  for (it = items.constBegin(); it != end; ++it) {
239  new EmailItem(*it, mEmailListBox, preferred);
240  preferred = false;
241  }
242 
243  // set default state
244  KAcceleratorManager::manage(this);
245 
246  readConfig();
247 }
248 
249 EmailEditDialog::~EmailEditDialog()
250 {
251  writeConfig();
252 }
253 
254 void EmailEditDialog::readConfig()
255 {
256  KConfigGroup group(KGlobal::config(), "EmailEditDialog");
257  const QSize sizeDialog = group.readEntry("Size", QSize(400, 200));
258  if (sizeDialog.isValid()) {
259  resize(sizeDialog);
260  }
261 }
262 
263 void EmailEditDialog::writeConfig()
264 {
265  KConfigGroup group(KGlobal::config(), "EmailEditDialog");
266  group.writeEntry("Size", size());
267 }
268 
269 QStringList EmailEditDialog::emails() const
270 {
271  QStringList emails;
272 
273  for (int i = 0; i < mEmailListBox->count(); ++i) {
274  EmailItem *item = static_cast<EmailItem *>(mEmailListBox->item(i));
275  if (item->preferred()) {
276  emails.prepend(item->text());
277  } else {
278  emails.append(item->text());
279  }
280  }
281 
282  return emails;
283 }
284 
285 void EmailEditDialog::add()
286 {
287  bool ok = false;
288 
289  QString email = KInputDialog::getText(i18n("Add Email"), i18n("New Email:"),
290  QString(), &ok, this);
291 
292  if (!ok) {
293  return;
294  }
295 
296  email = KPIMUtils::extractEmailAddress(email.toLower());
297 
298  // check if item already available, ignore if so...
299  for (int i = 0; i < mEmailListBox->count(); ++i) {
300  if (mEmailListBox->item(i)->text() == email) {
301  return;
302  }
303  }
304 
305  new EmailItem(email, mEmailListBox, (mEmailListBox->count() == 0));
306 
307  mChanged = true;
308 }
309 
310 void EmailEditDialog::edit()
311 {
312  bool ok = false;
313 
314  QListWidgetItem *item = mEmailListBox->currentItem();
315 
316  QString email = KInputDialog::getText(i18n("Edit Email"),
317  i18nc("@label:textbox Inputfield for an email address", "Email:"),
318  item->text(), &ok, this);
319 
320  if (!ok) {
321  return;
322  }
323 
324  email = KPIMUtils::extractEmailAddress(email.toLower());
325 
326  // check if item already available, ignore if so...
327  for (int i = 0; i < mEmailListBox->count(); ++i) {
328  if (mEmailListBox->item(i)->text() == email) {
329  return;
330  }
331  }
332 
333  EmailItem *eitem = static_cast<EmailItem *>(item);
334  eitem->setText(email);
335 
336  mChanged = true;
337 }
338 
339 void EmailEditDialog::remove()
340 {
341  const QString address = mEmailListBox->currentItem()->text();
342 
343  const QString text = i18n("<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>", address);
344  const QString caption = i18n("Confirm Remove");
345 
346  if (KMessageBox::warningContinueCancel(this, text, caption, KGuiItem(i18n("&Delete"), QLatin1String("edit-delete"))) == KMessageBox::Continue) {
347  EmailItem *item = static_cast<EmailItem *>(mEmailListBox->currentItem());
348 
349  const bool preferred = item->preferred();
350  mEmailListBox->takeItem(mEmailListBox->currentRow());
351  if (preferred) {
352  item = dynamic_cast<EmailItem *>(mEmailListBox->item(0));
353  if (item) {
354  item->setPreferred(true);
355  }
356  }
357 
358  mChanged = true;
359  }
360 }
361 
362 bool EmailEditDialog::changed() const
363 {
364  return mChanged;
365 }
366 
367 void EmailEditDialog::standard()
368 {
369  for (int i = 0; i < mEmailListBox->count(); ++i) {
370  EmailItem *item = static_cast<EmailItem *>(mEmailListBox->item(i));
371  if (i == mEmailListBox->currentRow()) {
372  item->setPreferred(true);
373  } else {
374  item->setPreferred(false);
375  }
376  }
377 
378  mChanged = true;
379 }
380 
381 void EmailEditDialog::selectionChanged()
382 {
383  int index = mEmailListBox->currentRow();
384  bool value = (index >= 0); // An item is selected
385 
386  mRemoveButton->setEnabled(value);
387  mEditButton->setEnabled(value);
388  mStandardButton->setEnabled(value);
389 }
AutoQPointer
A QPointer which when destructed, deletes the object it points to.
Definition: autoqpointer_p.h:34
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:52 by doxygen 1.8.6 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.13.3 API Reference

Skip menu "kdepimlibs-4.13.3 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
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