summaryrefslogtreecommitdiff
path: root/src/greeteruserimage.c
blob: 5ce7ad419a9e8c58d448922ccac735ac10578676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2010 - 2011, Robert Ancell <robert.ancell@canonical.com>
 * Copyright (C) 2011, Gunnar Hjalmarsson <ubuntu@gunnar.cc>
 * Copyright (C) 2012 - 2013, Lionel Le Folgoc <mrpouit@ubuntu.com>
 * Copyright (C) 2012, Julien Lavergne <gilir@ubuntu.com>
 * Copyright (C) 2013 - 2015, Simon Steinbeiß <ochosi@shimmerproject.org>
 * Copyright (C) 2013 - 2020, Sean Davis <sean@bluesabre.org>
 * Copyright (C) 2014, Andrew P. <pan.pav.7c5@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <lightdm.h>

#include "greeterconfiguration.h"
#include "greeteruserimage.h"

#define USER_IMAGE_SIZE 80

static GdkPixbuf *
round_image (GdkPixbuf *pixbuf)
{
    GdkPixbuf *dest = NULL;
    cairo_surface_t *surface;
    cairo_t *cr;
    gint size;

    size = gdk_pixbuf_get_width (pixbuf);
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size, size);
    cr = cairo_create (surface);

    /* Clip a circle */
    cairo_arc (cr, size/2, size/2, size/2, 0, 2 * G_PI);
    cairo_clip (cr);
    cairo_new_path (cr);

    gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
    cairo_paint (cr);

    dest = gdk_pixbuf_get_from_surface (surface, 0, 0, size, size);
    cairo_surface_destroy (surface);
    cairo_destroy (cr);

    return dest;
}

static GdkPixbuf *
get_default_user_image_from_settings (void)
{
    GdkPixbuf *image = NULL;
    gchar *value = NULL;
    GError *error = NULL;

    value = config_get_string (NULL, CONFIG_KEY_DEFAULT_USER_IMAGE, NULL);
    if (!value)
        return NULL;

    if (value[0] == '/')
    {
        image = gdk_pixbuf_new_from_file_at_scale (value,
                                                   USER_IMAGE_SIZE,
                                                   USER_IMAGE_SIZE,
                                                   FALSE,
                                                   &error);

        if (!image)
        {
            g_warning ("Failed to load default user image from path: %s", error->message);
            g_clear_error (&error);
        }
    }
    else if (value[0] == '#')
    {
        image = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                          value + 1,
                                          USER_IMAGE_SIZE,
                                          GTK_ICON_LOOKUP_FORCE_SIZE,
                                          &error);

        if (!image)
        {
            g_warning ("Failed to load default user image from icon theme: %s", error->message);
            g_clear_error (&error);
        }
    }

    g_free (value);

    return image;
}

GdkPixbuf *
get_default_user_image (void)
{
    GdkPixbuf *temp_image = NULL, *image = NULL;
    GError *error = NULL;

    /* If a file is set by preferences, it must be prioritized. */
    image = get_default_user_image_from_settings ();

    /* Fallback to avatar-default icon from theme */
    if (!image)
    {
        image = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                          "avatar-default",
                                          USER_IMAGE_SIZE,
                                          GTK_ICON_LOOKUP_FORCE_SIZE,
                                          &error);

        if (error != NULL)
        {
            g_warning ("Failed to load fallback user image: %s", error->message);
            g_clear_error (&error);
        }
    }

    /* Fallback again to old stock_person icon from theme */
    if (!image)
    {
        image = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                          "stock_person",
                                          USER_IMAGE_SIZE,
                                          GTK_ICON_LOOKUP_FORCE_SIZE,
                                          &error);

        if (error != NULL)
        {
            g_warning ("Failed to load old fallback user image: %s", error->message);
            g_clear_error (&error);
        }
    }

    if (image && config_get_bool (NULL, CONFIG_KEY_ROUND_USER_IMAGE, TRUE))
    {
        temp_image = round_image (image);
        if (temp_image != NULL)
        {
            g_object_unref (image);
            image = temp_image;
        }
    }

    return image;
}

GdkPixbuf *
get_user_image (const gchar *username)
{
    LightDMUser *user = NULL;
    GdkPixbuf *temp_image = NULL, *image = NULL;
    GError *error = NULL;
    const gchar *path;

    user = lightdm_user_list_get_user_by_name (lightdm_user_list_get_instance (), username);
    if (user)
    {
        path = lightdm_user_get_image (user);
        if (path)
        {
            image = gdk_pixbuf_new_from_file_at_scale (path,
                                                       USER_IMAGE_SIZE,
                                                       USER_IMAGE_SIZE,
                                                       FALSE,
                                                       &error);
            if (!image)
            {
                g_debug ("Failed to load user image: %s", error->message);
                g_clear_error (&error);
            }
        }
    }

    if (image && config_get_bool (NULL, CONFIG_KEY_ROUND_USER_IMAGE, TRUE))
    {
        temp_image = round_image (image);
        if (temp_image != NULL)
        {
            g_object_unref (image);
            image = temp_image;
        }
    }

    return image;
}