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

akonadi/contact

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

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