]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-presence-chooser.c
Don't make Hidden/Offline have customisable text
[empathy.git] / libempathy-gtk / empathy-presence-chooser.c
index 702379a7cfeab8c6fa6ce586b14ed1c5e73cb085..0bb2ce0dea2e50beb56523f4c938270a28c5bbb2 100644 (file)
@@ -1,6 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2005-2007 Imendio AB
+ * Copyright (C) 2009 Collabora Ltd.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -20,6 +21,7 @@
  * Authors: Richard Hult <richard@imendio.com>
  *          Martyn Russell <martyn@imendio.com>
  *          Xavier Claessens <xclaesse@gmail.com>
+ *          Davyd Madeley <davyd.madeley@collabora.co.uk>
  */
 
 #include "config.h"
@@ -30,6 +32,7 @@
 #include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 #include <glade/glade.h>
+#include <gdk/gdkkeysyms.h>
 
 #include <telepathy-glib/util.h>
 #include <libmissioncontrol/mc-enum-types.h>
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-status-presets.h>
 
+// FIXME - what's the correct debug flag?
+#define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
+#include <libempathy/empathy-debug.h>
+
 #include "empathy-ui-utils.h"
 #include "empathy-images.h"
 #include "empathy-presence-chooser.h"
@@ -102,7 +109,7 @@ static void            presence_chooser_set_state              (McPresence
                                                                const gchar                *status);
 static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
                                                                gpointer                    user_data);
-static void            presence_chooser_dialog_show            (void);
+static void            presence_chooser_dialog_show            (GtkWindow                  *parent);
 
 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
 
@@ -122,6 +129,7 @@ enum
        COL_STATE,
        COL_STATUS_TEXT,
        COL_DISPLAY_MARKUP,
+       COL_STATUS_CUSTOMISABLE,
        COL_TYPE,
        N_COLUMNS
 };
@@ -131,6 +139,8 @@ enum
        ENTRY_TYPE_BUILTIN,
        ENTRY_TYPE_SAVED,
        ENTRY_TYPE_CUSTOM,
+       ENTRY_TYPE_SEPARATOR,
+       ENTRY_TYPE_EDIT_CUSTOM,
 };
 
 static GtkTreeModel *
@@ -141,6 +151,7 @@ create_model (void)
                        MC_TYPE_PRESENCE,       /* COL_STATE */
                        G_TYPE_STRING,          /* COL_STATUS_TEXT */
                        G_TYPE_STRING,          /* COL_DISPLAY_MARKUP */
+                       G_TYPE_BOOLEAN,         /* COL_STATUS_CUSTOMISABLE */
                        G_TYPE_INT);            /* COL_TYPE */
        
        GtkTreeIter iter;
@@ -158,6 +169,7 @@ create_model (void)
                                COL_STATE, states[i],
                                COL_STATUS_TEXT, status,
                                COL_DISPLAY_MARKUP, status,
+                               COL_STATUS_CUSTOMISABLE, states[i+1],
                                COL_TYPE, ENTRY_TYPE_BUILTIN,
                                -1);
 
@@ -171,6 +183,7 @@ create_model (void)
                                                COL_STATE, states[i],
                                                COL_STATUS_TEXT, l->data,
                                                COL_DISPLAY_MARKUP, l->data,
+                                               COL_STATUS_CUSTOMISABLE, TRUE,
                                                COL_TYPE, ENTRY_TYPE_SAVED,
                                                -1);
                        }
@@ -182,11 +195,26 @@ create_model (void)
                                        COL_STATE, states[i],
                                        COL_STATUS_TEXT, "",
                                        COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
+                                       COL_STATUS_CUSTOMISABLE, TRUE,
                                        COL_TYPE, ENTRY_TYPE_CUSTOM,
                                        -1);
                }
 
        }
+       
+       /* add a separator */
+       gtk_list_store_append (store, &iter);
+       gtk_list_store_set (store, &iter,
+                       COL_TYPE, ENTRY_TYPE_SEPARATOR,
+                       -1);
+       
+       gtk_list_store_append (store, &iter);
+       gtk_list_store_set (store, &iter,
+                       COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
+                       COL_STATUS_TEXT, "",
+                       COL_DISPLAY_MARKUP, "Edit Custom Messages...",
+                       COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
+                       -1);
 
        return GTK_TREE_MODEL (store);
 }
