]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-presence-chooser.c
9dea442f2c147e74a4abaa1180e8c4fb0b436643
[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  * Copyright (C) 2009 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Richard Hult <richard@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  *          Davyd Madeley <davyd.madeley@collabora.co.uk>
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include <glib/gi18n-lib.h>
33 #include <gtk/gtk.h>
34 #include <glade/glade.h>
35 #include <gdk/gdkkeysyms.h>
36
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mc-enum-types.h>
39
40 #include <libempathy/empathy-idle.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-status-presets.h>
43
44 // FIXME - what's the correct debug flag?
45 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
46 #include <libempathy/empathy-debug.h>
47
48 #include "empathy-ui-utils.h"
49 #include "empathy-images.h"
50 #include "empathy-presence-chooser.h"
51
52 /* Flashing delay for icons (milliseconds). */
53 #define FLASH_TIMEOUT 500
54
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
56 typedef struct {
57         EmpathyIdle *idle;
58
59         gboolean     editing_status;
60         int          block_set_editing;
61         int          block_changed;
62         guint        focus_out_idle_source;
63
64         McPresence   state;
65         int          previous_type;
66
67         McPresence   flash_state_1;
68         McPresence   flash_state_2;
69         guint        flash_timeout_id;
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 static CustomMessageDialog *message_dialog = NULL;
89 /* States to be listed in the menu.
90  * Each state has a boolean telling if it can have custom message */
91 static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
92                          MC_PRESENCE_DO_NOT_DISTURB, TRUE,
93                          MC_PRESENCE_AWAY, TRUE,
94                          MC_PRESENCE_HIDDEN, FALSE,
95                          MC_PRESENCE_OFFLINE, FALSE};
96
97 static void            presence_chooser_finalize               (GObject                    *object);
98 static void            presence_chooser_presence_changed_cb    (EmpathyPresenceChooser      *chooser);
99 static gboolean        presence_chooser_flash_timeout_cb       (EmpathyPresenceChooser      *chooser);
100 static void            presence_chooser_flash_start            (EmpathyPresenceChooser      *chooser,
101                                                                 McPresence                  state_1,
102                                                                 McPresence                  state_2);
103 static void            presence_chooser_flash_stop             (EmpathyPresenceChooser      *chooser,
104                                                                 McPresence                  state);
105 static void            presence_chooser_menu_add_item          (GtkWidget                  *menu,
106                                                                 const gchar                *str,
107                                                                 McPresence                  state);
108 static void            presence_chooser_noncustom_activate_cb  (GtkWidget                  *item,
109                                                                 gpointer                    user_data);
110 static void            presence_chooser_set_state              (McPresence                  state,
111                                                                 const gchar                *status);
112 static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
113                                                                 gpointer                    user_data);
114 static void            presence_chooser_dialog_show            (GtkWindow                  *parent);
115
116 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
117
118 static void
119 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
120 {
121         GObjectClass *object_class = G_OBJECT_CLASS (klass);
122
123         object_class->finalize = presence_chooser_finalize;
124
125         g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
126 }
127
128 enum
129 {
130         COL_STATE_ICON_NAME,
131         COL_STATE,
132         COL_STATUS_TEXT,
133         COL_DISPLAY_MARKUP,
134         COL_STATUS_CUSTOMISABLE,
135         COL_TYPE,
136         N_COLUMNS
137 };
138
139 enum
140 {
141         ENTRY_TYPE_BUILTIN,
142         ENTRY_TYPE_SAVED,
143         ENTRY_TYPE_CUSTOM,
144         ENTRY_TYPE_SEPARATOR,
145         ENTRY_TYPE_EDIT_CUSTOM,
146 };
147
148 static GtkTreeModel *
149 create_model (void)
150 {
151         GtkListStore *store = gtk_list_store_new (N_COLUMNS,
152                         G_TYPE_STRING,          /* COL_STATE_ICON_NAME */
153                         MC_TYPE_PRESENCE,       /* COL_STATE */
154                         G_TYPE_STRING,          /* COL_STATUS_TEXT */
155                         G_TYPE_STRING,          /* COL_DISPLAY_MARKUP */
156                         G_TYPE_BOOLEAN,         /* COL_STATUS_CUSTOMISABLE */
157                         G_TYPE_INT);            /* COL_TYPE */
158         
159         GtkTreeIter iter;
160         
161         int i;
162         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
163                 GList       *list, *l;
164
165                 const char *status = empathy_presence_get_default_message (states[i]);
166                 const char *icon_name = empathy_icon_name_for_presence (states[i]);
167
168                 gtk_list_store_append (store, &iter);
169                 gtk_list_store_set (store, &iter,
170                                 COL_STATE_ICON_NAME, icon_name,
171                                 COL_STATE, states[i],
172                                 COL_STATUS_TEXT, status,
173                                 COL_DISPLAY_MARKUP, status,
174                                 COL_STATUS_CUSTOMISABLE, states[i+1],
175                                 COL_TYPE, ENTRY_TYPE_BUILTIN,
176                                 -1);
177
178                 if (states[i+1]) {
179                         /* Set custom messages if wanted */
180                         list = empathy_status_presets_get (states[i], 5);
181                         for (l = list; l; l = l->next) {
182                                 gtk_list_store_append (store, &iter);
183                                 gtk_list_store_set (store, &iter,
184                                                 COL_STATE_ICON_NAME, icon_name,
185                                                 COL_STATE, states[i],
186                                                 COL_STATUS_TEXT, l->data,
187                                                 COL_DISPLAY_MARKUP, l->data,
188                                                 COL_STATUS_CUSTOMISABLE, TRUE,
189                                                 COL_TYPE, ENTRY_TYPE_SAVED,
190                                                 -1);
191                         }
192                         g_list_free (list);
193                 
194                         gtk_list_store_append (store, &iter);
195                         gtk_list_store_set (store, &iter,
196                                         COL_STATE_ICON_NAME, icon_name,
197                                         COL_STATE, states[i],
198                                         COL_STATUS_TEXT, "",
199                                         COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
200                                         COL_STATUS_CUSTOMISABLE, TRUE,
201                                         COL_TYPE, ENTRY_TYPE_CUSTOM,
202                                         -1);
203                 }
204
205         }
206         
207         /* add a separator */
208         gtk_list_store_append (store, &iter);
209         gtk_list_store_set (store, &iter,
210                         COL_TYPE, ENTRY_TYPE_SEPARATOR,
211                         -1);
212         
213         gtk_list_store_append (store, &iter);
214         gtk_list_store_set (store, &iter,
215                         COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
216                         COL_STATUS_TEXT, "",
217                         COL_DISPLAY_MARKUP, "Edit Custom Messages...",
218                         COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
219                         -1);
220
221         return GTK_TREE_MODEL (store);
222 }
223
224 static void
225 presence_chooser_popup_shown_cb (GObject *self,
226                                  GParamSpec *pspec,
227                                  gpointer user_data)
228 {
229         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
230         gboolean shown;
231
232         g_object_get (self, "popup-shown", &shown, NULL);
233
234         if (!shown) return;
235
236         /* see presence_chooser_entry_focus_out_cb() for what this does */
237         if (priv->focus_out_idle_source != 0)
238         {
239                 g_source_remove (priv->focus_out_idle_source);
240                 priv->focus_out_idle_source = 0;
241         }
242
243         GtkTreeModel *model = create_model ();
244
245         gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (model));
246         
247         g_object_unref (model);
248 }
249
250 static void
251 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
252                                      gboolean editing)
253 {
254         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
255         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
256
257         if (priv->block_set_editing) return;
258
259         if (editing)
260         {
261                 priv->editing_status = TRUE;
262
263                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
264                                 GTK_ENTRY_ICON_SECONDARY,
265                                 GTK_STOCK_OK);
266                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
267                                 GTK_ENTRY_ICON_SECONDARY,
268                                 _("Set status"));
269                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
270                                 GTK_ENTRY_ICON_PRIMARY,
271                                 FALSE);
272         }
273         else
274         {
275                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
276                                 GTK_ENTRY_ICON_SECONDARY,
277                                 NULL);
278                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
279                                 GTK_ENTRY_ICON_SECONDARY,
280                                 NULL);
281                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
282                                 GTK_ENTRY_ICON_PRIMARY,
283                                 TRUE);
284
285                 /* attempt to get the toplevel for this widget */
286                 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
287                 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window))
288                 {
289                         /* unset the focus */
290                         gtk_window_set_focus (GTK_WINDOW (window), NULL);
291                 }
292
293                 /* see presence_chooser_entry_focus_out_cb()
294                  * for what this does */
295                 if (priv->focus_out_idle_source != 0)
296                 {
297                         g_source_remove (priv->focus_out_idle_source);
298                         priv->focus_out_idle_source = 0;
299                 }
300
301                 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
302
303                 priv->editing_status = FALSE;
304         }
305 }
306
307 static void
308 mc_set_custom_state (EmpathyPresenceChooser *self)
309 {
310         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
311         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
312
313         /* update the status with MC */
314         const char *status = gtk_entry_get_text (GTK_ENTRY (entry));
315         DEBUG ("Sending state to MC-> %s (%s)\n",
316                         g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
317                                 priv->state)->value_name,
318                         status);
319         empathy_idle_set_presence (priv->idle, priv->state, status);
320 }
321
322 static void
323 ui_set_custom_state (EmpathyPresenceChooser *self,
324                            McPresence state,
325                            const char *status)
326 {
327         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
328         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
329         const char *icon_name;
330
331         priv->block_set_editing++;
332         priv->block_changed++;
333
334         icon_name = empathy_icon_name_for_presence (state);
335         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
336                         GTK_ENTRY_ICON_PRIMARY,
337                         icon_name);
338         gtk_entry_set_text (GTK_ENTRY (entry), status);
339
340         priv->block_changed--;
341         priv->block_set_editing--;
342 }
343
344 static void
345 presence_chooser_reset_status (EmpathyPresenceChooser *self)
346 {
347         /* recover the status that was unset */
348         presence_chooser_presence_changed_cb (self);
349 }
350
351 static void
352 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser  *self,
353                                         GtkEntryIconPosition     icon_pos,
354                                         GdkEvent                *event,
355                                         GtkEntry                *entry)
356 {
357         presence_chooser_set_status_editing (self, FALSE);
358         mc_set_custom_state (self);
359 }
360
361 static void
362 presence_chooser_entry_activate_cb (EmpathyPresenceChooser      *self,
363                                     GtkEntry                    *entry)
364 {
365         presence_chooser_set_status_editing (self, FALSE);
366         mc_set_custom_state (self);
367 }
368
369 static gboolean
370 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser       *self,
371                                            GdkEventKey                  *event,
372                                            GtkWidget                    *entry)
373 {
374         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
375
376         if (priv->editing_status && event->keyval == GDK_Escape)
377         {
378                 /* the user pressed Escape, undo the editing */
379                 presence_chooser_set_status_editing (self, FALSE);
380                 presence_chooser_reset_status (self);
381
382                 return TRUE;
383         }
384         else if (event->keyval == GDK_Up || event->keyval == GDK_Down)
385         {
386                 /* ignore */
387                 return TRUE;
388         }
389
390         return FALSE; /* send this event elsewhere */
391 }
392
393 static gboolean
394 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
395                                               GdkEventButton         *event,
396                                               GtkWidget              *entry)
397 {
398         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
399
400         if (!priv->editing_status &&
401             event->button == 1 &&
402             !GTK_WIDGET_HAS_FOCUS (entry))
403         {
404                 gtk_widget_grab_focus (entry);
405                 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
406
407                 return TRUE;
408         }
409
410         return FALSE;
411 }
412
413 static void
414 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
415                                    GtkEntry               *entry)
416 {
417         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
418
419         if (priv->block_changed) return;
420
421         /* the combo is being edited to a custom entry */
422         if (!priv->editing_status)
423         {
424                 presence_chooser_set_status_editing (self, TRUE);
425         }
426 }
427
428 static void
429 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
430 {
431         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
432
433         if (priv->block_changed) return;
434
435         GtkTreeIter iter;
436         char *icon_name;
437         McPresence new_state;
438         gboolean customisable = TRUE;
439         int type = -1;
440
441         GtkTreeModel *model = gtk_combo_box_get_model (self);
442         if (!gtk_combo_box_get_active_iter (self, &iter))
443         {
444                 return;
445         }
446
447         gtk_tree_model_get (model, &iter,
448                         COL_STATE_ICON_NAME, &icon_name,
449                         COL_STATE, &new_state,
450                         COL_STATUS_CUSTOMISABLE, &customisable,
451                         COL_TYPE, &type,
452                         -1);
453
454         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
455
456         /* some types of status aren't editable, set the editability of the
457          * entry appropriately. Unless we're just about to reset it anyway,
458          * in which case, don't fiddle with it */
459         if (type != ENTRY_TYPE_EDIT_CUSTOM)
460         {
461                 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
462                 priv->state = new_state;
463         }
464
465         if (type == ENTRY_TYPE_EDIT_CUSTOM)
466         {
467                 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
468
469                 /* attempt to get the toplevel for this widget */
470                 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
471                 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window))
472                 {
473                         window = NULL;
474                 }
475
476                 presence_chooser_dialog_show (GTK_WINDOW (window));
477         }
478         else if (type == ENTRY_TYPE_CUSTOM)
479         {
480                 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
481                                 GTK_ENTRY_ICON_PRIMARY,
482                                 icon_name);
483
484                 /* preseed the status */
485                 if (priv->editing_status)
486                 {
487                         /* if the user is already in editing mode and changes
488                          * the status type, preseed the text they've already
489                          * entered */
490                         /* FIXME: make this work */
491                 }
492                 else if (priv->previous_type == ENTRY_TYPE_BUILTIN)
493                 {
494                         /* if their previous entry was a builtin, don't
495                          * preseed */
496                         gtk_entry_set_text (GTK_ENTRY (entry), "");
497                 }
498                 else
499                 {
500                         /* else preseed the text of their currently entered
501                          * status message */
502                         const char *status = empathy_idle_get_status (priv->idle);
503                         gtk_entry_set_text (GTK_ENTRY (entry), status);
504                 }
505
506                 /* grab the focus */
507                 gtk_widget_grab_focus (entry);
508         }
509         else
510         {
511                 char *status;
512                 /* just in case we were setting a new status when
513                  * things were changed */
514                 presence_chooser_set_status_editing (
515                                 EMPATHY_PRESENCE_CHOOSER (self),
516                                 FALSE);
517
518                 gtk_tree_model_get (model, &iter,
519                                 COL_STATUS_TEXT, &status,
520                                 -1);
521
522                 empathy_idle_set_presence (priv->idle, priv->state, status);
523
524                 g_free (status);
525         }
526
527         if (type != ENTRY_TYPE_EDIT_CUSTOM)
528         {
529                 priv->previous_type = type;
530         }
531         g_free (icon_name);
532 }
533
534 static gboolean
535 combo_row_separator_func (GtkTreeModel  *model,
536                           GtkTreeIter   *iter,
537                           gpointer       data)
538 {
539         int type;
540         gtk_tree_model_get (model, iter,
541                         COL_TYPE, &type,
542                         -1);
543
544         return (type == ENTRY_TYPE_SEPARATOR);
545 }
546
547 static gboolean
548 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
549 {
550         DEBUG ("Autocommiting status message\n");
551
552         EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
553         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
554
555         presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
556
557         return FALSE;
558 }
559
560 static gboolean
561 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
562                                      GdkEventFocus *event,
563                                      GtkEntry *entry)
564 {
565         EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
566
567         if (priv->editing_status)
568         {
569                 /* this seems a bit evil and maybe it will be fragile,
570                  * someone should think of a better way to do it.
571                  *
572                  * The entry has focused out, but we don't know where the focus
573                  * has gone. If it goes to the combo box, we don't want to
574                  * do anything. If it's gone anywhere else, we want to commit
575                  * the result.
576                  *
577                  * Thus we install this idle handler and store its source.
578                  * If the source is scheduled when the popup handler runs,
579                  * it will remove it, else the callback will commit the result.
580                  */
581                 priv->focus_out_idle_source = g_idle_add (
582                                 presence_chooser_entry_focus_out_idle_cb,
583                                 chooser);
584         }
585
586         gtk_editable_set_position (GTK_EDITABLE (entry), 0);
587
588         return FALSE;
589 }
590
591 static void
592 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
593 {
594         EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
595                 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
596
597         chooser->priv = priv;
598         
599         GtkTreeModel *model = create_model ();
600
601         gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
602         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
603                         COL_STATUS_TEXT);
604         gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
605                         combo_row_separator_func,
606                         NULL, NULL);
607         
608         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
609         gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
610                         GTK_ENTRY_ICON_PRIMARY, FALSE);
611         g_signal_connect_object (entry, "icon-release",
612                         G_CALLBACK (presence_chooser_entry_icon_release_cb),
613                         chooser,
614                         G_CONNECT_SWAPPED);
615         g_signal_connect_object (entry, "activate",
616                         G_CALLBACK (presence_chooser_entry_activate_cb),
617                         chooser,
618                         G_CONNECT_SWAPPED);
619         g_signal_connect_object (entry, "key-press-event",
620                         G_CALLBACK (presence_chooser_entry_key_press_event_cb),
621                         chooser,
622                         G_CONNECT_SWAPPED);
623         g_signal_connect_object (entry, "button-press-event",
624                         G_CALLBACK (presence_chooser_entry_button_press_event_cb),
625                         chooser,
626                         G_CONNECT_SWAPPED);
627
628         GtkCellRenderer *renderer;
629         gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
630
631         renderer = gtk_cell_renderer_pixbuf_new ();
632         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
633         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
634                         "icon-name", COL_STATE_ICON_NAME,
635                         NULL);
636         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
637
638         renderer = gtk_cell_renderer_text_new ();
639         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
640         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
641                         "markup", COL_DISPLAY_MARKUP,
642                         NULL);
643
644         g_object_unref (model);
645
646         g_signal_connect (chooser, "notify::popup-shown",
647                         G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
648         g_signal_connect (chooser, "changed",
649                         G_CALLBACK (presence_chooser_changed_cb), NULL);
650         g_signal_connect_swapped (entry, "changed",
651                         G_CALLBACK (presence_chooser_entry_changed_cb),
652                         chooser);
653         g_signal_connect_swapped (entry, "focus-out-event",
654                         G_CALLBACK (presence_chooser_entry_focus_out_cb),
655                         chooser);
656
657         priv->idle = empathy_idle_dup_singleton ();
658         presence_chooser_presence_changed_cb (chooser);
659         g_signal_connect_swapped (priv->idle, "notify",
660                         G_CALLBACK (presence_chooser_presence_changed_cb),
661                         chooser);
662
663         g_object_set (chooser,
664                         // FIXME: this string sucks
665                         "tooltip-text", _("Set your presence and current status"),
666                         NULL);
667 }
668
669 static void
670 presence_chooser_finalize (GObject *object)
671 {
672         EmpathyPresenceChooserPriv *priv;
673
674         priv = GET_PRIV (object);
675
676         if (priv->flash_timeout_id) {
677                 g_source_remove (priv->flash_timeout_id);
678         }
679
680         if (priv->focus_out_idle_source) {
681                 g_source_remove (priv->focus_out_idle_source);
682         }
683
684         g_signal_handlers_disconnect_by_func (priv->idle,
685                                               presence_chooser_presence_changed_cb,
686                                               object);
687         g_object_unref (priv->idle);
688
689         G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
690 }
691
692 GtkWidget *
693 empathy_presence_chooser_new (void)
694 {
695         GtkWidget *chooser;
696
697         chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
698
699         return chooser;
700 }
701
702 static void
703 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
704 {
705         EmpathyPresenceChooserPriv *priv;
706         McPresence                 state;
707         McPresence                 flash_state;
708         const gchar               *status;
709
710         priv = GET_PRIV (chooser);
711
712         priv->state = state = empathy_idle_get_state (priv->idle);
713         status = empathy_idle_get_status (priv->idle);
714         flash_state = empathy_idle_get_flash_state (priv->idle);
715
716         /* look through the model and attempt to find a matching state */
717         GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
718         GtkTreeIter iter;
719         gboolean valid, match_state = FALSE, match = FALSE;
720         for (valid = gtk_tree_model_get_iter_first (model, &iter);
721              valid;
722              valid = gtk_tree_model_iter_next (model, &iter))
723         {
724                 int m_type;
725                 McPresence m_state;
726                 char *m_status;
727
728                 gtk_tree_model_get (model, &iter,
729                                 COL_STATE, &m_state,
730                                 COL_TYPE, &m_type,
731                                 -1);
732
733                 if (m_type == ENTRY_TYPE_CUSTOM ||
734                     m_type == ENTRY_TYPE_SEPARATOR ||
735                     m_type == ENTRY_TYPE_EDIT_CUSTOM)
736                 {
737                         continue;
738                 }
739                 else if (!match_state && state == m_state)
740                 {
741                         /* we are now in the section that can contain our
742                          * match */
743                         match_state = TRUE;
744                 }
745                 else if (match_state && state != m_state)
746                 {
747                         /* we have passed the section that can contain our
748                          * match */
749                         break;
750                 }
751
752                 gtk_tree_model_get (model, &iter,
753                                 COL_STATUS_TEXT, &m_status,
754                                 -1);
755
756                 match = !strcmp (status, m_status);
757
758                 g_free (m_status);
759
760                 if (match) break;
761
762         }
763
764         if (match)
765         {
766                 priv->block_changed++;
767                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
768                 priv->block_changed--;
769         }
770         else
771         {
772                 ui_set_custom_state (chooser, state, status);
773         }
774
775         if (flash_state != MC_PRESENCE_UNSET) {
776                 presence_chooser_flash_start (chooser, state, flash_state);
777         } else {
778                 presence_chooser_flash_stop (chooser, state);
779         }
780 }
781
782 static gboolean
783 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
784 {
785         EmpathyPresenceChooserPriv *priv;
786         McPresence                 state;
787         static gboolean            on = FALSE;
788
789         priv = GET_PRIV (chooser);
790
791         if (on) {
792                 state = priv->flash_state_1;
793         } else {
794                 state = priv->flash_state_2;
795         }
796
797         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
798         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
799                         GTK_ENTRY_ICON_PRIMARY,
800                         empathy_icon_name_for_presence (state));
801
802         on = !on;
803
804         return TRUE;
805 }
806
807 static void
808 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
809                               McPresence             state_1,
810                               McPresence             state_2)
811 {
812         EmpathyPresenceChooserPriv *priv;
813
814         g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
815
816         priv = GET_PRIV (chooser);
817
818         priv->flash_state_1 = state_1;
819         priv->flash_state_2 = state_2;
820
821         if (!priv->flash_timeout_id) {
822                 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
823                                                         (GSourceFunc) presence_chooser_flash_timeout_cb,
824                                                         chooser);
825         }
826 }
827
828 static void
829 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
830                              McPresence             state)
831 {
832         EmpathyPresenceChooserPriv *priv;
833
834         g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
835
836         priv = GET_PRIV (chooser);
837
838         if (priv->flash_timeout_id) {
839                 g_source_remove (priv->flash_timeout_id);
840                 priv->flash_timeout_id = 0;
841         }
842         GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
843         
844         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
845                         GTK_ENTRY_ICON_PRIMARY,
846                         empathy_icon_name_for_presence (state));
847
848         // FIXME - what does this do?
849         // priv->last_state = state;
850 }
851
852 GtkWidget *
853 empathy_presence_chooser_create_menu (void)
854 {
855         const gchar *status;
856         GtkWidget   *menu;
857         GtkWidget   *item;
858         GtkWidget   *image;
859         guint        i;
860
861         menu = gtk_menu_new ();
862
863         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
864                 GList       *list, *l;
865
866                 status = empathy_presence_get_default_message (states[i]);
867                 presence_chooser_menu_add_item (menu,
868                                                 status,
869                                                 states[i]);
870
871                 if (states[i+1]) {
872                         /* Set custom messages if wanted */
873                         list = empathy_status_presets_get (states[i], 5);
874                         for (l = list; l; l = l->next) {
875                                 presence_chooser_menu_add_item (menu,
876                                                                 l->data,
877                                                                 states[i]);
878                         }
879                         g_list_free (list);
880                 }
881
882         }
883
884         /* Separator. */
885         item = gtk_menu_item_new ();
886         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
887         gtk_widget_show (item);
888
889         /* Custom messages */
890         item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
891         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
892         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
893         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
894         gtk_widget_show (image);
895         gtk_widget_show (item);
896
897         g_signal_connect (item,
898                           "activate",
899                           G_CALLBACK (presence_chooser_custom_activate_cb),
900                           NULL);
901
902         return menu;
903 }
904
905 static void
906 presence_chooser_menu_add_item (GtkWidget   *menu,
907                                 const gchar *str,
908                                 McPresence   state)
909 {
910         GtkWidget   *item;
911         GtkWidget   *image;
912         const gchar *icon_name;
913
914         item = gtk_image_menu_item_new_with_label (str);
915         icon_name = empathy_icon_name_for_presence (state);
916
917         g_signal_connect (item, "activate",
918                           G_CALLBACK (presence_chooser_noncustom_activate_cb),
919                           NULL);
920
921         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
922         gtk_widget_show (image);
923
924         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
925         gtk_widget_show (item);
926
927         g_object_set_data_full (G_OBJECT (item),
928                                 "status", g_strdup (str),
929                                 (GDestroyNotify) g_free);
930
931         g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
932
933         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
934 }
935
936 static void
937 presence_chooser_noncustom_activate_cb (GtkWidget *item,
938                                         gpointer   user_data)
939 {
940         McPresence   state;
941         const gchar *status;
942
943         status = g_object_get_data (G_OBJECT (item), "status");
944         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
945
946         presence_chooser_set_state (state, status);
947 }
948
949 static void
950 presence_chooser_set_state (McPresence   state,
951                             const gchar *status)
952 {
953         EmpathyIdle *idle;
954
955         idle = empathy_idle_dup_singleton ();
956         empathy_idle_set_presence (idle, state, status);
957         g_object_unref (idle);
958 }
959
960 static void
961 presence_chooser_custom_activate_cb (GtkWidget *item,
962                                      gpointer   user_data)
963 {
964         presence_chooser_dialog_show (NULL);
965 }
966
967 static McPresence
968 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
969 {
970         GtkTreeModel *model;
971         GtkTreeIter   iter;
972         McPresence    presence = LAST_MC_PRESENCE;
973
974         model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
975         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
976                 gtk_tree_model_get (model, &iter,
977                                     COL_PRESENCE, &presence,
978                                     -1);
979         }
980
981         return presence;
982 }
983
984 static void
985 presence_chooser_dialog_status_changed_cb (GtkWidget           *widget,
986                                            CustomMessageDialog *dialog)
987 {
988         GtkListStore *store;
989         GtkTreeIter   iter;
990         McPresence    presence = LAST_MC_PRESENCE;
991         GList        *messages, *l;
992
993         presence = presence_chooser_dialog_get_selected (dialog);
994
995         store = gtk_list_store_new (1, G_TYPE_STRING);
996         messages = empathy_status_presets_get (presence, -1);
997         for (l = messages; l; l = l->next) {
998                 gtk_list_store_append (store, &iter);
999                 gtk_list_store_set (store, &iter, 0, l->data, -1);
1000         }
1001
1002         gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
1003                             messages ? messages->data : "");
1004
1005         g_list_free (messages);
1006
1007         gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
1008                                  GTK_TREE_MODEL (store));
1009
1010         g_object_unref (store);
1011 }
1012
1013 static void
1014 presence_chooser_dialog_message_changed_cb (GtkWidget           *widget,
1015                                             CustomMessageDialog *dialog)
1016 {
1017         McPresence   presence;
1018         GList       *messages, *l;
1019         const gchar *text;
1020         gboolean     found = FALSE;
1021
1022         presence = presence_chooser_dialog_get_selected (dialog);
1023         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1024
1025         messages = empathy_status_presets_get (presence, -1);
1026         for (l = messages; l; l = l->next) {
1027                 if (!tp_strdiff (text, l->data)) {
1028                         found = TRUE;
1029                         break;
1030                 }
1031         }
1032         g_list_free (messages);
1033
1034         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
1035                                       found);
1036 }
1037
1038 static void
1039 presence_chooser_dialog_save_toggled_cb (GtkWidget           *widget,
1040                                          CustomMessageDialog *dialog)
1041 {
1042         gboolean     active;
1043         McPresence   state;
1044         const gchar *text;
1045
1046         active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
1047         state = presence_chooser_dialog_get_selected (dialog);
1048         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1049
1050         if (active) {
1051                 empathy_status_presets_set_last (state, text);
1052         } else {
1053                 empathy_status_presets_remove (state, text);
1054         }
1055 }
1056
1057 static void
1058 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
1059 {
1060         GtkListStore    *store;
1061         GtkCellRenderer *renderer;
1062         GtkTreeIter      iter;
1063         guint            i;
1064
1065         store = gtk_list_store_new (COL_COUNT,
1066                                     G_TYPE_STRING,     /* Icon name */
1067                                     G_TYPE_STRING,     /* Label     */
1068                                     MC_TYPE_PRESENCE); /* Presence   */
1069         gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1070                                  GTK_TREE_MODEL (store));
1071
1072         renderer = gtk_cell_renderer_pixbuf_new ();
1073         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1074         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1075                                         "icon-name", COL_ICON,
1076                                         NULL);
1077         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1078
1079         renderer = gtk_cell_renderer_text_new ();
1080         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1081         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1082                                         "text", COL_LABEL,
1083                                         NULL);
1084
1085         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1086                 if (!states[i+1]) {
1087                         continue;
1088                 }
1089
1090                 gtk_list_store_append (store, &iter);
1091                 gtk_list_store_set (store, &iter,
1092                                     COL_ICON, empathy_icon_name_for_presence (states[i]),
1093                                     COL_LABEL, empathy_presence_get_default_message (states[i]),
1094                                     COL_PRESENCE, states[i],
1095                                     -1);
1096         }
1097
1098         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1099 }
1100
1101 static void
1102 presence_chooser_dialog_response_cb (GtkWidget           *widget,
1103                                      gint                 response,
1104                                      CustomMessageDialog *dialog)
1105 {
1106         if (response == GTK_RESPONSE_APPLY) {
1107                 McPresence   state;
1108                 const gchar *text;
1109
1110                 state = presence_chooser_dialog_get_selected (dialog);
1111                 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1112
1113                 presence_chooser_set_state (state, text);
1114         }
1115
1116         gtk_widget_destroy (widget);
1117 }
1118
1119 static void
1120 presence_chooser_dialog_destroy_cb (GtkWidget           *widget,
1121                                     CustomMessageDialog *dialog)
1122 {
1123
1124         g_free (dialog);
1125         message_dialog = NULL;
1126 }
1127
1128 static void
1129 presence_chooser_dialog_show (GtkWindow *parent)
1130 {
1131         GladeXML *glade;
1132         gchar    *filename;
1133
1134         if (message_dialog) {
1135                 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1136                 return;
1137         }
1138
1139         message_dialog = g_new0 (CustomMessageDialog, 1);
1140
1141         filename = empathy_file_lookup ("empathy-presence-chooser.glade",
1142                                         "libempathy-gtk");
1143         glade = empathy_glade_get_file (filename,
1144                                        "custom_message_dialog",
1145                                        NULL,
1146                                        "custom_message_dialog", &message_dialog->dialog,
1147                                        "checkbutton_save", &message_dialog->checkbutton_save,
1148                                        "comboboxentry_message", &message_dialog->comboboxentry_message,
1149                                        "combobox_status", &message_dialog->combobox_status,
1150                                        NULL);
1151         g_free (filename);
1152
1153         empathy_glade_connect (glade,
1154                                message_dialog,
1155                                "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1156                                "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1157                                "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1158                                "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1159                                NULL);
1160
1161         g_object_unref (glade);
1162
1163         /* Setup the message combobox */
1164         message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1165         gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1166         gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1167         g_signal_connect (message_dialog->entry_message, "changed",
1168                           G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1169                           message_dialog);
1170
1171         presence_chooser_dialog_setup (message_dialog);
1172
1173         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1174
1175         if (parent)
1176         {
1177                 gtk_window_set_transient_for (
1178                                 GTK_WINDOW (message_dialog->dialog),
1179                                 parent);
1180         }
1181
1182         gtk_widget_show_all (message_dialog->dialog);
1183 }
1184