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