1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2024-12-12 15:47:35 +00:00

BestMatch: Turn avatar into a property

This commit is contained in:
Julian Sparber 2021-01-27 16:14:31 +01:00 committed by Evangelos Ribeiro Tzaras
parent d8f2534795
commit 99e427a529
2 changed files with 28 additions and 266 deletions

View file

@ -36,12 +36,6 @@ struct _CallsBestMatch
FolksIndividual *best_match;
gulong display_name_notify_handler_id;
gulong avatar_notify_handler_id;
/** All requested gint avatar sizes */
GList *avatar_sizes;
/** GCancellables for in-progress loads */
GList *avatar_loads;
/** Map of gint icon size to GdkPixbuf */
GHashTable *avatars;
};
G_DEFINE_TYPE (CallsBestMatch, calls_best_match, G_TYPE_OBJECT);
@ -51,164 +45,11 @@ enum {
PROP_0,
PROP_VIEW,
PROP_NAME,
PROP_AVATAR,
PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];
enum {
SIGNAL_AVATAR,
SIGNAL_LAST_SIGNAL,
};
static guint signals [SIGNAL_LAST_SIGNAL];
struct CallsAvatarRequestData
{
CallsBestMatch *self;
GCancellable *cancellable;
gint size;
};
static void
avatar_request_data_destroy (struct CallsAvatarRequestData *data)
{
data->self->avatar_loads =
g_list_remove (data->self->avatar_loads,
data->cancellable);
g_free (data);
}
inline static void
add_avatar (CallsBestMatch *self,
gint size,
GdkPixbuf *avatar)
{
g_hash_table_insert (self->avatars,
GINT_TO_POINTER (size),
avatar);
g_debug ("Added avatar of size %i for best match `%s'",
size,
folks_individual_get_display_name (self->best_match));
g_signal_emit_by_name (self, "avatar", size, avatar);
}
static void
request_avatar_pixbuf_new_cb (GInputStream *stream,
GAsyncResult *res,
struct CallsAvatarRequestData *data)
{
GdkPixbuf *avatar;
GError *error = NULL;
avatar = gdk_pixbuf_new_from_stream_finish (res, &error);
if (avatar)
{
add_avatar (data->self, data->size, avatar);
}
else
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
g_warning ("Error creating GdkPixbuf from avatar"
" icon stream at size %i for Folks"
" individual `%s': %s",
data->size,
calls_best_match_get_name (data->self),
error->message);
}
g_error_free (error);
}
avatar_request_data_destroy (data);
}
static void
request_avatar_icon_load_cb (GLoadableIcon *icon,
GAsyncResult *res,
struct CallsAvatarRequestData *data)
{
GInputStream *stream;
GError *error = NULL;
stream = g_loadable_icon_load_finish (icon, res, NULL, &error);
if (!stream)
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
g_warning ("Error loading avatar icon at size %i"
" for Folks individual `%s': %s",
data->size,
calls_best_match_get_name (data->self),
error->message);
}
g_error_free (error);
avatar_request_data_destroy (data);
return;
}
gdk_pixbuf_new_from_stream_at_scale_async
(stream,
-1,
data->size,
TRUE,
data->cancellable,
(GAsyncReadyCallback)request_avatar_pixbuf_new_cb,
data);
g_object_unref (stream);
}
static void
request_avatar (CallsBestMatch *self,
gint size)
{
GLoadableIcon *icon;
struct CallsAvatarRequestData *data;
if (!self->best_match)
{
return;
}
icon = folks_avatar_details_get_avatar
(FOLKS_AVATAR_DETAILS(self->best_match));
if (!icon)
{
return;
}
g_debug ("Requesting avatar of size %i for best match `%s'",
size,
folks_individual_get_display_name (self->best_match));
data = g_new (struct CallsAvatarRequestData, 1);
data->self = self;
data->size = size;
data->cancellable = g_cancellable_new ();
self->avatar_loads = g_list_prepend
(self->avatar_loads, data->cancellable);
g_loadable_icon_load_async
(icon,
size,
data->cancellable,
(GAsyncReadyCallback)request_avatar_icon_load_cb,
data);
g_object_unref (data->cancellable);
}
static void
notify_name (CallsBestMatch *self)
{
@ -216,47 +57,13 @@ notify_name (CallsBestMatch *self)
props[PROP_NAME]);
}
static void
clear_avatars (CallsBestMatch *self)
notify_avatar (CallsBestMatch *self)
{
GList *node;
for (node = self->avatar_loads; node; node = node->next)
{
g_cancellable_cancel ((GCancellable *)node->data);
g_object_notify_by_pspec (G_OBJECT (self),
props[PROP_AVATAR]);
}
g_list_free (self->avatar_loads);
self->avatar_loads = NULL;
g_hash_table_remove_all (self->avatars);
}
static void
request_avatars (CallsBestMatch *self)
{
GList *node;
for (node = self->avatar_sizes; node; node = node->next)
{
request_avatar (self, GPOINTER_TO_INT (node->data));
}
}
static void
change_avatar (CallsBestMatch *self)
{
g_debug ("Avatar changed for best match `%s'",
folks_individual_get_display_name (self->best_match));
clear_avatars (self);
request_avatars (self);
}
static void
set_best_match (CallsBestMatch *self,
FolksIndividual *best_match)
@ -277,7 +84,7 @@ set_best_match (CallsBestMatch *self,
self->avatar_notify_handler_id =
g_signal_connect_swapped (self->best_match,
"notify::avatar",
G_CALLBACK (change_avatar),
G_CALLBACK (notify_avatar),
self);
}
@ -299,8 +106,8 @@ new_best_match (CallsBestMatch *self,
FolksIndividual *best_match)
{
set_best_match (self, best_match);
request_avatars (self);
notify_name (self);
notify_avatar (self);
}
@ -310,29 +117,17 @@ change_best_match (CallsBestMatch *self,
{
clear_best_match (self);
set_best_match (self, best_match);
change_avatar (self);
notify_name (self);
notify_avatar (self);
}
static void
remove_best_match (CallsBestMatch *self)
{
GList *node;
clear_best_match (self);
clear_avatars (self);
// Emit empty avatars
for (node = self->avatar_sizes; node; node = node->next)
{
g_signal_emit_by_name (self,
"avatar",
GPOINTER_TO_INT (node->data),
NULL);
}
notify_name (self);
notify_avatar (self);
}
@ -438,6 +233,11 @@ get_property (GObject *object,
calls_best_match_get_name (self));
break;
case PROP_AVATAR:
g_value_set_object (value,
calls_best_match_get_avatar (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
@ -450,26 +250,12 @@ dispose (GObject *object)
{
CallsBestMatch *self = CALLS_BEST_MATCH (object);
clear_avatars (self);
g_clear_object (&self->view);
G_OBJECT_CLASS (calls_best_match_parent_class)->dispose (object);
}
static void
finalize (GObject *object)
{
CallsBestMatch *self = CALLS_BEST_MATCH (object);
g_list_free (self->avatar_sizes);
g_hash_table_unref (self->avatars);
G_OBJECT_CLASS (calls_best_match_parent_class)->finalize (object);
}
static void
calls_best_match_class_init (CallsBestMatchClass *klass)
{
@ -479,8 +265,6 @@ calls_best_match_class_init (CallsBestMatchClass *klass)
object_class->constructed = constructed;
object_class->get_property = get_property;
object_class->dispose = dispose;
object_class->finalize = finalize;
props[PROP_VIEW] =
g_param_spec_object ("view",
@ -496,28 +280,21 @@ calls_best_match_class_init (CallsBestMatchClass *klass)
NULL,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
props[PROP_AVATAR] =
g_param_spec_object ("avatar",
"Avatar",
"The avatar of the best match",
G_TYPE_LOADABLE_ICON,
G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
signals[SIGNAL_AVATAR] =
g_signal_new ("avatar",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE, 2,
G_TYPE_INT,
GDK_TYPE_PIXBUF);
}
static void
calls_best_match_init (CallsBestMatch *self)
{
self->avatars = g_hash_table_new_full
(g_direct_hash,
g_direct_equal,
NULL,
(GDestroyNotify)g_object_unref);
}
@ -548,31 +325,17 @@ calls_best_match_get_name (CallsBestMatch *self)
}
GdkPixbuf *
calls_best_match_request_avatar (CallsBestMatch *self,
gint size)
GLoadableIcon *
calls_best_match_get_avatar (CallsBestMatch *self)
{
gpointer sizeptr = GINT_TO_POINTER (size);
GdkPixbuf *avatar;
g_return_val_if_fail (CALLS_IS_BEST_MATCH (self), NULL);
avatar = g_hash_table_lookup (self->avatars, sizeptr);
if (avatar)
if (self->best_match)
{
// Already loaded
return avatar;
return folks_avatar_details_get_avatar (FOLKS_AVATAR_DETAILS (self->best_match));
}
if (!g_list_find (self->avatar_sizes, sizeptr))
else
{
// Not known, do the actual request
request_avatar (self, size);
// Add the size to the list
self->avatar_sizes = g_list_prepend
(self->avatar_sizes, sizeptr);
}
return NULL;
}
}

View file

@ -37,8 +37,7 @@ G_DECLARE_FINAL_TYPE (CallsBestMatch, calls_best_match, CALLS, BEST_MATCH, GObje
CallsBestMatch *calls_best_match_new (CallsBestMatchView *view);
const gchar * calls_best_match_get_name (CallsBestMatch *self);
GdkPixbuf * calls_best_match_request_avatar (CallsBestMatch *self,
gint size);
GLoadableIcon *calls_best_match_get_avatar (CallsBestMatch *self);
G_END_DECLS