~ubuntu-branches/ubuntu/oneiric/gdm3/oneiric

« back to all changes in this revision

Viewing changes to gui/simple-greeter/gdm-chooser-widget.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2010-03-25 20:02:20 UTC
  • Revision ID: james.westby@ubuntu.com-20100325200220-12cap62s6p304nuh
Tags: upstream-2.29.92
ImportĀ upstreamĀ versionĀ 2.29.92

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 Ray Strode <rstrode@redhat.com>
 
4
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 
5
 * Copyright (C) 2008 Red Hat, Inc.
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 *
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <stdlib.h>
 
26
#include <stdio.h>
 
27
#include <unistd.h>
 
28
#include <string.h>
 
29
#include <errno.h>
 
30
#include <dirent.h>
 
31
#include <sys/stat.h>
 
32
#include <syslog.h>
 
33
 
 
34
#include <glib.h>
 
35
#include <glib/gi18n.h>
 
36
#include <glib/gstdio.h>
 
37
#include <gtk/gtk.h>
 
38
 
 
39
#include "gdm-chooser-widget.h"
 
40
#include "gdm-scrollable-widget.h"
 
41
#include "gdm-cell-renderer-timer.h"
 
42
#include "gdm-timer.h"
 
43
 
 
44
#define GDM_CHOOSER_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_CHOOSER_WIDGET, GdmChooserWidgetPrivate))
 
45
 
 
46
#ifndef GDM_CHOOSER_WIDGET_DEFAULT_ICON_SIZE
 
47
#define GDM_CHOOSER_WIDGET_DEFAULT_ICON_SIZE 64
 
48
#endif
 
49
 
 
50
typedef enum {
 
51
        GDM_CHOOSER_WIDGET_STATE_GROWN = 0,
 
52
        GDM_CHOOSER_WIDGET_STATE_GROWING,
 
53
        GDM_CHOOSER_WIDGET_STATE_SHRINKING,
 
54
        GDM_CHOOSER_WIDGET_STATE_SHRUNK,
 
55
} GdmChooserWidgetState;
 
56
 
 
57
struct GdmChooserWidgetPrivate
 
58
{
 
59
        GtkWidget                *frame;
 
60
        GtkWidget                *frame_alignment;
 
61
        GtkWidget                *scrollable_widget;
 
62
 
 
63
        GtkWidget                *items_view;
 
64
        GtkListStore             *list_store;
 
65
 
 
66
        GtkTreeModelFilter       *model_filter;
 
67
        GtkTreeModelSort         *model_sorter;
 
68
 
 
69
        GdkPixbuf                *is_in_use_pixbuf;
 
70
 
 
71
        /* row for the list_store model */
 
72
        GtkTreeRowReference      *active_row;
 
73
        GtkTreeRowReference      *separator_row;
 
74
 
 
75
        GHashTable               *rows_with_timers;
 
76
 
 
77
        GtkTreeViewColumn        *status_column;
 
78
        GtkTreeViewColumn        *image_column;
 
79
 
 
80
        char                     *inactive_text;
 
81
        char                     *active_text;
 
82
        char                     *in_use_message;
 
83
 
 
84
        gint                     number_of_normal_rows;
 
85
        gint                     number_of_separated_rows;
 
86
        gint                     number_of_rows_with_status;
 
87
        gint                     number_of_rows_with_images;
 
88
        gint                     number_of_active_timers;
 
89
 
 
90
        guint                    update_idle_id;
 
91
        guint                    timer_animation_timeout_id;
 
92
 
 
93
        guint32                  should_hide_inactive_items : 1;
 
94
        guint32                  emit_activated_after_resize_animation : 1;
 
95
 
 
96
        GdmChooserWidgetPosition separator_position;
 
97
        GdmChooserWidgetState    state;
 
98
 
 
99
        double                   active_row_normalized_position;
 
100
};
 
101
 
 
102
enum {
 
103
        PROP_0,
 
104
        PROP_INACTIVE_TEXT,
 
105
        PROP_ACTIVE_TEXT,
 
106
        PROP_LIST_VISIBLE
 
107
};
 
108
 
 
109
enum {
 
110
        ACTIVATED = 0,
 
111
        DEACTIVATED,
 
112
        LOADED,
 
113
        NUMBER_OF_SIGNALS
 
114
};
 
115
 
 
116
static guint    signals[NUMBER_OF_SIGNALS];
 
117
 
 
118
static void     gdm_chooser_widget_class_init  (GdmChooserWidgetClass *klass);
 
119
static void     gdm_chooser_widget_init        (GdmChooserWidget      *chooser_widget);
 
120
static void     gdm_chooser_widget_finalize    (GObject               *object);
 
121
 
 
122
static void     update_timer_from_time         (GdmChooserWidget    *widget,
 
123
                                                GtkTreeRowReference *row,
 
124
                                                double               now);
 
125
 
 
126
G_DEFINE_TYPE (GdmChooserWidget, gdm_chooser_widget, GTK_TYPE_ALIGNMENT)
 
127
 
 
128
enum {
 
129
        CHOOSER_IMAGE_COLUMN = 0,
 
130
        CHOOSER_NAME_COLUMN,
 
131
        CHOOSER_COMMENT_COLUMN,
 
132
        CHOOSER_PRIORITY_COLUMN,
 
133
        CHOOSER_ITEM_IS_IN_USE_COLUMN,
 
134
        CHOOSER_ITEM_IS_SEPARATED_COLUMN,
 
135
        CHOOSER_ITEM_IS_VISIBLE_COLUMN,
 
136
        CHOOSER_TIMER_START_TIME_COLUMN,
 
137
        CHOOSER_TIMER_DURATION_COLUMN,
 
138
        CHOOSER_TIMER_VALUE_COLUMN,
 
139
        CHOOSER_ID_COLUMN,
 
140
        NUMBER_OF_CHOOSER_COLUMNS
 
141
};
 
142
 
 
143
static gboolean
 
144
find_item (GdmChooserWidget *widget,
 
145
           const char       *id,
 
146
           GtkTreeIter      *iter)
 
147
{
 
148
        GtkTreeModel *model;
 
149
        gboolean      found_item;
 
150
 
 
151
        g_assert (GDM_IS_CHOOSER_WIDGET (widget));
 
152
        g_assert (id != NULL);
 
153
 
 
154
        found_item = FALSE;
 
155
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
156
 
 
157
        if (!gtk_tree_model_get_iter_first (model, iter)) {
 
158
                return FALSE;
 
159
        }
 
160
 
 
161
        do {
 
162
                char *item_id;
 
163
 
 
164
                gtk_tree_model_get (model,
 
165
                                    iter,
 
166
                                    CHOOSER_ID_COLUMN,
 
167
                                    &item_id,
 
168
                                    -1);
 
169
 
 
170
                g_assert (item_id != NULL);
 
171
 
 
172
                if (strcmp (id, item_id) == 0) {
 
173
                        found_item = TRUE;
 
174
                }
 
175
                g_free (item_id);
 
176
 
 
177
        } while (!found_item && gtk_tree_model_iter_next (model, iter));
 
178
 
 
179
        return found_item;
 
180
}
 
181
 
 
182
typedef struct {
 
183
        GdmChooserWidget           *widget;
 
184
        GdmChooserUpdateForeachFunc func;
 
185
        gpointer                    user_data;
 
186
} UpdateForeachData;
 
187
 
 
188
static gboolean
 
189
foreach_item (GtkTreeModel      *model,
 
190
              GtkTreePath       *path,
 
191
              GtkTreeIter       *iter,
 
192
              UpdateForeachData *data)
 
193
{
 
194
        GdkPixbuf *image;
 
195
        char      *name;
 
196
        char      *comment;
 
197
        gboolean   in_use;
 
198
        gboolean   is_separate;
 
199
        gboolean   res;
 
200
        char      *id;
 
201
        gulong     priority;
 
202
 
 
203
        gtk_tree_model_get (model,
 
204
                            iter,
 
205
                            CHOOSER_ID_COLUMN, &id,
 
206
                            CHOOSER_IMAGE_COLUMN, &image,
 
207
                            CHOOSER_NAME_COLUMN, &name,
 
208
                            CHOOSER_COMMENT_COLUMN, &comment,
 
209
                            CHOOSER_PRIORITY_COLUMN, &priority,
 
210
                            CHOOSER_ITEM_IS_IN_USE_COLUMN, &in_use,
 
211
                            CHOOSER_ITEM_IS_SEPARATED_COLUMN, &is_separate,
 
212
                            -1);
 
213
        res = data->func (data->widget,
 
214
                          (const char *)id,
 
215
                          &image,
 
216
                          &name,
 
217
                          &comment,
 
218
                          &priority,
 
219
                          &in_use,
 
220
                          &is_separate,
 
221
                          data->user_data);
 
222
        if (res) {
 
223
                gtk_list_store_set (GTK_LIST_STORE (model),
 
224
                                    iter,
 
225
                                    CHOOSER_ID_COLUMN, id,
 
226
                                    CHOOSER_IMAGE_COLUMN, image,
 
227
                                    CHOOSER_NAME_COLUMN, name,
 
228
                                    CHOOSER_COMMENT_COLUMN, comment,
 
229
                                    CHOOSER_PRIORITY_COLUMN, priority,
 
230
                                    CHOOSER_ITEM_IS_IN_USE_COLUMN, in_use,
 
231
                                    CHOOSER_ITEM_IS_SEPARATED_COLUMN, is_separate,
 
232
                                    -1);
 
233
        }
 
234
 
 
235
        g_free (name);
 
236
        g_free (comment);
 
237
        if (image != NULL) {
 
238
                g_object_unref (image);
 
239
        }
 
240
 
 
241
        return FALSE;
 
242
}
 
243
 
 
244
void
 
245
gdm_chooser_widget_update_foreach_item (GdmChooserWidget           *widget,
 
246
                                        GdmChooserUpdateForeachFunc func,
 
247
                                        gpointer                    user_data)
 
248
{
 
249
        UpdateForeachData fdata;
 
250
 
 
251
        fdata.widget = widget;
 
252
        fdata.func = func;
 
253
        fdata.user_data = user_data;
 
254
        gtk_tree_model_foreach (GTK_TREE_MODEL (widget->priv->list_store),
 
255
                                (GtkTreeModelForeachFunc) foreach_item,
 
256
                                &fdata);
 
257
}
 
258
 
 
259
static void
 
260
translate_list_path_to_view_path (GdmChooserWidget  *widget,
 
261
                                  GtkTreePath      **path)
 
262
{
 
263
        GtkTreePath *filtered_path;
 
264
        GtkTreePath *sorted_path;
 
265
 
 
266
        /* the child model is the source for the filter */
 
267
        filtered_path = gtk_tree_model_filter_convert_child_path_to_path (widget->priv->model_filter,
 
268
                                                                          *path);
 
269
        sorted_path = gtk_tree_model_sort_convert_child_path_to_path (widget->priv->model_sorter,
 
270
                                                                      filtered_path);
 
271
        gtk_tree_path_free (filtered_path);
 
272
 
 
273
        gtk_tree_path_free (*path);
 
274
        *path = sorted_path;
 
275
}
 
276
 
 
277
 
 
278
static void
 
279
translate_view_path_to_list_path (GdmChooserWidget  *widget,
 
280
                                  GtkTreePath      **path)
 
281
{
 
282
        GtkTreePath *filtered_path;
 
283
        GtkTreePath *list_path;
 
284
 
 
285
        /* the child model is the source for the filter */
 
286
        filtered_path = gtk_tree_model_sort_convert_path_to_child_path (widget->priv->model_sorter,
 
287
                                                                        *path);
 
288
 
 
289
        list_path = gtk_tree_model_filter_convert_path_to_child_path (widget->priv->model_filter,
 
290
                                                                      filtered_path);
 
291
        gtk_tree_path_free (filtered_path);
 
292
 
 
293
        gtk_tree_path_free (*path);
 
294
        *path = list_path;
 
295
}
 
296
 
 
297
static GtkTreePath *
 
298
get_list_path_to_active_row (GdmChooserWidget *widget)
 
299
{
 
300
        GtkTreePath *path;
 
301
 
 
302
        if (widget->priv->active_row == NULL) {
 
303
                return NULL;
 
304
        }
 
305
 
 
306
        path = gtk_tree_row_reference_get_path (widget->priv->active_row);
 
307
        if (path == NULL) {
 
308
                return NULL;
 
309
        }
 
310
 
 
311
        return path;
 
312
}
 
313
 
 
314
static GtkTreePath *
 
315
get_view_path_to_active_row (GdmChooserWidget *widget)
 
316
{
 
317
        GtkTreePath *path;
 
318
 
 
319
        path = get_list_path_to_active_row (widget);
 
320
        if (path == NULL) {
 
321
                return NULL;
 
322
        }
 
323
 
 
324
        translate_list_path_to_view_path (widget, &path);
 
325
 
 
326
        return path;
 
327
}
 
328
 
 
329
static char *
 
330
get_active_item_id (GdmChooserWidget *widget,
 
331
                    GtkTreeIter      *iter)
 
