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