Commit 85f4e5fa authored by Andrea Schaefer's avatar Andrea Schaefer

Fix icon issues

- get avatar for new incoming SMS and URI-SMS
- add folks avatar to blist.xml
- show icon in chat details
- adjust icon sizes
- move icon shaping to chatty-icon.c
- cleanup chatty-icon.c
parent ddecd119
......@@ -116,7 +116,7 @@ row_selected_cb (GtkListBox *box,
avatar = chatty_icon_get_buddy_icon (node,
NULL,
CHATTY_ICON_SIZE_MEDIUM,
CHATTY_ICON_SIZE_SMALL,
CHATTY_COLOR_GREY,
FALSE);
......@@ -677,11 +677,13 @@ chatty_blist_add_buddy_from_uri (const char *uri)
buddy = purple_buddy_new (account, who, alias);
purple_blist_add_buddy (buddy, NULL, NULL, NULL);
chatty_folks_set_purple_buddy_avatar (folks_id, account, g_strdup (who));
}
avatar = chatty_icon_get_buddy_icon (PURPLE_BLIST_NODE(buddy),
who,
CHATTY_ICON_SIZE_MEDIUM,
CHATTY_ICON_SIZE_SMALL,
CHATTY_COLOR_GREEN,
FALSE);
......@@ -1246,9 +1248,7 @@ chatty_blist_contacts_update_node (PurpleBuddy *buddy,
gchar *name = NULL;
const gchar *alias;
const gchar *account_name;
const gchar *protocol_id;
PurpleAccount *account;
const char *color;
gboolean blur;
PurplePresence *presence = purple_buddy_get_presence (buddy);
......@@ -1268,16 +1268,8 @@ chatty_blist_contacts_update_node (PurpleBuddy *buddy,
return;
}
protocol_id = purple_account_get_protocol_id (account);
alias = purple_buddy_get_alias (buddy);
if (g_strcmp0 (protocol_id, "prpl-mm-sms") == 0) {
color = CHATTY_COLOR_GREEN;
} else {
color = CHATTY_COLOR_BLUE;
}
if (purple_prefs_get_bool (CHATTY_PREFS_ROOT "/blist/greyout_offline_buddies") &&
!PURPLE_BUDDY_IS_ONLINE(buddy)) {
......@@ -1288,8 +1280,9 @@ chatty_blist_contacts_update_node (PurpleBuddy *buddy,
avatar = chatty_icon_get_buddy_icon ((PurpleBlistNode *)buddy,
alias,
CHATTY_ICON_SIZE_LARGE,
color,
CHATTY_ICON_SIZE_MEDIUM,
chatty_blist_protocol_is_sms (account) ?
CHATTY_COLOR_GREEN : CHATTY_COLOR_BLUE,
blur);
if (purple_prefs_get_bool (CHATTY_PREFS_ROOT "/blist/blur_idle_buddies") &&
......@@ -1364,7 +1357,7 @@ chatty_blist_contacts_update_group_chat (PurpleBlistNode *node)
avatar = chatty_icon_get_buddy_icon (node,
NULL,
CHATTY_ICON_SIZE_LARGE,
CHATTY_ICON_SIZE_MEDIUM,
CHATTY_COLOR_BLUE,
FALSE);
......@@ -1424,8 +1417,6 @@ chatty_blist_chats_update_node (PurpleBuddy *buddy,
g_autofree gchar *unread_messages = NULL;
const gchar *tag;
const gchar *alias;
const gchar *protocol_id;
const char *color;
g_autofree gchar *last_message_striped = NULL;
gboolean notify;
gboolean blur;
......@@ -1440,16 +1431,8 @@ chatty_blist_chats_update_node (PurpleBuddy *buddy,
return;
}
protocol_id = purple_account_get_protocol_id (account);
alias = purple_buddy_get_alias (buddy);
if (g_strcmp0 (protocol_id, "prpl-mm-sms") == 0) {
color = CHATTY_COLOR_GREEN;
} else {
color = CHATTY_COLOR_BLUE;
}
if (!purple_prefs_get_bool (CHATTY_PREFS_ROOT "/status/first_start")) {
chatty_window_overlay_show (FALSE);
purple_prefs_set_bool (CHATTY_PREFS_ROOT "/status/first_start", FALSE);
......@@ -1465,8 +1448,9 @@ chatty_blist_chats_update_node (PurpleBuddy *buddy,
avatar = chatty_icon_get_buddy_icon (node,
alias,
CHATTY_ICON_SIZE_LARGE,
color,
CHATTY_ICON_SIZE_MEDIUM,
chatty_blist_protocol_is_sms (account) ?
CHATTY_COLOR_GREEN : CHATTY_COLOR_BLUE,
blur);
if (purple_prefs_get_bool (CHATTY_PREFS_ROOT "/blist/blur_idle_buddies") &&
......@@ -1583,7 +1567,7 @@ chatty_blist_chats_update_group_chat (PurpleBlistNode *node)
avatar = chatty_icon_get_buddy_icon (node,
NULL,
CHATTY_ICON_SIZE_LARGE,
CHATTY_ICON_SIZE_MEDIUM,
CHATTY_COLOR_BLUE,
FALSE);
......
......@@ -2463,7 +2463,7 @@ chatty_conv_show_conversation (PurpleConversation *conv)
avatar = chatty_icon_get_buddy_icon (PURPLE_BLIST_NODE(buddy),
name,
CHATTY_ICON_SIZE_MEDIUM,
CHATTY_ICON_SIZE_SMALL,
color,
FALSE);
......
......@@ -17,6 +17,7 @@
#include "chatty-dialogs.h"
#include "chatty-lurch.h"
#include "chatty-utils.h"
#include "chatty-icons.h"
#include "version.h"
#include <libebook-contacts/libebook-contacts.h>
......@@ -1299,6 +1300,8 @@ chatty_dialogs_show_dialog_user_info (ChattyConversation *chatty_conv)
GtkWindow *window;
GtkSwitch *switch_notify;
GtkListBox *listbox_prefs;
GdkPixbuf *icon;
GtkImage *avatar;
const char *protocol_id;
const char *alias;
......@@ -1307,6 +1310,7 @@ chatty_dialogs_show_dialog_user_info (ChattyConversation *chatty_conv)
builder = gtk_builder_new_from_resource ("/sm/puri/chatty/ui/chatty-dialog-user-info.ui");
avatar = GTK_IMAGE (gtk_builder_get_object (builder, "avatar"));
label_alias = GTK_WIDGET (gtk_builder_get_object (builder, "label_alias"));
label_user_id = GTK_WIDGET (gtk_builder_get_object (builder, "label_user_id"));
label_jid = GTK_WIDGET (gtk_builder_get_object (builder, "label_jid"));
......@@ -1355,6 +1359,17 @@ chatty_dialogs_show_dialog_user_info (ChattyConversation *chatty_conv)
buddy = purple_find_buddy (chatty_conv->conv->account, chatty_conv->conv->name);
alias = purple_buddy_get_alias (buddy);
icon = chatty_icon_get_buddy_icon (PURPLE_BLIST_NODE(buddy),
alias,
CHATTY_ICON_SIZE_LARGE,
chatty_blist_protocol_is_sms (account) ?
CHATTY_COLOR_GREEN : CHATTY_COLOR_BLUE,
FALSE);
if (icon != NULL) {
gtk_image_set_from_pixbuf (GTK_IMAGE(avatar), icon);
}
gtk_switch_set_state (switch_notify,
purple_blist_node_get_bool (PURPLE_BLIST_NODE(buddy),
"chatty-notifications"));
......
/*
* Copyright (C) 2018 Purism SPC
* Copyright (C) 2019 Purism SPC
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
......@@ -17,10 +17,11 @@
#include "chatty-window.h"
#include "chatty-contact-row.h"
#include "chatty-utils.h"
#include "chatty-icons.h"
#include "chatty-window.h"
static chatty_folks_data_t chatty_folks_data;
static GdkPixbuf *chatty_folks_shape_pixbuf (GdkPixbuf *pixbuf);
static void chatty_folks_individual_add_contact_rows (FolksIndividual *individual);
chatty_folks_data_t *chatty_get_folks_data (void)
......@@ -202,16 +203,17 @@ cb_pixbuf_from_stream_ready (GObject *source_object,
GAsyncResult *result,
gpointer avatar_data)
{
GdkPixbuf *pixbuf;
AvatarData *data = avatar_data;
PurpleBuddy *buddy;
GdkPixbuf *pixbuf;
AvatarData *data = avatar_data;
PurpleContact *contact;
PurpleBuddy *buddy;
g_autoptr(GError) error = NULL;
pixbuf = gdk_pixbuf_new_from_stream_finish (result, &error);
if (error != NULL) {
g_debug ("Could not get pixbuf from stream: %s", error->message);
g_error ("Could not get pixbuf from stream: %s", error->message);
g_slice_free (AvatarData, data);
......@@ -223,32 +225,36 @@ cb_pixbuf_from_stream_ready (GObject *source_object,
g_return_if_fail (data->row != NULL);
g_object_set (data->row,
"avatar", chatty_folks_shape_pixbuf (pixbuf),
"avatar", chatty_icon_shape_pixbuf (pixbuf),
NULL);
break;
case CHATTY_FOLKS_SET_PURPLE_BUDDY_ICON:
gdk_pixbuf_save (pixbuf, "/var/tmp/chatty_tmp.jpg", "jpeg", &error, "quality", "100", NULL);
buddy = purple_find_buddy (data->purple_account, data->purple_user_name);
purple_buddy_icons_node_set_custom_icon_from_file ((PurpleBlistNode*)buddy,
"/var/tmp/chatty_tmp.jpg");
if (error != NULL) {
g_debug ("Could not save pixbuf to file: %s", error->message);
g_error ("Could not save pixbuf to file: %s", error->message);
g_slice_free (AvatarData, data);
return;
}
remove ("/var/tmp/chatty_tmp.jpg");
buddy = purple_find_buddy (data->purple_account, data->purple_user_name);
contact = purple_buddy_get_contact (buddy);
purple_buddy_icons_node_set_custom_icon_from_file ((PurpleBlistNode*)contact,
"/var/tmp/chatty_tmp.jpg");
if (remove ("/var/tmp/chatty_tmp.jpg")) {
g_error ("Could not delete file: %s", "/var/tmp/chatty_tmp.jpg");
};
break;
default:
g_debug ("Chatty folks icon mode not set");
g_warning ("Chatty folks icon mode not set");
}
g_object_unref (pixbuf);
......@@ -271,7 +277,7 @@ cb_icon_load_async_ready (GObject *source_object,
data->stream = g_loadable_icon_load_finish (icon, result, NULL, &error);
if (error != NULL) {
g_debug ("Could not load icon: %s", error->message);
g_error ("Could not load icon: %s", error->message);
g_slice_free (AvatarData, data);
......@@ -288,62 +294,6 @@ cb_icon_load_async_ready (GObject *source_object,
}
static GdkPixbuf *
chatty_folks_shape_pixbuf (GdkPixbuf *pixbuf)
{
cairo_format_t format;
cairo_surface_t *surface;
cairo_t *cr;
GdkPixbuf *ret;
int width, height, size;
format = CAIRO_FORMAT_ARGB32;
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
size = (width >= height) ? width : height;
surface = cairo_image_surface_create (format, size, size);
cr = cairo_create (surface);
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
cairo_arc (cr,
size / 2,
size / 2,
size / 2,
0,
2 * M_PI);
cairo_fill (cr);
gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
cairo_arc (cr,
size / 2,
size / 2,
size / 2,
0,
2 * M_PI);
cairo_clip (cr);
cairo_paint (cr);
ret = gdk_pixbuf_get_from_surface (surface,
0,
0,
size,
size);
cairo_surface_destroy (surface);
cairo_destroy (cr);
return ret;
}
static void
chatty_folks_load_avatar (FolksIndividual *individual,
ChattyContactRow *row,
......@@ -354,14 +304,32 @@ chatty_folks_load_avatar (FolksIndividual *individual,
{
AvatarData *data;
GLoadableIcon *avatar;
GdkPixbuf *pixbuf;
const char *name;
g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
avatar = folks_avatar_details_get_avatar (FOLKS_AVATAR_DETAILS(individual));
if (avatar == NULL) {
g_debug ("Could not get folks avatar");
// alternatively, a green icon with the initial
// of the contact name will be created
if (avatar == NULL && mode == CHATTY_FOLKS_SET_CONTACT_ROW_ICON) {
g_return_if_fail (row);
name = folks_individual_get_display_name (individual);
pixbuf = chatty_icon_get_buddy_icon (NULL,
name,
CHATTY_ICON_SIZE_MEDIUM,
CHATTY_COLOR_GREEN,
FALSE);
g_object_set (row,
"avatar", chatty_icon_shape_pixbuf (pixbuf),
NULL);
g_object_unref (pixbuf);
return;
}
......@@ -542,7 +510,7 @@ chatty_folks_set_purple_buddy_avatar (const char *folks_id,
account,
user_name,
CHATTY_FOLKS_SET_PURPLE_BUDDY_ICON,
48);
CHATTY_ICON_SIZE_LARGE);
}
}
......@@ -655,7 +623,7 @@ chatty_folks_individual_add_contact_rows (FolksIndividual *individual)
NULL,
NULL,
CHATTY_FOLKS_SET_CONTACT_ROW_ICON,
36);
CHATTY_ICON_SIZE_MEDIUM);
g_object_unref (field_details);
}
......
/*
* Copyright (C) 2018 Purism SPC
* Copyright (C) 2019 Purism SPC
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
......
......@@ -13,104 +13,112 @@
#include "chatty-icons.h"
static GObject *
chatty_icon_pixbuf_from_data_helper (const guchar *buf,
gsize count,
gboolean animated)
static GdkPixbuf *
chatty_icon_pixbuf_from_data (const guchar *buf,
gsize count)
{
GObject *pixbuf;
GdkPixbuf *pixbuf;
GdkPixbufLoader *loader;
GError *error = NULL;
loader = gdk_pixbuf_loader_new ();
g_autoptr(GError) error = NULL;
if (!gdk_pixbuf_loader_write (loader, buf, count, &error) || error) {
purple_debug_warning ("gtkutils", "gdk_pixbuf_loader_write() "
"failed with size=%zu: %s\n", count,
error ? error->message : "(no error message)");
loader = gdk_pixbuf_loader_new ();
if (error) {
g_error_free (error);
}
if (!gdk_pixbuf_loader_write (loader, buf, count, &error)) {
g_error ("%s: pixbuf_loder_write failed: %s", __func__, error->message);
g_object_unref (G_OBJECT(loader));
return NULL;
}
if (!gdk_pixbuf_loader_close(loader, &error) || error) {
purple_debug_warning ("gtkutils", "gdk_pixbuf_loader_close() "
"failed for image of size %zu: %s\n", count,
error ? error->message : "(no error message)");
if (error) {
g_error_free(error);
}
if (!gdk_pixbuf_loader_close (loader, &error)) {
g_error ("%s: pixbuf_loder_close failed: %s", __func__, error->message);
g_object_unref(G_OBJECT(loader));
return NULL;
}
if (animated) {
pixbuf = G_OBJECT(gdk_pixbuf_loader_get_animation (loader));
} else {
pixbuf = G_OBJECT(gdk_pixbuf_loader_get_pixbuf (loader));
}
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (!pixbuf) {
purple_debug_warning("gtkutils", "%s() returned NULL for image "
"of size %zu\n",
animated ? "gdk_pixbuf_loader_get_animation"
: "gdk_pixbuf_loader_get_pixbuf", count);
g_error ("%s: pixbuf creation failed", __func__);
g_object_unref(G_OBJECT(loader));
g_object_unref (G_OBJECT(loader));
return NULL;
}
g_object_ref(pixbuf);
g_object_unref(G_OBJECT(loader));
g_object_ref (pixbuf);
g_object_unref (G_OBJECT(loader));
return pixbuf;
}
static GdkPixbuf *
chatty_icon_pixbuf_from_data (const guchar *buf,
gsize count)
GdkPixbuf *
chatty_icon_shape_pixbuf (GdkPixbuf *pixbuf)
{
return GDK_PIXBUF (chatty_icon_pixbuf_from_data_helper (buf, count, FALSE));
}
cairo_format_t format;
cairo_surface_t *surface;
cairo_t *cr;
GdkPixbuf *ret;
int width, height, size;
format = CAIRO_FORMAT_ARGB32;
GtkWidget *
chatty_icon_get_avatar_button (int size)
{
GtkWidget *image;
GtkWidget *button_avatar;
GtkStyleContext *sc;
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
size = (width >= height) ? width : height;
button_avatar = gtk_menu_button_new ();
surface = cairo_image_surface_create (format, size, size);
gtk_widget_set_hexpand (button_avatar, FALSE);
gtk_widget_set_halign (button_avatar, GTK_ALIGN_CENTER);
gtk_widget_set_size_request (GTK_WIDGET(button_avatar), size, size);
cr = cairo_create (surface);
image = gtk_image_new_from_icon_name ("avatar-default-symbolic", GTK_ICON_SIZE_DIALOG);
cairo_set_antialias (cr, CAIRO_ANTIALIAS_SUBPIXEL);
gtk_button_set_image (GTK_BUTTON (button_avatar), image);
sc = gtk_widget_get_style_context (button_avatar);
gtk_style_context_add_class (sc, "button_avatar");
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
return button_avatar;
cairo_arc (cr,
size / 2,
size / 2,
size / 2,
0,
2 * M_PI);
cairo_fill (cr);
gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
cairo_arc (cr,
size / 2,
size / 2,
size / 2,
0,
2 * M_PI);
cairo_clip (cr);
cairo_paint (cr);
ret = gdk_pixbuf_get_from_surface (surface,
0,
0,
size,
size);
cairo_surface_destroy (surface);
cairo_destroy (cr);
return ret;
}
GdkPixbuf *
chatty_icon_get_buddy_icon (PurpleBlistNode *node,
const char *name,
guint scale,
guint size,
const char *color,
gboolean greyed)
{
......@@ -121,7 +129,6 @@ chatty_icon_get_buddy_icon (PurpleBlistNode *node,
PurpleGroup *group = NULL;
const guchar *data = NULL;
GdkPixbuf *buf = NULL, *ret = NULL;
cairo_format_t format;
cairo_surface_t *surface;
cairo_t *cr;
PurpleBuddyIcon *icon = NULL;
......@@ -140,8 +147,6 @@ chatty_icon_get_buddy_icon (PurpleBlistNode *node,
gdouble color_g;
gdouble color_b;
//g_debug ("chatty_icon_get_buddy_icon name: %s", name);
// convert colors for drawing the cairo background
if (color) {
sub_str = g_utf8_substring (color, 0, 2);
......@@ -211,7 +216,7 @@ chatty_icon_get_buddy_icon (PurpleBlistNode *node,
buf = gtk_icon_theme_load_icon (icon_theme,
symbol,
36,
size,
0,
NULL);
}
......@@ -221,8 +226,8 @@ chatty_icon_get_buddy_icon (PurpleBlistNode *node,
// buddy name if there is no icon available
if (data == NULL && name != NULL) {
cairo_text_extents_t te;
int width = 36;
int height = 36;
int width = size;
int height = size;
double x_pos, y_pos;
char tmp[4];
char *initial_char;
......@@ -237,7 +242,7 @@ chatty_icon_get_buddy_icon (PurpleBlistNode *node,
cairo_paint (cr);
cairo_set_source_rgb (cr, 0.95, 0.95, 0.95);
cairo_set_font_size (cr, 24.0);
cairo_set_font_size (cr, size / 2);
cairo_select_font_face (cr,
"Sans",
CAIRO_FONT_SLANT_NORMAL,
......@@ -298,9 +303,9 @@ chatty_icon_get_buddy_icon (PurpleBlistNode *node,
&scale_height);
}
scale_size = 12.0 * (float)scale;
scale_size = (float)size;
if (scale) {
if (size) {
GdkPixbuf *tmpbuf;
if (scale_height > scale_width) {
......@@ -337,137 +342,9 @@ chatty_icon_get_buddy_icon (PurpleBlistNode *node,
GDK_INTERP_BILINEAR);
}
format = (gdk_pixbuf_get_has_alpha (ret)) ?
CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24;
surface = cairo_image_surface_create (format, scale_size, scale_size);
cr = cairo_create (surface);
cairo_set_source_rgb (cr, color_r, color_g, color_b);
// we want a round avatar
cairo_arc (cr,
scale_size / 2,
scale_size / 2,
scale_size / 2,
0,
2 * M_PI);
cairo_fill (cr);
gdk_cairo_set_source_pixbuf (cr, ret, 0, 0);
cairo_arc (cr,
scale_size / 2,
scale_size / 2,
scale_size / 2,
0,