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