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