]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-presence-chooser.c
Make use of tp-glib debug system.
[empathy.git] / libempathy-gtk / empathy-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  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33
34 #include <telepathy-glib/util.h>
35 #include <libmissioncontrol/mc-enum-types.h>
36
37 #include <libempathy/empathy-idle.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-status-presets.h>
40
41 #include "empathy-ui-utils.h"
42 #include "empathy-images.h"
43 #include "empathy-presence-chooser.h"
44
45 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv))
46
47 /* Flashing delay for icons (milliseconds). */
48 #define FLASH_TIMEOUT 500
49
50 typedef struct {
51         EmpathyIdle *idle;
52
53         GtkWidget   *hbox;
54         GtkWidget   *image;
55         GtkWidget   *label;
56         GtkWidget   *menu;
57
58         McPresence   last_state;
59
60         McPresence   flash_state_1;
61         McPresence   flash_state_2;
62         guint        flash_timeout_id;
63
64         /* The handle the kind of unnessecary scroll support. */
65         guint        scroll_timeout_id;
66         McPresence   scroll_state;
67         gchar       *scroll_status;
68 } EmpathyPresenceChooserPriv;
69
70 typedef struct {
71         GtkWidget    *dialog;
72         GtkWidget    *checkbutton_save;
73         GtkWidget    *comboboxentry_message;
74         GtkWidget    *entry_message;
75         GtkWidget    *combobox_status;
76         GtkTreeModel *model_status;
77 } CustomMessageDialog;
78
79 enum {
80         COL_ICON,
81         COL_LABEL,
82         COL_PRESENCE,
83         COL_COUNT
84 };
85
86 typedef struct {
87         McPresence   state;
88         const gchar *status;
89 } StateAndStatus;
90
91 static CustomMessageDialog *message_dialog = NULL;
92 /* States to be listed in the menu.
93  * Each state has a boolean telling if it can have custom message */
94 static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
95                          MC_PRESENCE_DO_NOT_DISTURB, TRUE,
96                          MC_PRESENCE_AWAY, TRUE,
97                          MC_PRESENCE_HIDDEN, FALSE,
98                          MC_PRESENCE_OFFLINE, FALSE};
99
100 static void            empathy_presence_chooser_class_init      (EmpathyPresenceChooserClass *klass);
101 static void            empathy_presence_chooser_init            (EmpathyPresenceChooser      *chooser);
102 static void            presence_chooser_finalize               (GObject                    *object);
103 static void            presence_chooser_presence_changed_cb    (EmpathyPresenceChooser      *chooser);
104 static void            presence_chooser_reset_scroll_timeout   (EmpathyPresenceChooser      *chooser);
105 static gboolean        presence_chooser_scroll_timeout_cb      (EmpathyPresenceChooser      *chooser);
106 static gboolean        presence_chooser_scroll_event_cb        (EmpathyPresenceChooser      *chooser,
107                                                                 GdkEventScroll             *event,
108                                                                 gpointer                    user_data);
109 static GList *         presence_chooser_get_presets            (EmpathyPresenceChooser      *chooser);
110 static StateAndStatus *presence_chooser_state_and_status_new   (McPresence                  state,
111                                                                 const gchar                *status);
112 static gboolean        presence_chooser_flash_timeout_cb       (EmpathyPresenceChooser      *chooser);
113 static void            presence_chooser_flash_start            (EmpathyPresenceChooser      *chooser,
114                                                                 McPresence                  state_1,
115                                                                 McPresence                  state_2);
116 static void            presence_chooser_flash_stop             (EmpathyPresenceChooser      *chooser,
117                                                                 McPresence                  state);
118 static gboolean        presence_chooser_button_press_event_cb  (GtkWidget                  *chooser,
119                                                                 GdkEventButton             *event,
120                                                                 gpointer                    user_data);
121 static void            presence_chooser_toggled_cb             (GtkWidget                  *chooser,
122                                                                 gpointer                    user_data);
123 static void            presence_chooser_menu_popup             (EmpathyPresenceChooser      *chooser);
124 static void            presence_chooser_menu_popdown           (EmpathyPresenceChooser      *chooser);
125 static void            presence_chooser_menu_selection_done_cb (GtkMenuShell               *menushell,
126                                                                 EmpathyPresenceChooser      *chooser);
127 static void            presence_chooser_menu_destroy_cb        (GtkWidget                  *menu,
128                                                                 EmpathyPresenceChooser      *chooser);
129 static void            presence_chooser_menu_detach            (GtkWidget                  *attach_widget,
130                                                                 GtkMenu                    *menu);
131 static void            presence_chooser_menu_align_func        (GtkMenu                    *menu,
132                                                                 gint                       *x,
133                                                                 gint                       *y,
134                                                                 gboolean                   *push_in,
135                                                                 GtkWidget                  *widget);
136 static void            presence_chooser_menu_add_item          (GtkWidget                  *menu,
137                                                                 const gchar                *str,
138                                                                 McPresence                  state);
139 static void            presence_chooser_noncustom_activate_cb  (GtkWidget                  *item,
140                                                                 gpointer                    user_data);
141 static void            presence_chooser_set_state              (McPresence                  state,
142                                                                 const gchar                *status);
143 static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
144                                                                 gpointer                    user_data);
145 static void            presence_chooser_dialog_show            (void);
146
147 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_TOGGLE_BUTTON);
148
149 static void
150 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
151 {
152         GObjectClass *object_class = G_OBJECT_CLASS (klass);
153
154         object_class->finalize = presence_chooser_finalize;
155
156         g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
157 }
158
159 static void
160 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
161 {
162         EmpathyPresenceChooserPriv *priv;
163         GtkWidget                 *arrow;
164         GtkWidget                 *alignment;
165
166         priv = GET_PRIV (chooser);
167
168         gtk_button_set_relief (GTK_BUTTON (chooser), GTK_RELIEF_NONE);
169         gtk_button_set_focus_on_click (GTK_BUTTON (chooser), FALSE);
170
171         alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
172         gtk_widget_show (alignment);
173         gtk_container_add (GTK_CONTAINER (chooser), alignment);
174         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 1, 0);
175
176         priv->hbox = gtk_hbox_new (FALSE, 1);
177         gtk_widget_show (priv->hbox);
178         gtk_container_add (GTK_CONTAINER (alignment), priv->hbox);
179
180         priv->image = gtk_image_new ();
181         gtk_widget_show (priv->image);
182         gtk_box_pack_start (GTK_BOX (priv->hbox), priv->image, FALSE, TRUE, 0);
183
184         priv->label = gtk_label_new (NULL);
185         gtk_widget_show (priv->label);
186         gtk_box_pack_start (GTK_BOX (priv->hbox), priv->label, TRUE, TRUE, 0);
187         gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
188         gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
189         gtk_misc_set_padding (GTK_MISC (priv->label), 4, 1);
190
191         alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
192         gtk_widget_show (alignment);
193         gtk_box_pack_start (GTK_BOX (priv->hbox), alignment, FALSE, FALSE, 0);
194
195         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
196         gtk_widget_show (arrow);
197         gtk_container_add (GTK_CONTAINER (alignment), arrow);
198
199         g_signal_connect (chooser, "toggled",
200                           G_CALLBACK (presence_chooser_toggled_cb),
201                           NULL);
202         g_signal_connect (chooser, "button-press-event",
203                           G_CALLBACK (presence_chooser_button_press_event_cb),
204                           NULL);
205         g_signal_connect (chooser, "scroll-event",
206                           G_CALLBACK (presence_chooser_scroll_event_cb),
207                           NULL);
208
209         priv->idle = empathy_idle_new ();
210         presence_chooser_presence_changed_cb (chooser);
211         g_signal_connect_swapped (priv->idle, "notify",
212                                   G_CALLBACK (presence_chooser_presence_changed_cb),
213                                   chooser);
214 }
215
216 static void
217 presence_chooser_finalize (GObject *object)
218 {
219         EmpathyPresenceChooserPriv *priv;
220
221         priv = GET_PRIV (object);
222
223         if (priv->flash_timeout_id) {
224                 g_source_remove (priv->flash_timeout_id);
225         }
226
227         if (priv->scroll_timeout_id) {
228                 g_source_remove (priv->scroll_timeout_id);
229         }
230
231         g_signal_handlers_disconnect_by_func (priv->idle,
232                                               presence_chooser_presence_changed_cb,
233                                               object);
234         g_object_unref (priv->idle);
235
236         G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
237 }
238
239 GtkWidget *
240 empathy_presence_chooser_new (void)
241 {
242         GtkWidget *chooser;
243
244         chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
245
246         return chooser;
247 }
248
249 static void
250 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
251 {
252         EmpathyPresenceChooserPriv *priv;
253         McPresence                 state;
254         McPresence                 flash_state;
255         const gchar               *status;
256
257         priv = GET_PRIV (chooser);
258
259         state = empathy_idle_get_state (priv->idle);
260         status = empathy_idle_get_status (priv->idle);
261         flash_state = empathy_idle_get_flash_state (priv->idle);
262
263         presence_chooser_reset_scroll_timeout (chooser);
264         gtk_label_set_text (GTK_LABEL (priv->label), status);
265
266         if (flash_state != MC_PRESENCE_UNSET) {
267                 presence_chooser_flash_start (chooser, state, flash_state);
268         } else {
269                 presence_chooser_flash_stop (chooser, state);
270         }
271 }
272
273 static void
274 presence_chooser_reset_scroll_timeout (EmpathyPresenceChooser *chooser)
275 {
276         EmpathyPresenceChooserPriv *priv;
277
278         priv = GET_PRIV (chooser);
279
280         if (priv->scroll_timeout_id) {
281                 g_source_remove (priv->scroll_timeout_id);
282                 priv->scroll_timeout_id = 0;
283         }
284
285         g_free (priv->scroll_status);
286         priv->scroll_status = NULL;
287 }
288
289 static gboolean
290 presence_chooser_scroll_timeout_cb (EmpathyPresenceChooser *chooser)
291 {
292         EmpathyPresenceChooserPriv *priv;
293
294         priv = GET_PRIV (chooser);
295
296         priv->scroll_timeout_id = 0;
297
298         empathy_idle_set_presence (priv->idle,
299                                    priv->scroll_state,
300                                    priv->scroll_status);
301
302         g_free (priv->scroll_status);
303         priv->scroll_status = NULL;
304
305         return FALSE;
306 }
307
308 static gboolean
309 presence_chooser_scroll_event_cb (EmpathyPresenceChooser *chooser,
310                                   GdkEventScroll        *event,
311                                   gpointer               user_data)
312 {
313         EmpathyPresenceChooserPriv *priv;
314         GList                     *list, *l;
315         const gchar               *current_status;
316         StateAndStatus            *sas;
317         gboolean                   match;
318
319         priv = GET_PRIV (chooser);
320
321         switch (event->direction) {
322         case GDK_SCROLL_UP:
323                 break;
324         case GDK_SCROLL_DOWN:
325                 break;
326         default:
327                 return FALSE;
328         }
329
330         current_status = gtk_label_get_text (GTK_LABEL (priv->label));
331
332         /* Get the list of presets, which in this context means all the items
333          * without a trailing "...".
334          */
335         list = presence_chooser_get_presets (chooser);
336         sas = NULL;
337         match = FALSE;
338         for (l = list; l; l = l->next) {
339                 sas = l->data;
340
341                 if (sas->state == priv->last_state &&
342                     strcmp (sas->status, current_status) == 0) {
343                         sas = NULL;
344                         match = TRUE;
345                         if (event->direction == GDK_SCROLL_UP) {
346                                 if (l->prev) {
347                                         sas = l->prev->data;
348                                 }
349                         }
350                         else if (event->direction == GDK_SCROLL_DOWN) {
351                                 if (l->next) {
352                                         sas = l->next->data;
353                                 }
354                         }
355                         break;
356                 }
357
358                 sas = NULL;
359         }
360
361         if (sas) {
362                 presence_chooser_reset_scroll_timeout (chooser);
363
364                 priv->scroll_status = g_strdup (sas->status);
365                 priv->scroll_state = sas->state;
366
367                 priv->scroll_timeout_id =
368                         g_timeout_add_seconds (1,
369                                                (GSourceFunc) presence_chooser_scroll_timeout_cb,
370                                                chooser);
371
372                 presence_chooser_flash_stop (chooser, sas->state);
373                 gtk_label_set_text (GTK_LABEL (priv->label), sas->status);      
374         }
375         else if (!match) {
376                 const gchar *status;
377                 /* If we didn't get any match at all, it means the last state
378                  * was a custom one. Just switch to the first one.
379                  */
380                 status = empathy_presence_get_default_message (states[0]);
381
382                 presence_chooser_reset_scroll_timeout (chooser);
383                 empathy_idle_set_presence (priv->idle, states[0], status);
384         }
385
386         g_list_foreach (list, (GFunc) g_free, NULL);
387         g_list_free (list);
388
389         return TRUE;
390 }
391
392 static GList *
393 presence_chooser_get_presets (EmpathyPresenceChooser *chooser)
394 {
395         GList      *list = NULL;
396         guint       i;
397
398         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
399                 GList          *presets, *p;
400                 StateAndStatus *sas;
401                 const gchar    *status;
402
403                 status = empathy_presence_get_default_message (states[i]);
404                 sas = presence_chooser_state_and_status_new (states[i], status);
405                 list = g_list_prepend (list, sas);
406
407                 /* Go to next state if we don't want messages for that state */
408                 if (!states[i+1]) {
409                         continue;
410                 }
411
412                 presets = empathy_status_presets_get (states[i], 5);
413                 for (p = presets; p; p = p->next) {
414                         sas = presence_chooser_state_and_status_new (states[i], p->data);
415                         list = g_list_prepend (list, sas);
416                 }
417                 g_list_free (presets);
418         }
419         list = g_list_reverse (list);
420
421         return list;
422 }
423
424 static StateAndStatus *
425 presence_chooser_state_and_status_new (McPresence   state,
426                                        const gchar *status)
427 {
428         StateAndStatus *sas;
429
430         sas = g_new0 (StateAndStatus, 1);
431
432         sas->state = state;
433         sas->status = status;
434
435         return sas;
436 }
437
438 static gboolean
439 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
440 {
441         EmpathyPresenceChooserPriv *priv;
442         McPresence                 state;
443         static gboolean            on = FALSE;
444
445         priv = GET_PRIV (chooser);
446
447         if (on) {
448                 state = priv->flash_state_1;
449         } else {
450                 state = priv->flash_state_2;
451         }
452
453         gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
454                                       empathy_icon_name_for_presence (state),
455                                       GTK_ICON_SIZE_MENU);
456
457         on = !on;
458
459         return TRUE;
460 }
461
462 static void
463 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
464                               McPresence             state_1,
465                               McPresence             state_2)
466 {
467         EmpathyPresenceChooserPriv *priv;
468
469         g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
470
471         priv = GET_PRIV (chooser);
472
473         priv->flash_state_1 = state_1;
474         priv->flash_state_2 = state_2;
475
476         if (!priv->flash_timeout_id) {
477                 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
478                                                         (GSourceFunc) presence_chooser_flash_timeout_cb,
479                                                         chooser);
480         }
481 }
482
483 static void
484 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
485                              McPresence             state)
486 {
487         EmpathyPresenceChooserPriv *priv;
488
489         g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
490
491         priv = GET_PRIV (chooser);
492
493         if (priv->flash_timeout_id) {
494                 g_source_remove (priv->flash_timeout_id);
495                 priv->flash_timeout_id = 0;
496         }
497
498         gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
499                                       empathy_icon_name_for_presence (state),
500                                       GTK_ICON_SIZE_MENU);
501
502         priv->last_state = state;
503 }
504
505 static gboolean
506 presence_chooser_button_press_event_cb (GtkWidget      *chooser,
507                                         GdkEventButton *event,
508                                         gpointer        user_data)
509 {
510         if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
511                 return FALSE;
512         }
513
514         if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
515                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), TRUE);
516                         return TRUE;
517                 }
518
519         return FALSE;
520 }
521
522 static void
523 presence_chooser_toggled_cb (GtkWidget *chooser,
524                              gpointer   user_data)
525 {
526         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
527                 presence_chooser_menu_popup (EMPATHY_PRESENCE_CHOOSER (chooser));
528         } else {
529                 presence_chooser_menu_popdown (EMPATHY_PRESENCE_CHOOSER (chooser));
530         }
531 }
532
533 static void
534 presence_chooser_menu_popup (EmpathyPresenceChooser *chooser)
535 {
536         EmpathyPresenceChooserPriv *priv;
537         GtkWidget                 *menu;
538
539         priv = GET_PRIV (chooser);
540
541         if (priv->menu) {
542                 return;
543         }
544
545         menu = empathy_presence_chooser_create_menu ();
546
547         g_signal_connect_after (menu, "selection-done",
548                                 G_CALLBACK (presence_chooser_menu_selection_done_cb),
549                                 chooser);
550
551         g_signal_connect (menu, "destroy",
552                           G_CALLBACK (presence_chooser_menu_destroy_cb),
553                           chooser);
554
555         gtk_menu_attach_to_widget (GTK_MENU (menu),
556                                    GTK_WIDGET (chooser),
557                                    presence_chooser_menu_detach);
558
559         gtk_menu_popup (GTK_MENU (menu),
560                         NULL, NULL,
561                         (GtkMenuPositionFunc) presence_chooser_menu_align_func,
562                         chooser,
563                         1,
564                         gtk_get_current_event_time ());
565
566         priv->menu = menu;
567 }
568
569 static void
570 presence_chooser_menu_popdown (EmpathyPresenceChooser *chooser)
571 {
572         EmpathyPresenceChooserPriv *priv;
573
574         priv = GET_PRIV (chooser);
575
576         if (priv->menu) {
577                 gtk_widget_destroy (priv->menu);
578         }
579 }
580
581 static void
582 presence_chooser_menu_selection_done_cb (GtkMenuShell          *menushell,
583                                          EmpathyPresenceChooser *chooser)
584 {
585         gtk_widget_destroy (GTK_WIDGET (menushell));
586
587         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), FALSE);
588 }
589
590 static void
591 presence_chooser_menu_destroy_cb (GtkWidget             *menu,
592                                   EmpathyPresenceChooser *chooser)
593 {
594         EmpathyPresenceChooserPriv *priv;
595
596         priv = GET_PRIV (chooser);
597
598         priv->menu = NULL;
599 }
600
601 static void
602 presence_chooser_menu_detach (GtkWidget *attach_widget,
603                               GtkMenu   *menu)
604 {
605         /* We don't need to do anything, but attaching the menu means
606          * we don't own the ref count and it is cleaned up properly.
607          */
608 }
609
610 static void
611 presence_chooser_menu_align_func (GtkMenu   *menu,
612                                   gint      *x,
613                                   gint      *y,
614                                   gboolean  *push_in,
615                                   GtkWidget *widget)
616 {
617         GtkRequisition  req;
618         GdkScreen      *screen;
619         gint            screen_height;
620
621         gtk_widget_size_request (GTK_WIDGET (menu), &req);
622
623         gdk_window_get_origin (widget->window, x, y);
624
625         *x += widget->allocation.x + 1;
626         *y += widget->allocation.y;
627
628         screen = gtk_widget_get_screen (GTK_WIDGET (menu));
629         screen_height = gdk_screen_get_height (screen);
630
631         if (req.height > screen_height) {
632                 /* Too big for screen height anyway. */
633                 *y = 0;
634                 return;
635         }
636
637         if ((*y + req.height + widget->allocation.height) > screen_height) {
638                 /* Can't put it below the button. */
639                 *y -= req.height;
640                 *y += 1;
641         } else {
642                 /* Put menu below button. */
643                 *y += widget->allocation.height;
644                 *y -= 1;
645         }
646
647         *push_in = FALSE;
648 }
649
650 GtkWidget *
651 empathy_presence_chooser_create_menu (void)
652 {
653         const gchar *status;
654         GtkWidget   *menu;
655         GtkWidget   *item;
656         GtkWidget   *image;
657         guint        i;
658
659         menu = gtk_menu_new ();
660
661         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
662                 GList       *list, *l;
663
664                 status = empathy_presence_get_default_message (states[i]);
665                 presence_chooser_menu_add_item (menu,
666                                                 status,
667                                                 states[i]);
668
669                 if (states[i+1]) {
670                         /* Set custom messages if wanted */
671                         list = empathy_status_presets_get (states[i], 5);
672                         for (l = list; l; l = l->next) {
673                                 presence_chooser_menu_add_item (menu,
674                                                                 l->data,
675                                                                 states[i]);
676                         }
677                         g_list_free (list);
678                 }
679
680         }
681
682         /* Separator. */
683         item = gtk_menu_item_new ();
684         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
685         gtk_widget_show (item);
686
687         /* Custom messages */
688         item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
689         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
690         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
691         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
692         gtk_widget_show (image);
693         gtk_widget_show (item);
694
695         g_signal_connect (item,
696                           "activate",
697                           G_CALLBACK (presence_chooser_custom_activate_cb),
698                           NULL);
699
700         return menu;
701 }
702
703 static void
704 presence_chooser_menu_add_item (GtkWidget   *menu,
705                                 const gchar *str,
706                                 McPresence   state)
707 {
708         GtkWidget   *item;
709         GtkWidget   *image;
710         const gchar *icon_name;
711
712         item = gtk_image_menu_item_new_with_label (str);
713         icon_name = empathy_icon_name_for_presence (state);
714
715         g_signal_connect (item, "activate",
716                           G_CALLBACK (presence_chooser_noncustom_activate_cb),
717                           NULL);
718
719         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
720         gtk_widget_show (image);
721
722         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
723         gtk_widget_show (item);
724
725         g_object_set_data_full (G_OBJECT (item),
726                                 "status", g_strdup (str),
727                                 (GDestroyNotify) g_free);
728
729         g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
730
731         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
732 }
733
734 static void
735 presence_chooser_noncustom_activate_cb (GtkWidget *item,
736                                         gpointer   user_data)
737 {
738         McPresence   state;
739         const gchar *status;
740
741         status = g_object_get_data (G_OBJECT (item), "status");
742         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
743
744         presence_chooser_set_state (state, status);
745 }
746
747 static void
748 presence_chooser_set_state (McPresence   state,
749                             const gchar *status)
750 {
751         EmpathyIdle *idle;
752
753         idle = empathy_idle_new ();
754         empathy_idle_set_presence (idle, state, status);
755         g_object_unref (idle);
756 }
757
758 static void
759 presence_chooser_custom_activate_cb (GtkWidget *item,
760                                      gpointer   user_data)
761 {
762         presence_chooser_dialog_show ();
763 }
764
765 static McPresence
766 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
767 {
768         GtkTreeModel *model;
769         GtkTreeIter   iter;
770         McPresence    presence = LAST_MC_PRESENCE;
771
772         model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
773         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
774                 gtk_tree_model_get (model, &iter,
775                                     COL_PRESENCE, &presence,
776                                     -1);
777         }
778
779         return presence;
780 }
781
782 static void
783 presence_chooser_dialog_status_changed_cb (GtkWidget           *widget,
784                                            CustomMessageDialog *dialog)
785 {
786         GtkListStore *store;
787         GtkTreeIter   iter;
788         McPresence    presence = LAST_MC_PRESENCE;
789         GList        *messages, *l;
790
791         presence = presence_chooser_dialog_get_selected (dialog);
792
793         store = gtk_list_store_new (1, G_TYPE_STRING);
794         messages = empathy_status_presets_get (presence, -1);
795         for (l = messages; l; l = l->next) {
796                 gtk_list_store_append (store, &iter);
797                 gtk_list_store_set (store, &iter, 0, l->data, -1);
798         }
799
800         gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
801                             messages ? messages->data : "");
802
803         g_list_free (messages);
804
805         gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
806                                  GTK_TREE_MODEL (store));
807
808         g_object_unref (store);
809 }
810
811 static void
812 presence_chooser_dialog_message_changed_cb (GtkWidget           *widget,
813                                             CustomMessageDialog *dialog)
814 {
815         McPresence   presence;
816         GList       *messages, *l;
817         const gchar *text;
818         gboolean     found = FALSE;
819
820         presence = presence_chooser_dialog_get_selected (dialog);
821         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
822
823         messages = empathy_status_presets_get (presence, -1);
824         for (l = messages; l; l = l->next) {
825                 if (!tp_strdiff (text, l->data)) {
826                         found = TRUE;
827                         break;
828                 }
829         }
830         g_list_free (messages);
831
832         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
833                                       found);
834 }
835
836 static void
837 presence_chooser_dialog_save_toggled_cb (GtkWidget           *widget,
838                                          CustomMessageDialog *dialog)
839 {
840         gboolean     active;
841         McPresence   state;
842         const gchar *text;
843
844         active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
845         state = presence_chooser_dialog_get_selected (dialog);
846         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
847
848         if (active) {
849                 empathy_status_presets_set_last (state, text);
850         } else {
851                 empathy_status_presets_remove (state, text);
852         }
853 }
854
855 static void
856 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
857 {
858         GtkListStore    *store;
859         GtkCellRenderer *renderer;
860         GtkTreeIter      iter;
861         guint            i;
862
863         store = gtk_list_store_new (COL_COUNT,
864                                     G_TYPE_STRING,     /* Icon name */
865                                     G_TYPE_STRING,     /* Label     */
866                                     MC_TYPE_PRESENCE); /* Presence   */
867         gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
868                                  GTK_TREE_MODEL (store));
869
870         renderer = gtk_cell_renderer_pixbuf_new ();
871         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
872         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
873                                         "icon-name", COL_ICON,
874                                         NULL);
875         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
876
877         renderer = gtk_cell_renderer_text_new ();
878         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
879         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
880                                         "text", COL_LABEL,
881                                         NULL);
882
883         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
884                 if (!states[i+1]) {
885                         continue;
886                 }
887
888                 gtk_list_store_append (store, &iter);
889                 gtk_list_store_set (store, &iter,
890                                     COL_ICON, empathy_icon_name_for_presence (states[i]),
891                                     COL_LABEL, empathy_presence_get_default_message (states[i]),
892                                     COL_PRESENCE, states[i],
893                                     -1);
894         }
895
896         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
897 }
898
899 static void
900 presence_chooser_dialog_response_cb (GtkWidget           *widget,
901                                      gint                 response,
902                                      CustomMessageDialog *dialog)
903 {
904         if (response == GTK_RESPONSE_APPLY) {
905                 McPresence   state;
906                 const gchar *text;
907
908                 state = presence_chooser_dialog_get_selected (dialog);
909                 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
910
911                 presence_chooser_set_state (state, text);
912         }
913
914         gtk_widget_destroy (widget);
915 }
916
917 static void
918 presence_chooser_dialog_destroy_cb (GtkWidget           *widget,
919                                     CustomMessageDialog *dialog)
920 {
921
922         g_free (dialog);
923         message_dialog = NULL;
924 }
925
926 static void
927 presence_chooser_dialog_show (void)
928 {
929         GladeXML *glade;
930         gchar    *filename;
931
932         if (message_dialog) {
933                 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
934                 return;
935         }
936
937         message_dialog = g_new0 (CustomMessageDialog, 1);
938
939         filename = empathy_file_lookup ("empathy-presence-chooser.glade",
940                                         "libempathy-gtk");
941         glade = empathy_glade_get_file (filename,
942                                        "custom_message_dialog",
943                                        NULL,
944                                        "custom_message_dialog", &message_dialog->dialog,
945                                        "checkbutton_save", &message_dialog->checkbutton_save,
946                                        "comboboxentry_message", &message_dialog->comboboxentry_message,
947                                        "combobox_status", &message_dialog->combobox_status,
948                                        NULL);
949         g_free (filename);
950
951         empathy_glade_connect (glade,
952                                message_dialog,
953                                "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
954                                "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
955                                "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
956                                "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
957                                NULL);
958
959         g_object_unref (glade);
960
961         /* Setup the message combobox */
962         message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
963         gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
964         gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
965         g_signal_connect (message_dialog->entry_message, "changed",
966                           G_CALLBACK (presence_chooser_dialog_message_changed_cb),
967                           message_dialog);
968
969         presence_chooser_dialog_setup (message_dialog);
970
971         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
972
973         /* FIXME: Set transian for a window ? */
974
975         gtk_widget_show_all (message_dialog->dialog);
976 }
977