~ubuntu-branches/ubuntu/dapper/gnome-screensaver/dapper

« back to all changes in this revision

Viewing changes to src/cut-n-paste/fusa-user-menu-item.c

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Grawert
  • Date: 2005-10-10 00:18:18 UTC
  • Revision ID: james.westby@ubuntu.com-20051010001818-3mujs05r8rht7xi1
Tags: upstream-0.0.15
ImportĀ upstreamĀ versionĀ 0.0.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Fast User Switch Applet: fusa-user-menu-item.c
 
3
 * 
 
4
 * Copyright (C) 2005 James M. Cape <jcape@ignore-your.tv>.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include <glib/gi18n.h>
 
24
 
 
25
#include <unistd.h>
 
26
#include <sys/types.h>
 
27
 
 
28
#include <string.h>
 
29
 
 
30
#include <gtk/gtkbutton.h>
 
31
#include <gtk/gtkdialog.h>
 
32
#include <gtk/gtkhbox.h>
 
33
#include <gtk/gtkiconfactory.h>
 
34
#include <gtk/gtkicontheme.h>
 
35
#include <gtk/gtkimage.h>
 
36
#include <gtk/gtklabel.h>
 
37
#include <gtk/gtkradiobutton.h>
 
38
#include <gtk/gtkstock.h>
 
39
#include <gtk/gtkvbox.h>
 
40
 
 
41
#include "fusa-user-menu-item.h"
 
42
 
 
43
 
 
44
/* **************** *
 
45
 *  Private Macros  *
 
46
 * **************** */
 
47
 
 
48
#define DEFAULT_ICON_SIZE       24
 
49
#define CLOSE_ENOUGH_SIZE       2
 
50
 
 
51
/* ********************** *
 
52
 *  Private Enumerations  *
 
53
 * ********************** */
 
54
 
 
55
enum
 
56
{
 
57
  PROP_0,
 
58
  PROP_USER,
 
59
  PROP_ICON_SIZE
 
60
};
 
61
 
 
62
 
 
63
/* ******************** *
 
64
 *  Private Structures  *
 
65
 * ******************** */
 
66
  
 
67
struct _FusaUserMenuItem
 
68
{
 
69
  GtkImageMenuItem parent;
 
70
 
 
71
  FusaUser *user;
 
72
 
 
73
  GtkWidget *image;
 
74
  GtkWidget *label;
 
75
 
 
76
  gulong user_notify_id;
 
77
  gulong user_icon_changed_id;
 
78
  gulong user_displays_changed_id;
 
79
  gint icon_size;
 
80
};
 
81
 
 
82
struct _FusaUserMenuItemClass
 
83
{
 
84
  GtkImageMenuItemClass parent_class;
 
85
};
 
86
 
 
87
 
 
88
/* ********************* *
 
89
 *  Function Prototypes  *
 
90
 * ********************* */
 
91
 
 
92
/* GObject Functions */
 
93
static void fusa_user_menu_item_set_property (GObject      *object,
 
94
                                              guint         param_id,
 
95
                                              const GValue *value,
 
96
                                              GParamSpec   *pspec);
 
97
static void fusa_user_menu_item_get_property (GObject      *object,
 
98
                                              guint         param_id,
 
99
                                              GValue       *value,
 
100
                                              GParamSpec   *pspec);
 
101
static void fusa_user_menu_item_finalize     (GObject      *object);
 
102
 
 
103
/* FusaUser Callbacks */
 
104
static void user_notify_cb           (GObject    *object,
 
105
                                      GParamSpec *pspec,
 
106
                                      gpointer    data);
 
107
static void user_icon_changed_cb     (FusaUser   *user,
 
108
                                      gpointer    data);
 
109
static void user_displays_changed_cb (FusaUser   *user,
 
110
                                      gpointer    data);
 
111
static void user_weak_notify         (gpointer    data,
 
112
                                      GObject    *user_ptr);
 
113
 
 
114
/* Widget Callbacks */
 
115
static void image_style_set_cb (GtkWidget *widget,
 
116
                                GtkStyle  *old_style,
 
117
                                gpointer   data);
 
118
static void label_style_set_cb (GtkWidget *widget,
 
119
                                GtkStyle  *old_style,
 
120
                                gpointer   data);
 
