summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/kopeteaccountmanager.cpp
blob: 21f0d688d5814609267a6764764c0c10f031fdf3 (plain)
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
/*
    kopeteaccountmanager.cpp - Kopete Account Manager

    Copyright (c) 2002-2003 by Martijn Klingens      <[email protected]>
    Copyright (c) 2003-2004 by Olivier Goffart       <ogoffart @ kde.org>

    Kopete    (c) 2002-2004 by the Kopete developers <[email protected]>

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include "kopeteaccountmanager.h"

#include <tqapplication.h>
#include <tqregexp.h>
#include <tqtimer.h>

#include <kconfig.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kplugininfo.h>

#include "kopeteaccount.h"
#include "kopeteaway.h"
#include "kopeteprotocol.h"
#include "kopetecontact.h"
#include "kopetecontactlist.h"
#include "kopetepluginmanager.h"
#include "kopeteonlinestatus.h"
#include "kopeteonlinestatusmanager.h"
#include "kopetemetacontact.h"
#include "kopetegroup.h"

namespace Kopete {

class AccountManager::Private
{
public:

	class AccountPtrList : public TQPtrList<Account>
	{
		protected:
			int compareItems( AccountPtrList::Item a, AccountPtrList::Item b )
			{
				uint priority1 = static_cast<Account*>(a)->priority();
				uint priority2 = static_cast<Account*>(b)->priority();

				if( a==b ) //two account are equal only if they are equal :-)
					return 0;  // remember than an account can be only once on the list, but two account may have the same priority when loading
				else if( priority1 > priority2 )
					return 1;
				else
					return -1;
			}
	} accounts;

};

AccountManager * AccountManager::s_self = 0L;

AccountManager * AccountManager::self()
{
	if ( !s_self )
		s_self = new AccountManager;

	return s_self;
}


AccountManager::AccountManager()
: TQObject( tqApp, "KopeteAccountManager" )
{
	d = new Private;
}


AccountManager::~AccountManager()
{
	s_self = 0L;

	delete d;
}

bool AccountManager::isAnyAccountConnected()
{
	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		if(it.current()->isConnected())
			return true;
	}
	return false;
}

void AccountManager::connectAll()
{
	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
		if(!it.current()->excludeConnect())
			it.current()->connect();
}

void AccountManager::setAvailableAll( const TQString &awayReason )
{
	Away::setGlobalAway( false );
	bool anyConnected = isAnyAccountConnected();

	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		if ( anyConnected )
		{
			if ( it.current()->isConnected() )
				it.current()->setAway( false, awayReason );
		}
		else
			if(!it.current()->excludeConnect())
				it.current()->connect();
	}
}

void AccountManager::disconnectAll()
{
	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
		it.current()->disconnect();
}

void AccountManager::setAwayAll( const TQString &awayReason, bool away )
{
	Away::setGlobalAway( true );
	bool anyConnected = isAnyAccountConnected();

	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		// FIXME: ICQ's invisible online should be set to invisible away
		Contact *self = it.current()->myself();
		bool isInvisible = self && self->onlineStatus().status() == OnlineStatus::Invisible;
		if ( anyConnected )
		{
			if ( it.current()->isConnected() && !isInvisible )
				it.current()->setAway( away, awayReason );
		}
		else
		{
			if ( !it.current()->excludeConnect() && !isInvisible )
				it.current()->setAway( away, awayReason );
		}
	}
}

void AccountManager::setOnlineStatus( uint category , const TQString& awayMessage, uint flags )
{
	OnlineStatusManager::Categories katgor=(OnlineStatusManager::Categories)category;
	bool anyConnected = isAnyAccountConnected();

	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		Account *account = it.current();
		Kopete::OnlineStatus status = OnlineStatusManager::self()->onlineStatus(account->protocol() , katgor);
		if ( anyConnected )
		{
			if ( account->isConnected() || ( (flags & ConnectIfOffline) && !account->excludeConnect() ) )
				account->setOnlineStatus( status , awayMessage );
		}
		else
		{
			if ( !account->excludeConnect() )
				account->setOnlineStatus( status , awayMessage );
		}
	}
}


TQColor AccountManager::guessColor( Protocol *protocol ) const
{
	// In a perfect wold, we should check if the color is actually not used by the account.
	// Anyway, this is not really required,  It would be a difficult job for about nothing more.
	//   -- Olivier
	int protocolCount = 0;

	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		if ( it.current()->protocol()->pluginId() == protocol->pluginId() )
			protocolCount++;
	}

	// let's figure a color
	TQColor color;
	switch ( protocolCount % 7 )
	{
	case 0:
		color = TQColor();
		break;
	case 1:
		color = TQt::red;
		break;
	case 2:
		color = TQt::green;
		break;
	case 3:
		color = TQt::blue;
		break;
	case 4:
		color = TQt::yellow;
		break;
	case 5:
		color = TQt::magenta;
		break;
	case 6:
		color = TQt::cyan;
		break;
	}

	return color;
}

Account* AccountManager::registerAccount( Account *account )
{
	if( !account || d->accounts.contains( account ) )
		return account;

	if( account->accountId().isEmpty() )
	{
		account->deleteLater();
		return 0L;
	}

	// If this account already exists, do nothing
	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		if ( ( account->protocol() == it.current()->protocol() ) && ( account->accountId() == it.current()->accountId() ) )
		{
			account->deleteLater();
			return 0L;
		}
	}

	d->accounts.append( account );
	d->accounts.sort();

	// Connect to the account's status changed signal
	connect(account->myself(), TQT_SIGNAL(onlineStatusChanged(Kopete::Contact *,
			const Kopete::OnlineStatus &, const Kopete::OnlineStatus &)),
		this, TQT_SLOT(slotAccountOnlineStatusChanged(Kopete::Contact *,
			const Kopete::OnlineStatus &, const Kopete::OnlineStatus &)));

	connect(account, TQT_SIGNAL(accountDestroyed(const Kopete::Account *)) , this, TQT_SLOT( unregisterAccount(const Kopete::Account *) ));

	emit accountRegistered( account );
	return account;
}

void AccountManager::unregisterAccount( const Account *account )
{
	kdDebug( 14010 ) << k_funcinfo << "Unregistering account " << account->accountId() << endl;
	d->accounts.remove( account );
	emit accountUnregistered( account );
}

const TQPtrList<Account>& AccountManager::accounts() const
{
	return d->accounts;
}

TQDict<Account> AccountManager::accounts( const Protocol *protocol ) const
{
	TQDict<Account> dict;
	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		if ( it.current()->protocol() == protocol && !it.current()->accountId().isNull() )
			dict.insert( it.current()->accountId(), it.current() );
	}

	return dict;
}

Account * AccountManager::findAccount( const TQString &protocolId, const TQString &accountId )
{
	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		if ( it.current()->protocol()->pluginId() == protocolId && it.current()->accountId() == accountId )
			return it.current();
	}
	return 0L;
}

void AccountManager::removeAccount( Account *account )
{
	if(!account->removeAccount())
		return;

	Protocol *protocol = account->protocol();


	TDEConfigGroup *configgroup = account->configGroup();

	// Clean up the contact list
	TQDictIterator<Kopete::Contact> it( account->contacts() );
	for ( ; it.current(); ++it )
	{
		Contact* c = it.current();
		MetaContact* mc = c->metaContact();
		if ( mc == ContactList::self()->myself() )
			continue;
		mc->removeContact( c );
		c->deleteLater();
		if ( mc->contacts().count() == 0 ) //we can delete the metacontact
		{
			//get the first group and it's members
			Group* group = mc->groups().first();
			TQPtrList<MetaContact> groupMembers = group->members();
			ContactList::self()->removeMetaContact( mc );
			if ( groupMembers.count() == 1 && groupMembers.findRef( mc ) != -1 )
				ContactList::self()->removeGroup( group );
		}
	}

	// Clean up the account list
	d->accounts.remove( account );

	// Clean up configuration
	configgroup->deleteGroup();
	configgroup->sync();

	delete account;

	if ( accounts( protocol ).isEmpty() )
	{
		// FIXME: pluginId() should return the internal name and not the class name, so
		//        we can get rid of this hack - Olivier/Martijn
		TQString protocolName = protocol->pluginId().remove( TQString::fromLatin1( "Protocol" ) ).lower();

		PluginManager::self()->setPluginEnabled( protocolName, false );
		PluginManager::self()->unloadPlugin( protocolName );
	}
}

void AccountManager::save()
{
	//kdDebug( 14010 ) << k_funcinfo << endl;
	d->accounts.sort();

	for ( TQPtrListIterator<Account> it( d->accounts ); it.current(); ++it )
	{
		TDEConfigBase *config = it.current()->configGroup();

		config->writeEntry( "Protocol", it.current()->protocol()->pluginId() );
		config->writeEntry( "AccountId", it.current()->accountId() );
	}

	TDEGlobal::config()->sync();
}

void AccountManager::load()
{
	connect( PluginManager::self(), TQT_SIGNAL( pluginLoaded( Kopete::Plugin * ) ),
		this, TQT_SLOT( slotPluginLoaded( Kopete::Plugin * ) ) );

	// Iterate over all groups that start with "Account_" as those are accounts
	// and load the required protocols if the account is enabled.
	// Don't try to optimize duplicate calls out, the plugin queue is smart enough
	// (and fast enough) to handle that without adding complexity here
	TDEConfig *config = TDEGlobal::config();
	TQStringList accountGroups = config->groupList().grep( TQRegExp( TQString::fromLatin1( "^Account_" ) ) );
	for ( TQStringList::Iterator it = accountGroups.begin(); it != accountGroups.end(); ++it )
	{
		config->setGroup( *it );

		TQString protocol = config->readEntry( "Protocol" );
		if ( protocol.endsWith( TQString::fromLatin1( "Protocol" ) ) )
			protocol = TQString::fromLatin1( "kopete_" ) + protocol.lower().remove( TQString::fromLatin1( "protocol" ) );

		if ( config->readBoolEntry( "Enabled", true ) )
			PluginManager::self()->loadPlugin( protocol, PluginManager::LoadAsync );
	}
}

void AccountManager::slotPluginLoaded( Plugin *plugin )
{
	Protocol* protocol = dynamic_cast<Protocol*>( plugin );
	if ( !protocol )
		return;

	// Iterate over all groups that start with "Account_" as those are accounts
	// and parse them if they are from this protocol
	TDEConfig *config = TDEGlobal::config();
	TQStringList accountGroups = config->groupList().grep( TQRegExp( TQString::fromLatin1( "^Account_" ) ) );
	for ( TQStringList::Iterator it = accountGroups.begin(); it != accountGroups.end(); ++it )
	{
		config->setGroup( *it );

		if ( config->readEntry( "Protocol" ) != protocol->pluginId() )
			continue;

		// There's no GUI for this, but developers may want to disable an account.
		if ( !config->readBoolEntry( "Enabled", true ) )
			continue;

		TQString accountId = config->readEntry( "AccountId" );
		if ( accountId.isEmpty() )
		{
			kdWarning( 14010 ) << k_funcinfo <<
				"Not creating account for empty accountId." << endl;
			continue;
		}

		kdDebug( 14010 ) << k_funcinfo <<
			"Creating account for '" << accountId << "'" << endl;

		Account *account = 0L;
		account = registerAccount( protocol->createNewAccount( accountId ) );
		if ( !account )
		{
			kdWarning( 14010 ) << k_funcinfo <<
				"Failed to create account for '" << accountId << "'" << endl;
			continue;
		}
	}
}

void AccountManager::slotAccountOnlineStatusChanged(Contact *c,
	const OnlineStatus &oldStatus, const OnlineStatus &newStatus)
{
	Account *account = c->account();
	if (!account)
		return;

	//kdDebug(14010) << k_funcinfo << endl;
	emit accountOnlineStatusChanged(account, oldStatus, newStatus);
}

} //END namespace Kopete

#include "kopeteaccountmanager.moc"
// vim: set noet ts=4 sts=4 sw=4:
// kate: tab-width 4; indent-mode csands;