addresseedialog.cpp Source File (kabc Library)

| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# 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., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <qlayout.h>
    00022 #include <qpushbutton.h>
    00023 #include <qgroupbox.h>
    00024 #include <qregexp.h>
    00025
    00026 #include <klocale.h>
    00027 #include <kdebug.h>
    00028
    00029 #include "stdaddressbook.h"
    00030
    00031 #include "addresseedialog.h"
    00032 #include "addresseedialog.moc"
    00033
    00034 using namespace KABC;
    00035
    00036 AddresseeItem::AddresseeItem( QListView *parent, const Addressee &addressee ) :
    00037   QListViewItem( parent ),
    00038   mAddressee( addressee )
    00039 {
    00040   setText( Name, addressee.realName() );
    00041   setText( Email, addressee.preferredEmail() );
    00042 }
    00043
    00044 QString AddresseeItem::key( int column, bool ) const
    00045 {
    00046   if (column == Email) {
    00047     QString value = text(Email);
    00048     QRegExp emailRe("<\\S*>");
    00049     int match = emailRe.search(value);
    00050     if (match > -1)
    00051       value = value.mid(match + 1, emailRe.matchedLength() - 2);
    00052
    00053     return value.lower();
    00054   }
    00055
    00056   return text(column).lower();
    00057 }
    00058
    00059 AddresseeDialog::AddresseeDialog( QWidget *parent, bool multiple ) :
    00060   KDialogBase( KDialogBase::Plain, i18n("Select Addressee"),
    00061                Ok|Cancel, Ok, parent ), mMultiple( multiple )
    00062 {
    00063   QWidget *topWidget = plainPage();
    00064
    00065   QBoxLayout *topLayout = new QHBoxLayout( topWidget );
    00066   QBoxLayout *listLayout = new QVBoxLayout;
    00067   topLayout->addLayout( listLayout );
    00068
    00069   mAddresseeList = new KListView( topWidget );
    00070   mAddresseeList->addColumn( i18n("Name") );
    00071   mAddresseeList->addColumn( i18n("Email") );
    00072   mAddresseeList->setAllColumnsShowFocus( true );
    00073   listLayout->addWidget( mAddresseeList );
    00074   connect( mAddresseeList, SIGNAL( doubleClicked( QListViewItem * ) ),
    00075            SLOT( slotOk() ) );
    00076   connect( mAddresseeList, SIGNAL( selectionChanged( QListViewItem * ) ),
    00077            SLOT( updateEdit( QListViewItem * ) ) );
    00078
    00079   mAddresseeEdit = new KLineEdit( topWidget );
    00080   mAddresseeEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
    00081   connect( mAddresseeEdit->completionObject(), SIGNAL( match( const QString & ) ),
    00082            SLOT( selectItem( const QString & ) ) );
    00083   mAddresseeEdit->setFocus();
    00084   listLayout->addWidget( mAddresseeEdit );
    00085
    00086   if ( mMultiple ) {
    00087     QBoxLayout *selectedLayout = new QVBoxLayout;
    00088     topLayout->addLayout( selectedLayout );
    00089     topLayout->setSpacing( spacingHint() );
    00090 //    selectedLayout->addSpacing( spacingHint() );
    00091
    00092     QGroupBox *selectedGroup = new QGroupBox( 1, Horizontal, i18n("Selected: "),
    00093                                               topWidget );
    00094     selectedLayout->addWidget( selectedGroup );
    00095
    00096     mSelectedList = new KListView( selectedGroup );
    00097     mSelectedList->addColumn( i18n("Name") );
    00098     mSelectedList->addColumn( i18n("Email") );
    00099     connect( mSelectedList, SIGNAL( doubleClicked( QListViewItem * ) ),
    00100              SLOT( removeSelected() ) );
    00101
    00102     QPushButton *unselectButton = new QPushButton( i18n("Unselect"), selectedGroup );
    00103     connect ( unselectButton, SIGNAL( clicked() ), SLOT( removeSelected() ) );
    00104
    00105     connect( mAddresseeList, SIGNAL( clicked( QListViewItem * ) ),
    00106              SLOT( addSelected( QListViewItem * ) ) );
    00107   }
    00108
    00109   mAddressBook = StdAddressBook::self();
    00110   connect( mAddressBook, SIGNAL( addressBookChanged( AddressBook* ) ), SLOT( addressBookChanged() ) );
    00111
    00112   loadAddressBook();
    00113 }
    00114
    00115 AddresseeDialog::~AddresseeDialog()
    00116 {
    00117 }
    00118
    00119 void AddresseeDialog::loadAddressBook()
    00120 {
    00121   mAddresseeList->clear();
    00122   mItemDict.clear();
    00123   mAddresseeEdit->completionObject()->clear();
    00124
    00125   AddressBook::Iterator it;
    00126   for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
    00127     AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
    00128     addCompletionItem( (*it).realName(), item );
    00129     addCompletionItem( (*it).preferredEmail(), item );
    00130   }
    00131 }
    00132
    00133 void AddresseeDialog::addCompletionItem( const QString &str, QListViewItem *item )
    00134 {
    00135   if ( str.isEmpty() ) return;
    00136
    00137   QString s = str.lower();
    00138   mItemDict.insert( s, item );
    00139   mAddresseeEdit->completionObject()->addItem( s );
    00140 }
    00141
    00142 void AddresseeDialog::selectItem( const QString &str )
    00143 {
    00144   if ( str.isEmpty() ) return;
    00145
    00146   QListViewItem *item = mItemDict.find( str );
    00147   if ( item ) {
    00148     mAddresseeList->blockSignals( true );
    00149     mAddresseeList->setSelected( item, true );
    00150     mAddresseeList->blockSignals( false );
    00151   }
    00152 }
    00153
    00154 void AddresseeDialog::updateEdit( QListViewItem *item )
    00155 {
    00156   mAddresseeEdit->setText( item->text( 0 ) );
    00157   mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
    00158 }
    00159
    00160 void AddresseeDialog::addSelected( QListViewItem *item )
    00161 {
    00162   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
    00163   if ( !addrItem ) return;
    00164
    00165   Addressee a = addrItem->addressee();
    00166
    00167   QListViewItem *selectedItem = mSelectedDict.find( a.uid() );
    00168   if ( !selectedItem ) {
    00169     selectedItem = new AddresseeItem( mSelectedList, a );
    00170     mSelectedDict.insert( a.uid(), selectedItem );
    00171   }
    00172 }
    00173
    00174 void AddresseeDialog::removeSelected()
    00175 {
    00176   QListViewItem *item = mSelectedList->selectedItem();
    00177   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
    00178   if ( !addrItem ) return;
    00179
    00180   mSelectedDict.remove( addrItem->addressee().uid() );
    00181   delete addrItem;
    00182 }
    00183
    00184 Addressee AddresseeDialog::addressee()
    00185 {
    00186   AddresseeItem *aItem = 0;
    00187
    00188   if ( mMultiple )
    00189     aItem = dynamic_cast<AddresseeItem *>( mSelectedList->firstChild() );
    00190   else
    00191     aItem = dynamic_cast<AddresseeItem *>( mAddresseeList->selectedItem() );
    00192
    00193   if (aItem) return aItem->addressee();
    00194   return Addressee();
    00195 }
    00196
    00197 Addressee::List AddresseeDialog::addressees()
    00198 {
    00199   Addressee::List al;
    00200   AddresseeItem *aItem = 0;
    00201
    00202   if ( mMultiple ) {
    00203     QListViewItem *item = mSelectedList->firstChild();
    00204     while( item ) {
    00205       aItem = dynamic_cast<AddresseeItem *>( item );
    00206       if ( aItem ) al.append( aItem->addressee() );
    00207       item = item->nextSibling();
    00208     }
    00209   }
    00210   else
    00211   {
    00212     aItem = dynamic_cast<AddresseeItem *>( mAddresseeList->selectedItem() );
    00213     if (aItem) al.append( aItem->addressee() );
    00214   }
    00215
    00216   return al;
    00217 }
    00218
    00219 Addressee AddresseeDialog::getAddressee( QWidget *parent )
    00220 {
    00221   AddresseeDialog *dlg = new AddresseeDialog( parent );
    00222   Addressee addressee;
    00223   int result = dlg->exec();
    00224
    00225   if ( result == QDialog::Accepted ) {
    00226     addressee =  dlg->addressee();
    00227   }
    00228
    00229   delete dlg;
    00230   return addressee;
    00231 }
    00232
    00233 Addressee::List AddresseeDialog::getAddressees( QWidget *parent )
    00234 {
    00235   AddresseeDialog *dlg = new AddresseeDialog( parent, true );
    00236   Addressee::List addressees;
    00237   int result = dlg->exec();
    00238   if ( result == QDialog::Accepted ) {
    00239     addressees =  dlg->addressees();
    00240   }
    00241
    00242   delete dlg;
    00243   return addressees;
    00244 }
    00245
    00246 void AddresseeDialog::addressBookChanged()
    00247 {
    00248   loadAddressBook();
    00249 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:17 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001