332
{
 
333
        char         *item_id;
 
334
        GtkTreeModel *model;
 
335
        GtkTreePath  *path;
 
336
 
 
337
        g_return_val_if_fail (GDM_IS_CHOOSER_WIDGET (widget), NULL);
 
338
 
 
339
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
340
        item_id = NULL;
 
341
 
 
342
        if (widget->priv->active_row == NULL) {
 
343
                return NULL;
 
344
        }
 
345
 
 
346
        path = get_list_path_to_active_row (widget);
 
347
        if (path == NULL) {
 
348
                return NULL;
 
349
        }
 
350
 
 
351
        if (gtk_tree_model_get_iter (model, iter, path)) {
 
352
                gtk_tree_model_get (model,
 
353
                                    iter,
 
354
                                    CHOOSER_ID_COLUMN,
 
355
                                    &item_id,
 
356
                                    -1);
 
357
        }
 
358
        gtk_tree_path_free (path);
 
359
 
 
360
        return item_id;
 
361
}
 
362
 
 
363
char *
 
364
gdm_chooser_widget_get_active_item (GdmChooserWidget *widget)
 
365
{
 
366
        GtkTreeIter iter;
 
367
 
 
368
        return get_active_item_id (widget, &iter);
 
369
}
 
370
 
 
371
static void
 
372
get_selected_list_path (GdmChooserWidget *widget,
 
373
                        GtkTreePath     **pathp)
 
374
{
 
375
        GtkTreeSelection    *selection;
 
376
        GtkTreeModel        *sort_model;
 
377
        GtkTreeIter          sorted_iter;
 
378
        GtkTreePath         *path;
 
379
 
 
380
        path = NULL;
 
381
 
 
382
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget->priv->items_view));
 
383
        if (gtk_tree_selection_get_selected (selection, &sort_model, &sorted_iter)) {
 
384
 
 
385
                g_assert (sort_model == GTK_TREE_MODEL (widget->priv->model_sorter));
 
386
 
 
387
                path = gtk_tree_model_get_path (sort_model, &sorted_iter);
 
388
 
 
389
                translate_view_path_to_list_path (widget, &path);
 
390
        } else {
 
391
                g_debug ("GdmChooserWidget: no rows selected");
 
392
        }
 
393
 
 
394
        *pathp = path;
 
395
}
 
396
 
 
397
char *
 
398
gdm_chooser_widget_get_selected_item (GdmChooserWidget *widget)
 
399
{
 
400
        GtkTreeIter   iter;
 
401
        GtkTreeModel *model;
 
402
        GtkTreePath  *path;
 
403
        char         *id;
 
404
 
 
405
        id = NULL;
 
406
 
 
407
        get_selected_list_path (widget, &path);
 
408
 
 
409
        if (path == NULL) {
 
410
                return NULL;
 
411
        }
 
412
 
 
413
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
414
 
 
415
        if (gtk_tree_model_get_iter (model, &iter, path)) {
 
416
                gtk_tree_model_get (model,
 
417
                                    &iter,
 
418
                                    CHOOSER_ID_COLUMN,
 
419
                                    &id,
 
420
                                    -1);
 
421
        }
 
422
 
 
423
        gtk_tree_path_free (path);
 
424
 
 
425
        return id;
 
426
}
 
427
 
 
428
void
 
429
gdm_chooser_widget_set_selected_item (GdmChooserWidget *widget,
 
430
                                      const char       *id)
 
431
{
 
432
        GtkTreeIter       iter;
 
433
        GtkTreeSelection *selection;
 
434
        GtkTreeModel     *model;
 
435
 
 
436
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
437
 
 
438
        g_debug ("GdmChooserWidget: setting selected item '%s'",
 
439
                 id ? id : "(null)");
 
440
 
 
441
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget->priv->items_view));
 
442
 
 
443
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
444
 
 
445
        if (find_item (widget, id, &iter)) {
 
446
                GtkTreePath  *path;
 
447
 
 
448
                path = gtk_tree_model_get_path (model, &iter);
 
449
                translate_list_path_to_view_path (widget, &path);
 
450
 
 
451
                gtk_tree_selection_select_path (selection, path);
 
452
 
 
453
                gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (widget->priv->items_view),
 
454
                                              path,
 
455
                                              NULL,
 
456
                                              TRUE,
 
457
                                              0.5,
 
458
                                              0.0);
 
459
 
 
460
                gtk_tree_view_set_cursor (GTK_TREE_VIEW (widget->priv->items_view),
 
461
                                          path,
 
462
                                          NULL,
 
463
                                          FALSE);
 
464
                gtk_tree_path_free (path);
 
465
        } else {
 
466
                gtk_tree_selection_unselect_all (selection);
 
467
        }
 
468
}
 
469
 
 
470
static void
 
471
activate_from_item_id (GdmChooserWidget *widget,
 
472
                       const char       *item_id)
 
473
{
 
474
        GtkTreeModel *model;
 
475
        GtkTreePath  *path;
 
476
        GtkTreeIter   iter;
 
477
        char         *path_str;
 
478
 
 
479
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
480
        path = NULL;
 
481
 
 
482
        if (find_item (widget, item_id, &iter)) {
 
483
                path = gtk_tree_model_get_path (model, &iter);
 
484
 
 
485
                path_str = gtk_tree_path_to_string (path);
 
486
                g_debug ("GdmChooserWidget: got list path '%s'", path_str);
 
487
                g_free (path_str);
 
488
 
 
489
                translate_list_path_to_view_path (widget, &path);
 
490
 
 
491
                path_str = gtk_tree_path_to_string (path);
 
492
                g_debug ("GdmChooserWidget: translated to view path '%s'", path_str);
 
493
                g_free (path_str);
 
494
        }
 
495
 
 
496
        if (path == NULL) {
 
497
                g_debug ("GdmChooserWidget: unable to activate - path for item '%s' not found", item_id);
 
498
                return;
 
499
        }
 
500
 
 
501
        gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (widget->priv->items_view),
 
502
                                      path,
 
503
                                      NULL,
 
504
                                      TRUE,
 
505
                                      0.5,
 
506
                                      0.0);
 
507
 
 
508
        gtk_tree_view_set_cursor (GTK_TREE_VIEW (widget->priv->items_view),
 
509
                                  path,
 
510
                                  NULL,
 
511
                                  FALSE);
 
512
 
 
513
        gtk_tree_view_row_activated (GTK_TREE_VIEW (widget->priv->items_view),
 
514
                                     path,
 
515
                                     NULL);
 
516
        gtk_tree_path_free (path);
 
517
}
 
518
 
 
519
static void
 
520
set_frame_text (GdmChooserWidget *widget,
 
521
                const char       *text)
 
522
{
 
523
        GtkWidget *label;
 
524
 
 
525
        label = gtk_frame_get_label_widget (GTK_FRAME (widget->priv->frame));
 
526
 
 
527
        if (text == NULL && label != NULL) {
 
528
                gtk_frame_set_label_widget (GTK_FRAME (widget->priv->frame),
 
529
                                            NULL);
 
530
                gtk_alignment_set_padding (GTK_ALIGNMENT (widget->priv->frame_alignment),
 
531
                                           0, 0, 0, 0);
 
532
        } else if (text != NULL && label == NULL) {
 
533
                label = gtk_label_new ("");
 
534
                gtk_label_set_mnemonic_widget (GTK_LABEL (label),
 
535
                                               widget->priv->items_view);
 
536
                gtk_widget_show (label);
 
537
                gtk_frame_set_label_widget (GTK_FRAME (widget->priv->frame),
 
538
                                            label);
 
539
                gtk_alignment_set_padding (GTK_ALIGNMENT (widget->priv->frame_alignment),
 
540
                                           0, 0, 0, 0);
 
541
        }
 
542
 
 
543
        if (label != NULL && text != NULL) {
 
544
                char *markup;
 
545
                markup = g_strdup_printf ("%s", text);
 
546
                gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), markup);
 
547
                g_free (markup);
 
548
        }
 
549
}
 
550
 
 
551
static void
 
552
on_shrink_animation_step (GdmScrollableWidget *scrollable_widget,
 
553
                          double               progress,
 
554
                          int                 *new_height,
 
555
                          GdmChooserWidget    *widget)
 
556
{
 
557
        GtkTreePath   *active_row_path;
 
558
        const double   final_alignment = 0.5;
 
559
        double         row_alignment;
 
560
 
 
561
        active_row_path = get_view_path_to_active_row (widget);
 
562
        row_alignment = widget->priv->active_row_normalized_position + progress * (final_alignment - widget->priv->active_row_normalized_position);
 
563
 
 
564
        gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (widget->priv->items_view),
 
565
                                     active_row_path, NULL, TRUE, row_alignment, 0.0);
 
566
        gtk_tree_path_free (active_row_path);
 
567
}
 
568
 
 
569
static void
 
570
update_separator_visibility (GdmChooserWidget *widget)
 
571
{
 
572
        GtkTreePath *separator_path;
 
573
        GtkTreeIter  iter;
 
574
        gboolean     is_visible;
 
575
 
 
576
        separator_path = gtk_tree_row_reference_get_path (widget->priv->separator_row);
 
577
 
 
578
        if (separator_path == NULL) {
 
579
                return;
 
580
        }
 
581
 
 
582
        gtk_tree_model_get_iter (GTK_TREE_MODEL (widget->priv->list_store),
 
583
                                 &iter, separator_path);
 
584
 
 
585
        if (widget->priv->number_of_normal_rows > 0 &&
 
586
            widget->priv->number_of_separated_rows > 0 &&
 
587
            widget->priv->state != GDM_CHOOSER_WIDGET_STATE_SHRUNK) {
 
588
                is_visible = TRUE;
 
589
        } else {
 
590
                is_visible = FALSE;
 
591
        }
 
592
 
 
593
        gtk_list_store_set (widget->priv->list_store,
 
594
                            &iter,
 
595
                            CHOOSER_ITEM_IS_VISIBLE_COLUMN, is_visible,
 
596
                            -1);
 
597
}
 
598
 
 
599
static void
 
600
update_chooser_visibility (GdmChooserWidget *widget)
 
601
{
 
602
        if (gdm_chooser_widget_get_number_of_items (widget) > 0) {
 
603
                gtk_widget_show (widget->priv->frame);
 
604
        } else {
 
605
                gtk_widget_hide (widget->priv->frame);
 
606
        }
 
607
        g_object_notify (G_OBJECT (widget), "list-visible");
 
608
}
 
609
 
 
610
static void
 
611
set_inactive_items_visible (GdmChooserWidget *widget,
 
612
                            gboolean          should_show)
 
613
{
 
614
        GtkTreeModel *model;
 
615
        char         *active_item_id;
 
616
        GtkTreeIter   active_item_iter;
 
617
        GtkTreeIter   iter;
 
618
 
 
619
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
620
 
 
621
        if (!gtk_tree_model_get_iter_first (model, &iter)) {
 
622
                return;
 
623
        }
 
624
 
 
625
        active_item_id = get_active_item_id (widget, &active_item_iter);
 
626
 
 
627
        do {
 
628
                gboolean is_active;
 
629
 
 
630
                is_active = FALSE;
 
631
                if (active_item_id != NULL) {
 
632
                        char *id;
 
633
 
 
634
                        gtk_tree_model_get (model, &iter,
 
635
                                            CHOOSER_ID_COLUMN, &id, -1);
 
636
 
 
637
                        if (strcmp (active_item_id, id) == 0) {
 
638
                                is_active = TRUE;
 
639
                                g_free (active_item_id);
 
640
                                active_item_id = NULL;
 
641
                        }
 
642
                        g_free (id);
 
643
                }
 
644
 
 
645
                if (!is_active) {
 
646
                        gtk_list_store_set (widget->priv->list_store,
 
647
                                            &iter,
 
648
                                            CHOOSER_ITEM_IS_VISIBLE_COLUMN, should_show,
 
649
                                            -1);
 
650
                } else {
 
651
                        gtk_list_store_set (widget->priv->list_store,
 
652
                                            &iter,
 
653
                                            CHOOSER_ITEM_IS_VISIBLE_COLUMN, TRUE,
 
654
                                            -1);
 
655
                }
 
656
        } while (gtk_tree_model_iter_next (model, &iter));
 
657
 
 
658
        g_free (active_item_id);
 
659
 
 
660
        update_separator_visibility (widget);
 
661
}
 
662
 
 
663
static void
 
664
on_shrink_animation_complete (GdmScrollableWidget *scrollable_widget,
 
665
                              GdmChooserWidget    *widget)
 
666
{
 
667
        g_assert (widget->priv->state == GDM_CHOOSER_WIDGET_STATE_SHRINKING);
 
668
 
 
669
        g_debug ("GdmChooserWidget: shrink complete");
 
670
 
 
671
        widget->priv->active_row_normalized_position = 0.5;
 
672
        set_inactive_items_visible (GDM_CHOOSER_WIDGET (widget), FALSE);
 
673
        gtk_tree_view_set_enable_search (GTK_TREE_VIEW (widget->priv->items_view), FALSE);
 
674
        widget->priv->state = GDM_CHOOSER_WIDGET_STATE_SHRUNK;
 
675
 
 
676
        update_separator_visibility (widget);
 
677
 
 
678
        if (widget->priv->emit_activated_after_resize_animation) {
 
679
                g_signal_emit (widget, signals[ACTIVATED], 0);
 
680
                widget->priv->emit_activated_after_resize_animation = FALSE;
 
681
        }
 
682
}
 
683
 
 
684
static int
 
685
get_height_of_row_at_path (GdmChooserWidget *widget,
 
686
                           GtkTreePath      *path)
 
