[evolution] EStockRequest cleanups.



commit 3fae1f7a1d94746ffecbb107ce4d4ca4fbca52c8
Author: Matthew Barnes <mbarnes redhat com>
Date:   Sat Jul 20 10:02:45 2013 -0400

    EStockRequest cleanups.

 e-util/e-stock-request.c |  182 +++++++++++++++++++++++-----------------------
 1 files changed, 91 insertions(+), 91 deletions(-)
---
diff --git a/e-util/e-stock-request.c b/e-util/e-stock-request.c
index 2b00f9f..ba2f508 100644
--- a/e-util/e-stock-request.c
+++ b/e-util/e-stock-request.c
@@ -26,8 +26,6 @@
 
 #include <string.h>
 
-#define d(x)
-
 #define E_STOCK_REQUEST_GET_PRIVATE(obj) \
        (G_TYPE_INSTANCE_GET_PRIVATE \
        ((obj), E_TYPE_STOCK_REQUEST, EStockRequestPrivate))
@@ -37,44 +35,38 @@ struct _EStockRequestPrivate {
        gint content_length;
 };
 
+static const gchar *data_schemes[] = { "gtk-stock", NULL };
+
 G_DEFINE_TYPE (EStockRequest, e_stock_request, SOUP_TYPE_REQUEST)
 
 static void
