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