scm: Add avatar to commit window

But it's crashing and I can't figure why
This commit is contained in:
Andy Williams 2017-06-07 22:38:21 -07:00
parent 5d0c634209
commit f7d2a85044
7 changed files with 410 additions and 6 deletions

View File

@ -1,3 +1,7 @@
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "Edi.h"
#include "mainview/edi_mainview.h"
#include "edi_consolepanel.h"
@ -70,7 +74,7 @@ _edi_scm_screens_commit_cb(void *data,
void
edi_scm_screens_commit(Evas_Object *parent)
{
Evas_Object *popup, *box, *label, *input, *button;
Evas_Object *popup, *box, *hbox, *label, *avatar, *input, *button;
Eina_Strbuf *user;
Edi_Scm_Engine *engine;
@ -90,18 +94,33 @@ edi_scm_screens_commit(Evas_Object *parent)
elm_box_horizontal_set(box, EINA_FALSE);
elm_object_content_set(popup, box);
label = elm_label_add(box);
hbox = elm_box_add(popup);
elm_box_horizontal_set(hbox, EINA_TRUE);
evas_object_size_hint_weight_set(hbox, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(hbox, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(hbox);
elm_box_pack_end(box, hbox);
label = elm_label_add(hbox);
evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(label);
elm_box_pack_end(box, label);
elm_box_pack_end(hbox, label);
user = eina_strbuf_new();
eina_strbuf_append_printf(user, "Author <b>%s</b> &lt;%s&gt;<br>",
eina_strbuf_append_printf(user, "Author:<br><b>%s</b><br>&lt;%s&gt;",
engine->remote_name_get(), engine->remote_email_get());
elm_object_text_set(label, eina_strbuf_string_get(user));
eina_strbuf_free(user);
avatar = elm_image_add(hbox);
edi_scm_screens_avatar_load(avatar, engine->remote_email_get());
evas_object_size_hint_min_set(avatar, 48 * elm_config_scale_get(), 48 * elm_config_scale_get());
evas_object_size_hint_weight_set(avatar, 0.1, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(avatar, 1.0, EVAS_HINT_FILL);
evas_object_show(avatar);
elm_box_pack_end(hbox, avatar);
input = elm_entry_add(box);
elm_entry_editable_set(input, EINA_TRUE);
elm_object_text_set(input, "Enter commit summary<br><br>And change details");
@ -149,3 +168,55 @@ edi_scm_screens_binary_missing(Evas_Object *parent, const char *binary)
evas_object_show(popup);
}
const char *
_edi_scm_avatar_cache_path_get(const char *email)
{
return eina_stringshare_printf("%s/%s/avatars/%s.png", efreet_cache_home_get(),
PACKAGE_NAME, email);
}
void _edi_scm_screens_avatar_download_complete(void *data, const char *file,
EINA_UNUSED int status)
{
Evas_Object *image = data;
// TODO figure why this crashes
//elm_image_file_set(image, file, NULL);
}
void edi_scm_screens_avatar_load(Evas_Object *image, const char *email)
{
const char *tmp, *cache, *cachedir, *cacheparentdir;
Ecore_File_Download_Job *job;
cache = _edi_scm_avatar_cache_path_get(email);
if (ecore_file_exists(cache))
{
elm_image_file_set(image, cache, NULL);
return;
}
tmp = dirname((char *) cache);
cachedir = strdup(tmp);
cacheparentdir = dirname((char *) tmp);
if (!ecore_file_exists(cacheparentdir) && !ecore_file_mkdir(cacheparentdir))
{
free((char *)tmp);
free((char *)cachedir);
return;
}
free((char *)tmp);
if (!ecore_file_exists(cachedir) && !ecore_file_mkdir(cachedir))
{
free((char *)cachedir);
return;
}
free((char *)cachedir);
ecore_file_download(edi_scm_avatar_url_get(email), cache,
_edi_scm_screens_avatar_download_complete, NULL,
image, &job);
}

View File

@ -14,7 +14,7 @@ extern "C" {
/**
* @brief Scm management functions.
* @defgroup Scm
* @defgroup Scm
*
* @{
*
@ -40,6 +40,13 @@ void edi_scm_screens_commit(Evas_Object *parent);
*/
void edi_scm_screens_binary_missing(Evas_Object *parent, const char *binary);
/**
* Load an avatar image to a given elm_image object.
*
* @ingroup Scm
*/
void edi_scm_screens_avatar_load(Evas_Object *image, const char *email);
/**
* @}
*/

View File

@ -30,6 +30,8 @@ edi_build_provider.c \
edi_builder.c \
edi_create.c \
edi_path.c \
md5.h \
md5.c \
edi_scm.c \
edi_exe.c \
edi.c

View File

@ -5,12 +5,14 @@
#include <Eina.h>
#include <Ecore.h>
#include <Ecore_File.h>
#include <Ethumb.h>
#include "Edi.h"
#include "edi_private.h"
#include <edi_exe.h>
#include "edi_exe.h"
#include "edi_path.h"
#include "edi_scm.h"
#include "md5.h"
Edi_Scm_Engine *_edi_scm_global_object = NULL;
@ -496,3 +498,37 @@ edi_scm_init(void)
return NULL;
}
EAPI const char *
edi_scm_avatar_url_get(const char *email)
{
int n;
char *id;
const char *url;
MD5_CTX ctx;
char md5out[(2 * MD5_HASHBYTES) + 1];
unsigned char hash[MD5_HASHBYTES];
static const char hex[] = "0123456789abcdef";
if (!email || strlen(email) == 0)
return NULL;
id = strdup(email);
eina_str_tolower(&id);
MD5Init(&ctx);
MD5Update(&ctx, (unsigned char const*)id, (unsigned)strlen(id));
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("http://www.gravatar.com/avatar/%s", md5out);
free(id);
return url;
}

View File

@ -198,6 +198,15 @@ Eina_Bool edi_scm_enabled(void);
*/
Eina_Bool edi_scm_remote_enabled(void);
/**
* Get the URL to an avatar image for the user (based on email).
*
* @return An allocated string containing the URL or NULL if no email
*
* @ingroup Scm
*/
const char *edi_scm_avatar_url_get(const char *email);
/**
* @}
*/

251
src/lib/md5.c Normal file
View File

@ -0,0 +1,251 @@
/*
* This code implements the MD5 message-digest algorithm.
* The algorithm is due to Ron Rivest. This code was
* written by Colin Plumb in 1993, no copyright is claimed.
* This code is in the public domain; do with it what you wish.
*
* Equivalent code is available from RSA Data Security, Inc.
* This code has been tested against that, and is equivalent,
* except that you don't need to include two pages of legalese
* with every copy.
*
* To compute the message digest of a chunk of bytes, declare an
* MD5Context structure, pass it to MD5Init, call MD5Update as
* needed on buffers full of bytes, and then call MD5Final, which
* will fill a supplied 16-byte array with the digest.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h> /* for memcpy() */
#include "md5.h"
#if (__BYTE_ORDER == 1234)
#define byteReverse(buf, len) /* Nothing */
#else
void byteReverse(unsigned char *buf, unsigned longs);
/*
* Note: this code is harmless on little-endian machines.
*/
void byteReverse(unsigned char *buf, unsigned longs)
{
uint32_t t;
do {
t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(uint32_t *) buf = t;
buf += 4;
} while (--longs);
}
#endif
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
void MD5Init(MD5_CTX *ctx)
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[3] = 0x10325476;
ctx->bits[0] = 0;
ctx->bits[1] = 0;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
void MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len)
{
uint32_t t;
/* Update bitcount */
t = ctx->bits[0];
if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
ctx->bits[1]++; /* Carry from low to high */
ctx->bits[1] += len >> 29;
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
/* Handle any leading odd-sized chunks */
if (t) {
unsigned char *p = ctx->in.s + t;
t = 64 - t;
if (len < t) {
memcpy(p, buf, len);
return;
}
memcpy(p, buf, t);
byteReverse(ctx->in.s, 16);
MD5Transform(ctx->buf, ctx->in.i);
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
while (len >= 64) {
memcpy(ctx->in.s, buf, 64);
byteReverse(ctx->in.s, 16);
MD5Transform(ctx->buf, ctx->in.i);
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
memcpy(ctx->in.s, buf, len);
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
{
unsigned count;
unsigned char *p;
/* Compute number of bytes mod 64 */
count = (ctx->bits[0] >> 3) & 0x3F;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
p = ctx->in.s + count;
*p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
/* Pad out to 56 mod 64 */
if (count < 8) {
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
byteReverse(ctx->in.s, 16);
MD5Transform(ctx->buf, ctx->in.i);
/* Now fill the next block with 56 bytes */
memset(ctx->in.s, 0, 56);
} else {
/* Pad block to 56 bytes */
memset(p, 0, count - 8);
}
byteReverse(ctx->in.s, 14);
/* Append length in bits and transform */
ctx->in.i[14] = ctx->bits[0];
ctx->in.i[15] = ctx->bits[1];
MD5Transform(ctx->buf, ctx->in.i);
byteReverse((unsigned char *) ctx->buf, 4);
memcpy(digest, ctx->buf, 16);
memset(ctx, 0, sizeof(MD5_CTX)); /* In case it's sensitive */
}
/* The four core functions - F1 is optimized somewhat */
/* #define F1(x, y, z) (x & y | ~x & z) */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))
/* This is the central step in the MD5 algorithm. */
#define MD5STEP(f, w, x, y, z, data, s) \
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
void MD5Transform(uint32_t buf[4], uint32_t const in[16])
{
register uint32_t a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}

28
src/lib/md5.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef _MD5_H_
#define _MD5_H_
#include <stdint.h>
#include <sys/types.h>
#define MD5_HASHBYTES 16
typedef struct MD5Context {
uint32_t buf[4];
uint32_t bits[2];
union
{
unsigned char s[64];
uint32_t i[16];
} in;
} MD5_CTX;
extern void MD5Init(MD5_CTX *context);
extern void MD5Update(MD5_CTX *context,unsigned char const *buf,unsigned len);
extern void MD5Final(unsigned char digest[MD5_HASHBYTES], MD5_CTX *context);
extern void MD5Transform(uint32_t buf[4], uint32_t const in[16]);
extern char *MD5End(MD5_CTX *, char *);
extern char *MD5File(const char *, char *);
extern char *MD5Data (const unsigned char *, unsigned int, char *);
#endif