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