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