]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-presence-chooser.c
43aad33c526e845a6de6f2fbe8aa1b0de2bb4c09
[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-lib.h>
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33 #include <gdk/gdkkeysyms.h>
34
35 #include <telepathy-glib/util.h>
36 #include <libmissioncontrol/mc-enum-types.h>
37
38 #include <libempathy/empathy-idle.h>
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-status-presets.h>
41
42 // FIXME - what's the correct debug flag?
43 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
44 #include <libempathy/empathy-debug.h>
45
46 #include "empathy-ui-utils.h"
47 #include "empathy-images.h"
48 #include "empathy-presence-chooser.h"
49
50 /* Flashing delay for icons (milliseconds). */
51 #define FLASH_TIMEOUT 500
52
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
54 typedef struct {
55         EmpathyIdle *idle;
56
57         gboolean     editing_status;
58         int          block_set_editing;
59         int          block_changed;
60
61         McPresence   state;
62
63         McPresence   flash_state_1;
64         McPresence   flash_state_2;
65         guint        flash_timeout_id;
66 } EmpathyPresenceChooserPriv;
67
68 typedef struct {
69         GtkWidget    *dialog;
70         GtkWidget    *checkbutton_save;
71         GtkWidget    *comboboxentry_message;
72         GtkWidget    *entry_message;
73         GtkWidget    *combobox_status;
74         GtkTreeModel *model_status;
75 } CustomMessageDialog;
76
77 enum {
78         COL_ICON,
79         COL_LABEL,
80         COL_PRESENCE,
81         COL_COUNT
82 };
83
84 static CustomMessageDialog *message_dialog = NULL;
85 /* States to be listed in the menu.
86  * Each state has a boolean telling if it can have custom message */
87 static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
88                          MC_PRESENCE_DO_NOT_DISTURB, TRUE,
89                          MC_PRESENCE_AWAY, TRUE,
90                          MC_PRESENCE_HIDDEN, FALSE,
91                          MC_PRESENCE_OFFLINE, FALSE};
92
93 static void            presence_chooser_finalize               (GObject                    *object);
94 static void            presence_chooser_presence_changed_cb    (EmpathyPresenceChooser      *chooser);
95 static gboolean        presence_chooser_flash_timeout_cb       (EmpathyPresenceChooser      *chooser);
96 static void            presence_chooser_flash_start            (EmpathyPresenceChooser      *chooser,
97                                                                 McPresence                  state_1,
98                                                                 McPresence                  state_2);
99 static void            presence_chooser_flash_stop             (EmpathyPresenceChooser      *chooser,
100                                                                 McPresence                  state);
101 static void            presence_chooser_menu_add_item          (GtkWidget                  *menu,
102                                                                 const gchar                *str,
103                                                                 McPresence                  state);
104 static void            presence_chooser_noncustom_activate_cb  (GtkWidget                  *item,
105                                                                 gpointer                    user_data);
106 static void            presence_chooser_set_state              (McPresence                  state,
107                                                                 const gchar                *status);
108 static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
109                                                                 gpointer                    user_data);
110 static void            presence_chooser_dialog_show            (void);
111
112 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
113
114 static void
115 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
116 {
117         GObjectClass *object_class = G_OBJECT_CLASS (klass);
118
119         object_class->finalize = presence_chooser_finalize;
120
121         g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
122 }
123
124 enum
125 {
126         COL_STATE_ICON_NAME,
127         COL_STATE,
128         COL_STATUS_TEXT,
129         COL_DISPLAY_MARKUP,
130         COL_TYPE,
131         N_COLUMNS
132 };
133
134 enum
135 {
136         ENTRY_TYPE_BUILTIN,
137         ENTRY_TYPE_SAVED,
138         ENTRY_TYPE_CUSTOM,
139 };
140
141 static GtkTreeModel *
142 create_model (void)
143 {
144         GtkListStore *store = gtk_list_store_new (N_COLUMNS,
145                         G_TYPE_STRING,          /* COL_STATE_ICON_NAME */
146                         MC_TYPE_PRESENCE,       /* COL_STATE */
147                         G_TYPE_STRING,          /* COL_STATUS_TEXT */
148                         G_TYPE_STRING,          /* COL_DISPLAY_MARKUP */
149                         G_TYPE_INT);            /* COL_TYPE */
150         
151         GtkTreeIter iter;
152         
153         int i;
154         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
155                 GList       *list, *l;
156
157                 const char *status = empathy_presence_get_default_message (states[i]);
158                 const char *icon_name = empathy_icon_name_for_presence (states[i]);
159
160                 gtk_list_store_append (store, &iter);
161                 gtk_list_store_set (store, &iter,
162                                 COL_STATE_ICON_NAME, icon_name,
163                                 COL_STATE, states[i],
164                                 COL_STATUS_TEXT, status,
165                                 COL_DISPLAY_MARKUP, status,
166                                 COL_TYPE, ENTRY_TYPE_BUILTIN,
167                                 -1);
168
169                 if (states[i+1]) {
170                         /* Set custom messages if wanted */
171                         list = empathy_status_presets_get (states[i], 5);
172                         for (l = list; l; l = l->next) {
173                                 gtk_list_store_append (store, &iter);
174                                 gtk_list_store_set (store, &iter,
175                                                 COL_STATE_ICON_NAME, icon_name,
176                                                 COL_STATE, states[i],
177                                                 COL_STATUS_TEXT, l->data,
178                                                 COL_DISPLAY_MARKUP, l->data,
179                                                 COL_TYPE, ENTRY_TYPE_SAVED,
180                                                 -1);
181                         }
182                         g_list_free (list);
183                 
184                         gtk_list_store_append (store, &iter);
185                         gtk_list_store_set (store, &iter,
186                                         COL_STATE_ICON_NAME, icon_name,
187                                         COL_STATE, states[i],
188                                         COL_STATUS_TEXT, "",
189                                         COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
190                                         COL_TYPE, ENTRY_TYPE_CUSTOM,
191                                         -1);
192                 }
193
194         }
195
196         return GTK_TREE_MODEL (store);
197 }
198
199 static void
200 popup_shown_cb (GObject *self, GParamSpec *pspec, gpointer user_data)
201 {
202         gboolean shown;
203         g_object_get (self, "popup-shown", &shown, NULL);
204
205         if (!shown) return;
206
207         GtkTreeModel *model = create_model ();
208
209         gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (model));
210         
211         g_object_unref (model);
212 }
213
214 static void
215 set_status_editing (EmpathyPresenceChooser *self, gboolean editing)
216 {
217         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
218         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
219
220         if (priv->block_set_editing) return;
221
222         if (editing)
223         {
224                 priv->editing_status = TRUE;
225                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
226                                 GTK_ENTRY_ICON_SECONDARY,
227                                 GTK_STOCK_OK);
228                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
229                                 GTK_ENTRY_ICON_SECONDARY,
230                                 _("Set status"));
231                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
232                                 GTK_ENTRY_ICON_PRIMARY,
233                                 FALSE);
234         }
235         else
236         {
237                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
238                                 GTK_ENTRY_ICON_SECONDARY,
239                                 NULL);
240                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
241                                 GTK_ENTRY_ICON_SECONDARY,
242                                 NULL);
243                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
244                                 GTK_ENTRY_ICON_PRIMARY,
245                                 TRUE);
246
247                 // FIXME - move the focus somewhere
248
249                 priv->editing_status = FALSE;
250         }
251 }
252
253 static void
254 mc_set_custom_state (EmpathyPresenceChooser *self)
255 {
256         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
257         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
258
259         /* update the status with MC */
260         const char *status = gtk_entry_get_text (GTK_ENTRY (entry));
261         DEBUG ("Sending state to MC-> %s (%s)\n",
262                         g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
263                                 priv->state)->value_name,
264                         status);
265         empathy_idle_set_presence (priv->idle, priv->state, status);
266 }
267
268 static void
269 ui_set_custom_state (EmpathyPresenceChooser *self,
270                            McPresence state,
271                            const char *status)
272 {
273         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
274         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
275         const char *icon_name;
276
277         priv->block_set_editing++;
278         priv->block_changed++;
279
280         icon_name = empathy_icon_name_for_presence (state);
281         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
282                         GTK_ENTRY_ICON_PRIMARY,
283                         icon_name);
284         gtk_entry_set_text (GTK_ENTRY (entry), status);
285
286         priv->block_changed--;
287         priv->block_set_editing--;
288 }
289
290 static void
291 entry_icon_release_cb (EmpathyPresenceChooser   *self,
292                        GtkEntryIconPosition      icon_pos,
293                        GdkEvent         *event,
294                        GtkEntry         *entry)
295 {
296         set_status_editing (self, FALSE);
297         mc_set_custom_state (self);
298 }
299
300 static void
301 entry_activate_cb (EmpathyPresenceChooser       *self,
302                    GtkEntry                     *entry)
303 {
304         set_status_editing (self, FALSE);
305         mc_set_custom_state (self);
306 }
307
308 static gboolean
309 entry_key_press_event_cb (EmpathyPresenceChooser        *self,
310                           GdkEventKey                   *event,
311                           GtkWidget                     *entry)
312 {
313         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
314
315         if (priv->editing_status && event->keyval == GDK_Escape)
316         {
317                 /* the user pressed Escape, undo the editing */
318                 set_status_editing (self, FALSE);
319                 presence_chooser_presence_changed_cb (self);
320
321                 return TRUE;
322         }
323
324         return FALSE; /* send this event elsewhere */
325 }
326
327 static void
328 changed_cb (GtkComboBox *self, gpointer user_data)
329 {
330         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
331
332         if (priv->block_changed) return;
333
334         GtkTreeIter iter;
335         char *icon_name;
336         int type = -1;
337
338         GtkTreeModel *model = gtk_combo_box_get_model (self);
339         if (!gtk_combo_box_get_active_iter (self, &iter))
340         {
341                 /* the combo is being edited to a custom entry */
342                 if (!priv->editing_status)
343                 {
344                         set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
345                 }
346                 return;
347         }
348
349         gtk_tree_model_get (model, &iter,
350                         COL_STATE_ICON_NAME, &icon_name,
351                         COL_STATE, &priv->state,
352                         COL_TYPE, &type,
353                         -1);
354
355         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
356         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
357                         GTK_ENTRY_ICON_PRIMARY,
358                         icon_name);
359
360         if (type == ENTRY_TYPE_CUSTOM)
361         {
362                 /* grab the focus */
363                 gtk_widget_grab_focus (entry);
364                 set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
365         }
366         else
367         {
368                 char *status;
369                 /* just in case we were setting a new status when
370                  * things were changed */
371                 set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), FALSE);
372
373                 gtk_tree_model_get (model, &iter,
374                                 COL_STATUS_TEXT, &status,
375                                 -1);
376
377                 empathy_idle_set_presence (priv->idle, priv->state, status);
378
379                 g_free (status);
380         }
381
382         g_free (icon_name);
383 }
384
385 static void
386 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
387 {
388         EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
389                 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
390
391         chooser->priv = priv;
392         
393         GtkTreeModel *model = create_model ();
394
395         gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
396         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser), COL_STATUS_TEXT);
397         
398         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
399         gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
400                         GTK_ENTRY_ICON_PRIMARY, FALSE);
401         g_signal_connect_object (entry, "icon-release",
402                         G_CALLBACK (entry_icon_release_cb), chooser,
403                         G_CONNECT_SWAPPED);
404         g_signal_connect_object (entry, "activate",
405                         G_CALLBACK (entry_activate_cb), chooser,
406                         G_CONNECT_SWAPPED);
407         g_signal_connect_object (entry, "key-press-event",
408                         G_CALLBACK (entry_key_press_event_cb), chooser,
409                         G_CONNECT_SWAPPED);
410         // FIXME - should this also happen when the user presses TAB ?
411
412         GtkCellRenderer *renderer;
413         gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
414
415         renderer = gtk_cell_renderer_pixbuf_new ();
416         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
417         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
418                         "icon-name", COL_STATE_ICON_NAME,
419                         NULL);
420         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
421
422         renderer = gtk_cell_renderer_text_new ();
423         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
424         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
425                         "markup", COL_DISPLAY_MARKUP,
426                         NULL);
427
428         g_object_unref (model);
429
430         g_signal_connect (chooser, "notify::popup-shown",
431                         G_CALLBACK (popup_shown_cb), NULL);
432         g_signal_connect (chooser, "changed",
433                         G_CALLBACK (changed_cb), NULL);
434
435         priv->idle = empathy_idle_dup_singleton ();
436         presence_chooser_presence_changed_cb (chooser);
437         g_signal_connect_swapped (priv->idle, "notify",
438                                   G_CALLBACK (presence_chooser_presence_changed_cb),
439                                   chooser);
440
441         g_object_set (chooser,
442                         // FIXME: this string sucks
443                         "tooltip-text", _("Set your presence and current status"),
444                         NULL);
445 }
446
447 static void
448 presence_chooser_finalize (GObject *object)
449 {
450         EmpathyPresenceChooserPriv *priv;
451
452         priv = GET_PRIV (object);
453
454         if (priv->flash_timeout_id) {
455                 g_source_remove (priv->flash_timeout_id);
456         }
457
458         g_signal_handlers_disconnect_by_func (priv->idle,
459                                               presence_chooser_presence_changed_cb,
460                                               object);
461         g_object_unref (priv->idle);
462
463         G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
464 }
465
466 GtkWidget *
467 empathy_presence_chooser_new (void)
468 {
469         GtkWidget *chooser;
470
471         chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
472
473         return chooser;
474 }
475
476 static void
477 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
478 {
479         EmpathyPresenceChooserPriv *priv;
480         McPresence                 state;
481         McPresence                 flash_state;
482         const gchar               *status;
483
484         priv = GET_PRIV (chooser);
485
486         priv->state = state = empathy_idle_get_state (priv->idle);
487         status = empathy_idle_get_status (priv->idle);
488         flash_state = empathy_idle_get_flash_state (priv->idle);
489
490         /* look through the model and attempt to find a matching state */
491         GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
492         GtkTreeIter iter;
493         gboolean valid, match_state = FALSE, match = FALSE;
494         for (valid = gtk_tree_model_get_iter_first (model, &iter);
495              valid;
496              valid = gtk_tree_model_iter_next (model, &iter))
497         {
498                 int m_type;
499                 McPresence m_state;
500                 char *m_status;
501
502                 gtk_tree_model_get (model, &iter,
503                                 COL_STATE, &m_state,
504                                 COL_TYPE, &m_type,
505                                 -1);
506
507                 if (m_type == ENTRY_TYPE_CUSTOM)
508                 {
509                         continue;
510                 }
511                 else if (!match_state && state == m_state)
512                 {
513                         /* we are now in the section that can contain our
514                          * match */
515                         match_state = TRUE;
516                 }
517                 else if (match_state && state != m_state)
518                 {
519                         /* we have passed the section that can contain our
520                          * match */
521                         break;
522                 }
523
524                 gtk_tree_model_get (model, &iter,
525                                 COL_STATUS_TEXT, &m_status,
526                                 -1);
527
528                 match = !strcmp (status, m_status);
529
530                 g_free (m_status);
531
532                 if (match) break;
533
534         }
535
536         if (match)
537         {
538                 priv->block_changed++;
539                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
540                 priv->block_changed--;
541         }
542         else
543         {
544                 // FIXME - do we insert the match into the menu?
545                 ui_set_custom_state (chooser, state, status);
546         }
547
548         if (flash_state != MC_PRESENCE_UNSET) {
549                 presence_chooser_flash_start (chooser, state, flash_state);
550         } else {
551                 presence_chooser_flash_stop (chooser, state);
552         }
553 }
554
555 static gboolean
556 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
557 {
558         EmpathyPresenceChooserPriv *priv;
559         McPresence                 state;
560         static gboolean            on = FALSE;
561
562         priv = GET_PRIV (chooser);
563
564         if (on) {
565                 state = priv->flash_state_1;
566         } else {
567                 state = priv->flash_state_2;
568         }
569
570         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
571         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
572                         GTK_ENTRY_ICON_PRIMARY,
573                         empathy_icon_name_for_presence (state));
574
575         on = !on;
576
577         return TRUE;
578 }
579
580 static void
581 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
582                               McPresence             state_1,
583                               McPresence             state_2)
584 {
585         EmpathyPresenceChooserPriv *priv;
586
587         g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
588
589         priv = GET_PRIV (chooser);
590
591         priv->flash_state_1 = state_1;
592         priv->flash_state_2 = state_2;
593
594         if (!priv->flash_timeout_id) {
595                 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
596                                                         (GSourceFunc) presence_chooser_flash_timeout_cb,
597                                                         chooser);
598         }
599 }
600
601 static void
602 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
603                              McPresence             state)
604 {
605         EmpathyPresenceChooserPriv *priv;
606
607         g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
608
609         priv = GET_PRIV (chooser);
610
611         if (priv->flash_timeout_id) {
612                 g_source_remove (priv->flash_timeout_id);
613                 priv->flash_timeout_id = 0;
614         }
615         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
616         
617         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
618                         GTK_ENTRY_ICON_PRIMARY,
619                         empathy_icon_name_for_presence (state));
620
621         // FIXME - what does this do?
622         // priv->last_state = state;
623 }
624
625 GtkWidget *
626 empathy_presence_chooser_create_menu (void)
627 {
628         const gchar *status;
629         GtkWidget   *menu;
630         GtkWidget   *item;
631         GtkWidget   *image;
632         guint        i;
633
634         menu = gtk_menu_new ();
635
636         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
637                 GList       *list, *l;
638
639                 status = empathy_presence_get_default_message (states[i]);
640                 presence_chooser_menu_add_item (menu,
641                                                 status,
642                                                 states[i]);
643
644                 if (states[i+1]) {
645                         /* Set custom messages if wanted */
646                         list = empathy_status_presets_get (states[i], 5);
647                         for (l = list; l; l = l->next) {
648                                 presence_chooser_menu_add_item (menu,
649                                                                 l->data,
650                                                                 states[i]);
651                         }
652                         g_list_free (list);
653                 }
654
655         }
656
657         /* Separator. */
658         item = gtk_menu_item_new ();
659         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
660         gtk_widget_show (item);
661
662         /* Custom messages */
663         item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
664         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
665         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
666         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
667         gtk_widget_show (image);
668         gtk_widget_show (item);
669
670         g_signal_connect (item,
671                           "activate",
672                           G_CALLBACK (presence_chooser_custom_activate_cb),
673                           NULL);
674
675         return menu;
676 }
677
678 static void
679 presence_chooser_menu_add_item (GtkWidget   *menu,
680                                 const gchar *str,
681                                 McPresence   state)
682 {
683         GtkWidget   *item;
684         GtkWidget   *image;
685         const gchar *icon_name;
686
687         item = gtk_image_menu_item_new_with_label (str);
688         icon_name = empathy_icon_name_for_presence (state);
689
690         g_signal_connect (item, "activate",
691                           G_CALLBACK (presence_chooser_noncustom_activate_cb),
692                           NULL);
693
694         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
695         gtk_widget_show (image);
696
697         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
698         gtk_widget_show (item);
699
700         g_object_set_data_full (G_OBJECT (item),
701                                 "status", g_strdup (str),
702                                 (GDestroyNotify) g_free);
703
704         g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
705
706         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
707 }
708
709 static void
710 presence_chooser_noncustom_activate_cb (GtkWidget *item,
711                                         gpointer   user_data)
712 {
713         McPresence   state;
714         const gchar *status;
715
716         status = g_object_get_data (G_OBJECT (item), "status");
717         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
718
719         presence_chooser_set_state (state, status);
720 }
721
722 static void
723 presence_chooser_set_state (McPresence   state,
724                             const gchar *status)
725 {
726         EmpathyIdle *idle;
727
728         idle = empathy_idle_dup_singleton ();
729         empathy_idle_set_presence (idle, state, status);
730         g_object_unref (idle);
731 }
732
733 static void
734 presence_chooser_custom_activate_cb (GtkWidget *item,
735                                      gpointer   user_data)
736 {
737         presence_chooser_dialog_show ();
738 }
739
740 static McPresence
741 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
742 {
743         GtkTreeModel *model;
744         GtkTreeIter   iter;
745         McPresence    presence = LAST_MC_PRESENCE;
746
747         model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
748         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
749                 gtk_tree_model_get (model, &iter,
750                                     COL_PRESENCE, &presence,
751                                     -1);
752         }
753
754         return presence;
755 }
756
757 static void
758 presence_chooser_dialog_status_changed_cb (GtkWidget           *widget,
759                                            CustomMessageDialog *dialog)
760 {
761         GtkListStore *store;
762         GtkTreeIter   iter;
763         McPresence    presence = LAST_MC_PRESENCE;
764         GList        *messages, *l;
765
766         presence = presence_chooser_dialog_get_selected (dialog);
767
768         store = gtk_list_store_new (1, G_TYPE_STRING);
769         messages = empathy_status_presets_get (presence, -1);
770         for (l = messages; l; l = l->next) {
771                 gtk_list_store_append (store, &iter);
772                 gtk_list_store_set (store, &iter, 0, l->data, -1);
773         }
774
775         gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
776                             messages ? messages->data : "");
777
778         g_list_free (messages);
779
780         gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
781                                  GTK_TREE_MODEL (store));
782
783         g_object_unref (store);
784 }
785
786 static void
787 presence_chooser_dialog_message_changed_cb (GtkWidget           *widget,
788                                             CustomMessageDialog *dialog)
789 {
790         McPresence   presence;
791         GList       *messages, *l;
792         const gchar *text;
793         gboolean     found = FALSE;
794
795         presence = presence_chooser_dialog_get_selected (dialog);
796         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
797
798         messages = empathy_status_presets_get (presence, -1);
799         for (l = messages; l; l = l->next) {
800                 if (!tp_strdiff (text, l->data)) {
801                         found = TRUE;
802                         break;
803                 }
804         }
805         g_list_free (messages);
806
807         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
808                                       found);
809 }
810
811 static void
812 presence_chooser_dialog_save_toggled_cb (GtkWidget           *widget,
813                                          CustomMessageDialog *dialog)
814 {
815         gboolean     active;
816         McPresence   state;
817         const gchar *text;
818
819         active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
820         state = presence_chooser_dialog_get_selected (dialog);
821         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
822
823         if (active) {
824                 empathy_status_presets_set_last (state, text);
825         } else {
826                 empathy_status_presets_remove (state, text);
827         }
828 }
829
830 static void
831 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
832 {
833         GtkListStore    *store;
834         GtkCellRenderer *renderer;
835         GtkTreeIter      iter;
836         guint            i;
837
838         store = gtk_list_store_new (COL_COUNT,
839                                     G_TYPE_STRING,     /* Icon name */
840                                     G_TYPE_STRING,     /* Label     */
841                                     MC_TYPE_PRESENCE); /* Presence   */
842         gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
843                                  GTK_TREE_MODEL (store));
844
845         renderer = gtk_cell_renderer_pixbuf_new ();
846         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
847         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
848                                         "icon-name", COL_ICON,
849                                         NULL);
850         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
851
852         renderer = gtk_cell_renderer_text_new ();
853         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
854         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
855                                         "text", COL_LABEL,
856                                         NULL);
857
858         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
859                 if (!states[i+1]) {
860                         continue;
861                 }
862
863                 gtk_list_store_append (store, &iter);
864                 gtk_list_store_set (store, &iter,
865                                     COL_ICON, empathy_icon_name_for_presence (states[i]),
866                                     COL_LABEL, empathy_presence_get_default_message (states[i]),
867                                     COL_PRESENCE, states[i],
868                                     -1);
869         }
870
871         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
872 }
873
874 static void
875 presence_chooser_dialog_response_cb (GtkWidget           *widget,
876                                      gint                 response,
877                                      CustomMessageDialog *dialog)
878 {
879         if (response == GTK_RESPONSE_APPLY) {
880                 McPresence   state;
881                 const gchar *text;
882
883                 state = presence_chooser_dialog_get_selected (dialog);
884                 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
885
886                 presence_chooser_set_state (state, text);
887         }
888
889         gtk_widget_destroy (widget);
890 }
891
892 static void
893 presence_chooser_dialog_destroy_cb (GtkWidget           *widget,
894                                     CustomMessageDialog *dialog)
895 {
896
897         g_free (dialog);
898         message_dialog = NULL;
899 }
900
901 static void
902 presence_chooser_dialog_show (void)
903 {
904         GladeXML *glade;
905         gchar    *filename;
906
907         if (message_dialog) {
908                 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
909                 return;
910         }
911
912         message_dialog = g_new0 (CustomMessageDialog, 1);
913
914         filename = empathy_file_lookup ("empathy-presence-chooser.glade",
915                                         "libempathy-gtk");
916         glade = empathy_glade_get_file (filename,
917                                        "custom_message_dialog",
918                                        NULL,
919                                        "custom_message_dialog", &message_dialog->dialog,
920                                        "checkbutton_save", &message_dialog->checkbutton_save,
921                                        "comboboxentry_message", &message_dialog->comboboxentry_message,
922                                        "combobox_status", &message_dialog->combobox_status,
923                                        NULL);
924         g_free (filename);
925
926         empathy_glade_connect (glade,
927                                message_dialog,
928                                "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
929                                "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
930                                "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
931                                "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
932                                NULL);
933
934         g_object_unref (glade);
935
936         /* Setup the message combobox */
937         message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
938         gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
939         gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
940         g_signal_connect (message_dialog->entry_message, "changed",
941                           G_CALLBACK (presence_chooser_dialog_message_changed_cb),
942                           message_dialog);
943
944         presence_chooser_dialog_setup (message_dialog);
945
946         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
947
948         /* FIXME: Set transian for a window ? */
949
950         gtk_widget_show_all (message_dialog->dialog);
951 }
952