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