• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 <QCheckBox>
29 #include <QGridLayout>
30 #include <QLabel>
31 #include <QPushButton>
32 #include <QToolButton>
33 
34 #include <kabc/addressee.h>
35 #include <kacceleratormanager.h>
36 #include <kinputdialog.h>
37 #include <klineedit.h>
38 #include <KListWidget>
39 #include <klocale.h>
40 #include <kmessagebox.h>
41 #include <kpimutils/email.h>
42 
43 class EmailAddressExtracter : public QObject
44 {
45  public:
46  EmailAddressExtracter( KLineEdit *lineEdit )
47  : QObject( lineEdit ), mLineEdit( lineEdit )
48  {
49  lineEdit->installEventFilter( this );
50  }
51 
52  virtual bool eventFilter( QObject *watched, QEvent *event )
53  {
54  if ( watched == mLineEdit && event->type() == QEvent::FocusOut ) {
55  const QString fullEmailAddress = mLineEdit->text();
56  const QString extractedEmailAddress = KPIMUtils::extractEmailAddress( fullEmailAddress );
57  mLineEdit->setText( extractedEmailAddress );
58  }
59 
60  return QObject::eventFilter( watched, event );
61  }
62 
63  private:
64  KLineEdit *mLineEdit;
65 };
66 
67 class EmailItem : public QListWidgetItem
68 {
69  public:
70  EmailItem( const QString &text, QListWidget *parent, bool preferred )
71  : QListWidgetItem( text, parent ), mPreferred( preferred )
72  {
73  format();
74  }
75 
76  void setPreferred( bool preferred ) { mPreferred = preferred; format(); }
77  bool preferred() const { return mPreferred; }
78 
79  private:
80  void format()
81  {
82  QFont f = font();
83  f.setBold( mPreferred );
84  setFont( f );
85  }
86 
87  private:
88  bool mPreferred;
89 };
90 
91 EmailEditWidget::EmailEditWidget( QWidget *parent )
92  : QWidget( parent )
93 {
94  QHBoxLayout *layout = new QHBoxLayout( this );
95  layout->setMargin( 0 );
96  layout->setSpacing( KDialog::spacingHint() );
97 
98  mEmailEdit = new KLineEdit;
99  new EmailAddressExtracter( mEmailEdit );
100  connect( mEmailEdit, SIGNAL(textChanged(QString)),
101  SLOT(textChanged(QString)) );
102  layout->addWidget( mEmailEdit );
103 
104  mEditButton = new QToolButton;
105  mEditButton->setText( QLatin1String( "..." ) );
106  connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
107  layout->addWidget( mEditButton );
108 }
109 
110 EmailEditWidget::~EmailEditWidget()
111 {
112 }
113 
114 void EmailEditWidget::setReadOnly( bool readOnly )
115 {
116  mEmailEdit->setReadOnly( readOnly );
117  mEditButton->setEnabled( !readOnly );
118 }
119 
120 void EmailEditWidget::loadContact( const KABC::Addressee &contact )
121 {
122  mEmailList = contact.emails();
123 
124  if ( !mEmailList.isEmpty() ) {
125  mEmailEdit->setText( mEmailList.first() );
126  } else {
127  mEmailEdit->setText( QString() );
128  }
129 }
130 
131 void EmailEditWidget::storeContact( KABC::Addressee &contact ) const
132 {
133  QStringList emails( mEmailList );
134 
135  // the preferred address is always the first one, remove it...
136  if ( !emails.isEmpty() ) {
137  emails.removeFirst();
138  }
139 
140  // ... and prepend the one from the line edit
141  if ( !mEmailEdit->text().isEmpty() ) {
142  emails.prepend( mEmailEdit->text().toLower() );
143  }
144 
145  contact.setEmails( emails );
146 }
147 
148 void EmailEditWidget::edit()
149 {
150  AutoQPointer<EmailEditDialog> dlg = new EmailEditDialog( mEmailList, this );
151 
152  if ( dlg->exec() ) {
153  if ( dlg->changed() ) {
154  mEmailList = dlg->emails();
155  if ( !mEmailList.isEmpty() ) {
156  mEmailEdit->setText( mEmailList.first() );
157  } else {
158  mEmailEdit->setText( QString() );
159  }
160  }
161  }
162 }
163 
164 void EmailEditWidget::textChanged( const QString &text )
165 {
166  if ( !mEmailList.isEmpty() ) {
167  mEmailList.removeFirst();
168  }
169 
170  mEmailList.prepend( text );
171 }
172 
173 
174 EmailEditDialog::EmailEditDialog( const QStringList &list, QWidget *parent )
175  : KDialog( parent )
176 {
177  setCaption( i18n( "Edit Email Addresses" ) );
178  setButtons( KDialog::Ok | KDialog::Cancel );
179  setDefaultButton( KDialog::Cancel );
180 
181  QWidget *page = new QWidget( this );
182  setMainWidget( page );
183 
184  QGridLayout *topLayout = new QGridLayout( page );
185  topLayout->setSpacing( spacingHint() );
186  topLayout->setMargin( 0 );
187 
188  mEmailListBox = new KListWidget( page );
189  mEmailListBox->setSelectionMode( QAbstractItemView::SingleSelection );
190 
191  // Make sure there is room for the scrollbar
192  mEmailListBox->setMinimumHeight( mEmailListBox->sizeHint().height() + 30 );
193  connect( mEmailListBox, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
194  SLOT(selectionChanged()) );
195  connect( mEmailListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
196  SLOT(edit()) );
197  topLayout->addWidget( mEmailListBox, 0, 0, 5, 2 );
198 
199  mAddButton = new QPushButton( i18n( "Add..." ), page );
200  connect( mAddButton, SIGNAL(clicked()), SLOT(add()) );
201  topLayout->addWidget( mAddButton, 0, 2 );
202 
203  mEditButton = new QPushButton( i18n( "Edit..." ), page );
204  mEditButton->setEnabled( false );
205  connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
206  topLayout->addWidget( mEditButton, 1, 2 );
207 
208  mRemoveButton = new QPushButton( i18n( "Remove" ), page );
209  mRemoveButton->setEnabled( false );
210  connect( mRemoveButton, SIGNAL(clicked()), SLOT(remove()) );
211  topLayout->addWidget( mRemoveButton, 2, 2 );
212 
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 );
217 
218  topLayout->setRowStretch( 4, 1 );
219 
220  QStringList items = list;
221  if ( items.removeAll( QLatin1String( "" ) ) > 0 ) {
222  mChanged = true;
223  } else {
224  mChanged = false;
225  }
226 
227  QStringList::ConstIterator it;
228  bool preferred = true;
229  for ( it = items.constBegin(); it != items.constEnd(); ++it ) {
230  new EmailItem( *it, mEmailListBox, preferred );
231  preferred = false;
232  }
233 
234  // set default state
235  KAcceleratorManager::manage( this );
236 
237  setInitialSize( QSize( 400, 200 ) );
238 }
239 
240 EmailEditDialog::~EmailEditDialog()
241 {
242 }
243 
244 QStringList EmailEditDialog::emails() const
245 {
246  QStringList emails;
247 
248  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
249  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
250  if ( item->preferred() ) {
251  emails.prepend( item->text() );
252  } else {
253  emails.append( item->text() );
254  }
255  }
256 
257  return emails;
258 }
259 
260 void EmailEditDialog::add()
261 {
262  bool ok = false;
263 
264  QString email = KInputDialog::getText( i18n( "Add Email" ), i18n( "New Email:" ),
265  QString(), &ok, this );
266 
267  if ( !ok ) {
268  return;
269  }
270 
271  email = KPIMUtils::extractEmailAddress( email.toLower() );
272 
273  // check if item already available, ignore if so...
274  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
275  if ( mEmailListBox->item( i )->text() == email ) {
276  return;
277  }
278  }
279 
280  new EmailItem( email, mEmailListBox, ( mEmailListBox->count() == 0 ) );
281 
282  mChanged = true;
283 }
284 
285 void EmailEditDialog::edit()
286 {
287  bool ok = false;
288 
289  QListWidgetItem *item = mEmailListBox->currentItem();
290 
291  QString email = KInputDialog::getText( i18n( "Edit Email" ),
292  i18nc( "@label:textbox Inputfield for an email address", "Email:" ),
293  item->text(), &ok, this );
294 
295  if ( !ok ) {
296  return;
297  }
298 
299  email = KPIMUtils::extractEmailAddress( email.toLower() );
300 
301  // check if item already available, ignore if so...
302  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
303  if ( mEmailListBox->item( i )->text() == email ) {
304  return;
305  }
306  }
307 
308  EmailItem *eitem = static_cast<EmailItem*>( item );
309  eitem->setText( email );
310 
311  mChanged = true;
312 }
313 
314 void EmailEditDialog::remove()
315 {
316  const QString address = mEmailListBox->currentItem()->text();
317 
318  const QString text = i18n( "<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>", address );
319  const QString caption = i18n( "Confirm Remove" );
320 
321  if ( KMessageBox::warningContinueCancel( this, text, caption, KGuiItem( i18n( "&Delete" ), QLatin1String( "edit-delete" ) ) ) == KMessageBox::Continue ) {
322  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->currentItem() );
323 
324  const bool preferred = item->preferred();
325  mEmailListBox->takeItem( mEmailListBox->currentRow() );
326  if ( preferred ) {
327  item = dynamic_cast<EmailItem*>( mEmailListBox->item( 0 ) );
328  if ( item ) {
329  item->setPreferred( true );
330  }
331  }
332 
333  mChanged = true;
334  }
335 }
336 
337 bool EmailEditDialog::changed() const
338 {
339  return mChanged;
340 }
341 
342 void EmailEditDialog::standard()
343 {
344  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
345  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
346  if ( i == mEmailListBox->currentRow() ) {
347  item->setPreferred( true );
348  } else {
349  item->setPreferred( false );
350  }
351  }
352 
353  mChanged = true;
354 }
355 
356 void EmailEditDialog::selectionChanged()
357 {
358  int index = mEmailListBox->currentRow();
359  bool value = ( index >= 0 ); // An item is selected
360 
361  mRemoveButton->setEnabled( value );
362  mEditButton->setEnabled( value );
363  mStandardButton->setEnabled( value );
364 }
365 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:35 by doxygen 1.8.3.1 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.10.5 API Reference

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