@@ -222,7 +250,7 @@ set_status_editing (EmpathyPresenceChooser *self, gboolean editing)
                                GTK_STOCK_OK);
                gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_SECONDARY,
-                               "Set status");
+                               _("Set status"));
                gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_PRIMARY,
                                FALSE);
@@ -239,7 +267,14 @@ set_status_editing (EmpathyPresenceChooser *self, gboolean editing)
                                GTK_ENTRY_ICON_PRIMARY,
                                TRUE);
 
-               // FIXME - move the focus somewhere
+               /* attempt to get the toplevel for this widget */
+               GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
+               if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window))
+               {
+                       /* unset the focus */
+                       gtk_window_set_focus (GTK_WINDOW (window), NULL);
+               }
+               gtk_editable_set_position (GTK_EDITABLE (entry), 0);
 
                priv->editing_status = FALSE;
        }
@@ -253,6 +288,10 @@ mc_set_custom_state (EmpathyPresenceChooser *self)
 
        /* update the status with MC */
        const char *status = gtk_entry_get_text (GTK_ENTRY (entry));
+       DEBUG ("Sending state to MC-> %s (%s)\n",
+                       g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
+                               priv->state)->value_name,
+                       status);
        empathy_idle_set_presence (priv->idle, priv->state, status);
 }
 
@@ -296,6 +335,25 @@ entry_activate_cb (EmpathyPresenceChooser  *self,
        mc_set_custom_state (self);
 }
 
+static gboolean
+entry_key_press_event_cb (EmpathyPresenceChooser       *self,
+                         GdkEventKey                   *event,
+                         GtkWidget                     *entry)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+
+       if (priv->editing_status && event->keyval == GDK_Escape)
+       {
+               /* the user pressed Escape, undo the editing */
+               set_status_editing (self, FALSE);
+               presence_chooser_presence_changed_cb (self);
+
+               return TRUE;
+       }
+
+       return FALSE; /* send this event elsewhere */
+}
+
 static void
 changed_cb (GtkComboBox *self, gpointer user_data)
 {
@@ -305,6 +363,7 @@ changed_cb (GtkComboBox *self, gpointer user_data)
 
        GtkTreeIter iter;
        char *icon_name;
+       gboolean customisable = TRUE;
        int type = -1;
 
        GtkTreeModel *model = gtk_combo_box_get_model (self);
@@ -321,18 +380,57 @@ changed_cb (GtkComboBox *self, gpointer user_data)
        gtk_tree_model_get (model, &iter,
                        COL_STATE_ICON_NAME, &icon_name,
                        COL_STATE, &priv->state,
+                       COL_STATUS_CUSTOMISABLE, &customisable,
                        COL_TYPE, &type,
                        -1);
 
        GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
-       gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
-                       GTK_ENTRY_ICON_PRIMARY,
-                       icon_name);
 
-       if (type == ENTRY_TYPE_CUSTOM)
+       /* some types of status aren't editable, set the editability of the
+        * entry appropriately. Unless we're just about to reset it anyway,
+        * in which case, don't fiddle with it */
+       /* FIXME: there is a bug here, if we start in the Hidden state, it
+        * will be editable. It's not something that often occurs though. */
+       if (type != ENTRY_TYPE_EDIT_CUSTOM)
+       {
+               gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
+       }
+
+       if (type == ENTRY_TYPE_EDIT_CUSTOM)
+       {
+               /* recover the status that was unset because COL_STATUS_TEXT
+                * is "". Unfortunately if you try and set COL_STATUS_TEXT to
+                * NULL, it generates a g_critical. I wonder if there is a
+                * better way around this. */
+               const char *status = empathy_idle_get_status (priv->idle);
+               priv->block_set_editing++;
+               gtk_entry_set_text (GTK_ENTRY (entry), status);
+               priv->block_set_editing--;
+
+               /* attempt to get the toplevel for this widget */
+               GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
+               if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window))
+               {
+                       window = NULL;
+               }
+
+               presence_chooser_dialog_show (GTK_WINDOW (window));
+       }
+       else if (type == ENTRY_TYPE_CUSTOM)
        {
+               gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
+                               GTK_ENTRY_ICON_PRIMARY,
+                               icon_name);
+
+               /* preseed the status */
+               const char *status = empathy_idle_get_status (priv->idle);
+               priv->block_set_editing++;
+               gtk_entry_set_text (GTK_ENTRY (entry), status);
+               priv->block_set_editing--;
+
                /* grab the focus */
                gtk_widget_grab_focus (entry);
