about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-05-22 21:43:06 +0100
committerJames Booth <boothj5@gmail.com>2013-05-22 21:43:06 +0100
commit2842b423d17a76d8a8c5be04bf9739c0caf66dd7 (patch)
tree0b5e770805185ab4ba421b334529e6504b86cae2 /src
parent15c782059435bbea671938124d6ff43dc0186b7e (diff)
downloadprofani-tty-2842b423d17a76d8a8c5be04bf9739c0caf66dd7.tar.gz
Refactor roster.c
Diffstat (limited to 'src')
-rw-r--r--src/xmpp/roster.c135
1 files changed, 63 insertions, 72 deletions
diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c
index 9d2b765f..4751bbd9 100644
--- a/src/xmpp/roster.c
+++ b/src/xmpp/roster.c
@@ -33,12 +33,8 @@
 #include "xmpp/stanza.h"
 #include "xmpp/xmpp.h"
 
-#define HANDLE(type, func) xmpp_handler_add(conn, func, XMPP_NS_ROSTER, STANZA_NAME_IQ, type, ctx)
-
-static int _roster_handle_push(xmpp_conn_t * const conn,
-    xmpp_stanza_t * const stanza, void * const userdata);
-static int _roster_handle_result(xmpp_conn_t * const conn,
-    xmpp_stanza_t * const stanza, void * const userdata);
+#define HANDLE(type, func) xmpp_handler_add(conn, func, XMPP_NS_ROSTER, \
+STANZA_NAME_IQ, type, ctx)
 
 // nicknames
 static Autocomplete name_ac;
@@ -55,7 +51,17 @@ static GHashTable *contacts;
 // nickname to jid map
 static GHashTable *name_to_barejid;
 
+// event handlers
+static int _roster_handle_push(xmpp_conn_t * const conn,
+    xmpp_stanza_t * const stanza, void * const userdata);
+static int _roster_handle_result(xmpp_conn_t * const conn,
+    xmpp_stanza_t * const stanza, void * const userdata);
+
 // helper functions
+static void _add_name_and_barejid(const char * const name,
+    const char * const barejid);
+static void _replace_name(const char * const current_name,
+    const char * const new_name, const char * const barejid);
 static gboolean _key_equals(void *key1, void *key2);
 static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2);
 
@@ -86,7 +92,8 @@ roster_init(void)
     fulljid_ac = autocomplete_new();
     contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
         (GDestroyNotify)p_contact_free);
-    name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+    name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+        g_free);
 }
 
 void
@@ -99,7 +106,8 @@ roster_clear(void)
     contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
         (GDestroyNotify)p_contact_free);
     g_hash_table_destroy(name_to_barejid);
-    name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+    name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+        g_free);
 }
 
 void
@@ -128,15 +136,8 @@ roster_add(const char * const barejid, const char * const name,
     if (contact == NULL) {
         contact = p_contact_new(barejid, name, subscription, NULL, pending_out);
         g_hash_table_insert(contacts, strdup(barejid), contact);
-        if (name != NULL) {
-            autocomplete_add(name_ac, strdup(name));
-            g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid));
-        } else {
-            autocomplete_add(name_ac, strdup(barejid));
-            g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid));
-        }
         autocomplete_add(barejid_ac, strdup(barejid));
-
+        _add_name_and_barejid(name, barejid);
         added = TRUE;
     }
 
@@ -155,36 +156,14 @@ roster_update(const char * const barejid, const char * const name,
         p_contact_set_subscription(contact, subscription);
         p_contact_set_pending_out(contact, pending_out);
 
-        const char * const new_handle = name;
-        const char * current_handle = NULL;
+        const char * const new_name = name;
+        const char * current_name = NULL;
         if (p_contact_name(contact) != NULL) {
-            current_handle = strdup(p_contact_name(contact));
+            current_name = strdup(p_contact_name(contact));
         }
 
-        p_contact_set_name(contact, new_handle);
-
-        // current handle exists already
-        if (current_handle != NULL) {
-            autocomplete_remove(name_ac, current_handle);
-            g_hash_table_remove(name_to_barejid, current_handle);
-
-            if (new_handle != NULL) {
-                autocomplete_add(name_ac, strdup(new_handle));
-                g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid));
-            } else {
-                autocomplete_add(name_ac, strdup(barejid));
-                g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid));
-            }
-        // no current handle
-        } else {
-            if (new_handle != NULL) {
-                autocomplete_remove(name_ac, barejid);
-                g_hash_table_remove(name_to_barejid, barejid);
-
-                autocomplete_add(name_ac, strdup(new_handle));
-                g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid));
-            }
-        }
+        p_contact_set_name(contact, new_name);
+        _replace_name(current_name, new_name, barejid);
     }
 }
 
