| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | 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   mAddresseeList->setFullWidth( true );
    00074   listLayout->addWidget( mAddresseeList );
    00075   connect( mAddresseeList, SIGNAL( doubleClicked( QListViewItem * ) ),
    00076            SLOT( slotOk() ) );
    00077   connect( mAddresseeList, SIGNAL( selectionChanged( QListViewItem * ) ),
    00078            SLOT( updateEdit( QListViewItem * ) ) );
    00079
    00080   mAddresseeEdit = new KLineEdit( topWidget );
    00081   mAddresseeEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
    00082   connect( mAddresseeEdit->completionObject(), SIGNAL( match( const QString & ) ),
    00083            SLOT( selectItem( const QString & ) ) );
    00084   mAddresseeEdit->setFocus();
    00085   mAddresseeEdit->completionObject()->setIgnoreCase( true );
    00086   listLayout->addWidget( mAddresseeEdit );
    00087
    00088   setInitialSize( QSize( 450, 300 ) );
    00089
    00090   if ( mMultiple ) {
    00091     QBoxLayout *selectedLayout = new QVBoxLayout;
    00092     topLayout->addLayout( selectedLayout );
    00093     topLayout->setSpacing( spacingHint() );
    00094
    00095     QGroupBox *selectedGroup = new QGroupBox( 1, Horizontal, i18n("Selected"),
    00096                                               topWidget );
    00097     selectedLayout->addWidget( selectedGroup );
    00098
    00099     mSelectedList = new KListView( selectedGroup );
    00100     mSelectedList->addColumn( i18n("Name") );
    00101     mSelectedList->addColumn( i18n("Email") );
    00102     mSelectedList->setAllColumnsShowFocus( true );
    00103     mSelectedList->setFullWidth( true );
    00104     connect( mSelectedList, SIGNAL( doubleClicked( QListViewItem * ) ),
    00105              SLOT( removeSelected() ) );
    00106
    00107     QPushButton *unselectButton = new QPushButton( i18n("Unselect"), selectedGroup );
    00108     connect ( unselectButton, SIGNAL( clicked() ), SLOT( removeSelected() ) );
    00109
    00110     connect( mAddresseeList, SIGNAL( clicked( QListViewItem * ) ),
    00111              SLOT( addSelected( QListViewItem * ) ) );
    00112
    00113     setInitialSize( QSize( 650, 350 ) );
    00114   }
    00115
    00116   mAddressBook = StdAddressBook::self( true );
    00117   connect( mAddressBook, SIGNAL( addressBookChanged( AddressBook* ) ),
    00118            SLOT( addressBookChanged() ) );
    00119   connect( mAddressBook, SIGNAL( loadingFinished( Resource* ) ),
    00120            SLOT( addressBookChanged() ) );
    00121
    00122   loadAddressBook();
    00123 }
    00124
    00125 AddresseeDialog::~AddresseeDialog()
    00126 {
    00127 }
    00128
    00129 void AddresseeDialog::loadAddressBook()
    00130 {
    00131   mAddresseeList->clear();
    00132   mItemDict.clear();
    00133   mAddresseeEdit->completionObject()->clear();
    00134
    00135   AddressBook::Iterator it;
    00136   for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
    00137     AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
    00138     addCompletionItem( (*it).realName(), item );
    00139     addCompletionItem( (*it).preferredEmail(), item );
    00140   }
    00141 }
    00142
    00143 void AddresseeDialog::addCompletionItem( const QString &str, QListViewItem *item )
    00144 {
    00145   if ( str.isEmpty() ) return;
    00146
    00147   mItemDict.insert( str, item );
    00148   mAddresseeEdit->completionObject()->addItem( str );
    00149 }
    00150
    00151 void AddresseeDialog::selectItem( const QString &str )
    00152 {
    00153   if ( str.isEmpty() ) return;
    00154
    00155   QListViewItem *item = mItemDict.find( str );
    00156   if ( item ) {
    00157     mAddresseeList->blockSignals( true );
    00158     mAddresseeList->setSelected( item, true );
    00159     mAddresseeList->ensureItemVisible( item );
    00160     mAddresseeList->blockSignals( false );
    00161   }
    00162 }
    00163
    00164 void AddresseeDialog::updateEdit( QListViewItem *item )
    00165 {
    00166   mAddresseeEdit->setText( item->text( 0 ) );
    00167   mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
    00168 }
    00169
    00170 void AddresseeDialog::addSelected( QListViewItem *item )
    00171 {
    00172   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
    00173   if ( !addrItem ) return;
    00174
    00175   Addressee a = addrItem->addressee();
    00176
    00177   QListViewItem *selectedItem = mSelectedDict.find( a.uid() );
    00178   if ( !selectedItem ) {
    00179     selectedItem = new AddresseeItem( mSelectedList, a );
    00180     mSelectedDict.insert( a.uid(), selectedItem );
    00181   }
    00182 }
    00183
    00184 void AddresseeDialog::removeSelected()
    00185 {
    00186   QListViewItem *item = mSelectedList->selectedItem();
    00187   AddresseeItem *addrItem = dynamic_cast<AddresseeItem *>( item );
    00188   if ( !addrItem ) return;
    00189
    00190   mSelectedDict.remove( addrItem->addressee().uid() );
    00191   delete addrItem;
    00192 }
    00193
    00194 Addressee AddresseeDialog::addressee()
    00195 {
    00196   AddresseeItem *aItem = 0;
    00197
    00198   if ( mMultiple )
    00199     aItem = dynamic_cast<AddresseeItem *>( mSelectedList->firstChild() );
    00200   else
    00201     aItem = dynamic_cast<AddresseeItem *>( mAddresseeList->selectedItem() );
    00202
    00203   if (aItem) return aItem->addressee();
    00204   return Addressee();
    00205 }
    00206
    00207 Addressee::List AddresseeDialog::addressees()
    00208 {
    00209   Addressee::List al;
    00210   AddresseeItem *aItem = 0;
    00211
    00212   if ( mMultiple ) {
    00213     QListViewItem *item = mSelectedList->firstChild();
    00214     while( item ) {
    00215       aItem = dynamic_cast<AddresseeItem *>( item );
    00216       if ( aItem ) al.append( aItem->addressee() );
    00217       item = item->nextSibling();
    00218     }
    00219   }
    00220   else
    00221   {
    00222     aItem = dynamic_cast<AddresseeItem *>( mAddresseeList->selectedItem() );
    00223     if (aItem) al.append( aItem->addressee() );
    00224   }
    00225
    00226   return al;
    00227 }
    00228
    00229 Addressee AddresseeDialog::getAddressee( QWidget *parent )
    00230 {
    00231   AddresseeDialog *dlg = new AddresseeDialog( parent );
    00232   Addressee addressee;
    00233   int result = dlg->exec();
    00234
    00235   if ( result == QDialog::Accepted ) {
    00236     addressee =  dlg->addressee();
    00237   }
    00238
    00239   delete dlg;
    00240   return addressee;
    00241 }
    00242
    00243 Addressee::List AddresseeDialog::getAddressees( QWidget *parent )
    00244 {
    00245   AddresseeDialog *dlg = new AddresseeDialog( parent, true );
    00246   Addressee::List addressees;
    00247   int result = dlg->exec();
    00248   if ( result == QDialog::Accepted ) {
    00249     addressees =  dlg->addressees();
    00250   }
    00251
    00252   delete dlg;
    00253   return addressees;
    00254 }
    00255
    00256 void AddresseeDialog::addressBookChanged()
    00257 {
    00258   loadAddressBook();
    00259 }

This file is part of the documentation for kabc Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:29 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003