aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/gravatar.c
blob: 06cbea9d0d64dfa4e8a6e1b0fdae75578c07bcd5 (plain) (blame)
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
#include "private.h"

#include <Elementary.h>

#include "gravatar.h"
#include "config.h"
#include "termio.h"
#include "media.h"
#include "md5/md5.h"

/* specific log domain to help debug the gravatar module */
int _gravatar_log_dom = -1;

#undef CRITICAL
#undef ERR
#undef WRN
#undef INF
#undef DBG

#define CRIT(...)     EINA_LOG_DOM_CRIT(_gravatar_log_dom, __VA_ARGS__)
#define ERR(...)      EINA_LOG_DOM_ERR (_gravatar_log_dom, __VA_ARGS__)
#define WRN(...)      EINA_LOG_DOM_WARN(_gravatar_log_dom, __VA_ARGS__)
#define INF(...)      EINA_LOG_DOM_INFO(_gravatar_log_dom, __VA_ARGS__)
#define DBG(...)      EINA_LOG_DOM_DBG (_gravatar_log_dom, __VA_ARGS__)

#define GRAVATAR_URL_START "http://www.gravatar.com/avatar/"
#define GRAVATAR_URL_END ""

typedef struct _Gravatar {
     const char *url;
     Config *config;
} Gravatar;

static Evas_Object *
_tooltip_content(void *data,
                 Evas_Object *obj,
                 Evas_Object *_tt EINA_UNUSED)
{
   Gravatar *g = data;
   Evas_Object *o;

   o = media_add(obj, g->url, g->config, MEDIA_STRETCH, MEDIA_TYPE_IMG);
   evas_object_size_hint_min_set(o, 80, 80);

   return o;
}

static void
_tooltip_del(void            *data,
             Evas_Object     *_obj EINA_UNUSED,
             void            *_event_info EINA_UNUSED)
{
   Gravatar *g = data;
   eina_stringshare_del(g->url);
   free(g);
}

void
gravatar_tooltip(Evas_Object *obj, Config *config, char *email)
{
   int n;
   MD5_CTX ctx;
   char md5out[(2 * MD5_HASHBYTES) + 1];
   unsigned char hash[MD5_HASHBYTES];
   static const char hex[] = "0123456789abcdef";
   const char *url;
   Gravatar *g;

   if (!config->gravatar)
     return;

   g = calloc(sizeof(Gravatar), 1);
   if (!g) return;
   g->config = config;

   eina_str_tolower(&email);

   MD5Init(&ctx);
   MD5Update(&ctx, (unsigned char const*)email, (unsigned)strlen(email));
   MD5Final(hash, &ctx);

   for (n = 0; n < MD5_HASHBYTES; n++)
     {
        md5out[2 * n] = hex[hash[n] >> 4];
        md5out[2 * n + 1] = hex[hash[n] & 0x0f];
     }
   md5out[2 * MD5_HASHBYTES] = '\0';

   url = eina_stringshare_printf(GRAVATAR_URL_START"%s"GRAVATAR_URL_END,
                                 md5out);

   g->url = url;
   elm_object_tooltip_content_cb_set(obj, _tooltip_content,
                                     g,
                                     _tooltip_del);
}

void
gravatar_init(void)
{
   if (_gravatar_log_dom >= 0) return;

   _gravatar_log_dom = eina_log_domain_register("gravatar", NULL);
   if (_gravatar_log_dom < 0)
     EINA_LOG_CRIT(_("Could not create logging domain '%s'."), "gravatar");
}

void
gravatar_shutdown(void)
{
   if (_gravatar_log_dom < 0) return;
   eina_log_domain_unregister(_gravatar_log_dom);
   _gravatar_log_dom = -1;
}