Commit cf53f100 authored by Mohammed Sadiq's avatar Mohammed Sadiq
Browse files

message-row: Use the newly added widgets to show messages

We now have a widget to show images
parent 29d8a0d5
Pipeline #66815 passed with stages
in 9 minutes and 37 seconds
......@@ -14,6 +14,8 @@
#include "chatty-avatar.h"
#include "chatty-utils.h"
#include "chatty-image-item.h"
#include "chatty-text-item.h"
#include "chatty-message-row.h"
......@@ -27,9 +29,10 @@ struct _ChattyMessageRow
GtkWidget *content_grid;
GtkWidget *avatar_image;
GtkWidget *message_event_box;
GtkWidget *message_label;
GtkWidget *footer_label;
GtkWidget *content;
GtkWidget *popover;
GtkGesture *multipress_gesture;
GtkGesture *longpress_gesture;
......@@ -42,26 +45,6 @@ struct _ChattyMessageRow
G_DEFINE_TYPE (ChattyMessageRow, chatty_message_row, GTK_TYPE_LIST_BOX_ROW)
static gchar *
chatty_msg_list_escape_message (ChattyMessageRow *self,
const char *message)
{
g_autofree char *nl_2_br = NULL;
g_autofree char *striped = NULL;
g_autofree char *escaped = NULL;
g_autofree char *linkified = NULL;
char *result;
nl_2_br = purple_strdup_withhtml (message);
striped = purple_markup_strip_html (nl_2_br);
escaped = purple_markup_escape_text (striped, -1);
linkified = purple_markup_linkify (escaped);
// convert all tags to lowercase for GtkLabel markup parser
purple_markup_html_to_xhtml (linkified, &result, NULL);
return result;
}
static void
copy_clicked_cb (ChattyMessageRow *self)
{
......@@ -71,9 +54,9 @@ copy_clicked_cb (ChattyMessageRow *self)
g_assert (CHATTY_IS_MESSAGE_ROW (self));
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
text = gtk_label_get_text (GTK_LABEL (self->message_label));
text = chatty_text_item_get_text (CHATTY_TEXT_ITEM (self->content));
if (text != NULL)
if (text && *text)
gtk_clipboard_set_text (clipboard, text, -1);
}
......@@ -109,44 +92,6 @@ message_row_hold_cb (ChattyMessageRow *self)
gtk_gesture_set_state (self->longpress_gesture, GTK_EVENT_SEQUENCE_CLAIMED);
}
static void
message_row_update_quotes (ChattyMessageRow *self)
{
const char *text, *end;
char *quote;
text = gtk_label_get_text (GTK_LABEL (self->message_label));
end = text;
if (!text || !*text)
return;
do {
quote = strchr (end, '>');
if (quote &&
(quote == text ||
*(quote - 1) == '\n')) {
PangoAttrList *list;
PangoAttribute *attribute;
list = gtk_label_get_attributes (GTK_LABEL (self->message_label));
end = strchr (quote, '\n');
if (!end)
end = quote + strlen (quote);
attribute = pango_attr_foreground_new (30000, 30000, 30000);
attribute->start_index = quote - text;
attribute->end_index = end - text + 1;
pango_attr_list_insert (list, attribute);
} else if (quote && *quote) {
/* Move forward one character if '>' happend midst a line */
end = end + 1;
}
} while (quote && *quote);
}
static void
message_row_update_message (ChattyMessageRow *self)
{
......@@ -154,7 +99,6 @@ message_row_update_message (ChattyMessageRow *self)
g_autofree char *footer = NULL;
const char *status_str = "";
ChattyMsgStatus status;
ChattyMsgType type;
g_assert (CHATTY_IS_MESSAGE_ROW (self));
g_assert (self->message);
......@@ -192,41 +136,8 @@ message_row_update_message (ChattyMessageRow *self)
NULL);
}
type = chatty_message_get_msg_type (self->message);
if (type == CHATTY_MESSAGE_IMAGE ||
type == CHATTY_MESSAGE_VIDEO ||
type == CHATTY_MESSAGE_AUDIO ||
type == CHATTY_MESSAGE_FILE) {
ChattyFileInfo *file;
const char *name = NULL;
GList *files = NULL;
files = chatty_message_get_files (self->message);
file = g_list_nth_data (files, 0);
if (file)
name = file->file_name ? file->file_name : chatty_message_get_text (self->message);
if (file && file->url) {
if (file->status == CHATTY_FILE_DOWNLOADED) {
const char *end = strrchr (file->url, '/');
if (end)
message = g_strconcat ("<a href='file://", g_get_user_cache_dir (), "/",
"chatty", "/", "files", end, "'>", name, "</a>", NULL);
}
if (!message)
message = g_strconcat ("<a href='", file->url, "'>", name, "</a>", NULL);
} else
message = g_strdup (name);
} else {
message = chatty_msg_list_escape_message (self, chatty_message_get_text (self->message));
}
gtk_label_set_markup (GTK_LABEL (self->message_label), message);
gtk_label_set_markup (GTK_LABEL (self->footer_label), footer);
gtk_widget_set_visible (self->footer_label, footer && *footer);
message_row_update_quotes (self);
}
static gboolean
......@@ -266,21 +177,14 @@ chatty_message_row_class_init (ChattyMessageRowClass *klass)
gtk_widget_class_bind_template_child (widget_class, ChattyMessageRow, content_grid);
gtk_widget_class_bind_template_child (widget_class, ChattyMessageRow, avatar_image);
gtk_widget_class_bind_template_child (widget_class, ChattyMessageRow, message_event_box);
gtk_widget_class_bind_template_child (widget_class, ChattyMessageRow, message_label);
gtk_widget_class_bind_template_child (widget_class, ChattyMessageRow, footer_label);
}
static void
chatty_message_row_init (ChattyMessageRow *self)
{
PangoAttrList *list;
gtk_widget_init_template (GTK_WIDGET (self));
list = pango_attr_list_new ();
gtk_label_set_attributes (GTK_LABEL (self->message_label), list);
pango_attr_list_unref (list);
self->multipress_gesture = gtk_gesture_multi_press_new (self->message_event_box);
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (self->multipress_gesture), GDK_BUTTON_SECONDARY);
g_signal_connect_swapped (self->multipress_gesture, "pressed",
......@@ -300,34 +204,46 @@ chatty_message_row_new (ChattyMessage *message,
ChattyMessageRow *self;
GtkStyleContext *sc;
ChattyMsgDirection direction;
ChattyMsgType type;
g_return_val_if_fail (CHATTY_IS_MESSAGE (message), NULL);
self = g_object_new (CHATTY_TYPE_MESSAGE_ROW, NULL);
sc = gtk_widget_get_style_context (self->message_label);
sc = gtk_widget_get_style_context (self->message_event_box);
self->protocol = protocol;
self->message = g_object_ref (message);
self->is_im = !!is_im;
direction = chatty_message_get_msg_direction (message);
type = chatty_message_get_msg_type (message);
if (direction == CHATTY_DIRECTION_IN) {
gtk_style_context_add_class (sc, "bubble_white");
gtk_widget_set_halign (self->content_grid, GTK_ALIGN_START);
gtk_widget_set_halign (self->message_label, GTK_ALIGN_START);
} else if (direction == CHATTY_DIRECTION_OUT && protocol == CHATTY_PROTOCOL_SMS) {
gtk_style_context_add_class (sc, "bubble_green");
} else if (direction == CHATTY_DIRECTION_OUT) {
gtk_style_context_add_class (sc, "bubble_blue");
} else { /* System message */
gtk_style_context_add_class (sc, "bubble_purple");
gtk_widget_set_hexpand (self->message_label, TRUE);
if (type == CHATTY_MESSAGE_IMAGE)
self->content = chatty_image_item_new (message, protocol);
else
self->content = chatty_text_item_new (message, protocol);
gtk_container_add (GTK_CONTAINER (self->message_event_box), self->content);
gtk_widget_show (self->content);
if (type != CHATTY_MESSAGE_IMAGE) {
if (direction == CHATTY_DIRECTION_IN) {
gtk_style_context_add_class (sc, "bubble_white");
gtk_widget_set_halign (self->content_grid, GTK_ALIGN_START);
gtk_widget_set_halign (self->message_event_box, GTK_ALIGN_START);
} else if (direction == CHATTY_DIRECTION_OUT && protocol == CHATTY_PROTOCOL_SMS) {
gtk_style_context_add_class (sc, "bubble_green");
} else if (direction == CHATTY_DIRECTION_OUT) {
gtk_style_context_add_class (sc, "bubble_blue");
} else { /* System message */
gtk_style_context_add_class (sc, "bubble_purple");
gtk_widget_set_hexpand (self->message_event_box, TRUE);
}
}
if (direction == CHATTY_DIRECTION_OUT) {
gtk_label_set_xalign (GTK_LABEL (self->footer_label), 1);
gtk_widget_set_halign (self->content_grid, GTK_ALIGN_END);
gtk_widget_set_halign (self->message_label, GTK_ALIGN_END);
gtk_widget_set_halign (self->message_event_box, GTK_ALIGN_END);
} else {
gtk_label_set_xalign (GTK_LABEL (self->footer_label), 0);
}
......
......@@ -37,19 +37,9 @@
<child>
<object class="GtkEventBox" id="message_event_box">
<property name="visible">True</property>
<child>
<object class="GtkLabel" id="message_label">
<property name="visible">True</property>
<property name="margin">3</property>
<property name="wrap">True</property>
<property name="wrap-mode">word-char</property>
<property name="use-markup">True</property>
<property name="xalign">0</property>
<style>
<class name="message_bubble"/>
</style>
</object>
</child>
<style>
<class name="message_bubble"/>
</style>
</object>
<packing>
<property name="left-attach">1</property>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment