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