messenger-cli

Command-line user interface for GNUnet Messenger
Log | Files | Refs | README | LICENSE

members.c (3871B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2022--2025 GNUnet e.V.
      4 
      5    GNUnet is free software: you can redistribute it and/or modify it
      6    under the terms of the GNU Affero General Public License as published
      7    by the Free Software Foundation, either version 3 of the License,
      8    or (at your option) any later version.
      9 
     10    GNUnet is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    Affero General Public License for more details.
     14 
     15    You should have received a copy of the GNU Affero General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18    SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 /*
     21  * @author Tobias Frisch
     22  * @file ui/members.c
     23  */
     24 
     25 #include "members.h"
     26 
     27 #include "list_input.h"
     28 #include "../application.h"
     29 #include "../util.h"
     30 
     31 void
     32 members_event(UI_MEMBERS_Handle *members,
     33 	      struct MESSENGER_Application *app,
     34 	      int key)
     35 {
     36   list_input_reset(members);
     37 
     38   UI_MEMBERS_List *element = members->head;
     39   while (element)
     40   {
     41     list_input_select(members, 1, element->contact);
     42     element = element->next;
     43   }
     44 
     45   switch (key)
     46   {
     47     case 27:
     48     case KEY_EXIT:
     49     case '\t':
     50       app->chat.show_members = FALSE;
     51       break;
     52     case '\n':
     53     case KEY_ENTER: {
     54       struct GNUNET_CHAT_Context *context;
     55 
     56       if (!(members->selected))
     57 	break;
     58 
     59       context = GNUNET_CHAT_contact_get_context(members->selected);
     60       GNUNET_CHAT_context_request(context);
     61 
     62       app->chat.show_members = FALSE;
     63       app->chat.context = context;
     64       break;
     65     }
     66     default:
     67       break;
     68   }
     69 
     70   list_input_event(members, key);
     71 }
     72 
     73 static void
     74 _members_iterate_print(UI_MEMBERS_Handle *members,
     75 		       const struct GNUNET_CHAT_Contact *contact)
     76 {
     77   list_input_print(members, 1);
     78 
     79   const int attrs_select = A_BOLD;
     80 
     81   if (selected) wattron(members->window, attrs_select);
     82 
     83   wmove(members->window, y, 0);
     84 
     85   const char *name = GNUNET_CHAT_contact_get_name(contact);
     86   const char *key = GNUNET_CHAT_contact_get_key(contact);
     87 
     88   size_t key_len = key? strlen(key) : 0;
     89 
     90   util_enable_unique_color(members->window, contact);
     91 
     92   if (key_len > 4)
     93     wprintw(members->window, "[%s]: %s", key + (key_len - 4), name);
     94   else
     95     wprintw(members->window, "%s", name);
     96 
     97   util_disable_unique_color(members->window, contact);
     98 
     99   if (selected) wattroff(members->window, attrs_select);
    100 }
    101 
    102 void
    103 members_print(UI_MEMBERS_Handle *members)
    104 {
    105   if (!(members->window))
    106     return;
    107 
    108   list_input_reset(members);
    109   werase(members->window);
    110 
    111   UI_MEMBERS_List *element = members->head;
    112   while (element)
    113   {
    114     _members_iterate_print(members, element->contact);
    115     element = element->next;
    116   }
    117 }
    118 
    119 void
    120 members_clear(UI_MEMBERS_Handle *members)
    121 {
    122   UI_MEMBERS_List *element;
    123   while (members->head)
    124   {
    125     element = members->head;
    126 
    127     GNUNET_CONTAINER_DLL_remove(
    128 	members->head,
    129 	members->tail,
    130 	element
    131     );
    132 
    133     GNUNET_free(element);
    134   }
    135 
    136   members->line_selected = 0;
    137 }
    138 
    139 bool
    140 members_add(UI_MEMBERS_Handle *members,
    141 	    struct GNUNET_CHAT_Contact *contact)
    142 {
    143   UI_MEMBERS_List *element = members->head;
    144   while (element)
    145   {
    146     if (element->contact == contact)
    147       break;
    148 
    149     element = element->next;
    150   }
    151 
    152   if (element)
    153     return FALSE;
    154 
    155   element = GNUNET_new(UI_MEMBERS_List);
    156   element->contact = contact;
    157 
    158   GNUNET_CONTAINER_DLL_insert_tail(
    159       members->head,
    160       members->tail,
    161       element
    162   );
    163 
    164   return TRUE;
    165 }
    166 
    167 void
    168 members_remove(UI_MEMBERS_Handle *members,
    169 	       const struct GNUNET_CHAT_Contact *contact)
    170 {
    171   UI_MEMBERS_List *element = members->head;
    172   while (element)
    173   {
    174     if (element->contact == contact)
    175       break;
    176 
    177     element = element->next;
    178   }
    179 
    180   if (element)
    181     GNUNET_CONTAINER_DLL_remove(
    182 	members->head,
    183 	members->tail,
    184 	element
    185     );
    186 }