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