-handle_stock_request (GSimpleAsyncResult *res,
+handle_stock_request (GSimpleAsyncResult *simple,
                       GObject *object,
                       GCancellable *cancellable)
 {
        SoupURI *uri;
-       GHashTable *query;
-       GtkIconTheme *icon_theme;
-       GtkIconInfo *icon_info;
-       const gchar *file;
-       gchar *a_size;
-       gssize size;
-       gchar *buffer;
-       gsize buff_len;
+       GHashTable *query = NULL;
        GtkStyleContext *context;
        GtkWidgetPath *path;
-       GtkIconSet *set;
+       GtkIconSet *icon_set;
        EStockRequest *request;
+       gssize size = GTK_ICON_SIZE_BUTTON;
+       gchar *a_size;
+       gchar *buffer = NULL;
+       gsize buff_len = 0;
+       GError *local_error = NULL;
 
        request = E_STOCK_REQUEST (object);
        uri = soup_request_get_uri (SOUP_REQUEST (object));
 
-       query = NULL;
-       if (uri->query)
+       if (uri->query != NULL)
                query = soup_form_decode (uri->query);
 
-       if (query) {
+       if (query != NULL) {
                a_size = g_hash_table_lookup (query, "size");
-               if (a_size) {
+               if (a_size != NULL)
                        size = atoi (a_size);
-               } else {
-                       size = GTK_ICON_SIZE_BUTTON;
-               }
                g_hash_table_destroy (query);
-       } else {
-               size = GTK_ICON_SIZE_BUTTON;
        }
 
        /* Try style context first */
@@ -83,104 +75,108 @@ handle_stock_request (GSimpleAsyncResult *res,
        gtk_widget_path_append_type (path, GTK_TYPE_WINDOW);
        gtk_widget_path_append_type (path, GTK_TYPE_BUTTON);
        gtk_style_context_set_path (context, path);
+       gtk_widget_path_free (path);
+
+       icon_set = gtk_style_context_lookup_icon_set (context, uri->host);
+       if (icon_set != NULL) {
+               GdkPixbuf *pixbuf;
+
+               pixbuf = gtk_icon_set_render_icon_pixbuf (
+                       icon_set, context, size);
+               gdk_pixbuf_save_to_buffer (
+                       pixbuf, &buffer, &buff_len,
+                       "png", &local_error, NULL);
+               g_object_unref (pixbuf);
+
+       /* Fallback to icon theme */
+       } else {
+               GtkIconTheme *icon_theme;
+               GtkIconInfo *icon_info;
+               const gchar *filename;
 
-       set = gtk_style_context_lookup_icon_set (context, uri->host);
-       if (!set) {
-               /* Fallback to icon theme */
                icon_theme = gtk_icon_theme_get_default ();
+
                icon_info = gtk_icon_theme_lookup_icon (
-                               icon_theme, uri->host, size,
-                               GTK_ICON_LOOKUP_USE_BUILTIN);
-               if (!icon_info) {
-                       gtk_widget_path_free (path);
-                       g_object_unref (context);
-                       return;
-               }
+                       icon_theme, uri->host, size,
+                       GTK_ICON_LOOKUP_USE_BUILTIN);
 
-               file = gtk_icon_info_get_filename (icon_info);
-               buffer = NULL;
-               if (file) {
-                       if (g_file_get_contents (file, &buffer, &buff_len, NULL)) {
-
-                               request->priv->content_type =
-                                       g_content_type_guess (file, NULL, 0, NULL);
-                               request->priv->content_length = buff_len;
-                       }
+               filename = gtk_icon_info_get_filename (icon_info);
+               if (filename != NULL) {
+                       g_file_get_contents (
+                               filename, &buffer, &buff_len, &local_error);
+                       request->priv->content_type =
+                               g_content_type_guess (filename, NULL, 0, NULL);
 
                } else {
                        GdkPixbuf *pixbuf;
 
                        pixbuf = gtk_icon_info_get_builtin_pixbuf (icon_info);
-                       if (pixbuf) {
+                       if (pixbuf != NULL) {
                                gdk_pixbuf_save_to_buffer (
-                                       pixbuf, &buffer,
-                                       &buff_len, "png", NULL, NULL);
-
-                               request->priv->content_type = g_strdup ("image/png");
-                               request->priv->content_length = buff_len;
-
+                                       pixbuf, &buffer, &buff_len,
+                                       "png", &local_error, NULL);
                                g_object_unref (pixbuf);
                        }
                }
 
                gtk_icon_info_free (icon_info);
+       }
 
-       } else {
-               GdkPixbuf *pixbuf;
-
-               pixbuf = gtk_icon_set_render_icon_pixbuf (set, context, size);
-                               gdk_pixbuf_save_to_buffer (
-                                       pixbuf, &buffer,
-                                       &buff_len, "png", NULL, NULL);
+       /* Sanity check */
+       g_return_if_fail (
+               ((buffer != NULL) && (local_error == NULL)) ||
+               ((buffer == NULL) && (local_error != NULL)));
 
+       if (request->priv->content_type == NULL)
                request->priv->content_type = g_strdup ("image/png");
-               request->priv->content_length = buff_len;
-
-               g_object_unref (pixbuf);
-       }
+       request->priv->content_length = buff_len;
 
-       if (buffer) {
+       if (buffer != NULL) {
                GInputStream *stream;
+
                stream = g_memory_input_stream_new_from_data (
-                               buffer, buff_len, (GDestroyNotify) g_free);
-               g_simple_async_result_set_op_res_gpointer (res, stream, NULL);
+                       buffer, buff_len, (GDestroyNotify) g_free);
+               g_simple_async_result_set_op_res_gpointer (
+                       simple, g_object_ref (stream),
+                       (GDestroyNotify) g_object_unref);
+               g_object_unref (stream);
        }
 
-       gtk_widget_path_free (path);
-       g_object_unref (context);
+       if (local_error != NULL)
+               g_simple_async_result_take_error (simple, local_error);
 
+       g_object_unref (context);
 }
+
 static void
 stock_request_finalize (GObject *object)
 {
-       EStockRequest *request = E_STOCK_REQUEST (object);
+       EStockRequestPrivate *priv;
 
-       if (request->priv->content_type) {
-               g_free (request->priv->content_type);
-               request->priv->content_type = NULL;
-       }
+       priv = E_STOCK_REQUEST_GET_PRIVATE (object);
+
+       g_free (priv->content_type);
 
+       /* Chain up to parent's finalize() method. */
        G_OBJECT_CLASS (e_stock_request_parent_class)->finalize (object);
 }
 
 static gboolean
 stock_request_check_uri (SoupRequest *request,
-                       SoupURI *uri,
-                       GError **error)
+                         SoupURI *uri,
+                         GError **error)
 {
        return (strcmp (uri->scheme, "gtk-stock") == 0);
 }
 
 static void
 stock_request_send_async (SoupRequest *request,
-                         GCancellable *cancellable,
-                         GAsyncReadyCallback callback,
-                         gpointer user_data)
+                          GCancellable *cancellable,
+                          GAsyncReadyCallback callback,
+                          gpointer user_data)
 {
        GSimpleAsyncResult *simple;
 
-       d (printf ("received request for %s\n", soup_uri_to_string (uri, FALSE)));
-
        simple = g_simple_async_result_new (
                G_OBJECT (request), callback, user_data,
                stock_request_send_async);
@@ -205,37 +201,41 @@ stock_request_send_finish (SoupRequest *request,
        simple = G_SIMPLE_ASYNC_RESULT (result);
        stream = g_simple_async_result_get_op_res_gpointer (simple);
 
-       /* Reset the stream before passing it back to webkit */
-       if (stream && G_IS_SEEKABLE (stream))
-               g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, NULL);
+       if (g_simple_async_result_propagate_error (simple, error))
+               return NULL;
 
-       if (!stream) /* We must always return something */
-               stream = g_memory_input_stream_new ();
+       /* Reset the stream before passing it back to WebKit. */
+       if (G_IS_SEEKABLE (stream))
+               g_seekable_seek (
+                       G_SEEKABLE (stream), 0,
+                       G_SEEK_SET, NULL, NULL);
 
-       return stream;
+       if (stream != NULL)
+               return g_object_ref (stream);
+
+       return g_memory_input_stream_new ();
 }
 
 static goffset
 stock_request_get_content_length (SoupRequest *request)
 {
-       EStockRequest *esr = E_STOCK_REQUEST (request);
+       EStockRequestPrivate *priv;
+
+       priv = E_STOCK_REQUEST_GET_PRIVATE (request);
 
-       d (printf ("Content-Length: %d bytes\n", esr->priv->content_length));
-       return esr->priv->content_length;
+       return priv->content_length;
 }
 
 static const gchar *
 stock_request_get_content_type (SoupRequest *request)
 {
-       EStockRequest *esr = E_STOCK_REQUEST (request);
+       EStockRequestPrivate *priv;
 
-       d (printf ("Content-Type: %s\n", esr->priv->content_type));
+       priv = E_STOCK_REQUEST_GET_PRIVATE (request);
 
-       return esr->priv->content_type;
+       return priv->content_type;
 }
 
-static const gchar *data_schemes[] = { "gtk-stock", NULL };
-
 static void
 e_stock_request_class_init (EStockRequestClass *class)
 {
@@ -249,11 +249,11 @@ e_stock_request_class_init (EStockRequestClass *class)
 
        request_class = SOUP_REQUEST_CLASS (class);
        request_class->schemes = data_schemes;
+       request_class->check_uri = stock_request_check_uri;
        request_class->send_async = stock_request_send_async;
        request_class->send_finish = stock_request_send_finish;
-       request_class->get_content_type = stock_request_get_content_type;
        request_class->get_content_length = stock_request_get_content_length;
-       request_class->check_uri = stock_request_check_uri;
+       request_class->get_content_type = stock_request_get_content_type;
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]