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