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