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