687
{
 
688
        GdkRectangle area;
 
689
 
 
690
        gtk_tree_view_get_background_area (GTK_TREE_VIEW (widget->priv->items_view),
 
691
                                           path, NULL, &area);
 
692
 
 
693
        return area.height;
 
694
}
 
695
 
 
696
static double
 
697
get_normalized_position_of_row_at_path (GdmChooserWidget *widget,
 
698
                                        GtkTreePath      *path)
 
699
{
 
700
        GdkRectangle area_of_row_at_path;
 
701
        GdkRectangle area_of_visible_rows;
 
702
 
 
703
        gtk_tree_view_get_background_area (GTK_TREE_VIEW (widget->priv->items_view),
 
704
                                           path, NULL, &area_of_row_at_path);
 
705
 
 
706
        gtk_tree_view_convert_tree_to_widget_coords (GTK_TREE_VIEW (widget->priv->items_view),
 
707
                                                     area_of_visible_rows.x,
 
708
                                                     area_of_visible_rows.y,
 
709
                                                     &area_of_visible_rows.x,
 
710
                                                     &area_of_visible_rows.y);
 
711
        return CLAMP (((double) area_of_row_at_path.y) / widget->priv->items_view->allocation.height, 0.0, 1.0);
 
712
}
 
713
 
 
714
static void
 
715
start_shrink_animation (GdmChooserWidget *widget)
 
716
{
 
717
       GtkTreePath *active_row_path;
 
718
       int          active_row_height;
 
719
       int          number_of_visible_rows;
 
720
 
 
721
       g_assert (widget->priv->active_row != NULL);
 
722
 
 
723
       number_of_visible_rows = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (widget->priv->model_sorter), NULL);
 
724
 
 
725
       if (number_of_visible_rows <= 1) {
 
726
               on_shrink_animation_complete (GDM_SCROLLABLE_WIDGET (widget->priv->scrollable_widget),
 
727
                                             widget);
 
728
               return;
 
729
       }
 
730
 
 
731
       active_row_path = get_view_path_to_active_row (widget);
 
732
       active_row_height = get_height_of_row_at_path (widget, active_row_path);
 
733
       widget->priv->active_row_normalized_position = get_normalized_position_of_row_at_path (widget, active_row_path);
 
734
       gtk_tree_path_free (active_row_path);
 
735
 
 
736
       gdm_scrollable_widget_slide_to_height (GDM_SCROLLABLE_WIDGET (widget->priv->scrollable_widget),
 
737
                                              active_row_height,
 
738
                                              (GdmScrollableWidgetSlideStepFunc)
 
739
                                              on_shrink_animation_step, widget,
 
740
                                              (GdmScrollableWidgetSlideDoneFunc)
 
741
                                              on_shrink_animation_complete, widget);
 
742
}
 
743
 
 
744
static char *
 
745
get_first_item (GdmChooserWidget *widget)
 
746
{
 
747
        GtkTreeModel *model;
 
748
        GtkTreeIter   iter;
 
749
        char         *id;
 
750
 
 
751
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
752
 
 
753
        if (!gtk_tree_model_get_iter_first (model, &iter)) {
 
754
                g_assert_not_reached ();
 
755
        }
 
756
 
 
757
        gtk_tree_model_get (model, &iter,
 
758
                            CHOOSER_ID_COLUMN, &id, -1);
 
759
        return id;
 
760
}
 
761
 
 
762
static gboolean
 
763
activate_if_one_item (GdmChooserWidget *widget)
 
764
{
 
765
        char *id;
 
766
 
 
767
        g_debug ("GdmChooserWidget: attempting to activate single item");
 
768
 
 
769
        if (gdm_chooser_widget_get_number_of_items (widget) != 1) {
 
770
                g_debug ("GdmChooserWidget: unable to activate single item - has %d items", gdm_chooser_widget_get_number_of_items (widget));
 
771
                return FALSE;
 
772
        }
 
773
 
 
774
        id = get_first_item (widget);
 
775
        if (id != NULL) {
 
776
                gdm_chooser_widget_set_active_item (widget, id);
 
777
                g_free (id);
 
778
        }
 
779
 
 
780
        return FALSE;
 
781
}
 
782
 
 
783
static void
 
784
_grab_focus (GtkWidget *widget)
 
785
{
 
786
        GtkWidget *foc_widget;
 
787
 
 
788
        foc_widget = GDM_CHOOSER_WIDGET (widget)->priv->items_view;
 
789
        g_debug ("GdmChooserWidget: grabbing focus");
 
790
        if (! GTK_WIDGET_REALIZED (foc_widget)) {
 
791
                g_debug ("GdmChooserWidget: not grabbing focus - not realized");
 
792
                return;
 
793
        }
 
794
 
 
795
        if (GTK_WIDGET_HAS_FOCUS (foc_widget)) {
 
796
                g_debug ("GdmChooserWidget: not grabbing focus - already has it");
 
797
                return;
 
798
        }
 
799
 
 
800
        gtk_widget_grab_focus (foc_widget);
 
801
}
 
802
 
 
803
static void
 
804
on_grow_animation_complete (GdmScrollableWidget *scrollable_widget,
 
805
                            GdmChooserWidget    *widget)
 
806
{
 
807
        g_assert (widget->priv->state == GDM_CHOOSER_WIDGET_STATE_GROWING);
 
808
        widget->priv->state = GDM_CHOOSER_WIDGET_STATE_GROWN;
 
809
        gtk_tree_view_set_enable_search (GTK_TREE_VIEW (widget->priv->items_view), TRUE);
 
810
 
 
811
        _grab_focus (GTK_WIDGET (widget));
 
812
}
 
813
 
 
814
static int
 
815
get_number_of_on_screen_rows (GdmChooserWidget *widget)
 
816
{
 
817
        GtkTreePath *start_path;
 
818
        GtkTreePath *end_path;
 
819
        int         *start_index;
 
820
        int         *end_index;
 
821
        int          number_of_rows;
 
822
 
 
823
        if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (widget->priv->items_view),
 
824
                                              &start_path, &end_path)) {
 
825
                return 0;
 
826
        }
 
827
 
 
828
        start_index = gtk_tree_path_get_indices (start_path);
 
829
        end_index = gtk_tree_path_get_indices (end_path);
 
830
 
 
831
        number_of_rows = *end_index - *start_index + 1;
 
832
 
 
833
        gtk_tree_path_free (start_path);
 
834
        gtk_tree_path_free (end_path);
 
835
 
 
836
        return number_of_rows;
 
837
}
 
838
 
 
839
static void
 
840
on_grow_animation_step (GdmScrollableWidget *scrollable_widget,
 
841
                        double               progress,
 
842
                        int                 *new_height,
 
843
                        GdmChooserWidget    *widget)
 
844
{
 
845
        int number_of_visible_rows;
 
846
        int number_of_on_screen_rows;
 
847
 
 
848
        number_of_visible_rows = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (widget->priv->model_sorter), NULL);
 
849
        number_of_on_screen_rows = get_number_of_on_screen_rows (widget);
 
850
 
 
851
        *new_height = GTK_BIN (scrollable_widget)->child->requisition.height;
 
852
}
 
853
 
 
854
static void
 
855
start_grow_animation (GdmChooserWidget *widget)
 
856
{
 
857
        set_inactive_items_visible (widget, TRUE);
 
858
 
 
859
        gdm_scrollable_widget_slide_to_height (GDM_SCROLLABLE_WIDGET (widget->priv->scrollable_widget),
 
860
                                               GTK_BIN (widget->priv->scrollable_widget)->child->requisition.height,
 
861
                                               (GdmScrollableWidgetSlideStepFunc)
 
862
                                               on_grow_animation_step, widget,
 
863
                                               (GdmScrollableWidgetSlideDoneFunc)
 
864
                                               on_grow_animation_complete, widget);
 
865
}
 
866
 
 
867
static void
 
868
skip_resize_animation (GdmChooserWidget *widget)
 
869
{
 
870
        if (widget->priv->state == GDM_CHOOSER_WIDGET_STATE_SHRINKING) {
 
871
                set_inactive_items_visible (GDM_CHOOSER_WIDGET (widget), FALSE);
 
872
                gtk_tree_view_set_enable_search (GTK_TREE_VIEW (widget->priv->items_view), FALSE);
 
873
                widget->priv->state = GDM_CHOOSER_WIDGET_STATE_SHRUNK;
 
874
        } else if (widget->priv->state == GDM_CHOOSER_WIDGET_STATE_GROWING) {
 
875
                set_inactive_items_visible (GDM_CHOOSER_WIDGET (widget), TRUE);
 
876
                gtk_tree_view_set_enable_search (GTK_TREE_VIEW (widget->priv->items_view), TRUE);
 
877
                widget->priv->state = GDM_CHOOSER_WIDGET_STATE_GROWN;
 
878
                _grab_focus (GTK_WIDGET (widget));
 
879
        }
 
880
}
 
881
 
 
882
static void
 
883
gdm_chooser_widget_grow (GdmChooserWidget *widget)
 
884
{
 
885
        if (widget->priv->state == GDM_CHOOSER_WIDGET_STATE_SHRINKING) {
 
886
                gdm_scrollable_widget_stop_sliding (GDM_SCROLLABLE_WIDGET (widget->priv->scrollable_widget));
 
887
        }
 
888
 
 
889
        gtk_alignment_set (GTK_ALIGNMENT (widget->priv->frame_alignment),
 
890
                           0.0, 0.0, 1.0, 1.0);
 
891
 
 
892
        set_frame_text (widget, widget->priv->inactive_text);
 
893
 
 
894
        widget->priv->state = GDM_CHOOSER_WIDGET_STATE_GROWING;
 
895
 
 
896
        if (GTK_WIDGET_VISIBLE (widget)) {
 
897
                start_grow_animation (widget);
 
898
        } else {
 
899
                skip_resize_animation (widget);
 
900
        }
 
901
}
 
902
 
 
903
static void
 
904
move_cursor_to_top (GdmChooserWidget *widget)
 
905
{
 
906
        GtkTreeModel *model;
 
907
        GtkTreePath  *path;
 
908
        GtkTreeIter   iter;
 
909
 
 
910
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget->priv->items_view));
 
911
        path = gtk_tree_path_new_first ();
 
912
        if (gtk_tree_model_get_iter (model, &iter, path)) {
 
913
                gtk_tree_view_set_cursor (GTK_TREE_VIEW (widget->priv->items_view),
 
914
                                          path,
 
915
                                          NULL,
 
916
                                          FALSE);
 
917
        }
 
918
        gtk_tree_path_free (path);
 
919
}
 
920
 
 
921
static gboolean
 
922
clear_selection (GdmChooserWidget *widget)
 
923
{
 
924
        GtkTreeSelection *selection;
 
925
        GtkWidget        *window;
 
926
 
 
927
        g_debug ("GdmChooserWidget: clearing selection");
 
928
 
 
929
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget->priv->items_view));
 
930
        gtk_tree_selection_unselect_all (selection);
 
931
 
 
932
        window = gtk_widget_get_ancestor (GTK_WIDGET (widget), GTK_TYPE_WINDOW);
 
933
 
 
934
        if (window != NULL) {
 
935
                gtk_window_set_focus (GTK_WINDOW (window), NULL);
 
936
        }
 
937
 
 
938
        return FALSE;
 
939
}
 
940
 
 
941
static void
 
942
gdm_chooser_widget_shrink (GdmChooserWidget *widget)
 
943
{
 
944
        g_assert (widget->priv->should_hide_inactive_items == TRUE);
 
945
 
 
946
        if (widget->priv->state == GDM_CHOOSER_WIDGET_STATE_GROWING) {
 
947
                gdm_scrollable_widget_stop_sliding (GDM_SCROLLABLE_WIDGET (widget->priv->scrollable_widget));
 
948
        }
 
949
 
 
950
        set_frame_text (widget, widget->priv->active_text);
 
951
 
 
952
        gtk_alignment_set (GTK_ALIGNMENT (widget->priv->frame_alignment),
 
953
                           0.0, 0.0, 1.0, 0.0);
 
954
 
 
955
        clear_selection (widget);
 
956
 
 
957
        widget->priv->state = GDM_CHOOSER_WIDGET_STATE_SHRINKING;
 
958
 
 
959
        if (GTK_WIDGET_VISIBLE (widget)) {
 
960
                start_shrink_animation (widget);
 
961
        } else {
 
962
                skip_resize_animation (widget);
 
963
        }
 
964
}
 
965
 
 
966
static void
 
967
activate_from_row (GdmChooserWidget    *widget,
 
968
                   GtkTreeRowReference *row)
 
969
{
 
970
        g_assert (row != NULL);
 
971
        g_assert (gtk_tree_row_reference_valid (row));
 
972
 
 
973
        if (widget->priv->active_row != NULL) {
 
974
                gtk_tree_row_reference_free (widget->priv->active_row);
 
975
                widget->priv->active_row = NULL;
 
976
        }
 
977
 
 
978
        widget->priv->active_row = gtk_tree_row_reference_copy (row);
 
979
 
 
980
        if (widget->priv->should_hide_inactive_items) {
 
981
                g_debug ("GdmChooserWidget: will emit activated after resize");
 
982
                widget->priv->emit_activated_after_resize_animation = TRUE;
 
983
                gdm_chooser_widget_shrink (widget);
 
984
        } else {
 
985
                g_debug ("GdmChooserWidget: emitting activated");
 
986
                g_signal_emit (widget, signals[ACTIVATED], 0);
 
987
        }
 
988
}
 
