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