As promised, I'm trying to ramp up the Graffiti content floating around on the Internets. This post will reflect my first attempt at creating a Graffiti Widget. This particular Widget will allow you to enter in your email address (or the email address of the poor sap who's picture will be displayed on your Graffiti-powered site), then will take said email address and perform the MD5 hash described here by Jon Galloway to get the proper Gravatar icon. The code for this Widget can be seen below. Feel free to steal it, optimize it, and do anything else you think might make it better.
using System;
using System.Collections.Specialized;
using System.Web;
using Graffiti.Core;
using System.Web.UI;
using System.Web.Security;
namespace Bmg.Graffiti.Extensions
{
[WidgetInfo("3FD5239E-D91A-4ec1-B93A-01F27089A1BF",
"Gravatar Icon",
"Displays a Gravatar icon for a specific email address.")]
public class GravatarWidget : Widget
{
public string HTML;
public override string RenderData()
{
return HTML;
}
public override string Name
{
get { return "Gravatar Icon Widget"; }
}
protected string GetGravatarUrl(string emailAddress)
{
string hash = FormsAuthentication.HashPasswordForStoringInConfigFile(emailAddress.Trim(), "MD5");
hash = hash.Trim().ToLower();
string gravatarUrl =
string.Format("http://www.gravatar.com/avatar.php?gravatar_id={0}&rating=G&size=60", hash);
return gravatarUrl;
}
protected override NameValueCollection DataAsNameValueCollection()
{
NameValueCollection nvc = base.DataAsNameValueCollection();
return nvc;
}
public override StatusType SetValues(HttpContext context,
NameValueCollection nvc)
{
base.SetValues(context, nvc);
string email = nvc["Email"];
string imgUrl = GetGravatarUrl(email);
HTML = string.Format("<img src=\"{0}\"/>", imgUrl);
return StatusType.Success;
}
protected override FormElementCollection AddFormElements()
{
FormElementCollection fec = new FormElementCollection();
fec.Add(new TextFormElement("Email", "Email Address",
"Enter the email address for the Gravatar icon you wish to display."));
return fec;
}
}
}