• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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 <QtGui/QCheckBox>
29 #include <QtGui/QGridLayout>
30 #include <QtGui/QLabel>
31 #include <QtGui/QPushButton>
32 #include <QtGui/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  bool mIgnoreFocusOutEvent;
66 };
67 
68 class EmailItem : public QListWidgetItem
69 {
70  public:
71  EmailItem( const QString &text, QListWidget *parent, bool preferred )
72  : QListWidgetItem( text, parent ), mPreferred( preferred )
73  {
74  format();
75  }
76 
77  void setPreferred( bool preferred ) { mPreferred = preferred; format(); }
78  bool preferred() const { return mPreferred; }
79 
80  private:
81  void format()
82  {
83  QFont f = font();
84  f.setBold( mPreferred );
85  setFont( f );
86  }
87 
88  private:
89  bool mPreferred;
90 };
91 
92 EmailEditWidget::EmailEditWidget( QWidget *parent )
93  : QWidget( parent )
94 {
95  QHBoxLayout *layout = new QHBoxLayout( this );
96  layout->setMargin( 0 );
97  layout->setSpacing( KDialog::spacingHint() );
98 
99  mEmailEdit = new KLineEdit;
100  new EmailAddressExtracter( mEmailEdit );
101  connect( mEmailEdit, SIGNAL(textChanged(QString)),
102  SLOT(textChanged(QString)) );
103  layout->addWidget( mEmailEdit );
104 
105  mEditButton = new QToolButton;
106  mEditButton->setText( QLatin1String( "..." ) );
107  connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
108  layout->addWidget( mEditButton );
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 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  // ... and prepend the one from the line edit
140  if ( !mEmailEdit->text().isEmpty() )
141  emails.prepend( mEmailEdit->text() );
142 
143  contact.setEmails( emails );
144 }
145 
146 void EmailEditWidget::edit()
147 {
148  AutoQPointer<EmailEditDialog> dlg = new EmailEditDialog( mEmailList, this );
149 
150  if ( dlg->exec() ) {
151  if ( dlg->changed() ) {
152  mEmailList = dlg->emails();
153  if ( !mEmailList.isEmpty() )
154  mEmailEdit->setText( mEmailList.first() );
155  else
156  mEmailEdit->setText( QString() );
157  }
158  }
159 }
160 
161 void EmailEditWidget::textChanged( const QString &text )
162 {
163  if ( !mEmailList.isEmpty() )
164  mEmailList.removeFirst();
165 
166  mEmailList.prepend( text );
167 }
168 
169 
170 EmailEditDialog::EmailEditDialog( const QStringList &list, QWidget *parent )
171  : KDialog( parent )
172 {
173  setCaption( i18n( "Edit Email Addresses" ) );
174  setButtons( KDialog::Ok | KDialog::Cancel );
175  setDefaultButton( KDialog::Cancel );
176 
177  QWidget *page = new QWidget( this);
178  setMainWidget( page );
179 
180  QGridLayout *topLayout = new QGridLayout( page );
181  topLayout->setSpacing( spacingHint() );
182  topLayout->setMargin( 0 );
183 
184  mEmailListBox = new KListWidget( page );
185  mEmailListBox->setSelectionMode( QAbstractItemView::SingleSelection );
186 
187  // Make sure there is room for the scrollbar
188  mEmailListBox->setMinimumHeight( mEmailListBox->sizeHint().height() + 30 );
189  connect( mEmailListBox, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
190  SLOT(selectionChanged()) );
191  connect( mEmailListBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
192  SLOT(edit()) );
193  topLayout->addWidget( mEmailListBox, 0, 0, 5, 2 );
194 
195  mAddButton = new QPushButton( i18n( "Add..." ), page );
196  connect( mAddButton, SIGNAL(clicked()), SLOT(add()) );
197  topLayout->addWidget( mAddButton, 0, 2 );
198 
199  mEditButton = new QPushButton( i18n( "Edit..." ), page );
200  mEditButton->setEnabled( false );
201  connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
202  topLayout->addWidget( mEditButton, 1, 2 );
203 
204  mRemoveButton = new QPushButton( i18n( "Remove" ), page );
205  mRemoveButton->setEnabled( false );
206  connect( mRemoveButton, SIGNAL(clicked()), SLOT(remove()) );
207  topLayout->addWidget( mRemoveButton, 2, 2 );
208 
209  mStandardButton = new QPushButton( i18n( "Set as Standard" ), page );
210  mStandardButton->setEnabled( false );
211  connect( mStandardButton, SIGNAL(clicked()), SLOT(standard()) );
212  topLayout->addWidget( mStandardButton, 3, 2 );
213 
214  topLayout->setRowStretch( 4, 1 );
215 
216  QStringList items = list;
217  if ( items.removeAll( QLatin1String( "" ) ) > 0 )
218  mChanged = true;
219  else
220  mChanged = false;
221 
222  QStringList::ConstIterator it;
223  bool preferred = true;
224  for ( it = items.constBegin(); it != items.constEnd(); ++it ) {
225  new EmailItem( *it, mEmailListBox, preferred );
226  preferred = false;
227  }
228 
229  // set default state
230  KAcceleratorManager::manage( this );
231 
232  setInitialSize( QSize( 400, 200 ) );
233 }
234 
235 EmailEditDialog::~EmailEditDialog()
236 {
237 }
238 
239 QStringList EmailEditDialog::emails() const
240 {
241  QStringList emails;
242 
243  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
244  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
245  if ( item->preferred() )
246  emails.prepend( item->text() );
247  else
248  emails.append( item->text() );
249  }
250 
251  return emails;
252 }
253 
254 void EmailEditDialog::add()
255 {
256  bool ok = false;
257 
258  QString email = KInputDialog::getText( i18n( "Add Email" ), i18n( "New Email:" ),
259  QString(), &ok, this );
260 
261  if ( !ok )
262  return;
263 
264  email = KPIMUtils::extractEmailAddress( email );
265 
266  // check if item already available, ignore if so...
267  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
268  if ( mEmailListBox->item( i )->text() == email )
269  return;
270  }
271 
272  new EmailItem( email, mEmailListBox, (mEmailListBox->count() == 0) );
273 
274  mChanged = true;
275 }
276 
277 void EmailEditDialog::edit()
278 {
279  bool ok = false;
280 
281  QListWidgetItem *item = mEmailListBox->currentItem();
282 
283  QString email = KInputDialog::getText( i18n( "Edit Email" ),
284  i18nc( "@label:textbox Inputfield for an email address", "Email:" ),
285  item->text(), &ok, this );
286 
287  if ( !ok )
288  return;
289 
290  email = KPIMUtils::extractEmailAddress( email );
291 
292  // check if item already available, ignore if so...
293  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
294  if ( mEmailListBox->item( i )->text() == email )
295  return;
296  }
297 
298  EmailItem *eitem = static_cast<EmailItem*>( item );
299  eitem->setText( email );
300 
301  mChanged = true;
302 }
303 
304 void EmailEditDialog::remove()
305 {
306  const QString address = mEmailListBox->currentItem()->text();
307 
308  const QString text = i18n( "<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>", address );
309  const QString caption = i18n( "Confirm Remove" );
310 
311  if ( KMessageBox::warningContinueCancel( this, text, caption, KGuiItem( i18n( "&Delete" ), QLatin1String( "edit-delete" ) ) ) == KMessageBox::Continue ) {
312  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->currentItem() );
313 
314  const bool preferred = item->preferred();
315  mEmailListBox->takeItem( mEmailListBox->currentRow() );
316  if ( preferred ) {
317  item = dynamic_cast<EmailItem*>( mEmailListBox->item( 0 ) );
318  if ( item )
319  item->setPreferred( true );
320  }
321 
322  mChanged = true;
323  }
324 }
325 
326 bool EmailEditDialog::changed() const
327 {
328  return mChanged;
329 }
330 
331 void EmailEditDialog::standard()
332 {
333  for ( int i = 0; i < mEmailListBox->count(); ++i ) {
334  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
335  if ( i == mEmailListBox->currentRow() )
336  item->setPreferred( true );
337  else
338  item->setPreferred( false );
339  }
340 
341  mChanged = true;
342 }
343 
344 void EmailEditDialog::selectionChanged()
345 {
346  int index = mEmailListBox->currentRow();
347  bool value = ( index >= 0 ); // An item is selected
348 
349  mRemoveButton->setEnabled( value );
350  mEditButton->setEnabled( value );
351  mStandardButton->setEnabled( value );
352 }
353 
354 #include "emaileditwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:48:34 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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