1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/*
IM address editor widget for KAddressBook
Copyright (c) 2004 Will Stephenson <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include <tqlistview.h>
#include <tqstringlist.h>
#include <tqvbox.h>
#include <tqlayout.h>
#include <tqfont.h>
#include <tqpainter.h>
#include <kdialogbase.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kplugininfo.h>
#include <kpushbutton.h>
#include <ktrader.h>
#include "imaddresswidget.h"
#include "imeditorbase.h"
#include "imeditorwidget.h"
IMAddressLVI::IMAddressLVI( KListView *parent, KPluginInfo *protocol,
const TQString &address, const IMContext &context )
: KListViewItem( parent )
{
setProtocol( protocol );
setAddress( address );
setContext( context );
mPreferred = false;
}
void IMAddressLVI::setPreferred( bool preferred )
{
mPreferred = preferred;
}
bool IMAddressLVI::preferred() const
{
return mPreferred;
}
void IMAddressLVI::paintCell( TQPainter *p, const TQColorGroup &cg,
int column, int width, int alignment )
{
if ( mPreferred ) {
TQFont font = p->font();
font.setBold( true );
p->setFont( font );
}
KListViewItem::paintCell( p, cg, column, width, alignment );
}
void IMAddressLVI::setAddress( const TQString &address )
{
// irc uses 0xE120 to seperate the nick and the server group.
TQString serverOrGroup = address.section( TQChar( 0xE120 ), 1 );
if ( serverOrGroup.isEmpty() )
setText( 1, address );
else {
TQString nickname = address.section( TQChar( 0xE120 ), 0, 0 );
setText( 1, i18n( "<nickname> on <server>","%1 on %2" )
.arg( nickname ).arg( serverOrGroup ) );
}
mAddress = address;
}
void IMAddressLVI::setContext( const IMContext &context )
{
mContext = context;
// set context
/* switch ( context )
{
case Home:
setText( 2, i18n( "Home" ) );
break;
case Work:
setText( 2, i18n( "Work" ) );
break;
case Any:
setText( 2, i18n( "Any" ) );
break;
}
*/
}
void IMAddressLVI::setProtocol( KPluginInfo *protocol )
{
mProtocol = protocol;
setPixmap( 0, SmallIcon( mProtocol->icon() ) );
setText( 0, mProtocol->name() );
}
KPluginInfo * IMAddressLVI::protocol() const
{
return mProtocol;
}
IMContext IMAddressLVI::context() const
{
return mContext;
}
TQString IMAddressLVI::address() const
{
return mAddress;
}
void IMAddressLVI::activate()
{
// show editor
}
/*===========================================================================*/
IMEditorWidget::IMEditorWidget( TQWidget *parent, const TQString &preferredIM, const char *name )
: KDialogBase( parent, name, false, i18n( "Edit Instant Messenging Address" ),
Help | Ok | Cancel, Ok, false ),
mReadOnly( false )
{
mWidget = new IMEditorBase( this );
setMainWidget( mWidget );
connect( mWidget->btnAdd, TQT_SIGNAL( clicked() ), TQT_SLOT( slotAdd() ) );
connect( mWidget->btnEdit, TQT_SIGNAL( clicked() ), TQT_SLOT( slotEdit() ) );
connect( mWidget->btnDelete, TQT_SIGNAL( clicked() ), TQT_SLOT( slotDelete() ) );
connect( mWidget->btnSetStandard, TQT_SIGNAL( clicked()), TQT_SLOT( slotSetStandard() ) );
connect( mWidget->lvAddresses, TQT_SIGNAL( selectionChanged() ), TQT_SLOT( slotUpdateButtons() ) );
connect( mWidget->lvAddresses, TQT_SIGNAL( doubleClicked( TQListViewItem*, const TQPoint&, int ) ),
TQT_SLOT( slotEdit() ) );
setHelp( "managing-contacts-im-addresses" );
mWidget->btnEdit->setEnabled( false );
mWidget->btnDelete->setEnabled( false );
mWidget->btnSetStandard->setEnabled( false );
// Disabled pending implementation
//mWidget->btnUp->setEnabled( false );
//mWidget->btnDown->setEnabled( false );
mPreferred = preferredIM;
mPreferred = mPreferred.replace( " on ", TQString( TQChar( 0xE120 ) ), true );
mProtocols = KPluginInfo::fromServices( KTrader::self()->query( TQString::fromLatin1( "KABC/IMProtocol" ) ) );
// order the protocols by putting them in a qmap, then sorting the set of keys and recreating the list
TQMap<TQString, KPluginInfo *> protocolMap;
TQValueList<KPluginInfo *>::ConstIterator it;
TQValueList<KPluginInfo *> sorted;
for ( it = mProtocols.begin(); it != mProtocols.end(); ++it )
protocolMap.insert( (*it)->name(), (*it) );
TQStringList keys = protocolMap.keys();
keys.sort();
TQStringList::ConstIterator keyIt = keys.begin();
TQStringList::ConstIterator end = keys.end();
for ( ; keyIt != end; ++keyIt )
sorted.append( protocolMap[*keyIt] );
mProtocols = sorted;
}
TQValueList<KPluginInfo *> IMEditorWidget::availableProtocols() const
{
return mProtocols;
}
void IMEditorWidget::loadContact( KABC::Addressee *addr )
{
if ( mWidget->lvAddresses )
mWidget->lvAddresses->clear();
// see README for details of how Evolution stores IM addresses (differently)
const TQStringList customs = addr->customs();
TQStringList::ConstIterator it;
bool isSet = false;
for ( it = customs.begin(); it != customs.end(); ++it ) {
TQString app, name, value;
splitField( *it, app, name, value );
if ( app.startsWith( TQString::fromLatin1( "messaging/" ) ) ) {
if ( name == TQString::fromLatin1( "All" ) ) {
KPluginInfo *protocol = protocolFromString( app );
if ( protocol ) {
TQStringList addresses = TQStringList::split( TQChar( 0xE000 ), value );
TQStringList::iterator end = addresses.end();
for ( TQStringList::ConstIterator it = addresses.begin(); it != end; ++it ) {
IMAddressLVI *imaddresslvi = new IMAddressLVI( mWidget->lvAddresses, protocol, *it, Any/*, false*/ );
if ( !isSet && (*it).stripWhiteSpace().lower() == mPreferred.stripWhiteSpace().lower() ) {
imaddresslvi->setPreferred( true );
isSet = true; //Only set one item to be preferred
}
}
} else
kdDebug( 5720 ) << k_funcinfo << " no protocol found for: " << app << endl;
}
}
}
if ( mWidget->lvAddresses->firstChild() )
mWidget->lvAddresses->firstChild()->setSelected( true );
}
void IMEditorWidget::storeContact( KABC::Addressee *addr )
{
// for each changed protocol, write a new custom field containing the current set of
// addresses
TQValueList<KPluginInfo *>::ConstIterator protocolIt;
for ( protocolIt = mChangedProtocols.begin(); protocolIt != mChangedProtocols.end(); ++protocolIt ) {
TQStringList lst;
TQListViewItemIterator addressIt( mWidget->lvAddresses );
while ( addressIt.current() ) {
IMAddressLVI* currentAddress = static_cast<IMAddressLVI*>( *addressIt );
if ( currentAddress->protocol() == *protocolIt )
lst.append( currentAddress->address() );
++addressIt;
}
TQString addrBookField = (*protocolIt)->property( "X-KDE-InstantMessagingKABCField" ).toString();
if ( !lst.isEmpty() )
addr->insertCustom( addrBookField, TQString::fromLatin1( "All" ), lst.join( TQChar( 0xE000 ) ) );
else
addr->removeCustom( addrBookField, TQString::fromLatin1( "All" ) );
}
}
void IMEditorWidget::setReadOnly( bool readOnly )
{
mReadOnly = readOnly;
slotUpdateButtons();
}
void IMEditorWidget::slotSetStandard()
{
TQListViewItemIterator it( mWidget->lvAddresses, TQListViewItemIterator::Selected );
// Just set the first one selected as standard
if ( IMAddressLVI *current = static_cast<IMAddressLVI*>( it.current() ) ) {
TQListViewItemIterator it2( mWidget->lvAddresses );
while ( it2.current() ) {
IMAddressLVI *item = static_cast<IMAddressLVI*>( it2.current() );
if ( item->preferred() ) {
if ( current == item )
return; //Selected is already preferred
else {
item->setPreferred( false );
mWidget->lvAddresses->repaintItem( item );
break;
}
}
++it2;
}
mPreferred = current->address();
current->setPreferred( true );
setModified( true );
mWidget->lvAddresses->repaintItem( current );
}
}
void IMEditorWidget::slotUpdateButtons()
{
int num_selected = 0;
TQListViewItemIterator it( mWidget->lvAddresses, TQListViewItemIterator::Selected );
while ( it.current() ) {
++num_selected;
if ( num_selected > 1 )
break; //no need to count above 2.
++it;
}
if ( !mReadOnly && num_selected == 1 ) {
mWidget->btnAdd->setEnabled( true );
mWidget->btnEdit->setEnabled( true );
mWidget->btnDelete->setEnabled( true );
IMAddressLVI *current = static_cast<IMAddressLVI*>( it.current() );
// Disable "set standard" if already standard
mWidget->btnSetStandard->setEnabled( !current || !current->preferred() );
} else if ( !mReadOnly && num_selected > 1 ) {
mWidget->btnAdd->setEnabled( true );
mWidget->btnEdit->setEnabled( false );
mWidget->btnDelete->setEnabled( true );
mWidget->btnSetStandard->setEnabled( false );
} else {
mWidget->btnAdd->setEnabled( !mReadOnly );
mWidget->btnSetStandard->setEnabled( false );
mWidget->btnEdit->setEnabled( false );
mWidget->btnDelete->setEnabled( false );
}
}
void IMEditorWidget::setModified( bool modified )
{
mModified = modified;
}
bool IMEditorWidget::isModified() const
{
return mModified;
}
void IMEditorWidget::slotAdd()
{
KDialogBase addDialog( this, "addaddress", true, i18n( "Instant messaging", "Add Address" ),
KDialogBase::Ok | KDialogBase::Cancel );
IMAddressWidget *addressWid = new IMAddressWidget( &addDialog, mProtocols );
addDialog.enableButtonOK( false );
connect( addressWid, TQT_SIGNAL( inValidState( bool ) ),
&addDialog, TQT_SLOT( enableButtonOK( bool ) ) );
addDialog.setMainWidget( addressWid );
if ( addDialog.exec() == TQDialog::Accepted ) {
// add the new item
IMAddressLVI *imaddresslvi = new IMAddressLVI( mWidget->lvAddresses, addressWid->protocol(),
addressWid->address() /*, addressWid->context() */ );
// If it's a new address, set is as preferred.
if ( mPreferred.isEmpty() ) {
imaddresslvi->setPreferred( true );
mPreferred = addressWid->address();
}
if ( mChangedProtocols.find( addressWid->protocol() ) == mChangedProtocols.end() )
mChangedProtocols.append( addressWid->protocol() );
mWidget->lvAddresses->sort();
setModified( true );
}
}
void IMEditorWidget::slotEdit()
{
if( mReadOnly )
return;
TQListViewItemIterator it( mWidget->lvAddresses, TQListViewItemIterator::Selected );
// Just edit the first one selected.
if ( IMAddressLVI *current = static_cast<IMAddressLVI*>( it.current() ) ) {
KDialogBase editDialog( this, "editaddress", true, i18n( "Instant messaging", "Edit Address" ),
KDialogBase::Ok | KDialogBase::Cancel );
IMAddressWidget *addressWid = new IMAddressWidget( &editDialog, mProtocols, current->protocol(),
current->address(), current->context() ) ;
connect( addressWid, TQT_SIGNAL( inValidState( bool ) ),
&editDialog, TQT_SLOT( enableButtonOK( bool ) ) );
editDialog.setMainWidget( addressWid );
if ( editDialog.exec() == TQDialog::Accepted ) {
bool modified = false;
if ( addressWid->address() != current->address() ) {
modified = true;
current->setAddress( addressWid->address() );
}
if ( addressWid->context() != current->context() ) {
modified = true;
current->setContext( addressWid->context() );
}
// the entry for the protocol of the current address has changed
if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() ) {
mChangedProtocols.append( current->protocol() );
}
// update protocol - has another protocol gained an address?
if ( current->protocol() != addressWid->protocol() ) {
modified = true;
// this proto is losing an entry
current->setProtocol( addressWid->protocol() );
if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() )
mChangedProtocols.append( current->protocol() );
}
if ( modified )
setModified(true);
}
}
}
void IMEditorWidget::slotDelete()
{
int num_selected = 0;
{
TQListViewItemIterator it( mWidget->lvAddresses, TQListViewItemIterator::Selected );
while ( it.current() ) {
num_selected++;
++it;
}
}
if ( num_selected == 0 )
return;
if ( KMessageBox::warningContinueCancel( this, i18n( "Do you really want to delete the selected address?",
"Do you really want to delete the %n selected addresses?", num_selected ),
i18n( "Confirm Delete" ), KStdGuiItem::del() ) != KMessageBox::Continue )
return;
TQListViewItemIterator it( mWidget->lvAddresses );
bool deletedPreferred = false;
while( it.current() ) {
if ( it.current()->isSelected() ) {
IMAddressLVI * current = static_cast<IMAddressLVI*>( *it );
if ( mChangedProtocols.find( current->protocol() ) == mChangedProtocols.end() )
mChangedProtocols.append( current->protocol() );
if ( current->preferred() )
deletedPreferred = true;
delete current;
} else
++it;
}
if ( deletedPreferred ) {
IMAddressLVI *first = static_cast<IMAddressLVI*>( mWidget->lvAddresses->firstChild() );
if ( first ) {
first->setPreferred( true );
mPreferred = first->address();
} else
mPreferred = "";
}
setModified( true );
}
TQString IMEditorWidget::preferred() const
{
TQString retval( mPreferred );
return retval.replace( TQChar( 0xE120 ), " on " );
}
KPluginInfo * IMEditorWidget::protocolFromString( const TQString &fieldValue ) const
{
TQValueList<KPluginInfo *>::ConstIterator it;
KPluginInfo * protocol = 0;
for ( it = mProtocols.begin(); it != mProtocols.end(); ++it ) {
if ( (*it)->property( "X-KDE-InstantMessagingKABCField" ).toString() == fieldValue ) {
protocol = *it;
break;
}
}
return protocol;
}
void IMEditorWidget::splitField( const TQString &str, TQString &app, TQString &name, TQString &value )
{
int colon = str.find( ':' );
if ( colon != -1 ) {
TQString tmp = str.left( colon );
value = str.mid( colon + 1 );
int dash = tmp.find( '-' );
if ( dash != -1 ) {
app = tmp.left( dash );
name = tmp.mid( dash + 1 );
}
}
}
#include "imeditorwidget.moc"
|