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