121
 
 
122
/* Utility Functions */
 
123
static void reset_label (FusaUserMenuItem *item);
 
124
static void reset_icon  (FusaUserMenuItem *item);
 
125
 
 
126
 
 
127
/* ******************* *
 
128
 *  GType Declaration  *
 
129
 * ******************* */
 
130
 
 
131
G_DEFINE_TYPE (FusaUserMenuItem, fusa_user_menu_item, GTK_TYPE_IMAGE_MENU_ITEM);
 
132
 
 
133
 
 
134
/* ***************** *
 
135
 *  GType Functions  *
 
136
 * ***************** */
 
137
 
 
138
static void
 
139
fusa_user_menu_item_class_init (FusaUserMenuItemClass *class)
 
140
{
 
141
  GObjectClass *gobject_class;
 
142
 
 
143
  gobject_class = G_OBJECT_CLASS (class);
 
144
 
 
145
  gobject_class->set_property = fusa_user_menu_item_set_property;
 
146
  gobject_class->get_property = fusa_user_menu_item_get_property;
 
147
  gobject_class->finalize = fusa_user_menu_item_finalize;
 
148
 
 
149
  g_object_class_install_property (gobject_class,
 
150
                                   PROP_USER,
 
151
                                   g_param_spec_object ("user",
 
152
                                                        _("User"),
 
153
                                                        _("The user this menu item represents."),
 
154
                                                        FUSA_TYPE_USER,
 
155
                                                        (G_PARAM_READWRITE |
 
156
                                                         G_PARAM_CONSTRUCT_ONLY)));
 
157
  g_object_class_install_property (gobject_class,
 
158
                                   PROP_ICON_SIZE,
 
159
                                   g_param_spec_int ("icon-size",
 
160
                                                     _("Icon Size"),
 
161
                                                     _("The size of the icon to use."),
 
162
                                                     12, G_MAXINT, DEFAULT_ICON_SIZE,
 
163
                                                     G_PARAM_READWRITE));
 
164
}
 
165
 
 
166
static void
 
167
fusa_user_menu_item_init (FusaUserMenuItem *item)
 
168
{
 
169
  item->icon_size = DEFAULT_ICON_SIZE;
 
170
 
 
171
  item->image = gtk_image_new ();
 
172
  g_signal_connect (item->image, "style-set",
 
173
                    G_CALLBACK (image_style_set_cb), item);
 
174
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), item->image);
 
175
  gtk_widget_show (item->image);
 
176
 
 
177
  item->label = gtk_label_new (NULL);
 
178
  gtk_label_set_use_markup (GTK_LABEL (item->label), TRUE);
 
179
  gtk_misc_set_alignment (GTK_MISC (item->label), 0.0, 0.5);
 
180
  g_signal_connect (item->label, "style-set",
 
181
                    G_CALLBACK (label_style_set_cb), item);
 
182
  gtk_container_add (GTK_CONTAINER (item), item->label);
 
183
  gtk_widget_show (item->label);
 
184
}
 
185
 
 
186
 
 
187
/* ******************* *
 
188
 *  GObject Functions  *
 
189
 * ******************* */
 
190
 
 
191
static void
 
192
fusa_user_menu_item_set_property (GObject      *object,
 
193
                                  guint         param_id,
 
194
                                  const GValue *value,
 
195
                                  GParamSpec   *pspec)
 
196
{
 
197
  FusaUserMenuItem *item;
 
198
 
 
199
  item = FUSA_USER_MENU_ITEM (object);
 
200
 
 
201
  switch (param_id)
 
202
    {
 
203
    case PROP_USER:
 
204
      item->user = g_value_get_object (value);
 
205
      g_object_weak_ref (G_OBJECT (item->user), user_weak_notify, item);
 
206
      item->user_notify_id =
 
207
        g_signal_connect (item->user, "notify",
 
208
                          G_CALLBACK (user_notify_cb), item);
 
209
      item->user_icon_changed_id =
 
210
        g_signal_connect (item->user, "icon-changed",
 
211
                          G_CALLBACK (user_icon_changed_cb), item);
 
212
      item->user_displays_changed_id =
 
213
        g_signal_connect (item->user, "displays-changed",
 
214
                          G_CALLBACK (user_displays_changed_cb), item);
 
215
 
 
216
      if (gtk_widget_get_style (GTK_WIDGET (object)))
 
217
        {
 
218
          reset_icon (item);
 
219
          reset_label (item);
 
220
        }
 
221
      break;
 
222
    case PROP_ICON_SIZE:
 
223
      fusa_user_menu_item_set_icon_size (item, g_value_get_int (value));
 
224
      break;
 
225
    default:
 
226
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
227
      break;
 
228
    }
 
229
}
 
