• Skip to content
  • Skip to link menu
KDE 4.1 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( i18nc( "@title:window", "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 << i18nc( "@title:column addressee name", "Name" )
00123                << i18nc( "@title:column addressee email", "Email" );
00124   d->mAddresseeList->setHeaderItem( new QTreeWidgetItem( headerTitles ) );
00125   listLayout->addWidget( d->mAddresseeList );
00126   connect( d->mAddresseeList, SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int ) ),
00127            SLOT( accept() ) );
00128   connect( d->mAddresseeList, SIGNAL( itemSelectionChanged() ),
00129            SLOT( updateEdit() ) );
00130 
00131   d->mAddresseeEdit = new KLineEdit( topWidget );
00132   d->mAddresseeEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
00133   connect( d->mAddresseeEdit->completionObject(), SIGNAL( match( const QString & ) ),
00134            SLOT( selectItem( const QString & ) ) );
00135   d->mAddresseeEdit->setFocus();
00136   d->mAddresseeEdit->completionObject()->setIgnoreCase( true );
00137   listLayout->addWidget( d->mAddresseeEdit );
00138 
00139   setInitialSize( QSize( 450, 300 ) );
00140 
00141   if ( d->mMultiple ) {
00142     QBoxLayout *selectedLayout = new QVBoxLayout;
00143     topLayout->addLayout( selectedLayout );
00144     topLayout->setSpacing( spacingHint() );
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   d->mAddressBook = StdAddressBook::self( true );
00175   connect( d->mAddressBook, SIGNAL( addressBookChanged( AddressBook* ) ),
00176            SLOT( addressBookChanged() ) );
00177   connect( d->mAddressBook, SIGNAL( loadingFinished( Resource* ) ),
00178            SLOT( addressBookChanged() ) );
00179 
00180   d->loadAddressBook();
00181 }
00182 
00183 AddresseeDialog::~AddresseeDialog()
00184 {
00185   delete d;
00186 }
00187 
00188 Addressee AddresseeDialog::addressee() const
00189 {
00190   AddresseeItem *aItem = 0;
00191 
00192   if ( d->mMultiple ) {
00193     aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( 0 ) );
00194   } else {
00195     QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
00196     if ( selected.count() != 0 ) {
00197       aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00198     }
00199   }
00200 
00201   if ( aItem ) {
00202     return aItem->addressee();
00203   }
00204   return Addressee();
00205 }
00206 
00207 Addressee::List AddresseeDialog::addressees() const
00208 {
00209   Addressee::List al;
00210   AddresseeItem *aItem = 0;
00211 
00212   if ( d->mMultiple ) {
00213     for ( int i = 0; i < d->mSelectedList->topLevelItemCount(); i++ ) {
00214       aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( i ) );
00215       if ( aItem ) {
00216         al.append( aItem->addressee() );
00217       }
00218     }
00219   } else {
00220     QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
00221     if ( selected.count() != 0 ) {
00222       aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00223     }
00224     if ( aItem ) {
00225       al.append( aItem->addressee() );
00226     }
00227   }
00228 
00229   return al;
00230 }
00231 
00232 Addressee AddresseeDialog::getAddressee( QWidget *parent )
00233 {
00234   AddresseeDialog dlg( parent );
00235   if ( dlg.exec() ) {
00236     return dlg.addressee();
00237   }
00238 
00239   return Addressee();
00240 }
00241 
00242 Addressee::List AddresseeDialog::getAddressees( QWidget *parent )
00243 {
00244   AddresseeDialog dlg( parent, true );
00245   if ( dlg.exec() ) {
00246     return dlg.addressees();
00247   }
00248 
00249   return Addressee::List();
00250 }
00251 
00252 void AddresseeDialog::Private::loadAddressBook()
00253 {
00254   mAddresseeList->clear();
00255   mItemDict.clear();
00256   mAddresseeEdit->completionObject()->clear();
00257 
00258   AddressBook::Iterator it;
00259   for ( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00260     AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
00261     addCompletionItem( (*it).realName(), item );
00262     addCompletionItem( (*it).preferredEmail(), item );
00263   }
00264 }
00265 
00266 void AddresseeDialog::Private::addCompletionItem( const QString &str, QTreeWidgetItem *item )
00267 {
00268   if ( str.isEmpty() ) {
00269     return;
00270   }
00271 
00272   mItemDict.insert( str, item );
00273   mAddresseeEdit->completionObject()->addItem( str );
00274 }
00275 
00276 void AddresseeDialog::Private::selectItem( const QString &str )
00277 {
00278   if ( str.isEmpty() ) {
00279     return;
00280   }
00281 
00282   QTreeWidgetItem *item = mItemDict.value( str, 0 );
00283   if ( item ) {
00284     mAddresseeList->blockSignals( true );
00285     mAddresseeList->setItemSelected( item, true );
00286     mAddresseeList->scrollToItem( item );
00287     mAddresseeList->blockSignals( false );
00288   }
00289 }
00290 
00291 void AddresseeDialog::Private::updateEdit()
00292 {
00293   QList<QTreeWidgetItem*> selected = mAddresseeList->selectedItems();
00294   if ( selected.count() == 0 ) {
00295     return;
00296   }
00297   QTreeWidgetItem *item = selected.at( 0 );
00298   mAddresseeEdit->setText( item->text( 0 ) );
00299   mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
00300 }
00301 
00302 void AddresseeDialog::Private::addSelected( QTreeWidgetItem *item )
00303 {
00304   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
00305   if ( !addrItem ) {
00306     return;
00307   }
00308 
00309   Addressee a = addrItem->addressee();
00310 
00311   QTreeWidgetItem *selectedItem = mSelectedDict.value( a.uid(), 0 );
00312   if ( !selectedItem ) {
00313     selectedItem = new AddresseeItem( mSelectedList, a );
00314     mSelectedDict.insert( a.uid(), selectedItem );
00315   }
00316 }
00317 
00318 void AddresseeDialog::Private::removeSelected()
00319 {
00320   QList<QTreeWidgetItem*> selected = mAddresseeList->selectedItems();
00321   if ( selected.count() == 0 ) {
00322     return;
00323   }
00324   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00325   if ( !addrItem ) {
00326     return;
00327   }
00328 
00329   mSelectedDict.remove( addrItem->addressee().uid() );
00330   delete addrItem;
00331 }
00332 
00333 void AddresseeDialog::Private::addressBookChanged()
00334 {
00335   loadAddressBook();
00336 }
00337 
00338 #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
  • 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.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