aboutsummaryrefslogtreecommitdiff
path: root/src/ui/members.c
blob: 710c0d6fa5b78595789496f947eb16cf15a37d46 (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
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
/*
   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 "../application.h"
#include "../util.h"

void
members_event(UI_MEMBERS_Handle *members,
	      struct MESSENGER_Application *app,
	      int key)
{
  members->line_index = 0;
  members->selected = NULL;

  int count = 0;

  UI_MEMBERS_List *element = members->head;
  while (element)
  {
    count++;

    element = element->next;
  }

  switch (key)
  {
    case 27:
    case KEY_EXIT:
    case '\t':
    {
      app->chat.show_members = FALSE;
      break;
    }
    case KEY_UP:
    {
      members->line_selected--;
      break;
    }
    case KEY_DOWN:
    {
      members->line_selected++;
      break;
    }
    case '\n':
    case KEY_ENTER:
    {
      if (members->selected)
      {
	// TODO
      }

      break;
    }
    default:
      break;
  }

  if (members->line_selected < 0)
    members->line_selected = 0;
  else if (members->line_selected >= count)
    members->line_selected = count - 1;

  if (!(members->window))
    return;

  const int height = getmaxy(members->window) - getbegy(members->window);
  const int y = members->line_selected - members->line_offset;

  if (y < 0)
    members->line_offset += y;
  else if (y + 1 >= height)
    members->line_offset += y + 1 - height;

  if (members->line_offset < 0)
    members->line_offset = 0;
  else if (members->line_offset >= count)
    members->line_offset = count - 1;
}

static void
_members_iterate_print(UI_MEMBERS_Handle *members,
		       const struct GNUNET_CHAT_Contact *contact)
{
  const bool selected = (members->line_selected == members->line_index);
  const int y = members->line_index - members->line_offset;

  members->line_index++;

  if (y < 0)
    return;

  const int height = getmaxy(members->window) - getbegy(members->window);

  if (y >= height)
    return;

  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;

  members->line_index = 0;
  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,
	    const 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
    );
}