kaddressbook

emaileditwidget.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 
24 #include <tqcheckbox.h>
25 #include <tqlabel.h>
26 #include <tqlayout.h>
27 #include <tqpainter.h>
28 #include <tqpushbutton.h>
29 #include <tqvalidator.h>
30 #include <tqstring.h>
31 #include <tqtoolbutton.h>
32 #include <tqtooltip.h>
33 
34 #include <tdeaccelmanager.h>
35 #include <tdeconfig.h>
36 #include <kcombobox.h>
37 #include <kdebug.h>
38 #include <kdialog.h>
39 #include <kiconloader.h>
40 #include <kinputdialog.h>
41 #include <klineedit.h>
42 #include <tdelocale.h>
43 #include <tdemessagebox.h>
44 
45 #include "emaileditwidget.h"
46 
47 class EmailValidator : public TQRegExpValidator
48 {
49  public:
50  EmailValidator()
51  : TQRegExpValidator( 0, "EmailValidator" )
52  {
53  TQRegExp rx( ".*@.*\\.[A-Za-z]+" );
54  setRegExp( rx );
55  }
56 };
57 
58 class EmailItem : public TQListBoxText
59 {
60  public:
61  EmailItem( TQListBox *parent, const TQString &text, bool preferred )
62  : TQListBoxText( parent, text ), mPreferred( preferred )
63  {}
64 
65  void setPreferred( bool preferred ) { mPreferred = preferred; }
66  bool preferred() const { return mPreferred; }
67 
68  void setText( const TQString &text )
69  {
70  TQListBoxText::setText( text );
71  }
72 
73  protected:
74  virtual void paint( TQPainter *p )
75  {
76  if ( mPreferred ) {
77  TQFont font = p->font();
78  font.setBold( true );
79  p->setFont( font );
80  }
81 
82  TQListBoxText::paint( p );
83  }
84 
85  private:
86  bool mPreferred;
87 };
88 
89 EmailEditWidget::EmailEditWidget( TQWidget *parent, const char *name )
90  : TQWidget( parent, name )
91 {
92  TQGridLayout *topLayout = new TQGridLayout( this, 2, 2, KDialog::marginHint(),
93  KDialog::spacingHint() );
94 
95  TQLabel *label = new TQLabel( i18n( "Email:" ), this );
96  topLayout->addWidget( label, 0, 0 );
97 
98  mEmailEdit = new KLineEdit( this );
99  mEmailEdit->setValidator( new EmailValidator );
100  connect( mEmailEdit, TQ_SIGNAL( textChanged( const TQString& ) ),
101  TQ_SLOT( textChanged( const TQString& ) ) );
102  connect( mEmailEdit, TQ_SIGNAL( textChanged( const TQString& ) ),
103  TQ_SIGNAL( modified() ) );
104  label->setBuddy( mEmailEdit );
105  topLayout->addWidget( mEmailEdit, 0, 1 );
106 
107  mEditButton = new TQPushButton( i18n( "Edit Email Addresses..." ), this);
108  connect( mEditButton, TQ_SIGNAL( clicked() ), TQ_SLOT( edit() ) );
109  topLayout->addMultiCellWidget( mEditButton, 1, 1, 0, 1 );
110 
111  topLayout->activate();
112 }
113 
114 EmailEditWidget::~EmailEditWidget()
115 {
116 }
117 
118 void EmailEditWidget::setReadOnly( bool readOnly )
119 {
120  mEmailEdit->setReadOnly( readOnly );
121  mEditButton->setEnabled( !readOnly );
122 }
123 
124 void EmailEditWidget::setEmails( const TQStringList &list )
125 {
126  mEmailList = list;
127 
128  bool blocked = mEmailEdit->signalsBlocked();
129  mEmailEdit->blockSignals( true );
130  if ( list.count() > 0 )
131  mEmailEdit->setText( list[ 0 ] );
132  else
133  mEmailEdit->setText( "" );
134  mEmailEdit->blockSignals( blocked );
135 }
136 
137 TQStringList EmailEditWidget::emails()
138 {
139  if ( mEmailEdit->text().isEmpty() ) {
140  if ( mEmailList.count() > 0 )
141  mEmailList.remove( mEmailList.begin() );
142  } else {
143  if ( mEmailList.count() > 0 )
144  mEmailList.remove( mEmailList.begin() );
145 
146  mEmailList.prepend( mEmailEdit->text() );
147  }
148 
149  return mEmailList;
150 }
151 
152 void EmailEditWidget::edit()
153 {
154  EmailEditDialog dlg( mEmailList, this );
155 
156  if ( dlg.exec() ) {
157  if ( dlg.changed() ) {
158  mEmailList = dlg.emails();
159  mEmailEdit->setText( mEmailList[ 0 ] );
160  emit modified();
161  }
162  }
163 }
164 
165 void EmailEditWidget::textChanged( const TQString &text )
166 {
167  if ( mEmailList.count() > 0 )
168  mEmailList.remove( mEmailList.begin() );
169 
170  mEmailList.prepend( text );
171 }
172 
173 
174 EmailEditDialog::EmailEditDialog( const TQStringList &list, TQWidget *parent,
175  const char *name )
176  : KDialogBase( KDialogBase::Plain, i18n( "Edit Email Addresses" ),
177  KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Help,
178  parent, name, true )
179 {
180  TQWidget *page = plainPage();
181 
182  TQGridLayout *topLayout = new TQGridLayout( page, 4, 3, 0, spacingHint() );
183 
184  mEmailListBox = new TQListBox( page );
185 
186  // Make sure there is room for the scrollbar
187  mEmailListBox->setMinimumHeight( mEmailListBox->sizeHint().height() + 30 );
188  connect( mEmailListBox, TQ_SIGNAL( highlighted( int ) ),
189  TQ_SLOT( selectionChanged( int ) ) );
190  connect( mEmailListBox, TQ_SIGNAL( selected( int ) ),
191  TQ_SLOT( edit() ) );
192  topLayout->addMultiCellWidget( mEmailListBox, 0, 3, 0, 1 );
193 
194  mAddButton = new TQPushButton( i18n( "Add..." ), page );
195  connect( mAddButton, TQ_SIGNAL( clicked() ), TQ_SLOT( add() ) );
196  topLayout->addWidget( mAddButton, 0, 2 );
197 
198  mEditButton = new TQPushButton( i18n( "Edit..." ), page );
199  connect( mEditButton, TQ_SIGNAL( clicked() ), TQ_SLOT( edit() ) );
200  topLayout->addWidget( mEditButton, 1, 2 );
201 
202  mRemoveButton = new TQPushButton( i18n( "Remove" ), page );
203  connect( mRemoveButton, TQ_SIGNAL( clicked() ), TQ_SLOT( remove() ) );
204  topLayout->addWidget( mRemoveButton, 2, 2 );
205 
206  mStandardButton = new TQPushButton( i18n( "Set Standard" ), page );
207  connect( mStandardButton, TQ_SIGNAL( clicked() ), TQ_SLOT( standard() ) );
208  topLayout->addWidget( mStandardButton, 3, 2 );
209 
210  topLayout->activate();
211 
212  TQStringList items = list;
213  if ( items.remove( "" ) > 0 )
214  mChanged = true;
215  else
216  mChanged = false;
217 
218  TQStringList::ConstIterator it;
219  bool preferred = true;
220  for ( it = items.begin(); it != items.end(); ++it ) {
221  new EmailItem( mEmailListBox, *it, preferred );
222  preferred = false;
223  }
224 
225  // set default state
226  selectionChanged( -1 );
227  TDEAcceleratorManager::manage( this );
228 
229  setInitialSize( TQSize( 400, 200 ) );
230 }
231 
232 EmailEditDialog::~EmailEditDialog()
233 {
234 }
235 
236 TQStringList EmailEditDialog::emails() const
237 {
238  TQStringList emails;
239 
240  for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
241  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
242  if ( item->preferred() )
243  emails.prepend( item->text() );
244  else
245  emails.append( item->text() );
246  }
247 
248  return emails;
249 }
250 
251 void EmailEditDialog::add()
252 {
253  EmailValidator *validator = new EmailValidator;
254  bool ok = false;
255 
256  TQString email = KInputDialog::getText( i18n( "Add Email" ), i18n( "New Email:" ),
257  TQString(), &ok, this, "EmailEditDialog",
258  validator );
259 
260  if ( !ok )
261  return;
262 
263  // check if item already available, ignore if so...
264  for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
265  if ( mEmailListBox->text( i ) == email )
266  return;
267  }
268 
269  new EmailItem( mEmailListBox, email, (mEmailListBox->count() == 0) );
270 
271  mChanged = true;
272 }
273 
274 void EmailEditDialog::edit()
275 {
276  EmailValidator *validator = new EmailValidator;
277  bool ok = false;
278 
279  int editPos = mEmailListBox->currentItem();
280 
281  TQString email = KInputDialog::getText( i18n( "Edit Email" ), i18n( "Email:" ),
282  mEmailListBox->text( editPos ), &ok, this,
283  "EmailEditDialog", validator );
284 
285  if ( !ok )
286  return;
287 
288  // check if item already available, ignore if so...
289  for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
290  if ( mEmailListBox->text( i ) == email )
291  return;
292  }
293 
294  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( editPos ) );
295  item->setText( email );
296  mEmailListBox->triggerUpdate( true );
297 
298  mChanged = true;
299 }
300 
301 void EmailEditDialog::remove()
302 {
303  TQString address = mEmailListBox->currentText();
304 
305  TQString text = i18n( "<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>" ).arg( address );
306  TQString caption = i18n( "Confirm Remove" );
307 
308  if ( KMessageBox::warningContinueCancel( this, text, caption, KGuiItem( i18n("&Delete"), "edit-delete") ) == KMessageBox::Continue) {
309  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( mEmailListBox->currentItem() ) );
310 
311  bool preferred = item->preferred();
312  mEmailListBox->removeItem( mEmailListBox->currentItem() );
313  if ( preferred ) {
314  item = dynamic_cast<EmailItem*>( mEmailListBox->item( 0 ) );
315  if ( item )
316  item->setPreferred( true );
317  }
318 
319  mChanged = true;
320  }
321 }
322 
323 bool EmailEditDialog::changed() const
324 {
325  return mChanged;
326 }
327 
328 void EmailEditDialog::standard()
329 {
330  for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
331  EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
332  if ( (int)i == mEmailListBox->currentItem() )
333  item->setPreferred( true );
334  else
335  item->setPreferred( false );
336  }
337 
338  mEmailListBox->triggerUpdate( true );
339 
340  mChanged = true;
341 }
342 
343 void EmailEditDialog::selectionChanged( int index )
344 {
345  bool value = ( index >= 0 ); // An item is selected
346 
347  mRemoveButton->setEnabled( value );
348  mEditButton->setEnabled( value );
349  mStandardButton->setEnabled( value );
350 }
351 
352 #include "emaileditwidget.moc"