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