From 83842e66487646f41788b2c311431ee1be2d4fe2 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 26 Sep 2014 14:32:49 -0400 Subject: add API function to unset avatar --- toxcore/Messenger.c | 53 ++++++++++++++++++++++++++++++++++------------------- toxcore/Messenger.h | 5 +++++ toxcore/tox.c | 6 ++++++ toxcore/tox.h | 4 ++++ 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 99b95f67..3fc2cc63 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -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; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index e6877002..4a806e8c 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -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. diff --git a/toxcore/tox.c b/toxcore/tox.c index e8ec593b..a0873a4c 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -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; diff --git a/toxcore/tox.h b/toxcore/tox.h index 61cfdf70..af6f282c 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -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 -- cgit v1.2.3