From 5e9ee3427898e799cebe06787b409ca770e6e1ee Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Thu, 18 Nov 2021 01:37:30 +0100 Subject: Implemented querying contacts to visualize list of them Signed-off-by: TheJackiMonster --- Makefile | 3 +- resources/ui/contact_entry.ui | 98 +++++++++++++++++++++++++++++++++++++++++++ src/event.c | 3 +- src/ui/contact_entry.c | 61 +++++++++++++++++++++++++++ src/ui/contact_entry.h | 48 +++++++++++++++++++++ src/ui/contacts.c | 86 +++++++++++++++++++++++++++++++++++++ src/ui/contacts.h | 2 + 7 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 resources/ui/contact_entry.ui create mode 100644 src/ui/contact_entry.c create mode 100644 src/ui/contact_entry.h diff --git a/Makefile b/Makefile index 894973f..7dc9f52 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ SOURCES = messenger_gtk.c\ chat/messenger.c\ ui/chat.c\ ui/chat_entry.c\ + ui/contact_entry.c\ ui/contacts.c\ ui/message.c\ ui/messenger.c\ @@ -18,7 +19,7 @@ SOURCES = messenger_gtk.c\ HEADERS = LIBRARIES = gnunetchat zbargtk -PACKAGES = gnunetutil libhandy-1 gtk+-3.0 libnotify zbar libqrencode +PACKAGES = gnunetutil libhandy-1 gtk+-3.0 libnotify zbar libqrencode gnunetidentity GNU_CC ?= gcc GNU_LD ?= gcc diff --git a/resources/ui/contact_entry.ui b/resources/ui/contact_entry.ui new file mode 100644 index 0000000..c0376b3 --- /dev/null +++ b/resources/ui/contact_entry.ui @@ -0,0 +1,98 @@ + + + + + + + True + False + 8 + + + True + False + 48 + + + False + True + 0 + + + + + True + False + 2 + vertical + 2 + + + True + False + True + word-char + end + 0 + + + + + + False + True + 0 + + + + + True + False + True + word-char + end + 48 + 0 + + + + + + True + True + 1 + + + + + True + True + end + 1 + + + + + diff --git a/src/event.c b/src/event.c index 6e585b2..a9ae35d 100644 --- a/src/event.c +++ b/src/event.c @@ -25,6 +25,7 @@ #include "event.h" #include "ui/chat_entry.h" +#include "ui/contact_entry.h" #include "ui/message.h" static void @@ -81,7 +82,7 @@ _add_new_chat_entry(MESSENGER_Application *app, static int _iterate_profile_contacts(void *cls, UNUSED struct GNUNET_CHAT_Handle *handle, - UNUSED struct GNUNET_CHAT_Contact *contact) + struct GNUNET_CHAT_Contact *contact) { MESSENGER_Application *app = (MESSENGER_Application*) cls; _add_new_chat_entry(app, GNUNET_CHAT_contact_get_context(contact)); diff --git a/src/ui/contact_entry.c b/src/ui/contact_entry.c new file mode 100644 index 0000000..cf830db --- /dev/null +++ b/src/ui/contact_entry.c @@ -0,0 +1,61 @@ +/* + This file is part of GNUnet. + Copyright (C) 2021 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 . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/contact_entry.c + */ + +#include "contact_entry.h" + +#include "../application.h" + +UI_CONTACT_ENTRY_Handle* +ui_contact_entry_new(void) +{ + UI_CONTACT_ENTRY_Handle* handle = g_malloc(sizeof(UI_CONTACT_ENTRY_Handle)); + + handle->builder = gtk_builder_new_from_file("resources/ui/contact_entry.ui"); + + handle->entry_box = GTK_WIDGET( + gtk_builder_get_object(handle->builder, "entry_box") + ); + + handle->entry_avatar = HDY_AVATAR( + gtk_builder_get_object(handle->builder, "entry_avatar") + ); + + handle->title_label = GTK_LABEL( + gtk_builder_get_object(handle->builder, "title_label") + ); + + handle->subtitle_label = GTK_LABEL( + gtk_builder_get_object(handle->builder, "subtitle_label") + ); + + return handle; +} + +void +ui_contact_entry_delete(UI_CONTACT_ENTRY_Handle *handle) +{ + g_object_unref(handle->builder); + + g_free(handle); +} diff --git a/src/ui/contact_entry.h b/src/ui/contact_entry.h new file mode 100644 index 0000000..6b39077 --- /dev/null +++ b/src/ui/contact_entry.h @@ -0,0 +1,48 @@ +/* + This file is part of GNUnet. + Copyright (C) 2021 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 . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/contact_entry.h + */ + +#ifndef UI_CONTACT_ENTRY_H_ +#define UI_CONTACT_ENTRY_H_ + +#include "messenger.h" + +typedef struct UI_CONTACT_ENTRY_Handle +{ + GtkBuilder *builder; + + GtkWidget *entry_box; + + HdyAvatar *entry_avatar; + + GtkLabel *title_label; + GtkLabel *subtitle_label; +} UI_CONTACT_ENTRY_Handle; + +UI_CONTACT_ENTRY_Handle* +ui_contact_entry_new(void); + +void +ui_contact_entry_delete(UI_CONTACT_ENTRY_Handle *handle); + +#endif /* UI_CONTACT_ENTRY_H_ */ diff --git a/src/ui/contacts.c b/src/ui/contacts.c index 3750f8f..34ceaed 100644 --- a/src/ui/contacts.c +++ b/src/ui/contacts.c @@ -24,8 +24,11 @@ #include "contacts.h" +#include "contact_entry.h" #include "../application.h" +#include + static void handle_close_button_click(UNUSED GtkButton *button, gpointer user_data) @@ -41,10 +44,66 @@ handle_dialog_destroy(UNUSED GtkWidget *window, ui_contacts_dialog_cleanup((UI_CONTACTS_Handle*) user_data); } +static int +_iterate_clear_contacts(UNUSED void *cls, + UNUSED struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Contact *contact) +{ + GNUNET_CHAT_contact_set_user_pointer(contact, NULL); + return GNUNET_YES; +} + +static int +_iterate_contacts(void *cls, + UNUSED struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Contact *contact) +{ + MESSENGER_Application *app = (MESSENGER_Application*) cls; + + if (GNUNET_CHAT_contact_get_user_pointer(contact)) + return GNUNET_YES; + + const char *title; + title = GNUNET_CHAT_contact_get_name(contact); + + const struct GNUNET_IDENTITY_PublicKey *key; + key = GNUNET_CHAT_contact_get_key(contact); + + UI_CONTACT_ENTRY_Handle *entry = ui_contact_entry_new(); + gtk_container_add( + GTK_CONTAINER(app->ui.contacts.contacts_listbox), + entry->entry_box + ); + + GNUNET_CHAT_contact_set_user_pointer(contact, entry); + + if (title) + { + gtk_label_set_text(entry->title_label, title); + hdy_avatar_set_text(entry->entry_avatar, title); + } + + if (key) + { + char *key_string = GNUNET_IDENTITY_public_key_to_string(key); + gtk_label_set_text(entry->subtitle_label, key_string); + GNUNET_free(key_string); + } + + app->ui.contacts.contact_entries = g_list_append( + app->ui.contacts.contact_entries, + entry + ); + + return GNUNET_YES; +} + void ui_contacts_dialog_init(MESSENGER_Application *app, UI_CONTACTS_Handle *handle) { + handle->contact_entries = g_list_alloc(); + handle->builder = gtk_builder_new_from_file("resources/ui/contacts.ui"); handle->dialog = GTK_DIALOG( @@ -65,6 +124,10 @@ ui_contacts_dialog_init(MESSENGER_Application *app, gtk_builder_get_object(handle->builder, "contact_search_entry") ); + handle->contacts_listbox = GTK_LIST_BOX( + gtk_builder_get_object(handle->builder, "contacts_listbox") + ); + handle->close_button = GTK_BUTTON( gtk_builder_get_object(handle->builder, "close_button") ); @@ -82,10 +145,33 @@ ui_contacts_dialog_init(MESSENGER_Application *app, G_CALLBACK(handle_dialog_destroy), handle ); + + GNUNET_CHAT_iterate_contacts( + app->chat.messenger.handle, + _iterate_clear_contacts, + NULL + ); + + GNUNET_CHAT_iterate_contacts( + app->chat.messenger.handle, + _iterate_contacts, + app + ); } void ui_contacts_dialog_cleanup(UI_CONTACTS_Handle *handle) { g_object_unref(handle->builder); + + GList *list = handle->contact_entries; + + while (list) { + if (list->data) + ui_contact_entry_delete((UI_CONTACT_ENTRY_Handle*) list->data); + + list = list->next; + } + + g_list_free(handle->contact_entries); } diff --git a/src/ui/contacts.h b/src/ui/contacts.h index dbd952b..90b4b4c 100644 --- a/src/ui/contacts.h +++ b/src/ui/contacts.h @@ -29,6 +29,8 @@ typedef struct UI_CONTACTS_Handle { + GList *contact_entries; + GtkBuilder *builder; GtkDialog *dialog; -- cgit v1.2.3