TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

517 lines
17 KiB

  1. /*
  2. identitymanager.cpp
  3. This file is part of KMail, the KDE mail client.
  4. Copyright (c) 2002 Marc Mutz <mutz@kde.org>
  5. KMail is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License, version 2, as
  7. published by the Free Software Foundation.
  8. KMail is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. In addition, as a special exception, the copyright holders give
  16. permission to link the code of this program with any edition of
  17. the TQt library by Trolltech AS, Norway (or with modified versions
  18. of TQt that use the same license as TQt), and distribute linked
  19. combinations including the two. You must obey the GNU General
  20. Public License in all respects for all of the code used other than
  21. TQt. If you modify this file, you may extend this exception to
  22. your version of the file, but you are not obligated to do so. If
  23. you do not wish to do so, delete this exception statement from
  24. your version.
  25. */
  26. // config keys:
  27. static const char configKeyDefaultIdentity[] = "Default Identity";
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include "identitymanager.h"
  32. #include "identity.h" // for IdentityList::{export,import}Data
  33. #include <libemailfunctions/email.h> // for static helper functions
  34. #include <tdeemailsettings.h> // for IdentityEntry::fromControlCenter()
  35. #include <tdeapplication.h>
  36. #include <tdelocale.h>
  37. #include <kdebug.h>
  38. #include <tdeconfig.h>
  39. #include <kuser.h>
  40. #include <dcopclient.h>
  41. #include <tqregexp.h>
  42. #include <assert.h>
  43. using namespace KPIM;
  44. static TQCString newDCOPObjectName()
  45. {
  46. static int s_count = 0;
  47. TQCString name( "KPIM::IdentityManager" );
  48. if ( s_count++ ) {
  49. name += '-';
  50. name += TQCString().setNum( s_count );
  51. }
  52. return name;
  53. }
  54. IdentityManager::IdentityManager( bool readonly, TQObject * parent, const char * name )
  55. : ConfigManager( parent, name ), DCOPObject( newDCOPObjectName() )
  56. {
  57. mReadOnly = readonly;
  58. mConfig = new TDEConfig( "emailidentities", readonly );
  59. readConfig(mConfig);
  60. if ( mIdentities.isEmpty() ) {
  61. kdDebug(5006) << "emailidentities is empty -> convert from kmailrc" << endl;
  62. // No emailidentities file, or an empty one due to broken conversion (tdeconf_update bug in tdelibs <= 3.2.2)
  63. // => convert it, i.e. read settings from kmailrc
  64. TDEConfig kmailConf( "kmailrc", true );
  65. readConfig( &kmailConf );
  66. }
  67. // we need at least a default identity:
  68. if ( mIdentities.isEmpty() ) {
  69. kdDebug( 5006 ) << "IdentityManager: No identity found. Creating default." << endl;
  70. createDefaultIdentity();
  71. commit();
  72. }
  73. // Migration: people without settings in kemailsettings should get some
  74. if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
  75. writeConfig();
  76. }
  77. // The emitter is always called KPIM::IdentityManager even if we are not
  78. if ( !connectDCOPSignal( 0, "KPIM::IdentityManager", "identitiesChanged(TQCString,TQCString)",
  79. "slotIdentitiesChanged(TQCString,TQCString)", false ) )
  80. kdError(5650) << "IdentityManager: connection to identitiesChanged failed" << endl;
  81. }
  82. IdentityManager::~IdentityManager()
  83. {
  84. kdWarning( hasPendingChanges(), 5006 )
  85. << "IdentityManager: There were uncommitted changes!" << endl;
  86. delete mConfig;
  87. }
  88. void IdentityManager::commit()
  89. {
  90. // early out:
  91. if ( !hasPendingChanges() || mReadOnly ) return;
  92. TQValueList<uint> seenUOIDs;
  93. for ( TQValueList<Identity>::ConstIterator it = mIdentities.begin() ;
  94. it != mIdentities.end() ; ++it )
  95. seenUOIDs << (*it).uoid();
  96. TQValueList<uint> changedUOIDs;
  97. // find added and changed identities:
  98. for ( TQValueList<Identity>::ConstIterator it = mShadowIdentities.begin() ;
  99. it != mShadowIdentities.end() ; ++it ) {
  100. TQValueList<uint>::Iterator uoid = seenUOIDs.find( (*it).uoid() );
  101. if ( uoid != seenUOIDs.end() ) {
  102. const Identity & orig = identityForUoid( *uoid ); // look it up in mIdentities
  103. if ( *it != orig ) {
  104. // changed identity
  105. kdDebug( 5006 ) << "emitting changed() for identity " << *uoid << endl;
  106. emit changed( *it );
  107. changedUOIDs << *uoid;
  108. }
  109. seenUOIDs.remove( uoid );
  110. } else {
  111. // new identity
  112. kdDebug( 5006 ) << "emitting added() for identity " << (*it).uoid() << endl;
  113. emit added( *it );
  114. }
  115. }
  116. // what's left are deleted identities:
  117. for ( TQValueList<uint>::ConstIterator it = seenUOIDs.begin() ;
  118. it != seenUOIDs.end() ; ++it ) {
  119. kdDebug( 5006 ) << "emitting deleted() for identity " << (*it) << endl;
  120. emit deleted( *it );
  121. }
  122. mIdentities = mShadowIdentities;
  123. writeConfig();
  124. // now that mIdentities has all the new info, we can emit the added/changed
  125. // signals that ship a uoid. This is because the slots might use identityForUoid(uoid)...
  126. for ( TQValueList<uint>::ConstIterator it = changedUOIDs.begin() ;
  127. it != changedUOIDs.end() ; ++it )
  128. emit changed( *it );
  129. emit ConfigManager::changed(); // normal signal
  130. // DCOP signal for other IdentityManager instances
  131. // The emitter is always set to KPIM::IdentityManager, so that the connect works
  132. // This is why we can't use k_dcop_signals here, but need to use emitDCOPSignal
  133. TQByteArray data; TQDataStream arg( data, IO_WriteOnly );
  134. arg << kapp->dcopClient()->appId();
  135. arg << DCOPObject::objId(); // the real objId, for checking in slotIdentitiesChanged
  136. kapp->dcopClient()->emitDCOPSignal( "KPIM::IdentityManager", "identitiesChanged(TQCString,TQCString)", data );
  137. }
  138. void IdentityManager::rollback()
  139. {
  140. mShadowIdentities = mIdentities;
  141. }
  142. bool IdentityManager::hasPendingChanges() const
  143. {
  144. return mIdentities != mShadowIdentities;
  145. }
  146. TQStringList IdentityManager::identities() const
  147. {
  148. TQStringList result;
  149. for ( ConstIterator it = mIdentities.begin() ;
  150. it != mIdentities.end() ; ++it )
  151. result << (*it).identityName();
  152. return result;
  153. }
  154. TQStringList IdentityManager::shadowIdentities() const
  155. {
  156. TQStringList result;
  157. for ( ConstIterator it = mShadowIdentities.begin() ;
  158. it != mShadowIdentities.end() ; ++it )
  159. result << (*it).identityName();
  160. return result;
  161. }
  162. void IdentityManager::sort() {
  163. qHeapSort( mShadowIdentities );
  164. }
  165. void IdentityManager::writeConfig() const {
  166. TQStringList identities = groupList(mConfig);
  167. for ( TQStringList::Iterator group = identities.begin() ;
  168. group != identities.end() ; ++group )
  169. mConfig->deleteGroup( *group );
  170. int i = 0;
  171. for ( ConstIterator it = mIdentities.begin() ;
  172. it != mIdentities.end() ; ++it, ++i ) {
  173. TDEConfigGroup cg( mConfig, TQString::fromLatin1("Identity #%1").arg(i) );
  174. (*it).writeConfig( &cg );
  175. if ( (*it).isDefault() ) {
  176. // remember which one is default:
  177. TDEConfigGroup general( mConfig, "General" );
  178. general.writeEntry( configKeyDefaultIdentity, (*it).uoid() );
  179. // Also write the default identity to emailsettings
  180. KEMailSettings es;
  181. es.setSetting( KEMailSettings::RealName, (*it).fullName() );
  182. es.setSetting( KEMailSettings::EmailAddress, (*it).primaryEmailAddress() );
  183. es.setSetting( KEMailSettings::Organization, (*it).organization() );
  184. es.setSetting( KEMailSettings::ReplyToAddress, (*it).replyToAddr() );
  185. }
  186. }
  187. mConfig->sync();
  188. }
  189. void IdentityManager::readConfig(TDEConfigBase* config) {
  190. mIdentities.clear();
  191. TQStringList identities = groupList(config);
  192. if ( identities.isEmpty() ) return; // nothing to be done...
  193. TDEConfigGroup general( config, "General" );
  194. uint defaultIdentity = general.readUnsignedNumEntry( configKeyDefaultIdentity );
  195. bool haveDefault = false;
  196. for ( TQStringList::Iterator group = identities.begin() ;
  197. group != identities.end() ; ++group ) {
  198. TDEConfigGroup configGroup( config, *group );
  199. mIdentities << Identity();
  200. mIdentities.last().readConfig( &configGroup );
  201. if ( !haveDefault && mIdentities.last().uoid() == defaultIdentity ) {
  202. haveDefault = true;
  203. mIdentities.last().setIsDefault( true );
  204. }
  205. }
  206. if ( !haveDefault ) {
  207. kdWarning( 5006 ) << "IdentityManager: There was no default identity. Marking first one as default." << endl;
  208. mIdentities.first().setIsDefault( true );
  209. }
  210. qHeapSort( mIdentities );
  211. mShadowIdentities = mIdentities;
  212. }
  213. TQStringList IdentityManager::groupList(TDEConfigBase* config) const {
  214. return config->groupList().grep( TQRegExp("^Identity #\\d+$") );
  215. }
  216. IdentityManager::ConstIterator IdentityManager::begin() const {
  217. return mIdentities.begin();
  218. }
  219. IdentityManager::ConstIterator IdentityManager::end() const {
  220. return mIdentities.end();
  221. }
  222. IdentityManager::Iterator IdentityManager::modifyBegin() {
  223. return mShadowIdentities.begin();
  224. }
  225. IdentityManager::Iterator IdentityManager::modifyEnd() {
  226. return mShadowIdentities.end();
  227. }
  228. const Identity & IdentityManager::identityForName( const TQString & name ) const
  229. {
  230. kdWarning( 5006 )
  231. << "deprecated method IdentityManager::identityForName() called!" << endl;
  232. for ( ConstIterator it = begin() ; it != end() ; ++it )
  233. if ( (*it).identityName() == name ) return (*it);
  234. return Identity::null();
  235. }
  236. const Identity & IdentityManager::identityForUoid( uint uoid ) const {
  237. for ( ConstIterator it = begin() ; it != end() ; ++it )
  238. if ( (*it).uoid() == uoid ) return (*it);
  239. return Identity::null();
  240. }
  241. const Identity & IdentityManager::identityForNameOrDefault( const TQString & name ) const
  242. {
  243. const Identity & ident = identityForName( name );
  244. if ( ident.isNull() )
  245. return defaultIdentity();
  246. else
  247. return ident;
  248. }
  249. const Identity & IdentityManager::identityForUoidOrDefault( uint uoid ) const
  250. {
  251. const Identity & ident = identityForUoid( uoid );
  252. if ( ident.isNull() )
  253. return defaultIdentity();
  254. else
  255. return ident;
  256. }
  257. const Identity & IdentityManager::identityForAddress( const TQString & addresses ) const
  258. {
  259. const TQStringList addressList = KPIM::splitEmailAddrList( addresses );
  260. for( TQStringList::ConstIterator addrIt = addressList.begin();
  261. addrIt != addressList.end(); ++addrIt ) {
  262. const TQString addr = KPIM::getEmailAddress( *addrIt ).lower();
  263. for ( ConstIterator it = begin() ; it != end() ; ++it ) {
  264. const Identity & id = *it;
  265. if ( id.matchesEmailAddress( addr ) ) {
  266. return id;
  267. }
  268. }
  269. }
  270. return Identity::null();
  271. }
  272. bool IdentityManager::thatIsMe( const TQString & addressList ) const {
  273. return !identityForAddress( addressList ).isNull();
  274. }
  275. Identity & IdentityManager::modifyIdentityForName( const TQString & name )
  276. {
  277. for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
  278. if ( (*it).identityName() == name ) return (*it);
  279. kdWarning( 5006 ) << "IdentityManager::identityForName() used as newFromScratch() replacement!"
  280. << "\n name == \"" << name << "\"" << endl;
  281. return newFromScratch( name );
  282. }
  283. Identity & IdentityManager::modifyIdentityForUoid( uint uoid )
  284. {
  285. for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
  286. if ( (*it).uoid() == uoid ) return (*it);
  287. kdWarning( 5006 ) << "IdentityManager::identityForUoid() used as newFromScratch() replacement!"
  288. << "\n uoid == \"" << uoid << "\"" << endl;
  289. return newFromScratch( i18n("Unnamed") );
  290. }
  291. const Identity & IdentityManager::defaultIdentity() const {
  292. for ( ConstIterator it = begin() ; it != end() ; ++it )
  293. if ( (*it).isDefault() ) return (*it);
  294. (mIdentities.isEmpty() ? kdFatal( 5006 ) : kdWarning( 5006 ) )
  295. << "IdentityManager: No default identity found!" << endl;
  296. return *begin();
  297. }
  298. bool IdentityManager::setAsDefault( const TQString & name ) {
  299. // First, check if the identity actually exists:
  300. TQStringList names = shadowIdentities();
  301. if ( names.find( name ) == names.end() ) return false;
  302. // Then, change the default as requested:
  303. for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
  304. (*it).setIsDefault( (*it).identityName() == name );
  305. // and re-sort:
  306. sort();
  307. return true;
  308. }
  309. bool IdentityManager::setAsDefault( uint uoid ) {
  310. // First, check if the identity actually exists:
  311. bool found = false;
  312. for ( ConstIterator it = mShadowIdentities.begin() ;
  313. it != mShadowIdentities.end() ; ++it )
  314. if ( (*it).uoid() == uoid ) {
  315. found = true;
  316. break;
  317. }
  318. if ( !found ) return false;
  319. // Then, change the default as requested:
  320. for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
  321. (*it).setIsDefault( (*it).uoid() == uoid );
  322. // and re-sort:
  323. sort();
  324. return true;
  325. }
  326. bool IdentityManager::removeIdentity( const TQString & name ) {
  327. for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
  328. if ( (*it).identityName() == name ) {
  329. bool removedWasDefault = (*it).isDefault();
  330. mShadowIdentities.remove( it );
  331. if ( removedWasDefault )
  332. mShadowIdentities.first().setIsDefault( true );
  333. return true;
  334. }
  335. return false;
  336. }
  337. Identity & IdentityManager::newFromScratch( const TQString & name ) {
  338. return newFromExisting( Identity( name ) );
  339. }
  340. Identity & IdentityManager::newFromControlCenter( const TQString & name ) {
  341. KEMailSettings es;
  342. es.setProfile( es.defaultProfileName() );
  343. return newFromExisting( Identity( name,
  344. es.getSetting( KEMailSettings::RealName ),
  345. es.getSetting( KEMailSettings::EmailAddress ),
  346. es.getSetting( KEMailSettings::Organization ),
  347. es.getSetting( KEMailSettings::ReplyToAddress )
  348. ) );
  349. }
  350. Identity & IdentityManager::newFromExisting( const Identity & other,
  351. const TQString & name ) {
  352. mShadowIdentities << other;
  353. Identity & result = mShadowIdentities.last();
  354. result.setIsDefault( false ); // we don't want two default identities!
  355. result.setUoid( newUoid() ); // we don't want two identies w/ same UOID
  356. if ( !name.isNull() )
  357. result.setIdentityName( name );
  358. return result;
  359. }
  360. void IdentityManager::createDefaultIdentity() {
  361. TQString fullName, emailAddress;
  362. bool done = false;
  363. // Check if the application has any settings
  364. createDefaultIdentity( fullName, emailAddress );
  365. // If not, then use the kcontrol settings
  366. if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
  367. KEMailSettings emailSettings;
  368. fullName = emailSettings.getSetting( KEMailSettings::RealName );
  369. emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
  370. if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
  371. newFromControlCenter( i18n("Default") );
  372. done = true;
  373. } else {
  374. // If KEmailSettings doesn't have name and address, generate something from KUser
  375. KUser user;
  376. if ( fullName.isEmpty() )
  377. fullName = user.fullName();
  378. if ( emailAddress.isEmpty() ) {
  379. emailAddress = user.loginName();
  380. if ( !emailAddress.isEmpty() ) {
  381. TDEConfigGroup general( mConfig, "General" );
  382. TQString defaultdomain = general.readEntry( "Default domain" );
  383. if( !defaultdomain.isEmpty() ) {
  384. emailAddress += '@' + defaultdomain;
  385. }
  386. else {
  387. emailAddress = TQString();
  388. }
  389. }
  390. }
  391. }
  392. }
  393. if ( !done )
  394. mShadowIdentities << Identity( i18n("Default"), fullName, emailAddress );
  395. mShadowIdentities.last().setIsDefault( true );
  396. mShadowIdentities.last().setUoid( newUoid() );
  397. if ( mReadOnly ) // commit won't do it in readonly mode
  398. mIdentities = mShadowIdentities;
  399. }
  400. int IdentityManager::newUoid()
  401. {
  402. int uoid;
  403. // determine the UOIDs of all saved identities
  404. TQValueList<uint> usedUOIDs;
  405. for ( TQValueList<Identity>::ConstIterator it = mIdentities.begin() ;
  406. it != mIdentities.end() ; ++it )
  407. usedUOIDs << (*it).uoid();
  408. if ( hasPendingChanges() ) {
  409. // add UOIDs of all shadow identities. Yes, we will add a lot of duplicate
  410. // UOIDs, but avoiding duplicate UOIDs isn't worth the effort.
  411. for ( TQValueList<Identity>::ConstIterator it = mShadowIdentities.begin() ;
  412. it != mShadowIdentities.end() ; ++it ) {
  413. usedUOIDs << (*it).uoid();
  414. }
  415. }
  416. usedUOIDs << 0; // no UOID must be 0 because this value always refers to the
  417. // default identity
  418. do {
  419. uoid = kapp->random();
  420. } while ( usedUOIDs.find( uoid ) != usedUOIDs.end() );
  421. return uoid;
  422. }
  423. TQStringList KPIM::IdentityManager::allEmails() const
  424. {
  425. TQStringList lst;
  426. for ( ConstIterator it = begin() ; it != end() ; ++it ) {
  427. lst << (*it).primaryEmailAddress();
  428. }
  429. return lst;
  430. }
  431. void KPIM::IdentityManager::slotIdentitiesChanged( TQCString appId, TQCString objId )
  432. {
  433. // From standalone kmail to standalone korganizer, the appId will differ
  434. // From kontact the appId will match, so we need to test the objId
  435. if ( kapp->dcopClient()->appId() != appId || DCOPObject::objId() != objId ) {
  436. mConfig->reparseConfiguration();
  437. Q_ASSERT( !hasPendingChanges() );
  438. readConfig( mConfig );
  439. }
  440. }
  441. #include "identitymanager.moc"