989
 
 
990
static void
 
991
deactivate (GdmChooserWidget *widget)
 
992
{
 
993
        GtkTreePath *path;
 
994
 
 
995
        if (widget->priv->active_row == NULL) {
 
996
                return;
 
997
        }
 
998
 
 
999
        path = get_view_path_to_active_row (widget);
 
1000
 
 
1001
        gtk_tree_row_reference_free (widget->priv->active_row);
 
1002
        widget->priv->active_row = NULL;
 
1003
 
 
1004
        gtk_tree_view_set_cursor (GTK_TREE_VIEW (widget->priv->items_view),
 
1005
                                  path, NULL, FALSE);
 
1006
        gtk_tree_path_free (path);
 
1007
 
 
1008
        if (widget->priv->state != GDM_CHOOSER_WIDGET_STATE_GROWN) {
 
1009
                gdm_chooser_widget_grow (widget);
 
1010
        }
 
1011
 
 
1012
        g_signal_emit (widget, signals[DEACTIVATED], 0);
 
1013
}
 
1014
 
 
1015
void
 
1016
gdm_chooser_widget_activate_selected_item (GdmChooserWidget *widget)
 
1017
{
 
1018
        GtkTreeRowReference *row;
 
1019
        gboolean             is_already_active;
 
1020
        GtkTreePath         *path;
 
1021
        GtkTreeModel        *model;
 
1022
 
 
1023
        row = NULL;
 
1024
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
1025
        is_already_active = FALSE;
 
1026
 
 
1027
        path = NULL;
 
1028
 
 
1029
        get_selected_list_path (widget, &path);
 
1030
        if (path == NULL) {
 
1031
                g_debug ("GdmChooserWidget: no row selected");
 
1032
                return;
 
1033
        }
 
1034
 
 
1035
        if (widget->priv->active_row != NULL) {
 
1036
                GtkTreePath *active_path;
 
1037
 
 
1038
                active_path = gtk_tree_row_reference_get_path (widget->priv->active_row);
 
1039
 
 
1040
                if (gtk_tree_path_compare (path, active_path) == 0) {
 
1041
                        is_already_active = TRUE;
 
1042
                }
 
1043
                gtk_tree_path_free (active_path);
 
1044
        }
 
1045
        g_assert (path != NULL);
 
1046
        row = gtk_tree_row_reference_new (model, path);
 
1047
        gtk_tree_path_free (path);
 
1048
 
 
1049
        if (!is_already_active) {
 
1050
                activate_from_row (widget, row);
 
1051
        } else {
 
1052
                g_debug ("GdmChooserWidget: row is already active");
 
1053
        }
 
1054
        gtk_tree_row_reference_free (row);
 
1055
}
 
1056
 
 
1057
void
 
1058
gdm_chooser_widget_set_active_item (GdmChooserWidget *widget,
 
1059
                                    const char       *id)
 
1060
{
 
1061
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
1062
 
 
1063
        g_debug ("GdmChooserWidget: setting active item '%s'",
 
1064
                 id ? id : "(null)");
 
1065
 
 
1066
        if (id != NULL) {
 
1067
                activate_from_item_id (widget, id);
 
1068
        } else {
 
1069
                deactivate (widget);
 
1070
        }
 
1071
}
 
1072
 
 
1073
static void
 
1074
gdm_chooser_widget_set_property (GObject        *object,
 
1075
                                 guint           prop_id,
 
1076
                                 const GValue   *value,
 
1077
                                 GParamSpec     *pspec)
 
1078
{
 
1079
        GdmChooserWidget *self;
 
1080
 
 
1081
        self = GDM_CHOOSER_WIDGET (object);
 
1082
 
 
1083
        switch (prop_id) {
 
1084
 
 
1085
        case PROP_INACTIVE_TEXT:
 
1086
                g_free (self->priv->inactive_text);
 
1087
                self->priv->inactive_text = g_value_dup_string (value);
 
1088
 
 
1089
                if (self->priv->active_row == NULL) {
 
1090
                        set_frame_text (self, self->priv->inactive_text);
 
1091
                }
 
1092
                break;
 
1093
 
 
1094
        case PROP_ACTIVE_TEXT:
 
1095
                g_free (self->priv->active_text);
 
1096
                self->priv->active_text = g_value_dup_string (value);
 
1097
 
 
1098
                if (self->priv->active_row != NULL) {
 
1099
                        set_frame_text (self, self->priv->active_text);
 
1100
                }
 
1101
                break;
 
1102
 
 
1103
        default:
 
1104
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
1105
                break;
 
1106
        }
 
1107
}
 
1108
 
 
1109
static void
 
1110
gdm_chooser_widget_get_property (GObject        *object,
 
1111
                                 guint           prop_id,
 
1112
                                 GValue         *value,
 
1113
                                 GParamSpec     *pspec)
 
1114
{
 
1115
        GdmChooserWidget *self;
 
1116
 
 
1117
        self = GDM_CHOOSER_WIDGET (object);
 
1118
 
 
1119
        switch (prop_id) {
 
1120
        case PROP_INACTIVE_TEXT:
 
1121
                g_value_set_string (value, self->priv->inactive_text);
 
1122
                break;
 
1123
 
 
1124
        case PROP_ACTIVE_TEXT:
 
1125
                g_value_set_string (value, self->priv->active_text);
 
1126
                break;
 
1127
        case PROP_LIST_VISIBLE:
 
1128
                g_value_set_boolean (value, GTK_WIDGET_VISIBLE (self->priv->scrollable_widget));
 
1129
                break;
 
1130
        default:
 
1131
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
1132
                break;
 
1133
        }
 
1134
}
 
1135
 
 
1136
static void
 
1137
gdm_chooser_widget_dispose (GObject *object)
 
1138
{
 
1139
        GdmChooserWidget *widget;
 
1140
 
 
1141
        widget = GDM_CHOOSER_WIDGET (object);
 
1142
 
 
1143
        if (widget->priv->separator_row != NULL) {
 
1144
                gtk_tree_row_reference_free (widget->priv->separator_row);
 
1145
                widget->priv->separator_row = NULL;
 
1146
        }
 
1147
 
 
1148
        if (widget->priv->active_row != NULL) {
 
1149
                gtk_tree_row_reference_free (widget->priv->active_row);
 
1150
                widget->priv->active_row = NULL;
 
1151
        }
 
1152
 
 
1153
        if (widget->priv->inactive_text != NULL) {
 
1154
                g_free (widget->priv->inactive_text);
 
1155
                widget->priv->inactive_text = NULL;
 
1156
        }
 
1157
 
 
1158
        if (widget->priv->active_text != NULL) {
 
1159
                g_free (widget->priv->active_text);
 
1160
                widget->priv->active_text = NULL;
 
1161
        }
 
1162
 
 
1163
        if (widget->priv->in_use_message != NULL) {
 
1164
                g_free (widget->priv->in_use_message);
 
1165
                widget->priv->in_use_message = NULL;
 
1166
        }
 
1167
 
 
1168
        G_OBJECT_CLASS (gdm_chooser_widget_parent_class)->dispose (object);
 
1169
}
 
1170
 
 
1171
static void
 
1172
gdm_chooser_widget_hide (GtkWidget *widget)
 
1173
{
 
1174
        skip_resize_animation (GDM_CHOOSER_WIDGET (widget));
 
1175
        GTK_WIDGET_CLASS (gdm_chooser_widget_parent_class)->hide (widget);
 
1176
}
 
1177
 
 
1178
static void
 
1179
gdm_chooser_widget_show (GtkWidget *widget)
 
1180
{
 
1181
        skip_resize_animation (GDM_CHOOSER_WIDGET (widget));
 
1182
 
 
1183
        GTK_WIDGET_CLASS (gdm_chooser_widget_parent_class)->show (widget);
 
1184
}
 
1185
 
 
1186
static void
 
1187
gdm_chooser_widget_size_allocate (GtkWidget     *widget,
 
1188
                                  GtkAllocation *allocation)
 
1189
{
 
1190
        GdmChooserWidget *chooser_widget;
 
1191
 
 
1192
        GTK_WIDGET_CLASS (gdm_chooser_widget_parent_class)->size_allocate (widget, allocation);
 
1193
 
 
1194
        chooser_widget = GDM_CHOOSER_WIDGET (widget);
 
1195
 
 
1196
}
 
1197
 
 
1198
static gboolean
 
1199
gdm_chooser_widget_focus (GtkWidget        *widget,
 
1200
                          GtkDirectionType  direction)
 
1201
{
 
1202
        /* Since we only have one focusable child (the tree view),
 
1203
         * no matter which direction we're going the rules are the
 
1204
         * same:
 
1205
         *
 
1206
         *    1) if it's aready got focus, return FALSE to surrender
 
1207
         *    that focus.
 
1208
         *    2) if it doesn't already have focus, then grab it
 
1209
         */
 
1210
        if (GTK_CONTAINER (widget)->focus_child != NULL) {
 
1211
                g_debug ("GdmChooserWidget: not focusing - focus child not null");
 
1212
                return FALSE;
 
1213
        }
 
1214
 
 
1215
        _grab_focus (widget);
 
1216
 
 
1217
        return TRUE;
 
1218
}
 
1219
 
 
1220
static gboolean
 
1221
gdm_chooser_widget_focus_in_event (GtkWidget     *widget,
 
1222
                                   GdkEventFocus *focus_event)
 
1223
{
 
1224
        /* We don't ever want the chooser widget itself to have focus.
 
1225
         * Focus should always go to the tree view.
 
1226
         */
 
1227
        _grab_focus (widget);
 
1228
 
 
1229
        return FALSE;
 
1230
}
 
1231
 
 
1232
static void
 
1233
gdm_chooser_widget_class_init (GdmChooserWidgetClass *klass)
 
1234
{
 
1235
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
 
1236
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
1237
 
 
1238
        object_class->get_property = gdm_chooser_widget_get_property;
 
1239
        object_class->set_property = gdm_chooser_widget_set_property;
 
1240
        object_class->dispose = gdm_chooser_widget_dispose;
 
1241
        object_class->finalize = gdm_chooser_widget_finalize;
 
1242
        widget_class->size_allocate = gdm_chooser_widget_size_allocate;
 
1243
        widget_class->hide = gdm_chooser_widget_hide;
 
1244
        widget_class->show = gdm_chooser_widget_show;
 
1245
        widget_class->focus = gdm_chooser_widget_focus;
 
1246
        widget_class->focus_in_event = gdm_chooser_widget_focus_in_event;
 
1247
 
 
1248
        signals [LOADED] = g_signal_new ("loaded",
 
1249
                                         G_TYPE_FROM_CLASS (object_class),
 
1250
                                         G_SIGNAL_RUN_LAST,
 
1251
                                         G_STRUCT_OFFSET (GdmChooserWidgetClass, loaded),
 
1252
                                         NULL,
 
1253
                                         NULL,
 
1254
                                         g_cclosure_marshal_VOID__VOID,
 
1255
                                         G_TYPE_NONE,
 
1256
                                         0);
 
1257
 
 
1258
        signals [ACTIVATED] = g_signal_new ("activated",
 
1259
                                            G_TYPE_FROM_CLASS (object_class),
 
1260
                                            G_SIGNAL_RUN_LAST,
 
1261
                                            G_STRUCT_OFFSET (GdmChooserWidgetClass, activated),
 
1262
                                            NULL,
 
1263
                                            NULL,
 
1264
                                            g_cclosure_marshal_VOID__VOID,
 
1265
                                            G_TYPE_NONE,
 
1266
                                            0);
 
1267
 
 
1268
        signals [DEACTIVATED] = g_signal_new ("deactivated",
 
1269
                                              G_TYPE_FROM_CLASS (object_class),
 
1270
                                              G_SIGNAL_RUN_LAST,
 
1271
                                              G_STRUCT_OFFSET (GdmChooserWidgetClass, deactivated),
 
1272
                                              NULL,
 
1273
                                              NULL,
 
1274
                                              g_cclosure_marshal_VOID__VOID,
 
1275
                                              G_TYPE_NONE,
 
1276
                                              0);
 
1277
 
 
1278
        g_object_class_install_property (object_class,
 
1279
                                         PROP_INACTIVE_TEXT,
 
1280
                                         g_param_spec_string ("inactive-text",
 
1281
                                                              _("Inactive Text"),
 
1282
                                                              _("The text to use in the label if the "
 
1283
                                                                "user hasn't picked an item yet"),
 
1284
                                                              NULL,
 
1285
                                                              (G_PARAM_READWRITE |
 
1286
                                                               G_PARAM_CONSTRUCT)));
 
1287
        g_object_class_install_property (object_class,
 
1288
                                         PROP_ACTIVE_TEXT,
 
1289
                                         g_param_spec_string ("active-text",
 
1290
                                                              _("Active Text"),
 
1291
                                                              _("The text to use in the label if the "
 
1292
                                                                "user has picked an item"),
 
1293
                                                              NULL,
 
1294
                                                              (G_PARAM_READWRITE |
 
1295
                                                               G_PARAM_CONSTRUCT)));
 
1296
 
 
1297
        g_object_class_install_property (object_class,
 
1298
                                         PROP_LIST_VISIBLE,
 
1299
                                         g_param_spec_boolean ("list-visible",
 
1300
                                                              _("List Visible"),
 
1301
                                                              _("Whether the chooser list is visible"),
 
1302
                                                              TRUE,
 
1303
                                                              G_PARAM_READABLE));
 
1304
 
 
1305
        g_type_class_add_private (klass, sizeof (GdmChooserWidgetPrivate));
 
1306
}
 
