Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : *
3 : : * Copyright © 2022 Endless Mobile, Inc.
4 : : * Copyright © 2015 Red Hat, Inc.
5 : : *
6 : : * SPDX-License-Identifier: GPL-2.0-or-later
7 : : *
8 : : * This program is free software; you can redistribute it and/or modify
9 : : * it under the terms of the GNU General Public License as published by
10 : : * the Free Software Foundation; either version 2 of the License, or
11 : : * (at your option) any later version.
12 : : *
13 : : * This program is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : * GNU General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU General Public License
19 : : * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 : : *
21 : : * Authors:
22 : : * - Georges Basile Stavracas Neto <georges@endlessos.org>
23 : : * - Ondrej Holy <oholy@redhat.com>
24 : : */
25 : :
26 : : #include <adwaita.h>
27 : : #include <libmalcontent/malcontent.h>
28 : : #include <sys/stat.h>
29 : :
30 : : #include "user-image.h"
31 : :
32 : :
33 : : struct _MctUserImage
34 : : {
35 : : AdwBin parent_instance;
36 : :
37 : : AdwAvatar *avatar;
38 : :
39 : : MctUser *user; /* (owned) */
40 : : unsigned long user_notify_id;
41 : : };
42 : :
43 [ # # # # : 0 : G_DEFINE_TYPE (MctUserImage, mct_user_image, ADW_TYPE_BIN)
# # ]
44 : :
45 : : static GdkTexture *
46 : 0 : render_user_icon_texture (MctUser *user)
47 : : {
48 : 0 : g_autoptr(GdkTexture) texture = NULL;
49 : 0 : g_autoptr(GError) error = NULL;
50 : : const gchar *icon_file;
51 : :
52 : 0 : g_return_val_if_fail (MCT_IS_USER (user), NULL);
53 : :
54 : 0 : icon_file = mct_user_get_icon_path (user);
55 [ # # ]: 0 : if (icon_file == NULL)
56 : 0 : return NULL;
57 : :
58 : 0 : texture = gdk_texture_new_from_filename (icon_file, &error);
59 [ # # ]: 0 : if (error != NULL)
60 : : {
61 : 0 : g_warning ("Error loading user icon: %s", error->message);
62 : 0 : return NULL;
63 : : }
64 : :
65 : 0 : return g_steal_pointer (&texture);
66 : : }
67 : :
68 : : static void
69 : 0 : render_image (MctUserImage *image)
70 : : {
71 [ # # ]: 0 : g_autoptr(GdkTexture) texture = NULL;
72 : :
73 [ # # ]: 0 : if (image->user == NULL)
74 : 0 : return;
75 : :
76 : 0 : texture = render_user_icon_texture (image->user);
77 : 0 : adw_avatar_set_custom_image (image->avatar, GDK_PAINTABLE (texture));
78 : 0 : adw_avatar_set_text (image->avatar, mct_user_get_display_name (image->user));
79 : : }
80 : :
81 : : static void
82 : 0 : user_notify_cb (GObject *object,
83 : : GParamSpec *pspec,
84 : : void *user_data)
85 : : {
86 : 0 : MctUserImage *image = MCT_USER_IMAGE (user_data);
87 : :
88 : : /* icon-path or display-name may have changed */
89 : 0 : render_image (image);
90 : 0 : }
91 : :
92 : : void
93 : 0 : mct_user_image_set_user (MctUserImage *image,
94 : : MctUser *user)
95 : : {
96 [ # # ]: 0 : g_clear_signal_handler (&image->user_notify_id, image->user);
97 [ # # ]: 0 : g_clear_object (&image->user);
98 : 0 : image->user = g_object_ref (user);
99 : 0 : image->user_notify_id = g_signal_connect (user, "notify",
100 : : G_CALLBACK (user_notify_cb), image);
101 : :
102 : 0 : render_image (image);
103 : 0 : }
104 : :
105 : : void
106 : 0 : mct_user_image_set_size (MctUserImage *image,
107 : : int size)
108 : : {
109 : 0 : g_return_if_fail (MCT_IS_USER_IMAGE (image));
110 : :
111 : 0 : adw_avatar_set_size (image->avatar, size);
112 : : }
113 : :
114 : : static void
115 : 0 : mct_user_image_dispose (GObject *object)
116 : : {
117 : 0 : MctUserImage *image = MCT_USER_IMAGE (object);
118 : :
119 [ # # ]: 0 : g_clear_signal_handler (&image->user_notify_id, image->user);
120 [ # # ]: 0 : g_clear_object (&image->user);
121 : :
122 : 0 : G_OBJECT_CLASS (mct_user_image_parent_class)->dispose (object);
123 : 0 : }
124 : :
125 : : static void
126 : 0 : mct_user_image_class_init (MctUserImageClass *class)
127 : : {
128 : 0 : GObjectClass *object_class = G_OBJECT_CLASS (class);
129 : :
130 : 0 : object_class->dispose = mct_user_image_dispose;
131 : 0 : }
132 : :
133 : : static void
134 : 0 : mct_user_image_init (MctUserImage *image)
135 : : {
136 : 0 : image->avatar = ADW_AVATAR (adw_avatar_new (48, NULL, TRUE));
137 : 0 : adw_bin_set_child (ADW_BIN (image), GTK_WIDGET (image->avatar));
138 : 0 : }
139 : :
140 : : GtkWidget *
141 : 0 : mct_user_image_new (void)
142 : : {
143 : 0 : return g_object_new (MCT_TYPE_USER_IMAGE, NULL);
144 : : }
|