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

kabc

  • kabc
addresseedialog.cpp
1 /*
2  This file is part of libkabc.
3  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library 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 GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "addresseedialog.h"
22 #ifndef KDEPIM_NO_KRESOURCES
23 #include "stdaddressbook.h"
24 #endif
25 
26 #include <kdebug.h>
27 #include <klocale.h>
28 
29 #include <QtCore/QPointer>
30 #include <QtCore/QRegExp>
31 #include <QtGui/QGroupBox>
32 #include <QtGui/QLayout>
33 #include <QtGui/QPushButton>
34 
35 using namespace KABC;
36 
37 class AddresseeItem::Private
38 {
39  public:
40  Addressee mAddressee;
41 };
42 
43 AddresseeItem::AddresseeItem( QTreeWidget *parent, const Addressee &addressee ) :
44  QTreeWidgetItem( parent ), d( new Private )
45 {
46  d->mAddressee = addressee;
47 
48  setText( Name, addressee.realName() );
49  setText( Email, addressee.preferredEmail() );
50 }
51 
52 AddresseeItem::~AddresseeItem()
53 {
54  delete d;
55 }
56 
57 Addressee AddresseeItem::addressee() const
58 {
59  return d->mAddressee;
60 }
61 
62 QString AddresseeItem::key( int column, bool ) const
63 {
64  if ( column == Email ) {
65  QString value = text( Email );
66  QRegExp emailRe( QLatin1String( "<\\S*>" ) );
67  int match = emailRe.indexIn( value );
68  if ( match > -1 ) {
69  value = value.mid( match + 1, emailRe.matchedLength() - 2 );
70  }
71 
72  return value.toLower();
73  }
74 
75  return text( column ).toLower();
76 }
77 
78 class AddresseeDialog::Private
79 {
80  public:
81  Private( bool multiple )
82  : mMultiple( multiple )
83  {
84  }
85 
86  void addressBookChanged();
87  void selectItem( const QString & );
88  void updateEdit();
89  void addSelected( QTreeWidgetItem *item );
90  void removeSelected();
91 
92  void loadAddressBook();
93  void addCompletionItem( const QString &str, QTreeWidgetItem *item );
94 
95  bool mMultiple;
96 
97  QTreeWidget *mAddresseeList;
98  KLineEdit *mAddresseeEdit;
99 
100  QTreeWidget *mSelectedList;
101 
102 #ifndef KDEPIM_NO_KRESOURCES
103  AddressBook *mAddressBook;
104 #endif
105 
106  QHash<QString, QTreeWidgetItem*> mItemDict;
107  QHash<QString, QTreeWidgetItem*> mSelectedDict;
108 };
109 
110 AddresseeDialog::AddresseeDialog( QWidget *parent, bool multiple )
111  : KDialog( parent ), d( new Private( multiple ) )
112 {
113  setCaption( i18nc( "@title:window", "Select Addressee" ) );
114  setButtons( Ok | Cancel );
115  setDefaultButton( Ok );
116 
117  QWidget *topWidget = new QWidget( this );
118  setMainWidget( topWidget );
119 
120  QBoxLayout *topLayout = new QHBoxLayout( topWidget );
121  QBoxLayout *listLayout = new QVBoxLayout;
122  topLayout->addLayout( listLayout );
123 
124  d->mAddresseeList = new QTreeWidget( topWidget );
125  d->mAddresseeList->setColumnCount( 2 );
126  QStringList headerTitles;
127  headerTitles << i18nc( "@title:column addressee name", "Name" )
128  << i18nc( "@title:column addressee email", "Email" );
129  d->mAddresseeList->setHeaderItem( new QTreeWidgetItem( headerTitles ) );
130  listLayout->addWidget( d->mAddresseeList );
131  connect( d->mAddresseeList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
132  SLOT(accept()) );
133  connect( d->mAddresseeList, SIGNAL(itemSelectionChanged()),
134  SLOT(updateEdit()) );
135 
136  d->mAddresseeEdit = new KLineEdit( topWidget );
137  d->mAddresseeEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
138  connect( d->mAddresseeEdit->completionObject(), SIGNAL(match(QString)),
139  SLOT(selectItem(QString)) );
140  d->mAddresseeEdit->setFocus();
141  d->mAddresseeEdit->completionObject()->setIgnoreCase( true );
142  listLayout->addWidget( d->mAddresseeEdit );
143 
144  setInitialSize( QSize( 450, 300 ) );
145 
146  if ( d->mMultiple ) {
147  QBoxLayout *selectedLayout = new QVBoxLayout;
148  topLayout->addLayout( selectedLayout );
149 
150  QGroupBox *selectedGroup =
151  new QGroupBox( i18nc( "@title:group selected addressees", "Selected" ), topWidget );
152  QHBoxLayout *groupLayout = new QHBoxLayout;
153  selectedGroup->setLayout( groupLayout );
154  selectedLayout->addWidget( selectedGroup );
155 
156  d->mSelectedList = new QTreeWidget( selectedGroup );
157  groupLayout->addWidget( d->mSelectedList );
158  d->mSelectedList->setColumnCount( 2 );
159  QStringList headerTitles;
160  headerTitles << i18nc( "@title:column addressee name", "Name" )
161  << i18nc( "@title:column addressee email", "Email" );
162  d->mSelectedList->setHeaderItem( new QTreeWidgetItem( headerTitles ) );
163 
164  connect( d->mSelectedList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
165  SLOT(removeSelected()) );
166 
167  QPushButton *unselectButton =
168  new QPushButton( i18nc( "@action:button unselect addressee", "Unselect" ), selectedGroup );
169  selectedLayout->addWidget( unselectButton );
170  connect( unselectButton, SIGNAL(clicked()), SLOT(removeSelected()) );
171 
172  connect( d->mAddresseeList, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
173  SLOT(addSelected(QTreeWidgetItem*)) );
174 
175  setInitialSize( QSize( 650, 350 ) );
176  }
177 
178 #ifndef KDEPIM_NO_KRESOURCES
179  d->mAddressBook = StdAddressBook::self( true );
180  connect( d->mAddressBook, SIGNAL(addressBookChanged(AddressBook*)),
181  SLOT(addressBookChanged()) );
182  connect( d->mAddressBook, SIGNAL(loadingFinished(Resource*)),
183  SLOT(addressBookChanged()) );
184 #endif
185 
186  d->loadAddressBook();
187 }
188 
189 AddresseeDialog::~AddresseeDialog()
190 {
191  delete d;
192 }
193 
194 Addressee AddresseeDialog::addressee() const
195 {
196  AddresseeItem *aItem = 0;
197 
198  if ( d->mMultiple ) {
199  aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( 0 ) );
200  } else {
201  QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
202  if ( !selected.isEmpty() ) {
203  aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
204  }
205  }
206 
207  if ( aItem ) {
208  return aItem->addressee();
209  }
210  return Addressee();
211 }
212 
213 Addressee::List AddresseeDialog::addressees() const
214 {
215  Addressee::List al;
216  AddresseeItem *aItem = 0;
217 
218  if ( d->mMultiple ) {
219  for ( int i = 0; i < d->mSelectedList->topLevelItemCount(); ++i ) {
220  aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( i ) );
221  if ( aItem ) {
222  al.append( aItem->addressee() );
223  }
224  }
225  } else {
226  QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
227  if ( !selected.isEmpty() ) {
228  aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
229  }
230  if ( aItem ) {
231  al.append( aItem->addressee() );
232  }
233  }
234 
235  return al;
236 }
237 
238 Addressee AddresseeDialog::getAddressee( QWidget *parent )
239 {
240  Addressee contact;
241 
242  QPointer<AddresseeDialog> dlg = new AddresseeDialog( parent );
243  if ( dlg->exec() && dlg ) {
244  contact = dlg->addressee();
245  }
246 
247  delete dlg;
248 
249  return contact;
250 }
251 
252 Addressee::List AddresseeDialog::getAddressees( QWidget *parent )
253 {
254  Addressee::List contacts;
255 
256  QPointer<AddresseeDialog> dlg = new AddresseeDialog( parent, true );
257  if ( dlg->exec() && dlg ) {
258  contacts = dlg->addressees();
259  }
260 
261  delete dlg;
262 
263  return contacts;
264 }
265 
266 void AddresseeDialog::Private::loadAddressBook()
267 {
268  mAddresseeList->clear();
269  mItemDict.clear();
270  mAddresseeEdit->completionObject()->clear();
271 
272 #ifndef KDEPIM_NO_KRESOURCES
273  AddressBook::Iterator it;
274  for ( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
275  AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
276  addCompletionItem( (*it).realName(), item );
277  addCompletionItem( (*it).preferredEmail(), item );
278  }
279 #endif
280 }
281 
282 void AddresseeDialog::Private::addCompletionItem( const QString &str, QTreeWidgetItem *item )
283 {
284  if ( str.isEmpty() ) {
285  return;
286  }
287 
288  mItemDict.insert( str, item );
289  mAddresseeEdit->completionObject()->addItem( str );
290 }
291 
292 void AddresseeDialog::Private::selectItem( const QString &str )
293 {
294  if ( str.isEmpty() ) {
295  return;
296  }
297 
298  QTreeWidgetItem *item = mItemDict.value( str, 0 );
299  if ( item ) {
300  mAddresseeList->blockSignals( true );
301  mAddresseeList->setItemSelected( item, true );
302  mAddresseeList->scrollToItem( item );
303  mAddresseeList->blockSignals( false );
304  }
305 }
306 
307 void AddresseeDialog::Private::updateEdit()
308 {
309  QList<QTreeWidgetItem*> selected = mAddresseeList->selectedItems();
310  if ( selected.isEmpty() ) {
311  return;
312  }
313  QTreeWidgetItem *item = selected.at( 0 );
314  mAddresseeEdit->setText( item->text( 0 ) );
315  mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
316 }
317 
318 void AddresseeDialog::Private::addSelected( QTreeWidgetItem *item )
319 {
320  AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
321  if ( !addrItem ) {
322  return;
323  }
324 
325  Addressee a = addrItem->addressee();
326 
327  QTreeWidgetItem *selectedItem = mSelectedDict.value( a.uid(), 0 );
328  if ( !selectedItem ) {
329  selectedItem = new AddresseeItem( mSelectedList, a );
330  mSelectedDict.insert( a.uid(), selectedItem );
331  }
332 }
333 
334 void AddresseeDialog::Private::removeSelected()
335 {
336  QList<QTreeWidgetItem*> selected = mSelectedList->selectedItems();
337  if ( selected.isEmpty() ) {
338  return;
339  }
340  AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
341  if ( !addrItem ) {
342  return;
343  }
344 
345  mSelectedDict.remove( addrItem->addressee().uid() );
346  delete addrItem;
347 }
348 
349 void AddresseeDialog::Private::addressBookChanged()
350 {
351  loadAddressBook();
352 }
353 
354 #include "addresseedialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Nov 26 2012 16:49:46 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.9.3 API Reference

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