new_tag.c (4840B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2024 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/new_tag.c 23 */ 24 25 #include "new_tag.h" 26 27 #include "../application.h" 28 #include "../ui.h" 29 30 #include <gnunet/gnunet_common.h> 31 32 static void 33 _add_new_tag(MESSENGER_Application *app, 34 UI_NEW_TAG_Handle *handle) 35 { 36 g_assert((app) && (handle)); 37 38 char *tag = ui_entry_get_text(handle->tag_entry); 39 40 if (handle->callback) 41 handle->callback( 42 app, 43 handle->selected, 44 tag, 45 handle->user_data 46 ); 47 48 if (tag) 49 GNUNET_free(tag); 50 51 gtk_window_close(GTK_WINDOW(handle->dialog)); 52 } 53 54 static void 55 handle_tag_entry_changed(GtkEditable *editable, 56 gpointer user_data) 57 { 58 g_assert((editable) && (user_data)); 59 60 HdyAvatar *avatar = HDY_AVATAR(user_data); 61 GtkEntry *entry = GTK_ENTRY(editable); 62 63 const gchar *text = gtk_entry_get_text(entry); 64 65 hdy_avatar_set_text(avatar, text); 66 } 67 68 static void 69 handle_tag_entry_activate(UNUSED GtkEntry *entry, 70 gpointer user_data) 71 { 72 g_assert(user_data); 73 74 MESSENGER_Application *app = (MESSENGER_Application*) user_data; 75 76 _add_new_tag(app, &(app->ui.new_tag)); 77 78 gtk_window_close(GTK_WINDOW(app->ui.new_tag.dialog)); 79 } 80 81 static void 82 handle_cancel_button_click(UNUSED GtkButton *button, 83 gpointer user_data) 84 { 85 g_assert(user_data); 86 87 GtkDialog *dialog = GTK_DIALOG(user_data); 88 gtk_window_close(GTK_WINDOW(dialog)); 89 } 90 91 static void 92 handle_confirm_button_click(UNUSED GtkButton *button, 93 gpointer user_data) 94 { 95 g_assert(user_data); 96 97 MESSENGER_Application *app = (MESSENGER_Application*) user_data; 98 99 _add_new_tag(app, &(app->ui.new_tag)); 100 101 gtk_window_close(GTK_WINDOW(app->ui.new_tag.dialog)); 102 } 103 104 static void 105 handle_dialog_destroy(UNUSED GtkWidget *window, 106 gpointer user_data) 107 { 108 g_assert(user_data); 109 110 MESSENGER_Application *app = (MESSENGER_Application*) user_data; 111 112 ui_new_tag_dialog_cleanup(&(app->ui.new_tag)); 113 } 114 115 void 116 ui_new_tag_dialog_init(MESSENGER_Application *app, 117 UI_NEW_TAG_Handle *handle) 118 { 119 g_assert((app) && (handle)); 120 121 handle->selected = NULL; 122 handle->user_data = NULL; 123 handle->callback = NULL; 124 125 handle->builder = ui_builder_from_resource( 126 application_get_resource_path(app, "ui/new_tag.ui") 127 ); 128 129 handle->dialog = GTK_DIALOG( 130 gtk_builder_get_object(handle->builder, "new_tag_dialog") 131 ); 132 133 gtk_window_set_transient_for( 134 GTK_WINDOW(handle->dialog), 135 GTK_WINDOW(app->ui.messenger.main_window) 136 ); 137 138 handle->tag_avatar = HDY_AVATAR( 139 gtk_builder_get_object(handle->builder, "tag_avatar") 140 ); 141 142 handle->tag_entry = GTK_ENTRY( 143 gtk_builder_get_object(handle->builder, "tag_entry") 144 ); 145 146 g_signal_connect( 147 handle->tag_entry, 148 "changed", 149 G_CALLBACK(handle_tag_entry_changed), 150 handle->tag_avatar 151 ); 152 153 g_signal_connect( 154 handle->tag_entry, 155 "activate", 156 G_CALLBACK(handle_tag_entry_activate), 157 app 158 ); 159 160 handle->cancel_button = GTK_BUTTON( 161 gtk_builder_get_object(handle->builder, "cancel_button") 162 ); 163 164 g_signal_connect( 165 handle->cancel_button, 166 "clicked", 167 G_CALLBACK(handle_cancel_button_click), 168 handle->dialog 169 ); 170 171 handle->confirm_button = GTK_BUTTON( 172 gtk_builder_get_object(handle->builder, "confirm_button") 173 ); 174 175 g_signal_connect( 176 handle->confirm_button, 177 "clicked", 178 G_CALLBACK(handle_confirm_button_click), 179 app 180 ); 181 182 g_signal_connect( 183 handle->dialog, 184 "destroy", 185 G_CALLBACK(handle_dialog_destroy), 186 app 187 ); 188 } 189 190 void 191 ui_new_tag_dialog_link(UI_NEW_TAG_Handle *handle, 192 UI_NEW_TAG_Callback callback, 193 GList *selected, 194 gpointer user_data) 195 { 196 g_assert((handle) && (callback)); 197 198 handle->selected = selected; 199 handle->user_data = user_data; 200 handle->callback = callback; 201 } 202 203 void 204 ui_new_tag_dialog_cleanup(UI_NEW_TAG_Handle *handle) 205 { 206 g_assert(handle); 207 208 if (handle->selected) 209 g_list_free(handle->selected); 210 211 g_object_unref(handle->builder); 212 213 memset(handle, 0, sizeof(*handle)); 214 }