• Skip to content
  • Skip to link menu
KDE 4.4 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( QLatin1String( "<\\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 
00145     QGroupBox *selectedGroup =
00146       new QGroupBox( i18nc( "@title:group selected addressees", "Selected" ), topWidget );
00147     QHBoxLayout *groupLayout = new QHBoxLayout;
00148     selectedGroup->setLayout( groupLayout );
00149     selectedLayout->addWidget( selectedGroup );
00150 
00151     d->mSelectedList = new QTreeWidget( selectedGroup );
00152     groupLayout->addWidget( d->mSelectedList );
00153     d->mSelectedList->setColumnCount( 2 );
00154     QStringList headerTitles;
00155     headerTitles << i18nc( "@title:column addressee name", "Name" )
00156                  << i18nc( "@title:column addressee email", "Email" );
00157     d->mSelectedList->setHeaderItem( new QTreeWidgetItem( headerTitles ) );
00158 
00159     connect( d->mSelectedList, SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int ) ),
00160              SLOT( removeSelected() ) );
00161 
00162     QPushButton *unselectButton =
00163       new QPushButton( i18nc( "@action:button unselect addressee", "Unselect" ), selectedGroup );
00164     selectedLayout->addWidget( unselectButton );
00165     connect( unselectButton, SIGNAL( clicked() ), SLOT( removeSelected() ) );
00166 
00167     connect( d->mAddresseeList, SIGNAL( itemClicked( QTreeWidgetItem *, int ) ),
00168              SLOT( addSelected( QTreeWidgetItem * ) ) );
00169 
00170     setInitialSize( QSize( 650, 350 ) );
00171   }
00172 
00173   d->mAddressBook = StdAddressBook::self( true );
00174   connect( d->mAddressBook, SIGNAL( addressBookChanged( AddressBook* ) ),
00175            SLOT( addressBookChanged() ) );
00176   connect( d->mAddressBook, SIGNAL( loadingFinished( Resource* ) ),
00177            SLOT( addressBookChanged() ) );
00178 
00179   d->loadAddressBook();
00180 }
00181 
00182 AddresseeDialog::~AddresseeDialog()
00183 {
00184   delete d;
00185 }
00186 
00187 Addressee AddresseeDialog::addressee() const
00188 {
00189   AddresseeItem *aItem = 0;
00190 
00191   if ( d->mMultiple ) {
00192     aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( 0 ) );
00193   } else {
00194     QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
00195     if ( selected.count() != 0 ) {
00196       aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00197     }
00198   }
00199 
00200   if ( aItem ) {
00201     return aItem->addressee();
00202   }
00203   return Addressee();
00204 }
00205 
00206 Addressee::List AddresseeDialog::addressees() const
00207 {
00208   Addressee::List al;
00209   AddresseeItem *aItem = 0;
00210 
00211   if ( d->mMultiple ) {
00212     for ( int i = 0; i < d->mSelectedList->topLevelItemCount(); ++i ) {
00213       aItem = dynamic_cast<AddresseeItem *>( d->mSelectedList->topLevelItem( i ) );
00214       if ( aItem ) {
00215         al.append( aItem->addressee() );
00216       }
00217     }
00218   } else {
00219     QList<QTreeWidgetItem*> selected = d->mAddresseeList->selectedItems();
00220     if ( selected.count() != 0 ) {
00221       aItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00222     }
00223     if ( aItem ) {
00224       al.append( aItem->addressee() );
00225     }
00226   }
00227 
00228   return al;
00229 }
00230 
00231 Addressee AddresseeDialog::getAddressee( QWidget *parent )
00232 {
00233   AddresseeDialog dlg( parent );
00234   if ( dlg.exec() ) {
00235     return dlg.addressee();
00236   }
00237 
00238   return Addressee();
00239 }
00240 
00241 Addressee::List AddresseeDialog::getAddressees( QWidget *parent )
00242 {
00243   AddresseeDialog dlg( parent, true );
00244   if ( dlg.exec() ) {
00245     return dlg.addressees();
00246   }
00247 
00248   return Addressee::List();
00249 }
00250 
00251 void AddresseeDialog::Private::loadAddressBook()
00252 {
00253   mAddresseeList->clear();
00254   mItemDict.clear();
00255   mAddresseeEdit->completionObject()->clear();
00256 
00257   AddressBook::Iterator it;
00258   for ( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00259     AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
00260     addCompletionItem( (*it).realName(), item );
00261     addCompletionItem( (*it).preferredEmail(), item );
00262   }
00263 }
00264 
00265 void AddresseeDialog::Private::addCompletionItem( const QString &str, QTreeWidgetItem *item )
00266 {
00267   if ( str.isEmpty() ) {
00268     return;
00269   }
00270 
00271   mItemDict.insert( str, item );
00272   mAddresseeEdit->completionObject()->addItem( str );
00273 }
00274 
00275 void AddresseeDialog::Private::selectItem( const QString &str )
00276 {
00277   if ( str.isEmpty() ) {
00278     return;
00279   }
00280 
00281   QTreeWidgetItem *item = mItemDict.value( str, 0 );
00282   if ( item ) {
00283     mAddresseeList->blockSignals( true );
00284     mAddresseeList->setItemSelected( item, true );
00285     mAddresseeList->scrollToItem( item );
00286     mAddresseeList->blockSignals( false );
00287   }
00288 }
00289 
00290 void AddresseeDialog::Private::updateEdit()
00291 {
00292   QList<QTreeWidgetItem*> selected = mAddresseeList->selectedItems();
00293   if ( selected.count() == 0 ) {
00294     return;
00295   }
00296   QTreeWidgetItem *item = selected.at( 0 );
00297   mAddresseeEdit->setText( item->text( 0 ) );
00298   mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
00299 }
00300 
00301 void AddresseeDialog::Private::addSelected( QTreeWidgetItem *item )
00302 {
00303   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
00304   if ( !addrItem ) {
00305     return;
00306   }
00307 
00308   Addressee a = addrItem->addressee();
00309 
00310   QTreeWidgetItem *selectedItem = mSelectedDict.value( a.uid(), 0 );
00311   if ( !selectedItem ) {
00312     selectedItem = new AddresseeItem( mSelectedList, a );
00313     mSelectedDict.insert( a.uid(), selectedItem );
00314   }
00315 }
00316 
00317 void AddresseeDialog::Private::removeSelected()
00318 {
00319   QList<QTreeWidgetItem*> selected = mSelectedList->selectedItems();
00320   if ( selected.count() == 0 ) {
00321     return;
00322   }
00323   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( selected.at( 0 ) );
00324   if ( !addrItem ) {
00325     return;
00326   }
00327 
00328   mSelectedDict.remove( addrItem->addressee().uid() );
00329   delete addrItem;
00330 }
00331 
00332 void AddresseeDialog::Private::addressBookChanged()
00333 {
00334   loadAddressBook();
00335 }
00336 
00337 #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.6.2-20100208
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