@@ -199,7 +178,6 @@ roster_update_presence(const char * const barejid, Resource *resource,
     if (contact == NULL) {
         return FALSE;
     }
-
     if (!_datetimes_equal(p_contact_last_activity(contact), last_activity)) {
         p_contact_set_last_activity(contact, last_activity);
     }
@@ -216,6 +194,7 @@ roster_contact_offline(const char * const barejid,
     const char * const resource, const char * const status)
 {
     PContact contact = g_hash_table_lookup(contacts, barejid);
+
     if (contact == NULL) {
         return FALSE;
     }
@@ -244,29 +223,7 @@ roster_change_name(const char * const barejid, const char * const new_name)
 
     if (contact != NULL) {
         p_contact_set_name(contact, new_name);
-
-        // current name exists already
-        if (current_name != NULL) {
-            autocomplete_remove(name_ac, current_name);
-            g_hash_table_remove(name_to_barejid, current_name);
-
-            if (new_name != NULL) {
-                autocomplete_add(name_ac, strdup(new_name));
-                g_hash_table_insert(name_to_barejid, strdup(new_name), strdup(barejid));
-            } else {
-                autocomplete_add(name_ac, strdup(barejid));
-                g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid));
-            }
-        // no current name
-        } else {
-            if (new_name != NULL) {
-                autocomplete_remove(name_ac, barejid);
-                g_hash_table_remove(name_to_barejid, barejid);
-
-                autocomplete_add(name_ac, strdup(new_name));
-                g_hash_table_insert(name_to_barejid, strdup(new_name), strdup(barejid));
-            }
-        }
+        _replace_name(current_name, new_name, barejid);
 
         xmpp_conn_t * const conn = connection_get_conn();
         xmpp_ctx_t * const ctx = connection_get_ctx();
@@ -414,13 +371,17 @@ _roster_handle_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
 
     // handle initial roster response
     if (g_strcmp0(id, "roster") == 0) {
-        xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
+        xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza,
+            STANZA_NAME_QUERY);
         xmpp_stanza_t *item = xmpp_stanza_get_children(query);
 
         while (item != NULL) {
-            const char *barejid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID);
-            const char *name = xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME);
-            const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION);
+            const char *barejid =
+                xmpp_stanza_get_attribute(item, STANZA_ATTR_JID);
+            const char *name =
+                xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME);
+            const char *sub =
+                xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION);
 
             gboolean pending_out = FALSE;
             const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK);
@@ -437,13 +398,43 @@ _roster_handle_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
             item = xmpp_stanza_get_next(item);
         }
 
-        contact_presence_t conn_presence = accounts_get_login_presence(jabber_get_account_name());
+        contact_presence_t conn_presence =
+            accounts_get_login_presence(jabber_get_account_name());
         presence_update(conn_presence, NULL, 0);
     }
 
     return 1;
 }
 
+static void
+_add_name_and_barejid(const char * const name, const char * const barejid)
+{
+    if (name != NULL) {
+        autocomplete_add(name_ac, strdup(name));
+        g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid));
+    } else {
+        autocomplete_add(name_ac, strdup(barejid));
+        g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid));
+    }
+}
+
+static void
+_replace_name(const char * const current_name, const char * const new_name,
+    const char * const barejid)
+{
+    // current handle exists already
+    if (current_name != NULL) {
+        autocomplete_remove(name_ac, current_name);
+        g_hash_table_remove(name_to_barejid, current_name);
+        _add_name_and_barejid(new_name, barejid);
+    // no current handle
+    } else if (new_name != NULL) {
+        autocomplete_remove(name_ac, barejid);
+        g_hash_table_remove(name_to_barejid, barejid);
+        _add_name_and_barejid(new_name, barejid);
+    }
+}
+
 static
 gboolean _key_equals(void *key1, void *key2)
 {
amp;id=8f34dfd1e09e9e33176bba8f05a2ec229cf3b56f'>8f34dfd1 ^
cec5ef31 ^
966daf14 ^







1d7aae96 ^





bfcf5c72 ^
1d7aae96 ^


















































cec5ef31 ^
fdc3df04 ^
1d7aae96 ^






















cec5ef31 ^
1d7aae96 ^







a99775b4 ^








bfcf5c72 ^
a99775b4 ^

























cec5ef31 ^
a99775b4 ^














cec5ef31 ^
a99775b4 ^






a51bc7a1 ^


f5ece045 ^
08c55cb2 ^
1d7aae96 ^


a99775b4 ^
a51bc7a1 ^
966daf14 ^
a51bc7a1 ^


f5ece045 ^
1d7aae96 ^


a99775b4 ^
a51bc7a1 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327