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