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