summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/kopeteprotocol.cpp
blob: db40cced9889c80b47dcd4179bff2d0af8ef31fb (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
/*
    kopeteprotocol.cpp - Kopete Protocol

    Copyright (c) 2002      by Duncan Mac-Vicar Prett <duncan@kde.org>
    Copyright (c) 2002-2003 by Martijn Klingens       <klingens@kde.org>
    Copyright (c) 2002-2004 by Olivier Goffart        <ogoffart @tiscalinet.be>

    Kopete    (c) 2002-2004 by the Kopete developers  <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * 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 "kopeteprotocol.h"

#include <kdebug.h>
#include <tdeaction.h>
#include <tdelocale.h>

#include <tqdict.h>

#include "kopeteaccountmanager.h"
#include "kopeteaccount.h"
#include "kopetecontact.h"
#include "kopeteglobal.h"
#include "kopetecontactproperty.h"
#include "kopetemetacontact.h"

namespace Kopete
{

class Protocol::Private
{
public:
	bool unloading;
	int capabilities;
	/*
	 * Make sure we always have a lastSeen and a fullname property as long as
	 * a protocol is loaded
	 */
	ContactPropertyTmpl mStickLastSeen;
	ContactPropertyTmpl mStickFullName;
	
	Kopete::OnlineStatus accountNotConnectedStatus;
};

Protocol::Protocol( TDEInstance *instance, TQObject *parent, const char *name )
: Plugin( instance, parent, name )
{
	d = new Private;
	d->mStickLastSeen = Global::Properties::self()->lastSeen();
	d->mStickFullName = Global::Properties::self()->fullName();
	d->unloading = false;
	d->capabilities = 0;
	d->accountNotConnectedStatus = Kopete::OnlineStatus( Kopete::OnlineStatus::Unknown, 0, this, Kopete::OnlineStatus::AccountOffline, TQString::fromLatin1( "account_offline_overlay" ), i18n( "Account Offline" ) );
}

Protocol::~Protocol()
{
	// Remove all active accounts
	TQDict<Account> accounts = AccountManager::self()->accounts( this );
	if ( !accounts.isEmpty() )
	{
		kdWarning( 14010 ) << k_funcinfo << "Deleting protocol with existing accounts! Did the account unloading go wrong?" << endl;

		for( TQDictIterator<Account> it( accounts ); it.current() ; ++it )
			delete *it;
	}

	delete d;
}

unsigned int Protocol::capabilities() const
{
	return d->capabilities;
}

void Protocol::setCapabilities( unsigned int capabilities )
{
	d->capabilities = capabilities;
}


Kopete::OnlineStatus Protocol::accountOfflineStatus() const
{
	return d->accountNotConnectedStatus;
}

void Protocol::slotAccountOnlineStatusChanged( Contact *self )
{//slot connected in aboutToUnload
	if ( !self || !self->account() || self->account()->isConnected())
		return;
	// some protocols change status several times during shutdown.  We should only call deleteLater() once
	disconnect( self, 0, this, 0 );

	connect( self->account(), TQ_SIGNAL(accountDestroyed(const Kopete::Account* )),
		this, TQ_SLOT( slotAccountDestroyed( ) ) );

	self->account()->deleteLater();
}

void Protocol::slotAccountDestroyed( )
{
	TQDict<Account> dict = AccountManager::self()->accounts( this );
	if ( dict.isEmpty() )
	{
		// While at this point we are still in a stack trace from the destroyed
		// account it's safe to emit readyForUnload already, because it uses a
		// deleteLater rather than a delete for exactly this reason, to keep the
		// API managable
		emit( readyForUnload() );
	}
}

void Protocol::aboutToUnload()
{

	d->unloading = true;

	// Disconnect all accounts
	TQDict<Account> accounts = AccountManager::self()->accounts( this );
	
	if ( accounts.isEmpty() )
		emit readyForUnload();
	else for ( TQDictIterator<Account> it( accounts ); it.current() ; ++it )
	{
		if ( it.current()->myself() && it.current()->myself()->isOnline() )
		{
			kdDebug( 14010 ) << k_funcinfo << it.current()->accountId() <<
				" is still connected, disconnecting..." << endl;

			TQObject::connect( it.current()->myself(),
				TQ_SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ),
				this, TQ_SLOT( slotAccountOnlineStatusChanged( Kopete::Contact * ) ) );
			it.current()->disconnect();
		}
		else
		{
			// Remove account, it's already disconnected
			kdDebug( 14010 ) << k_funcinfo << it.current()->accountId() <<
				" is already disconnected, deleting..." << endl;

			TQObject::connect( it.current(), TQ_SIGNAL( accountDestroyed( const Kopete::Account* ) ),
				this, TQ_SLOT( slotAccountDestroyed( ) ) );
			it.current()->deleteLater();
		}
	}
}



