00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00065
00066 setProtocol( protocol );
00067
00068
00069 setAddress( address );
00070
00071
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
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
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
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
00144
00145
00146
00147 mProtocols = KPluginInfo::fromServices( KTrader::self()->query( QString::fromLatin1( "KABC/IMProtocol" ) ) );
00148
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
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 );
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
00194
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
00209
00210
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
00236 mWidget->btnEdit->setEnabled( true );
00237 mWidget->btnDelete->setEnabled( true );
00238 }
00239 else
00240 {
00241
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
00255 new IMAddressLVI( mWidget->lvAddresses, addressWid->protocol(), addressWid->address() );
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
00280 if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() )
00281 mChangedProtocols.append( current->protocol() );
00282
00283 if ( current->protocol() != addressWid->protocol() )
00284 {
00285
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
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"