Custom avatar service and implementation for nimbuzz avatars.

cusax-fix
Damian Minkov 14 years ago
parent 2964ade25a
commit ab33c2e27a

@ -925,6 +925,7 @@
bundle-contactsource,bundle-plugin-reconnect,bundle-plugin-securityconfig,
bundle-plugin-advancedconfig,
bundle-credentialsstorage,bundle-credentialsstorage-slick,
bundle-plugin-nimbuzzavatar,
bundle-replacement,bundle-youtube,bundle-dailymotion,bundle-smiley,
bundle-vimeo,bundle-vbox7,bundle-metacafe,bundle-flickr,bundle-hulu,
bundle-twitpic,bundle-directimage,bundle-bliptv,bundle-viddler,
@ -2458,6 +2459,17 @@ javax.swing.event, javax.swing.border"/>
</jar>
</target>
<!-- BUNDLE-NIMBUZZAVATAR -->
<target name="bundle-plugin-nimbuzzavatar">
<jar compress="false" destfile="${bundles.dest}/plugin-nimbuzzavatars.jar"
manifest="${src}/net/java/sip/communicator/plugin/nimbuzzavatars/nimbuzzavatars.manifest.mf">
<zipfileset dir="${dest}/net/java/sip/communicator/plugin/nimbuzzavatars"
prefix="net/java/sip/communicator/plugin/nimbuzzavatars" />
<zipfileset dir="${dest}/net/java/sip/communicator/service/customavatar"
prefix="net/java/sip/communicator/service/customavatar" />
</jar>
</target>
<!-- BUNDLE-REPLACEMENT -->
<target name="bundle-replacement">
<!-- Creates a bundle containing the replacement service.-->

@ -152,6 +152,7 @@ felix.auto.start.67= \
reference:file:sc-bundles/replacement-directimage.jar \
reference:file:sc-bundles/replacement-bliptv.jar \
reference:file:sc-bundles/replacement-viddler.jar \
reference:file:sc-bundles/plugin-nimbuzzavatars.jar \
reference:file:sc-bundles/chatconfig.jar \
reference:file:sc-bundles/addrbook.jar \
reference:file:sc-bundles/plugin-ldap.jar \

@ -98,6 +98,7 @@ felix.auto.start.6= \
reference:file:sc-bundles/metahistory.jar \
reference:file:sc-bundles/notification.jar \
reference:file:sc-bundles/credentialsstorage.jar \
reference:file:sc-bundles/plugin-nimbuzzavatars.jar \
reference:file:sc-bundles/osdependent.jar
felix.auto.start.7= \

@ -8,6 +8,7 @@
import java.util.*;
import net.java.sip.communicator.service.customavatar.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
@ -16,6 +17,7 @@
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smackx.packet.*;
import org.osgi.framework.*;
/**
* This class encapsulates the Roster class. Once created, it will
@ -1413,6 +1415,7 @@ void quit()
*/
private byte[] getAvatar(ContactJabberImpl contact)
{
byte[] result;
try
{
XMPPConnection connection = jabberProvider.getConnection();
@ -1423,7 +1426,14 @@ private byte[] getAvatar(ContactJabberImpl contact)
VCard card = new VCard();
card.load(connection, contact.getAddress());
return card.getAvatar();
result = card.getAvatar();
if(result == null)
{
result = searchForCustomAvatar(contact.getAddress());
}
return result;
}
catch (Exception ex)
{
@ -1436,11 +1446,50 @@ private byte[] getAvatar(ContactJabberImpl contact)
+ ex.getMessage(),
ex);
}
return new byte[0];
result = searchForCustomAvatar(contact.getAddress());
if(result == null)
result = new byte[0];
}
return result;
}
}
/**
* Query custom avatar services and returns the first found avtar.
* @return the found avatar if any.
*/
private byte[] searchForCustomAvatar(String address)
{
try
{
ServiceReference[] refs = JabberActivator.bundleContext
.getServiceReferences(CustomAvatarService.class.getName(), null);
if(refs == null)
return null;
for(ServiceReference r : refs)
{
CustomAvatarService avatarService =
(CustomAvatarService)JabberActivator
.bundleContext.getService(r);
byte[] res = avatarService.getAvatar(address);
if(res != null)
return res;
}
}
catch(Throwable t)
{
// if something is wrong just return empty image
}
return null;
}
/**
* Completes the identifier with the server part if no server part was
* previously added.

@ -30,6 +30,7 @@ Import-Package: org.osgi.framework,
org.jivesoftware.smackx.filetransfer,
org.jivesoftware.smackx.provider,
org.w3c.dom,
net.java.sip.communicator.service.customavatar,
net.java.sip.communicator.service.configuration,
net.java.sip.communicator.service.resources,
net.java.sip.communicator.util,

@ -0,0 +1,101 @@
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.plugin.nimbuzzavatars;
import net.java.sip.communicator.service.customavatar.*;
import org.osgi.framework.*;
import java.io.*;
import java.net.*;
/**
* OSGi bundle activator for the Nimbuzz custom avatars service.
*
* @author Damian Minkov
*/
public class NimbuzzAvatarsActivator
implements BundleActivator,
CustomAvatarService
{
/**
* The context.
*/
static BundleContext bundleContext;
/**
* The download link location.
*/
private static final String AVATAR_DOWNLOAD_LINK =
"http://avatar.nimbuzz.com/getAvatar?jid=";
/**
* The service name we are handling.
*/
private static final String SERVICE_NAME = "nimbuzz.com";
/**
* Starts this bundle.
* @param bc the bundle context
* @throws Exception if something goes wrong
*/
public void start(BundleContext bc)
throws Exception
{
bundleContext = bc;
bc.registerService(
CustomAvatarService.class.getName(),
this, null);
}
/**
* Stops this bundle.
* @param bc the bundle context
* @throws Exception if something goes wrong
*/
public void stop(BundleContext bc)
throws Exception
{
}
/**
* Returns the avatar bytes for the given contact address.
* @param address the address to search for its avatar.
* @return image bytes.
*/
public byte[] getAvatar(String address)
{
if(address == null || !address.endsWith(SERVICE_NAME))
{
return null;
}
try
{
URL sourceURL = new URL(AVATAR_DOWNLOAD_LINK + address);
URLConnection conn = sourceURL.openConnection();
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = conn.getInputStream();
byte[] b = new byte[1024];
int read;
while((read = in.read(b)) != -1)
{
out.write(b, 0, read);
}
in.close();
return out.toByteArray();
}
catch(Throwable t)
{}
return null;
}
}

@ -0,0 +1,8 @@
Bundle-Activator: net.java.sip.communicator.plugin.nimbuzzavatars.NimbuzzAvatarsActivator
Bundle-Name: NimbuzzAvatars
Bundle-Description: Custom Nimbuzz Avatars plugin
Bundle-Vendor: jitsi.org
Bundle-Version: 0.0.1
System-Bundle: yes
Export-Package: net.java.sip.communicator.service.customavatar
Import-Package: org.osgi.framework

@ -0,0 +1,24 @@
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.service.customavatar;
/**
* Service implementers can give a custom way of retrieving
* avatars for given contact address. ProtocolProviders will use
* these methods to search for avatar if their contacts are missing
* picture.
* @author Damian Minkov
*/
public interface CustomAvatarService
{
/**
* Returns the avatar bytes for the given contact address.
* @param address the address to search for its avatar.
* @return image bytes.
*/
public byte[] getAvatar(String address);
}
Loading…
Cancel
Save