aboutsummaryrefslogtreecommitdiff
path: root/src/ui/members.c
blob: 7e3239f3a3b76084f12bc291cfc4bef88b235b0d (plain) (blame)
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
/*
   This file is part of GNUnet.
   Copyright (C) 2022 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */
/*
 * @author Tobias Frisch
 * @file ui/members.c
 */

#include "members.h"

#include "list_input.h"
#include "../application.h"
#include "../util.h"

void
members_event(UI_MEMBERS_Handle *members,
	      struct MESSENGER_Application *app,
	      int key)
{
  list_input_reset(members);

  UI_MEMBERS_List *element = members->head;
  while (element)
  {
    list_input_select(members, 1, element->contact);
    element = element->next;
  }

  switch (key)
  {
    case 27:
    case KEY_EXIT:
    case '\t':
      app->chat.show_members = FALSE;
      break;
    case '\n':
    case KEY_ENTER: {
      struct GNUNET_CHAT_Context *context;

      if (!(members->selected))
	break;

      context = GNUNET_CHAT_contact_get_context(members->selected);
      GNUNET_CHAT_context_request(context);

      app->chat.show_members = FALSE;
      app->chat.context = context;
      break;
    }
    default:
      break;
  }

  list_input_event(members, key);
}

static void
_members_iterate_print(UI_MEMBERS_Handle *members,
		       const struct GNUNET_CHAT_Contact *contact)
{
  list_input_print(members, 1);

  const int attrs_select = A_BOLD;

  if (selected) wattron(members->window, attrs_select);

  wmove(members->window, y, 0);

  const char *name = GNUNET_CHAT_contact_get_name(contact);
  const char *key = GNUNET_CHAT_contact_get_key(contact);

  size_t key_len = key? strlen(key) : 0;

  if (key_len > 4)
    wprintw(members->window, "[%s]: %s", key + (key_len - 4), name);
  else
    wprintw(members->window, "%s", name);

  if (selected) wattroff(members->window, attrs_select);
}

void
members_print(UI_MEMBERS_Handle *members)
{
  if (!(members->window))
    return;

  list_input_reset(members);
  werase(members->window);

  UI_MEMBERS_List *element = members->head;
  while (element)
  {
    _members_iterate_print(members, element->contact);
    element = element->next;
  }
}

void
members_clear(UI_MEMBERS_Handle *members)
{
  UI_MEMBERS_List *element;
  while (members->head)
  {
    element = members->head;

    GNUNET_CONTAINER_DLL_remove(
	members->head,
	members->tail,
	element
    );

    GNUNET_free(element);
  }

  members->line_selected = 0;
}

bool
members_add(UI_MEMBERS_Handle *members,
	    struct GNUNET_CHAT_Contact *contact)
{
  UI_MEMBERS_List *element = members->head;
  while (element)
  {
    if (element->contact == contact)
      break;

    element = element->next;
  }

  if (element)
    return FALSE;

  element = GNUNET_new(UI_MEMBERS_List);
  element->contact = contact;

  GNUNET_CONTAINER_DLL_insert_tail(
      members->head,
      members->tail,
      element
  );

  return TRUE;
}

void
members_remove(UI_MEMBERS_Handle *members,
	       const struct GNUNET_CHAT_Contact *contact)
{
  UI_MEMBERS_List *element = members->head;
  while (element)
  {
    if (element->contact == contact)
      break;

    element = element->next;
  }

  if (element)
    GNUNET_CONTAINER_DLL_remove(
	members->head,
	members->tail,
	element
    );
}