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