1307
 
 
1308
static void
 
1309
on_row_activated (GtkTreeView          *tree_view,
 
1310
                  GtkTreePath          *tree_path,
 
1311
                  GtkTreeViewColumn    *tree_column,
 
1312
                  GdmChooserWidget     *widget)
 
1313
{
 
1314
        char *path_str;
 
1315
 
 
1316
        path_str = gtk_tree_path_to_string (tree_path);
 
1317
        g_debug ("GdmChooserWidget: row activated '%s'", path_str ? path_str : "(null)");
 
1318
        g_free (path_str);
 
1319
        gdm_chooser_widget_activate_selected_item (widget);
 
1320
}
 
1321
 
 
1322
static gboolean
 
1323
path_is_separator (GdmChooserWidget *widget,
 
1324
                   GtkTreeModel     *model,
 
1325
                   GtkTreePath      *path)
 
1326
{
 
1327
        GtkTreePath      *separator_path;
 
1328
        GtkTreePath      *translated_path;
 
1329
        gboolean          is_separator;
 
1330
 
 
1331
        separator_path = gtk_tree_row_reference_get_path (widget->priv->separator_row);
 
1332
 
 
1333
        if (separator_path == NULL) {
 
1334
                return FALSE;
 
1335
        }
 
1336
 
 
1337
        if (model == GTK_TREE_MODEL (widget->priv->model_sorter)) {
 
1338
                GtkTreePath *filtered_path;
 
1339
 
 
1340
                filtered_path = gtk_tree_model_sort_convert_path_to_child_path (widget->priv->model_sorter, path);
 
1341
 
 
1342
                translated_path = gtk_tree_model_filter_convert_path_to_child_path (widget->priv->model_filter, filtered_path);
 
1343
                gtk_tree_path_free (filtered_path);
 
1344
        } else if (model == GTK_TREE_MODEL (widget->priv->model_filter)) {
 
1345
                translated_path = gtk_tree_model_filter_convert_path_to_child_path (widget->priv->model_filter, path);
 
1346
        } else {
 
1347
                g_assert (model == GTK_TREE_MODEL (widget->priv->list_store));
 
1348
                translated_path = gtk_tree_path_copy (path);
 
1349
        }
 
1350
 
 
1351
        if (gtk_tree_path_compare (separator_path, translated_path) == 0) {
 
1352
                is_separator = TRUE;
 
1353
        } else {
 
1354
                is_separator = FALSE;
 
1355
        }
 
1356
        gtk_tree_path_free (translated_path);
 
1357
 
 
1358
        return is_separator;
 
1359
}
 
1360
 
 
1361
static int
 
1362
compare_item  (GtkTreeModel *model,
 
1363
               GtkTreeIter  *a,
 
1364
               GtkTreeIter  *b,
 
1365
               gpointer      data)
 
1366
{
 
1367
        GdmChooserWidget *widget;
 
1368
        char             *name_a;
 
1369
        char             *name_b;
 
1370
        char             *text_a;
 
1371
        char             *text_b;
 
1372
        gulong            prio_a;
 
1373
        gulong            prio_b;
 
1374
        gboolean          is_separate_a;
 
1375
        gboolean          is_separate_b;
 
1376
        int               result;
 
1377
        int               direction;
 
1378
        GtkTreeIter      *separator_iter;
 
1379
        char             *id;
 
1380
        PangoAttrList    *attrs;
 
1381
 
 
1382
        g_assert (GDM_IS_CHOOSER_WIDGET (data));
 
1383
 
 
1384
        widget = GDM_CHOOSER_WIDGET (data);
 
1385
 
 
1386
        separator_iter = NULL;
 
1387
        if (widget->priv->separator_row != NULL) {
 
1388
 
 
1389
                GtkTreePath      *path_a;
 
1390
                GtkTreePath      *path_b;
 
1391
 
 
1392
                path_a = gtk_tree_model_get_path (model, a);
 
1393
                path_b = gtk_tree_model_get_path (model, b);
 
1394
 
 
1395
                if (path_is_separator (widget, model, path_a)) {
 
1396
                        separator_iter = a;
 
1397
                } else if (path_is_separator (widget, model, path_b)) {
 
1398
                        separator_iter = b;
 
1399
                }
 
1400
 
 
1401
                gtk_tree_path_free (path_a);
 
1402
                gtk_tree_path_free (path_b);
 
1403
        }
 
1404
 
 
1405
        name_a = NULL;
 
1406
        is_separate_a = FALSE;
 
1407
        if (separator_iter != a) {
 
1408
                gtk_tree_model_get (model, a,
 
1409
                                    CHOOSER_NAME_COLUMN, &name_a,
 
1410
                                    CHOOSER_PRIORITY_COLUMN, &prio_a,
 
1411
                                    CHOOSER_ITEM_IS_SEPARATED_COLUMN, &is_separate_a,
 
1412
                                    -1);
 
1413
        }
 
1414
 
 
1415
        name_b = NULL;
 
1416
        is_separate_b = FALSE;
 
1417
        if (separator_iter != b) {
 
1418
                gtk_tree_model_get (model, b,
 
1419
                                    CHOOSER_NAME_COLUMN, &name_b,
 
1420
                                    CHOOSER_ID_COLUMN, &id,
 
1421
                                    CHOOSER_PRIORITY_COLUMN, &prio_b,
 
1422
                                    CHOOSER_ITEM_IS_SEPARATED_COLUMN, &is_separate_b,
 
1423
                                    -1);
 
1424
        }
 
1425
 
 
1426
        if (widget->priv->separator_position == GDM_CHOOSER_WIDGET_POSITION_TOP) {
 
1427
                direction = -1;
 
1428
        } else {
 
1429
                direction = 1;
 
1430
        }
 
1431
 
 
1432
        if (separator_iter == b) {
 
1433
                result = is_separate_a? 1 : -1;
 
1434
                result *= direction;
 
1435
        } else if (separator_iter == a) {
 
1436
                result = is_separate_b? -1 : 1;
 
1437
                result *= direction;
 
1438
        } else if (is_separate_b == is_separate_a) {
 
1439
                if (prio_a == prio_b) {
 
1440
                        pango_parse_markup (name_a, -1, 0, &attrs, &text_a, NULL, NULL);
 
1441
                        pango_parse_markup (name_b, -1, 0, &attrs, &text_b, NULL, NULL);
 
1442
                        if (text_a && text_b)
 
1443
                            result = g_utf8_collate (text_a, text_b);
 
1444
                        else
 
1445
                            result = g_utf8_collate (name_a, name_b);
 
1446
                        g_free (text_a);
 
1447
                        g_free (text_b);
 
1448
                } else if (prio_a > prio_b) {
 
1449
                        result = -1;
 
1450
                } else {
 
1451
                        result = 1;
 
1452
                }
 
1453
        } else {
 
1454
                result = is_separate_a - is_separate_b;
 
1455
                result *= direction;
 
1456
        }
 
1457
 
 
1458
        g_free (name_a);
 
1459
        g_free (name_b);
 
1460
 
 
1461
        return result;
 
1462
}
 
1463
 
 
1464
static void
 
1465
name_cell_data_func (GtkTreeViewColumn  *tree_column,
 
1466
                     GtkCellRenderer    *cell,
 
1467
                     GtkTreeModel       *model,
 
1468
                     GtkTreeIter        *iter,
 
1469
                     GdmChooserWidget   *widget)
 
1470
{
 
1471
        gboolean is_in_use;
 
1472
        char    *name;
 
1473
        char    *markup;
 
1474
 
 
1475
        name = NULL;
 
1476
        gtk_tree_model_get (model,
 
1477
                            iter,
 
1478
                            CHOOSER_ITEM_IS_IN_USE_COLUMN, &is_in_use,
 
1479
                            CHOOSER_NAME_COLUMN, &name,
 
1480
                            -1);
 
1481
 
 
1482
        if (is_in_use) {
 
1483
                markup = g_strdup_printf ("<b>%s</b>\n"
 
1484
                                          "<i><span size=\"x-small\">%s</span></i>",
 
1485
                                          name ? name : "(null)", widget->priv->in_use_message);
 
1486
        } else {
 
1487
                markup = g_strdup_printf ("%s", name ? name : "(null)");
 
1488
        }
 
1489
        g_free (name);
 
1490
 
 
1491
        g_object_set (cell, "markup", markup, NULL);
 
1492
        g_free (markup);
 
1493
}
 
1494
 
 
1495
static void
 
1496
check_cell_data_func (GtkTreeViewColumn    *tree_column,
 
1497
                      GtkCellRenderer      *cell,
 
1498
                      GtkTreeModel         *model,
 
1499
                      GtkTreeIter          *iter,
 
1500
                      GdmChooserWidget     *widget)
 
1501
{
 
1502
        gboolean   is_in_use;
 
1503
        GdkPixbuf *pixbuf;
 
1504
 
 
1505
        gtk_tree_model_get (model,
 
1506
                            iter,
 
1507
                            CHOOSER_ITEM_IS_IN_USE_COLUMN, &is_in_use,
 
1508
                            -1);
 
1509
 
 
1510
        if (is_in_use) {
 
1511
                pixbuf = widget->priv->is_in_use_pixbuf;
 
1512
        } else {
 
1513
                pixbuf = NULL;
 
1514
        }
 
1515
 
 
1516
        g_object_set (cell, "pixbuf", pixbuf, NULL);
 
1517
}
 
1518
 
 
1519
static GdkPixbuf *
 
1520
get_is_in_use_pixbuf (GdmChooserWidget *widget)
 
1521
{
 
1522
        GtkIconTheme *theme;
 
1523
        GdkPixbuf    *pixbuf;
 
1524
 
 
1525
        theme = gtk_icon_theme_get_default ();
 
1526
        pixbuf = gtk_icon_theme_load_icon (theme,
 
1527
                                           "emblem-default",
 
1528
                                           GDM_CHOOSER_WIDGET_DEFAULT_ICON_SIZE / 3,
 
1529
                                           0,
 
1530
                                           NULL);
 
1531
 
 
1532
        return pixbuf;
 
1533
}
 
1534
 
 
1535
static gboolean
 
1536
separator_func (GtkTreeModel *model,
 
1537
                GtkTreeIter  *iter,
 
1538
                gpointer      data)
 
1539
{
 
1540
        GdmChooserWidget *widget;
 
1541
        GtkTreePath      *path;
 
1542
        gboolean          is_separator;
 
1543
 
 
1544
        g_assert (GDM_IS_CHOOSER_WIDGET (data));
 
1545
 
 
1546
        widget = GDM_CHOOSER_WIDGET (data);
 
1547
 
 
1548
        g_assert (widget->priv->separator_row != NULL);
 
1549
 
 
1550
        path = gtk_tree_model_get_path (model, iter);
 
1551
 
 
1552
        is_separator = path_is_separator (widget, model, path);
 
1553
 
 
1554
        gtk_tree_path_free (path);
 
1555
 
 
1556
        return is_separator;
 
1557
}
 
1558
 
 
1559
static void
 
1560
add_separator (GdmChooserWidget *widget)
 
1561
{
 
1562
        GtkTreeIter   iter;
 
1563
        GtkTreeModel *model;
 
1564
        GtkTreePath  *path;
 
1565
 
 
1566
        g_assert (widget->priv->separator_row == NULL);
 
1567
 
 
1568
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
1569
 
 
1570
        gtk_list_store_insert_with_values (widget->priv->list_store,
 
1571
                                           &iter, 0,
 
1572
                                           CHOOSER_ID_COLUMN, "-", -1);
 
1573
        path = gtk_tree_model_get_path (model, &iter);
 
1574
        widget->priv->separator_row = gtk_tree_row_reference_new (model, path);
 
1575
        gtk_tree_path_free (path);
 
1576
}
 
1577
 
 
1578
static gboolean
 
1579
update_column_visibility (GdmChooserWidget *widget)
 
1580
{
 
1581
        if (widget->priv->number_of_rows_with_images > 0) {
 
1582
                gtk_tree_view_column_set_visible (widget->priv->image_column,
 
1583
                                                  TRUE);
 
1584
        } else {
 
1585
                gtk_tree_view_column_set_visible (widget->priv->image_column,
 
1586
                                                  FALSE);
 
1587
        }
 
1588
        if (widget->priv->number_of_rows_with_status > 0) {
 
1589
                gtk_tree_view_column_set_visible (widget->priv->status_column,
 
1590
                                                  TRUE);
 
1591
        } else {
 
1592
                gtk_tree_view_column_set_visible (widget->priv->status_column,
 
1593
                                                  FALSE);
 
1594
        }
 
1595
 
 
1596
        return FALSE;
 
1597
}
 
1598
 
 
1599
static void
 
1600
clear_canceled_visibility_update (GdmChooserWidget *widget)
 
1601
{
 
1602
        widget->priv->update_idle_id = 0;
 
1603
}
 
1604
 
 
1605
static void
 
1606
queue_column_visibility_update (GdmChooserWidget *widget)
 
