terminology/src/bin/gravatar.c

132 lines
2.9 KiB
C
Raw Normal View History

#include "private.h"
#include <Elementary.h>
#include "gravatar.h"
2014-10-06 14:15:44 -07:00
#include "config.h"
#include "termio.h"
#include "media.h"
2020-05-24 02:21:52 -07:00
#include "md5.h"
2020-06-23 03:48:27 -07:00
#include "theme.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__)
2017-09-20 11:10:52 -07:00
#define GRAVATAR_URL_START "https://www.gravatar.com/avatar/"
2014-10-06 14:15:44 -07:00
#define GRAVATAR_URL_END ""
2014-10-08 15:12:34 -07:00
typedef struct _Gravatar {
const char *url;
2017-07-06 11:55:02 -07:00
const Config *config;
2014-10-08 15:12:34 -07:00
} Gravatar;
2014-10-06 14:15:44 -07:00
static Evas_Object *
_tooltip_content(void *data,
Evas_Object *obj,
Evas_Object *_tt EINA_UNUSED)
2014-10-06 14:15:44 -07:00
{
2014-10-08 15:12:34 -07:00
Gravatar *g = data;
2014-10-06 14:15:44 -07:00
Evas_Object *o;
2014-10-09 12:45:01 -07:00
o = media_add(obj, g->url, g->config, MEDIA_STRETCH, MEDIA_TYPE_IMG);
evas_object_size_hint_min_set(o, 80, 80);
2014-10-06 14:15:44 -07:00
return o;
}
static void
_tooltip_del(void *data,
Evas_Object *_obj EINA_UNUSED,
void *_event_info EINA_UNUSED)
2014-10-06 14:15:44 -07:00
{
2014-10-08 15:12:34 -07:00
Gravatar *g = data;
eina_stringshare_del(g->url);
free(g);
2014-10-06 14:15:44 -07:00
}
void
2018-10-13 13:05:48 -07:00
gravatar_tooltip(Evas_Object *obj, const Config *config, const char *email)
{
2014-10-06 14:15:44 -07:00
int n;
MD5_CTX ctx;
char md5out[(2 * MD5_HASHBYTES) + 1];
unsigned char hash[MD5_HASHBYTES];
static const char hex[] = "0123456789abcdef";
const char *url;
2014-10-08 15:12:34 -07:00
Gravatar *g;
2018-10-13 13:05:48 -07:00
size_t len;
char *str;
2014-10-08 15:12:34 -07:00
2014-10-14 13:57:22 -07:00
if (!config->gravatar)
return;
2014-10-08 15:12:34 -07:00
g = calloc(sizeof(Gravatar), 1);
2018-10-13 13:05:48 -07:00
if (!g)
return;
2014-10-08 15:12:34 -07:00
g->config = config;
2014-10-06 14:15:44 -07:00
if (casestartswith(email, "mailto:"))
email += strlen("mailto:");
2018-10-13 13:05:48 -07:00
len = strlen(email);
str = strndup(email, len);
if (!str)
{
free(g);
return;
}
eina_str_tolower(&str);
2014-10-06 14:15:44 -07:00
MD5Init(&ctx);
2018-10-13 13:05:48 -07:00
MD5Update(&ctx, (unsigned char const*)str, (unsigned)len);
2014-10-06 14:15:44 -07:00
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);
2014-10-08 15:12:34 -07:00
g->url = url;
2014-10-06 14:15:44 -07:00
elm_object_tooltip_content_cb_set(obj, _tooltip_content,
2014-10-08 15:12:34 -07:00
g,
2014-10-06 14:15:44 -07:00
_tooltip_del);
2018-10-13 13:05:48 -07:00
free(str);
}
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;
}