230
 
 
231
static void
 
232
fusa_user_menu_item_get_property (GObject    *object,
 
233
                                  guint       param_id,
 
234
                                  GValue     *value,
 
235
                                  GParamSpec *pspec)
 
236
{
 
237
  FusaUserMenuItem *item;
 
238
 
 
239
  item = FUSA_USER_MENU_ITEM (object);
 
240
 
 
241
  switch (param_id)
 
242
    {
 
243
    case PROP_USER:
 
244
      g_value_set_object (value, item->user);
 
245
      break;
 
246
    case PROP_ICON_SIZE:
 
247
      g_value_set_int (value, item->icon_size);
 
248
      break;
 
249
    default:
 
250
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
251
      break;
 
252
    }
 
253
}
 
254
 
 
255
static void
 
256
fusa_user_menu_item_finalize (GObject *object)
 
257
{
 
258
  FusaUserMenuItem *item;
 
259
 
 
260
  item = FUSA_USER_MENU_ITEM (object);
 
261
 
 
262
  if (item->user)
 
263
    {
 
264
      g_signal_handler_disconnect (item->user, item->user_notify_id);
 
265
      g_signal_handler_disconnect (item->user, item->user_icon_changed_id);
 
266
      g_signal_handler_disconnect (item->user, item->user_displays_changed_id);
 
267
      g_object_weak_unref (G_OBJECT (item->user), user_weak_notify, object);
 
268
    }
 
269
 
 
270
  if (G_OBJECT_CLASS (fusa_user_menu_item_parent_class)->finalize)
 
271
    (*G_OBJECT_CLASS (fusa_user_menu_item_parent_class)->finalize) (object);
 
272
}
 
273
 
 
274
 
 
275
/* ******************** *
 
276
 *  FusaUser Callbacks  *
 
277
 * ******************** */
 
278
 
 
279
static void
 
280
user_notify_cb (GObject    *object,
 
281
                GParamSpec *pspec,
 
282
                gpointer    data)
 
283
{
 
284
  if (!pspec || !pspec->name)
 
285
    return;
 
286
 
 
287
  if (strcmp (pspec->name, "user-name") == 0 ||
 
288
      strcmp (pspec->name, "display-name") == 0)
 
289
    reset_label (data);
 
290
}
 
291
 
 
292
static void
 
293
user_icon_changed_cb (FusaUser *user,
 
294
                      gpointer  data)
 
295
{
 
296
  if (gtk_widget_has_screen (data))
 
297
    reset_icon (data);
 
298
}
 
299
 
 
300
static void
 
301
user_displays_changed_cb (FusaUser *user,
 
302
                          gpointer  data)
 
303
{
 
304
  if (gtk_widget_has_screen (data))
 
305
    reset_icon (data);
 
306
 
 
307
  if (fusa_user_get_uid (user) == getuid ())
 
308
    gtk_widget_set_sensitive (data, (fusa_user_get_n_displays (user) > 1));
 
309
  else
 
310
    gtk_widget_set_sensitive (data, TRUE);
 
311
}
 
312
 
 
313
static void
 
314
user_weak_notify (gpointer  data,
 
315
                  GObject  *user_ptr)
 
316
{
 
317
  FUSA_USER_MENU_ITEM (data)->user = NULL;
 
318
 
 
319
  gtk_widget_destroy (data);
 
320
}
 
321
 
 
322
 
 
323
/* ****************** *
 
324
 *  Widget Callbacks  *
 
325
 * ****************** */
 
326
 
 
327
static void
 
328
image_style_set_cb (GtkWidget *widget,
 
329
                    GtkStyle  *old_style,
 
330
                    gpointer   data)
 
