summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/kopetemimesourcefactory.cpp
blob: a34d8aee648d1a2dc0b8ed0a6a3e1a565978326a (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
/*
    kopetemimesourcefactory.cpp - Kopete mime source factory

    Copyright (c) 2004      by Richard Smith         <kde@metafoo.co.uk>

    Kopete    (c) 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 "kopetemimesourcefactory.h"

#include "kopeteaccountmanager.h"
#include "kopetecontactlist.h"
#include "kopeteaccount.h"
#include "kopetecontact.h"
#include "kopetemetacontact.h"

#include <kdebug.h>
#include <kiconloader.h>

#include <qdragobject.h>
#include <qstring.h>
#include <qstringlist.h>

namespace Kopete
{

class MimeSourceFactory::Private
{
public:
	Private() : lastMimeSource( 0 ) {}
	~Private() { delete lastMimeSource; }
	mutable QMimeSource *lastMimeSource;
};

MimeSourceFactory::MimeSourceFactory()
 : d( new Private )
{
}

MimeSourceFactory::~MimeSourceFactory()
{
	delete d;
}

const QMimeSource *MimeSourceFactory::data( const QString &abs_name ) const
{
	// flag used to signal something went wrong when creating a mimesource
	bool completed = false;
	// extract and decode arguments
	QStringList parts = QStringList::split( QChar(':'), abs_name );
	for ( QStringList::Iterator it = parts.begin(); it != parts.end(); ++it )
		*it = KURL::decode_string( *it );

	QPixmap img;
	if ( parts[0] == QString::fromLatin1("kopete-contact-icon") )
	{
		if ( parts.size() >= 4 )
		{
			Account *account = AccountManager::self()->findAccount( parts[1], parts[2] );
			if ( account ) 
			{
				Contact *contact = account->contacts()[ parts[3] ];
				if ( contact )
				{
					img = contact->onlineStatus().iconFor( contact );
					completed = true;
				}
				else
					kdDebug( 14010 ) << k_funcinfo << "kopete-contact-icon: contact not found" << endl;
			}
			else
				kdDebug( 14010 ) << k_funcinfo << "kopete-contact-icon: account not found" << endl;
		}
		else
			kdDebug( 14010 ) << k_funcinfo << "kopete-contact-icon: insufficient information in abs_name: " << parts << endl;
	}

	if ( parts[0] == QString::fromLatin1("kopete-account-icon") )
	{
		if ( parts.size() >= 3 )
		{
			Account *account = AccountManager::self()->findAccount( parts[1], parts[2] );
			if ( account )
			{
				img = account->myself()->onlineStatus().iconFor( account->myself() );
				completed = true;
			}
			else
				kdDebug( 14010 ) << k_funcinfo << "kopete-account-icon: account not found" << endl;
		}
		else
			kdDebug( 14010 ) << k_funcinfo << "kopete-account-icon: insufficient information in abs_name: " << parts << endl;
	}

	if ( parts[0] == QString::fromLatin1("kopete-metacontact-icon") )
	{
		if ( parts.size() >= 2 )
		{
			MetaContact *mc = ContactList::self()->metaContact( parts[1] );
			if ( mc )
			{	
				img = SmallIcon( mc->statusIcon() );
				completed = true;
			}
		}
		else 
			kdDebug( 14010 ) << k_funcinfo << "kopete-metacontact-icon: insufficient information in abs_name: " << parts << endl;
	}

	if ( parts[0] == QString::fromLatin1("kopete-metacontact-photo") )
	{
		if ( parts.size() >= 2 )
		{
			MetaContact *mc = ContactList::self()->metaContact( parts[1] );
			if ( mc )
			{
				QImage photo = mc->photo();
				delete d->lastMimeSource;
				d->lastMimeSource = new QImageDrag( photo );
				return d->lastMimeSource;	
			}
		}
		else 
			kdDebug( 14010 ) << k_funcinfo << "kopete-metacontact-photo: insufficient information in abs_name: " << parts << endl;
	}
	
	if ( parts[0] == QString::fromLatin1("kopete-onlinestatus-icon") )
	{
		if ( parts.size() >= 2 )
		{
			/*
			 * We are using a dirty trick here: this mime source is supposed to return the
			 * icon for an arbitrary KOS instance. To do this, the caller needs to ask
			 * the KOS for the mime source key first, which also ensures the icon is
			 * currently in the cache. The cache is global, so we just need to find any
			 * existing KOS instance to return us the rendered icon from the cache.
			 * To find a valid KOS, we ask Kopete's account manager to locate an existing
			 * account. We'll use the myself() instance of that account to reference its
			 * current KOS object, which in turn has access to the global KOS icon cache.
			 * Note that if the cache has been invalidated in the meantime, we'll just
			 * get an empty pixmap back.
			 */
			Account *account = AccountManager::self()->accounts().getFirst();
			if ( account )
			{
				img = account->myself()->onlineStatus().iconFor( parts[1] );
				completed = true;
			}
			else
				kdDebug( 14010 ) << k_funcinfo << "kopete-onlinestatus-icon: no active account found" << endl;
		}
		else
			kdDebug( 14010 ) << k_funcinfo << "kopete-onlinestatus-icon: insufficient information in abs_name: " << parts << endl;
	}

	delete d->lastMimeSource;
	if ( completed )
		d->lastMimeSource = new QImageDrag( img.convertToImage() );
	else
		d->lastMimeSource = 0;
	return d->lastMimeSource;
}

} // END namespace Kopete

// vim: set noet ts=4 sts=4 sw=4: