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 <act/act.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 : : ActUser *user;
40 : : };
41 : :
42 [ # # # # : 0 : G_DEFINE_TYPE (MctUserImage, mct_user_image, ADW_TYPE_BIN)
# # ]
43 : :
44 : : static GdkTexture *
45 : 0 : render_user_icon_texture (ActUser *user)
46 : : {
47 : 0 : g_autoptr(GdkTexture) texture = NULL;
48 : 0 : g_autoptr(GError) error = NULL;
49 : : const gchar *icon_file;
50 : :
51 : 0 : g_return_val_if_fail (ACT_IS_USER (user), NULL);
52 : :
53 : 0 : icon_file = act_user_get_icon_file (user);
54 [ # # ]: 0 : if (icon_file == NULL)
55 : 0 : return NULL;
56 : :
57 : 0 : texture = gdk_texture_new_from_filename (icon_file, &error);
58 [ # # ]: 0 : if (error != NULL)
59 : : {
60 : 0 : g_warning ("Error loading user icon: %s", error->message);
61 : 0 : return NULL;
62 : : }
63 : :
64 : 0 : return g_steal_pointer (&texture);
65 : : }
66 : :
67 : : static void
68 : 0 : render_image (MctUserImage *image)
69 : : {
70 [ # # ]: 0 : g_autoptr(GdkTexture) texture = NULL;
71 : :
72 [ # # ]: 0 : if (image->user == NULL)
73 : 0 : return;
74 : :
75 : 0 : texture = render_user_icon_texture (image->user);
76 : 0 : adw_avatar_set_custom_image (image->avatar, GDK_PAINTABLE (texture));
77 : 0 : adw_avatar_set_text (image->avatar, act_user_get_real_name (image->user));
78 : : }
79 : :
80 : : void
81 : 0 : mct_user_image_set_user (MctUserImage *image,
82 : : ActUser *user)
83 : : {
84 [ # # ]: 0 : g_clear_object (&image->user);
85 : 0 : image->user = g_object_ref (user);
86 : :
87 : 0 : render_image (image);
88 : 0 : }
89 : :
90 : : static void
91 : 0 : mct_user_image_finalize (GObject *object)
92 : : {
93 : 0 : MctUserImage *image = MCT_USER_IMAGE (object);
94 : :
95 [ # # ]: 0 : g_clear_object (&image->user);
96 : :
97 : 0 : G_OBJECT_CLASS (mct_user_image_parent_class)->finalize (object);
98 : 0 : }
99 : :
100 : : static void
101 : 0 : mct_user_image_class_init (MctUserImageClass *class)
102 : : {
103 : 0 : GObjectClass *object_class = G_OBJECT_CLASS (class);
104 : :
105 : 0 : object_class->finalize = mct_user_image_finalize;
106 : 0 : }
107 : :
108 : : static void
109 : 0 : mct_user_image_init (MctUserImage *image)
110 : : {
111 : 0 : image->avatar = ADW_AVATAR (adw_avatar_new (48, NULL, TRUE));
112 : 0 : adw_bin_set_child (ADW_BIN (image), GTK_WIDGET (image->avatar));
113 : 0 : }
114 : :
115 : : GtkWidget *
116 : 0 : mct_user_image_new (void)
117 : : {
118 : 0 : return g_object_new (MCT_TYPE_USER_IMAGE, NULL);
119 : : }
|