1607
{
 
1608
        if (widget->priv->update_idle_id == 0) {
 
1609
                widget->priv->update_idle_id =
 
1610
                        g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
 
1611
                                         (GSourceFunc)
 
1612
                                         update_column_visibility, widget,
 
1613
                                         (GDestroyNotify)
 
1614
                                         clear_canceled_visibility_update);
 
1615
        }
 
1616
}
 
1617
 
 
1618
static void
 
1619
on_row_changed (GtkTreeModel     *model,
 
1620
                GtkTreePath      *path,
 
1621
                GtkTreeIter      *iter,
 
1622
                GdmChooserWidget *widget)
 
1623
{
 
1624
        queue_column_visibility_update (widget);
 
1625
}
 
1626
 
 
1627
static void
 
1628
add_frame (GdmChooserWidget *widget)
 
1629
{
 
1630
        widget->priv->frame = gtk_frame_new (NULL);
 
1631
        gtk_frame_set_shadow_type (GTK_FRAME (widget->priv->frame),
 
1632
                                   GTK_SHADOW_NONE);
 
1633
        gtk_widget_show (widget->priv->frame);
 
1634
        gtk_container_add (GTK_CONTAINER (widget), widget->priv->frame);
 
1635
 
 
1636
        widget->priv->frame_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
 
1637
        gtk_widget_show (widget->priv->frame_alignment);
 
1638
        gtk_container_add (GTK_CONTAINER (widget->priv->frame),
 
1639
                           widget->priv->frame_alignment);
 
1640
}
 
1641
 
 
1642
static gboolean
 
1643
on_button_release (GtkTreeView      *items_view,
 
1644
                   GdkEventButton   *event,
 
1645
                   GdmChooserWidget *widget)
 
1646
{
 
1647
        GtkTreeModel     *model;
 
1648
        GtkTreeIter       iter;
 
1649
        GtkTreeSelection *selection;
 
1650
 
 
1651
        if (!widget->priv->should_hide_inactive_items) {
 
1652
                return FALSE;
 
1653
        }
 
1654
 
 
1655
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget->priv->items_view));
 
1656
        if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
 
1657
                GtkTreePath *path;
 
1658
 
 
1659
                path = gtk_tree_model_get_path (model, &iter);
 
1660
                gtk_tree_view_row_activated (GTK_TREE_VIEW (items_view),
 
1661
                                             path, NULL);
 
1662
                gtk_tree_path_free (path);
 
1663
        }
 
1664
 
 
1665
        return FALSE;
 
1666
}
 
1667
 
 
1668
static gboolean
 
1669
search_equal_func (GtkTreeModel     *model,
 
1670
                   int               column,
 
1671
                   const char       *key,
 
1672
                   GtkTreeIter      *iter,
 
1673
                   GdmChooserWidget *widget)
 
1674
{
 
1675
        char       *id;
 
1676
        char       *name;
 
1677
        char       *key_folded;
 
1678
        gboolean    ret;
 
1679
 
 
1680
        if (key == NULL) {
 
1681
                return FALSE;
 
1682
        }
 
1683
 
 
1684
        ret = TRUE;
 
1685
        id = NULL;
 
1686
        name = NULL;
 
1687
 
 
1688
        key_folded = g_utf8_casefold (key, -1);
 
1689
 
 
1690
        gtk_tree_model_get (model,
 
1691
                            iter,
 
1692
                            CHOOSER_ID_COLUMN, &id,
 
1693
                            CHOOSER_NAME_COLUMN, &name,
 
1694
                            -1);
 
1695
        if (name != NULL) {
 
1696
                char *name_folded;
 
1697
 
 
1698
                name_folded = g_utf8_casefold (name, -1);
 
1699
                ret = !g_str_has_prefix (name_folded, key_folded);
 
1700
                g_free (name_folded);
 
1701
 
 
1702
                if (!ret) {
 
1703
                        goto out;
 
1704
                }
 
1705
        }
 
1706
 
 
1707
        if (id != NULL) {
 
1708
                char *id_folded;
 
1709
 
 
1710
 
 
1711
                id_folded = g_utf8_casefold (id, -1);
 
1712
                ret = !g_str_has_prefix (id_folded, key_folded);
 
1713
                g_free (id_folded);
 
1714
 
 
1715
                if (!ret) {
 
1716
                        goto out;
 
1717
                }
 
1718
        }
 
1719
 out:
 
1720
        g_free (id);
 
1721
        g_free (name);
 
1722
        g_free (key_folded);
 
1723
 
 
1724
        return ret;
 
1725
}
 
1726
 
 
1727
static void
 
1728
search_position_func (GtkTreeView *tree_view,
 
1729
                      GtkWidget   *search_dialog,
 
1730
                      gpointer     user_data)
 
1731
{
 
1732
        /* Move it outside the region viewable by
 
1733
         * the user.
 
1734
         * FIXME: This is pretty inelegant.
 
1735
         *
 
1736
         * It might be nicer to make a GdmOffscreenBin
 
1737
         * widget that we pack into the chooser widget below
 
1738
         * the frame but gets redirected offscreen.
 
1739
         *
 
1740
         * Then we would add a GtkEntry to the bin and set
 
1741
         * that entry as the search entry for the tree view
 
1742
         * instead of using a search position func.
 
1743
         */
 
1744
        gtk_window_move (GTK_WINDOW (search_dialog), -24000, -24000);
 
1745
}
 
1746
 
 
1747
static void
 
1748
on_selection_changed (GtkTreeSelection *selection,
 
1749
                      GdmChooserWidget *widget)
 
1750
{
 
1751
        GtkTreePath *path;
 
1752
 
 
1753
        get_selected_list_path (widget, &path);
 
1754
        if (path != NULL) {
 
1755
                char *path_str;
 
1756
                path_str = gtk_tree_path_to_string (path);
 
1757
                g_debug ("GdmChooserWidget: selection change to list path '%s'", path_str);
 
1758
                g_free (path_str);
 
1759
        } else {
 
1760
                g_debug ("GdmChooserWidget: selection cleared");
 
1761
        }
 
1762
}
 
1763
 
 
1764
static void
 
1765
gdm_chooser_widget_init (GdmChooserWidget *widget)
 
1766
{
 
1767
        GtkTreeViewColumn *column;
 
1768
        GtkTreeSelection  *selection;
 
1769
        GtkCellRenderer   *renderer;
 
1770
 
 
1771
        widget->priv = GDM_CHOOSER_WIDGET_GET_PRIVATE (widget);
 
1772
 
 
1773
        /* Even though, we're a container and also don't ever take
 
1774
         * focus for ourselve, we set CAN_FOCUS so that gtk_widget_grab_focus
 
1775
         * works on us.  We then override grab_focus requests to
 
1776
         * be redirected our internal tree view
 
1777
         */
 
1778
        GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
 
1779
 
 
1780
        gtk_alignment_set_padding (GTK_ALIGNMENT (widget), 0, 0, 0, 0);
 
1781
 
 
1782
        add_frame (widget);
 
1783
 
 
1784
        widget->priv->scrollable_widget = gdm_scrollable_widget_new ();
 
1785
        gtk_widget_show (widget->priv->scrollable_widget);
 
1786
        gtk_container_add (GTK_CONTAINER (widget->priv->frame_alignment),
 
1787
                           widget->priv->scrollable_widget);
 
1788
 
 
1789
        widget->priv->items_view = gtk_tree_view_new ();
 
1790
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (widget->priv->items_view),
 
1791
                                           FALSE);
 
1792
        g_signal_connect (widget->priv->items_view,
 
1793
                          "row-activated",
 
1794
                          G_CALLBACK (on_row_activated),
 
1795
                          widget);
 
1796
 
 
1797
        gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (widget->priv->items_view),
 
1798
                                             (GtkTreeViewSearchEqualFunc)search_equal_func,
 
1799
                                             widget,
 
1800
                                             NULL);
 
1801
 
 
1802
        gtk_tree_view_set_search_position_func (GTK_TREE_VIEW (widget->priv->items_view),
 
1803
                                                (GtkTreeViewSearchPositionFunc)search_position_func,
 
1804
                                                widget,
 
1805
                                                NULL);
 
1806
 
 
1807
        /* hack to make single-click activate work
 
1808
         */
 
1809
        g_signal_connect_after (widget->priv->items_view,
 
1810
                               "button-release-event",
 
1811
                               G_CALLBACK (on_button_release),
 
1812
                               widget);
 
1813
 
 
1814
        gtk_widget_show (widget->priv->items_view);
 
1815
        gtk_container_add (GTK_CONTAINER (widget->priv->scrollable_widget),
 
1816
                           widget->priv->items_view);
 
1817
 
 
1818
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget->priv->items_view));
 
1819
        gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
 
1820
 
 
1821
        g_signal_connect (selection, "changed", G_CALLBACK (on_selection_changed), widget);
 
1822
 
 
1823
        g_assert (NUMBER_OF_CHOOSER_COLUMNS == 11);
 
1824
        widget->priv->list_store = gtk_list_store_new (NUMBER_OF_CHOOSER_COLUMNS,
 
1825
                                                       GDK_TYPE_PIXBUF,
 
1826
                                                       G_TYPE_STRING,
 
1827
                                                       G_TYPE_STRING,
 
1828
                                                       G_TYPE_ULONG,
 
1829
                                                       G_TYPE_BOOLEAN,
 
1830
                                                       G_TYPE_BOOLEAN,
 
1831
                                                       G_TYPE_BOOLEAN,
 
1832
                                                       G_TYPE_DOUBLE,
 
1833
                                                       G_TYPE_DOUBLE,
 
1834
                                                       G_TYPE_DOUBLE,
 
1835
                                                       G_TYPE_STRING);
 
1836
 
 
1837
        widget->priv->model_filter = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (widget->priv->list_store), NULL));
 
1838
 
 
1839
        gtk_tree_model_filter_set_visible_column (widget->priv->model_filter,
 
1840
                                                  CHOOSER_ITEM_IS_VISIBLE_COLUMN);
 
1841
        g_signal_connect (G_OBJECT (widget->priv->model_filter), "row-changed",
 
1842
                          G_CALLBACK (on_row_changed), widget);
 
1843
 
 
1844
        widget->priv->model_sorter = GTK_TREE_MODEL_SORT (gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (widget->priv->model_filter)));
 
1845
 
 
1846
        gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (widget->priv->model_sorter),
 
1847
                                         CHOOSER_ID_COLUMN,
 
1848
                                         compare_item,
 
1849
                                         widget, NULL);
 
1850
 
 
1851
        gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (widget->priv->model_sorter),
 
1852
                                              CHOOSER_ID_COLUMN,
 
1853
                                              GTK_SORT_ASCENDING);
 
1854
        gtk_tree_view_set_model (GTK_TREE_VIEW (widget->priv->items_view),
 
1855
                                 GTK_TREE_MODEL (widget->priv->model_sorter));
 
1856
        gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (widget->priv->items_view),
 
1857
                                              separator_func,
 
1858
                                              widget, NULL);
 
1859
 
 
1860
        /* IMAGE COLUMN */
 
1861
        renderer = gtk_cell_renderer_pixbuf_new ();
 
1862
        column = gtk_tree_view_column_new ();
 
1863
        gtk_tree_view_column_pack_start (column, renderer, FALSE);
 
1864
        gtk_tree_view_append_column (GTK_TREE_VIEW (widget->priv->items_view), column);
 
1865
        widget->priv->image_column = column;
 
1866
 
 
1867
        gtk_tree_view_column_set_attributes (column,
 
1868
                                             renderer,
 
1869
                                             "pixbuf", CHOOSER_IMAGE_COLUMN,
 
1870
                                             NULL);
 
1871
 
 
1872
        g_object_set (renderer,
 
1873
                      "xalign", 1.0,
 
1874
                      NULL);
 
1875
 
 
1876
        /* NAME COLUMN */
 
1877
        renderer = gtk_cell_renderer_text_new ();
 
1878
        column = gtk_tree_view_column_new ();
 
1879
        gtk_tree_view_column_pack_start (column, renderer, FALSE);
 
1880
        gtk_tree_view_append_column (GTK_TREE_VIEW (widget->priv->items_view), column);
 
1881
        gtk_tree_view_column_set_cell_data_func (column,
 
1882
                                                 renderer,
 
1883
                                                 (GtkTreeCellDataFunc) name_cell_data_func,
 
1884
                                                 widget,
 
1885
                                                 NULL);
 
1886
 
 
1887
        gtk_tree_view_set_tooltip_column (GTK_TREE_VIEW (widget->priv->items_view),
 
1888
                                          CHOOSER_COMMENT_COLUMN);
 
1889
 
 
1890
        /* STATUS COLUMN */
 
1891
        renderer = gtk_cell_renderer_pixbuf_new ();
 
1892
        column = gtk_tree_view_column_new ();
 
1893
        gtk_tree_view_column_pack_start (column, renderer, FALSE);
 
1894
        gtk_tree_view_append_column (GTK_TREE_VIEW (widget->priv->items_view), column);
 
1895
        widget->priv->status_column = column;
 
1896
 
 
1897
        gtk_tree_view_column_set_cell_data_func (column,
 
1898
                                                 renderer,
 
1899
                                                 (GtkTreeCellDataFunc) check_cell_data_func,
 
1900
                                                 widget,
 
1901
                                                 NULL);
 
1902
        widget->priv->is_in_use_pixbuf = get_is_in_use_pixbuf (widget);
 
