]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-presence-chooser.c
Merge commit 'staz/dnd'
[empathy.git] / libempathy-gtk / empathy-presence-chooser.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2005-2007 Imendio AB
4  * Copyright (C) 2009 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Richard Hult <richard@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  *          Davyd Madeley <davyd.madeley@collabora.co.uk>
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include <glib/gi18n-lib.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkkeysyms.h>
35
36 #include <telepathy-glib/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_finalize               (GObject                    *object);
130 static void            presence_chooser_presence_changed_cb    (EmpathyPresenceChooser      *chooser);
131 static void            presence_chooser_menu_add_item          (GtkWidget                  *menu,
132                                                                 const gchar                *str,
133                                                                 TpConnectionPresenceType                  state);
134 static void            presence_chooser_noncustom_activate_cb  (GtkWidget                  *item,
135                                                                 gpointer                    user_data);
136 static void            presence_chooser_set_state              (TpConnectionPresenceType                  state,
137                                                                 const gchar                *status);
138 static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
139                                                                 gpointer                    user_data);
140
141 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
142
143 static void
144 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
145 {
146         GObjectClass *object_class = G_OBJECT_CLASS (klass);
147
148         object_class->finalize = presence_chooser_finalize;
149
150         g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
151 }
152
153 static void
154 presence_chooser_create_model (EmpathyPresenceChooser *self)
155 {
156         GtkListStore *store;
157         char *custom_message;
158         int i;
159
160         store = gtk_list_store_new (N_COLUMNS,
161                                     G_TYPE_STRING,    /* COL_STATUS_TEXT */
162                                     G_TYPE_STRING,    /* COL_STATE_ICON_NAME */
163                                     G_TYPE_UINT,      /* COL_STATE */
164                                     G_TYPE_STRING,    /* COL_DISPLAY_MARKUP */
165                                     G_TYPE_BOOLEAN,   /* COL_STATUS_CUSTOMISABLE */
166                                     G_TYPE_INT);      /* COL_TYPE */
167
168         custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Messageā€¦"));
169
170         for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
171                 GList       *list, *l;
172                 const char *status, *icon_name;
173
174                 status = empathy_presence_get_default_message (states[i].state);
175                 icon_name = empathy_icon_name_for_presence (states[i].state);
176
177                 gtk_list_store_insert_with_values (store, NULL, -1,
178                         COL_STATUS_TEXT, status,
179                         COL_STATE_ICON_NAME, icon_name,
180                         COL_STATE, states[i].state,
181                         COL_DISPLAY_MARKUP, status,
182                         COL_STATUS_CUSTOMISABLE, states[i].customisable,
183                         COL_TYPE, ENTRY_TYPE_BUILTIN,
184                         -1);
185
186                 if (states[i].customisable) {
187                         /* Set custom messages if wanted */
188                         list = empathy_status_presets_get (states[i].state, -1);
189                         list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
190                         for (l = list; l; l = l->next) {
191                                 gtk_list_store_insert_with_values (store,
192                                         NULL, -1,
193                                         COL_STATUS_TEXT, l->data,
194                                         COL_STATE_ICON_NAME, icon_name,
195                                         COL_STATE, states[i].state,
196                                         COL_DISPLAY_MARKUP, l->data,
197                                         COL_STATUS_CUSTOMISABLE, TRUE,
198                                         COL_TYPE, ENTRY_TYPE_SAVED,
199                                         -1);
200                         }
201                         g_list_free (list);
202
203                         gtk_list_store_insert_with_values (store, NULL, -1,
204                                 COL_STATUS_TEXT, _("Custom Messageā€¦"),
205                                 COL_STATE_ICON_NAME, icon_name,
206                                 COL_STATE, states[i].state,
207                                 COL_DISPLAY_MARKUP, custom_message,
208                                 COL_STATUS_CUSTOMISABLE, TRUE,
209                                 COL_TYPE, ENTRY_TYPE_CUSTOM,
210                                 -1);
211                 }
212
213         }
214
215         /* add a separator */
216         gtk_list_store_insert_with_values (store, NULL, -1,
217                         COL_TYPE, ENTRY_TYPE_SEPARATOR,
218                         -1);
219
220         gtk_list_store_insert_with_values (store, NULL, -1,
221                 COL_STATUS_TEXT, _("Edit Custom Messagesā€¦"),
222                 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
223                 COL_DISPLAY_MARKUP, _("Edit Custom Messagesā€¦"),
224                 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
225                 -1);
226
227         g_free (custom_message);
228
229         gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
230         g_object_unref (store);
231 }
232
233 static void
234 presence_chooser_popup_shown_cb (GObject *self,
235                                  GParamSpec *pspec,
236                                  gpointer user_data)
237 {
238         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
239         gboolean shown;
240
241         g_object_get (self, "popup-shown", &shown, NULL);
242         if (!shown) {
243                 return;
244         }
245
246         /* see presence_chooser_entry_focus_out_cb () for what this does */
247         if (priv->focus_out_idle_source != 0) {
248                 g_source_remove (priv->focus_out_idle_source);
249                 priv->focus_out_idle_source = 0;
250         }
251
252         presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
253 }
254
255 static PresenceChooserEntryType
256 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
257 {
258         GtkTreeIter iter;
259         PresenceChooserEntryType type = -1;
260
261         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
262                 type = ENTRY_TYPE_CUSTOM;
263         }
264         else {
265                 GtkTreeModel *model;
266
267                 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
268                 gtk_tree_model_get (model, &iter,
269                                     COL_TYPE, &type,
270                                     -1);
271         }
272
273         return type;
274 }
275
276 static TpConnectionPresenceType
277 get_state_and_status (EmpathyPresenceChooser *self,
278         gchar **status)
279 {
280         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
281         TpConnectionPresenceType state;
282         gchar *tmp;
283
284         state = tp_account_manager_get_most_available_presence (
285                 priv->account_manager, NULL, &tmp);
286         if (EMP_STR_EMPTY (tmp)) {
287                 /* no message, use the default message */
288                 g_free (tmp);
289                 tmp = g_strdup (empathy_presence_get_default_message (state));
290         }
291
292         if (status != NULL)
293                 *status = tmp;
294         else
295                 g_free (tmp);
296
297         return state;
298 }
299
300 static gboolean
301 presence_chooser_is_preset (EmpathyPresenceChooser *self)
302 {
303         TpConnectionPresenceType state;
304         char *status;
305         GList *presets, *l;
306         gboolean match = FALSE;
307
308         state = get_state_and_status (self, &status);
309
310         presets = empathy_status_presets_get (state, -1);
311         for (l = presets; l; l = l->next) {
312                 char *preset = (char *) l->data;
313
314                 if (!tp_strdiff (status, preset)) {
315                         match = TRUE;
316                         break;
317                 }
318         }
319
320         g_list_free (presets);
321
322         DEBUG ("is_preset(%i, %s) = %i", state, status, match);
323
324         g_free (status);
325         return match;
326 }
327
328 static void
329 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
330 {
331         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
332         GtkWidget *entry;
333         PresenceChooserEntryType type;
334
335         entry = gtk_bin_get_child (GTK_BIN (self));
336         type = presence_chooser_get_entry_type (self);
337
338         if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
339                 if (presence_chooser_is_preset (self)) {
340                         /* saved entries can be removed from the list */
341                         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
342                                            GTK_ENTRY_ICON_SECONDARY,
343                                            "emblem-favorite");
344                         gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
345                                          GTK_ENTRY_ICON_SECONDARY,
346                                          _("Click to remove this status as a favorite"));
347                 }
348                 else if (priv->not_favorite_pixbuf != NULL) {
349                         /* custom entries can be favorited */
350                         gtk_entry_set_icon_from_pixbuf (GTK_ENTRY (entry),
351                                            GTK_ENTRY_ICON_SECONDARY,
352                                            priv->not_favorite_pixbuf);
353                         gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
354                                          GTK_ENTRY_ICON_SECONDARY,
355                                          _("Click to make this status a favorite"));
356                 }
357         }
358         else {
359                 /* built-in entries cannot be favorited */
360                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
361                                            GTK_ENTRY_ICON_SECONDARY,
362                                            NULL);
363                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
364                                          GTK_ENTRY_ICON_SECONDARY,
365                                          NULL);
366         }
367 }
368
369 static void
370 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
371                                      gboolean editing)
372 {
373         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
374         GtkWidget *entry;
375
376         if (priv->block_set_editing) {
377                 return;
378         }
379
380         entry = gtk_bin_get_child (GTK_BIN (self));
381         if (editing) {
382                 priv->editing_status = TRUE;
383
384                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
385                                                GTK_ENTRY_ICON_SECONDARY,
386                                                GTK_STOCK_OK);
387                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
388                                                  GTK_ENTRY_ICON_SECONDARY,
389                                                  _("Set status"));
390                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
391                                               GTK_ENTRY_ICON_PRIMARY,
392                                               FALSE);
393         } else {
394                 GtkWidget *window;
395
396                 presence_chooser_set_favorite_icon (self);
397                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
398                                               GTK_ENTRY_ICON_PRIMARY,
399                                               TRUE);
400
401                 /* attempt to get the toplevel for this widget */
402                 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
403                 if (gtk_widget_is_toplevel (window) && GTK_IS_WINDOW (window)) {
404                         /* unset the focus */
405                         gtk_window_set_focus (GTK_WINDOW (window), NULL);
406                 }
407
408                 /* see presence_chooser_entry_focus_out_cb ()
409                  * for what this does */
410                 if (priv->focus_out_idle_source != 0) {
411                         g_source_remove (priv->focus_out_idle_source);
412                         priv->focus_out_idle_source = 0;
413                 }
414
415                 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
416
417                 priv->editing_status = FALSE;
418         }
419 }
420
421 static void
422 mc_set_custom_state (EmpathyPresenceChooser *self)
423 {
424         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
425         GtkWidget *entry;
426         const char *status;
427
428         entry = gtk_bin_get_child (GTK_BIN (self));
429         /* update the status with MC */
430         status = gtk_entry_get_text (GTK_ENTRY (entry));
431
432         DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
433
434         empathy_idle_set_presence (priv->idle, priv->state, status);
435 }
436
437 static void
438 ui_set_custom_state (EmpathyPresenceChooser *self,
439                      TpConnectionPresenceType state,
440                      const char *status)
441 {
442         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
443         GtkWidget *entry;
444         const char *icon_name;
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         gtk_entry_set_text (GTK_ENTRY (entry), status == NULL ? "" : status);
456         presence_chooser_set_favorite_icon (self);
457
458         priv->block_changed--;
459         priv->block_set_editing--;
460 }
461
462 static void
463 presence_chooser_reset_status (EmpathyPresenceChooser *self)
464 {
465         /* recover the status that was unset */
466         presence_chooser_set_status_editing (self, FALSE);
467         presence_chooser_presence_changed_cb (self);
468 }
469
470 static void
471 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
472                                         GtkEntryIconPosition    icon_pos,
473                                         GdkEvent               *event,
474                                         GtkEntry               *entry)
475 {
476         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
477
478         if (priv->editing_status) {
479                 presence_chooser_set_status_editing (self, FALSE);
480                 mc_set_custom_state (self);
481         }
482         else {
483                 PresenceChooserEntryType type;
484                 TpConnectionPresenceType state;
485                 char *status;
486
487                 type = presence_chooser_get_entry_type (self);
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_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_Up || event->keyval == GDK_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_idle_set_presence (priv->idle, 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_account_manager_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_account_manager_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         GtkWidget *entry;
852         GtkCellRenderer *renderer;
853
854         chooser->priv = priv;
855
856         /* Create the not-favorite icon */
857         priv->not_favorite_pixbuf = create_not_favorite_pixbuf ();
858
859         empathy_signal_connect_weak (gtk_icon_theme_get_default (), "changed",
860                                      G_CALLBACK (icon_theme_changed_cb),
861                                      G_OBJECT (chooser));
862
863         presence_chooser_create_model (chooser);
864
865         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
866                                              COL_STATUS_TEXT);
867         gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
868                                               combo_row_separator_func,
869                                               NULL, NULL);
870
871         entry = gtk_bin_get_child (GTK_BIN (chooser));
872         gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
873                                         GTK_ENTRY_ICON_PRIMARY,
874                                         FALSE);
875
876         g_signal_connect_swapped (entry, "icon-release",
877                 G_CALLBACK (presence_chooser_entry_icon_release_cb),
878                 chooser);
879         g_signal_connect_swapped (entry, "activate",
880                 G_CALLBACK (presence_chooser_entry_activate_cb),
881                 chooser);
882         g_signal_connect_swapped (entry, "key-press-event",
883                 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
884                 chooser);
885         g_signal_connect_swapped (entry, "button-press-event",
886                 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
887                 chooser);
888
889         gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
890
891         renderer = gtk_cell_renderer_pixbuf_new ();
892         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
893         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
894                                         "icon-name", COL_STATE_ICON_NAME,
895                                         NULL);
896         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
897
898         renderer = gtk_cell_renderer_text_new ();
899         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
900         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
901                                         "markup", COL_DISPLAY_MARKUP,
902                                         NULL);
903         g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
904
905         g_signal_connect (chooser, "notify::popup-shown",
906                         G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
907         g_signal_connect (chooser, "changed",
908                         G_CALLBACK (presence_chooser_changed_cb), NULL);
909         g_signal_connect_swapped (entry, "changed",
910                         G_CALLBACK (presence_chooser_entry_changed_cb),
911                         chooser);
912         g_signal_connect_swapped (entry, "focus-out-event",
913                         G_CALLBACK (presence_chooser_entry_focus_out_cb),
914                         chooser);
915
916         priv->idle = empathy_idle_dup_singleton ();
917
918         priv->account_manager = tp_account_manager_dup ();
919         g_signal_connect_swapped (priv->account_manager,
920                 "most-available-presence-changed",
921                 G_CALLBACK (presence_chooser_presence_changed_cb),
922                 chooser);
923
924         empathy_signal_connect_weak (priv->account_manager, "account-validity-changed",
925                 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
926                 G_OBJECT (chooser));
927         empathy_signal_connect_weak (priv->account_manager, "account-removed",
928                 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
929                 G_OBJECT (chooser));
930         empathy_signal_connect_weak (priv->account_manager, "account-enabled",
931                 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
932                 G_OBJECT (chooser));
933         empathy_signal_connect_weak (priv->account_manager, "account-disabled",
934                 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
935                 G_OBJECT (chooser));
936
937         /* FIXME: this string sucks */
938         gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
939                 _("Set your presence and current status"));
940
941         priv->connectivity = empathy_connectivity_dup_singleton ();
942         empathy_signal_connect_weak (priv->connectivity,
943                 "state-change",
944                 G_CALLBACK (presence_chooser_connectivity_state_change),
945                 G_OBJECT (chooser));
946
947         presence_chooser_update_sensitivity (chooser);
948 }
949
950 static void
951 presence_chooser_finalize (GObject *object)
952 {
953         EmpathyPresenceChooserPriv *priv;
954
955         priv = GET_PRIV (object);
956
957         if (priv->focus_out_idle_source) {
958                 g_source_remove (priv->focus_out_idle_source);
959         }
960
961         if (priv->account_manager != NULL)
962                 g_object_unref (priv->account_manager);
963
964         g_signal_handlers_disconnect_by_func (priv->idle,
965                                               presence_chooser_presence_changed_cb,
966                                               object);
967         g_object_unref (priv->idle);
968
969         g_object_unref (priv->connectivity);
970         if (priv->not_favorite_pixbuf != NULL)
971                 g_object_unref (priv->not_favorite_pixbuf);
972
973         G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
974 }
975
976 /**
977  * empathy_presence_chooser_new:
978  *
979  * Creates a new #EmpathyPresenceChooser widget.
980  *
981  * Return value: A new #EmpathyPresenceChooser widget
982  */
983 GtkWidget *
984 empathy_presence_chooser_new (void)
985 {
986         GtkWidget *chooser;
987
988         chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
989
990         return chooser;
991 }
992
993 static void
994 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
995 {
996         EmpathyPresenceChooserPriv *priv;
997         TpConnectionPresenceType    state;
998         gchar                      *status;
999         GtkTreeModel               *model;
1000         GtkTreeIter                 iter;
1001         gboolean valid, match_state = FALSE, match = FALSE;
1002         GtkWidget                  *entry;
1003
1004         priv = GET_PRIV (chooser);
1005
1006         if (priv->editing_status) {
1007                 return;
1008         }
1009
1010         state = get_state_and_status (chooser, &status);
1011         priv->state = state;
1012
1013         /* An unset presence here doesn't make any sense. Force it to appear as
1014          * offline. */
1015         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
1016                 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
1017         }
1018
1019         /* look through the model and attempt to find a matching state */
1020         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
1021         for (valid = gtk_tree_model_get_iter_first (model, &iter);
1022              valid;
1023              valid = gtk_tree_model_iter_next (model, &iter)) {
1024                 int m_type;
1025                 TpConnectionPresenceType m_state;
1026                 char *m_status;
1027
1028                 gtk_tree_model_get (model, &iter,
1029                                 COL_STATE, &m_state,
1030                                 COL_TYPE, &m_type,
1031                                 -1);
1032
1033                 if (m_type == ENTRY_TYPE_CUSTOM ||
1034                     m_type == ENTRY_TYPE_SEPARATOR ||
1035                     m_type == ENTRY_TYPE_EDIT_CUSTOM) {
1036                         continue;
1037                 }
1038                 else if (!match_state && state == m_state) {
1039                         /* we are now in the section that can contain our
1040                          * match */
1041                         match_state = TRUE;
1042                 }
1043                 else if (match_state && state != m_state) {
1044                         /* we have passed the section that can contain our
1045                          * match */
1046                         break;
1047                 }
1048
1049                 gtk_tree_model_get (model, &iter,
1050                                 COL_STATUS_TEXT, &m_status,
1051                                 -1);
1052
1053                 match = !tp_strdiff (status, m_status);
1054
1055                 g_free (m_status);
1056
1057                 if (match) break;
1058
1059         }
1060
1061         if (match) {
1062                 priv->block_changed++;
1063                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
1064                 presence_chooser_set_favorite_icon (chooser);
1065                 priv->block_changed--;
1066         }
1067         else {
1068                 ui_set_custom_state (chooser, state, status);
1069         }
1070
1071         entry = gtk_bin_get_child (GTK_BIN (chooser));
1072         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1073               GTK_ENTRY_ICON_PRIMARY,
1074               empathy_icon_name_for_presence (state));
1075
1076         entry = gtk_bin_get_child (GTK_BIN (chooser));
1077         gtk_editable_set_editable (GTK_EDITABLE (entry),
1078             state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1079
1080         g_free (status);
1081 }
1082
1083 /**
1084  * empathy_presence_chooser_create_menu:
1085  *
1086  * Creates a new #GtkMenu allowing users to change their presence from a menu.
1087  *
1088  * Return value: a new #GtkMenu for changing presence in a menu.
1089  */
1090 GtkWidget *
1091 empathy_presence_chooser_create_menu (void)
1092 {
1093         const gchar *status;
1094         GtkWidget   *menu;
1095         GtkWidget   *item;
1096         GtkWidget   *image;
1097         guint        i;
1098
1099         menu = gtk_menu_new ();
1100
1101         for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1102                 GList       *list, *l;
1103
1104                 status = empathy_presence_get_default_message (states[i].state);
1105                 presence_chooser_menu_add_item (menu,
1106                                                 status,
1107                                                 states[i].state);
1108
1109                 if (states[i].customisable) {
1110                         /* Set custom messages if wanted */
1111                         list = empathy_status_presets_get (states[i].state, 5);
1112                         for (l = list; l; l = l->next) {
1113                                 presence_chooser_menu_add_item (menu,
1114                                                                 l->data,
1115                                                                 states[i].state);
1116                         }
1117                         g_list_free (list);
1118                 }
1119
1120         }
1121
1122         /* Separator */
1123         item = gtk_menu_item_new ();
1124         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1125         gtk_widget_show (item);
1126
1127         /* Custom messages */
1128         item = gtk_image_menu_item_new_with_label (_("Custom messagesā€¦"));
1129         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1130         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1131         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1132         gtk_widget_show (image);
1133         gtk_widget_show (item);
1134
1135         g_signal_connect (item,
1136                           "activate",
1137                           G_CALLBACK (presence_chooser_custom_activate_cb),
1138                           NULL);
1139
1140         return menu;
1141 }
1142
1143 static void
1144 presence_chooser_menu_add_item (GtkWidget   *menu,
1145                                 const gchar *str,
1146                                 TpConnectionPresenceType state)
1147 {
1148         GtkWidget   *item;
1149         GtkWidget   *image;
1150         const gchar *icon_name;
1151
1152         item = gtk_image_menu_item_new_with_label (str);
1153         icon_name = empathy_icon_name_for_presence (state);
1154
1155         g_signal_connect (item, "activate",
1156                           G_CALLBACK (presence_chooser_noncustom_activate_cb),
1157                           NULL);
1158
1159         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1160         gtk_widget_show (image);
1161
1162         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1163         gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1164         gtk_widget_show (item);
1165
1166         g_object_set_data_full (G_OBJECT (item),
1167                                 "status", g_strdup (str),
1168                                 (GDestroyNotify) g_free);
1169
1170         g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1171
1172         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1173 }
1174
1175 static void
1176 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1177                                         gpointer   user_data)
1178 {
1179         TpConnectionPresenceType state;
1180         const gchar *status;
1181
1182         status = g_object_get_data (G_OBJECT (item), "status");
1183         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1184
1185         presence_chooser_set_state (state, status);
1186 }
1187
1188 static void
1189 presence_chooser_set_state (TpConnectionPresenceType state,
1190                             const gchar *status)
1191 {
1192         EmpathyIdle *idle;
1193
1194         idle = empathy_idle_dup_singleton ();
1195         empathy_idle_set_presence (idle, state, status);
1196         g_object_unref (idle);
1197 }
1198
1199 static void
1200 presence_chooser_custom_activate_cb (GtkWidget *item,
1201                                      gpointer   user_data)
1202 {
1203         GtkWidget *dialog;
1204
1205         dialog = empathy_status_preset_dialog_new (NULL);
1206         gtk_dialog_run (GTK_DIALOG (dialog));
1207         gtk_widget_destroy (dialog);
1208 }