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

akonadi

  • sources
  • kde-4.12
  • 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 <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  setFocusProxy( mEditButton );
108  setFocusPolicy( Qt::StrongFocus );
109 }
110 
111 EmailEditWidget::~EmailEditWidget()
112 {
113 }
114 
115 void EmailEditWidget::setReadOnly( bool readOnly )
116 {
117  mEmailEdit->setReadOnly( readOnly );
118  mEditButton->setEnabled( !readOnly );
119 }
120 
121 void EmailEditWidget::loadContact( const KABC::Addressee &contact )
122 {
123  mEmailList = contact.emails();
124 
125  if ( !mEmailList.isEmpty() ) {
126  mEmailEdit->setText( mEmailList.first() );
127  } else {
128  mEmailEdit->setText( QString() );
129  }
130 }
131 
132 void EmailEditWidget::storeContact( KABC::Addressee &contact ) const
133 {
134  QStringList emails( mEmailList );
135 
136  // the preferred address is always the first one, remove it...
137  if ( !emails.isEmpty() ) {
138  emails.removeFirst();
139  }
140 
141  // ... and prepend the one from the line edit
142  if ( !mEmailEdit->text().isEmpty() ) {
143  emails.prepend( mEmailEdit->text().toLower() );
144  }
145 
146  contact.setEmails( emails );
147 }
148 
149 void EmailEditWidget::edit()
150 {
151  AutoQPointer<EmailEditDialog> dlg = new EmailEditDialog( mEmailList, this );
152 
153  if ( dlg->exec() ) {
154  if ( dlg->changed() ) {
155  mEmailList = dlg->emails();
156  if ( !mEmailList.isEmpty() ) {
157  mEmailEdit->setText( mEmailList.first() );
158  } else {
159  mEmailEdit->setText( QString() );
160  }
161  }
162  }
163 }
164 
165 void EmailEditWidget::textChanged( const QString &text )
166 {
167  if ( !mEmailList.isEmpty() ) {
168  mEmailList.removeFirst();
169  }
170 
171  mEmailList.prepend( text );
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  readConfig();
238 }
239 
240 EmailEditDialog::~EmailEditDialog()
241 {
242  writeConfig();
243 }
244 
245 void EmailEditDialog::readConfig()
246 {
247  KConfigGroup group( KGlobal::config(), "EmailEditDialog" );
248  const QSize sizeDialog = group.readEntry( "Size", QSize(400,200) );
249  if ( sizeDialog.isValid() ) {
250  resize( sizeDialog );
251  }
252 }
253 
254 void EmailEditDialog::writeConfig()
255 {
256  KConfigGroup group( KGlobal::config(), "EmailEditDialog" );
257  group.writeEntry( "Size", size() );
258 }
259 
260 QStringList EmailEditDialog::emails() const
261 {
262  QStringList emails;
263 
264  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
265  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
266  if ( item->preferred() ) {
267  emails.prepend( item->text() );
268  } else {
269  emails.append( item->text() );
270  }
271  }
272 
273  return emails;
274 }
275 
276 void EmailEditDialog::add()
277 {
278  bool ok = false;
279 
280  QString email = KInputDialog::getText( i18n( "Add Email" ), i18n( "New Email:" ),
281  QString(), &ok, this );
282 
283  if ( !ok ) {
284  return;
285  }
286 
287  email = KPIMUtils::extractEmailAddress( email.toLower() );
288 
289  // check if item already available, ignore if so...
290  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
291  if ( mEmailListBox->item( i )->text() == email ) {
292  return;
293  }
294  }
295 
296  new EmailItem( email, mEmailListBox, ( mEmailListBox->count() == 0 ) );
297 
298  mChanged = true;
299 }
300 
301 void EmailEditDialog::edit()
302 {
303  bool ok = false;
304 
305  QListWidgetItem *item = mEmailListBox->currentItem();
306 
307  QString email = KInputDialog::getText( i18n( "Edit Email" ),
308  i18nc( "@label:textbox Inputfield for an email address", "Email:" ),
309  item->text(), &ok, this );
310 
311  if ( !ok ) {
312  return;
313  }
314 
315  email = KPIMUtils::extractEmailAddress( email.toLower() );
316 
317  // check if item already available, ignore if so...
318  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
319  if ( mEmailListBox->item( i )->text() == email ) {
320  return;
321  }
322  }
323 
324  EmailItem *eitem = static_cast<EmailItem*>( item );
325  eitem->setText( email );
326 
327  mChanged = true;
328 }
329 
330 void EmailEditDialog::remove()
331 {
332  const QString address = mEmailListBox->currentItem()->text();
333 
334  const QString text = i18n( "<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>", address );
335  const QString caption = i18n( "Confirm Remove" );
336 
337  if ( KMessageBox::warningContinueCancel( this, text, caption, KGuiItem( i18n( "&Delete" ), QLatin1String( "edit-delete" ) ) ) == KMessageBox::Continue ) {
338  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->currentItem() );
339 
340  const bool preferred = item->preferred();
341  mEmailListBox->takeItem( mEmailListBox->currentRow() );
342  if ( preferred ) {
343  item = dynamic_cast<EmailItem*>( mEmailListBox->item( 0 ) );
344  if ( item ) {
345  item->setPreferred( true );
346  }
347  }
348 
349  mChanged = true;
350  }
351 }
352 
353 bool EmailEditDialog::changed() const
354 {
355  return mChanged;
356 }
357 
358 void EmailEditDialog::standard()
359 {
360  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
361  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
362  if ( i == mEmailListBox->currentRow() ) {
363  item->setPreferred( true );
364  } else {
365  item->setPreferred( false );
366  }
367  }
368 
369  mChanged = true;
370 }
371 
372 void EmailEditDialog::selectionChanged()
373 {
374  int index = mEmailListBox->currentRow();
375  bool value = ( index >= 0 ); // An item is selected
376 
377  mRemoveButton->setEnabled( value );
378  mEditButton->setEnabled( value );
379  mStandardButton->setEnabled( value );
380 }
381 
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 Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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