1903
 
 
1904
        renderer = gdm_cell_renderer_timer_new ();
 
1905
        gtk_tree_view_column_pack_start (column, renderer, FALSE);
 
1906
        gtk_tree_view_column_add_attribute (column, renderer, "value",
 
1907
                                            CHOOSER_TIMER_VALUE_COLUMN);
 
1908
 
 
1909
        widget->priv->rows_with_timers =
 
1910
            g_hash_table_new_full (g_str_hash,
 
1911
                                   g_str_equal,
 
1912
                                  (GDestroyNotify) g_free,
 
1913
                                  (GDestroyNotify)
 
1914
                                  gtk_tree_row_reference_free);
 
1915
 
 
1916
        add_separator (widget);
 
1917
        queue_column_visibility_update (widget);
 
1918
}
 
1919
 
 
1920
static void
 
1921
gdm_chooser_widget_finalize (GObject *object)
 
1922
{
 
1923
        GdmChooserWidget *widget;
 
1924
 
 
1925
        g_return_if_fail (object != NULL);
 
1926
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (object));
 
1927
 
 
1928
        widget = GDM_CHOOSER_WIDGET (object);
 
1929
 
 
1930
        g_return_if_fail (widget->priv != NULL);
 
1931
 
 
1932
        g_hash_table_destroy (widget->priv->rows_with_timers);
 
1933
        widget->priv->rows_with_timers = NULL;
 
1934
 
 
1935
        G_OBJECT_CLASS (gdm_chooser_widget_parent_class)->finalize (object);
 
1936
}
 
1937
 
 
1938
GtkWidget *
 
1939
gdm_chooser_widget_new (const char *inactive_text,
 
1940
                        const char *active_text)
 
1941
{
 
1942
        GObject *object;
 
1943
 
 
1944
        object = g_object_new (GDM_TYPE_CHOOSER_WIDGET,
 
1945
                               "inactive-text", inactive_text,
 
1946
                               "active-text", active_text, NULL);
 
1947
 
 
1948
        return GTK_WIDGET (object);
 
1949
}
 
1950
 
 
1951
void
 
1952
gdm_chooser_widget_update_item (GdmChooserWidget *widget,
 
1953
                                const char       *id,
 
1954
                                GdkPixbuf        *new_image,
 
1955
                                const char       *new_name,
 
1956
                                const char       *new_comment,
 
1957
                                gulong            new_priority,
 
1958
                                gboolean          new_in_use,
 
1959
                                gboolean          new_is_separate)
 
1960
{
 
1961
        GtkTreeModel *model;
 
1962
        GtkTreeIter   iter;
 
1963
        GdkPixbuf    *image;
 
1964
        gboolean      is_separate;
 
1965
        gboolean      in_use;
 
1966
 
 
1967
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
1968
 
 
1969
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
1970
 
 
1971
        if (!find_item (widget, id, &iter)) {
 
1972
                g_critical ("Tried to remove non-existing item from chooser");
 
1973
                return;
 
1974
        }
 
1975
 
 
1976
        is_separate = FALSE;
 
1977
        gtk_tree_model_get (model, &iter,
 
1978
                            CHOOSER_IMAGE_COLUMN, &image,
 
1979
                            CHOOSER_ITEM_IS_IN_USE_COLUMN, &in_use,
 
1980
                            CHOOSER_ITEM_IS_SEPARATED_COLUMN, &is_separate,
 
1981
                            -1);
 
1982
 
 
1983
        if (image != new_image) {
 
1984
                if (image == NULL && new_image != NULL) {
 
1985
                        widget->priv->number_of_rows_with_images++;
 
1986
                } else if (image != NULL && new_image == NULL) {
 
1987
                        widget->priv->number_of_rows_with_images--;
 
1988
                }
 
1989
                queue_column_visibility_update (widget);
 
1990
        }
 
1991
        if (image != NULL) {
 
1992
                g_object_unref (image);
 
1993
        }
 
1994
 
 
1995
        if (in_use != new_in_use) {
 
1996
                if (new_in_use) {
 
1997
                        widget->priv->number_of_rows_with_status++;
 
1998
                } else {
 
1999
                        widget->priv->number_of_rows_with_status--;
 
2000
                }
 
2001
                queue_column_visibility_update (widget);
 
2002
        }
 
2003
 
 
2004
        if (is_separate != new_is_separate) {
 
2005
                if (new_is_separate) {
 
2006
                        widget->priv->number_of_separated_rows++;
 
2007
                        widget->priv->number_of_normal_rows--;
 
2008
                } else {
 
2009
                        widget->priv->number_of_separated_rows--;
 
2010
                        widget->priv->number_of_normal_rows++;
 
2011
                }
 
2012
                update_separator_visibility (widget);
 
2013
        }
 
2014
 
 
2015
        gtk_list_store_set (widget->priv->list_store,
 
2016
                            &iter,
 
2017
                            CHOOSER_IMAGE_COLUMN, new_image,
 
2018
                            CHOOSER_NAME_COLUMN, new_name,
 
2019
                            CHOOSER_COMMENT_COLUMN, new_comment,
 
2020
                            CHOOSER_PRIORITY_COLUMN, new_priority,
 
2021
                            CHOOSER_ITEM_IS_IN_USE_COLUMN, new_in_use,
 
2022
                            CHOOSER_ITEM_IS_SEPARATED_COLUMN, new_is_separate,
 
2023
                            -1);
 
2024
}
 
2025
 
 
2026
void
 
2027
gdm_chooser_widget_add_item (GdmChooserWidget *widget,
 
2028
                             const char       *id,
 
2029
                             GdkPixbuf        *image,
 
2030
                             const char       *name,
 
2031
                             const char       *comment,
 
2032
                             gulong            priority,
 
2033
                             gboolean          in_use,
 
2034
                             gboolean          keep_separate)
 
2035
{
 
2036
        gboolean is_visible;
 
2037
 
 
2038
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
2039
 
 
2040
        if (keep_separate) {
 
2041
                widget->priv->number_of_separated_rows++;
 
2042
        } else {
 
2043
                widget->priv->number_of_normal_rows++;
 
2044
        }
 
2045
        update_separator_visibility (widget);
 
2046
 
 
2047
        if (in_use) {
 
2048
                widget->priv->number_of_rows_with_status++;
 
2049
                queue_column_visibility_update (widget);
 
2050
        }
 
2051
 
 
2052
        if (image != NULL) {
 
2053
                widget->priv->number_of_rows_with_images++;
 
2054
                queue_column_visibility_update (widget);
 
2055
        }
 
2056
 
 
2057
        is_visible = widget->priv->active_row == NULL;
 
2058
 
 
2059
        gtk_list_store_insert_with_values (widget->priv->list_store,
 
2060
                                           NULL, 0,
 
2061
                                           CHOOSER_IMAGE_COLUMN, image,
 
2062
                                           CHOOSER_NAME_COLUMN, name,
 
2063
                                           CHOOSER_COMMENT_COLUMN, comment,
 
2064
                                           CHOOSER_PRIORITY_COLUMN, priority,
 
2065
                                           CHOOSER_ITEM_IS_IN_USE_COLUMN, in_use,
 
2066
                                           CHOOSER_ITEM_IS_SEPARATED_COLUMN, keep_separate,
 
2067
                                           CHOOSER_ITEM_IS_VISIBLE_COLUMN, is_visible,
 
2068
                                           CHOOSER_ID_COLUMN, id,
 
2069
                                           -1);
 
2070
 
 
2071
        move_cursor_to_top (widget);
 
2072
        update_chooser_visibility (widget);
 
2073
}
 
2074
 
 
2075
void
 
2076
gdm_chooser_widget_remove_item (GdmChooserWidget *widget,
 
2077
                                const char       *id)
 
2078
{
 
2079
        GtkTreeModel *model;
 
2080
        GtkTreeIter   iter;
 
2081
        GdkPixbuf    *image;
 
2082
        gboolean      is_separate;
 
2083
        gboolean      is_in_use;
 
2084
 
 
2085
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
2086
 
 
2087
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
2088
 
 
2089
        if (!find_item (widget, id, &iter)) {
 
2090
                g_critical ("Tried to remove non-existing item from chooser");
 
2091
                return;
 
2092
        }
 
2093
 
 
2094
        is_separate = FALSE;
 
2095
        gtk_tree_model_get (model, &iter,
 
2096
                            CHOOSER_IMAGE_COLUMN, &image,
 
2097
                            CHOOSER_ITEM_IS_IN_USE_COLUMN, &is_in_use,
 
2098
                            CHOOSER_ITEM_IS_SEPARATED_COLUMN, &is_separate,
 
2099
                            -1);
 
2100
 
 
2101
        if (image != NULL) {
 
2102
                widget->priv->number_of_rows_with_images--;
 
2103
                g_object_unref (image);
 
2104
        }
 
2105
 
 
2106
        if (is_in_use) {
 
2107
                widget->priv->number_of_rows_with_status--;
 
2108
                queue_column_visibility_update (widget);
 
2109
        }
 
2110
 
 
2111
        if (is_separate) {
 
2112
                widget->priv->number_of_separated_rows--;
 
2113
        } else {
 
2114
                widget->priv->number_of_normal_rows--;
 
2115
        }
 
2116
        update_separator_visibility (widget);
 
2117
 
 
2118
        gtk_list_store_remove (widget->priv->list_store, &iter);
 
2119
 
 
2120
        move_cursor_to_top (widget);
 
2121
        update_chooser_visibility (widget);
 
2122
}
 
2123
 
 
2124
gboolean
 
2125
gdm_chooser_widget_lookup_item (GdmChooserWidget *widget,
 
2126
                                const char       *id,
 
2127
                                GdkPixbuf       **image,
 
2128
                                char            **name,
 
2129
                                char            **comment,
 
2130
                                gulong           *priority,
 
2131
                                gboolean         *is_in_use,
 
2132
                                gboolean         *is_separate)
 
2133
{
 
2134
        GtkTreeIter   iter;
 
2135
        char         *active_item_id;
 
2136
 
 
2137
        g_return_val_if_fail (GDM_IS_CHOOSER_WIDGET (widget), FALSE);
 
2138
        g_return_val_if_fail (id != NULL, FALSE);
 
2139
 
 
2140
        active_item_id = get_active_item_id (widget, &iter);
 
2141
 
 
2142
        if (active_item_id == NULL || strcmp (active_item_id, id) != 0) {
 
2143
                g_free (active_item_id);
 
2144
 
 
2145
                if (!find_item (widget, id, &iter)) {
 
2146
                        return FALSE;
 
2147
                }
 
2148
        }
 
2149
        g_free (active_item_id);
 
2150
 
 
2151
        if (image != NULL) {
 
2152
                gtk_tree_model_get (GTK_TREE_MODEL (widget->priv->list_store), &iter,
 
2153
                                    CHOOSER_IMAGE_COLUMN, image, -1);
 
2154
        }
 
2155
 
 
2156
        if (name != NULL) {
 
2157
                gtk_tree_model_get (GTK_TREE_MODEL (widget->priv->list_store), &iter,
 
2158
                                    CHOOSER_NAME_COLUMN, name, -1);
 
2159
        }
 
2160
 
 
2161
        if (priority != NULL) {
 
2162
                gtk_tree_model_get (GTK_TREE_MODEL (widget->priv->list_store), &iter,
 
2163
                                    CHOOSER_PRIORITY_COLUMN, priority, -1);
 
2164
        }
 
2165
 
 
2166
        if (is_in_use != NULL) {
 
2167
                gtk_tree_model_get (GTK_TREE_MODEL (widget->priv->list_store), &iter,
 
2168
                                    CHOOSER_ITEM_IS_IN_USE_COLUMN, is_in_use, -1);
 
2169
        }
 
2170
 
 
2171
        if (is_separate != NULL) {
 
2172
                gtk_tree_model_get (GTK_TREE_MODEL (widget->priv->list_store), &iter,
 
2173
                                    CHOOSER_ITEM_IS_SEPARATED_COLUMN, is_separate, -1);
 
2174
        }
 
2175
 
 
2176
        return TRUE;
 
2177
}
 
2178
 
 
2179
void
 
2180
gdm_chooser_widget_set_item_in_use (GdmChooserWidget *widget,
 
2181
                                    const char       *id,
 
2182
                                    gboolean          is_in_use)
 
2183
{
 
2184
        GtkTreeIter   iter;
 
2185
        gboolean      was_in_use;
 
2186
 
 
2187
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
2188
 
 
2189
        if (!find_item (widget, id, &iter)) {
 
2190
                return;
 
2191
        }
 
2192
 
 
2193
        gtk_tree_model_get (GTK_TREE_MODEL (widget->priv->list_store), &iter,
 
2194
                            CHOOSER_ITEM_IS_IN_USE_COLUMN, &was_in_use,
 
2195
                            -1);
 
2196
 
 
2197
        if (was_in_use != is_in_use) {
 
2198
 
 
2199
                if (is_in_use) {
 
2200
                        widget->priv->number_of_rows_with_status++;
 
2201
                } else {
 
2202
                        widget->priv->number_of_rows_with_status--;
 
2203
                }
 
2204
                queue_column_visibility_update (widget);
 
2205
 
 
2206
                gtk_list_store_set (widget->priv->list_store,
 
2207
                                    &iter,
 
2208
                                    CHOOSER_ITEM_IS_IN_USE_COLUMN, is_in_use,
 
2209
                                    -1);
 
2210
 
 
2211
        }
 
2212
}
 
