]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-presence-chooser.c
[darcs-to-svn @ Fix setting presence]
[empathy.git] / libempathy-gtk / gossip-presence-chooser.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2005-2007 Imendio AB
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Richard Hult <richard@imendio.com>
21  *          Martyn Russell <martyn@imendio.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <glade/glade.h>
32
33 #include <libempathy/gossip-utils.h>
34 #include <libempathy/empathy-marshal.h>
35
36 #include "gossip-ui-utils.h"
37 #include "gossip-stock.h"
38 #include "gossip-presence-chooser.h"
39 #include "gossip-status-presets.h"
40
41 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_PRESENCE_CHOOSER, GossipPresenceChooserPriv))
42
43 typedef struct {
44         GtkWidget  *hbox;
45         GtkWidget  *image;
46         GtkWidget  *label;
47         GtkWidget  *menu;
48
49         McPresence  last_state;
50
51         guint       flash_interval;
52         McPresence  flash_state_1;
53         McPresence  flash_state_2;
54         guint       flash_timeout_id;
55
56         /* The handle the kind of unnessecary scroll support. */
57         guint       scroll_timeout_id;
58         McPresence  scroll_state;
59         gchar      *scroll_status;
60 } GossipPresenceChooserPriv;
61
62 /* States for listed in the menu */
63 static McPresence states[] = {MC_PRESENCE_AVAILABLE,
64                               MC_PRESENCE_DO_NOT_DISTURB,
65                               MC_PRESENCE_AWAY};
66
67 static void     presence_chooser_finalize               (GObject               *object);
68 static void     presence_chooser_reset_scroll_timeout   (GossipPresenceChooser *chooser);
69 static void     presence_chooser_set_state              (GossipPresenceChooser *chooser,
70                                                          McPresence             state,
71                                                          const gchar           *status,
72                                                          gboolean               save);
73 static void     presence_chooser_dialog_response_cb     (GtkWidget             *dialog,
74                                                          gint                   response,
75                                                          GossipPresenceChooser *chooser);
76 static void     presence_chooser_show_dialog            (GossipPresenceChooser *chooser,
77                                                          McPresence             state);
78 static void     presence_chooser_custom_activate_cb     (GtkWidget             *item,
79                                                          GossipPresenceChooser *chooser);
80 static void     presence_chooser_clear_response_cb      (GtkWidget             *widget,
81                                                          gint                   response,
82                                                          gpointer               user_data);
83 static void     presence_chooser_clear_activate_cb      (GtkWidget             *item,
84                                                          GossipPresenceChooser *chooser);
85 static void     presence_chooser_menu_add_item          (GossipPresenceChooser *chooser,
86                                                          GtkWidget             *menu,
87                                                          const gchar           *str,
88                                                          McPresence             state,
89                                                          gboolean               custom);
90 static void     presence_chooser_menu_align_func        (GtkMenu               *menu,
91                                                          gint                  *x,
92                                                          gint                  *y,
93                                                          gboolean              *push_in,
94                                                          GossipPresenceChooser *chooser);
95 static void     presence_chooser_menu_selection_done_cb (GtkMenuShell          *menushell,
96                                                          GossipPresenceChooser *chooser);
97 static void     presence_chooser_menu_detach            (GtkWidget             *attach_widget,
98                                                          GtkMenu               *menu);
99 static void     presence_chooser_menu_popup             (GossipPresenceChooser *chooser);
100 static void     presence_chooser_menu_popdown           (GossipPresenceChooser *chooser);
101 static void     presence_chooser_toggled_cb             (GtkWidget             *chooser,
102                                                          gpointer               user_data);
103 static gboolean presence_chooser_button_press_event_cb  (GtkWidget             *chooser,
104                                                          GdkEventButton        *event,
105                                                          gpointer               user_data);
106 static gboolean presence_chooser_scroll_event_cb        (GtkWidget             *chooser,
107                                                          GdkEventScroll        *event,
108                                                          gpointer               user_data);
109 static gboolean presence_chooser_flash_timeout_cb       (GossipPresenceChooser *chooser);
110
111 G_DEFINE_TYPE (GossipPresenceChooser, gossip_presence_chooser, GTK_TYPE_TOGGLE_BUTTON);
112
113 enum {
114         CHANGED,
115         LAST_SIGNAL
116 };
117
118 static guint signals[LAST_SIGNAL];
119
120 static void
121 gossip_presence_chooser_class_init (GossipPresenceChooserClass *klass)
122 {
123         GObjectClass *object_class = G_OBJECT_CLASS (klass);
124
125         object_class->finalize = presence_chooser_finalize;
126
127         signals[CHANGED] =
128                 g_signal_new ("changed",
129                               G_TYPE_FROM_CLASS (klass),
130                               G_SIGNAL_RUN_LAST,
131                               0,
132                               NULL, NULL,
133                               empathy_marshal_VOID__INT_STRING,
134                               G_TYPE_NONE, 2,
135                               G_TYPE_INT, G_TYPE_STRING);
136
137         g_type_class_add_private (object_class, sizeof (GossipPresenceChooserPriv));
138 }
139
140 static void
141 gossip_presence_chooser_init (GossipPresenceChooser *chooser)
142 {
143         GossipPresenceChooserPriv *priv;
144         GtkWidget                 *arrow;
145         GtkWidget                 *alignment;
146
147         priv = GET_PRIV (chooser);
148
149         /* Default to 1/2 a second flash interval */
150         priv->flash_interval = 500;
151
152         gtk_button_set_relief (GTK_BUTTON (chooser), GTK_RELIEF_NONE);
153         gtk_button_set_focus_on_click (GTK_BUTTON (chooser), FALSE);
154
155         alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
156         gtk_widget_show (alignment);
157         gtk_container_add (GTK_CONTAINER (chooser), alignment);
158         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 1, 0);
159
160         priv->hbox = gtk_hbox_new (FALSE, 1);
161         gtk_widget_show (priv->hbox);
162         gtk_container_add (GTK_CONTAINER (alignment), priv->hbox);
163
164         priv->image = gtk_image_new ();
165         gtk_widget_show (priv->image);
166         gtk_box_pack_start (GTK_BOX (priv->hbox), priv->image, FALSE, TRUE, 0);
167
168         priv->label = gtk_label_new (NULL);
169         gtk_widget_show (priv->label);
170         gtk_box_pack_start (GTK_BOX (priv->hbox), priv->label, TRUE, TRUE, 0);
171         gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
172         gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
173         gtk_misc_set_padding (GTK_MISC (priv->label), 4, 1);
174
175         alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
176         gtk_widget_show (alignment);
177         gtk_box_pack_start (GTK_BOX (priv->hbox), alignment, FALSE, FALSE, 0);
178
179         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
180         gtk_widget_show (arrow);
181         gtk_container_add (GTK_CONTAINER (alignment), arrow);
182
183         g_signal_connect (chooser, "toggled",
184                           G_CALLBACK (presence_chooser_toggled_cb),
185                           NULL);
186         g_signal_connect (chooser, "button-press-event",
187                           G_CALLBACK (presence_chooser_button_press_event_cb),
188                           NULL);
189         g_signal_connect (chooser, "scroll-event",
190                           G_CALLBACK (presence_chooser_scroll_event_cb),
191                           NULL);
192 }
193
194 static void
195 presence_chooser_finalize (GObject *object)
196 {
197         GossipPresenceChooserPriv *priv;
198
199         priv = GET_PRIV (object);
200
201         if (priv->flash_timeout_id) {
202                 g_source_remove (priv->flash_timeout_id);
203         }
204
205         if (priv->scroll_timeout_id) {
206                 g_source_remove (priv->scroll_timeout_id);
207         }
208
209         G_OBJECT_CLASS (gossip_presence_chooser_parent_class)->finalize (object);
210 }
211
212 static void
213 presence_chooser_reset_scroll_timeout (GossipPresenceChooser *chooser)
214 {
215         GossipPresenceChooserPriv *priv;
216
217         priv = GET_PRIV (chooser);
218
219         if (priv->scroll_timeout_id) {
220                 g_source_remove (priv->scroll_timeout_id);
221                 priv->scroll_timeout_id = 0;
222         }
223
224         g_free (priv->scroll_status);
225         priv->scroll_status = NULL;
226 }
227
228 static void
229 presence_chooser_set_state (GossipPresenceChooser *chooser,
230                             McPresence             state,
231                             const gchar           *status,
232                             gboolean               save)
233 {
234         GossipPresenceChooserPriv *priv;
235         const gchar               *default_status;
236
237         priv = GET_PRIV (chooser);
238
239         default_status = gossip_presence_state_get_default_status (state);
240
241         if (G_STR_EMPTY (status)) {
242                 status = default_status;
243         } else {
244                 /* Only store the value if it differs from the default ones. */
245                 if (save && strcmp (status, default_status) != 0) {
246                         gossip_status_presets_set_last (state, status);
247                 }
248         }
249
250         priv->last_state = state;
251
252         presence_chooser_reset_scroll_timeout (chooser);
253         g_signal_emit (chooser, signals[CHANGED], 0, state, status);
254 }
255
256 static void
257 presence_chooser_dialog_response_cb (GtkWidget             *dialog,
258                                      gint                   response,
259                                      GossipPresenceChooser *chooser)
260 {
261         if (response == GTK_RESPONSE_OK) {
262                 GtkWidget           *entry;
263                 GtkWidget           *checkbutton;
264                 GtkListStore        *store;
265                 GtkTreeModel        *model;
266                 GtkTreeIter          iter;
267                 McPresence           state;
268                 const gchar         *status;
269                 gboolean             save;
270                 gboolean             duplicate = FALSE;
271                 gboolean             has_next;
272
273                 entry = g_object_get_data (G_OBJECT (dialog), "entry");
274                 status = gtk_entry_get_text (GTK_ENTRY (entry));
275                 store = g_object_get_data (G_OBJECT (dialog), "store");
276                 model = GTK_TREE_MODEL (store);
277
278                 has_next = gtk_tree_model_get_iter_first (model, &iter);
279                 while (has_next) {
280                         gchar *str;
281
282                         gtk_tree_model_get (model, &iter,
283                                             0, &str,
284                                             -1);
285
286                         if (strcmp (status, str) == 0) {
287                                 g_free (str);
288                                 duplicate = TRUE;
289                                 break;
290                         }
291
292                         g_free (str);
293
294                         has_next = gtk_tree_model_iter_next (model, &iter);
295                 }
296
297                 if (!duplicate) {
298                         gtk_list_store_append (store, &iter);
299                         gtk_list_store_set (store, &iter, 0, status, -1);
300                 }
301
302                 checkbutton = g_object_get_data (G_OBJECT (dialog), "checkbutton");
303                 save = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton));
304                 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dialog), "state"));
305
306                 presence_chooser_set_state (chooser, state, status, save);
307         }
308
309         gtk_widget_destroy (dialog);
310 }
311
312 static void
313 presence_chooser_show_dialog (GossipPresenceChooser *chooser,
314                               McPresence             state)
315 {
316         GossipPresenceChooserPriv *priv;
317         static GtkWidget          *dialog = NULL;
318         static GtkListStore       *store[LAST_MC_PRESENCE];
319         GladeXML                  *glade;
320         GtkWidget                 *image;
321         GtkWidget                 *combo;
322         GtkWidget                 *entry;
323         GtkWidget                 *checkbutton;
324         GdkPixbuf                 *pixbuf;
325         const gchar               *default_status;
326
327         priv = GET_PRIV (chooser);
328
329         if (dialog) {
330                 gtk_widget_destroy (dialog);
331                 dialog = NULL;
332         } else {
333                 guint i;
334
335                 for (i = 0; i < LAST_MC_PRESENCE; i++) {
336                         store[i] = NULL;
337                 }
338         }
339
340         glade = gossip_glade_get_file ("gossip-presence-chooser.glade",
341                                        "status_message_dialog",
342                                        NULL,
343                                        "status_message_dialog", &dialog,
344                                        "comboentry_status", &combo,
345                                        "image_status", &image,
346                                        "checkbutton_add", &checkbutton,
347                                        NULL);
348
349         g_object_unref (glade);
350
351         g_signal_connect (dialog, "destroy",
352                           G_CALLBACK (gtk_widget_destroyed),
353                           &dialog);
354         g_signal_connect (dialog, "response",
355                           G_CALLBACK (presence_chooser_dialog_response_cb),
356                           chooser);
357
358         pixbuf = gossip_pixbuf_for_presence_state (state);
359         gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
360         g_object_unref (pixbuf);
361
362         if (!store[state]) {
363                 GList       *presets, *l;
364                 GtkTreeIter  iter;
365
366                 store[state] = gtk_list_store_new (1, G_TYPE_STRING);
367
368                 presets = gossip_status_presets_get (state, -1);
369                 for (l = presets; l; l = l->next) {
370                         gtk_list_store_append (store[state], &iter);
371                         gtk_list_store_set (store[state], &iter, 0, l->data, -1);
372                 }
373
374                 g_list_free (presets);
375         }
376
377         default_status = gossip_presence_state_get_default_status (state);
378
379         entry = GTK_BIN (combo)->child;
380         gtk_entry_set_text (GTK_ENTRY (entry), default_status);
381         gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
382         gtk_entry_set_width_chars (GTK_ENTRY (entry), 25);
383
384         gtk_combo_box_set_model (GTK_COMBO_BOX (combo), GTK_TREE_MODEL (store[state]));
385         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo), 0);
386
387         /* FIXME: Set transian for a window ? */
388
389         g_object_set_data (G_OBJECT (dialog), "store", store[state]);
390         g_object_set_data (G_OBJECT (dialog), "entry", entry);
391         g_object_set_data (G_OBJECT (dialog), "checkbutton", checkbutton);
392         g_object_set_data (G_OBJECT (dialog), "state", GINT_TO_POINTER (state));
393
394         gtk_widget_show_all (dialog);
395 }
396
397 static void
398 presence_chooser_custom_activate_cb (GtkWidget             *item,
399                                      GossipPresenceChooser *chooser)
400 {
401         McPresence state;
402
403         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
404
405         presence_chooser_show_dialog (chooser, state);
406 }
407
408 static void
409 presence_chooser_noncustom_activate_cb (GtkWidget             *item,
410                                         GossipPresenceChooser *chooser)
411 {
412         McPresence   state;
413         const gchar *status;
414
415         status = g_object_get_data (G_OBJECT (item), "status");
416         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
417         presence_chooser_reset_scroll_timeout (chooser);
418         g_signal_emit (chooser, signals[CHANGED], 0, state, status);
419 }
420
421 static void
422 presence_chooser_clear_response_cb (GtkWidget *widget,
423                                     gint       response,
424                                     gpointer   user_data)
425 {
426         if (response == GTK_RESPONSE_OK) {
427                 gossip_status_presets_reset ();
428         }
429
430         gtk_widget_destroy (widget);
431 }
432
433 static void
434 presence_chooser_clear_activate_cb (GtkWidget             *item,
435                                     GossipPresenceChooser *chooser)
436 {
437         GtkWidget *dialog;
438         GtkWidget *toplevel;
439         GtkWindow *parent = NULL;
440
441         toplevel = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
442         if (GTK_WIDGET_TOPLEVEL (toplevel) &&
443             GTK_IS_WINDOW (toplevel)) {
444                 GtkWindow *window;
445                 gboolean   visible;
446
447                 window = GTK_WINDOW (toplevel);
448                 visible = gossip_window_get_is_visible (window);
449
450                 if (visible) {
451                         parent = window;
452                 }
453         }
454
455         dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
456                                          0,
457                                          GTK_MESSAGE_QUESTION,
458                                          GTK_BUTTONS_NONE,
459                                          _("Are you sure you want to clear the list?"));
460
461         gtk_message_dialog_format_secondary_text (
462                 GTK_MESSAGE_DIALOG (dialog),
463                 _("This will remove any custom messages you have "
464                   "added to the list of preset status messages."));
465
466         gtk_dialog_add_buttons (GTK_DIALOG (dialog),
467                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
468                                 _("Clear List"), GTK_RESPONSE_OK,
469                                 NULL);
470
471         gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), FALSE);
472
473         g_signal_connect (dialog, "response",
474                           G_CALLBACK (presence_chooser_clear_response_cb),
475                           NULL);
476
477         gtk_widget_show (dialog);
478 }
479
480 static void
481 presence_chooser_menu_add_item (GossipPresenceChooser *chooser,
482                                 GtkWidget             *menu,
483                                 const gchar           *str,
484                                 McPresence             state,
485                                 gboolean               custom)
486 {
487         GtkWidget   *item;
488         GtkWidget   *image;
489         const gchar *stock;
490
491         item = gtk_image_menu_item_new_with_label (str);
492         stock = gossip_stock_for_state (state);
493
494         if (custom) {
495                 g_signal_connect (
496                         item,
497                         "activate",
498                         G_CALLBACK (presence_chooser_custom_activate_cb),
499                         chooser);
500         } else {
501                 g_signal_connect (
502                         item,
503                         "activate",
504                         G_CALLBACK (presence_chooser_noncustom_activate_cb),
505                         chooser);
506         }
507
508         image = gtk_image_new_from_stock (stock, GTK_ICON_SIZE_MENU);
509         gtk_widget_show (image);
510
511         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
512         gtk_widget_show (item);
513
514         g_object_set_data_full (G_OBJECT (item),
515                                 "status", g_strdup (str),
516                                 (GDestroyNotify) g_free);
517
518         g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
519
520         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
521 }
522
523 static void
524 presence_chooser_menu_align_func (GtkMenu               *menu,
525                                   gint                  *x,
526                                   gint                  *y,
527                                   gboolean              *push_in,
528                                   GossipPresenceChooser *chooser)
529 {
530         GtkWidget      *widget;
531         GtkRequisition  req;
532         GdkScreen      *screen;
533         gint            screen_height;
534
535         widget = GTK_WIDGET (chooser);
536
537         gtk_widget_size_request (GTK_WIDGET (menu), &req);
538
539         gdk_window_get_origin (widget->window, x, y);
540
541         *x += widget->allocation.x + 1;
542         *y += widget->allocation.y;
543
544         screen = gtk_widget_get_screen (GTK_WIDGET (menu));
545         screen_height = gdk_screen_get_height (screen);
546
547         if (req.height > screen_height) {
548                 /* Too big for screen height anyway. */
549                 *y = 0;
550                 return;
551         }
552
553         if ((*y + req.height + widget->allocation.height) > screen_height) {
554                 /* Can't put it below the button. */
555                 *y -= req.height;
556                 *y += 1;
557         } else {
558                 /* Put menu below button. */
559                 *y += widget->allocation.height;
560                 *y -= 1;
561         }
562
563         *push_in = FALSE;
564 }
565
566 static void
567 presence_chooser_menu_selection_done_cb (GtkMenuShell          *menushell,
568                                          GossipPresenceChooser *chooser)
569 {
570         gtk_widget_destroy (GTK_WIDGET (menushell));
571
572         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), FALSE);
573 }
574
575 static void
576 presence_chooser_menu_destroy_cb (GtkWidget             *menu,
577                                   GossipPresenceChooser *chooser)
578 {
579         GossipPresenceChooserPriv *priv;
580
581         priv = GET_PRIV (chooser);
582
583         priv->menu = NULL;
584 }
585
586 static void
587 presence_chooser_menu_detach (GtkWidget *attach_widget,
588                               GtkMenu   *menu)
589 {
590         /* We don't need to do anything, but attaching the menu means
591          * we don't own the ref count and it is cleaned up properly.
592          */
593 }
594
595 static void
596 presence_chooser_menu_popup (GossipPresenceChooser *chooser)
597 {
598         GossipPresenceChooserPriv *priv;
599         GtkWidget                 *menu;
600
601         priv = GET_PRIV (chooser);
602
603         if (priv->menu) {
604                 return;
605         }
606
607         menu = gossip_presence_chooser_create_menu (chooser);
608
609         g_signal_connect_after (menu, "selection-done",
610                                 G_CALLBACK (presence_chooser_menu_selection_done_cb),
611                                 chooser);
612
613         g_signal_connect (menu, "destroy",
614                           G_CALLBACK (presence_chooser_menu_destroy_cb),
615                           chooser);
616
617         gtk_menu_attach_to_widget (GTK_MENU (menu),
618                                    GTK_WIDGET (chooser),
619                                    presence_chooser_menu_detach);
620
621         gtk_menu_popup (GTK_MENU (menu),
622                         NULL, NULL,
623                         (GtkMenuPositionFunc) presence_chooser_menu_align_func,
624                         chooser,
625                         1,
626                         gtk_get_current_event_time ());
627
628         priv->menu = menu;
629 }
630
631 static void
632 presence_chooser_menu_popdown (GossipPresenceChooser *chooser)
633 {
634         GossipPresenceChooserPriv *priv;
635
636         priv = GET_PRIV (chooser);
637
638         if (priv->menu) {
639                 gtk_widget_destroy (priv->menu);
640         }
641 }
642
643 static void
644 presence_chooser_toggled_cb (GtkWidget *chooser,
645                              gpointer   user_data)
646 {
647         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
648                 presence_chooser_menu_popup (GOSSIP_PRESENCE_CHOOSER (chooser));
649         } else {
650                 presence_chooser_menu_popdown (GOSSIP_PRESENCE_CHOOSER (chooser));
651         }
652 }
653
654 static gboolean
655 presence_chooser_button_press_event_cb (GtkWidget      *chooser,
656                                         GdkEventButton *event,
657                                         gpointer        user_data)
658 {
659         if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
660                 return FALSE;
661         }
662
663         if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
664                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), TRUE);
665                         return TRUE;
666                 }
667
668         return FALSE;
669 }
670
671 typedef struct {
672         McPresence   state;
673         const gchar *status;
674 } StateAndStatus;
675
676 static StateAndStatus *
677 presence_chooser_state_and_status_new (McPresence   state,
678                                        const gchar *status)
679 {
680         StateAndStatus *sas;
681
682         sas = g_new0 (StateAndStatus, 1);
683
684         sas->state = state;
685         sas->status = status;
686
687         return sas;
688 }
689
690 static GList *
691 presence_chooser_get_presets (GossipPresenceChooser *chooser)
692 {
693         GList      *list = NULL;
694         guint       i;
695
696         for (i = 0; i < G_N_ELEMENTS (states); i++) {
697                 GList          *presets, *p;
698                 StateAndStatus *sas;
699                 const gchar    *status;
700
701                 status = gossip_presence_state_get_default_status (states[i]);
702                 sas = presence_chooser_state_and_status_new (states[i], status);
703                 list = g_list_append (list, sas);
704         
705                 presets = gossip_status_presets_get (states[i], 5);
706                 for (p = presets; p; p = p->next) {
707                         sas = presence_chooser_state_and_status_new (states[i], p->data);
708                         list = g_list_append (list, sas);
709                 }
710                 g_list_free (presets);
711         }
712
713         return list;
714 }
715
716 static gboolean
717 presence_chooser_scroll_timeout_cb (GossipPresenceChooser *chooser)
718 {
719         GossipPresenceChooserPriv *priv;
720
721         priv = GET_PRIV (chooser);
722
723         g_signal_emit (chooser, signals[CHANGED], 0,
724                        priv->scroll_state,
725                        priv->scroll_status);
726
727         priv->scroll_timeout_id = 0;
728
729         g_free (priv->scroll_status);
730         priv->scroll_status = NULL;
731
732         return FALSE;
733 }
734
735 static gboolean
736 presence_chooser_scroll_event_cb (GtkWidget      *chooser,
737                                   GdkEventScroll *event,
738                                   gpointer        user_data)
739 {
740         GossipPresenceChooserPriv *priv;
741         GList                     *list, *l;
742         const gchar               *current_status;
743         StateAndStatus            *sas;
744         gboolean                   match;
745
746         priv = GET_PRIV (chooser);
747
748         switch (event->direction) {
749         case GDK_SCROLL_UP:
750                 break;
751         case GDK_SCROLL_DOWN:
752                 break;
753         default:
754                 return FALSE;
755         }
756
757         current_status = gtk_label_get_text (GTK_LABEL (priv->label));
758
759         /* Get the list of presets, which in this context means all the items
760          * without a trailing "...".
761          */
762         list = presence_chooser_get_presets (GOSSIP_PRESENCE_CHOOSER (chooser));
763         sas = NULL;
764         match = FALSE;
765         for (l = list; l; l = l->next) {
766                 sas = l->data;
767
768                 if (sas->state == priv->last_state &&
769                     strcmp (sas->status, current_status) == 0) {
770                         sas = NULL;
771                         match = TRUE;
772                         if (event->direction == GDK_SCROLL_UP) {
773                                 if (l->prev) {
774                                         sas = l->prev->data;
775                                 }
776                         }
777                         else if (event->direction == GDK_SCROLL_DOWN) {
778                                 if (l->next) {
779                                         sas = l->next->data;
780                                 }
781                         }
782                         break;
783                 }
784
785                 sas = NULL;
786         }
787
788         if (sas) {
789                 presence_chooser_reset_scroll_timeout (GOSSIP_PRESENCE_CHOOSER (chooser));
790
791                 priv->scroll_status = g_strdup (sas->status);
792                 priv->scroll_state = sas->state;
793
794                 priv->scroll_timeout_id =
795                         g_timeout_add (500,
796                                        (GSourceFunc) presence_chooser_scroll_timeout_cb,
797                                        chooser);
798
799                 gossip_presence_chooser_set_status (GOSSIP_PRESENCE_CHOOSER (chooser),
800                                                     sas->status);
801                 gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (chooser),
802                                                    sas->state);
803         }
804         else if (!match) {
805                 /* If we didn't get any match at all, it means the last state
806                  * was a custom one. Just switch to the first one.
807                  */
808                 presence_chooser_reset_scroll_timeout (GOSSIP_PRESENCE_CHOOSER (chooser));
809                 g_signal_emit (chooser, signals[CHANGED], 0,
810                                MC_PRESENCE_AVAILABLE,
811                                _("Available"));
812         }
813
814         g_list_foreach (list, (GFunc) g_free, NULL);
815         g_list_free (list);
816
817         return TRUE;
818 }
819
820 GtkWidget *
821 gossip_presence_chooser_new (void)
822 {
823         GtkWidget *chooser;
824
825         chooser = g_object_new (GOSSIP_TYPE_PRESENCE_CHOOSER, NULL);
826
827         return chooser;
828 }
829
830 GtkWidget *
831 gossip_presence_chooser_create_menu (GossipPresenceChooser *chooser)
832 {
833         GtkWidget *menu;
834         GtkWidget *item;
835         guint      i;
836
837         menu = gtk_menu_new ();
838
839         for (i = 0; i < G_N_ELEMENTS (states); i++) {
840                 GList       *list, *l;
841                 const gchar *status;
842
843                 status = gossip_presence_state_get_default_status (states[i]);
844                 presence_chooser_menu_add_item (chooser,
845                                                 menu,
846                                                 status,
847                                                 states[i],
848                                                 FALSE);
849
850                 list = gossip_status_presets_get (states[i], 5);
851                 for (l = list; l; l = l->next) {
852                         presence_chooser_menu_add_item (chooser,
853                                                         menu,
854                                                         l->data,
855                                                         states[i],
856                                                         FALSE);
857                 }
858                 g_list_free (list);
859
860                 presence_chooser_menu_add_item (chooser,
861                                                 menu,
862                                                 _("Custom message..."),
863                                                 states[i],
864                                                 TRUE);
865
866                 /* Separator. */
867                 item = gtk_menu_item_new ();
868                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
869                 gtk_widget_show (item);
870         }
871
872         item = gtk_menu_item_new_with_label (_("Clear List..."));
873         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
874         gtk_widget_show (item);
875
876         g_signal_connect (item,
877                           "activate",
878                           G_CALLBACK (presence_chooser_clear_activate_cb),
879                           chooser);
880
881         return menu;
882 }
883
884 void
885 gossip_presence_chooser_set_state (GossipPresenceChooser *chooser,
886                                    McPresence             state)
887 {
888         GossipPresenceChooserPriv *priv;
889
890         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
891
892         priv = GET_PRIV (chooser);
893
894         gossip_presence_chooser_flash_stop (chooser, state);
895 }
896
897 void
898 gossip_presence_chooser_set_status (GossipPresenceChooser *chooser,
899                                     const gchar           *status)
900 {
901         GossipPresenceChooserPriv *priv;
902
903         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
904
905         priv = GET_PRIV (chooser);
906
907         gtk_label_set_text (GTK_LABEL (priv->label), status);
908 }
909
910 void
911 gossip_presence_chooser_set_flash_interval (GossipPresenceChooser *chooser,
912                                             guint                  ms)
913 {
914         GossipPresenceChooserPriv *priv;
915
916         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
917         g_return_if_fail (ms > 1 && ms < 30000);
918
919         priv = GET_PRIV (chooser);
920
921         priv->flash_interval = ms;
922 }
923
924 static gboolean
925 presence_chooser_flash_timeout_cb (GossipPresenceChooser *chooser)
926 {
927         GossipPresenceChooserPriv *priv;
928         McPresence                 state;
929         GdkPixbuf                 *pixbuf;
930         static gboolean            on = FALSE;
931
932         priv = GET_PRIV (chooser);
933
934         if (on) {
935                 state = priv->flash_state_1;
936         } else {
937                 state = priv->flash_state_2;
938         }
939
940         pixbuf = gossip_pixbuf_for_presence_state (state);
941         gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
942         g_object_unref (pixbuf);
943
944         on = !on;
945
946         return TRUE;
947 }
948
949 void
950 gossip_presence_chooser_flash_start (GossipPresenceChooser *chooser,
951                                      McPresence             state_1,
952                                      McPresence             state_2)
953 {
954         GossipPresenceChooserPriv *priv;
955
956         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
957
958         priv = GET_PRIV (chooser);
959
960         if (priv->flash_timeout_id != 0) {
961                 return;
962         }
963
964         priv->flash_state_1 = state_1;
965         priv->flash_state_2 = state_2;
966
967         priv->flash_timeout_id = g_timeout_add (priv->flash_interval,
968                                                 (GSourceFunc) presence_chooser_flash_timeout_cb,
969                                                 chooser);
970 }
971
972 void
973 gossip_presence_chooser_flash_stop (GossipPresenceChooser *chooser,
974                                     McPresence             state)
975 {
976         GossipPresenceChooserPriv *priv;
977         GdkPixbuf                 *pixbuf;
978
979         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
980
981         priv = GET_PRIV (chooser);
982
983         if (priv->flash_timeout_id) {
984                 g_source_remove (priv->flash_timeout_id);
985                 priv->flash_timeout_id = 0;
986         }
987
988         pixbuf = gossip_pixbuf_for_presence_state (state);
989         gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
990         g_object_unref (pixbuf);
991
992         priv->last_state = state;
993 }
994
995 gboolean
996 gossip_presence_chooser_is_flashing (GossipPresenceChooser *chooser)
997 {
998         GossipPresenceChooserPriv *priv;
999
1000         g_return_val_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser), FALSE);
1001
1002         priv = GET_PRIV (chooser);
1003
1004         if (priv->flash_timeout_id) {
1005                 return TRUE;
1006         }
1007
1008         return FALSE;
1009 }
1010