~jamesodhunt/ubuntu/natty/gdm/fix-for-gdm-upstart-conf-lp706842

« back to all changes in this revision

Viewing changes to .pc/90_git_name_escaping.patch/gui/simple-greeter/gdm-user-chooser-widget.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-09-08 17:37:51 UTC
  • Revision ID: james.westby@ubuntu.com-20100908173751-5kq3dawu9pbr01mf
Tags: 2.30.5-0ubuntu3
* debian/patches/90_git_name_escaping.patch:
  - git change to correctly escape usernames (lp: #633050)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 
4
 * Copyright (C) 2007 Ray Strode <rstrode@redhat.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <unistd.h>
 
27
#include <string.h>
 
28
#include <errno.h>
 
29
#include <dirent.h>
 
30
#include <stdarg.h>
 
31
#include <sys/stat.h>
 
32
 
 
33
#include <glib.h>
 
34
#include <glib/gi18n.h>
 
35
#include <glib/gstdio.h>
 
36
#include <gtk/gtk.h>
 
37
 
 
38
#include <gconf/gconf-client.h>
 
39
 
 
40
#include "gdm-user-manager.h"
 
41
#include "gdm-user-chooser-widget.h"
 
42
#include "gdm-settings-keys.h"
 
43
#include "gdm-settings-client.h"
 
44
 
 
45
#define KEY_DISABLE_USER_LIST "/apps/gdm/simple-greeter/disable_user_list"
 
46
 
 
47
enum {
 
48
        USER_NO_DISPLAY              = 1 << 0,
 
49
        USER_ACCOUNT_DISABLED        = 1 << 1,
 
50
};
 
51
 
 
52
#define DEFAULT_USER_ICON "avatar-default"
 
53
#define OLD_DEFAULT_USER_ICON "stock_person"
 
54
 
 
55
#define GDM_USER_CHOOSER_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_USER_CHOOSER_WIDGET, GdmUserChooserWidgetPrivate))
 
56
 
 
57
#define MAX_ICON_SIZE 128
 
58
#define NUM_USERS_TO_ADD_PER_ITERATION 50
 
59
 
 
60
struct GdmUserChooserWidgetPrivate
 
61
{
 
62
        GdmUserManager *manager;
 
63
        GtkIconTheme   *icon_theme;
 
64
 
 
65
        GSList         *users_to_add;
 
66
 
 
67
        GdkPixbuf      *logged_in_pixbuf;
 
68
        GdkPixbuf      *stock_person_pixbuf;
 
69
 
 
70
        guint           loaded : 1;
 
71
        guint           show_user_other : 1;
 
72
        guint           show_user_guest : 1;
 
73
        guint           show_user_auto : 1;
 
74
        guint           show_normal_users : 1;
 
75
 
 
76
        guint           has_user_other : 1;
 
77
 
 
78
        guint           update_other_visibility_idle_id;
 
79
        guint           load_idle_id;
 
80
        guint           add_users_idle_id;
 
81
};
 
82
 
 
83
enum {
 
84
        PROP_0,
 
85
        PROP_SHOW_USER_GUEST,
 
86
        PROP_SHOW_USER_AUTO,
 
87
        PROP_SHOW_USER_OTHER,
 
88
};
 
89
 
 
90
static void     gdm_user_chooser_widget_class_init  (GdmUserChooserWidgetClass *klass);
 
91
static void     gdm_user_chooser_widget_init        (GdmUserChooserWidget      *user_chooser_widget);
 
92
static void     gdm_user_chooser_widget_finalize    (GObject                   *object);
 
93
 
 
94
G_DEFINE_TYPE (GdmUserChooserWidget, gdm_user_chooser_widget, GDM_TYPE_CHOOSER_WIDGET)
 
95
 
 
96
static void     add_user_other    (GdmUserChooserWidget *widget);
 
97
static void     remove_user_other (GdmUserChooserWidget *widget);
 
98
 
 
99
static int
 
100
get_font_height_for_widget (GtkWidget *widget)
 
101
{
 
102
        PangoFontMetrics *metrics;
 
103
        PangoContext     *context;
 
104
        int               ascent;
 
105
        int               descent;
 
106
        int               height;
 
107
 
 
108
        gtk_widget_ensure_style (widget);
 
109
        context = gtk_widget_get_pango_context (widget);
 
110
        metrics = pango_context_get_metrics (context,
 
111
                                             widget->style->font_desc,
 
112
                                             pango_context_get_language (context));
 
113
 
 
114
        ascent = pango_font_metrics_get_ascent (metrics);
 
115
        descent = pango_font_metrics_get_descent (metrics);
 
116
        height = PANGO_PIXELS (ascent + descent);
 
117
        pango_font_metrics_unref (metrics);
 
118
        return height;
 
119
}
 
120
 
 
121
static int
 
122
get_icon_height_for_widget (GtkWidget *widget)
 
123
{
 
124
        int font_height;
 
125
        int height;
 
126
 
 
127
        font_height = get_font_height_for_widget (widget);
 
128
        height = 3 * font_height;
 
129
        if (height > MAX_ICON_SIZE) {
 
130
                height = MAX_ICON_SIZE;
 
131
        }
 
132
 
 
133
        g_debug ("GdmUserChooserWidget: font height %d; using icon size %d", font_height, height);
 
134
 
 
135
        return height;
 
136
}
 
137
 
 
138
static gboolean
 
139
update_other_user_visibility (GdmUserChooserWidget *widget)
 
140
{
 
141
        int number_of_users;
 
142
 
 
143
        g_debug ("GdmUserChooserWidget: updating other user visibility");
 
144
 
 
145
        if (!widget->priv->show_user_other) {
 
146
                if (widget->priv->has_user_other) {
 
147
                        remove_user_other (widget);
 
148
                }
 
149
 
 
150
                goto out;
 
151
        }
 
152
 
 
153
        number_of_users = gdm_chooser_widget_get_number_of_items (GDM_CHOOSER_WIDGET (widget));
 
154
 
 
155
        /* we hide the Other user if it's the last one, and we show it
 
156
         * if there's another user */
 
157
        if (number_of_users == 1 && widget->priv->has_user_other) {
 
158
                remove_user_other (widget);
 
159
        } if (number_of_users >= 1 && !widget->priv->has_user_other) {
 
160
                add_user_other (widget);
 
161
        }
 
162
 
 
163
 out:
 
164
        widget->priv->update_other_visibility_idle_id = 0;
 
165
        return FALSE;
 
166
}
 
167
 
 
168
static void
 
169
queue_update_other_user_visibility (GdmUserChooserWidget *widget)
 
170
{
 
171
        if (widget->priv->update_other_visibility_idle_id == 0) {
 
172
                widget->priv->update_other_visibility_idle_id =
 
173
                        g_idle_add ((GSourceFunc) update_other_user_visibility, widget);
 
174
        }
 
175
}
 
176
 
 
177
static void
 
178
update_item_for_user (GdmUserChooserWidget *widget,
 
179
                      GdmUser              *user)
 
180
{
 
181
        GdkPixbuf    *pixbuf;
 
182
        char         *tooltip;
 
183
        gboolean      is_logged_in;
 
184
        int           size;
 
185
 
 
186
 
 
187
        size = get_icon_height_for_widget (GTK_WIDGET (widget));
 
188
        pixbuf = gdm_user_render_icon (user, size);
 
189
 
 
190
        if (pixbuf == NULL && widget->priv->stock_person_pixbuf != NULL) {
 
191
                pixbuf = g_object_ref (widget->priv->stock_person_pixbuf);
 
192
        }
 
193
 
 
194
        tooltip = g_strdup_printf (_("Log in as %s"),
 
195
                                   gdm_user_get_user_name (user));
 
196
 
 
197
        is_logged_in = gdm_user_is_logged_in (user);
 
198
 
 
199
        g_debug ("GdmUserChooserWidget: User added name:%s logged-in:%d pixbuf:%p",
 
200
                 gdm_user_get_user_name (user),
 
201
                 is_logged_in,
 
202
                 pixbuf);
 
203
 
 
204
        gdm_chooser_widget_update_item (GDM_CHOOSER_WIDGET (widget),
 
205
                                        gdm_user_get_user_name (user),
 
206
                                        pixbuf,
 
207
                                        gdm_user_get_real_name (user),
 
208
                                        tooltip,
 
209
                                        gdm_user_get_login_frequency (user),
 
210
                                        is_logged_in,
 
211
                                        FALSE);
 
212
        g_free (tooltip);
 
213
 
 
214
        if (pixbuf != NULL) {
 
215
                g_object_unref (pixbuf);
 
216
        }
 
217
}
 
218
 
 
219
static void
 
220
on_item_load (GdmChooserWidget     *widget,
 
221
              const char           *id,
 
222
              GdmUserChooserWidget *user_chooser)
 
223
{
 
224
        GdmUser *user;
 
225
 
 
226
        g_debug ("GdmUserChooserWidget: Loading item for id=%s", id);
 
227
 
 
228
        if (user_chooser->priv->manager == NULL) {
 
229
                return;
 
230
        }
 
231
 
 
232
        user = gdm_user_manager_get_user (user_chooser->priv->manager, id);
 
233
        if (user != NULL) {
 
234
                update_item_for_user (user_chooser, user);
 
235
        }
 
236
}
 
237
 
 
238
static void
 
239
add_user_other (GdmUserChooserWidget *widget)
 
240
{
 
241
        widget->priv->has_user_other = TRUE;
 
242
        gdm_chooser_widget_add_item (GDM_CHOOSER_WIDGET (widget),
 
243
                                     GDM_USER_CHOOSER_USER_OTHER,
 
244
                                     NULL,
 
245
                                     /* translators: This option prompts
 
246
                                      * the user to type in a username
 
247
                                      * manually instead of choosing from
 
248
                                      * a list.
 
249
                                      */
 
250
                                     C_("user", "Other..."),
 
251
                                     _("Choose a different account"),
 
252
                                     0,
 
253
                                     FALSE,
 
254
                                     TRUE,
 
255
                                     (GdmChooserWidgetItemLoadFunc) on_item_load,
 
256
                                     widget);
 
257
}
 
258
 
 
259
static void
 
260
add_user_guest (GdmUserChooserWidget *widget)
 
261
{
 
262
        gdm_chooser_widget_add_item (GDM_CHOOSER_WIDGET (widget),
 
263
                                     GDM_USER_CHOOSER_USER_GUEST,
 
264
                                     widget->priv->stock_person_pixbuf,
 
265
                                     _("Guest"),
 
266
                                     _("Login as a temporary guest"),
 
267
                                     0,
 
268
                                     FALSE,
 
269
                                     TRUE,
 
270
                                     (GdmChooserWidgetItemLoadFunc) on_item_load,
 
271
                                     widget);
 
272
        queue_update_other_user_visibility (widget);
 
273
}
 
274
 
 
275
static void
 
276
add_user_auto (GdmUserChooserWidget *widget)
 
277
{
 
278
        gdm_chooser_widget_add_item (GDM_CHOOSER_WIDGET (widget),
 
279
                                     GDM_USER_CHOOSER_USER_AUTO,
 
280
                                     NULL,
 
281
                                     _("Automatic Login"),
 
282
                                     _("Automatically login to the system after selecting options"),
 
283
                                     0,
 
284
                                     FALSE,
 
285
                                     TRUE,
 
286
                                     (GdmChooserWidgetItemLoadFunc) on_item_load,
 
287
                                     widget);
 
288
        queue_update_other_user_visibility (widget);
 
289
}
 
290
 
 
291
static void
 
292
remove_user_other (GdmUserChooserWidget *widget)
 
293
{
 
294
        widget->priv->has_user_other = FALSE;
 
295
        gdm_chooser_widget_remove_item (GDM_CHOOSER_WIDGET (widget),
 
296
                                        GDM_USER_CHOOSER_USER_OTHER);
 
297
}
 
298
 
 
299
static void
 
300
remove_user_guest (GdmUserChooserWidget *widget)
 
301
{
 
302
        gdm_chooser_widget_remove_item (GDM_CHOOSER_WIDGET (widget),
 
303
                                        GDM_USER_CHOOSER_USER_GUEST);
 
304
        queue_update_other_user_visibility (widget);
 
305
}
 
306
 
 
307
static void
 
308
remove_user_auto (GdmUserChooserWidget *widget)
 
309
{
 
310
        gdm_chooser_widget_remove_item (GDM_CHOOSER_WIDGET (widget),
 
311
                                        GDM_USER_CHOOSER_USER_AUTO);
 
312
        queue_update_other_user_visibility (widget);
 
313
}
 
314
 
 
315
void
 
316
gdm_user_chooser_widget_set_show_user_other (GdmUserChooserWidget *widget,
 
317
                                             gboolean              show_user)
 
318
{
 
319
        g_return_if_fail (GDM_IS_USER_CHOOSER_WIDGET (widget));
 
320
 
 
321
        if (widget->priv->show_user_other != show_user) {
 
322
                widget->priv->show_user_other = show_user;
 
323
                queue_update_other_user_visibility (widget);
 
324
        }
 
325
}
 
326
 
 
327
void
 
328
gdm_user_chooser_widget_set_show_user_guest (GdmUserChooserWidget *widget,
 
329
                                             gboolean              show_user)
 
330
{
 
331
        g_return_if_fail (GDM_IS_USER_CHOOSER_WIDGET (widget));
 
332
 
 
333
        if (widget->priv->show_user_guest != show_user) {
 
334
                widget->priv->show_user_guest = show_user;
 
335
                if (show_user) {
 
336
                        add_user_guest (widget);
 
337
                } else {
 
338
                        remove_user_guest (widget);
 
339
                }
 
340
        }
 
341
}
 
342
 
 
343
void
 
344
gdm_user_chooser_widget_set_show_user_auto (GdmUserChooserWidget *widget,
 
345
                                            gboolean              show_user)
 
346
{
 
347
        g_return_if_fail (GDM_IS_USER_CHOOSER_WIDGET (widget));
 
348
 
 
349
        if (widget->priv->show_user_auto != show_user) {
 
350
                widget->priv->show_user_auto = show_user;
 
351
                if (show_user) {
 
352
                        add_user_auto (widget);
 
353
                } else {
 
354
                        remove_user_auto (widget);
 
355
                }
 
356
        }
 
357
}
 
358
 
 
359
char *
 
360
gdm_user_chooser_widget_get_chosen_user_name (GdmUserChooserWidget *widget)
 
361
{
 
362
        g_return_val_if_fail (GDM_IS_USER_CHOOSER_WIDGET (widget), NULL);
 
363
 
 
364
        return gdm_chooser_widget_get_active_item (GDM_CHOOSER_WIDGET (widget));
 
365
}
 
366
 
 
367
void
 
368
gdm_user_chooser_widget_set_chosen_user_name (GdmUserChooserWidget *widget,
 
369
                                              const char           *name)
 
370
{
 
371
        g_return_if_fail (GDM_IS_USER_CHOOSER_WIDGET (widget));
 
372
 
 
373
        gdm_chooser_widget_set_active_item (GDM_CHOOSER_WIDGET (widget), name);
 
374
}
 
375
 
 
376
void
 
377
gdm_user_chooser_widget_set_show_only_chosen (GdmUserChooserWidget *widget,
 
378
                                              gboolean              show_only) {
 
379
        g_return_if_fail (GDM_IS_USER_CHOOSER_WIDGET (widget));
 
380
 
 
381
        gdm_chooser_widget_set_hide_inactive_items (GDM_CHOOSER_WIDGET (widget),
 
382
                                                    show_only);
 
383
 
 
384
}
 
385
static void
 
386
gdm_user_chooser_widget_set_property (GObject        *object,
 
387
                                      guint           prop_id,
 
388
                                      const GValue   *value,
 
389
                                      GParamSpec     *pspec)
 
390
{
 
391
        GdmUserChooserWidget *self;
 
392
 
 
393
        self = GDM_USER_CHOOSER_WIDGET (object);
 
394
 
 
395
        switch (prop_id) {
 
396
        case PROP_SHOW_USER_AUTO:
 
397
                gdm_user_chooser_widget_set_show_user_auto (self, g_value_get_boolean (value));
 
398
                break;
 
399
        case PROP_SHOW_USER_GUEST:
 
400
                gdm_user_chooser_widget_set_show_user_guest (self, g_value_get_boolean (value));
 
401
                break;
 
402
        case PROP_SHOW_USER_OTHER:
 
403
                gdm_user_chooser_widget_set_show_user_other (self, g_value_get_boolean (value));
 
404
                break;
 
405
        default:
 
406
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
407
                break;
 
408
        }
 
409
}
 
410
 
 
411
static void
 
412
gdm_user_chooser_widget_get_property (GObject        *object,
 
413
                                      guint           prop_id,
 
414
                                      GValue         *value,
 
415
                                      GParamSpec     *pspec)
 
416
{
 
417
        GdmUserChooserWidget *self;
 
418
 
 
419
        self = GDM_USER_CHOOSER_WIDGET (object);
 
420
 
 
421
        switch (prop_id) {
 
422
        case PROP_SHOW_USER_AUTO:
 
423
                g_value_set_boolean (value, self->priv->show_user_auto);
 
424
                break;
 
425
        case PROP_SHOW_USER_GUEST:
 
426
                g_value_set_boolean (value, self->priv->show_user_guest);
 
427
                break;
 
428
        case PROP_SHOW_USER_OTHER:
 
429
                g_value_set_boolean (value, self->priv->show_user_other);
 
430
                break;
 
431
        default:
 
432
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
433
                break;
 
434
        }
 
435
}
 
436
 
 
437
static gboolean
 
438
is_user_list_disabled (GdmUserChooserWidget *widget)
 
439
{
 
440
        GConfClient *client;
 
441
        GError      *error;
 
442
        gboolean     result;
 
443
 
 
444
        client = gconf_client_get_default ();
 
445
        error = NULL;
 
446
        result = gconf_client_get_bool (client, KEY_DISABLE_USER_LIST, &error);
 
447
        if (error != NULL) {
 
448
                g_debug ("GdmUserChooserWidget: unable to get disable-user-list configuration: %s", error->message);
 
449
                g_error_free (error);
 
450
        }
 
451
        g_object_unref (client);
 
452
 
 
453
        return result;
 
454
}
 
455
 
 
456
static void
 
457
add_user (GdmUserChooserWidget *widget,
 
458
          GdmUser              *user)
 
459
{
 
460
        GdkPixbuf    *pixbuf;
 
461
        char         *tooltip;
 
462
        gboolean      is_logged_in;
 
463
        char         *escaped_username;
 
464
        char         *escaped_real_name;
 
465
 
 
466
        if (!widget->priv->show_normal_users) {
 
467
                return;
 
468
        }
 
469
 
 
470
        pixbuf = g_object_ref (widget->priv->stock_person_pixbuf);
 
471
 
 
472
        tooltip = g_strdup_printf (_("Log in as %s"),
 
473
                                   gdm_user_get_user_name (user));
 
474
 
 
475
        is_logged_in = gdm_user_is_logged_in (user);
 
476
 
 
477
        escaped_username = g_markup_escape_text (gdm_user_get_user_name (user), -1);
 
478
        escaped_real_name = g_markup_escape_text (gdm_user_get_real_name (user), -1);
 
479
        gdm_chooser_widget_add_item (GDM_CHOOSER_WIDGET (widget),
 
480
                                     escaped_username,
 
481
                                     pixbuf,
 
482
                                     escaped_real_name,
 
483
                                     tooltip,
 
484
                                     gdm_user_get_login_frequency (user),
 
485
                                     is_logged_in,
 
486
                                     FALSE,
 
487
                                     (GdmChooserWidgetItemLoadFunc) on_item_load,
 
488
                                     widget);
 
489
        g_free (escaped_real_name);
 
490
        g_free (escaped_username);
 
491
        g_free (tooltip);
 
492
 
 
493
        if (pixbuf != NULL) {
 
494
                g_object_unref (pixbuf);
 
495
        }
 
496
 
 
497
        queue_update_other_user_visibility (widget);
 
498
}
 
499
 
 
500
static void
 
501
on_user_added (GdmUserManager       *manager,
 
502
               GdmUser              *user,
 
503
               GdmUserChooserWidget *widget)
 
504
{
 
505
        /* wait for all users to be loaded */
 
506
        if (! widget->priv->loaded) {
 
507
                return;
 
508
        }
 
509
        add_user (widget, user);
 
510
}
 
511
 
 
512
static void
 
513
on_user_removed (GdmUserManager       *manager,
 
514
                 GdmUser              *user,
 
515
                 GdmUserChooserWidget *widget)
 
516
{
 
517
        const char *user_name;
 
518
 
 
519
        g_debug ("GdmUserChooserWidget: User removed: %s", gdm_user_get_user_name (user));
 
520
        /* wait for all users to be loaded */
 
521
        if (! widget->priv->loaded) {
 
522
                return;
 
523
        }
 
524
 
 
525
        user_name = gdm_user_get_user_name (user);
 
526
 
 
527
        gdm_chooser_widget_remove_item (GDM_CHOOSER_WIDGET (widget),
 
528
                                        user_name);
 
529
 
 
530
        queue_update_other_user_visibility (widget);
 
531
}
 
532
 
 
533
static void
 
534
on_user_is_logged_in_changed (GdmUserManager       *manager,
 
535
                              GdmUser              *user,
 
536
                              GdmUserChooserWidget *widget)
 
537
{
 
538
        const char *user_name;
 
539
        gboolean    is_logged_in;
 
540
 
 
541
        g_debug ("GdmUserChooserWidget: User logged in changed: %s", gdm_user_get_user_name (user));
 
542
 
 
543
        user_name = gdm_user_get_user_name (user);
 
544
        is_logged_in = gdm_user_is_logged_in (user);
 
545
 
 
546
        gdm_chooser_widget_set_item_in_use (GDM_CHOOSER_WIDGET (widget),
 
547
                                            user_name,
 
548
                                            is_logged_in);
 
549
}
 
550
 
 
551
static void
 
552
on_user_changed (GdmUserManager       *manager,
 
553
                 GdmUser              *user,
 
554
                 GdmUserChooserWidget *widget)
 
555
{
 
556
        /* wait for all users to be loaded */
 
557
        if (! widget->priv->loaded) {
 
558
                return;
 
559
        }
 
560
        if (! widget->priv->show_normal_users) {
 
561
                return;
 
562
        }
 
563
 
 
564
        update_item_for_user (widget, user);
 
565
}
 
566
 
 
567
static gboolean
 
568
add_users (GdmUserChooserWidget *widget)
 
569
{
 
570
        guint cnt;
 
571
 
 
572
        cnt = 0;
 
573
        while (widget->priv->users_to_add != NULL && cnt < NUM_USERS_TO_ADD_PER_ITERATION) {
 
574
                add_user (widget, widget->priv->users_to_add->data);
 
575
                g_object_unref (widget->priv->users_to_add->data);
 
576
                widget->priv->users_to_add = g_slist_delete_link (widget->priv->users_to_add, widget->priv->users_to_add);
 
577
                cnt++;
 
578
        }
 
579
        g_debug ("GdmUserChooserWidget: added %u items", cnt);
 
580
 
 
581
        if (! widget->priv->loaded) {
 
582
                widget->priv->loaded = TRUE;
 
583
 
 
584
                gdm_chooser_widget_loaded (GDM_CHOOSER_WIDGET (widget));
 
585
        }
 
586
 
 
587
        return (widget->priv->users_to_add != NULL);
 
588
}
 
589
 
 
590
static void
 
591
queue_add_users (GdmUserChooserWidget *widget)
 
592
{
 
593
        if (widget->priv->add_users_idle_id == 0) {
 
594
                widget->priv->add_users_idle_id = g_idle_add ((GSourceFunc) add_users, widget);
 
595
        }
 
596
}
 
597
 
 
598
static void
 
599
on_is_loaded_changed (GdmUserManager       *manager,
 
600
                      GParamSpec           *pspec,
 
601
                      GdmUserChooserWidget *widget)
 
602
{
 
603
        GSList *users;
 
604
 
 
605
        /* FIXME: handle is-loaded=FALSE */
 
606
 
 
607
        g_debug ("GdmUserChooserWidget: Users loaded");
 
608
 
 
609
        users = gdm_user_manager_list_users (manager);
 
610
        g_slist_foreach (users, (GFunc) g_object_ref, NULL);
 
611
        widget->priv->users_to_add = g_slist_concat (widget->priv->users_to_add, g_slist_copy (users));
 
612
 
 
613
        queue_add_users (widget);
 
614
}
 
615
 
 
616
static void
 
617
parse_string_list (char *value, GSList **retval)
 
618
{
 
619
        char **temp_array;
 
620
        int    i;
 
621
 
 
622
        *retval = NULL;
 
623
 
 
624
        if (value == NULL || *value == '\0') {
 
625
                g_debug ("Not adding NULL user");
 
626
                *retval = NULL;
 
627
                return;
 
628
        }
 
629
 
 
630
        temp_array = g_strsplit (value, ",", 0);
 
631
        for (i = 0; temp_array[i] != NULL; i++) {
 
632
                g_debug ("Adding value %s", temp_array[i]);
 
633
                g_strstrip (temp_array[i]);
 
634
                *retval = g_slist_prepend (*retval, g_strdup (temp_array[i]));
 
635
        }
 
636
 
 
637
        g_strfreev (temp_array);
 
638
}
 
639
 
 
640
static gboolean
 
641
load_users (GdmUserChooserWidget *widget)
 
642
{
 
643
 
 
644
        if (widget->priv->show_normal_users) {
 
645
                char          *temp;
 
646
                gboolean       res;
 
647
                gboolean       include_all;
 
648
                GSList        *includes;
 
649
                GSList        *excludes;
 
650
 
 
651
                widget->priv->manager = gdm_user_manager_ref_default ();
 
652
 
 
653
                /* exclude/include */
 
654
                g_debug ("Setting users to include:");
 
655
                res = gdm_settings_client_get_string (GDM_KEY_INCLUDE,
 
656
                                                      &temp);
 
657
                parse_string_list (temp, &includes);
 
658
 
 
659
                g_debug ("Setting users to exclude:");
 
660
                res = gdm_settings_client_get_string  (GDM_KEY_EXCLUDE,
 
661
                                                       &temp);
 
662
                parse_string_list (temp, &excludes);
 
663
 
 
664
                include_all = FALSE;
 
665
                res = gdm_settings_client_get_boolean (GDM_KEY_INCLUDE_ALL,
 
666
                                                       &include_all);
 
667
                g_object_set (widget->priv->manager,
 
668
                              "include-all", include_all,
 
669
                              "include-usernames-list", includes,
 
670
                              "exclude-usernames-list", excludes,
 
671
                              NULL);
 
672
 
 
673
                g_slist_foreach (includes, (GFunc) g_free, NULL);
 
674
                g_slist_free (includes);
 
675
                g_slist_foreach (excludes, (GFunc) g_free, NULL);
 
676
                g_slist_free (excludes);
 
677
 
 
678
                g_signal_connect (widget->priv->manager,
 
679
                                  "user-added",
 
680
                                  G_CALLBACK (on_user_added),
 
681
                                  widget);
 
682
                g_signal_connect (widget->priv->manager,
 
683
                                  "user-removed",
 
684
                                  G_CALLBACK (on_user_removed),
 
685
                                  widget);
 
686
                g_signal_connect (widget->priv->manager,
 
687
                                  "notify::is-loaded",
 
688
                                  G_CALLBACK (on_is_loaded_changed),
 
689
                                  widget);
 
690
                g_signal_connect (widget->priv->manager,
 
691
                                  "user-is-logged-in-changed",
 
692
                                  G_CALLBACK (on_user_is_logged_in_changed),
 
693
                                  widget);
 
694
                g_signal_connect (widget->priv->manager,
 
695
                                  "user-changed",
 
696
                                  G_CALLBACK (on_user_changed),
 
697
                                  widget);
 
698
                gdm_user_manager_queue_load (widget->priv->manager);
 
699
        } else {
 
700
                gdm_chooser_widget_loaded (GDM_CHOOSER_WIDGET (widget));
 
701
        }
 
702
 
 
703
        widget->priv->load_idle_id = 0;
 
704
 
 
705
        return FALSE;
 
706
}
 
707
 
 
708
static GObject *
 
709
gdm_user_chooser_widget_constructor (GType                  type,
 
710
                                     guint                  n_construct_properties,
 
711
                                     GObjectConstructParam *construct_properties)
 
712
{
 
713
        GdmUserChooserWidget      *widget;
 
714
 
 
715
        widget = GDM_USER_CHOOSER_WIDGET (G_OBJECT_CLASS (gdm_user_chooser_widget_parent_class)->constructor (type,
 
716
                                                                                                              n_construct_properties,
 
717
                                                                                                              construct_properties));
 
718
 
 
719
        widget->priv->show_normal_users = !is_user_list_disabled (widget);
 
720
 
 
721
        widget->priv->load_idle_id = g_idle_add ((GSourceFunc)load_users, widget);
 
722
 
 
723
        return G_OBJECT (widget);
 
724
}
 
725
 
 
726
static void
 
727
gdm_user_chooser_widget_dispose (GObject *object)
 
728
{
 
729
        GdmUserChooserWidget *widget;
 
730
 
 
731
        widget = GDM_USER_CHOOSER_WIDGET (object);
 
732
 
 
733
        G_OBJECT_CLASS (gdm_user_chooser_widget_parent_class)->dispose (object);
 
734
 
 
735
        if (widget->priv->load_idle_id > 0) {
 
736
                g_source_remove (widget->priv->load_idle_id);
 
737
                widget->priv->load_idle_id = 0;
 
738
        }
 
739
 
 
740
        if (widget->priv->add_users_idle_id > 0) {
 
741
                g_source_remove (widget->priv->add_users_idle_id);
 
742
                widget->priv->add_users_idle_id = 0;
 
743
        }
 
744
 
 
745
        if (widget->priv->update_other_visibility_idle_id > 0) {
 
746
                g_source_remove (widget->priv->update_other_visibility_idle_id);
 
747
                widget->priv->update_other_visibility_idle_id = 0;
 
748
        }
 
749
 
 
750
        if (widget->priv->users_to_add != NULL) {
 
751
                g_slist_foreach (widget->priv->users_to_add, (GFunc) g_object_ref, NULL);
 
752
                g_slist_free (widget->priv->users_to_add);
 
753
                widget->priv->users_to_add = NULL;
 
754
        }
 
755
 
 
756
        if (widget->priv->logged_in_pixbuf != NULL) {
 
757
                g_object_unref (widget->priv->logged_in_pixbuf);
 
758
                widget->priv->logged_in_pixbuf = NULL;
 
759
        }
 
760
 
 
761
        if (widget->priv->stock_person_pixbuf != NULL) {
 
762
                g_object_unref (widget->priv->stock_person_pixbuf);
 
763
                widget->priv->stock_person_pixbuf = NULL;
 
764
        }
 
765
 
 
766
        if (widget->priv->manager != NULL) {
 
767
                g_object_unref (widget->priv->manager);
 
768
                widget->priv->manager = NULL;
 
769
        }
 
770
}
 
771
 
 
772
static void
 
773
gdm_user_chooser_widget_class_init (GdmUserChooserWidgetClass *klass)
 
774
{
 
775
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
 
776
 
 
777
        object_class->get_property = gdm_user_chooser_widget_get_property;
 
778
        object_class->set_property = gdm_user_chooser_widget_set_property;
 
779
        object_class->constructor = gdm_user_chooser_widget_constructor;
 
780
        object_class->dispose = gdm_user_chooser_widget_dispose;
 
781
        object_class->finalize = gdm_user_chooser_widget_finalize;
 
782
 
 
783
 
 
784
        g_object_class_install_property (object_class,
 
785
                                         PROP_SHOW_USER_AUTO,
 
786
                                         g_param_spec_boolean ("show-user-auto",
 
787
                                                               "show user auto",
 
788
                                                               "show user auto",
 
789
                                                               FALSE,
 
790
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
791
        g_object_class_install_property (object_class,
 
792
                                         PROP_SHOW_USER_GUEST,
 
793
                                         g_param_spec_boolean ("show-user-guest",
 
794
                                                               "show user guest",
 
795
                                                               "show user guest",
 
796
                                                               FALSE,
 
797
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
798
        g_object_class_install_property (object_class,
 
799
                                         PROP_SHOW_USER_OTHER,
 
800
                                         g_param_spec_boolean ("show-user-other",
 
801
                                                               "show user other",
 
802
                                                               "show user other",
 
803
                                                               TRUE,
 
804
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
805
 
 
806
        g_type_class_add_private (klass, sizeof (GdmUserChooserWidgetPrivate));
 
807
}
 
808
 
 
809
static GdkPixbuf *
 
810
get_pixbuf_from_icon_names (GdmUserChooserWidget *widget,
 
811
                            const char           *first_name,
 
812
                            ...)
 
813
{
 
814
        GdkPixbuf   *pixbuf;
 
815
        GtkIconInfo *icon_info;
 
816
        GPtrArray   *array;
 
817
        int          size;
 
818
        const char  *icon_name;
 
819
        va_list      argument_list;
 
820
 
 
821
        array = g_ptr_array_new ();
 
822
 
 
823
        g_ptr_array_add (array, (gpointer) first_name);
 
824
 
 
825
        va_start (argument_list, first_name);
 
826
        icon_name = (const char *) va_arg (argument_list, const char *);
 
827
        while (icon_name != NULL) {
 
828
                g_ptr_array_add (array, (gpointer) icon_name);
 
829
                icon_name = (const char *) va_arg (argument_list, const char *);
 
830
        }
 
831
        va_end (argument_list);
 
832
        g_ptr_array_add (array, NULL);
 
833
 
 
834
        size = get_icon_height_for_widget (GTK_WIDGET (widget));
 
835
 
 
836
        icon_info = gtk_icon_theme_choose_icon (widget->priv->icon_theme,
 
837
                                                (const char **) array->pdata,
 
838
                                                size,
 
839
                                                GTK_ICON_LOOKUP_GENERIC_FALLBACK);
 
840
        g_ptr_array_free (array, FALSE);
 
841
 
 
842
        if (icon_info != NULL) {
 
843
                GError *error;
 
844
 
 
845
                error = NULL;
 
846
                pixbuf = gtk_icon_info_load_icon (icon_info, &error);
 
847
                gtk_icon_info_free (icon_info);
 
848
 
 
849
                if (error != NULL) {
 
850
                        g_warning ("Could not load icon '%s': %s",
 
851
                                   first_name, error->message);
 
852
                        g_error_free (error);
 
853
                }
 
854
        } else {
 
855
                g_warning ("Could not find icon '%s' or fallbacks", first_name);
 
856
                pixbuf = NULL;
 
857
        }
 
858
 
 
859
        return pixbuf;
 
860
}
 
861
 
 
862
static GdkPixbuf *
 
863
get_stock_person_pixbuf (GdmUserChooserWidget *widget)
 
864
{
 
865
        GdkPixbuf   *pixbuf;
 
866
 
 
867
        pixbuf = get_pixbuf_from_icon_names (widget,
 
868
                                             DEFAULT_USER_ICON,
 
869
                                             OLD_DEFAULT_USER_ICON,
 
870
                                             NULL);
 
871
 
 
872
        return pixbuf;
 
873
}
 
874
 
 
875
static GdkPixbuf *
 
876
get_logged_in_pixbuf (GdmUserChooserWidget *widget)
 
877
{
 
878
        GdkPixbuf *pixbuf;
 
879
 
 
880
        pixbuf = get_pixbuf_from_icon_names (widget,
 
881
                                             DEFAULT_USER_ICON,
 
882
                                             "emblem-default",
 
883
                                             NULL);
 
884
 
 
885
        return pixbuf;
 
886
}
 
887
 
 
888
typedef struct {
 
889
        GdkPixbuf *old_icon;
 
890
        GdkPixbuf *new_icon;
 
891
} IconUpdateData;
 
892
 
 
893
static gboolean
 
894
update_icons (GdmChooserWidget *widget,
 
895
              const char       *id,
 
896
              GdkPixbuf       **image,
 
897
              char            **name,
 
898
              char            **comment,
 
899
              gulong           *priority,
 
900
              gboolean         *is_in_use,
 
901
              gboolean         *is_separate,
 
902
              IconUpdateData   *data)
 
903
{
 
904
        if (data->old_icon == *image) {
 
905
                *image = data->new_icon;
 
906
                return TRUE;
 
907
        }
 
908
 
 
909
        return FALSE;
 
910
}
 
911
 
 
912
static void
 
913
load_icons (GdmUserChooserWidget *widget)
 
914
{
 
915
        GdkPixbuf     *old_pixbuf;
 
916
        IconUpdateData data;
 
917
 
 
918
        if (widget->priv->logged_in_pixbuf != NULL) {
 
919
                g_object_unref (widget->priv->logged_in_pixbuf);
 
920
        }
 
921
        widget->priv->logged_in_pixbuf = get_logged_in_pixbuf (widget);
 
922
 
 
923
        old_pixbuf = widget->priv->stock_person_pixbuf;
 
924
        widget->priv->stock_person_pixbuf = get_stock_person_pixbuf (widget);
 
925
        /* update the icons in the model */
 
926
        data.old_icon = old_pixbuf;
 
927
        data.new_icon = widget->priv->stock_person_pixbuf;
 
928
        gdm_chooser_widget_update_foreach_item (GDM_CHOOSER_WIDGET (widget),
 
929
                                                (GdmChooserUpdateForeachFunc)update_icons,
 
930
                                                &data);
 
931
        if (old_pixbuf != NULL) {
 
932
                g_object_unref (old_pixbuf);
 
933
        }
 
934
}
 
935
 
 
936
static void
 
937
on_icon_theme_changed (GtkIconTheme         *icon_theme,
 
938
                       GdmUserChooserWidget *widget)
 
939
{
 
940
        g_debug ("GdmUserChooserWidget: icon theme changed");
 
941
        load_icons (widget);
 
942
}
 
943
 
 
944
static void
 
945
setup_icons (GdmUserChooserWidget *widget)
 
946
{
 
947
        if (gtk_widget_has_screen (GTK_WIDGET (widget))) {
 
948
                widget->priv->icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (widget)));
 
949
        } else {
 
950
                widget->priv->icon_theme = gtk_icon_theme_get_default ();
 
951
        }
 
952
 
 
953
        if (widget->priv->icon_theme != NULL) {
 
954
                g_signal_connect (widget->priv->icon_theme,
 
955
                                  "changed",
 
956
                                  G_CALLBACK (on_icon_theme_changed),
 
957
                                  widget);
 
958
        }
 
959
 
 
960
        load_icons (widget);
 
961
}
 
962
 
 
963
static void
 
964
on_list_visible_changed (GdmChooserWidget *widget,
 
965
                         GParamSpec       *pspec,
 
966
                         gpointer          data)
 
967
{
 
968
        gboolean is_visible;
 
969
 
 
970
        g_object_get (G_OBJECT (widget), "list-visible", &is_visible, NULL);
 
971
        if (is_visible) {
 
972
                gtk_widget_grab_focus (GTK_WIDGET (widget));
 
973
        }
 
974
}
 
975
 
 
976
static void
 
977
gdm_user_chooser_widget_init (GdmUserChooserWidget *widget)
 
978
{
 
979
        widget->priv = GDM_USER_CHOOSER_WIDGET_GET_PRIVATE (widget);
 
980
 
 
981
        gdm_chooser_widget_set_separator_position (GDM_CHOOSER_WIDGET (widget),
 
982
                                                   GDM_CHOOSER_WIDGET_POSITION_BOTTOM);
 
983
        gdm_chooser_widget_set_in_use_message (GDM_CHOOSER_WIDGET (widget),
 
984
                                               _("Currently logged in"));
 
985
 
 
986
        g_signal_connect (widget,
 
987
                          "notify::list-visible",
 
988
                          G_CALLBACK (on_list_visible_changed),
 
989
                          NULL);
 
990
 
 
991
        setup_icons (widget);
 
992
}
 
993
 
 
994
static void
 
995
gdm_user_chooser_widget_finalize (GObject *object)
 
996
{
 
997
        GdmUserChooserWidget *widget;
 
998
 
 
999
        g_return_if_fail (object != NULL);
 
1000
        g_return_if_fail (GDM_IS_USER_CHOOSER_WIDGET (object));
 
1001
 
 
1002
        widget = GDM_USER_CHOOSER_WIDGET (object);
 
1003
 
 
1004
        g_return_if_fail (widget->priv != NULL);
 
1005
 
 
1006
        G_OBJECT_CLASS (gdm_user_chooser_widget_parent_class)->finalize (object);
 
1007
}
 
1008
 
 
1009
GtkWidget *
 
1010
gdm_user_chooser_widget_new (void)
 
1011
{
 
1012
        GObject *object;
 
1013
 
 
1014
        object = g_object_new (GDM_TYPE_USER_CHOOSER_WIDGET,
 
1015
                               NULL);
 
1016
 
 
1017
        return GTK_WIDGET (object);
 
1018
}