2213
 
 
2214
void
 
2215
gdm_chooser_widget_set_item_priority (GdmChooserWidget *widget,
 
2216
                                      const char       *id,
 
2217
                                      gulong            priority)
 
2218
{
 
2219
        GtkTreeIter   iter;
 
2220
        gulong        was_priority;
 
2221
 
 
2222
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
2223
 
 
2224
        if (!find_item (widget, id, &iter)) {
 
2225
                return;
 
2226
        }
 
2227
 
 
2228
        gtk_tree_model_get (GTK_TREE_MODEL (widget->priv->list_store), &iter,
 
2229
                            CHOOSER_PRIORITY_COLUMN, &was_priority,
 
2230
                            -1);
 
2231
 
 
2232
        if (was_priority != priority) {
 
2233
 
 
2234
                gtk_list_store_set (widget->priv->list_store,
 
2235
                                    &iter,
 
2236
                                    CHOOSER_PRIORITY_COLUMN, priority,
 
2237
                                    -1);
 
2238
                gtk_tree_model_filter_refilter (widget->priv->model_filter);
 
2239
        }
 
2240
}
 
2241
 
 
2242
static double
 
2243
get_current_time (void)
 
2244
{
 
2245
  const double microseconds_per_second = 1000000.0;
 
2246
  double       timestamp;
 
2247
  GTimeVal     now;
 
2248
 
 
2249
  g_get_current_time (&now);
 
2250
 
 
2251
  timestamp = ((microseconds_per_second * now.tv_sec) + now.tv_usec) /
 
2252
               microseconds_per_second;
 
2253
 
 
2254
  return timestamp;
 
2255
}
 
2256
 
 
2257
static gboolean
 
2258
on_timer_timeout (GdmChooserWidget *widget)
 
2259
{
 
2260
        GHashTableIter  iter;
 
2261
        GSList         *list;
 
2262
        GSList         *tmp;
 
2263
        gpointer        key;
 
2264
        gpointer        value;
 
2265
        double          now;
 
2266
 
 
2267
        list = NULL;
 
2268
        g_hash_table_iter_init (&iter, widget->priv->rows_with_timers);
 
2269
        while (g_hash_table_iter_next (&iter, &key, &value)) {
 
2270
                list = g_slist_prepend (list, value);
 
2271
        }
 
2272
 
 
2273
        now = get_current_time ();
 
2274
        for (tmp = list; tmp != NULL; tmp = tmp->next) {
 
2275
                GtkTreeRowReference *row;
 
2276
 
 
2277
                row = (GtkTreeRowReference *) tmp->data;
 
2278
 
 
2279
                update_timer_from_time (widget, row, now);
 
2280
        }
 
2281
        g_slist_free (list);
 
2282
 
 
2283
        return TRUE;
 
2284
}
 
2285
 
 
2286
static void
 
2287
start_timer (GdmChooserWidget    *widget,
 
2288
             GtkTreeRowReference *row,
 
2289
             double               duration)
 
2290
{
 
2291
        GtkTreeModel *model;
 
2292
        GtkTreePath  *path;
 
2293
        GtkTreeIter   iter;
 
2294
 
 
2295
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
2296
 
 
2297
        path = gtk_tree_row_reference_get_path (row);
 
2298
        gtk_tree_model_get_iter (model, &iter, path);
 
2299
        gtk_tree_path_free (path);
 
2300
 
 
2301
        gtk_list_store_set (widget->priv->list_store, &iter,
 
2302
                            CHOOSER_TIMER_START_TIME_COLUMN,
 
2303
                            get_current_time (), -1);
 
2304
        gtk_list_store_set (widget->priv->list_store, &iter,
 
2305
                            CHOOSER_TIMER_DURATION_COLUMN,
 
2306
                            duration, -1);
 
2307
        gtk_list_store_set (widget->priv->list_store, &iter,
 
2308
                            CHOOSER_TIMER_VALUE_COLUMN, 0.0, -1);
 
2309
 
 
2310
        widget->priv->number_of_active_timers++;
 
2311
        if (widget->priv->timer_animation_timeout_id == 0) {
 
2312
                g_assert (g_hash_table_size (widget->priv->rows_with_timers) == 1);
 
2313
 
 
2314
                widget->priv->timer_animation_timeout_id =
 
2315
                    g_timeout_add (1000 / 20,
 
2316
                                   (GSourceFunc) on_timer_timeout,
 
2317
                                   widget);
 
2318
 
 
2319
        }
 
2320
}
 
2321
 
 
2322
static void
 
2323
stop_timer (GdmChooserWidget    *widget,
 
2324
            GtkTreeRowReference *row)
 
2325
{
 
2326
        GtkTreeModel *model;
 
2327
        GtkTreePath  *path;
 
2328
        GtkTreeIter   iter;
 
2329
 
 
2330
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
2331
 
 
2332
        path = gtk_tree_row_reference_get_path (row);
 
2333
        gtk_tree_model_get_iter (model, &iter, path);
 
2334
        gtk_tree_path_free (path);
 
2335
 
 
2336
        gtk_list_store_set (widget->priv->list_store, &iter,
 
2337
                            CHOOSER_TIMER_START_TIME_COLUMN,
 
2338
                            0.0, -1);
 
2339
        gtk_list_store_set (widget->priv->list_store, &iter,
 
2340
                            CHOOSER_TIMER_DURATION_COLUMN,
 
2341
                            0.0, -1);
 
2342
        gtk_list_store_set (widget->priv->list_store, &iter,
 
2343
                            CHOOSER_TIMER_VALUE_COLUMN, 0.0, -1);
 
2344
 
 
2345
        widget->priv->number_of_active_timers--;
 
2346
        if (widget->priv->number_of_active_timers == 0) {
 
2347
                g_source_remove (widget->priv->timer_animation_timeout_id);
 
2348
                widget->priv->timer_animation_timeout_id = 0;
 
2349
        }
 
2350
}
 
2351
 
 
2352
static void
 
2353
update_timer_from_time (GdmChooserWidget    *widget,
 
2354
                        GtkTreeRowReference *row,
 
2355
                        double               now)
 
2356
{
 
2357
        GtkTreeModel *model;
 
2358
        GtkTreePath  *path;
 
2359
        GtkTreeIter   iter;
 
2360
        double        start_time;
 
2361
        double        duration;
 
2362
        double        elapsed_ratio;
 
2363
 
 
2364
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
2365
 
 
2366
        path = gtk_tree_row_reference_get_path (row);
 
2367
        gtk_tree_model_get_iter (model, &iter, path);
 
2368
        gtk_tree_path_free (path);
 
2369
 
 
2370
        gtk_tree_model_get (model, &iter,
 
2371
                            CHOOSER_TIMER_START_TIME_COLUMN, &start_time,
 
2372
                            CHOOSER_TIMER_DURATION_COLUMN, &duration, -1);
 
2373
 
 
2374
        if (duration > G_MINDOUBLE) {
 
2375
                elapsed_ratio = (now - start_time) / duration;
 
2376
        } else {
 
2377
                elapsed_ratio = 0.0;
 
2378
        }
 
2379
 
 
2380
        gtk_list_store_set (widget->priv->list_store, &iter,
 
2381
                            CHOOSER_TIMER_VALUE_COLUMN,
 
2382
                            elapsed_ratio, -1);
 
2383
 
 
2384
        if (elapsed_ratio > .999) {
 
2385
                char *id;
 
2386
 
 
2387
                stop_timer (widget, row);
 
2388
 
 
2389
                gtk_tree_model_get (model, &iter,
 
2390
                                    CHOOSER_ID_COLUMN, &id, -1);
 
2391
                g_hash_table_remove (widget->priv->rows_with_timers,
 
2392
                                     id);
 
2393
                g_free (id);
 
2394
 
 
2395
                widget->priv->number_of_rows_with_status--;
 
2396
                queue_column_visibility_update (widget);
 
2397
        }
 
2398
}
 
2399
 
 
2400
void
 
2401
gdm_chooser_widget_set_item_timer (GdmChooserWidget *widget,
 
2402
                                   const char       *id,
 
2403
                                   gulong            timeout)
 
2404
{
 
2405
        GtkTreeModel        *model;
 
2406
        GtkTreeRowReference *row;
 
2407
 
 
2408
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
2409
 
 
2410
        model = GTK_TREE_MODEL (widget->priv->list_store);
 
2411
 
 
2412
        row = g_hash_table_lookup (widget->priv->rows_with_timers,
 
2413
                                   id);
 
2414
 
 
2415
        g_assert (row == NULL || gtk_tree_row_reference_valid (row));
 
2416
 
 
2417
        if (row != NULL) {
 
2418
                stop_timer (widget, row);
 
2419
        }
 
2420
 
 
2421
        if (timeout == 0) {
 
2422
                if (row == NULL) {
 
2423
                        g_warning ("could not find item with ID '%s' to "
 
2424
                                   "remove timer", id);
 
2425
                        return;
 
2426
                }
 
2427
 
 
2428
                g_hash_table_remove (widget->priv->rows_with_timers,
 
2429
                                     id);
 
2430
                gtk_tree_model_filter_refilter (widget->priv->model_filter);
 
2431
                return;
 
2432
        }
 
2433
 
 
2434
        if (row == NULL) {
 
2435
                GtkTreeIter  iter;
 
2436
                GtkTreePath *path;
 
2437
 
 
2438
                if (!find_item (widget, id, &iter)) {
 
2439
                        g_warning ("could not find item with ID '%s' to "
 
2440
                                   "add timer", id);
 
2441
                        return;
 
2442
                }
 
2443
 
 
2444
                path = gtk_tree_model_get_path (model, &iter);
 
2445
                row = gtk_tree_row_reference_new (model, path);
 
2446
 
 
2447
                g_hash_table_insert (widget->priv->rows_with_timers,
 
2448
                                     g_strdup (id), row);
 
2449
 
 
2450
                widget->priv->number_of_rows_with_status++;
 
2451
                queue_column_visibility_update (widget);
 
2452
        }
 
2453
 
 
2454
        start_timer (widget, row, timeout / 1000.0);
 
2455
}
 
2456
 
 
2457
void
 
2458
gdm_chooser_widget_set_in_use_message (GdmChooserWidget *widget,
 
2459
                                       const char       *message)
 
2460
{
 
2461
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
2462
 
 
2463
        g_free (widget->priv->in_use_message);
 
2464
        widget->priv->in_use_message = g_strdup (message);
 
2465
}
 
2466
 
 
2467
void
 
2468
gdm_chooser_widget_set_separator_position (GdmChooserWidget         *widget,
 
2469
                                           GdmChooserWidgetPosition  position)
 
2470
{
 
2471
        g_return_if_fail (GDM_IS_CHOOSER_WIDGET (widget));
 
2472
 
 
2473
        if (widget->priv->separator_position != position) {
 
2474
                widget->priv->separator_position = position;
 
2475
        }
 
2476
 
 
2477
        gtk_tree_model_filter_refilter (widget->priv->model_filter);
 
2478
}
 
2479
 
 
2480
void
 
2481
gdm_chooser_widget_set_hide_inactive_items (GdmChooserWidget  *widget,
 
2482
                                            gboolean           should_hide)
 
2483
{
 
2484
        widget->priv->should_hide_inactive_items = should_hide;
 
2485
 
 
2486
        if (should_hide &&
 
2487
            (widget->priv->state != GDM_CHOOSER_WIDGET_STATE_SHRUNK
 
2488
             || widget->priv->state != GDM_CHOOSER_WIDGET_STATE_SHRINKING) &&
 
2489
            widget->priv->active_row != NULL) {
 
2490
                gdm_chooser_widget_shrink (widget);
 
2491
        } else if (!should_hide &&
 
2492
              (widget->priv->state != GDM_CHOOSER_WIDGET_STATE_GROWN
 
2493
               || widget->priv->state != GDM_CHOOSER_WIDGET_STATE_GROWING)) {
 
2494
                gdm_chooser_widget_grow (widget);
 
2495
        }
 
2496
}
 
2497
 
 
2498
int
 
2499
gdm_chooser_widget_get_number_of_items (GdmChooserWidget *widget)
 
2500
{
 
2501
        return widget->priv->number_of_normal_rows +
 
2502
               widget->priv->number_of_separated_rows;
 
2503
}
 
2504
 
 
2505
void
 
2506
gdm_chooser_widget_activate_if_one_item (GdmChooserWidget *widget)
 
2507
{
 
2508
        activate_if_one_item (widget);
 
2509
}
 
2510
 
 
2511
void
 
2512
gdm_chooser_widget_propagate_pending_key_events (GdmChooserWidget *widget)
 
2513
{
 
2514
        if (!gdm_scrollable_widget_has_queued_key_events (GDM_SCROLLABLE_WIDGET (widget->priv->scrollable_widget))) {
 
2515
                return;
 
2516
        }
 
2517
 
 
2518
        gdm_scrollable_widget_replay_queued_key_events (GDM_SCROLLABLE_WIDGET (widget->priv->scrollable_widget));
 
2519
}
 
2520
 
 
2521
void
 
2522
gdm_chooser_widget_loaded (GdmChooserWidget *widget)
 
2523
{
 
2524
        gdm_chooser_widget_grow (widget);
 
2525
        g_signal_emit (widget, signals[LOADED], 0);
 
2526
}