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