• Skip to content
  • Skip to link menu
KDE 4.0 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 <QtGui/QGroupBox>
00028 #include <QtGui/QLayout>
00029 #include <QtGui/QPushButton>
00030 #include <QtCore/QRegExp>
00031 
00032 using namespace KABC;
00033 
00034 class AddresseeItem::Private
00035 {
00036   public:
00037     Addressee mAddressee;
00038 };
00039 
00040 AddresseeItem::AddresseeItem( QTreeWidget *parent, const Addressee &addressee ) :
00041   QTreeWidgetItem( parent ), d( new Private )
00042 {
00043   d->mAddressee = addressee;
00044 
00045   setText( Name, addressee.realName() );
00046   setText( Email, addressee.preferredEmail() );
00047 }
00048 
00049 AddresseeItem::~AddresseeItem()
00050 {
00051   delete d;
00052 }
00053 
00054 Addressee AddresseeItem::addressee() const
00055 {
00056   return d->mAddressee;
00057 }
00058 
00059 QString AddresseeItem::key( int column, bool ) const
00060 {
00061   if ( column == Email ) {
00062     QString value = text( Email );
00063     QRegExp emailRe( "<\\S*>" );
00064     int match = emailRe.indexIn( value );
00065     if ( match > -1 ) {
00066       value = value.mid( match + 1, emailRe.matchedLength() - 2 );
00067     }
00068 
00069     return value.toLower();
00070   }
00071 
00072   return text( column ).toLower();
00073 }
00074 
00075 class AddresseeDialog::Private
00076 {
00077   public:
00078     Private( bool multiple )
00079       : mMultiple( multiple )
00080     {
00081     }
00082 
00083     void addressBookChanged();
00084     void selectItem( const QString & );
00085     void updateEdit();
00086     void addSelected( QTreeWidgetItem *item );
00087     void removeSelected();
00088 
00089     void loadAddressBook();
00090     void addCompletionItem( const QString &str, QTreeWidgetItem *item );
00091 
00092     bool mMultiple;
00093 
00094     QTreeWidget *mAddresseeList;
00095     KLineEdit *mAddresseeEdit;
00096 
00097     QTreeWidget *mSelectedList;
00098 
00099     AddressBook *mAddressBook;
00100 
00101     QHash<QString, QTreeWidgetItem*> mItemDict;
00102     QHash<QString, QTreeWidgetItem*> mSelectedDict;
00103 };
00104 
00105 AddresseeDialog::AddresseeDialog( QWidget *parent, bool multiple )
00106   : KDialog( parent ), d( new Private( multiple ) )
00107 {
00108   setCaption( i18n( "Select Addressee" ) );
00109   setButtons( Ok | Cancel );
00110   setDefaultButton( Ok );
00111 
00112   QWidget *topWidget = new QWidget( this );
00113   setMainWidget( topWidget );
00114 
00115   QBoxLayout *topLayout = new QHBoxLayout( topWidget );
00116   QBoxLayout *listLayout = new QVBoxLayout;
00117   topLayout->addLayout( listLayout );
00118 
00119   d->mAddresseeList = new QTreeWidget( topWidget );
00120   d->mAddresseeList->setColumnCount( 2 );
00121   QStringList headerTitles;
00122   headerTitles << i18n( "Name" ) << i18n( "Email" );
00123   d->mAddresseeList->setHeaderItem( new QTreeWidgetItem( headerTitles ) );
00124   listLayout->addWidget( d->mAddresseeList );
00125   connect( d->mAddresseeList, SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int ) ),
00126            SLOT( accept() ) );
00127   connect( d->mAddresseeList, SIGNAL( itemSelectionChanged() ),
00128            SLOT( updateEdit() ) );
00129 
00130   d->mAddresseeEdit = new KLineEdit( topWidget );
00131   d->mAddresseeEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
00132   connect( d->mAddresseeEdit->completionObject(), SIGNAL( match( const QString & ) ),
00133            SLOT( selectItem( const QString & ) ) );
00134   d->mAddresseeEdit->setFocus();
00135   d->mAddresseeEdit->completionObject()->setIgnoreCase( true );
00136   listLayout->addWidget( d->mAddresseeEdit );
00137 
00138   setInitialSize( QSize( 450, 300 ) );
00139 
00140   if ( d->mMultiple ) {
00141     QBoxLayout *selectedLayout = new QVBoxLayout;
00142     topLayout->addLayout( selectedLayout );
00143     topLayout->setSpacing( spacingHint() );
00144 
00145     QGroupBox *selectedGroup = new QGroupBox( i18n( "Selected" ), topWidget );
00146     QHBoxLayout *groupLayout = new QHBoxLayout;
00147     selectedGroup->setLayout( groupLayout );
00148     selectedLayout->addWidget( selectedGroup );
00149 
00150     d->mSelectedList = new QTreeWidget( selectedGroup );
00151     groupLayout->addWidget( d->mSelectedList );
00152     d->mSelectedList->setColumnCount( 2 );
00153     QStringList headerTitles;
00154     headerTitles << i18n( "Name" ) << i18n( "Email" );
00155     d->mSelectedList->setHeaderItem( new QTreeWidgetItem( headerTitles ) );
00156 
00157     connect( d->mSelectedList, SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int ) ),
00158              SLOT( removeSelected() ) );
00159 
00160     QPushButton *unselectButton = new QPushButton( i18n( "Unselect" ), selectedGroup );
00161     selectedLayout->addWidget( unselectButton );
00162     connect( unselectButton, SIGNAL( clicked() ), SLOT( removeSelected() ) );
00163 
00164     connect( d->mAddresseeList, SIGNAL( itemClicked( QTreeWidgetItem *, int ) ),
00165              SLOT( addSelected( QTreeWidgetItem * ) ) );
00166 
00167     setInitialSize( QSize( 650, 350 ) );
00168   }
00169 
00170   d->mAddressBook = StdAddressBook::self( true );
00171   connect( d->mAddressBook, SIGNAL( addressBookChanged( AddressBook* ) ),
00172            SLOT( addressBookChanged() ) );
00173   connect( d->mAddressBook, SIGNAL( loadingFinished( Resource* ) ),
00174            SLOT( addressBookChanged() ) );
00175 
00176   d->loadAddressBook();
00177 }
00178 
00179 AddresseeDialog::~AddresseeDialog()
00180 {
00181   delete d;
00182 }
00183 
00184 Addressee AddresseeDialog::addressee() const
00185 {
00186   AddresseeItem *aItem = 0;
00187 
00188   if ( d->mMultiple ) {
00189     aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( 0 ) );
00190   } else {
00191     QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
00192     if ( selected.count() != 0 ) {
00193       aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00194     }
00195   }
00196 
00197   if ( aItem ) {
00198     return aItem->addressee();
00199   }
00200   return Addressee();
00201 }
00202 
00203 Addressee::List AddresseeDialog::addressees() const
00204 {
00205   Addressee::List al;
00206   AddresseeItem *aItem = 0;
00207 
00208   if ( d->mMultiple ) {
00209     for ( int i = 0; i < d->mSelectedList->topLevelItemCount(); i++ ) {
00210       aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( i ) );
00211       if ( aItem ) {
00212         al.append( aItem->addressee() );
00213       }
00214     }
00215   } else {
00216     QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
00217     if ( selected.count() != 0 ) {
00218       aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00219     }
00220     if ( aItem ) {
00221       al.append( aItem->addressee() );
00222     }
00223   }
00224 
00225   return al;
00226 }
00227 
00228 Addressee AddresseeDialog::getAddressee( QWidget *parent )
00229 {
00230   AddresseeDialog dlg( parent );
00231   if ( dlg.exec() ) {
00232     return dlg.addressee();
00233   }
00234 
00235   return Addressee();
00236 }
00237 
00238 Addressee::List AddresseeDialog::getAddressees( QWidget *parent )
00239 {
00240   AddresseeDialog dlg( parent, true );
00241   if ( dlg.exec() ) {
00242     return dlg.addressees();
00243   }
00244 
00245   return Addressee::List();
00246 }
00247 
00248 void AddresseeDialog::Private::loadAddressBook()
00249 {
00250   mAddresseeList->clear();
00251   mItemDict.clear();
00252   mAddresseeEdit->completionObject()->clear();
00253 
00254   AddressBook::Iterator it;
00255   for ( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00256     AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
00257     addCompletionItem( (*it).realName(), item );
00258     addCompletionItem( (*it).preferredEmail(), item );
00259   }
00260 }
00261 
00262 void AddresseeDialog::Private::addCompletionItem( const QString &str, QTreeWidgetItem *item )
00263 {
00264   if ( str.isEmpty() ) {
00265     return;
00266   }
00267 
00268   mItemDict.insert( str, item );
00269   mAddresseeEdit->completionObject()->addItem( str );
00270 }
00271 
00272 void AddresseeDialog::Private::selectItem( const QString &str )
00273 {
00274   if ( str.isEmpty() ) {
00275     return;
00276   }
00277 
00278   QTreeWidgetItem *item = mItemDict.value( str, 0 );
00279   if ( item ) {
00280     mAddresseeList->blockSignals( true );
00281     mAddresseeList->setItemSelected( item, true );
00282     mAddresseeList->scrollToItem( item );
00283     mAddresseeList->blockSignals( false );
00284   }
00285 }
00286 
00287 void AddresseeDialog::Private::updateEdit()
00288 {
00289   QList<QTreeWidgetItem*> selected = mAddresseeList->selectedItems();
00290   if ( selected.count() == 0 ) {
00291     return;
00292   }
00293   QTreeWidgetItem *item = selected.at( 0 );
00294   mAddresseeEdit->setText( item->text( 0 ) );
00295   mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
00296 }
00297 
00298 void AddresseeDialog::Private::addSelected( QTreeWidgetItem *item )
00299 {
00300   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
00301   if ( !addrItem ) {
00302     return;
00303   }
00304 
00305   Addressee a = addrItem->addressee();
00306 
00307   QTreeWidgetItem *selectedItem = mSelectedDict.value( a.uid(), 0 );
00308   if ( !selectedItem ) {
00309     selectedItem = new AddresseeItem( mSelectedList, a );
00310     mSelectedDict.insert( a.uid(), selectedItem );
00311   }
00312 }
00313 
00314 void AddresseeDialog::Private::removeSelected()
00315 {
00316   QList<QTreeWidgetItem*> selected = mAddresseeList->selectedItems();
00317   if ( selected.count() == 0 ) {
00318     return;
00319   }
00320   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00321   if ( !addrItem ) {
00322     return;
00323   }
00324 
00325   mSelectedDict.remove( addrItem->addressee().uid() );
00326   delete addrItem;
00327 }
00328 
00329 void AddresseeDialog::Private::addressBookChanged()
00330 {
00331   loadAddressBook();
00332 }
00333 
00334 #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"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.5
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