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