diff options
Diffstat (limited to 'testing/toxic/friendlist.c')
-rw-r--r-- | testing/toxic/friendlist.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c new file mode 100644 index 00000000..f8b8c840 --- /dev/null +++ b/testing/toxic/friendlist.c | |||
@@ -0,0 +1,125 @@ | |||
1 | /* | ||
2 | * Toxic -- Tox Curses Client | ||
3 | */ | ||
4 | |||
5 | #include <curses.h> | ||
6 | #include <string.h> | ||
7 | #include <stdint.h> | ||
8 | #include <ctype.h> | ||
9 | |||
10 | #include "../../core/Messenger.h" | ||
11 | #include "../../core/network.h" | ||
12 | |||
13 | #include "windows.h" | ||
14 | |||
15 | #define MAX_FRIENDS_NUM 100 | ||
16 | |||
17 | typedef struct { | ||
18 | uint8_t name[MAX_NAME_LENGTH]; | ||
19 | uint8_t status[MAX_USERSTATUS_LENGTH]; | ||
20 | int num; | ||
21 | |||
22 | } friend_t; | ||
23 | |||
24 | static friend_t friends[MAX_FRIENDS_NUM]; | ||
25 | static int num_friends = 0; | ||
26 | |||
27 | |||
28 | void fix_name(uint8_t* name) { | ||
29 | |||
30 | // Remove all non alphanumeric characters. | ||
31 | uint8_t* p = name; | ||
32 | uint8_t* q = name; | ||
33 | |||
34 | while(*p != 0) { | ||
35 | if(isalnum(*p)) { | ||
36 | *q++ = *p; | ||
37 | } | ||
38 | |||
39 | p++; | ||
40 | } | ||
41 | |||
42 | *q = 0; | ||
43 | } | ||
44 | |||
45 | int friendlist_nickchange(int num, uint8_t* str, uint16_t len) { | ||
46 | |||
47 | if(len >= MAX_NAME_LENGTH || num >= num_friends) | ||
48 | return -1; | ||
49 | |||
50 | memcpy((char*) &friends[num].name, (char*) str, len); | ||
51 | friends[num].name[len] = 0; | ||
52 | fix_name(friends[num].name); | ||
53 | |||
54 | return 0; | ||
55 | } | ||
56 | |||
57 | int friendlist_statuschange(int num, uint8_t* str, uint16_t len) { | ||
58 | |||
59 | if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends) | ||
60 | return -1; | ||
61 | |||
62 | memcpy((char*) &friends[num].status, (char*) str, len); | ||
63 | friends[num].status[len] = 0; | ||
64 | fix_name(friends[num].status); | ||
65 | |||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | int friendlist_addfriend(int num) { | ||
70 | |||
71 | if(num_friends == MAX_FRIENDS_NUM) | ||
72 | return -1; | ||
73 | |||
74 | friends[num_friends].num = num; | ||
75 | getname(num, friends[num_friends].name); | ||
76 | strcpy((char*) friends[num_friends].name, "unknown"); | ||
77 | strcpy((char*) friends[num_friends].status, "unknown"); | ||
78 | |||
79 | num_friends++; | ||
80 | return 0; | ||
81 | } | ||
82 | |||
83 | static void friendlist_onKey(ToxWindow* self, int key) { | ||
84 | |||
85 | } | ||
86 | |||
87 | static void friendlist_onDraw(ToxWindow* self) { | ||
88 | size_t i; | ||
89 | |||
90 | wclear(self->window); | ||
91 | |||
92 | if(num_friends == 0) { | ||
93 | wprintw(self->window, "Empty. Add some friends! :-)\n"); | ||
94 | } | ||
95 | |||
96 | wprintw(self->window, "\n"); | ||
97 | |||
98 | for(i=0; i<num_friends; i++) { | ||
99 | wprintw(self->window, "[%d] ", friends[i].num); | ||
100 | |||
101 | attron(A_BOLD); | ||
102 | wprintw(self->window, "%s ", friends[i].name); | ||
103 | attroff(A_BOLD); | ||
104 | |||
105 | wprintw(self->window, "(%s)\n", friends[i].status); | ||
106 | } | ||
107 | |||
108 | wrefresh(self->window); | ||
109 | } | ||
110 | |||
111 | static void friendlist_onInit(ToxWindow* self) { | ||
112 | |||
113 | } | ||
114 | |||
115 | |||
116 | ToxWindow new_friendlist() { | ||
117 | ToxWindow ret; | ||
118 | |||
119 | ret.onKey = &friendlist_onKey; | ||
120 | ret.onDraw = &friendlist_onDraw; | ||
121 | ret.onInit = &friendlist_onInit; | ||
122 | strcpy(ret.title, "[friends]"); | ||
123 | |||
124 | return ret; | ||
125 | } | ||