]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-presence-chooser.c
Updated Oriya Translation
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  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
38 #include <libempathy/empathy-idle.h>
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-status-presets.h>
41
42 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
43 #include <libempathy/empathy-debug.h>
44
45 #include "empathy-ui-utils.h"
46 #include "empathy-images.h"
47 #include "empathy-presence-chooser.h"
48 #include "empathy-status-preset-dialog.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_STATUS_TEXT,
83         COL_STATE_ICON_NAME,
84         COL_STATE,
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         TpConnectionPresenceType state;
108         PresenceChooserEntryType previous_type;
109
110         TpConnectionPresenceType   flash_state_1;
111         TpConnectionPresenceType   flash_state_2;
112         guint        flash_timeout_id;
113 } EmpathyPresenceChooserPriv;
114
115 /* States to be listed in the menu.
116  * Each state has a boolean telling if it can have custom message */
117 static struct { TpConnectionPresenceType state;
118          gboolean customisable;
119 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
120                          { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
121                          { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
122                          { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
123                          { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
124                          { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
125                         };
126
127 static void            presence_chooser_finalize               (GObject                    *object);
128 static void            presence_chooser_presence_changed_cb    (EmpathyPresenceChooser      *chooser);
129 static gboolean        presence_chooser_flash_timeout_cb       (EmpathyPresenceChooser      *chooser);
130 static void            presence_chooser_flash_start            (EmpathyPresenceChooser      *chooser,
131                                                                 TpConnectionPresenceType                  state_1,
132                                                                 TpConnectionPresenceType                  state_2);
133 static void            presence_chooser_flash_stop             (EmpathyPresenceChooser      *chooser,
134                                                                 TpConnectionPresenceType                  state);
135 static void            presence_chooser_menu_add_item          (GtkWidget                  *menu,
136                                                                 const gchar                *str,
137                                                                 TpConnectionPresenceType                  state);
138 static void            presence_chooser_noncustom_activate_cb  (GtkWidget                  *item,
139                                                                 gpointer                    user_data);
140 static void            presence_chooser_set_state              (TpConnectionPresenceType                  state,
141                                                                 const gchar                *status);
142 static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
143                                                                 gpointer                    user_data);
144
145 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
146
147 static void
148 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
149 {
150         GObjectClass *object_class = G_OBJECT_CLASS (klass);
151
152         object_class->finalize = presence_chooser_finalize;
153
154         g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
155 }
156
157 static void
158 presence_chooser_create_model (EmpathyPresenceChooser *self)
159 {
160         GtkListStore *store;
161         char *custom_message;
162         int i;
163
164         store = gtk_list_store_new (N_COLUMNS,
165                                     G_TYPE_STRING,    /* COL_STATUS_TEXT */
166                                     G_TYPE_STRING,    /* COL_STATE_ICON_NAME */
167                                     G_TYPE_UINT,      /* COL_STATE */
168                                     G_TYPE_STRING,    /* COL_DISPLAY_MARKUP */
169                                     G_TYPE_BOOLEAN,   /* COL_STATUS_CUSTOMISABLE */
170                                     G_TYPE_INT);      /* COL_TYPE */
171
172         custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
173
174         for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
175                 GList       *list, *l;
176                 const char *status, *icon_name;
177
178                 status = empathy_presence_get_default_message (states[i].state);
179                 icon_name = empathy_icon_name_for_presence (states[i].state);
180
181                 gtk_list_store_insert_with_values (store, NULL, -1,
182                         COL_STATUS_TEXT, status,
183                         COL_STATE_ICON_NAME, icon_name,
184                         COL_STATE, states[i].state,
185                         COL_DISPLAY_MARKUP, status,
186                         COL_STATUS_CUSTOMISABLE, states[i].customisable,
187                         COL_TYPE, ENTRY_TYPE_BUILTIN,
188                         -1);
189
190                 if (states[i].customisable) {
191                         /* Set custom messages if wanted */
192                         list = empathy_status_presets_get (states[i].state, -1);
193                         list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
194                         for (l = list; l; l = l->next) {
195                                 gtk_list_store_insert_with_values (store,
196                                         NULL, -1,
197                                         COL_STATUS_TEXT, l->data,
198                                         COL_STATE_ICON_NAME, icon_name,
199                                         COL_STATE, states[i].state,
200                                         COL_DISPLAY_MARKUP, l->data,
201                                         COL_STATUS_CUSTOMISABLE, TRUE,
202                                         COL_TYPE, ENTRY_TYPE_SAVED,
203                                         -1);
204                         }
205                         g_list_free (list);
206
207                         gtk_list_store_insert_with_values (store, NULL, -1,
208                                 COL_STATUS_TEXT, _("Custom Message..."),
209                                 COL_STATE_ICON_NAME, icon_name,
210                                 COL_STATE, states[i].state,
211                                 COL_DISPLAY_MARKUP, custom_message,
212                                 COL_STATUS_CUSTOMISABLE, TRUE,
213                                 COL_TYPE, ENTRY_TYPE_CUSTOM,
214                                 -1);
215                 }
216
217         }
218
219         /* add a separator */
220         gtk_list_store_insert_with_values (store, NULL, -1,
221                         COL_TYPE, ENTRY_TYPE_SEPARATOR,
222                         -1);
223
224         gtk_list_store_insert_with_values (store, NULL, -1,
225                 COL_STATUS_TEXT, _("Edit Custom Messages..."),
226                 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
227                 COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
228                 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
229                 -1);
230
231         g_free (custom_message);
232
233         gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
234         g_object_unref (store);
235 }
236
237 static void
238 presence_chooser_popup_shown_cb (GObject *self,
239                                  GParamSpec *pspec,
240                                  gpointer user_data)
241 {
242         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
243         gboolean shown;
244
245         g_object_get (self, "popup-shown", &shown, NULL);
246         if (!shown) {
247                 return;
248         }
249
250         /* see presence_chooser_entry_focus_out_cb () for what this does */
251         if (priv->focus_out_idle_source != 0) {
252                 g_source_remove (priv->focus_out_idle_source);
253                 priv->focus_out_idle_source = 0;
254         }
255
256         presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
257 }
258
259 static PresenceChooserEntryType
260 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
261 {
262         GtkTreeIter iter;
263         PresenceChooserEntryType type = -1;
264
265         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
266                 type = ENTRY_TYPE_CUSTOM;
267         }
268         else {
269                 GtkTreeModel *model;
270
271                 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
272                 gtk_tree_model_get (model, &iter,
273                                     COL_TYPE, &type,
274                                     -1);
275         }
276
277         return type;
278 }
279
280 static gboolean
281 presence_chooser_is_preset (EmpathyPresenceChooser *self)
282 {
283         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
284         TpConnectionPresenceType state;
285         const char *status;
286         GList *presets, *l;
287         gboolean match = FALSE;
288
289         state = empathy_idle_get_state (priv->idle);
290         status = empathy_idle_get_status (priv->idle);
291
292         presets = empathy_status_presets_get (state, -1);
293         for (l = presets; l; l = l->next) {
294                 char *preset = (char *) l->data;
295
296                 if (!tp_strdiff (status, preset)) {
297                         match = TRUE;
298                         break;
299                 }
300         }
301
302         g_list_free (presets);
303
304         DEBUG ("is_preset(%i, %s) = %i\n", state, status, match);
305
306         return match;
307 }
308
309 static void
310 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
311 {
312         GtkWidget *entry;
313         PresenceChooserEntryType type;
314
315         entry = gtk_bin_get_child (GTK_BIN (self));
316         type = presence_chooser_get_entry_type (self);
317
318         if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
319                 if (presence_chooser_is_preset (self)) {
320                         /* saved entries can be removed from the list */
321                         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
322                                            GTK_ENTRY_ICON_SECONDARY,
323                                            "empathy-starred");
324                         gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
325                                          GTK_ENTRY_ICON_SECONDARY,
326                                          _("Click to remove this status as a favorite"));
327                 }
328                 else {
329                         /* custom entries can be favorited */
330                         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
331                                            GTK_ENTRY_ICON_SECONDARY,
332                                            "empathy-unstarred");
333                         gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
334                                          GTK_ENTRY_ICON_SECONDARY,
335                                          _("Click to make this status a favorite"));
336                 }
337         }
338         else {
339                 /* built-in entries cannot be favorited */
340                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
341                                            GTK_ENTRY_ICON_SECONDARY,
342                                            NULL);
343                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
344                                          GTK_ENTRY_ICON_SECONDARY,
345                                          NULL);
346         }
347 }
348
349 static void
350 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
351                                      gboolean editing)
352 {
353         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
354         GtkWidget *entry;
355
356         if (priv->block_set_editing) {
357                 return;
358         }
359
360         entry = gtk_bin_get_child (GTK_BIN (self));
361         if (editing) {
362                 priv->editing_status = TRUE;
363
364                 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
365                                                GTK_ENTRY_ICON_SECONDARY,
366                                                GTK_STOCK_OK);
367                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
368                                                  GTK_ENTRY_ICON_SECONDARY,
369                                                  _("Set status"));
370                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
371                                               GTK_ENTRY_ICON_PRIMARY,
372                                               FALSE);
373         } else {
374                 GtkWidget *window;
375
376                 presence_chooser_set_favorite_icon (self);
377                 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
378                                               GTK_ENTRY_ICON_PRIMARY,
379                                               TRUE);
380
381                 /* attempt to get the toplevel for this widget */
382                 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
383                 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
384                         /* unset the focus */
385                         gtk_window_set_focus (GTK_WINDOW (window), NULL);
386                 }
387
388                 /* see presence_chooser_entry_focus_out_cb ()
389                  * for what this does */
390                 if (priv->focus_out_idle_source != 0) {
391                         g_source_remove (priv->focus_out_idle_source);
392                         priv->focus_out_idle_source = 0;
393                 }
394
395                 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
396
397                 priv->editing_status = FALSE;
398         }
399 }
400
401 static void
402 mc_set_custom_state (EmpathyPresenceChooser *self)
403 {
404         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
405         GtkWidget *entry;
406         const char *status;
407
408         entry = gtk_bin_get_child (GTK_BIN (self));
409         /* update the status with MC */
410         status = gtk_entry_get_text (GTK_ENTRY (entry));
411
412         DEBUG ("Sending state to MC-> %d (%s)\n", priv->state, status);
413
414         empathy_idle_set_presence (priv->idle, priv->state, status);
415 }
416
417 static void
418 ui_set_custom_state (EmpathyPresenceChooser *self,
419                      TpConnectionPresenceType state,
420                      const char *status)
421 {
422         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
423         GtkWidget *entry;
424         const char *icon_name;
425
426         entry = gtk_bin_get_child (GTK_BIN (self));
427
428         priv->block_set_editing++;
429         priv->block_changed++;
430
431         icon_name = empathy_icon_name_for_presence (state);
432         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
433                                            GTK_ENTRY_ICON_PRIMARY,
434                                            icon_name);
435         gtk_entry_set_text (GTK_ENTRY (entry), status == NULL ? "" : status);
436         presence_chooser_set_favorite_icon (self);
437
438         priv->block_changed--;
439         priv->block_set_editing--;
440 }
441
442 static void
443 presence_chooser_reset_status (EmpathyPresenceChooser *self)
444 {
445         /* recover the status that was unset */
446         presence_chooser_set_status_editing (self, FALSE);
447         presence_chooser_presence_changed_cb (self);
448 }
449
450 static void
451 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
452                                         GtkEntryIconPosition    icon_pos,
453                                         GdkEvent               *event,
454                                         GtkEntry               *entry)
455 {
456         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
457
458         if (priv->editing_status) {
459                 presence_chooser_set_status_editing (self, FALSE);
460                 mc_set_custom_state (self);
461         }
462         else {
463                 PresenceChooserEntryType type;
464                 TpConnectionPresenceType state;
465                 const char *status;
466
467                 type = presence_chooser_get_entry_type (self);
468                 state = empathy_idle_get_state (priv->idle);
469                 status = empathy_idle_get_status (priv->idle);
470
471                 if (!empathy_status_presets_is_valid (state))
472                         /* It doesn't make sense to add such presence as favorite */
473                         return;
474
475                 if (presence_chooser_is_preset (self)) {
476                         /* remove the entry */
477                         DEBUG ("REMOVING PRESET (%i, %s)\n", state, status);
478                         empathy_status_presets_remove (state, status);
479                 }
480                 else {
481                         /* save the entry */
482                         DEBUG ("SAVING PRESET (%i, %s)\n", state, status);
483                         empathy_status_presets_set_last (state, status);
484                 }
485
486                 /* update the icon */
487                 presence_chooser_set_favorite_icon (self);
488         }
489 }
490
491 static void
492 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
493                                     GtkEntry               *entry)
494 {
495         presence_chooser_set_status_editing (self, FALSE);
496         mc_set_custom_state (self);
497 }
498
499 static gboolean
500 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
501                                            GdkEventKey            *event,
502                                            GtkWidget              *entry)
503 {
504         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
505
506         if (priv->editing_status && event->keyval == GDK_Escape) {
507                 /* the user pressed Escape, undo the editing */
508                 presence_chooser_reset_status (self);
509                 return TRUE;
510         }
511         else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
512                 /* ignore */
513                 return TRUE;
514         }
515
516         return FALSE; /* send this event elsewhere */
517 }
518
519 static gboolean
520 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
521                                               GdkEventButton         *event,
522                                               GtkWidget              *entry)
523 {
524         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
525
526         if (!priv->editing_status &&
527             event->button == 1 &&
528             !GTK_WIDGET_HAS_FOCUS (entry)) {
529                 gtk_widget_grab_focus (entry);
530                 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
531
532                 return TRUE;
533         }
534
535         return FALSE;
536 }
537
538 static void
539 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
540                                    GtkEntry               *entry)
541 {
542         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
543
544         if (priv->block_changed){
545                 return;
546         }
547
548         /* the combo is being edited to a custom entry */
549         if (!priv->editing_status) {
550                 presence_chooser_set_status_editing (self, TRUE);
551         }
552 }
553
554 static void
555 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
556 {
557         EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
558         GtkTreeIter iter;
559         char *icon_name;
560         TpConnectionPresenceType new_state;
561         gboolean customisable = TRUE;
562         PresenceChooserEntryType type = -1;
563         GtkWidget *entry;
564         GtkTreeModel *model;
565
566         if (priv->block_changed ||
567             !gtk_combo_box_get_active_iter (self, &iter)) {
568                 return;
569         }
570
571         model = gtk_combo_box_get_model (self);
572         gtk_tree_model_get (model, &iter,
573                             COL_STATE_ICON_NAME, &icon_name,
574                             COL_STATE, &new_state,
575                             COL_STATUS_CUSTOMISABLE, &customisable,
576                             COL_TYPE, &type,
577                             -1);
578
579         entry = gtk_bin_get_child (GTK_BIN (self));
580
581         /* some types of status aren't editable, set the editability of the
582          * entry appropriately. Unless we're just about to reset it anyway,
583          * in which case, don't fiddle with it */
584         if (type != ENTRY_TYPE_EDIT_CUSTOM) {
585                 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
586                 priv->state = new_state;
587         }
588
589         if (type == ENTRY_TYPE_EDIT_CUSTOM) {
590                 GtkWidget *window, *dialog;
591
592                 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
593
594                 /* attempt to get the toplevel for this widget */
595                 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
596                 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
597                         window = NULL;
598                 }
599
600                 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
601                 gtk_dialog_run (GTK_DIALOG (dialog));
602                 gtk_widget_destroy (dialog);
603         }
604         else if (type == ENTRY_TYPE_CUSTOM) {
605                 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
606                                                    GTK_ENTRY_ICON_PRIMARY,
607                                                    icon_name);
608
609                 /* preseed the status */
610                 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
611                         /* if their previous entry was a builtin, don't
612                          * preseed */
613                         gtk_entry_set_text (GTK_ENTRY (entry), "");
614                 } else {
615                         /* else preseed the text of their currently entered
616                          * status message */
617                         const char *status;
618
619                         status = empathy_idle_get_status (priv->idle);
620                         gtk_entry_set_text (GTK_ENTRY (entry), status);
621                 }
622
623                 /* grab the focus */
624                 gtk_widget_grab_focus (entry);
625         } else {
626                 char *status;
627
628                 /* just in case we were setting a new status when
629                  * things were changed */
630                 presence_chooser_set_status_editing (
631                         EMPATHY_PRESENCE_CHOOSER (self),
632                         FALSE);
633                 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
634                                            GTK_ENTRY_ICON_PRIMARY,
635                                            icon_name);
636
637                 gtk_tree_model_get (model, &iter,
638                                     COL_STATUS_TEXT, &status,
639                                     -1);
640
641                 empathy_idle_set_presence (priv->idle, priv->state, status);
642
643                 g_free (status);
644         }
645
646         if (type != ENTRY_TYPE_EDIT_CUSTOM) {
647                 priv->previous_type = type;
648         }
649         g_free (icon_name);
650 }
651
652 static gboolean
653 combo_row_separator_func (GtkTreeModel  *model,
654                           GtkTreeIter   *iter,
655                           gpointer       data)
656 {
657         PresenceChooserEntryType type;
658
659         gtk_tree_model_get (model, iter,
660                             COL_TYPE, &type,
661                             -1);
662
663         return (type == ENTRY_TYPE_SEPARATOR);
664 }
665
666 static gboolean
667 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
668 {
669         EmpathyPresenceChooser *chooser;
670         GtkWidget *entry;
671
672         DEBUG ("Autocommiting status message\n");
673
674         chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
675         entry = gtk_bin_get_child (GTK_BIN (chooser));
676
677         presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
678
679         return FALSE;
680 }
681
682 static gboolean
683 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
684                                      GdkEventFocus *event,
685                                      GtkEntry *entry)
686 {
687         EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
688
689         if (priv->editing_status) {
690                 /* this seems a bit evil and maybe it will be fragile,
691                  * someone should think of a better way to do it.
692                  *
693                  * The entry has focused out, but we don't know where the focus
694                  * has gone. If it goes to the combo box, we don't want to
695                  * do anything. If it's gone anywhere else, we want to commit
696                  * the result.
697                  *
698                  * Thus we install this idle handler and store its source.
699                  * If the source is scheduled when the popup handler runs,
700                  * it will remove it, else the callback will commit the result.
701                  */
702                 priv->focus_out_idle_source = g_idle_add (
703                         presence_chooser_entry_focus_out_idle_cb,
704                         chooser);
705         }
706
707         gtk_editable_set_position (GTK_EDITABLE (entry), 0);
708
709         return FALSE;
710 }
711
712 static void
713 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
714 {
715         EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
716                 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
717         GtkWidget *entry;
718         GtkCellRenderer *renderer;
719
720         chooser->priv = priv;
721
722         presence_chooser_create_model (chooser);
723
724         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
725                                              COL_STATUS_TEXT);
726         gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
727                                               combo_row_separator_func,
728                                               NULL, NULL);
729
730         entry = gtk_bin_get_child (GTK_BIN (chooser));
731         gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
732                                         GTK_ENTRY_ICON_PRIMARY,
733                                         FALSE);
734
735         g_signal_connect_swapped (entry, "icon-release",
736                 G_CALLBACK (presence_chooser_entry_icon_release_cb),
737                 chooser);
738         g_signal_connect_swapped (entry, "activate",
739                 G_CALLBACK (presence_chooser_entry_activate_cb),
740                 chooser);
741         g_signal_connect_swapped (entry, "key-press-event",
742                 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
743                 chooser);
744         g_signal_connect_swapped (entry, "button-press-event",
745                 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
746                 chooser);
747
748         gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
749
750         renderer = gtk_cell_renderer_pixbuf_new ();
751         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
752         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
753                                         "icon-name", COL_STATE_ICON_NAME,
754                                         NULL);
755         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
756
757         renderer = gtk_cell_renderer_text_new ();
758         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
759         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
760                                         "markup", COL_DISPLAY_MARKUP,
761                                         NULL);
762
763         g_signal_connect (chooser, "notify::popup-shown",
764                         G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
765         g_signal_connect (chooser, "changed",
766                         G_CALLBACK (presence_chooser_changed_cb), NULL);
767         g_signal_connect_swapped (entry, "changed",
768                         G_CALLBACK (presence_chooser_entry_changed_cb),
769                         chooser);
770         g_signal_connect_swapped (entry, "focus-out-event",
771                         G_CALLBACK (presence_chooser_entry_focus_out_cb),
772                         chooser);
773
774         priv->idle = empathy_idle_dup_singleton ();
775         presence_chooser_presence_changed_cb (chooser);
776         g_signal_connect_swapped (priv->idle, "notify",
777                 G_CALLBACK (presence_chooser_presence_changed_cb),
778                 chooser);
779
780         /* FIXME: this string sucks */
781         gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
782                 _("Set your presence and current status"));
783 }
784
785 static void
786 presence_chooser_finalize (GObject *object)
787 {
788         EmpathyPresenceChooserPriv *priv;
789
790         priv = GET_PRIV (object);
791
792         if (priv->flash_timeout_id) {
793                 g_source_remove (priv->flash_timeout_id);
794         }
795
796         if (priv->focus_out_idle_source) {
797                 g_source_remove (priv->focus_out_idle_source);
798         }
799
800         g_signal_handlers_disconnect_by_func (priv->idle,
801                                               presence_chooser_presence_changed_cb,
802                                               object);
803         g_object_unref (priv->idle);
804
805         G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
806 }
807
808 /**
809  * empathy_presence_chooser_new:
810  *
811  * Creates a new #EmpathyPresenceChooser widget.
812  *
813  * Return value: A new #EmpathyPresenceChooser widget
814  */
815 GtkWidget *
816 empathy_presence_chooser_new (void)
817 {
818         GtkWidget *chooser;
819
820         chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
821
822         return chooser;
823 }
824
825 static void
826 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
827 {
828         EmpathyPresenceChooserPriv *priv;
829         TpConnectionPresenceType    state;
830         TpConnectionPresenceType    flash_state;
831         const gchar                *status;
832         GtkTreeModel               *model;
833         GtkTreeIter                 iter;
834         gboolean valid, match_state = FALSE, match = FALSE;
835
836         priv = GET_PRIV (chooser);
837
838         if (priv->editing_status) {
839                 return;
840         }
841
842         priv->state = state = empathy_idle_get_state (priv->idle);
843         status = empathy_idle_get_status (priv->idle);
844         flash_state = empathy_idle_get_flash_state (priv->idle);
845
846         /* look through the model and attempt to find a matching state */
847         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
848         for (valid = gtk_tree_model_get_iter_first (model, &iter);
849              valid;
850              valid = gtk_tree_model_iter_next (model, &iter)) {
851                 int m_type;
852                 TpConnectionPresenceType m_state;
853                 char *m_status;
854
855                 gtk_tree_model_get (model, &iter,
856                                 COL_STATE, &m_state,
857                                 COL_TYPE, &m_type,
858                                 -1);
859
860                 if (m_type == ENTRY_TYPE_CUSTOM ||
861                     m_type == ENTRY_TYPE_SEPARATOR ||
862                     m_type == ENTRY_TYPE_EDIT_CUSTOM) {
863                         continue;
864                 }
865                 else if (!match_state && state == m_state) {
866                         /* we are now in the section that can contain our
867                          * match */
868                         match_state = TRUE;
869                 }
870                 else if (match_state && state != m_state) {
871                         /* we have passed the section that can contain our
872                          * match */
873                         break;
874                 }
875
876                 gtk_tree_model_get (model, &iter,
877                                 COL_STATUS_TEXT, &m_status,
878                                 -1);
879
880                 match = !tp_strdiff (status, m_status);
881
882                 g_free (m_status);
883
884                 if (match) break;
885
886         }
887
888         if (match) {
889                 priv->block_changed++;
890                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
891                 presence_chooser_set_favorite_icon (chooser);
892                 priv->block_changed--;
893         }
894         else {
895                 ui_set_custom_state (chooser, state, status);
896         }
897
898         if (flash_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
899                 presence_chooser_flash_start (chooser, state, flash_state);
900         }
901         else {
902                 presence_chooser_flash_stop (chooser, state);
903         }
904 }
905
906 static gboolean
907 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
908 {
909         EmpathyPresenceChooserPriv *priv;
910         TpConnectionPresenceType    state;
911         static gboolean             on = FALSE;
912         GtkWidget                  *entry;
913
914         priv = GET_PRIV (chooser);
915
916         if (on) {
917                 state = priv->flash_state_1;
918         }
919         else {
920                 state = priv->flash_state_2;
921         }
922
923         entry = gtk_bin_get_child (GTK_BIN (chooser));
924         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
925                                            GTK_ENTRY_ICON_PRIMARY,
926                                            empathy_icon_name_for_presence (state));
927
928         on = !on;
929
930         return TRUE;
931 }
932
933 static void
934 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
935                               TpConnectionPresenceType state_1,
936                               TpConnectionPresenceType state_2)
937 {
938         EmpathyPresenceChooserPriv *priv;
939
940         g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
941
942         priv = GET_PRIV (chooser);
943
944         priv->flash_state_1 = state_1;
945         priv->flash_state_2 = state_2;
946
947         if (!priv->flash_timeout_id) {
948                 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
949                         (GSourceFunc) presence_chooser_flash_timeout_cb,
950                         chooser);
951         }
952 }
953
954 static void
955 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
956                              TpConnectionPresenceType state)
957 {
958         EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
959         GtkWidget *entry;
960
961         if (priv->flash_timeout_id) {
962                 g_source_remove (priv->flash_timeout_id);
963                 priv->flash_timeout_id = 0;
964         }
965
966         entry = gtk_bin_get_child (GTK_BIN (chooser));
967         gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
968                                            GTK_ENTRY_ICON_PRIMARY,
969                                            empathy_icon_name_for_presence (state));
970 }
971
972 /**
973  * empathy_presence_chooser_create_menu:
974  *
975  * Creates a new #GtkMenu allowing users to change their presence from a menu.
976  *
977  * Return value: a new #GtkMenu for changing presence in a menu.
978  */
979 GtkWidget *
980 empathy_presence_chooser_create_menu (void)
981 {
982         const gchar *status;
983         GtkWidget   *menu;
984         GtkWidget   *item;
985         GtkWidget   *image;
986         guint        i;
987
988         menu = gtk_menu_new ();
989
990         for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
991                 GList       *list, *l;
992
993                 status = empathy_presence_get_default_message (states[i].state);
994                 presence_chooser_menu_add_item (menu,
995                                                 status,
996                                                 states[i].state);
997
998                 if (states[i].customisable) {
999                         /* Set custom messages if wanted */
1000                         list = empathy_status_presets_get (states[i].state, 5);
1001                         for (l = list; l; l = l->next) {
1002                                 presence_chooser_menu_add_item (menu,
1003                                                                 l->data,
1004                                                                 states[i].state);
1005                         }
1006                         g_list_free (list);
1007                 }
1008
1009         }
1010
1011         /* Separator */
1012         item = gtk_menu_item_new ();
1013         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1014         gtk_widget_show (item);
1015
1016         /* Custom messages */
1017         item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
1018         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1019         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1020         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1021         gtk_widget_show (image);
1022         gtk_widget_show (item);
1023
1024         g_signal_connect (item,
1025                           "activate",
1026                           G_CALLBACK (presence_chooser_custom_activate_cb),
1027                           NULL);
1028
1029         return menu;
1030 }
1031
1032 static void
1033 presence_chooser_menu_add_item (GtkWidget   *menu,
1034                                 const gchar *str,
1035                                 TpConnectionPresenceType state)
1036 {
1037         GtkWidget   *item;
1038         GtkWidget   *image;
1039         const gchar *icon_name;
1040
1041         item = gtk_image_menu_item_new_with_label (str);
1042         icon_name = empathy_icon_name_for_presence (state);
1043
1044         g_signal_connect (item, "activate",
1045                           G_CALLBACK (presence_chooser_noncustom_activate_cb),
1046                           NULL);
1047
1048         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1049         gtk_widget_show (image);
1050
1051         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1052         gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
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         TpConnectionPresenceType 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 (TpConnectionPresenceType 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         GtkWidget *dialog;
1093
1094         dialog = empathy_status_preset_dialog_new (NULL);
1095         gtk_dialog_run (GTK_DIALOG (dialog));
1096         gtk_widget_destroy (dialog);
1097 }