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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
/* -*- mode: C++; c-file-style: "gnu" -*-
identitymanager.cpp
This file is part of KMail, the KDE mail client.
Copyright (c) 2002 Marc Mutz <mutz@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
KMail 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
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the TQt library by Trolltech AS, Norway (or with modified versions
of TQt that use the same license as TQt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
TQt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
// config keys:
static const char configKeyDefaultIdentity[] = "Default Identity";
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "identitymanager.h"
#include "identity.h" // for IdentityList::{export,import}Data
#include <libemailfunctions/email.h> // for static helper functions
#include <kemailsettings.h> // for IdentityEntry::fromControlCenter()
#include <kapplication.h>
#include <klocale.h>
#include <kdebug.h>
#include <kconfig.h>
#include <kuser.h>
#include <dcopclient.h>
#include <tqregexp.h>
#include <assert.h>
using namespace KPIM;
static TQCString newDCOPObjectName()
{
static int s_count = 0;
TQCString name( "KPIM::IdentityManager" );
if ( s_count++ ) {
name += '-';
name += TQCString().setNum( s_count );
}
return name;
}
IdentityManager::IdentityManager( bool readonly, TQObject * parent, const char * name )
: ConfigManager( parent, name ), DCOPObject( newDCOPObjectName() )
{
mReadOnly = readonly;
mConfig = new TDEConfig( "emailidentities", readonly );
readConfig(mConfig);
if ( mIdentities.isEmpty() ) {
kdDebug(5006) << "emailidentities is empty -> convert from kmailrc" << endl;
// No emailidentities file, or an empty one due to broken conversion (kconf_update bug in tdelibs <= 3.2.2)
// => convert it, i.e. read settings from kmailrc
TDEConfig kmailConf( "kmailrc", true );
readConfig( &kmailConf );
}
// we need at least a default identity:
if ( mIdentities.isEmpty() ) {
kdDebug( 5006 ) << "IdentityManager: No identity found. Creating default." << endl;
createDefaultIdentity();
commit();
}
// Migration: people without settings in kemailsettings should get some
if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
writeConfig();
}
// The emitter is always called KPIM::IdentityManager even if we are not
if ( !connectDCOPSignal( 0, "KPIM::IdentityManager", "identitiesChanged(TQCString,TQCString)",
"slotIdentitiesChanged(TQCString,TQCString)", false ) )
kdError(5650) << "IdentityManager: connection to identitiesChanged failed" << endl;
}
IdentityManager::~IdentityManager()
{
kdWarning( hasPendingChanges(), 5006 )
<< "IdentityManager: There were uncommitted changes!" << endl;
delete mConfig;
}
void IdentityManager::commit()
{
// early out:
if ( !hasPendingChanges() || mReadOnly ) return;
TQValueList<uint> seenUOIDs;
for ( TQValueList<Identity>::ConstIterator it = mIdentities.begin() ;
it != mIdentities.end() ; ++it )
seenUOIDs << (*it).uoid();
TQValueList<uint> changedUOIDs;
// find added and changed identities:
for ( TQValueList<Identity>::ConstIterator it = mShadowIdentities.begin() ;
it != mShadowIdentities.end() ; ++it ) {
TQValueList<uint>::Iterator uoid = seenUOIDs.find( (*it).uoid() );
if ( uoid != seenUOIDs.end() ) {
const Identity & orig = identityForUoid( *uoid ); // look it up in mIdentities
if ( *it != orig ) {
// changed identity
kdDebug( 5006 ) << "emitting changed() for identity " << *uoid << endl;
emit changed( *it );
changedUOIDs << *uoid;
}
seenUOIDs.remove( uoid );
} else {
// new identity
kdDebug( 5006 ) << "emitting added() for identity " << (*it).uoid() << endl;
emit added( *it );
}
}
// what's left are deleted identities:
for ( TQValueList<uint>::ConstIterator it = seenUOIDs.begin() ;
it != seenUOIDs.end() ; ++it ) {
kdDebug( 5006 ) << "emitting deleted() for identity " << (*it) << endl;
emit deleted( *it );
}
mIdentities = mShadowIdentities;
writeConfig();
// now that mIdentities has all the new info, we can emit the added/changed
// signals that ship a uoid. This is because the slots might use identityForUoid(uoid)...
for ( TQValueList<uint>::ConstIterator it = changedUOIDs.begin() ;
it != changedUOIDs.end() ; ++it )
emit changed( *it );
emit ConfigManager::changed(); // normal signal
// DCOP signal for other IdentityManager instances
// The emitter is always set to KPIM::IdentityManager, so that the connect works
// This is why we can't use k_dcop_signals here, but need to use emitDCOPSignal
TQByteArray data; TQDataStream arg( data, IO_WriteOnly );
arg << kapp->dcopClient()->appId();
arg << DCOPObject::objId(); // the real objId, for checking in slotIdentitiesChanged
kapp->dcopClient()->emitDCOPSignal( "KPIM::IdentityManager", "identitiesChanged(TQCString,TQCString)", data );
}
void IdentityManager::rollback()
{
mShadowIdentities = mIdentities;
}
bool IdentityManager::hasPendingChanges() const
{
return mIdentities != mShadowIdentities;
}
TQStringList IdentityManager::identities() const
{
TQStringList result;
for ( ConstIterator it = mIdentities.begin() ;
it != mIdentities.end() ; ++it )
result << (*it).identityName();
return result;
}
TQStringList IdentityManager::shadowIdentities() const
{
TQStringList result;
for ( ConstIterator it = mShadowIdentities.begin() ;
it != mShadowIdentities.end() ; ++it )
result << (*it).identityName();
return result;
}
void IdentityManager::sort() {
qHeapSort( mShadowIdentities );
}
void IdentityManager::writeConfig() const {
TQStringList identities = groupList(mConfig);
for ( TQStringList::Iterator group = identities.begin() ;
group != identities.end() ; ++group )
mConfig->deleteGroup( *group );
int i = 0;
for ( ConstIterator it = mIdentities.begin() ;
it != mIdentities.end() ; ++it, ++i ) {
TDEConfigGroup cg( mConfig, TQString::fromLatin1("Identity #%1").arg(i) );
(*it).writeConfig( &cg );
if ( (*it).isDefault() ) {
// remember which one is default:
TDEConfigGroup general( mConfig, "General" );
general.writeEntry( configKeyDefaultIdentity, (*it).uoid() );
// Also write the default identity to emailsettings
KEMailSettings es;
es.setSetting( KEMailSettings::RealName, (*it).fullName() );
es.setSetting( KEMailSettings::EmailAddress, (*it).primaryEmailAddress() );
es.setSetting( KEMailSettings::Organization, (*it).organization() );
es.setSetting( KEMailSettings::ReplyToAddress, (*it).replyToAddr() );
}
}
mConfig->sync();
}
void IdentityManager::readConfig(TDEConfigBase* config) {
mIdentities.clear();
TQStringList identities = groupList(config);
if ( identities.isEmpty() ) return; // nothing to be done...
TDEConfigGroup general( config, "General" );
uint defaultIdentity = general.readUnsignedNumEntry( configKeyDefaultIdentity );
bool haveDefault = false;
for ( TQStringList::Iterator group = identities.begin() ;
group != identities.end() ; ++group ) {
TDEConfigGroup configGroup( config, *group );
mIdentities << Identity();
mIdentities.last().readConfig( &configGroup );
if ( !haveDefault && mIdentities.last().uoid() == defaultIdentity ) {
haveDefault = true;
mIdentities.last().setIsDefault( true );
}
}
if ( !haveDefault ) {
kdWarning( 5006 ) << "IdentityManager: There was no default identity. Marking first one as default." << endl;
mIdentities.first().setIsDefault( true );
}
qHeapSort( mIdentities );
mShadowIdentities = mIdentities;
}
TQStringList IdentityManager::groupList(TDEConfigBase* config) const {
return config->groupList().grep( TQRegExp("^Identity #\\d+$") );
}
IdentityManager::ConstIterator IdentityManager::begin() const {
return mIdentities.begin();
}
IdentityManager::ConstIterator IdentityManager::end() const {
return mIdentities.end();
}
IdentityManager::Iterator IdentityManager::modifyBegin() {
return mShadowIdentities.begin();
}
IdentityManager::Iterator IdentityManager::modifyEnd() {
return mShadowIdentities.end();
}
const Identity & IdentityManager::identityForName( const TQString & name ) const
{
kdWarning( 5006 )
<< "deprecated method IdentityManager::identityForName() called!" << endl;
for ( ConstIterator it = begin() ; it != end() ; ++it )
if ( (*it).identityName() == name ) return (*it);
return Identity::null();
}
const Identity & IdentityManager::identityForUoid( uint uoid ) const {
for ( ConstIterator it = begin() ; it != end() ; ++it )
if ( (*it).uoid() == uoid ) return (*it);
return Identity::null();
}
const Identity & IdentityManager::identityForNameOrDefault( const TQString & name ) const
{
const Identity & ident = identityForName( name );
if ( ident.isNull() )
return defaultIdentity();
else
return ident;
}
const Identity & IdentityManager::identityForUoidOrDefault( uint uoid ) const
{
const Identity & ident = identityForUoid( uoid );
if ( ident.isNull() )
return defaultIdentity();
else
return ident;
}
const Identity & IdentityManager::identityForAddress( const TQString & addresses ) const
{
const TQStringList addressList = KPIM::splitEmailAddrList( addresses );
for( TQStringList::ConstIterator addrIt = addressList.begin();
addrIt != addressList.end(); ++addrIt ) {
const TQString addr = KPIM::getEmailAddress( *addrIt ).lower();
for ( ConstIterator it = begin() ; it != end() ; ++it ) {
const Identity & id = *it;
if ( id.matchesEmailAddress( addr ) ) {
return id;
}
}
}
return Identity::null();
}
bool IdentityManager::thatIsMe( const TQString & addressList ) const {
return !identityForAddress( addressList ).isNull();
}
Identity & IdentityManager::modifyIdentityForName( const TQString & name )
{
for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
if ( (*it).identityName() == name ) return (*it);
kdWarning( 5006 ) << "IdentityManager::identityForName() used as newFromScratch() replacement!"
<< "\n name == \"" << name << "\"" << endl;
return newFromScratch( name );
}
Identity & IdentityManager::modifyIdentityForUoid( uint uoid )
{
for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
if ( (*it).uoid() == uoid ) return (*it);
kdWarning( 5006 ) << "IdentityManager::identityForUoid() used as newFromScratch() replacement!"
<< "\n uoid == \"" << uoid << "\"" << endl;
return newFromScratch( i18n("Unnamed") );
}
const Identity & IdentityManager::defaultIdentity() const {
for ( ConstIterator it = begin() ; it != end() ; ++it )
if ( (*it).isDefault() ) return (*it);
(mIdentities.isEmpty() ? kdFatal( 5006 ) : kdWarning( 5006 ) )
<< "IdentityManager: No default identity found!" << endl;
return *begin();
}
bool IdentityManager::setAsDefault( const TQString & name ) {
// First, check if the identity actually exists:
TQStringList names = shadowIdentities();
if ( names.find( name ) == names.end() ) return false;
// Then, change the default as requested:
for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
(*it).setIsDefault( (*it).identityName() == name );
// and re-sort:
sort();
return true;
}
bool IdentityManager::setAsDefault( uint uoid ) {
// First, check if the identity actually exists:
bool found = false;
for ( ConstIterator it = mShadowIdentities.begin() ;
it != mShadowIdentities.end() ; ++it )
if ( (*it).uoid() == uoid ) {
found = true;
break;
}
if ( !found ) return false;
// Then, change the default as requested:
for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
(*it).setIsDefault( (*it).uoid() == uoid );
// and re-sort:
sort();
return true;
}
bool IdentityManager::removeIdentity( const TQString & name ) {
for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
if ( (*it).identityName() == name ) {
bool removedWasDefault = (*it).isDefault();
mShadowIdentities.remove( it );
if ( removedWasDefault )
mShadowIdentities.first().setIsDefault( true );
return true;
}
return false;
}
Identity & IdentityManager::newFromScratch( const TQString & name ) {
return newFromExisting( Identity( name ) );
}
Identity & IdentityManager::newFromControlCenter( const TQString & name ) {
KEMailSettings es;
es.setProfile( es.defaultProfileName() );
return newFromExisting( Identity( name,
es.getSetting( KEMailSettings::RealName ),
es.getSetting( KEMailSettings::EmailAddress ),
es.getSetting( KEMailSettings::Organization ),
es.getSetting( KEMailSettings::ReplyToAddress )
) );
}
Identity & IdentityManager::newFromExisting( const Identity & other,
const TQString & name ) {
mShadowIdentities << other;
Identity & result = mShadowIdentities.last();
result.setIsDefault( false ); // we don't want two default identities!
result.setUoid( newUoid() ); // we don't want two identies w/ same UOID
if ( !name.isNull() )
result.setIdentityName( name );
return result;
}
void IdentityManager::createDefaultIdentity() {
TQString fullName, emailAddress;
bool done = false;
// Check if the application has any settings
createDefaultIdentity( fullName, emailAddress );
// If not, then use the kcontrol settings
if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
KEMailSettings emailSettings;
fullName = emailSettings.getSetting( KEMailSettings::RealName );
emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
newFromControlCenter( i18n("Default") );
done = true;
} else {
// If KEmailSettings doesn't have name and address, generate something from KUser
KUser user;
if ( fullName.isEmpty() )
fullName = user.fullName();
if ( emailAddress.isEmpty() ) {
emailAddress = user.loginName();
if ( !emailAddress.isEmpty() ) {
TDEConfigGroup general( mConfig, "General" );
TQString defaultdomain = general.readEntry( "Default domain" );
if( !defaultdomain.isEmpty() ) {
emailAddress += '@' + defaultdomain;
}
else {
emailAddress = TQString();
}
}
}
}
}
if ( !done )
mShadowIdentities << Identity( i18n("Default"), fullName, emailAddress );
mShadowIdentities.last().setIsDefault( true );
mShadowIdentities.last().setUoid( newUoid() );
if ( mReadOnly ) // commit won't do it in readonly mode
mIdentities = mShadowIdentities;
}
int IdentityManager::newUoid()
{
int uoid;
// determine the UOIDs of all saved identities
TQValueList<uint> usedUOIDs;
for ( TQValueList<Identity>::ConstIterator it = mIdentities.begin() ;
it != mIdentities.end() ; ++it )
usedUOIDs << (*it).uoid();
if ( hasPendingChanges() ) {
// add UOIDs of all shadow identities. Yes, we will add a lot of duplicate
// UOIDs, but avoiding duplicate UOIDs isn't worth the effort.
for ( TQValueList<Identity>::ConstIterator it = mShadowIdentities.begin() ;
it != mShadowIdentities.end() ; ++it ) {
usedUOIDs << (*it).uoid();
}
}
usedUOIDs << 0; // no UOID must be 0 because this value always refers to the
// default identity
do {
uoid = kapp->random();
} while ( usedUOIDs.find( uoid ) != usedUOIDs.end() );
return uoid;
}
TQStringList KPIM::IdentityManager::allEmails() const
{
TQStringList lst;
for ( ConstIterator it = begin() ; it != end() ; ++it ) {
lst << (*it).primaryEmailAddress();
}
return lst;
}
void KPIM::IdentityManager::slotIdentitiesChanged( TQCString appId, TQCString objId )
{
// From standalone kmail to standalone korganizer, the appId will differ
// From kontact the appId will match, so we need to test the objId
if ( kapp->dcopClient()->appId() != appId || DCOPObject::objId() != objId ) {
mConfig->reparseConfiguration();
Q_ASSERT( !hasPendingChanges() );
readConfig( mConfig );
}
}
#include "identitymanager.moc"
|