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