]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-avatar-chooser.c
use avatar-default instead of the deprecated stock_person icon
[empathy.git] / libempathy-gtk / empathy-avatar-chooser.c
index 02aa689eb28aa48c3db98cf58ad4d54485db5173..a7e8683e852f4513d5d6968b3e269d22f9450b21 100644 (file)
@@ -14,8 +14,8 @@
  *
  * You should have received a copy of the GNU General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
  *
  * Authors: Based on Novell's e-image-chooser.
  *          Xavier Claessens <xclaesse@gmail.com>
 #include <gio/gio.h>
 
 #include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-contact-factory.h>
+#include <libempathy/empathy-tp-contact-factory.h>
 
 #include "empathy-avatar-chooser.h"
 #include "empathy-conf.h"
+#include "empathy-images.h"
 #include "empathy-ui-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
 #include <libempathy/empathy-debug.h>
 
+/**
+ * SECTION:empathy-avatar-chooser
+ * @title: EmpathyAvatarChooser
+ * @short_description: A widget used to change avatar
+ * @include: libempathy-gtk/empathy-avatar-chooser.h
+ *
+ * #EmpathyAvatarChooser is a widget which extends #GtkButton to
+ * provide a way of changing avatar.
+ */
+
+/**
+ * EmpathyAvatarChooser:
+ * @parent: parent object
+ *
+ * Widget which extends #GtkButton to provide a way of changing avatar.
+ */
+
 #define AVATAR_SIZE_SAVE 96
 #define AVATAR_SIZE_VIEW 64
 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
 typedef struct {
-       EmpathyContactFactory   *contact_factory;
-       McAccount               *account;
-       EmpathyTpContactFactory *tp_contact_factory;
+       EmpathyTpContactFactory *factory;
+       TpConnection            *connection;
        GtkFileChooser          *chooser_dialog;
 
        gulong ready_handler_id;
@@ -56,8 +73,8 @@ typedef struct {
 } EmpathyAvatarChooserPriv;
 
 static void       avatar_chooser_finalize              (GObject              *object);
-static void       avatar_chooser_set_account           (EmpathyAvatarChooser *self,
-                                                       McAccount            *account);
+static void       avatar_chooser_set_connection        (EmpathyAvatarChooser *self,
+                                                       TpConnection         *connection);
 static void       avatar_chooser_set_image             (EmpathyAvatarChooser *chooser,
                                                        EmpathyAvatar        *avatar,
                                                        GdkPixbuf            *pixbuf,
@@ -96,7 +113,7 @@ enum {
 
 enum {
        PROP_0,
-       PROP_ACCOUNT
+       PROP_CONNECTION
 };
 
 static guint signals [LAST_SIGNAL];
@@ -125,8 +142,8 @@ avatar_chooser_get_property (GObject    *object,
        EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
 
        switch (param_id) {
-       case PROP_ACCOUNT:
-               g_value_set_object (value, priv->account);
+       case PROP_CONNECTION:
+               g_value_set_object (value, priv->connection);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -143,8 +160,8 @@ avatar_chooser_set_property (GObject      *object,
        EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
 
        switch (param_id) {
-       case PROP_ACCOUNT:
-               avatar_chooser_set_account (self, g_value_get_object (value));
+       case PROP_CONNECTION:
+               avatar_chooser_set_connection (self, g_value_get_object (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -162,6 +179,13 @@ empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
        object_class->get_property = avatar_chooser_get_property;
        object_class->set_property = avatar_chooser_set_property;
 
+       /**
+        * EmpathyAvatarChooser::changed:
+        * @chooser: an #EmpathyAvatarChooser
+        *
+        * Emitted when the chosen avatar has changed.
+        *
+        */
        signals[CHANGED] =
                g_signal_new ("changed",
                              G_TYPE_FROM_CLASS (klass),
@@ -171,15 +195,21 @@ empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE, 0);
 
-       param_spec = g_param_spec_object ("account",
-                                         "McAccount",
-                                         "McAccount whose avatar should be "
+       /**
+        * EmpathyAvatarChooser:connection:
+        *
+        * The #TpConnection whose avatar should be shown and modified by
+        * the #EmpathyAvatarChooser instance.
+        */
+       param_spec = g_param_spec_object ("connection",
+                                         "TpConnection",
+                                         "TpConnection whose avatar should be "
                                          "shown and modified by this widget",
-                                         MC_TYPE_ACCOUNT,
+                                         TP_TYPE_CONNECTION,
                                          G_PARAM_READWRITE |
                                          G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (object_class,
-                                        PROP_ACCOUNT,
+                                        PROP_CONNECTION,
                                         param_spec);
 
        g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
@@ -214,8 +244,6 @@ empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
                          G_CALLBACK (avatar_chooser_clicked_cb),
                          chooser);
 
-       priv->contact_factory = empathy_contact_factory_dup_singleton ();
-
        empathy_avatar_chooser_set (chooser, NULL);
 }
 
@@ -226,11 +254,9 @@ avatar_chooser_finalize (GObject *object)
 
        priv = GET_PRIV (object);
 
-       avatar_chooser_set_account (EMPATHY_AVATAR_CHOOSER (object), NULL);
-       g_assert (priv->account == NULL);
-       g_assert (priv->tp_contact_factory == NULL);
-
-       g_object_unref (priv->contact_factory);
+       avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
+       g_assert (priv->connection == NULL);
+       g_assert (priv->factory == NULL);
 
        if (priv->avatar != NULL) {
                empathy_avatar_unref (priv->avatar);
@@ -240,51 +266,22 @@ avatar_chooser_finalize (GObject *object)
 }
 
 static void
-avatar_chooser_tp_cf_ready_cb (EmpathyTpContactFactory *tp_cf,
-                              GParamSpec              *unused,
-                              EmpathyAvatarChooser    *self)
+avatar_chooser_set_connection (EmpathyAvatarChooser *self,
+                              TpConnection         *connection)
 {
        EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
-       gboolean ready;
 
-       /* sanity check that we're listening on the right ETpCF */
-       g_assert (priv->tp_contact_factory == tp_cf);
+       if (priv->connection != NULL) {
+               g_object_unref (priv->connection);
+               priv->connection = NULL;
 
-       ready = empathy_tp_contact_factory_is_ready (tp_cf);
-       gtk_widget_set_sensitive (GTK_WIDGET (self), ready);
-}
-
-static void
-avatar_chooser_set_account (EmpathyAvatarChooser *self,
-                           McAccount            *account)
-{
-       EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
-
-       if (priv->account != NULL) {
-               g_object_unref (priv->account);
-               priv->account = NULL;
-
-               g_assert (priv->tp_contact_factory != NULL);
-
-               g_signal_handler_disconnect (priv->tp_contact_factory,
-                       priv->ready_handler_id);
-               priv->ready_handler_id = 0;
-
-               g_object_unref (priv->tp_contact_factory);
-               priv->tp_contact_factory = NULL;
+               g_object_unref (priv->factory);
+               priv->factory = NULL;
        }
 
-       if (account != NULL) {
-               priv->account = g_object_ref (account);
-               priv->tp_contact_factory = g_object_ref (
-                       empathy_contact_factory_get_tp_factory (
-                               priv->contact_factory, priv->account));
-
-               priv->ready_handler_id = g_signal_connect (
-                       priv->tp_contact_factory, "notify::ready",
-                       G_CALLBACK (avatar_chooser_tp_cf_ready_cb), self);
-               avatar_chooser_tp_cf_ready_cb (priv->tp_contact_factory, NULL,
-                       self);
+       if (connection != NULL) {
+               priv->connection = g_object_ref (connection);
+               priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
        }
 }
 
@@ -412,11 +409,10 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
                                        EmpathyAvatar        *avatar)
 {
        EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
-       EmpathyTpContactFactory  *tp_cf = priv->tp_contact_factory;
        guint                     max_width = 0, max_height = 0, max_size = 0;
        gchar                   **mime_types = NULL;
        gboolean                  needs_conversion = FALSE;
-       gint                      width, height;
+       guint                     width, height;
        gchar                    *new_format_name = NULL;
        gchar                    *new_mime_type = NULL;
        gdouble                   min_factor, max_factor;
@@ -424,15 +420,7 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
        gchar                    *converted_image_data = NULL;
        gsize                     converted_image_size = 0;
 
-       /* This should only be called if the user is setting a new avatar,
-        * which should only be allowed once the avatar requirements have been
-        * discovered.
-        */
-       g_return_val_if_fail (tp_cf != NULL, NULL);
-       g_return_val_if_fail (empathy_tp_contact_factory_is_ready (tp_cf),
-               NULL);
-
-       g_object_get (tp_cf,
+       g_object_get (priv->factory,
                "avatar-mime-types", &mime_types, /* Needs g_strfreev-ing */
                "avatar-max-width", &max_width,
                "avatar-max-height", &max_height,
@@ -460,7 +448,7 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
        /* If there is no format we can use, report error to the user. */
        if (new_mime_type == NULL || new_format_name == NULL) {
                avatar_chooser_error_show (chooser, _("Couldn't convert image"),
-                               _("None of the accepted image formats is "
+                               _("None of the accepted image formats are "
                                  "supported on your system"));
                return NULL;
        }
@@ -563,12 +551,12 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
                 *   a difference of 1k.
                 */
        } while (min_factor != max_factor &&
-                ABS (max_size - converted_image_size) > 1024);
+                abs (max_size - converted_image_size) > 1024);
        g_free (new_format_name);
 
        /* Takes ownership of new_mime_type and converted_image_data */
-       avatar = empathy_avatar_new (converted_image_data,
-               converted_image_size, new_mime_type, NULL);
+       avatar = empathy_avatar_new ((guchar *) converted_image_data,
+               converted_image_size, new_mime_type, NULL, NULL);
 
        return avatar;
 }
@@ -584,7 +572,8 @@ avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
                priv->avatar = NULL;
        }
 
-       image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
+       image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
+               GTK_ICON_SIZE_DIALOG);
        gtk_button_set_image (GTK_BUTTON (chooser), image);
        g_signal_emit (chooser, signals[CHANGED], 0);
 }
@@ -607,11 +596,12 @@ avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
        pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
        if (pixbuf == NULL) {
                g_free (data);
+               data = NULL;
                return;
        }
 
        /* avatar takes ownership of data and mime_type */
-       avatar = empathy_avatar_new (data, size, mime_type, NULL);
+       avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL, NULL);
 
        avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
 }
@@ -626,7 +616,7 @@ avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
 
        g_assert (avatar != NULL);
 
-       pixbuf = empathy_pixbuf_from_data_and_mime (avatar->data,
+       pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
                                                    avatar->len,
                                                    &mime_type);
        if (pixbuf == NULL) {
@@ -716,7 +706,7 @@ avatar_chooser_drag_motion_cb (GtkWidget          *widget,
                              GdkDragContext     *context,
                              gint                x,
                              gint                y,
-                             guint               time,
+                             guint               time_,
                              EmpathyAvatarChooser *chooser)
 {
        EmpathyAvatarChooserPriv *priv;
@@ -731,7 +721,7 @@ avatar_chooser_drag_motion_cb (GtkWidget          *widget,
 
                if (!strcmp (possible_type, URI_LIST_TYPE)) {
                        g_free (possible_type);
-                       gdk_drag_status (context, GDK_ACTION_COPY, time);
+                       gdk_drag_status (context, GDK_ACTION_COPY, time_);
 
                        return TRUE;
                }
@@ -745,7 +735,7 @@ avatar_chooser_drag_motion_cb (GtkWidget          *widget,
 static void
 avatar_chooser_drag_leave_cb (GtkWidget          *widget,
                             GdkDragContext     *context,
-                            guint               time,
+                            guint               time_,
                             EmpathyAvatarChooser *chooser)
 {
 }
@@ -755,7 +745,7 @@ avatar_chooser_drag_drop_cb (GtkWidget          *widget,
                            GdkDragContext     *context,
                            gint                x,
                            gint                y,
-                           guint               time,
+                           guint               time_,
                            EmpathyAvatarChooser *chooser)
 {
        EmpathyAvatarChooserPriv *priv;
@@ -775,7 +765,7 @@ avatar_chooser_drag_drop_cb (GtkWidget          *widget,
                        g_free (possible_type);
                        gtk_drag_get_data (widget, context,
                                           GDK_POINTER_TO_ATOM (p->data),
-                                          time);
+                                          time_);
 
                        return TRUE;
                }
@@ -793,69 +783,50 @@ avatar_chooser_drag_data_received_cb (GtkWidget          *widget,
                                     gint                y,
                                     GtkSelectionData   *selection_data,
                                     guint               info,
-                                    guint               time,
+                                    guint               time_,
                                     EmpathyAvatarChooser *chooser)
 {
        gchar    *target_type;
        gboolean  handled = FALSE;
 
-       target_type = gdk_atom_name (selection_data->target);
+       target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
        if (!strcmp (target_type, URI_LIST_TYPE)) {
                GFile            *file;
-               GFileInputStream *input_stream;
                gchar            *nl;
                gchar            *data = NULL;
+               gsize             bytes_read;
 
-               nl = strstr (selection_data->data, "\r\n");
+               nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
+                                               "\r\n");
                if (nl) {
                        gchar *uri;
 
-                       uri = g_strndup (selection_data->data,
-                                        nl - (gchar*) selection_data->data);
+                       uri = g_strndup ((gchar *) gtk_selection_data_get_data (selection_data),
+                                        nl - (gchar *) gtk_selection_data_get_data (selection_data));
 
                        file = g_file_new_for_uri (uri);
                        g_free (uri);
                } else {
-                       file = g_file_new_for_uri (selection_data->data);
+                       file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
+                                               selection_data));
                }
 
-               input_stream = g_file_read (file, NULL, NULL);
-
-               if (input_stream != NULL) {
-                       GFileInfo *info;
-                       
-                       info = g_file_query_info (file,
-                                                 G_FILE_ATTRIBUTE_STANDARD_SIZE,
-                                                 0, NULL, NULL);
-                       if (info != NULL) {
-                               goffset size;
-                               gssize bytes_read;
-                               
-                               size = g_file_info_get_size (info);
-                               data = g_malloc (size);
-
-                               bytes_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
-                                                                 data, size,
-                                                                 NULL, NULL);
-                               if (bytes_read != -1) {
-                                       avatar_chooser_set_image_from_data (
-                                               chooser, data,
-                                               (gsize) bytes_read,
-                                               TRUE);
-                                       handled = TRUE;
-                               }
-
-                               g_free (data);
-                               g_object_unref (info);
-                       }
+               handled = g_file_load_contents (file, NULL, &data, &bytes_read,
+                                               NULL, NULL);
 
-                       g_object_unref (input_stream);
+               if (handled) {
+                       /* this in turn calls empathy_avatar_new (), which assumes
+                        * ownership of data.
+                        */
+                       avatar_chooser_set_image_from_data (chooser, data,
+                                                           bytes_read,
+                                                           TRUE);
                }
-               
+
                g_object_unref (file);
        }
 
-       gtk_drag_finish (context, handled, FALSE, time);
+       gtk_drag_finish (context, handled, FALSE, time_);
 }
 
 static void
@@ -901,16 +872,6 @@ avatar_chooser_response_cb (GtkWidget            *widget,
 
        priv->chooser_dialog = NULL;
 
-       if (response == GTK_RESPONSE_CANCEL) {
-               goto out;
-       }
-
-       /* Check if we went non-ready since displaying the dialog. */
-       if (!empathy_tp_contact_factory_is_ready (priv->tp_contact_factory)) {
-               DEBUG ("Can't set avatar when contact factory isn't ready.");
-               goto out;
-       }
-
        if (response == GTK_RESPONSE_OK) {
                gchar *filename;
                gchar *path;
@@ -932,7 +893,6 @@ avatar_chooser_response_cb (GtkWidget            *widget,
                avatar_chooser_clear_image (chooser);
        }
 
-out:
        gtk_widget_destroy (widget);
 }
 
@@ -1024,7 +984,7 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
        gtk_file_chooser_add_filter (chooser_dialog, filter);
        filter = gtk_file_filter_new ();
        gtk_file_filter_set_name (filter, _("All Files"));
-       gtk_file_filter_add_pattern(filter, "*");
+       gtk_file_filter_add_pattern (filter, "*");
        gtk_file_chooser_add_filter (chooser_dialog, filter);
 
        /* Setup response */
@@ -1037,12 +997,26 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
        g_free (saved_dir);
 }
 
+/**
+ * empathy_avatar_chooser_new:
+ *
+ * Creates a new #EmpathyAvatarChooser.
+ *
+ * Return value: a new #EmpathyAvatarChooser
+ */
 GtkWidget *
 empathy_avatar_chooser_new ()
 {
        return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
 }
 
+/**
+ * empathy_avatar_chooser_set:
+ * @chooser: an #EmpathyAvatarChooser
+ * @avatar: a new #EmpathyAvatar
+ *
+ * Sets the @chooser to display the avatar indicated by @avatar.
+ */
 void
 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
                            EmpathyAvatar        *avatar)
@@ -1056,6 +1030,15 @@ empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
        }
 }
 
+/**
+ * empathy_avatar_chooser_get_image_data:
+ * @chooser: an #EmpathyAvatarChooser
+ * @data: avatar bytes
+ * @data_size: size of @data
+ * @mime_type: avatar mime-type
+ *
+ * Gets image data about the currently selected avatar.
+ */
 void
 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser  *chooser,
                                       const gchar          **data,
@@ -1070,7 +1053,7 @@ empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser  *chooser,
 
        if (priv->avatar != NULL) {
                if (data != NULL) {
-                       *data = priv->avatar->data;
+                       *data = (gchar *) priv->avatar->data;
                }
                if (data_size != NULL) {
                        *data_size = priv->avatar->len;
@@ -1091,3 +1074,12 @@ empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser  *chooser,
        }
 }
 
+void
+empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
+                                      TpAccount *account)
+{
+       g_return_if_fail (account != NULL);
+
+       avatar_chooser_set_connection (self, tp_account_get_connection (account));
+       g_object_notify (G_OBJECT (self), "connection");
+}