kaddressbook Library API Documentation

imeditorwidget.cpp

00001 /* 00002 imeditorwidget.cpp 00003 00004 IM address editor widget for KAddressBook 00005 00006 Copyright (c) 2004 Will Stephenson <lists@stevello.free-online.co.uk> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00021 00022 As a special exception, permission is given to link this program 00023 with any edition of Qt, and distribute the resulting executable, 00024 without including the source code for Qt in the source distribution. 00025 */ 00026 00027 #include <qlistview.h> 00028 #include <qstringlist.h> 00029 00030 #include <kdialogbase.h> 00031 #include <kdebug.h> 00032 #include <kiconloader.h> 00033 #include <klocale.h> 00034 #include <kmessagebox.h> 00035 #include <kplugininfo.h> 00036 #include <kpushbutton.h> 00037 #include <ktrader.h> 00038 00039 #include "imaddresswidget.h" 00040 #include "imeditorwidget.h" 00041 00042 00043 extern "C" { 00044 void *init_libkaddrbk_instantmessaging() 00045 { 00046 return ( new IMEditorWidgetFactory ); 00047 } 00048 } 00049 00050 QString IMEditorWidgetFactory::pageTitle() const 00051 { 00052 return i18n( "IM Addresses" ); 00053 } 00054 00055 QString IMEditorWidgetFactory::pageIdentifier() const 00056 { 00057 return "instantmessaging"; 00058 } 00059 00060 /*===========================================================================*/ 00061 00062 IMAddressLVI::IMAddressLVI( KListView *parent, KPluginInfo *protocol, QString address, IMContext context ) : KListViewItem( parent ) 00063 { 00064 //mInVCard = inVCard; 00065 00066 setProtocol( protocol ); 00067 00068 // set address 00069 setAddress( address ); 00070 00071 // set context 00072 setContext( context ); 00073 00074 } 00075 00076 void IMAddressLVI::setAddress( const QString &address ) 00077 { 00078 setText( 1, address ); 00079 } 00080 00081 void IMAddressLVI::setContext( IMContext context ) 00082 { 00083 mContext = context; 00084 // set context 00085 /* switch ( context ) 00086 { 00087 case Home: 00088 setText( 2, i18n( "Home" ) ); 00089 break; 00090 case Work: 00091 setText( 2, i18n( "Work" ) ); 00092 break; 00093 case Any: 00094 setText( 2, i18n( "Any" ) ); 00095 break; 00096 } 00097 */ 00098 } 00099 00100 void IMAddressLVI::setProtocol( KPluginInfo *protocol ) 00101 { 00102 mProtocol = protocol; 00103 setPixmap( 0, SmallIcon( mProtocol->icon() ) ); 00104 setText( 0, mProtocol->name() ); 00105 } 00106 00107 KPluginInfo * IMAddressLVI::protocol() const 00108 { 00109 return mProtocol; 00110 } 00111 00112 IMContext IMAddressLVI::context() const 00113 { 00114 return mContext; 00115 } 00116 00117 QString IMAddressLVI::address() const 00118 { 00119 return text( 1 ); 00120 } 00121 00122 void IMAddressLVI::activate() 00123 { 00124 // show editor 00125 } 00126 00127 /*===========================================================================*/ 00128 00129 IMEditorWidget::IMEditorWidget( KABC::AddressBook *ab, QWidget *parent, const char *name ) 00130 : KAB::ContactEditorWidget( ab, parent, name ), mReadOnly( false ) 00131 { 00132 mWidget = new IMEditorBase( this ); 00133 00134 connect( mWidget->btnAdd, SIGNAL( clicked() ), SLOT( slotAdd() ) ); 00135 connect( mWidget->btnEdit, SIGNAL( clicked() ), SLOT( slotEdit() ) ); 00136 connect( mWidget->btnDelete, SIGNAL( clicked() ), SLOT( slotDelete() ) ); 00137 connect( mWidget->lvAddresses, SIGNAL( selectionChanged() ), SLOT( slotUpdateButtons() ) ); 00138 00139 connect( mWidget->lvAddresses, SIGNAL( doubleClicked ( QListViewItem *, const QPoint &, int ) ),SLOT( slotEdit() ) ); 00140 00141 mWidget->btnEdit->setEnabled( false ); 00142 mWidget->btnDelete->setEnabled( false ); 00143 // Disabled pending implementation 00144 //mWidget->btnUp->setEnabled( false ); 00145 //mWidget->btnDown->setEnabled( false ); 00146 00147 mProtocols = KPluginInfo::fromServices( KTrader::self()->query( QString::fromLatin1( "KABC/IMProtocol" ) ) ); 00148 //kdDebug ( 5720 ) << " found " << mProtocols.count() << " protocols " << endl; 00149 } 00150 00151 QValueList<KPluginInfo *> IMEditorWidget::availableProtocols() const 00152 { 00153 return mProtocols; 00154 } 00155 00156 void IMEditorWidget::loadContact( KABC::Addressee *addr ) 00157 { 00158 if ( mWidget->lvAddresses ) 00159 mWidget->lvAddresses->clear(); 00160 00161 // see README for details of how Evolution stores IM addresses (differently) 00162 QStringList customs = addr->customs(); 00163 00164 QStringList::ConstIterator it; 00165 for ( it = customs.begin(); it != customs.end(); ++it ) 00166 { 00167 QString app, name, value; 00168 splitField( *it, app, name, value ); 00169 00170 if ( app.startsWith( QString::fromLatin1( "messaging/" ) ) ) 00171 { 00172 if ( name == QString::fromLatin1( "All" ) ) 00173 { 00174 KPluginInfo *protocol = protocolFromString( app ); 00175 if ( protocol ) 00176 { 00177 QStringList addresses = QStringList::split( QChar( 0xE000 ), value ); 00178 QStringList::iterator end = addresses.end(); 00179 for ( QStringList::iterator it = addresses.begin(); it != end; ++it ) 00180 { 00181 new IMAddressLVI( mWidget->lvAddresses, protocol, *it, Any/*, false*/ ); 00182 } 00183 } 00184 else 00185 kdDebug( 5720 ) << k_funcinfo << " no protocol found for: " << app << endl; 00186 } 00187 } 00188 } 00189 } 00190 00191 void IMEditorWidget::storeContact( KABC::Addressee *addr ) 00192 { 00193 // for each changed protocol, write a new custom field containing the current set of 00194 // addresses 00195 QValueList<KPluginInfo *>::iterator protocolIt; 00196 for ( protocolIt = mChangedProtocols.begin(); protocolIt != mChangedProtocols.end(); ++protocolIt ) 00197 { 00198 QStringList lst; 00199 QListViewItemIterator addressIt( mWidget->lvAddresses ); 00200 while ( addressIt.current() ) 00201 { 00202 IMAddressLVI* currentAddress = static_cast<IMAddressLVI*>(*addressIt); 00203 if ( currentAddress->protocol() == *protocolIt ) 00204 lst.append( currentAddress->address() ); 00205 ++addressIt; 00206 } 00207 00208 //kdDebug( 0 ) << QString::fromLatin1("messaging/%1").arg( protocolToString( *protocolIt ) ) << 00209 // QString::fromLatin1("All") << 00210 // lst.join( QChar( 0xE000 ) ) << endl; 00211 QString addrBookField; 00212 if ( !lst.isEmpty() ) 00213 { 00214 addrBookField = ( *protocolIt )->property( "X-KDE-InstantMessagingKABCField" ).toString(); 00215 addr->insertCustom( addrBookField, QString::fromLatin1( "All" ), lst.join( QChar( 0xE000 ) ) ); 00216 } 00217 else 00218 addr->removeCustom( addrBookField, QString::fromLatin1("All") ); 00219 } 00220 } 00221 00222 void IMEditorWidget::setReadOnly( bool readOnly ) 00223 { 00224 mReadOnly = readOnly; 00225 00226 mWidget->btnAdd->setEnabled( !readOnly ); 00227 mWidget->btnEdit->setEnabled( !readOnly && mWidget->lvAddresses->currentItem() ); 00228 mWidget->btnDelete->setEnabled( !readOnly && mWidget->lvAddresses->currentItem() ); 00229 } 00230 00231 void IMEditorWidget::slotUpdateButtons() 00232 { 00233 if ( !mReadOnly && mWidget->lvAddresses->selectedItem() ) 00234 { 00235 //mWidget->btnAdd->setEnabled( true ); 00236 mWidget->btnEdit->setEnabled( true ); 00237 mWidget->btnDelete->setEnabled( true ); 00238 } 00239 else 00240 { 00241 //mWidget->btnAdd->setEnabled( false ); 00242 mWidget->btnEdit->setEnabled( false ); 00243 mWidget->btnDelete->setEnabled( false ); 00244 } 00245 } 00246 00247 void IMEditorWidget::slotAdd() 00248 { 00249 KDialogBase *addDialog = new KDialogBase( this, "addaddress", true, i18n("Add Address"), KDialogBase::Ok|KDialogBase::Cancel ); 00250 IMAddressWidget *addressWid = new IMAddressWidget( addDialog, mProtocols ); 00251 addDialog->setMainWidget( addressWid ); 00252 if ( addDialog->exec() == QDialog::Accepted ) 00253 { 00254 // add the new item 00255 new IMAddressLVI( mWidget->lvAddresses, addressWid->protocol(), addressWid->address() /*, addressWid->context() */ ); 00256 if ( mChangedProtocols.find( addressWid->protocol() ) == mChangedProtocols.end() ) 00257 mChangedProtocols.append( addressWid->protocol() ); 00258 mWidget->lvAddresses->sort(); 00259 00260 setModified( true ); 00261 } 00262 delete addDialog; 00263 } 00264 00265 void IMEditorWidget::slotEdit() 00266 { 00267 if ( IMAddressLVI *current = static_cast<IMAddressLVI*>(mWidget->lvAddresses->selectedItem() ) ) 00268 { 00269 KDialogBase *editDialog = new KDialogBase( this, "editaddress", true, i18n("Edit Address"), KDialogBase::Ok|KDialogBase::Cancel ); 00270 IMAddressWidget *addressWid = new IMAddressWidget( editDialog, mProtocols, current->protocol(), current->address(), current->context() ) ; 00271 00272 editDialog->setMainWidget( addressWid ); 00273 00274 if ( editDialog->exec() == QDialog::Accepted ) 00275 { 00276 current->setAddress( addressWid->address() ); 00277 current->setContext( addressWid->context() ); 00278 00279 // the entry for the protocol of the current address has changed 00280 if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() ) 00281 mChangedProtocols.append( current->protocol() ); 00282 // update protocol - has another protocol gained an address? 00283 if ( current->protocol() != addressWid->protocol() ) 00284 { 00285 // this proto is losing an entry 00286 current->setProtocol( addressWid->protocol() ); 00287 if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() ) 00288 mChangedProtocols.append( current->protocol() ); 00289 } 00290 00291 setModified( true ); 00292 } 00293 delete editDialog; 00294 } 00295 } 00296 00297 void IMEditorWidget::slotDelete() 00298 { 00299 if ( mWidget->lvAddresses->selectedItem() && KMessageBox::warningContinueCancel( this, i18n("Do you really want to delete the selected address?"), i18n("Confirm Delete"), KGuiItem(i18n("&Delete"),"editdelete") ) == KMessageBox::Continue ) 00300 { 00301 IMAddressLVI * current = static_cast<IMAddressLVI*>( mWidget->lvAddresses->selectedItem() ); 00302 if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() ) 00303 { 00304 mChangedProtocols.append( current->protocol() ); 00305 //kdDebug ( 0 ) << " changed protocols: " << mProtocols.count() << endl; 00306 } 00307 delete current; 00308 00309 setModified( true ); 00310 } 00311 } 00312 00313 KPluginInfo * IMEditorWidget::protocolFromString( QString fieldValue ) 00314 { 00315 QValueList<KPluginInfo *>::ConstIterator it; 00316 KPluginInfo * protocol = 0; 00317 for ( it = mProtocols.begin(); it != mProtocols.end(); ++it ) 00318 { 00319 if ( ( (*it)->property( "X-KDE-InstantMessagingKABCField" ).toString() == fieldValue ) ) 00320 { 00321 protocol = *it; 00322 break; 00323 } 00324 } 00325 return protocol; 00326 } 00327 00328 void IMEditorWidget::splitField( const QString &str, QString &app, QString &name, QString &value ) 00329 { 00330 int colon = str.find( ':' ); 00331 if ( colon != -1 ) { 00332 QString tmp = str.left( colon ); 00333 value = str.mid( colon + 1 ); 00334 00335 int dash = tmp.find( '-' ); 00336 if ( dash != -1 ) { 00337 app = tmp.left( dash ); 00338 name = tmp.mid( dash + 1 ); 00339 } 00340 } 00341 } 00342 00343 #include "imeditorwidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 27 12:51:13 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003