add API function to unset avatar

This commit is contained in:
Jfreegman 2014-09-26 14:32:49 -04:00
parent d264010364
commit 83842e6648
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
4 changed files with 49 additions and 19 deletions

View File

@ -574,33 +574,48 @@ int m_set_userstatus(Messenger *m, uint8_t status)
return 0;
}
int m_unset_avatar(Messenger *m)
{
if (m->avatar_data != NULL)
free(m->avatar_data);
m->avatar_data = NULL;
m->avatar_data_length = 0;
m->avatar_format = AVATAR_FORMAT_NONE;
memset(m->avatar_hash, 0, AVATAR_HASH_LENGTH);
uint32_t i;
for (i = 0; i < m->numfriends; ++i)
m->friendlist[i].avatar_info_sent = 0;
return 0;
}
int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t length)
{
if (length > AVATAR_MAX_DATA_LENGTH)
if (format == AVATAR_FORMAT_NONE) {
m_unset_avatar(m);
return 0;
}
if (length > AVATAR_MAX_DATA_LENGTH || length == 0)
return -1;
if (format == AVATAR_FORMAT_NONE) {
free(m->avatar_data);
m->avatar_data = NULL;
m->avatar_data_length = 0;
m->avatar_format = format;
memset(m->avatar_hash, 0, AVATAR_HASH_LENGTH);
} else {
if (length == 0 || data == NULL)
return -1;
if (data == NULL)
return -1;
uint8_t *tmp = realloc(m->avatar_data, length);
uint8_t *tmp = realloc(m->avatar_data, length);
if (tmp == NULL)
return -1;
if (tmp == NULL)
return -1;
m->avatar_format = format;
m->avatar_data = tmp;
m->avatar_data_length = length;
memcpy(m->avatar_data, data, length);
m->avatar_format = format;
m->avatar_data = tmp;
m->avatar_data_length = length;
memcpy(m->avatar_data, data, length);
m_avatar_hash(m->avatar_hash, m->avatar_data, m->avatar_data_length);
}
m_avatar_hash(m->avatar_hash, m->avatar_data, m->avatar_data_length);
uint32_t i;

View File

@ -518,6 +518,11 @@ uint8_t m_get_self_userstatus(const Messenger *m);
*/
int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t length);
/* Unsets the user avatar.
returns 0 on success (currently always returns 0) */
int m_unset_avatar(Messenger *m);
/* Get avatar data from the current user.
* Copies the current user avatar data to the destination buffer and sets the image format
* accordingly.

View File

@ -820,6 +820,12 @@ int tox_set_avatar(Tox *tox, uint8_t format, const uint8_t *data, uint32_t lengt
return m_set_avatar(m, format, data, length);
}
int tox_unset_avatar(Tox *tox)
{
Messenger *m = tox;
return m_unset_avatar(m);
}
int tox_get_self_avatar(const Tox *tox, uint8_t *format, uint8_t *buf, uint32_t *length, uint32_t maxlen, uint8_t *hash)
{
const Messenger *m = tox;

View File

@ -586,6 +586,10 @@ void tox_callback_avatar_data(Tox *tox, void (*function)(Tox *tox, int32_t, uint
*/
int tox_set_avatar(Tox *tox, uint8_t format, const uint8_t *data, uint32_t length);
/* Unsets the user avatar.
returns 0 on success (currently always returns 0) */
int tox_unset_avatar(Tox *tox);
/* Get avatar data from the current user.
* Copies the current user avatar data to the destination buffer and sets the image format