accounts.c (3395B)
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/accounts.c 23 */ 24 25 #include "accounts.h" 26 27 #include "list_input.h" 28 #include "../application.h" 29 #include "../util.h" 30 31 int 32 _accounts_iterate(void *cls, 33 UNUSED struct GNUNET_CHAT_Handle *handle, 34 struct GNUNET_CHAT_Account *account) 35 { 36 UI_ACCOUNTS_Handle *accounts = cls; 37 list_input_select(accounts, 1, account); 38 return GNUNET_YES; 39 } 40 41 void 42 accounts_event(UI_ACCOUNTS_Handle *accounts, 43 MESSENGER_Application *app, 44 int key) 45 { 46 if (accounts->create_dialog.window) 47 { 48 account_create_dialog_event(&(accounts->create_dialog), app, key); 49 return; 50 } 51 52 list_input_reset(accounts); 53 54 GNUNET_CHAT_iterate_accounts( 55 app->chat.handle, 56 &_accounts_iterate, 57 accounts 58 ); 59 60 list_input_select(accounts, 1, NULL); 61 62 switch (key) 63 { 64 case 27: 65 case KEY_EXIT: 66 app->chat.quit = TRUE; 67 break; 68 case '\n': 69 case KEY_ENTER: 70 if (accounts->selected) 71 GNUNET_CHAT_connect(app->chat.handle, accounts->selected); 72 else 73 accounts->create_dialog.window = &(accounts->window); 74 break; 75 default: 76 break; 77 } 78 79 list_input_event(accounts, key); 80 } 81 82 static int 83 _accounts_print_entry(UI_ACCOUNTS_Handle *accounts, 84 char type, 85 const char *text, 86 const void *data) 87 { 88 list_input_print_gnunet(accounts, 1); 89 90 const int attrs_select = A_BOLD; 91 92 if (selected) wattron(accounts->window, attrs_select); 93 else type = ' '; 94 95 wmove(accounts->window, y, 0); 96 97 util_enable_unique_color(accounts->window, data); 98 99 wprintw(accounts->window, "[%c] %s", type, text); 100 101 util_disable_unique_color(accounts->window, data); 102 103 if (selected) wattroff(accounts->window, attrs_select); 104 105 return GNUNET_YES; 106 } 107 108 int 109 _accounts_iterate_print(void *cls, 110 UNUSED struct GNUNET_CHAT_Handle *handle, 111 struct GNUNET_CHAT_Account *account) 112 { 113 UI_ACCOUNTS_Handle *accounts = cls; 114 const char *name = GNUNET_CHAT_account_get_name(account); 115 return _accounts_print_entry(accounts, 'x', name, account); 116 } 117 118 void 119 accounts_print(UI_ACCOUNTS_Handle *accounts, 120 MESSENGER_Application *app) 121 { 122 if (accounts->create_dialog.window) 123 { 124 account_create_dialog_print(&(accounts->create_dialog)); 125 return; 126 } 127 128 if (!(accounts->window)) 129 return; 130 131 list_input_reset(accounts); 132 werase(accounts->window); 133 134 GNUNET_CHAT_iterate_accounts( 135 app->chat.handle, 136 &_accounts_iterate_print, 137 accounts 138 ); 139 140 _accounts_print_entry(accounts, '+', "Add account", NULL); 141 }