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