messenger-gtk

Gtk+3 graphical user interfaces for GNUnet Messenger
Log | Files | Refs | Submodules | README | LICENSE

commit 55728292d4b360c4ca7a55e48c98bf1221bc6f63
parent 69f2d93d65309bf74a7c7471c38e6a9d2537527b
Author: Jacki <jacki@thejackimonster.de>
Date:   Sat, 11 May 2024 17:15:14 +0200

Support interactive links in message texts

Signed-off-by: Jacki <jacki@thejackimonster.de>

Diffstat:
Msrc/event.c | 2+-
Msrc/ui.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/ui.h | 12++++++++++++
3 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/src/event.c b/src/event.c @@ -841,7 +841,7 @@ event_receive_message(MESSENGER_Application *app, "im.received" ); - ui_label_set_text(message->text_label, text); + ui_label_set_markup_text(message->text_label, text); ui_label_set_text(message->timestamp_label, time); ui_chat_add_message(handle->chat, app, message); diff --git a/src/ui.c b/src/ui.c @@ -62,6 +62,75 @@ ui_label_set_text(GtkLabel *label, const char *text) } void +ui_label_set_markup_text(GtkLabel *label, + const char *text) +{ + g_assert(label); + + if (!text) + { + gtk_label_set_markup(label, ""); + return; + } + + gchar *_text = g_locale_to_utf8(text, -1, NULL, NULL, NULL); + gchar *_escaped = g_markup_escape_text(_text, -1); + + if (_escaped) + { + g_free(_text); + _text = _escaped; + } + + GError *error = NULL; + GRegex *regex = g_regex_new( + "https?://(www.)?[-a-z0-9@:%._\\+~#=/&?]+", + G_REGEX_MULTILINE | G_REGEX_OPTIMIZE, + G_REGEX_MATCH_DEFAULT, + &error + ); + + if (error) + { + fprintf (stderr, "ERROR: %s (%d)\n", error->message, error->code); + g_error_free(error); + goto skip_regex; + } + + if (!regex) + goto skip_regex; + + gchar *_replaced = g_regex_replace( + regex, + _text, + -1, + 0, + "<a href=\"\\0\">\\0</a>", + G_REGEX_MATCH_DEFAULT, + &error + ); + + if (error) + { + fprintf (stderr, "%s\n", error->message); + g_error_free(error); + } + + if (_replaced) + { + g_free(_text); + _text = _replaced; + } + +skip_regex: + if (regex) + g_regex_unref(regex); + + gtk_label_set_markup(label, _text); + g_free(_text); +} + +void ui_label_set_size(GtkLabel *label, uint64_t size) { diff --git a/src/ui.h b/src/ui.h @@ -51,6 +51,18 @@ ui_label_set_text(GtkLabel *label, const char *text); /** + * Sets the text of a GtkLabel applying automatic utf8 + * conversion and replaces supported syntax with proper + * markup. + * + * @param label Label + * @param text Non-utf8 text + */ +void +ui_label_set_markup_text(GtkLabel *label, + const char *text); + +/** * Sets the text of a GtkLabel applying conversion from * file size to string representation. *