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