source: proiecte/PPPP/gdm/gui/simple-greeter/libnotificationarea/na-tray.c @ 134

Last change on this file since 134 was 134, checked in by (none), 14 years ago

gdm sources with the modifications for webcam

File size: 17.2 KB
Line 
1/*
2 * Copyright (C) 2002 Red Hat, Inc.
3 * Copyright (C) 2003-2006 Vincent Untz
4 * Copyright (C) 2007 Christian Persch
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * 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
19 * 02111-1307, USA.
20 */
21
22#include <config.h>
23#include <string.h>
24
25#include <gtk/gtk.h>
26
27#include "na-tray-manager.h"
28#include "fixedtip.h"
29#include "obox.h"
30
31#include "na-tray.h"
32
33#define ICON_SPACING 1
34#define MIN_BOX_SIZE 3
35
36typedef struct
37{
38  NaTrayManager *tray_manager;
39  GSList        *all_trays;
40  GHashTable    *icon_table;
41  GHashTable    *tip_table;
42} TraysScreen;
43
44struct _NaTrayPrivate
45{
46  GdkScreen   *screen;
47  TraysScreen *trays_screen;
48
49  GtkWidget *box;
50  GtkWidget *frame;
51
52  guint idle_redraw_id;
53
54  GtkOrientation orientation;
55};
56
57typedef struct
58{
59  char  *text;
60  glong  id;
61  glong  timeout;
62} IconTipBuffer;
63
64typedef struct
65{
66  NaTray *tray;      /* tray containing the tray icon */
67  GtkWidget  *icon;      /* tray icon sending the message */
68  GtkWidget  *fixedtip;
69  guint       source_id;
70  glong       id;        /* id of the current message */
71  GSList     *buffer;    /* buffered messages */
72} IconTip;
73
74enum
75{
76  PROP_0,
77  PROP_ORIENTATION,
78  PROP_SCREEN
79};
80
81static gboolean     initialized   = FALSE;
82static TraysScreen *trays_screens = NULL;
83
84static void icon_tip_show_next (IconTip *icontip);
85
86G_DEFINE_TYPE (NaTray, na_tray, GTK_TYPE_BIN)
87
88static NaTray *
89get_tray (TraysScreen *trays_screen)
90{
91  if (trays_screen->all_trays == NULL)
92    return NULL;
93 
94  return trays_screen->all_trays->data;
95}
96
97static void
98tray_added (NaTrayManager *manager,
99            GtkWidget     *icon,
100            TraysScreen   *trays_screen)
101{
102  NaTray *tray;
103  NaTrayPrivate *priv;
104
105  tray = get_tray (trays_screen);
106  if (tray == NULL)
107    return;
108
109  priv = tray->priv;
110
111  g_assert (priv->trays_screen == trays_screen);
112 
113  g_hash_table_insert (trays_screen->icon_table, icon, tray);
114
115  gtk_box_pack_end (GTK_BOX (priv->box), icon, FALSE, FALSE, 0);
116
117  gtk_widget_show (icon);
118  na_tray_force_redraw (tray);
119}
120
121static void
122tray_removed (NaTrayManager *manager,
123              GtkWidget     *icon,
124              TraysScreen   *trays_screen)
125{
126  NaTray *tray;
127
128  tray = g_hash_table_lookup (trays_screen->icon_table, icon);
129  if (tray == NULL)
130    return;
131
132  g_assert (tray->priv->trays_screen == trays_screen);
133
134  na_tray_force_redraw (tray);
135
136  g_hash_table_remove (trays_screen->icon_table, icon);
137  /* this will also destroy the tip associated to this icon */
138  g_hash_table_remove (trays_screen->tip_table, icon);
139}
140
141static void
142icon_tip_buffer_free (gpointer data,
143                      gpointer userdata)
144{
145  IconTipBuffer *buffer;
146
147  buffer = data;
148
149  g_free (buffer->text);
150  buffer->text = NULL;
151
152  g_free (buffer);
153}
154
155static void
156icon_tip_free (gpointer data)
157{
158  IconTip *icontip;
159
160  if (data == NULL)
161    return;
162
163  icontip = data;
164
165  if (icontip->fixedtip != NULL)
166    gtk_widget_destroy (GTK_WIDGET (icontip->fixedtip));
167  icontip->fixedtip = NULL;
168
169  if (icontip->source_id != 0)
170    g_source_remove (icontip->source_id);
171  icontip->source_id = 0;
172
173  if (icontip->buffer != NULL)
174    {
175      g_slist_foreach (icontip->buffer, icon_tip_buffer_free, NULL);
176      g_slist_free (icontip->buffer);
177    }
178  icontip->buffer = NULL;
179
180  g_free (icontip);
181}
182
183static int
184icon_tip_buffer_compare (gconstpointer a,
185                         gconstpointer b)
186{
187  const IconTipBuffer *buffer_a = a;
188  const IconTipBuffer *buffer_b = b;
189
190  if (buffer_a == NULL || buffer_b == NULL)
191    return !(buffer_a == buffer_b);
192
193  return buffer_a->id - buffer_b->id;
194}
195
196static void
197icon_tip_show_next_clicked (GtkWidget *widget,
198                            gpointer   data)
199{
200  icon_tip_show_next ((IconTip *) data);
201}
202
203static gboolean
204icon_tip_show_next_timeout (gpointer data)
205{
206  IconTip *icontip = (IconTip *) data;
207
208  icon_tip_show_next (icontip);
209
210  return FALSE;
211}
212
213static void
214icon_tip_show_next (IconTip *icontip)
215{
216  IconTipBuffer *buffer;
217
218  if (icontip->buffer == NULL)
219    {
220      /* this will also destroy the tip window */
221      g_hash_table_remove (icontip->tray->priv->trays_screen->tip_table,
222                           icontip->icon);
223      return;
224    }
225
226  if (icontip->source_id != 0)
227    g_source_remove (icontip->source_id);
228  icontip->source_id = 0;
229
230  buffer = icontip->buffer->data;
231  icontip->buffer = g_slist_remove (icontip->buffer, buffer);
232
233  if (icontip->fixedtip == NULL)
234    {
235      icontip->fixedtip = na_fixed_tip_new (icontip->icon,
236                                            na_tray_get_orientation (icontip->tray));
237
238      g_signal_connect (icontip->fixedtip, "clicked",
239                        G_CALLBACK (icon_tip_show_next_clicked), icontip);
240    }
241
242  na_fixed_tip_set_markup (icontip->fixedtip, buffer->text);
243
244  if (!GTK_WIDGET_MAPPED (icontip->fixedtip))
245    gtk_widget_show (icontip->fixedtip);
246
247  icontip->id = buffer->id;
248
249  if (buffer->timeout > 0)
250    icontip->source_id = g_timeout_add (buffer->timeout * 1000,
251                                        icon_tip_show_next_timeout, icontip);
252
253  icon_tip_buffer_free (buffer, NULL);
254}
255
256static void
257message_sent (NaTrayManager *manager,
258              GtkWidget     *icon,
259              const char    *text,
260              glong          id,
261              glong          timeout,
262              TraysScreen   *trays_screen)
263{
264  IconTip       *icontip;
265  IconTipBuffer  find_buffer;
266  IconTipBuffer *buffer;
267  gboolean       show_now;
268
269  icontip = g_hash_table_lookup (trays_screen->tip_table, icon);
270
271  find_buffer.id = id;
272  if (icontip && 
273      (icontip->id == id ||
274       g_slist_find_custom (icontip->buffer, &find_buffer,
275                            icon_tip_buffer_compare) != NULL))
276    /* we already have this message, so ignore it */
277    /* FIXME: in an ideal world, we'd remember all the past ids and ignore them
278     * too */
279    return;
280
281  show_now = FALSE;
282
283  if (icontip == NULL)
284    {
285      NaTray *tray;
286
287      tray = g_hash_table_lookup (trays_screen->icon_table, icon);
288      if (tray == NULL)
289        {
290          /* We don't know about the icon sending the message, so ignore it.
291           * But this should never happen since NaTrayManager shouldn't send
292           * us the message if there's no socket for it. */
293          g_critical ("Ignoring a message sent by a tray icon "
294                      "we don't know: \"%s\".\n", text);
295          return;
296        }
297
298      icontip = g_new0 (IconTip, 1);
299      icontip->tray = tray;
300      icontip->icon = icon;
301
302      g_hash_table_insert (trays_screen->tip_table, icon, icontip);
303
304      show_now = TRUE;
305    }
306
307  buffer = g_new0 (IconTipBuffer, 1);
308
309  buffer->text    = g_strdup (text);
310  buffer->id      = id;
311  buffer->timeout = timeout;
312
313  icontip->buffer = g_slist_append (icontip->buffer, buffer);
314
315  if (show_now)
316    icon_tip_show_next (icontip);
317}
318
319static void
320message_cancelled (NaTrayManager *manager,
321                   GtkWidget     *icon,
322                   glong          id,
323                   TraysScreen   *trays_screen)
324{
325  IconTip       *icontip;
326  IconTipBuffer  find_buffer;
327  GSList        *cancel_buffer_l;
328  IconTipBuffer *cancel_buffer;
329
330  icontip = g_hash_table_lookup (trays_screen->tip_table, icon);
331  if (icontip == NULL)
332    return;
333
334  if (icontip->id == id)
335    {
336      icon_tip_show_next (icontip);
337      return;
338    }
339
340  find_buffer.id = id;
341  cancel_buffer_l = g_slist_find_custom (icontip->buffer, &find_buffer,
342                                         icon_tip_buffer_compare);
343  if (cancel_buffer_l == NULL)
344    return;
345
346  cancel_buffer = cancel_buffer_l->data;
347  icon_tip_buffer_free (cancel_buffer, NULL);
348
349  icontip->buffer = g_slist_remove_link (icontip->buffer, cancel_buffer_l);
350  g_slist_free_1 (cancel_buffer_l);
351}
352
353static void
354update_orientation_for_messages (gpointer key,
355                                 gpointer value,
356                                 gpointer data)
357{
358  NaTray *tray;
359  IconTip    *icontip;
360
361  if (value == NULL)
362    return;
363
364  icontip = value;
365  tray    = data;
366  if (icontip->tray != tray)
367    return;
368
369  if (icontip->fixedtip)
370    na_fixed_tip_set_orientation (icontip->fixedtip, tray->priv->orientation);
371}
372
373static void
374update_size_and_orientation (NaTray *tray)
375{
376  NaTrayPrivate *priv = tray->priv;
377
378  na_obox_set_orientation (NA_OBOX (priv->box), priv->orientation);
379
380  /* This only happens when setting the property during object construction */
381  if (!priv->trays_screen)
382    return;
383
384  g_hash_table_foreach (priv->trays_screen->tip_table,
385                        update_orientation_for_messages, tray);
386
387  if (get_tray (priv->trays_screen) == tray)
388    na_tray_manager_set_orientation (priv->trays_screen->tray_manager,
389                                     priv->orientation);
390
391  /* note, you want this larger if the frame has non-NONE relief by default. */
392  switch (priv->orientation)
393    {
394    case GTK_ORIENTATION_VERTICAL:
395      /* Give box a min size so the frame doesn't look dumb */
396      gtk_widget_set_size_request (priv->box, MIN_BOX_SIZE, -1);
397      break;
398    case GTK_ORIENTATION_HORIZONTAL:
399      gtk_widget_set_size_request (priv->box, -1, MIN_BOX_SIZE);
400      break;
401    }
402
403  na_tray_force_redraw (tray);
404}
405
406static void
407na_tray_init (NaTray *tray)
408{
409  NaTrayPrivate *priv;
410
411  priv = tray->priv = G_TYPE_INSTANCE_GET_PRIVATE (tray, NA_TYPE_TRAY, NaTrayPrivate);
412
413  priv->screen = NULL;
414  priv->orientation = GTK_ORIENTATION_HORIZONTAL;
415
416  priv->frame = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
417  gtk_container_add (GTK_CONTAINER (tray), priv->frame);
418  gtk_widget_show (priv->frame);
419
420  priv->box = na_obox_new ();
421  gtk_box_set_spacing (GTK_BOX (priv->box), ICON_SPACING);
422  gtk_container_add (GTK_CONTAINER (priv->frame), priv->box);
423  gtk_widget_show (priv->box);
424}
425
426static GObject *
427na_tray_constructor (GType type,
428                     guint n_construct_properties,
429                     GObjectConstructParam *construct_params)
430{
431  GObject *object;
432  NaTray *tray;
433  NaTrayPrivate *priv;
434  int screen_number;
435
436  object = G_OBJECT_CLASS (na_tray_parent_class)->constructor (type,
437                                                               n_construct_properties,
438                                                               construct_params);
439  tray = NA_TRAY (object);
440  priv = tray->priv;
441
442  g_assert (priv->screen != NULL);
443
444  if (!initialized)
445    {
446      GdkDisplay *display;
447      int n_screens;
448
449      display = gdk_display_get_default ();
450      n_screens = gdk_display_get_n_screens (display);
451      trays_screens = g_new0 (TraysScreen, n_screens);
452      initialized = TRUE;
453    }
454
455  screen_number = gdk_screen_get_number (priv->screen);
456
457  if (trays_screens [screen_number].tray_manager == NULL)
458    {
459      NaTrayManager *tray_manager;
460
461      tray_manager = na_tray_manager_new ();
462
463      if (na_tray_manager_manage_screen (tray_manager, priv->screen))
464        {
465          trays_screens [screen_number].tray_manager = tray_manager;
466
467          g_signal_connect (tray_manager, "tray_icon_added",
468                            G_CALLBACK (tray_added),
469                            &trays_screens [screen_number]);
470          g_signal_connect (tray_manager, "tray_icon_removed",
471                            G_CALLBACK (tray_removed),
472                            &trays_screens [screen_number]);
473          g_signal_connect (tray_manager, "message_sent",
474                            G_CALLBACK (message_sent),
475                            &trays_screens [screen_number]);
476          g_signal_connect (tray_manager, "message_cancelled",
477                            G_CALLBACK (message_cancelled),
478                            &trays_screens [screen_number]);
479
480          trays_screens [screen_number].icon_table = g_hash_table_new (NULL,
481                                                                       NULL);
482          trays_screens [screen_number].tip_table = g_hash_table_new_full (
483                                                                NULL,
484                                                                NULL,
485                                                                NULL,
486                                                                icon_tip_free);
487        }
488      else
489        {
490          g_printerr ("System tray didn't get the system tray manager selection for screen %d\n",
491                      screen_number);
492          g_object_unref (tray_manager);
493        }
494    }
495     
496  priv->trays_screen = &trays_screens [screen_number];
497  trays_screens [screen_number].all_trays = g_slist_append (trays_screens [screen_number].all_trays,
498                                                            tray);
499
500  update_size_and_orientation (tray);
501
502  return object;
503}
504
505static void
506na_tray_dispose (GObject *object)
507{
508  NaTray *tray = NA_TRAY (object);
509  NaTrayPrivate *priv = tray->priv;
510  TraysScreen *trays_screen = priv->trays_screen;
511
512  if (trays_screen != NULL)
513    {
514      trays_screen->all_trays = g_slist_remove (trays_screen->all_trays, tray);
515
516      if (trays_screen->all_trays == NULL)
517        {
518          /* Make sure we drop the manager selection */
519          g_object_unref (trays_screen->tray_manager);
520          trays_screen->tray_manager = NULL;
521
522          g_hash_table_destroy (trays_screen->icon_table);
523          trays_screen->icon_table = NULL;
524
525          g_hash_table_destroy (trays_screen->tip_table);
526          trays_screen->tip_table = NULL;
527        }
528      else
529        {
530          NaTray *new_tray;
531
532          new_tray = get_tray (trays_screen);
533          if (new_tray != NULL)
534            na_tray_manager_set_orientation (trays_screen->tray_manager,
535                                             na_tray_get_orientation (new_tray));
536        }
537    }
538
539  priv->trays_screen = NULL;
540
541  if (priv->idle_redraw_id != 0)
542    {
543      g_source_remove (priv->idle_redraw_id);
544      priv->idle_redraw_id = 0;
545    }
546
547  G_OBJECT_CLASS (na_tray_parent_class)->dispose (object);
548}
549
550static void
551na_tray_set_property (GObject      *object,
552                      guint         prop_id,
553                      const GValue *value,
554                      GParamSpec   *pspec)
555{
556  NaTray *tray = NA_TRAY (object);
557  NaTrayPrivate *priv = tray->priv;
558
559  switch (prop_id)
560    {
561    case PROP_ORIENTATION:
562      na_tray_set_orientation (tray, g_value_get_enum (value));
563      break;
564    case PROP_SCREEN:
565      priv->screen = g_value_get_object (value);
566      break;
567    default:
568      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
569      break;
570    }
571}
572
573static void
574na_tray_size_request (GtkWidget        *widget,
575                      GtkRequisition   *requisition)
576{
577  gtk_widget_size_request (gtk_bin_get_child (GTK_BIN (widget)), requisition);
578}
579
580static void
581na_tray_size_allocate (GtkWidget        *widget,
582                       GtkAllocation    *allocation)
583{
584  gtk_widget_size_allocate (gtk_bin_get_child (GTK_BIN (widget)), allocation);
585}
586
587static void
588na_tray_class_init (NaTrayClass *klass)
589{
590  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
591  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
592
593  gobject_class->constructor = na_tray_constructor;
594  gobject_class->set_property = na_tray_set_property;
595  gobject_class->dispose = na_tray_dispose;
596
597  widget_class->size_request = na_tray_size_request;
598  widget_class->size_allocate = na_tray_size_allocate;
599
600  g_object_class_install_property
601    (gobject_class,
602     PROP_ORIENTATION,
603     g_param_spec_enum ("orientation", "orientation", "orientation",
604                        GTK_TYPE_ORIENTATION,
605                        GTK_ORIENTATION_HORIZONTAL,
606                        G_PARAM_WRITABLE |
607                        G_PARAM_CONSTRUCT_ONLY |
608                        G_PARAM_STATIC_NAME |
609                        G_PARAM_STATIC_NICK |
610                        G_PARAM_STATIC_BLURB));
611 
612  g_object_class_install_property
613    (gobject_class,
614     PROP_SCREEN,
615     g_param_spec_object ("screen", "screen", "screen",
616                          GDK_TYPE_SCREEN,
617                          G_PARAM_WRITABLE |
618                          G_PARAM_CONSTRUCT_ONLY |
619                          G_PARAM_STATIC_NAME |
620                          G_PARAM_STATIC_NICK |
621                          G_PARAM_STATIC_BLURB));
622
623  g_type_class_add_private (gobject_class, sizeof (NaTrayPrivate));
624}
625
626NaTray *
627na_tray_new_for_screen (GdkScreen      *screen,
628                        GtkOrientation  orientation)
629{
630  return g_object_new (NA_TYPE_TRAY,
631                       "screen", screen,
632                       "orientation", orientation,
633                       NULL);
634}
635
636void
637na_tray_set_orientation (NaTray         *tray,
638                         GtkOrientation  orientation)
639{
640  NaTrayPrivate *priv = tray->priv;
641
642  if (orientation == priv->orientation)
643    return;
644 
645  priv->orientation = orientation;
646
647  update_size_and_orientation (tray);
648}
649
650GtkOrientation
651na_tray_get_orientation (NaTray *tray)
652{
653  return tray->priv->orientation;
654}
655
656static gboolean
657idle_redraw_cb (NaTray *tray)
658{
659  NaTrayPrivate *priv = tray->priv;
660
661  priv->idle_redraw_id = 0;
662  gtk_widget_hide (priv->box);
663  gtk_widget_show (priv->box);
664
665  return FALSE;
666}
667
668void
669na_tray_force_redraw (NaTray *tray)
670{
671  NaTrayPrivate *priv = tray->priv;
672
673  /* Force the icons to redraw their backgrounds.
674   * gtk_widget_queue_draw() doesn't work across process boundaries,
675   * so we do this instead.
676   */
677  if (priv->idle_redraw_id == 0)
678    priv->idle_redraw_id = g_idle_add ((GSourceFunc) idle_redraw_cb, tray);
679}
Note: See TracBrowser for help on using the repository browser.