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