• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

kabc

addresseedialog.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "addresseedialog.h"
00022 #include "stdaddressbook.h"
00023 
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 
00027 #include <QtCore/QPointer>
00028 #include <QtCore/QRegExp>
00029 #include <QtGui/QGroupBox>
00030 #include <QtGui/QLayout>
00031 #include <QtGui/QPushButton>
00032 
00033 using namespace KABC;
00034 
00035 class AddresseeItem::Private
00036 {
00037   public:
00038     Addressee mAddressee;
00039 };
00040 
00041 AddresseeItem::AddresseeItem( QTreeWidget *parent, const Addressee &addressee ) :
00042   QTreeWidgetItem( parent ), d( new Private )
00043 {
00044   d->mAddressee = addressee;
00045 
00046   setText( Name, addressee.realName() );
00047   setText( Email, addressee.preferredEmail() );
00048 }
00049 
00050 AddresseeItem::~AddresseeItem()
00051 {
00052   delete d;
00053 }
00054 
00055 Addressee AddresseeItem::addressee() const
00056 {
00057   return d->mAddressee;
00058 }
00059 
00060 QString AddresseeItem::key( int column, bool ) const
00061 {
00062   if ( column == Email ) {
00063     QString value = text( Email );
00064     QRegExp emailRe( QLatin1String( "<\\S*>" ) );
00065     int match = emailRe.indexIn( value );
00066     if ( match > -1 ) {
00067       value = value.mid( match + 1, emailRe.matchedLength() - 2 );
00068     }
00069 
00070     return value.toLower();
00071   }
00072 
00073   return text( column ).toLower();
00074 }
00075 
00076 class AddresseeDialog::Private
00077 {
00078   public:
00079     Private( bool multiple )
00080       : mMultiple( multiple )
00081     {
00082     }
00083 
00084     void addressBookChanged();
00085     void selectItem( const QString & );
00086     void updateEdit();
00087     void addSelected( QTreeWidgetItem *item );
00088     void removeSelected();
00089 
00090     void loadAddressBook();
00091     void addCompletionItem( const QString &str, QTreeWidgetItem *item );
00092 
00093     bool mMultiple;
00094 
00095     QTreeWidget *mAddresseeList;
00096     KLineEdit *mAddresseeEdit;
00097 
00098     QTreeWidget *mSelectedList;
00099 
00100     AddressBook *mAddressBook;
00101 
00102     QHash<QString, QTreeWidgetItem*> mItemDict;
00103     QHash<QString, QTreeWidgetItem*> mSelectedDict;
00104 };
00105 
00106 AddresseeDialog::AddresseeDialog( QWidget *parent, bool multiple )
00107   : KDialog( parent ), d( new Private( multiple ) )
00108 {
00109   setCaption( i18nc( "@title:window", "Select Addressee" ) );
00110   setButtons( Ok | Cancel );
00111   setDefaultButton( Ok );
00112 
00113   QWidget *topWidget = new QWidget( this );
00114   setMainWidget( topWidget );
00115 
00116   QBoxLayout *topLayout = new QHBoxLayout( topWidget );
00117   QBoxLayout *listLayout = new QVBoxLayout;
00118   topLayout->addLayout( listLayout );
00119 
00120   d->mAddresseeList = new QTreeWidget( topWidget );
00121   d->mAddresseeList->setColumnCount( 2 );
00122   QStringList headerTitles;
00123   headerTitles << i18nc( "@title:column addressee name", "Name" )
00124                << i18nc( "@title:column addressee email", "Email" );
00125   d->mAddresseeList->setHeaderItem( new QTreeWidgetItem( headerTitles ) );
00126   listLayout->addWidget( d->mAddresseeList );
00127   connect( d->mAddresseeList, SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int ) ),
00128            SLOT( accept() ) );
00129   connect( d->mAddresseeList, SIGNAL( itemSelectionChanged() ),
00130            SLOT( updateEdit() ) );
00131 
00132   d->mAddresseeEdit = new KLineEdit( topWidget );
00133   d->mAddresseeEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
00134   connect( d->mAddresseeEdit->completionObject(), SIGNAL( match( const QString & ) ),
00135            SLOT( selectItem( const QString & ) ) );
00136   d->mAddresseeEdit->setFocus();
00137   d->mAddresseeEdit->completionObject()->setIgnoreCase( true );
00138   listLayout->addWidget( d->mAddresseeEdit );
00139 
00140   setInitialSize( QSize( 450, 300 ) );
00141 
00142   if ( d->mMultiple ) {
00143     QBoxLayout *selectedLayout = new QVBoxLayout;
00144     topLayout->addLayout( selectedLayout );
00145 
00146     QGroupBox *selectedGroup =
00147       new QGroupBox( i18nc( "@title:group selected addressees", "Selected" ), topWidget );
00148     QHBoxLayout *groupLayout = new QHBoxLayout;
00149     selectedGroup->setLayout( groupLayout );
00150     selectedLayout->addWidget( selectedGroup );
00151 
00152     d->mSelectedList = new QTreeWidget( selectedGroup );
00153     groupLayout->addWidget( d->mSelectedList );
00154     d->mSelectedList->setColumnCount( 2 );
00155     QStringList headerTitles;
00156     headerTitles << i18nc( "@title:column addressee name", "Name" )
00157                  << i18nc( "@title:column addressee email", "Email" );
00158     d->mSelectedList->setHeaderItem( new QTreeWidgetItem( headerTitles ) );
00159 
00160     connect( d->mSelectedList, SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int ) ),
00161              SLOT( removeSelected() ) );
00162 
00163     QPushButton *unselectButton =
00164       new QPushButton( i18nc( "@action:button unselect addressee", "Unselect" ), selectedGroup );
00165     selectedLayout->addWidget( unselectButton );
00166     connect( unselectButton, SIGNAL( clicked() ), SLOT( removeSelected() ) );
00167 
00168     connect( d->mAddresseeList, SIGNAL( itemClicked( QTreeWidgetItem *, int ) ),
00169              SLOT( addSelected( QTreeWidgetItem * ) ) );
00170 
00171     setInitialSize( QSize( 650, 350 ) );
00172   }
00173 
00174 #ifndef KDEPIM_NO_KRESOURCES
00175   d->mAddressBook = StdAddressBook::self( true );
00176   connect( d->mAddressBook, SIGNAL( addressBookChanged( AddressBook* ) ),
00177            SLOT( addressBookChanged() ) );
00178   connect( d->mAddressBook, SIGNAL( loadingFinished( Resource* ) ),
00179            SLOT( addressBookChanged() ) );
00180 #endif
00181 
00182   d->loadAddressBook();
00183 }
00184 
00185 AddresseeDialog::~AddresseeDialog()
00186 {
00187   delete d;
00188 }
00189 
00190 Addressee AddresseeDialog::addressee() const
00191 {
00192   AddresseeItem *aItem = 0;
00193 
00194   if ( d->mMultiple ) {
00195     aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( 0 ) );
00196   } else {
00197     QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
00198     if ( !selected.isEmpty() ) {
00199       aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00200     }
00201   }
00202 
00203   if ( aItem ) {
00204     return aItem->addressee();
00205   }
00206   return Addressee();
00207 }
00208 
00209 Addressee::List AddresseeDialog::addressees() const
00210 {
00211   Addressee::List al;
00212   AddresseeItem *aItem = 0;
00213 
00214   if ( d->mMultiple ) {
00215     for ( int i = 0; i < d->mSelectedList->topLevelItemCount(); ++i ) {
00216       aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( i ) );
00217       if ( aItem ) {
00218         al.append( aItem->addressee() );
00219       }
00220     }
00221   } else {
00222     QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
00223     if ( !selected.isEmpty() ) {
00224       aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00225     }
00226     if ( aItem ) {
00227       al.append( aItem->addressee() );
00228     }
00229   }
00230 
00231   return al;
00232 }
00233 
00234 Addressee AddresseeDialog::getAddressee( QWidget *parent )
00235 {
00236   Addressee contact;
00237 
00238   QPointer<AddresseeDialog> dlg = new AddresseeDialog( parent );
00239   if ( dlg->exec() && dlg ) {
00240     contact = dlg->addressee();
00241   }
00242 
00243   delete dlg;
00244 
00245   return contact;
00246 }
00247 
00248 Addressee::List AddresseeDialog::getAddressees( QWidget *parent )
00249 {
00250   Addressee::List contacts;
00251 
00252   QPointer<AddresseeDialog> dlg = new AddresseeDialog( parent, true );
00253   if ( dlg->exec() && dlg ) {
00254     contacts = dlg->addressees();
00255   }
00256 
00257   delete dlg;
00258 
00259   return contacts;
00260 }
00261 
00262 void AddresseeDialog::Private::loadAddressBook()
00263 {
00264   mAddresseeList->clear();
00265   mItemDict.clear();
00266   mAddresseeEdit->completionObject()->clear();
00267 
00268 #ifndef KDEPIM_NO_KRESOURCES
00269   AddressBook::Iterator it;
00270   for ( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00271     AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
00272     addCompletionItem( (*it).realName(), item );
00273     addCompletionItem( (*it).preferredEmail(), item );
00274   }
00275 #endif
00276 }
00277 
00278 void AddresseeDialog::Private::addCompletionItem( const QString &str, QTreeWidgetItem *item )
00279 {
00280   if ( str.isEmpty() ) {
00281     return;
00282   }
00283 
00284   mItemDict.insert( str, item );
00285   mAddresseeEdit->completionObject()->addItem( str );
00286 }
00287 
00288 void AddresseeDialog::Private::selectItem( const QString &str )
00289 {
00290   if ( str.isEmpty() ) {
00291     return;
00292   }
00293 
00294   QTreeWidgetItem *item = mItemDict.value( str, 0 );
00295   if ( item ) {
00296     mAddresseeList->blockSignals( true );
00297     mAddresseeList->setItemSelected( item, true );
00298     mAddresseeList->scrollToItem( item );
00299     mAddresseeList->blockSignals( false );
00300   }
00301 }
00302 
00303 void AddresseeDialog::Private::updateEdit()
00304 {
00305   QList<QTreeWidgetItem*> selected = mAddresseeList->selectedItems();
00306   if ( selected.isEmpty() ) {
00307     return;
00308   }
00309   QTreeWidgetItem *item = selected.at( 0 );
00310   mAddresseeEdit->setText( item->text( 0 ) );
00311   mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
00312 }
00313 
00314 void AddresseeDialog::Private::addSelected( QTreeWidgetItem *item )
00315 {
00316   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
00317   if ( !addrItem ) {
00318     return;
00319   }
00320 
00321   Addressee a = addrItem->addressee();
00322 
00323   QTreeWidgetItem *selectedItem = mSelectedDict.value( a.uid(), 0 );
00324   if ( !selectedItem ) {
00325     selectedItem = new AddresseeItem( mSelectedList, a );
00326     mSelectedDict.insert( a.uid(), selectedItem );
00327   }
00328 }
00329 
00330 void AddresseeDialog::Private::removeSelected()
00331 {
00332   QList<QTreeWidgetItem*> selected = mSelectedList->selectedItems();
00333   if ( selected.isEmpty() ) {
00334     return;
00335   }
00336   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00337   if ( !addrItem ) {
00338     return;
00339   }
00340 
00341   mSelectedDict.remove( addrItem->addressee().uid() );
00342   delete addrItem;
00343 }
00344 
00345 void AddresseeDialog::Private::addressBookChanged()
00346 {
00347   loadAddressBook();
00348 }
00349 
00350 #include "addresseedialog.moc"

kabc

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal