25 #include <tqlistview.h>
26 #include <tqstringlist.h>
30 #include <tqpainter.h>
32 #include <kdialogbase.h>
34 #include <kiconloader.h>
35 #include <tdelocale.h>
36 #include <tdemessagebox.h>
37 #include <kplugininfo.h>
38 #include <kpushbutton.h>
41 #include "imaddresswidget.h"
42 #include "imeditorbase.h"
43 #include "imeditorwidget.h"
46 IMAddressLVI::IMAddressLVI( TDEListView *parent, KPluginInfo *protocol,
47 const TQString &address,
const IMContext &context )
48 : TDEListViewItem( parent )
50 setProtocol( protocol );
51 setAddress( address );
52 setContext( context );
56 void IMAddressLVI::setPreferred(
bool preferred )
58 mPreferred = preferred;
61 bool IMAddressLVI::preferred()
const
66 void IMAddressLVI::paintCell( TQPainter *p,
const TQColorGroup &cg,
67 int column,
int width,
int alignment )
70 TQFont font = p->font();
75 TDEListViewItem::paintCell( p, cg, column, width, alignment );
78 void IMAddressLVI::setAddress(
const TQString &address )
81 TQString serverOrGroup = address.section( TQChar( 0xE120 ), 1 );
83 if ( serverOrGroup.isEmpty() )
84 setText( 1, address );
86 TQString nickname = address.section( TQChar( 0xE120 ), 0, 0 );
87 setText( 1, i18n(
"<nickname> on <server>",
"%1 on %2" )
88 .arg( nickname ).arg( serverOrGroup ) );
94 void IMAddressLVI::setContext(
const IMContext &context )
113 void IMAddressLVI::setProtocol( KPluginInfo *protocol )
115 mProtocol = protocol;
117 setPixmap( 0, SmallIcon( mProtocol->icon() ) );
118 setText( 0, mProtocol->name() );
121 KPluginInfo * IMAddressLVI::protocol()
const
126 IMContext IMAddressLVI::context()
const
131 TQString IMAddressLVI::address()
const
136 void IMAddressLVI::activate()
143 IMEditorWidget::IMEditorWidget( TQWidget *parent,
const TQString &preferredIM,
const char *name )
144 : KDialogBase( parent, name, false, i18n(
"Edit Instant Messenging Address" ),
145 Help | Ok | Cancel, Ok, false ),
148 mWidget =
new IMEditorBase(
this );
149 setMainWidget( mWidget );
151 connect( mWidget->btnAdd, TQ_SIGNAL( clicked() ), TQ_SLOT( slotAdd() ) );
152 connect( mWidget->btnEdit, TQ_SIGNAL( clicked() ), TQ_SLOT( slotEdit() ) );
153 connect( mWidget->btnDelete, TQ_SIGNAL( clicked() ), TQ_SLOT( slotDelete() ) );
154 connect( mWidget->btnSetStandard, TQ_SIGNAL( clicked()), TQ_SLOT( slotSetStandard() ) );
155 connect( mWidget->lvAddresses, TQ_SIGNAL( selectionChanged() ), TQ_SLOT( slotUpdateButtons() ) );
157 connect( mWidget->lvAddresses, TQ_SIGNAL( doubleClicked( TQListViewItem*,
const TQPoint&,
int ) ),
158 TQ_SLOT( slotEdit() ) );
160 setHelp(
"managing-contacts-im-addresses" );
162 mWidget->btnEdit->setEnabled(
false );
163 mWidget->btnDelete->setEnabled(
false );
164 mWidget->btnSetStandard->setEnabled(
false );
168 mPreferred = preferredIM;
169 mPreferred = mPreferred.replace(
" on ", TQString( TQChar( 0xE120 ) ),
true );
170 mProtocols = KPluginInfo::fromServices( TDETrader::self()->query( TQString::fromLatin1(
"KABC/IMProtocol" ) ) );
173 TQMap<TQString, KPluginInfo *> protocolMap;
174 TQValueList<KPluginInfo *>::ConstIterator it;
175 TQValueList<KPluginInfo *> sorted;
176 for ( it = mProtocols.begin(); it != mProtocols.end(); ++it )
177 protocolMap.insert( (*it)->name(), (*it) );
179 TQStringList keys = protocolMap.keys();
181 TQStringList::ConstIterator keyIt = keys.begin();
182 TQStringList::ConstIterator end = keys.end();
183 for ( ; keyIt != end; ++keyIt )
184 sorted.append( protocolMap[*keyIt] );
188 TQValueList<KPluginInfo *> IMEditorWidget::availableProtocols()
const
193 void IMEditorWidget::loadContact( TDEABC::Addressee *addr )
195 if ( mWidget->lvAddresses )
196 mWidget->lvAddresses->clear();
199 const TQStringList customs = addr->customs();
201 TQStringList::ConstIterator it;
203 for ( it = customs.begin(); it != customs.end(); ++it ) {
204 TQString app, name, value;
207 if ( app.startsWith( TQString::fromLatin1(
"messaging/" ) ) ) {
208 if ( name == TQString::fromLatin1(
"All" ) ) {
211 TQStringList addresses = TQStringList::split( TQChar( 0xE000 ), value );
212 TQStringList::iterator end = addresses.end();
213 for ( TQStringList::ConstIterator it = addresses.begin(); it != end; ++it ) {
215 if ( !isSet && (*it).stripWhiteSpace().lower() == mPreferred.stripWhiteSpace().lower() ) {
216 imaddresslvi->setPreferred(
true );
221 kdDebug( 5720 ) << k_funcinfo <<
" no protocol found for: " << app << endl;
226 if ( mWidget->lvAddresses->firstChild() )
227 mWidget->lvAddresses->firstChild()->setSelected(
true );
230 void IMEditorWidget::storeContact( TDEABC::Addressee *addr )
234 TQValueList<KPluginInfo *>::ConstIterator protocolIt;
235 for ( protocolIt = mChangedProtocols.begin(); protocolIt != mChangedProtocols.end(); ++protocolIt ) {
237 TQListViewItemIterator addressIt( mWidget->lvAddresses );
238 while ( addressIt.current() ) {
240 if ( currentAddress->protocol() == *protocolIt )
241 lst.append( currentAddress->address() );
245 TQString addrBookField = (*protocolIt)->property(
"X-TDE-InstantMessagingKABCField" ).toString();
246 if ( !lst.isEmpty() )
247 addr->insertCustom( addrBookField, TQString::fromLatin1(
"All" ), lst.join( TQChar( 0xE000 ) ) );
249 addr->removeCustom( addrBookField, TQString::fromLatin1(
"All" ) );
253 void IMEditorWidget::setReadOnly(
bool readOnly )
255 mReadOnly = readOnly;
259 void IMEditorWidget::slotSetStandard()
261 TQListViewItemIterator it( mWidget->lvAddresses, TQListViewItemIterator::Selected );
265 TQListViewItemIterator it2( mWidget->lvAddresses );
266 while ( it2.current() ) {
269 if ( item->preferred() ) {
270 if ( current == item )
273 item->setPreferred(
false );
274 mWidget->lvAddresses->repaintItem( item );
282 mPreferred = current->address();
283 current->setPreferred(
true );
285 mWidget->lvAddresses->repaintItem( current );
289 void IMEditorWidget::slotUpdateButtons()
291 int num_selected = 0;
292 TQListViewItemIterator it( mWidget->lvAddresses, TQListViewItemIterator::Selected );
293 while ( it.current() ) {
295 if ( num_selected > 1 )
301 if ( !mReadOnly && num_selected == 1 ) {
302 mWidget->btnAdd->setEnabled(
true );
303 mWidget->btnEdit->setEnabled(
true );
304 mWidget->btnDelete->setEnabled(
true );
308 mWidget->btnSetStandard->setEnabled( !current || !current->preferred() );
309 }
else if ( !mReadOnly && num_selected > 1 ) {
310 mWidget->btnAdd->setEnabled(
true );
311 mWidget->btnEdit->setEnabled(
false );
312 mWidget->btnDelete->setEnabled(
true );
313 mWidget->btnSetStandard->setEnabled(
false );
315 mWidget->btnAdd->setEnabled( !mReadOnly );
316 mWidget->btnSetStandard->setEnabled(
false );
317 mWidget->btnEdit->setEnabled(
false );
318 mWidget->btnDelete->setEnabled(
false );
322 void IMEditorWidget::setModified(
bool modified )
324 mModified = modified;
327 bool IMEditorWidget::isModified()
const
332 void IMEditorWidget::slotAdd()
334 KDialogBase addDialog(
this,
"addaddress",
true, i18n(
"Instant messaging",
"Add Address" ),
335 KDialogBase::Ok | KDialogBase::Cancel );
338 addDialog.enableButtonOK(
false );
339 connect( addressWid, TQ_SIGNAL( inValidState(
bool ) ),
340 &addDialog, TQ_SLOT( enableButtonOK(
bool ) ) );
341 addDialog.setMainWidget( addressWid );
343 if ( addDialog.exec() == TQDialog::Accepted ) {
346 addressWid->address() );
349 if ( mPreferred.isEmpty() ) {
350 imaddresslvi->setPreferred(
true );
351 mPreferred = addressWid->address();
354 if ( mChangedProtocols.find( addressWid->protocol() ) == mChangedProtocols.end() )
355 mChangedProtocols.append( addressWid->protocol() );
357 mWidget->lvAddresses->sort();
363 void IMEditorWidget::slotEdit()
367 TQListViewItemIterator it( mWidget->lvAddresses, TQListViewItemIterator::Selected );
371 KDialogBase editDialog(
this,
"editaddress",
true, i18n(
"Instant messaging",
"Edit Address" ),
372 KDialogBase::Ok | KDialogBase::Cancel );
374 current->address(), current->context() ) ;
375 connect( addressWid, TQ_SIGNAL( inValidState(
bool ) ),
376 &editDialog, TQ_SLOT( enableButtonOK(
bool ) ) );
377 editDialog.setMainWidget( addressWid );
379 if ( editDialog.exec() == TQDialog::Accepted ) {
380 bool modified =
false;
381 if ( addressWid->address() != current->address() ) {
383 current->setAddress( addressWid->address() );
385 if ( addressWid->context() != current->context() ) {
387 current->setContext( addressWid->context() );
391 if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() ) {
392 mChangedProtocols.append( current->protocol() );
395 if ( current->protocol() != addressWid->protocol() ) {
398 current->setProtocol( addressWid->protocol() );
399 if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() )
400 mChangedProtocols.append( current->protocol() );
409 void IMEditorWidget::slotDelete()
411 int num_selected = 0;
414 TQListViewItemIterator it( mWidget->lvAddresses, TQListViewItemIterator::Selected );
415 while ( it.current() ) {
421 if ( num_selected == 0 )
424 if ( KMessageBox::warningContinueCancel(
this, i18n(
"Do you really want to delete the selected address?",
425 "Do you really want to delete the %n selected addresses?", num_selected ),
426 i18n(
"Confirm Delete" ), KStdGuiItem::del() ) != KMessageBox::Continue )
429 TQListViewItemIterator it( mWidget->lvAddresses );
430 bool deletedPreferred =
false;
431 while( it.current() ) {
432 if ( it.current()->isSelected() ) {
434 if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() )
435 mChangedProtocols.append( current->protocol() );
437 if ( current->preferred() )
438 deletedPreferred =
true;
445 if ( deletedPreferred ) {
448 first->setPreferred(
true );
449 mPreferred = first->address();
457 TQString IMEditorWidget::preferred()
const
459 TQString retval( mPreferred );
460 return retval.replace( TQChar( 0xE120 ),
" on " );
466 TQValueList<KPluginInfo *>::ConstIterator it;
467 KPluginInfo * protocol = 0;
468 for ( it = mProtocols.begin(); it != mProtocols.end(); ++it ) {
469 if ( (*it)->property(
"X-TDE-InstantMessagingKABCField" ).toString() == fieldValue ) {
480 int colon = str.find(
':' );
482 TQString tmp = str.left( colon );
483 value = str.mid( colon + 1 );
485 int dash = tmp.find(
'-' );
487 app = tmp.left( dash );
488 name = tmp.mid( dash + 1 );
493 #include "imeditorwidget.moc"
List view item representing a single IM address.