]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-presence-chooser.c
[darcs-to-svn @ Use icon-name API instead of stock icons and update tango icons]
[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 "empathy-images.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 to be 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         const gchar               *default_status;
325
326         priv = GET_PRIV (chooser);
327
328         if (dialog) {
329                 gtk_widget_destroy (dialog);
330                 dialog = NULL;
331         } else {
332                 guint i;
333
334                 for (i = 0; i < LAST_MC_PRESENCE; i++) {
335                         store[i] = NULL;
336                 }
337         }
338
339         glade = gossip_glade_get_file ("gossip-presence-chooser.glade",
340                                        "status_message_dialog",
341                                        NULL,
342                                        "status_message_dialog", &dialog,
343                                        "comboentry_status", &combo,
344                                        "image_status", &image,
345                                        "checkbutton_add", &checkbutton,
346                                        NULL);
347
348         g_object_unref (glade);
349
350         g_signal_connect (dialog, "destroy",
351                           G_CALLBACK (gtk_widget_destroyed),
352                           &dialog);
353         g_signal_connect (dialog, "response",
354                           G_CALLBACK (presence_chooser_dialog_response_cb),
355                           chooser);
356
357         gtk_image_set_from_icon_name (GTK_IMAGE (image),
358                                       gossip_icon_name_for_presence_state (state),
359                                       GTK_ICON_SIZE_MENU);
360
361         if (!store[state]) {
362                 GList       *presets, *l;
363                 GtkTreeIter  iter;
364
365                 store[state] = gtk_list_store_new (1, G_TYPE_STRING);
366
367                 presets = gossip_status_presets_get (state, -1);
368                 for (l = presets; l; l = l->next) {
369                         gtk_list_store_append (store[state], &iter);
370                         gtk_list_store_set (store[state], &iter, 0, l->data, -1);
371                 }
372
373                 g_list_free (presets);
374         }
375
376         default_status = gossip_presence_state_get_default_status (state);
377
378         entry = GTK_BIN (combo)->child;
379         gtk_entry_set_text (GTK_ENTRY (entry), default_status);
380         gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
381         gtk_entry_set_width_chars (GTK_ENTRY (entry), 25);
382
383         gtk_combo_box_set_model (GTK_COMBO_BOX (combo), GTK_TREE_MODEL (store[state]));
384         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo), 0);
385
386         /* FIXME: Set transian for a window ? */
387
388         g_object_set_data (G_OBJECT (dialog), "store", store[state]);
389         g_object_set_data (G_OBJECT (dialog), "entry", entry);
390         g_object_set_data (G_OBJECT (dialog), "checkbutton", checkbutton);
391         g_object_set_data (G_OBJECT (dialog), "state", GINT_TO_POINTER (state));
392
393         gtk_widget_show_all (dialog);
394 }
395
396 static void
397 presence_chooser_custom_activate_cb (GtkWidget             *item,
398                                      GossipPresenceChooser *chooser)
399 {
400         McPresence state;
401
402         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
403
404         presence_chooser_show_dialog (chooser, state);
405 }
406
407 static void
408 presence_chooser_noncustom_activate_cb (GtkWidget             *item,
409                                         GossipPresenceChooser *chooser)
410 {
411         McPresence   state;
412         const gchar *status;
413
414         status = g_object_get_data (G_OBJECT (item), "status");
415         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
416         presence_chooser_reset_scroll_timeout (chooser);
417         g_signal_emit (chooser, signals[CHANGED], 0, state, status);
418 }
419
420 static void
421 presence_chooser_clear_response_cb (GtkWidget *widget,
422                                     gint       response,
423                                     gpointer   user_data)
424 {
425         if (response == GTK_RESPONSE_OK) {
426                 gossip_status_presets_reset ();
427         }
428
429         gtk_widget_destroy (widget);
430 }
431
432 static void
433 presence_chooser_clear_activate_cb (GtkWidget             *item,
434                                     GossipPresenceChooser *chooser)
435 {
436         GtkWidget *dialog;
437         GtkWidget *toplevel;
438         GtkWindow *parent = NULL;
439
440         toplevel = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
441         if (GTK_WIDGET_TOPLEVEL (toplevel) &&
442             GTK_IS_WINDOW (toplevel)) {
443                 GtkWindow *window;
444                 gboolean   visible;
445
446                 window = GTK_WINDOW (toplevel);
447                 visible = gossip_window_get_is_visible (window);
448
449                 if (visible) {
450                         parent = window;
451                 }
452         }
453
454         dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
455                                          0,
456                                          GTK_MESSAGE_QUESTION,
457                                          GTK_BUTTONS_NONE,
458                                          _("Are you sure you want to clear the list?"));
459
460         gtk_message_dialog_format_secondary_text (
461                 GTK_MESSAGE_DIALOG (dialog),
462                 _("This will remove any custom messages you have "
463                   "added to the list of preset status messages."));
464
465         gtk_dialog_add_buttons (GTK_DIALOG (dialog),
466                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
467                                 _("Clear List"), GTK_RESPONSE_OK,
468                                 NULL);
469
470         gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), FALSE);
471
472         g_signal_connect (dialog, "response",
473                           G_CALLBACK (presence_chooser_clear_response_cb),
474                           NULL);
475
476         gtk_widget_show (dialog);
477 }
478
479 static void
480 presence_chooser_menu_add_item (GossipPresenceChooser *chooser,
481                                 GtkWidget             *menu,
482                                 const gchar           *str,
483                                 McPresence             state,
484                                 gboolean               custom)
485 {
486         GtkWidget   *item;
487         GtkWidget   *image;
488         const gchar *icon_name;
489
490         item = gtk_image_menu_item_new_with_label (str);
491         icon_name = gossip_icon_name_for_presence_state (state);
492
493         if (custom) {
494                 g_signal_connect (
495                         item,
496                         "activate",
497                         G_CALLBACK (presence_chooser_custom_activate_cb),
498                         chooser);
499         } else {
500                 g_signal_connect (
501                         item,
502                         "activate",
503                         G_CALLBACK (presence_chooser_noncustom_activate_cb),
504                         chooser);
505         }
506
507         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
508         gtk_widget_show (image);
509
510         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
511         gtk_widget_show (item);
512
513         g_object_set_data_full (G_OBJECT (item),
514                                 "status", g_strdup (str),
515                                 (GDestroyNotify) g_free);
516
517         g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
518
519         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
520 }
521
522 static void
523 presence_chooser_menu_align_func (GtkMenu               *menu,
524                                   gint                  *x,
525                                   gint                  *y,
526                                   gboolean              *push_in,
527                                   GossipPresenceChooser *chooser)
528 {
529         GtkWidget      *widget;
530         GtkRequisition  req;
531         GdkScreen      *screen;
532         gint            screen_height;
533
534         widget = GTK_WIDGET (chooser);
535
536         gtk_widget_size_request (GTK_WIDGET (menu), &req);
537
538         gdk_window_get_origin (widget->window, x, y);
539
540         *x += widget->allocation.x + 1;
541         *y += widget->allocation.y;
542
543         screen = gtk_widget_get_screen (GTK_WIDGET (menu));
544         screen_height = gdk_screen_get_height (screen);
545
546         if (req.height > screen_height) {
547                 /* Too big for screen height anyway. */
548                 *y = 0;
549                 return;
550         }
551
552         if ((*y + req.height + widget->allocation.height) > screen_height) {
553                 /* Can't put it below the button. */
554                 *y -= req.height;
555                 *y += 1;
556         } else {
557                 /* Put menu below button. */
558                 *y += widget->allocation.height;
559                 *y -= 1;
560         }
561
562         *push_in = FALSE;
563 }
564
565 static void
566 presence_chooser_menu_selection_done_cb (GtkMenuShell          *menushell,
567                                          GossipPresenceChooser *chooser)
568 {
569         gtk_widget_destroy (GTK_WIDGET (menushell));
570
571         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), FALSE);
572 }
573
574 static void
575 presence_chooser_menu_destroy_cb (GtkWidget             *menu,
576                                   GossipPresenceChooser *chooser)
577 {
578         GossipPresenceChooserPriv *priv;
579
580         priv = GET_PRIV (chooser);
581
582         priv->menu = NULL;
583 }
584
585 static void
586 presence_chooser_menu_detach (GtkWidget *attach_widget,
587                               GtkMenu   *menu)
588 {
589         /* We don't need to do anything, but attaching the menu means
590          * we don't own the ref count and it is cleaned up properly.
591          */
592 }
593
594 static void
595 presence_chooser_menu_popup (GossipPresenceChooser *chooser)
596 {
597         GossipPresenceChooserPriv *priv;
598         GtkWidget                 *menu;
599
600         priv = GET_PRIV (chooser);
601
602         if (priv->menu) {
603                 return;
604         }
605
606         menu = gossip_presence_chooser_create_menu (chooser);
607
608         g_signal_connect_after (menu, "selection-done",
609                                 G_CALLBACK (presence_chooser_menu_selection_done_cb),
610                                 chooser);
611
612         g_signal_connect (menu, "destroy",
613                           G_CALLBACK (presence_chooser_menu_destroy_cb),
614                           chooser);
615
616         gtk_menu_attach_to_widget (GTK_MENU (menu),
617                                    GTK_WIDGET (chooser),
618                                    presence_chooser_menu_detach);
619
620         gtk_menu_popup (GTK_MENU (menu),
621                         NULL, NULL,
622                         (GtkMenuPositionFunc) presence_chooser_menu_align_func,
623                         chooser,
624                         1,
625                         gtk_get_current_event_time ());
626
627         priv->menu = menu;
628 }
629
630 static void
631 presence_chooser_menu_popdown (GossipPresenceChooser *chooser)
632 {
633         GossipPresenceChooserPriv *priv;
634
635         priv = GET_PRIV (chooser);
636
637         if (priv->menu) {
638                 gtk_widget_destroy (priv->menu);
639         }
640 }
641
642 static void
643 presence_chooser_toggled_cb (GtkWidget *chooser,
644                              gpointer   user_data)
645 {
646         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
647                 presence_chooser_menu_popup (GOSSIP_PRESENCE_CHOOSER (chooser));
648         } else {
649                 presence_chooser_menu_popdown (GOSSIP_PRESENCE_CHOOSER (chooser));
650         }
651 }
652
653 static gboolean
654 presence_chooser_button_press_event_cb (GtkWidget      *chooser,
655                                         GdkEventButton *event,
656                                         gpointer        user_data)
657 {
658         if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
659                 return FALSE;
660         }
661
662         if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
663                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), TRUE);
664                         return TRUE;
665                 }
666
667         return FALSE;
668 }
669
670 typedef struct {
671         McPresence   state;
672         const gchar *status;
673 } StateAndStatus;
674
675 static StateAndStatus *
676 presence_chooser_state_and_status_new (McPresence   state,
677                                        const gchar *status)
678 {
679         StateAndStatus *sas;
680
681         sas = g_new0 (StateAndStatus, 1);
682
683         sas->state = state;
684         sas->status = status;
685
686         return sas;
687 }
688
689 static GList *
690 presence_chooser_get_presets (GossipPresenceChooser *chooser)
691 {
692         GList      *list = NULL;
693         guint       i;
694
695         for (i = 0; i < G_N_ELEMENTS (states); i++) {
696                 GList          *presets, *p;
697                 StateAndStatus *sas;
698                 const gchar    *status;
699
700                 status = gossip_presence_state_get_default_status (states[i]);
701                 sas = presence_chooser_state_and_status_new (states[i], status);
702                 list = g_list_append (list, sas);
703         
704                 presets = gossip_status_presets_get (states[i], 5);
705                 for (p = presets; p; p = p->next) {
706                         sas = presence_chooser_state_and_status_new (states[i], p->data);
707                         list = g_list_append (list, sas);
708                 }
709                 g_list_free (presets);
710         }
711
712         return list;
713 }
714
715 static gboolean
716 presence_chooser_scroll_timeout_cb (GossipPresenceChooser *chooser)
717 {
718         GossipPresenceChooserPriv *priv;
719
720         priv = GET_PRIV (chooser);
721
722         g_signal_emit (chooser, signals[CHANGED], 0,
723                        priv->scroll_state,
724                        priv->scroll_status);
725
726         priv->scroll_timeout_id = 0;
727
728         g_free (priv->scroll_status);
729         priv->scroll_status = NULL;
730
731         return FALSE;
732 }
733
734 static gboolean
735 presence_chooser_scroll_event_cb (GtkWidget      *chooser,
736                                   GdkEventScroll *event,
737                                   gpointer        user_data)
738 {
739         GossipPresenceChooserPriv *priv;
740         GList                     *list, *l;
741         const gchar               *current_status;
742         StateAndStatus            *sas;
743         gboolean                   match;
744
745         priv = GET_PRIV (chooser);
746
747         switch (event->direction) {
748         case GDK_SCROLL_UP:
749                 break;
750         case GDK_SCROLL_DOWN:
751                 break;
752         default:
753                 return FALSE;
754         }
755
756         current_status = gtk_label_get_text (GTK_LABEL (priv->label));
757
758         /* Get the list of presets, which in this context means all the items
759          * without a trailing "...".
760          */
761         list = presence_chooser_get_presets (GOSSIP_PRESENCE_CHOOSER (chooser));
762         sas = NULL;
763         match = FALSE;
764         for (l = list; l; l = l->next) {
765                 sas = l->data;
766
767                 if (sas->state == priv->last_state &&
768                     strcmp (sas->status, current_status) == 0) {
769                         sas = NULL;
770                         match = TRUE;
771                         if (event->direction == GDK_SCROLL_UP) {
772                                 if (l->prev) {
773                                         sas = l->prev->data;
774                                 }
775                         }
776                         else if (event->direction == GDK_SCROLL_DOWN) {
777                                 if (l->next) {
778                                         sas = l->next->data;
779                                 }
780                         }
781                         break;
782                 }
783
784                 sas = NULL;
785         }
786
787         if (sas) {
788                 presence_chooser_reset_scroll_timeout (GOSSIP_PRESENCE_CHOOSER (chooser));
789
790                 priv->scroll_status = g_strdup (sas->status);
791                 priv->scroll_state = sas->state;
792
793                 priv->scroll_timeout_id =
794                         g_timeout_add (500,
795                                        (GSourceFunc) presence_chooser_scroll_timeout_cb,
796                                        chooser);
797
798                 gossip_presence_chooser_set_status (GOSSIP_PRESENCE_CHOOSER (chooser),
799                                                     sas->status);
800                 gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (chooser),
801                                                    sas->state);
802         }
803         else if (!match) {
804                 /* If we didn't get any match at all, it means the last state
805                  * was a custom one. Just switch to the first one.
806                  */
807                 presence_chooser_reset_scroll_timeout (GOSSIP_PRESENCE_CHOOSER (chooser));
808                 g_signal_emit (chooser, signals[CHANGED], 0,
809                                MC_PRESENCE_AVAILABLE,
810                                _("Available"));
811         }
812
813         g_list_foreach (list, (GFunc) g_free, NULL);
814         g_list_free (list);
815
816         return TRUE;
817 }
818
819 GtkWidget *
820 gossip_presence_chooser_new (void)
821 {
822         GtkWidget *chooser;
823
824         chooser = g_object_new (GOSSIP_TYPE_PRESENCE_CHOOSER, NULL);
825
826         return chooser;
827 }
828
829 GtkWidget *
830 gossip_presence_chooser_create_menu (GossipPresenceChooser *chooser)
831 {
832         const gchar *status;
833         GtkWidget *menu;
834         GtkWidget *item;
835         GtkWidget *image;
836         guint      i;
837
838         menu = gtk_menu_new ();
839
840         for (i = 0; i < G_N_ELEMENTS (states); i++) {
841                 GList       *list, *l;
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         /* Offline to disconnect */
873         status = gossip_presence_state_get_default_status (MC_PRESENCE_OFFLINE);
874         presence_chooser_menu_add_item (chooser,
875                                         menu,
876                                         status,
877                                         MC_PRESENCE_OFFLINE,
878                                         FALSE);
879         /* Separator. */
880         item = gtk_menu_item_new ();
881         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
882         gtk_widget_show (item);
883
884         /* Clear list */
885         item = gtk_image_menu_item_new_with_label (_("Clear List..."));
886         image = gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
887         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
888         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
889         gtk_widget_show (image);
890         gtk_widget_show (item);
891
892         g_signal_connect (item,
893                           "activate",
894                           G_CALLBACK (presence_chooser_clear_activate_cb),
895                           chooser);
896
897         return menu;
898 }
899
900 void
901 gossip_presence_chooser_set_state (GossipPresenceChooser *chooser,
902                                    McPresence             state)
903 {
904         GossipPresenceChooserPriv *priv;
905
906         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
907
908         priv = GET_PRIV (chooser);
909
910         gossip_presence_chooser_flash_stop (chooser, state);
911 }
912
913 void
914 gossip_presence_chooser_set_status (GossipPresenceChooser *chooser,
915                                     const gchar           *status)
916 {
917         GossipPresenceChooserPriv *priv;
918
919         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
920
921         priv = GET_PRIV (chooser);
922
923         gtk_label_set_text (GTK_LABEL (priv->label), status);
924 }
925
926 void
927 gossip_presence_chooser_set_flash_interval (GossipPresenceChooser *chooser,
928                                             guint                  ms)
929 {
930         GossipPresenceChooserPriv *priv;
931
932         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
933         g_return_if_fail (ms > 1 && ms < 30000);
934
935         priv = GET_PRIV (chooser);
936
937         priv->flash_interval = ms;
938 }
939
940 static gboolean
941 presence_chooser_flash_timeout_cb (GossipPresenceChooser *chooser)
942 {
943         GossipPresenceChooserPriv *priv;
944         McPresence                 state;
945         static gboolean            on = FALSE;
946
947         priv = GET_PRIV (chooser);
948
949         if (on) {
950                 state = priv->flash_state_1;
951         } else {
952                 state = priv->flash_state_2;
953         }
954
955         gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
956                                       gossip_icon_name_for_presence_state (state),
957                                       GTK_ICON_SIZE_MENU);
958
959         on = !on;
960
961         return TRUE;
962 }
963
964 void
965 gossip_presence_chooser_flash_start (GossipPresenceChooser *chooser,
966                                      McPresence             state_1,
967                                      McPresence             state_2)
968 {
969         GossipPresenceChooserPriv *priv;
970
971         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
972
973         priv = GET_PRIV (chooser);
974
975         if (priv->flash_timeout_id != 0) {
976                 return;
977         }
978
979         priv->flash_state_1 = state_1;
980         priv->flash_state_2 = state_2;
981
982         priv->flash_timeout_id = g_timeout_add (priv->flash_interval,
983                                                 (GSourceFunc) presence_chooser_flash_timeout_cb,
984                                                 chooser);
985 }
986
987 void
988 gossip_presence_chooser_flash_stop (GossipPresenceChooser *chooser,
989                                     McPresence             state)
990 {
991         GossipPresenceChooserPriv *priv;
992
993         g_return_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser));
994
995         priv = GET_PRIV (chooser);
996
997         if (priv->flash_timeout_id) {
998                 g_source_remove (priv->flash_timeout_id);
999                 priv->flash_timeout_id = 0;
1000         }
1001
1002         gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
1003                                       gossip_icon_name_for_presence_state (state),
1004                                       GTK_ICON_SIZE_MENU);
1005
1006         priv->last_state = state;
1007 }
1008
1009 gboolean
1010 gossip_presence_chooser_is_flashing (GossipPresenceChooser *chooser)
1011 {
1012         GossipPresenceChooserPriv *priv;
1013
1014         g_return_val_if_fail (GOSSIP_IS_PRESENCE_CHOOSER (chooser), FALSE);
1015
1016         priv = GET_PRIV (chooser);
1017
1018         if (priv->flash_timeout_id) {
1019                 return TRUE;
1020         }
1021
1022         return FALSE;
1023 }
1024