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