1 #ifndef _PURPLE_SLACK_OBJECT_H
2 #define _PURPLE_SLACK_OBJECT_H
3
4 #include <string.h>
5
6 #include <blist.h>
7 #include <glib-object.h>
8 #include "glibcompat.h"
9
10 /* object IDs seem to always be of the form "TXXXXXXXX" where T is a type identifier and X are [0-9A-Z] (base32?) */
11 #define SLACK_OBJECT_ID_SIZ 12
12 /* These may be safely treated as strings (always NULL terminated and padded),
13 * but strings are not valid slack_object_ids */
14 typedef char slack_object_id[SLACK_OBJECT_ID_SIZ];
15
16 #define slack_object_id_copy(dst, src) \
17 memcpy(dst, src, SLACK_OBJECT_ID_SIZ)
18 #define slack_object_id_cmp(a, b) \
19 memcmp(a, b, SLACK_OBJECT_ID_SIZ-1)
20 #define slack_object_id_clear(id) \
21 memset(id, 0, SLACK_OBJECT_ID_SIZ)
22
slack_object_id_set(slack_object_id id,const char * s)23 static inline void slack_object_id_set(slack_object_id id, const char *s) {
24 if (s) {
25 strncpy((char *)id, s, SLACK_OBJECT_ID_SIZ-1);
26 id[SLACK_OBJECT_ID_SIZ-1] = 0;
27 g_warn_if_fail(id[SLACK_OBJECT_ID_SIZ-2] == 0 || s[SLACK_OBJECT_ID_SIZ-1] == 0);
28 } else
29 slack_object_id_clear(id);
30 }
31
slack_object_id_is(const slack_object_id id,const char * s)32 static inline gboolean slack_object_id_is(const slack_object_id id, const char *s) {
33 return s ? !strncmp(id, s, SLACK_OBJECT_ID_SIZ-1) : !*id;
34 }
35
36 guint slack_object_id_hash(gconstpointer id);
37 gboolean slack_object_id_equal(gconstpointer a, gconstpointer b);
38
39 struct _SlackObject {
40 GObject parent;
41
42 slack_object_id id;
43
44 char *name;
45 PurpleBlistNode *buddy;
46
47 char *last_mesg, *last_read, *last_mark, *last_sent; /* ts marking */
48 struct _SlackObject *mark_next; /* on mark_list if non-null */
49 };
50
51 #define SLACK_TYPE_OBJECT slack_object_get_type()
G_DECLARE_FINAL_TYPE(SlackObject,slack_object,SLACK,OBJECT,GObject)52 G_DECLARE_FINAL_TYPE(SlackObject, slack_object, SLACK, OBJECT, GObject)
53
54 #define slack_object_hash_table_new() \
55 g_hash_table_new_full(slack_object_id_hash, slack_object_id_equal, NULL, g_object_unref)
56
57 static inline void slack_object_hash_table_replace(GHashTable *hash_table, SlackObject *obj) {
58 g_hash_table_replace(hash_table, obj->id, obj);
59 }
60
slack_object_hash_table_lookup(GHashTable * hash_table,const char * sid)61 static inline SlackObject *slack_object_hash_table_lookup(GHashTable *hash_table, const char *sid) {
62 if (!sid)
63 return NULL;
64 slack_object_id id;
65 slack_object_id_set(id, sid);
66 return g_hash_table_lookup(hash_table, id);
67 }
68
slack_object_hash_table_remove(GHashTable * hash_table,const char * sid)69 static inline gboolean slack_object_hash_table_remove(GHashTable *hash_table, const char *sid) {
70 slack_object_id id;
71 slack_object_id_set(id, sid);
72 return g_hash_table_remove(hash_table, id);
73 }
74
75 #endif
76