1 #ifndef AVATAR_H
2 #define AVATAR_H
3 
4 #include <tox/tox.h>
5 
6 typedef struct native_image NATIVE_IMAGE;
7 
8 // TODO: remove?
9 #define UTOX_AVATAR_MAX_DATA_LENGTH (64 * 1024) // NOTE: increasing this above 64k might cause
10                                                 // issues with other clients who do stupid things.
11 #define UTOX_AVATAR_FORMAT_NONE 0
12 #define UTOX_AVATAR_FORMAT_PNG 1
13 
14 /* data needed for each avatar in memory */
15 typedef struct avatar {
16     NATIVE_IMAGE *img; /* converted avatar image to draw */
17 
18     size_t   size;
19     uint16_t width, height;         /* width and height of image (in pixels) */
20     uint8_t  format;                /* one of TOX_AVATAR_FORMAT */
21     uint8_t  hash[TOX_HASH_LENGTH]; /* tox_hash for the png data of this avatar */
22 } AVATAR;
23 
24 /* Whether user's avatar is set. */
25 #define self_has_avatar() (self.avatar && self.avatar->format != UTOX_AVATAR_FORMAT_NONE)
26 /* Whether friend f's avatar is set, where f is a pointer to a friend struct */
27 #define friend_has_avatar(f) (f) && (f->avatar->format != UTOX_AVATAR_FORMAT_NONE)
28 
29 /** tries to load avatar from disk for given client id string and set avatar based on saved png data
30  * avatar is avatar to initialize. Will be unset if no file is found on disk or if file is corrupt or too large,
31  *     otherwise will be set to avatar found on disk
32  * id is cid string of whose avatar to find(see also avatar_load in avatar.c)
33  * if png_data_out is not NULL, the png data loaded from disk will be copied to it.
34  *     if it is not null, it should be at least UTOX_AVATAR_MAX_DATA_LENGTH bytes long
35  * if png_size_out is not null, the size of the png data will be stored in it
36  *
37  * returns: true on successful loading, false on failure
38  */
39 bool avatar_init(char hexid[TOX_PUBLIC_KEY_SIZE * 2], AVATAR *avatar);
40 
41 /** Converts png data given by data to a NATIVE_IMAGE and uses that to populate the avatar struct
42  * avatar is pointer to an avatar struct to store result in. Remains unchanged if function fails.
43  * data is pointer to png data to convert
44  * size is size of data
45  *
46  * on success: returns true
47  * on failure: returns false
48  *
49  * notes: fails if given size is larger than UTOX_AVATAR_MAX_DATA_LENGTH or data is not valid PNG data
50  */
51 bool avatar_set(AVATAR *avatar, const uint8_t *data, size_t size);
52 
53 /* Helper function to set the user's avatar. */
54 bool avatar_set_self(const uint8_t *data, size_t size);
55 
56 /* Helper function to unset the user's avatar. */
57 void avatar_unset_self(void);
58 
59 /* Helper function to delete user's avatar file. */
60 void avatar_delete_self(void);
61 
62 /* Unsets an avatar by setting its format to UTOX_AVATAR_FORMAT_NONE and freeing its image. */
63 void avatar_unset(AVATAR *avatar);
64 
65 /** Sets own avatar based on given png data and saves it to disk if successful.
66  * data is png data to set avatar to.
67  * size is size of data.
68  *
69  * on success: returns true
70  * on failure: returns false
71  *
72  * Notes: Fails if size is too large or data is not a valid png file.
73  */
74 bool self_set_and_save_avatar(const uint8_t *data, uint32_t size);
75 
76 /* Unsets own avatar and removes it from disk */
77 bool avatar_remove_self(void);
78 
79 /** Call this every time friend_number goes online from the tox_do thread.
80  *
81  *  on success: returns true
82  *  on failure: returns false
83  */
84 bool avatar_on_friend_online(Tox *tox, uint32_t friend_number);
85 
86 /** Colled by incoming file transfers to change the avater.
87  *
88  * If size <=0, we'll unset the avatar, else we'll set and update the friend
89  */
90 void utox_incoming_avatar(uint32_t friend_number, uint8_t *avatar, size_t size);
91 
92 /* Saves the avatar for user with hexid
93  *
94  * returns true on success
95  * returns false on failure
96  */
97 bool avatar_save(char hexid[TOX_PUBLIC_KEY_SIZE * 2], const uint8_t *data, size_t length);
98 
99 /* Deletes the avatar for user with hexid
100  *
101  * returns true on success
102  * returns false on failure
103  */
104 bool avatar_delete(char hexid[TOX_PUBLIC_KEY_SIZE * 2]);
105 
106 /* Helper function to initialize the user's avatar */
107 bool avatar_init_self(void);
108 
109 /* Moves the avatar to its new name */
110 bool avatar_move(const uint8_t *source, const uint8_t *dest);
111 
112 #endif
113