+
                set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
        }
        else
@@ -354,6 +452,19 @@ changed_cb (GtkComboBox *self, gpointer user_data)
        g_free (icon_name);
 }
 
+static gboolean
+combo_row_separator_func (GtkTreeModel *model,
+                         GtkTreeIter   *iter,
+                         gpointer       data)
+{
+       int type;
+       gtk_tree_model_get (model, iter,
+                       COL_TYPE, &type,
+                       -1);
+
+       return (type == ENTRY_TYPE_SEPARATOR);
+}
+
 static void
 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
 {
@@ -365,7 +476,11 @@ empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
        GtkTreeModel *model = create_model ();
 
        gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
-       gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser), COL_STATUS_TEXT);
+       gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
+                       COL_STATUS_TEXT);
+       gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
+                       combo_row_separator_func,
+                       NULL, NULL);
        
        GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
        gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
@@ -376,6 +491,9 @@ empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
        g_signal_connect_object (entry, "activate",
                        G_CALLBACK (entry_activate_cb), chooser,
                        G_CONNECT_SWAPPED);
+       g_signal_connect_object (entry, "key-press-event",
+                       G_CALLBACK (entry_key_press_event_cb), chooser,
+                       G_CONNECT_SWAPPED);
        // FIXME - should this also happen when the user presses TAB ?
 
        GtkCellRenderer *renderer;
@@ -400,12 +518,19 @@ empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
                        G_CALLBACK (popup_shown_cb), NULL);
        g_signal_connect (chooser, "changed",
                        G_CALLBACK (changed_cb), NULL);
+       g_signal_connect_swapped (entry, "changed",
+                       G_CALLBACK (changed_cb), chooser);
 
        priv->idle = empathy_idle_dup_singleton ();
        presence_chooser_presence_changed_cb (chooser);
        g_signal_connect_swapped (priv->idle, "notify",
                                  G_CALLBACK (presence_chooser_presence_changed_cb),
                                  chooser);
+
+       g_object_set (chooser,
+                       // FIXME: this string sucks
+                       "tooltip-text", _("Set your presence and current status"),
+                       NULL);
 }
 
 static void
@@ -447,7 +572,7 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
 
        priv = GET_PRIV (chooser);
 
-       state = empathy_idle_get_state (priv->idle);
+       priv->state = state = empathy_idle_get_state (priv->idle);
        status = empathy_idle_get_status (priv->idle);
        flash_state = empathy_idle_get_flash_state (priv->idle);
 
@@ -468,7 +593,9 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
                                COL_TYPE, &m_type,
                                -1);
 
-               if (m_type == ENTRY_TYPE_CUSTOM)
+               if (m_type == ENTRY_TYPE_CUSTOM ||
+                   m_type == ENTRY_TYPE_SEPARATOR ||
+                   m_type == ENTRY_TYPE_EDIT_CUSTOM)
                {
                        continue;
                }
@@ -698,7 +825,7 @@ static void
 presence_chooser_custom_activate_cb (GtkWidget *item,
                                     gpointer   user_data)
 {
-       presence_chooser_dialog_show ();
+       presence_chooser_dialog_show (NULL);
 }
 
 static McPresence
@@ -863,7 +990,7 @@ presence_chooser_dialog_destroy_cb (GtkWidget           *widget,
 }
 
 static void
-presence_chooser_dialog_show (void)
+presence_chooser_dialog_show (GtkWindow *parent)
 {
        GladeXML *glade;
        gchar    *filename;
@@ -909,7 +1036,12 @@ presence_chooser_dialog_show (void)
 
        gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
 
-       /* FIXME: Set transian for a window ? */
+       if (parent)
+       {
+               gtk_window_set_transient_for (
+                               GTK_WINDOW (message_dialog->dialog),
+                               parent);
+       }
 
        gtk_widget_show_all (message_dialog->dialog);
 }