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