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