]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-presence-chooser.c
Port EmpathyPresenceChooser to new API
[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 void
250 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
251                                      gboolean editing)
252 {
253         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
254         GtkWidget *entry;
255
256         if (priv->block_set_editing) {
257                 return;
258         }
259
260         entry = gtk_bin_get_child (GTK_BIN (self));
261         if (editing) {
262                 priv->editing_status = TRUE;
263
264                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
265                                                GTK_ENTRY_ICON_SECONDARY,
266                                                GTK_STOCK_OK);
267                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
268                                                  GTK_ENTRY_ICON_SECONDARY,
269                                                  _("Set status"));
270                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
271                                               GTK_ENTRY_ICON_PRIMARY,
272                                               FALSE);
273         } else {
274                 GtkWidget *window;
275
276                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
277                                                GTK_ENTRY_ICON_SECONDARY,
278                                                NULL);
279                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
280                                                  GTK_ENTRY_ICON_SECONDARY,
281                                                  NULL);
282                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
283                                               GTK_ENTRY_ICON_PRIMARY,
284                                               TRUE);
285
286                 /* attempt to get the toplevel for this widget */
287                 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
288                 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
289                         /* unset the focus */
290                         gtk_window_set_focus (GTK_WINDOW (window), NULL);
291                 }
292
293                 /* see presence_chooser_entry_focus_out_cb()
294                  * for what this does */
295                 if (priv->focus_out_idle_source != 0) {
296                         g_source_remove (priv->focus_out_idle_source);
297                         priv->focus_out_idle_source = 0;
298                 }
299
300                 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
301
302                 priv->editing_status = FALSE;
303         }
304 }
305
306 static void
307 mc_set_custom_state (EmpathyPresenceChooser *self)
308 {
309         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
310         GtkWidget *entry;
311         const char *status;
312
313         entry = gtk_bin_get_child (GTK_BIN (self));
314         /* update the status with MC */
315         status = gtk_entry_get_text (GTK_ENTRY (entry));
316
317         DEBUG ("Sending state to MC-> %s (%s)\n",
318                 g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
319                         priv->state)->value_name,
320                 status);
321
322         empathy_idle_set_presence (priv->idle, priv->state, status);
323 }
324
325 static void
326 ui_set_custom_state (EmpathyPresenceChooser *self,
327                      McPresence state,
328                      const char *status)
329 {
330         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
331         GtkWidget *entry;
332         const char *icon_name;
333
334         entry = gtk_bin_get_child (GTK_BIN (self));
335
336         priv->block_set_editing++;
337         priv->block_changed++;
338
339         icon_name = empathy_icon_name_for_presence (state);
340         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
341                                            GTK_ENTRY_ICON_PRIMARY,
342                                            icon_name);
343         gtk_entry_set_text (GTK_ENTRY (entry), status);
344
345         priv->block_changed--;
346         priv->block_set_editing--;
347 }
348
349 static void
350 presence_chooser_reset_status (EmpathyPresenceChooser *self)
351 {
352         /* recover the status that was unset */
353         presence_chooser_set_status_editing (self, FALSE);
354         presence_chooser_presence_changed_cb (self);
355 }
356
357 static void
358 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
359                                         GtkEntryIconPosition    icon_pos,
360                                         GdkEvent               *event,
361                                         GtkEntry               *entry)
362 {
363         presence_chooser_set_status_editing (self, FALSE);
364         mc_set_custom_state (self);
365 }
366
367 static void
368 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
369                                     GtkEntry               *entry)
370 {
371         presence_chooser_set_status_editing (self, FALSE);
372         mc_set_custom_state (self);
373 }
374
375 static gboolean
376 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
377                                            GdkEventKey            *event,
378                                            GtkWidget              *entry)
379 {
380         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
381
382         if (priv->editing_status && event->keyval == GDK_Escape) {
383                 /* the user pressed Escape, undo the editing */
384                 presence_chooser_reset_status (self);
385                 return TRUE;
386         }
387         else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
388                 /* ignore */
389                 return TRUE;
390         }
391
392         return FALSE; /* send this event elsewhere */
393 }
394
395 static gboolean
396 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
397                                               GdkEventButton         *event,
398                                               GtkWidget              *entry)
399 {
400         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
401
402         if (!priv->editing_status &&
403             event->button == 1 &&
404             !GTK_WIDGET_HAS_FOCUS (entry)) {
405                 gtk_widget_grab_focus (entry);
406                 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
407
408                 return TRUE;
409         }
410
411         return FALSE;
412 }
413
414 static void
415 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
416                                    GtkEntry               *entry)
417 {
418         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
419
420         if (priv->block_changed){
421                 return;
422         }
423
424         /* the combo is being edited to a custom entry */
425         if (!priv->editing_status) {
426                 presence_chooser_set_status_editing (self, TRUE);
427         }
428 }
429
430 static void
431 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
432 {
433         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
434         GtkTreeIter iter;
435         char *icon_name;
436         McPresence new_state;
437         gboolean customisable = TRUE;
438         PresenceChooserEntryType type = -1;
439         GtkWidget *entry;
440         GtkTreeModel *model;
441
442         if (priv->block_changed ||
443             !gtk_combo_box_get_active_iter (self, &iter)) {
444                 return;
445         }
446
447         model = gtk_combo_box_get_model (self);
448         gtk_tree_model_get (model, &iter,
449                             COL_STATE_ICON_NAME, &icon_name,
450                             COL_STATE, &new_state,
451                             COL_STATUS_CUSTOMISABLE, &customisable,
452                             COL_TYPE, &type,
453                             -1);
454
455         entry = gtk_bin_get_child (GTK_BIN (self));
456
457         /* some types of status aren't editable, set the editability of the
458          * entry appropriately. Unless we're just about to reset it anyway,
459          * in which case, don't fiddle with it */
460         if (type != ENTRY_TYPE_EDIT_CUSTOM) {
461                 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
462                 priv->state = new_state;
463         }
464
465         if (type == ENTRY_TYPE_EDIT_CUSTOM) {
466                 GtkWidget *window;
467
468                 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
469
470                 /* attempt to get the toplevel for this widget */
471                 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
472                 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
473                         window = NULL;
474                 }
475
476                 presence_chooser_dialog_show (GTK_WINDOW (window));
477         }
478         else if (type == ENTRY_TYPE_CUSTOM) {
479                 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
480                                                    GTK_ENTRY_ICON_PRIMARY,
481                                                    icon_name);
482
483                 /* preseed the status */
484                 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
485                         /* if their previous entry was a builtin, don't
486                          * preseed */
487                         gtk_entry_set_text (GTK_ENTRY (entry), "");
488                 } else {
489                         /* else preseed the text of their currently entered
490                          * status message */
491                         const char *status;
492
493                         status = empathy_idle_get_status (priv->idle);
494                         gtk_entry_set_text (GTK_ENTRY (entry), status);
495                 }
496
497                 /* grab the focus */
498                 gtk_widget_grab_focus (entry);
499         } else {
500                 char *status;
501
502                 /* just in case we were setting a new status when
503                  * things were changed */
504                 presence_chooser_set_status_editing (
505                         EMPATHY_PRESENCE_CHOOSER (self),
506                         FALSE);
507
508                 gtk_tree_model_get (model, &iter,
509                                     COL_STATUS_TEXT, &status,
510                                     -1);
511
512                 empathy_idle_set_presence (priv->idle, priv->state, status);
513
514                 g_free (status);
515         }
516
517         if (type != ENTRY_TYPE_EDIT_CUSTOM) {
518                 priv->previous_type = type;
519         }
520         g_free (icon_name);
521 }
522
523 static gboolean
524 combo_row_separator_func (GtkTreeModel  *model,
525                           GtkTreeIter   *iter,
526                           gpointer       data)
527 {
528         PresenceChooserEntryType type;
529
530         gtk_tree_model_get (model, iter,
531                             COL_TYPE, &type,
532                             -1);
533
534         return (type == ENTRY_TYPE_SEPARATOR);
535 }
536
537 static gboolean
538 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
539 {
540         EmpathyPresenceChooser *chooser;
541         GtkWidget *entry;
542
543         DEBUG ("Autocommiting status message\n");
544
545         chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
546         entry = gtk_bin_get_child (GTK_BIN (chooser));
547
548         presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
549
550         return FALSE;
551 }
552
553 static gboolean
554 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
555                                      GdkEventFocus *event,
556                                      GtkEntry *entry)
557 {
558         EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
559
560         if (priv->editing_status) {
561                 /* this seems a bit evil and maybe it will be fragile,
562                  * someone should think of a better way to do it.
563                  *
564                  * The entry has focused out, but we don't know where the focus
565                  * has gone. If it goes to the combo box, we don't want to
566                  * do anything. If it's gone anywhere else, we want to commit
567                  * the result.
568                  *
569                  * Thus we install this idle handler and store its source.
570                  * If the source is scheduled when the popup handler runs,
571                  * it will remove it, else the callback will commit the result.
572                  */
573                 priv->focus_out_idle_source = g_idle_add (
574                         presence_chooser_entry_focus_out_idle_cb,
575                         chooser);
576         }
577
578         gtk_editable_set_position (GTK_EDITABLE (entry), 0);
579
580         return FALSE;
581 }
582
583 static void
584 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
585 {
586         EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
587                 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
588         GtkWidget *entry;
589         GtkCellRenderer *renderer;
590
591         chooser->priv = priv;
592
593         presence_chooser_create_model (chooser);
594
595         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
596                                              COL_STATUS_TEXT);
597         gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
598                                               combo_row_separator_func,
599                                               NULL, NULL);
600
601         entry = gtk_bin_get_child (GTK_BIN (chooser));
602         gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
603                                         GTK_ENTRY_ICON_PRIMARY,
604                                         FALSE);
605
606         g_signal_connect_swapped (entry, "icon-release",
607                 G_CALLBACK (presence_chooser_entry_icon_release_cb),
608                 chooser);
609         g_signal_connect_swapped (entry, "activate",
610                 G_CALLBACK (presence_chooser_entry_activate_cb),
611                 chooser);
612         g_signal_connect_swapped (entry, "key-press-event",
613                 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
614                 chooser);
615         g_signal_connect_swapped (entry, "button-press-event",
616                 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
617                 chooser);
618
619         gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
620
621         renderer = gtk_cell_renderer_pixbuf_new ();
622         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
623         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
624                                         "icon-name", COL_STATE_ICON_NAME,
625                                         NULL);
626         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
627
628         renderer = gtk_cell_renderer_text_new ();
629         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
630         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
631                                         "markup", COL_DISPLAY_MARKUP,
632                                         NULL);
633
634         g_signal_connect (chooser, "notify::popup-shown",
635                         G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
636         g_signal_connect (chooser, "changed",
637                         G_CALLBACK (presence_chooser_changed_cb), NULL);
638         g_signal_connect_swapped (entry, "changed",
639                         G_CALLBACK (presence_chooser_entry_changed_cb),
640                         chooser);
641         g_signal_connect_swapped (entry, "focus-out-event",
642                         G_CALLBACK (presence_chooser_entry_focus_out_cb),
643                         chooser);
644
645         priv->idle = empathy_idle_dup_singleton ();
646         presence_chooser_presence_changed_cb (chooser);
647         g_signal_connect_swapped (priv->idle, "notify",
648                 G_CALLBACK (presence_chooser_presence_changed_cb),
649                 chooser);
650
651         /* FIXME: this string sucks */
652         gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
653                 _("Set your presence and current status"));
654 }
655
656 static void
657 presence_chooser_finalize (GObject *object)
658 {
659         EmpathyPresenceChooserPriv *priv;
660
661         priv = GET_PRIV (object);
662
663         if (priv->flash_timeout_id) {
664                 g_source_remove (priv->flash_timeout_id);
665         }
666
667         if (priv->focus_out_idle_source) {
668                 g_source_remove (priv->focus_out_idle_source);
669         }
670
671         g_signal_handlers_disconnect_by_func (priv->idle,
672                                               presence_chooser_presence_changed_cb,
673                                               object);
674         g_object_unref (priv->idle);
675
676         G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
677 }
678
679 GtkWidget *
680 empathy_presence_chooser_new (void)
681 {
682         GtkWidget *chooser;
683
684         chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
685
686         return chooser;
687 }
688
689 static void
690 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
691 {
692         EmpathyPresenceChooserPriv *priv;
693         McPresence                  state;
694         McPresence                  flash_state;
695         const gchar                *status;
696         GtkTreeModel               *model;
697         GtkTreeIter                 iter;
698         gboolean valid, match_state = FALSE, match = FALSE;
699
700         priv = GET_PRIV (chooser);
701
702         if (priv->editing_status) {
703                 return;
704         }
705
706         priv->state = state = empathy_idle_get_state (priv->idle);
707         status = empathy_idle_get_status (priv->idle);
708         flash_state = empathy_idle_get_flash_state (priv->idle);
709
710         /* look through the model and attempt to find a matching state */
711         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
712         for (valid = gtk_tree_model_get_iter_first (model, &iter);
713              valid;
714              valid = gtk_tree_model_iter_next (model, &iter)) {
715                 int m_type;
716                 McPresence m_state;
717                 char *m_status;
718
719                 gtk_tree_model_get (model, &iter,
720                                 COL_STATE, &m_state,
721                                 COL_TYPE, &m_type,
722                                 -1);
723
724                 if (m_type == ENTRY_TYPE_CUSTOM ||
725                     m_type == ENTRY_TYPE_SEPARATOR ||
726                     m_type == ENTRY_TYPE_EDIT_CUSTOM) {
727                         continue;
728                 }
729                 else if (!match_state && state == m_state) {
730                         /* we are now in the section that can contain our
731                          * match */
732                         match_state = TRUE;
733                 }
734                 else if (match_state && state != m_state) {
735                         /* we have passed the section that can contain our
736                          * match */
737                         break;
738                 }
739
740                 gtk_tree_model_get (model, &iter,
741                                 COL_STATUS_TEXT, &m_status,
742                                 -1);
743
744                 match = !strcmp (status, m_status);
745
746                 g_free (m_status);
747
748                 if (match) break;
749
750         }
751
752         if (match) {
753                 priv->block_changed++;
754                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
755                 priv->block_changed--;
756         }
757         else {
758                 ui_set_custom_state (chooser, state, status);
759         }
760
761         if (flash_state != MC_PRESENCE_UNSET) {
762                 presence_chooser_flash_start (chooser, state, flash_state);
763         }
764         else {
765                 presence_chooser_flash_stop (chooser, state);
766         }
767 }
768
769 static gboolean
770 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
771 {
772         EmpathyPresenceChooserPriv *priv;
773         McPresence                  state;
774         static gboolean             on = FALSE;
775         GtkWidget                  *entry;
776
777         priv = GET_PRIV (chooser);
778
779         if (on) {
780                 state = priv->flash_state_1;
781         }
782         else {
783                 state = priv->flash_state_2;
784         }
785
786         entry = gtk_bin_get_child (GTK_BIN (chooser));
787         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
788                                            GTK_ENTRY_ICON_PRIMARY,
789                                            empathy_icon_name_for_presence (state));
790
791         on = !on;
792
793         return TRUE;
794 }
795
796 static void
797 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
798                               McPresence              state_1,
799                               McPresence              state_2)
800 {
801         EmpathyPresenceChooserPriv *priv;
802
803         g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
804
805         priv = GET_PRIV (chooser);
806
807         priv->flash_state_1 = state_1;
808         priv->flash_state_2 = state_2;
809
810         if (!priv->flash_timeout_id) {
811                 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
812                         (GSourceFunc) presence_chooser_flash_timeout_cb,
813                         chooser);
814         }
815 }
816
817 static void
818 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
819                              McPresence             state)
820 {
821         EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
822         GtkWidget *entry;
823
824         if (priv->flash_timeout_id) {
825                 g_source_remove (priv->flash_timeout_id);
826                 priv->flash_timeout_id = 0;
827         }
828
829         entry = gtk_bin_get_child (GTK_BIN (chooser));
830         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
831                                            GTK_ENTRY_ICON_PRIMARY,
832                                            empathy_icon_name_for_presence (state));
833 }
834
835 GtkWidget *
836 empathy_presence_chooser_create_menu (void)
837 {
838         const gchar *status;
839         GtkWidget   *menu;
840         GtkWidget   *item;
841         GtkWidget   *image;
842         guint        i;
843
844         menu = gtk_menu_new ();
845
846         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
847                 GList       *list, *l;
848
849                 status = empathy_presence_get_default_message (states[i]);
850                 presence_chooser_menu_add_item (menu,
851                                                 status,
852                                                 states[i]);
853
854                 if (states[i+1]) {
855                         /* Set custom messages if wanted */
856                         list = empathy_status_presets_get (states[i], 5);
857                         for (l = list; l; l = l->next) {
858                                 presence_chooser_menu_add_item (menu,
859                                                                 l->data,
860                                                                 states[i]);
861                         }
862                         g_list_free (list);
863                 }
864
865         }
866
867         /* Separator */
868         item = gtk_menu_item_new ();
869         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
870         gtk_widget_show (item);
871
872         /* Custom messages */
873         item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
874         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
875         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
876         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
877         gtk_widget_show (image);
878         gtk_widget_show (item);
879
880         g_signal_connect (item,
881                           "activate",
882                           G_CALLBACK (presence_chooser_custom_activate_cb),
883                           NULL);
884
885         return menu;
886 }
887
888 static void
889 presence_chooser_menu_add_item (GtkWidget   *menu,
890                                 const gchar *str,
891                                 McPresence   state)
892 {
893         GtkWidget   *item;
894         GtkWidget   *image;
895         const gchar *icon_name;
896
897         item = gtk_image_menu_item_new_with_label (str);
898         icon_name = empathy_icon_name_for_presence (state);
899
900         g_signal_connect (item, "activate",
901                           G_CALLBACK (presence_chooser_noncustom_activate_cb),
902                           NULL);
903
904         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
905         gtk_widget_show (image);
906
907         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
908         gtk_widget_show (item);
909
910         g_object_set_data_full (G_OBJECT (item),
911                                 "status", g_strdup (str),
912                                 (GDestroyNotify) g_free);
913
914         g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
915
916         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
917 }
918
919 static void
920 presence_chooser_noncustom_activate_cb (GtkWidget *item,
921                                         gpointer   user_data)
922 {
923         McPresence   state;
924         const gchar *status;
925
926         status = g_object_get_data (G_OBJECT (item), "status");
927         state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
928
929         presence_chooser_set_state (state, status);
930 }
931
932 static void
933 presence_chooser_set_state (McPresence   state,
934                             const gchar *status)
935 {
936         EmpathyIdle *idle;
937
938         idle = empathy_idle_dup_singleton ();
939         empathy_idle_set_presence (idle, state, status);
940         g_object_unref (idle);
941 }
942
943 static void
944 presence_chooser_custom_activate_cb (GtkWidget *item,
945                                      gpointer   user_data)
946 {
947         presence_chooser_dialog_show (NULL);
948 }
949
950 static McPresence
951 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
952 {
953         GtkTreeModel *model;
954         GtkTreeIter   iter;
955         McPresence    presence = LAST_MC_PRESENCE;
956
957         model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
958         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
959                 gtk_tree_model_get (model, &iter,
960                                     COL_PRESENCE, &presence,
961                                     -1);
962         }
963
964         return presence;
965 }
966
967 static void
968 presence_chooser_dialog_status_changed_cb (GtkWidget           *widget,
969                                            CustomMessageDialog *dialog)
970 {
971         GtkListStore *store;
972         GtkTreeIter   iter;
973         McPresence    presence = LAST_MC_PRESENCE;
974         GList        *messages, *l;
975
976         presence = presence_chooser_dialog_get_selected (dialog);
977
978         store = gtk_list_store_new (1, G_TYPE_STRING);
979         messages = empathy_status_presets_get (presence, -1);
980         for (l = messages; l; l = l->next) {
981                 gtk_list_store_append (store, &iter);
982                 gtk_list_store_set (store, &iter, 0, l->data, -1);
983         }
984
985         gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
986                             messages ? messages->data : "");
987
988         g_list_free (messages);
989
990         gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
991                                  GTK_TREE_MODEL (store));
992
993         g_object_unref (store);
994 }
995
996 static void
997 presence_chooser_dialog_message_changed_cb (GtkWidget           *widget,
998                                             CustomMessageDialog *dialog)
999 {
1000         McPresence   presence;
1001         GList       *messages, *l;
1002         const gchar *text;
1003         gboolean     found = FALSE;
1004
1005         presence = presence_chooser_dialog_get_selected (dialog);
1006         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1007
1008         messages = empathy_status_presets_get (presence, -1);
1009         for (l = messages; l; l = l->next) {
1010                 if (!tp_strdiff (text, l->data)) {
1011                         found = TRUE;
1012                         break;
1013                 }
1014         }
1015         g_list_free (messages);
1016
1017         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
1018                                       found);
1019 }
1020
1021 static void
1022 presence_chooser_dialog_save_toggled_cb (GtkWidget           *widget,
1023                                          CustomMessageDialog *dialog)
1024 {
1025         gboolean     active;
1026         McPresence   state;
1027         const gchar *text;
1028
1029         active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
1030         state = presence_chooser_dialog_get_selected (dialog);
1031         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1032
1033         if (active) {
1034                 empathy_status_presets_set_last (state, text);
1035         } else {
1036                 empathy_status_presets_remove (state, text);
1037         }
1038 }
1039
1040 static void
1041 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
1042 {
1043         GtkListStore    *store;
1044         GtkCellRenderer *renderer;
1045         GtkTreeIter      iter;
1046         guint            i;
1047
1048         store = gtk_list_store_new (COL_COUNT,
1049                                     G_TYPE_STRING,     /* Icon name */
1050                                     G_TYPE_STRING,     /* Label     */
1051                                     MC_TYPE_PRESENCE); /* Presence   */
1052         gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1053                                  GTK_TREE_MODEL (store));
1054
1055         renderer = gtk_cell_renderer_pixbuf_new ();
1056         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1057         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1058                                         "icon-name", COL_ICON,
1059                                         NULL);
1060         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1061
1062         renderer = gtk_cell_renderer_text_new ();
1063         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1064         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1065                                         "text", COL_LABEL,
1066                                         NULL);
1067
1068         for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1069                 if (!states[i+1]) {
1070                         continue;
1071                 }
1072
1073                 gtk_list_store_append (store, &iter);
1074                 gtk_list_store_set (store, &iter,
1075                                     COL_ICON, empathy_icon_name_for_presence (states[i]),
1076                                     COL_LABEL, empathy_presence_get_default_message (states[i]),
1077                                     COL_PRESENCE, states[i],
1078                                     -1);
1079         }
1080
1081         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1082 }
1083
1084 static void
1085 presence_chooser_dialog_response_cb (GtkWidget           *widget,
1086                                      gint                 response,
1087                                      CustomMessageDialog *dialog)
1088 {
1089         if (response == GTK_RESPONSE_APPLY) {
1090                 McPresence   state;
1091                 const gchar *text;
1092
1093                 state = presence_chooser_dialog_get_selected (dialog);
1094                 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1095
1096                 presence_chooser_set_state (state, text);
1097         }
1098
1099         gtk_widget_destroy (widget);
1100 }
1101
1102 static void
1103 presence_chooser_dialog_destroy_cb (GtkWidget           *widget,
1104                                     CustomMessageDialog *dialog)
1105 {
1106
1107         g_free (dialog);
1108         message_dialog = NULL;
1109 }
1110
1111 static void
1112 presence_chooser_dialog_show (GtkWindow *parent)
1113 {
1114         GtkBuilder *gui;
1115         gchar      *filename;
1116
1117         if (message_dialog) {
1118                 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1119                 return;
1120         }
1121
1122         message_dialog = g_new0 (CustomMessageDialog, 1);
1123
1124         filename = empathy_file_lookup ("empathy-presence-chooser.ui",
1125                                         "libempathy-gtk");
1126         gui = empathy_builder_get_file (filename,
1127                                        "custom_message_dialog", &message_dialog->dialog,
1128                                        "checkbutton_save", &message_dialog->checkbutton_save,
1129                                        "comboboxentry_message", &message_dialog->comboboxentry_message,
1130                                        "combobox_status", &message_dialog->combobox_status,
1131                                        NULL);
1132         g_free (filename);
1133
1134         empathy_builder_connect (gui, message_dialog,
1135                                "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1136                                "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1137                                "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1138                                "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1139                                NULL);
1140
1141         g_object_unref (gui);
1142
1143         /* Setup the message combobox */
1144         message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1145         gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1146         gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1147         g_signal_connect (message_dialog->entry_message, "changed",
1148                           G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1149                           message_dialog);
1150
1151         presence_chooser_dialog_setup (message_dialog);
1152
1153         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1154
1155         if (parent) {
1156                 gtk_window_set_transient_for (
1157                         GTK_WINDOW (message_dialog->dialog),
1158                         parent);
1159         }
1160
1161         gtk_widget_show_all (message_dialog->dialog);
1162 }
1163