331
{
 
332
  reset_icon (data);
 
333
}
 
334
 
 
335
static void
 
336
label_style_set_cb (GtkWidget *widget,
 
337
                    GtkStyle  *old_style,
 
338
                    gpointer   data)
 
339
{
 
340
  reset_label (data);
 
341
}
 
342
 
 
343
 
 
344
/* ******************* *
 
345
 *  Utility Functions  *
 
346
 * ******************* */
 
347
 
 
348
static void
 
349
reset_label (FusaUserMenuItem *item)
 
350
{
 
351
  gchar *text;
 
352
  PangoLayout *layout;
 
353
  gint height;
 
354
 
 
355
  if (!item->user)
 
356
    return;
 
357
 
 
358
  text = g_strconcat ("<b>",
 
359
                      fusa_user_get_display_name (item->user),
 
360
                      "</b>\n",
 
361
                      "<small>",
 
362
                      fusa_user_get_user_name (item->user),
 
363
                      "</small>",
 
364
                      NULL);
 
365
 
 
366
  gtk_label_set_markup (GTK_LABEL (item->label), text);
 
367
  g_free (text);
 
368
 
 
369
  layout = gtk_label_get_layout (GTK_LABEL (item->label));
 
370
  pango_layout_get_pixel_size (layout, NULL, &height);
 
371
 
 
372
  if (height > (item->icon_size + CLOSE_ENOUGH_SIZE))
 
373
    gtk_label_set_markup (GTK_LABEL (item->label),
 
374
                          fusa_user_get_display_name (item->user));
 
375
}
 
376
 
 
377
static void
 
378
reset_icon (FusaUserMenuItem *item)
 
379
{
 
380
  GdkPixbuf *pixbuf;
 
381
 
 
382
  if (!item->user || !gtk_widget_has_screen (GTK_WIDGET (item)))
 
383
    return;
 
384
 
 
385
  g_assert (item->icon_size != 0);
 
386
 
 
387
  pixbuf = fusa_user_render_icon (item->user, GTK_WIDGET (item),
 
388
                                  item->icon_size,
 
389
                                  fusa_user_get_n_displays (item->user));
 
390
  gtk_image_set_from_pixbuf (GTK_IMAGE (item->image), pixbuf);
 
391
  g_object_unref (pixbuf);
 
392
}
 
393
 
 
394
 
 
395
/* ************************************************************************** *
 
396
 *  Public API                                                                *
 
397
 * ************************************************************************** */
 
398
 
 
399
GtkWidget *
 
400
fusa_user_menu_item_new (FusaUser *user)
 
401
{
 
402
  g_return_val_if_fail (FUSA_IS_USER (user), NULL);
 
403
 
 
404
  return g_object_new (FUSA_TYPE_USER_MENU_ITEM, "user", user, NULL);
 
405
}
 
406
 
 
407
FusaUser *
 
408
fusa_user_menu_item_get_user (FusaUserMenuItem *item)
 
409
{
 
410
  g_return_val_if_fail (FUSA_IS_USER_MENU_ITEM (item), NULL);
 
411
 
 
412
  return item->user;
 
413
}
 
414
 
 
415
gint
 
416
fusa_user_menu_item_get_icon_size (FusaUserMenuItem *item)
 
417
{
 
418
  g_return_val_if_fail (FUSA_IS_USER_MENU_ITEM (item), -1);
 
419
 
 
420
  return item->icon_size;
 
421
}
 
422
 
 
423
void
 
424
fusa_user_menu_item_set_icon_size (FusaUserMenuItem *item,
 
425
                                   gint              pixel_size)
 
426
{
 
427
  g_return_if_fail (FUSA_IS_USER_MENU_ITEM (item));
 
428
  g_return_if_fail (pixel_size != 0);
 
429
 
 
430
  if (pixel_size < 0)
 
431
    item->icon_size = DEFAULT_ICON_SIZE;
 
432
  else
 
433
    item->icon_size = pixel_size;
 
434
 
 
435
  if (gtk_widget_get_style (GTK_WIDGET (item)))
 
436
    {
 
437
      reset_icon (item);
 
438
      reset_label (item);
 
439
    }
 
440
  g_object_notify (G_OBJECT (item), "icon-size");
 
441
}