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