void Protocol::slotMetaContactAboutToSave( MetaContact *metaContact )
{
	TQMap<TQString, TQString> serializedData, sd;
	TQMap<TQString, TQString> addressBookData, ad;
	TQMap<TQString, TQString>::Iterator it;

	//kdDebug( 14010 ) << "Protocol::metaContactAboutToSave: protocol " << pluginId() << ": serializing " << metaContact->displayName() << endl;

	TQPtrList<Contact> contacts=metaContact->contacts();
	for (Contact *c=contacts.first() ; c ; c=contacts.next() )
	{
		if( c->protocol()->pluginId() != pluginId() )
			continue;

		sd.clear();
		ad.clear();

		// Preset the contactId and displayName, if the plugin doesn't want to save
		// them, or use its own format, it can call clear() on the provided list
		sd[ TQString::fromLatin1( "contactId" ) ] =   c->contactId();
		//TODO(nick) remove
		sd[ TQString::fromLatin1( "displayName" ) ] = c->property(Global::Properties::self()->nickName()).value().toString();
		if(c->account())
			sd[ TQString::fromLatin1( "accountId" ) ] = c->account()->accountId();

		// If there's an index field preset it too
		TQString index = c->protocol()->addressBookIndexField();
		if( !index.isEmpty() )
			ad[ index ] = c->contactId();

		c->serializeProperties( sd );
		c->serialize( sd, ad );

		// Merge the returned fields with what we already (may) have
		for( it = sd.begin(); it != sd.end(); ++it )
		{
			// The Unicode chars E000-F800 are non-printable and reserved for
			// private use in applications. For more details, see also
			// http://www.unicode.org/charts/PDF/UE000.pdf.
			// Inside libtdeabc the use of TQChar( 0xE000 ) has been standardized
			// as separator for the string lists, use this also for the 'normal'
			// serialized data.
			if( serializedData.contains( it.key() ) )
				serializedData[ it.key() ] = serializedData[ it.key() ] + TQChar( 0xE000 ) + it.data();
			else
				serializedData[ it.key() ] = it.data();
		}

		for( it = ad.begin(); it != ad.end(); ++it )
		{
			if( addressBookData.contains( it.key() ) )
				addressBookData[ it.key() ] = addressBookData[ it.key() ] + TQChar( 0xE000 ) + it.data();
			else
				addressBookData[ it.key() ] = it.data();
		}
	}

	// Pass all returned fields to the contact list
	//if( !serializedData.isEmpty() ) //even if we are empty, that mean there are no contact, so remove old value
	metaContact->setPluginData( this, serializedData );

	for( it = addressBookData.begin(); it != addressBookData.end(); ++it )
	{
		//kdDebug( 14010 ) << "Protocol::metaContactAboutToSave: addressBookData: key: " << it.key() << ", data: " << it.data() << endl;
		// FIXME: This is a terrible hack to check the key name for the phrase "messaging/"
		//        to indicate what app name to use, but for now it's by far the easiest
		//        way to get this working.
		//        Once all this is in CVS and the actual storage in libtdeabc is working
		//        we can devise a better API, but with the constantly changing
		//        requirements every time I learn more about tdeabc I'd better no touch
		//        the API yet - Martijn
		if( it.key().startsWith( TQString::fromLatin1( "messaging/" ) ) )
		{
			metaContact->setAddressBookField( this, it.key(), TQString::fromLatin1( "All" ), it.data() );
//			kdDebug(14010) << k_funcinfo << "metaContact->setAddressBookField( " << this << ", " << it.key() << ", \"All\", " << it.data() << " );" << endl;
		}
		else
			metaContact->setAddressBookField( this, TQString::fromLatin1( "kopete" ), it.key(), it.data() );
	}
}

