aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_contact.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet_chat_contact.c')
-rw-r--r--src/gnunet_chat_contact.c124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/gnunet_chat_contact.c b/src/gnunet_chat_contact.c
new file mode 100644
index 0000000..1fec6af
--- /dev/null
+++ b/src/gnunet_chat_contact.c
@@ -0,0 +1,124 @@
1/*
2 * @author Tobias Frisch
3 * @file gnunet_chat_contact.c
4 */
5
6#include "gnunet_chat_lib.h"
7#include "gnunet_chat_contact.h"
8#include "gnunet_chat_handle.h"
9
10struct GNUNET_CHAT_Contact*
11contact_create (struct GNUNET_CHAT_Handle *handle)
12{
13 struct GNUNET_CHAT_Contact* contact = GNUNET_new(struct GNUNET_CHAT_Contact);
14
15 contact->handle = handle;
16 contact->context = context_create(handle, NULL); // TODO: check for existing context?
17
18 contact->contact = NULL;
19 contact->nick = NULL;
20
21 if (!contact->context)
22 {
23 contact_destroy (contact);
24 return NULL;
25 }
26
27 const struct GNUNET_HashCode *key = context_get_key(contact->context);
28
29 const int result = GNUNET_CONTAINER_multihashmap_put(
30 handle->contacts,
31 key,
32 contact,
33 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
34 );
35
36 if (GNUNET_OK != result) {
37 contact_destroy (contact);
38 return NULL;
39 }
40
41 return contact;
42}
43
44void
45contact_destroy (struct GNUNET_CHAT_Contact *contact)
46{
47 if (!contact->context)
48 goto skip_context;
49
50 struct GNUNET_CHAT_Handle *handle = contact->handle;
51 const struct GNUNET_HashCode *key = context_get_key(contact->context);
52
53 GNUNET_CONTAINER_multihashmap_remove(
54 handle->contacts,
55 key,
56 contact
57 );
58
59 context_destroy(contact->context);
60
61skip_context:
62 if (contact->nick)
63 GNUNET_free(contact->nick);
64}
65
66int
67GNUNET_CHAT_contact_delete (struct GNUNET_CHAT_Contact *contact)
68{
69 if (!contact)
70 return GNUNET_SYSERR;
71
72 if (contact->context)
73 GNUNET_MESSENGER_close_room(contact->context->room);
74
75 contact_destroy(contact);
76 return GNUNET_OK;
77}
78
79void
80GNUNET_CHAT_contact_set_blocking (struct GNUNET_CHAT_Contact *contact,
81 int blocking)
82{
83 //TODO
84}
85
86int
87GNUNET_CHAT_contact_is_blocking (const struct GNUNET_CHAT_Contact *contact)
88{
89 return GNUNET_NO;
90}
91
92void
93GNUNET_CHAT_contact_set_name (struct GNUNET_CHAT_Contact *contact,
94 const char *name)
95{
96 if (!contact)
97 return;
98
99 if (contact->nick)
100 GNUNET_free(contact->nick);
101
102 contact->nick = name? GNUNET_strdup(name) : NULL;
103}
104
105const char*
106GNUNET_CHAT_contact_get_name (const struct GNUNET_CHAT_Contact *contact)
107{
108 if (!contact)
109 return NULL;
110
111 if (contact->nick)
112 return contact->nick;
113
114 return GNUNET_MESSENGER_contact_get_name(contact->contact);
115}
116
117struct GNUNET_CHAT_Context*
118GNUNET_CHAT_contact_get_context (struct GNUNET_CHAT_Contact *contact)
119{
120 if (!contact)
121 return NULL;
122
123 return contact->context;
124}