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