void Protocol::deserialize( MetaContact *metaContact, const TQMap<TQString, TQString> &data )
{
	/*kdDebug( 14010 ) << "Protocol::deserialize: protocol " <<
		pluginId() << ": deserializing " << metaContact->displayName() << endl;*/

	TQMap<TQString, TQStringList> serializedData;
	TQMap<TQString, TQStringList::Iterator> serializedDataIterators;
	TQMap<TQString, TQString>::ConstIterator it;
	for( it = data.begin(); it != data.end(); ++it )
	{
		serializedData[ it.key() ] = TQStringList::split( TQChar( 0xE000 ), it.data(), true );
		serializedDataIterators[ it.key() ] = serializedData[ it.key() ].begin();
	}

	uint count = serializedData[TQString::fromLatin1("contactId")].count();

	// Prepare the independent entries to pass to the plugin's implementation
	for( uint i = 0; i < count ; i++ )
	{
		TQMap<TQString, TQString> sd;
		TQMap<TQString, TQStringList::Iterator>::Iterator serializedDataIt;
		for( serializedDataIt = serializedDataIterators.begin(); serializedDataIt != serializedDataIterators.end(); ++serializedDataIt )
		{
			sd[ serializedDataIt.key() ] = *( serializedDataIt.data() );
			++( serializedDataIt.data() );
		}

		const TQString& accountId=sd[ TQString::fromLatin1( "accountId" ) ];
		// myself was allowed in the contactlist in old version of kopete.
		// But if one keep it on the contactlist now, it may conflict witht he myself metacontact.
		// So ignore it
		if(accountId == sd[ TQString::fromLatin1( "contactId" ) ] )
		{
			kdDebug( 14010 ) << k_funcinfo << "Myself contact was on the contactlist.xml for account " << accountId << ".  Ignore it" << endl;
			continue;
		}

		// FIXME: This code almost certainly breaks when having more than
		//        one contact in a meta contact. There are solutions, but
		//        they are all hacky and the API needs revision anyway (see
		//        FIXME a few lines below :), so I'm not going to add that
		//        for now.
		//        Note that even though it breaks, the current code will
		//        never notice, since none of the plugins use the address
		//        book data in the deserializer yet, only when serializing.
		//        - Martijn
		TQMap<TQString, TQString> ad;
		TQStringList tdeabcFields = addressBookFields();
		for( TQStringList::Iterator fieldIt = tdeabcFields.begin(); fieldIt != tdeabcFields.end(); ++fieldIt )
		{
			// FIXME: This hack is even more ugly, and has the same reasons as the similar
			//        hack in the serialize code.
			//        Once this code is actually capable of talking to tdeabc this hack
			//        should be removed ASAP! - Martijn
			if( ( *fieldIt ).startsWith( TQString::fromLatin1( "messaging/" ) ) )
				ad[ *fieldIt ] = metaContact->addressBookField( this, *fieldIt, TQString::fromLatin1( "All" ) );
			else
				ad[ *fieldIt ] = metaContact->addressBookField( this, TQString::fromLatin1( "kopete" ), *fieldIt );
		}

		// Check if we have an account id. If not we're deserializing a Kopete 0.6 contact
		// (our our config is corrupted). Pick the first available account there. This
		// might not be what you want for corrupted accounts, but it's correct for people
		// who migrate from 0.6, as there's only one account in that case
		if( accountId.isNull() )
		{
			TQDict<Account> accounts = AccountManager::self()->accounts( this );
			if ( accounts.count() > 0 )
			{
				sd[ TQString::fromLatin1( "accountId" ) ] = TQDictIterator<Account>( accounts ).currentKey();
			}
			else
			{
				kdWarning( 14010 ) << k_funcinfo <<
					"No account available and account not set in " \
					"contactlist.xml either!" << endl
					<< "Not deserializing this contact." << endl;
				return;
			}
		}

		
		Contact *c = deserializeContact( metaContact, sd, ad );
		if (c) // should never be null but I do not like crashes
			c->deserializeProperties( sd );
	}
}

Contact *Protocol::deserializeContact(
	MetaContact */*metaContact */,
	const TQMap<TQString, TQString> & /* serializedData */,
	const TQMap<TQString, TQString> & /* addressBookData */ )
{
	/* Default implementation does nothing */
	return 0;
}


} //END namespace Kopete

#include "kopeteprotocol.moc"