diff options
author | Dan Vrátil <dvratil@redhat.com> | 2012-04-23 14:40:20 +0800 |
---|---|---|
committer | Dan Vrátil <dvratil@redhat.com> | 2012-04-23 14:40:20 +0800 |
commit | d571c56260d30a00a03112b8bdfff463d4a24b81 (patch) | |
tree | 28905f8b19ac50d999ff3ed9cb6ea7f547628a7d /e-util | |
parent | 95746c8e3a9c341fac7beae7bc279d6379beb7e4 (diff) | |
download | gsoc2013-evolution-d571c56260d30a00a03112b8bdfff463d4a24b81.tar.gz gsoc2013-evolution-d571c56260d30a00a03112b8bdfff463d4a24b81.tar.zst gsoc2013-evolution-d571c56260d30a00a03112b8bdfff463d4a24b81.zip |
Bug #674272 - Contacts preview differs with mailer running and not
This splits the giant EMailRequest to individual EFileRequest, EStockRequest, EHTTPRequest and EMailRequest,
making the first two available globally from e-utils, the othe two are loaded only with mailer,
since no other component uses them.
Diffstat (limited to 'e-util')
-rw-r--r-- | e-util/Makefile.am | 4 | ||||
-rw-r--r-- | e-util/e-file-request.c | 177 | ||||
-rw-r--r-- | e-util/e-file-request.h | 65 | ||||
-rw-r--r-- | e-util/e-stock-request.c | 257 | ||||
-rw-r--r-- | e-util/e-stock-request.h | 65 |
5 files changed, 568 insertions, 0 deletions
diff --git a/e-util/Makefile.am b/e-util/Makefile.am index 0087a21776..63fce88fbe 100644 --- a/e-util/Makefile.am +++ b/e-util/Makefile.am @@ -27,6 +27,7 @@ eutilinclude_HEADERS = \ e-dialog-utils.h \ e-dialog-widgets.h \ e-event.h \ + e-file-request.h \ e-file-utils.h \ e-html-utils.h \ e-icon-factory.h \ @@ -41,6 +42,7 @@ eutilinclude_HEADERS = \ e-selection.h \ e-sorter.h \ e-sorter-array.h \ + e-stock-request.h \ e-text-event-processor-emacs-like.h \ e-text-event-processor-types.h \ e-text-event-processor.h \ @@ -90,6 +92,7 @@ libeutil_la_SOURCES = \ e-dialog-utils.c \ e-dialog-widgets.c \ e-event.c \ + e-file-request.c \ e-file-utils.c \ e-html-utils.c \ e-icon-factory.c \ @@ -104,6 +107,7 @@ libeutil_la_SOURCES = \ e-selection.c \ e-sorter.c \ e-sorter-array.c \ + e-stock-request.c \ e-text-event-processor-emacs-like.c \ e-text-event-processor.c \ e-ui-manager.c \ diff --git a/e-util/e-file-request.c b/e-util/e-file-request.c new file mode 100644 index 0000000000..62c64e34a8 --- /dev/null +++ b/e-util/e-file-request.c @@ -0,0 +1,177 @@ +/* + * e-file-request.c + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with the program; if not, see <http://www.gnu.org/licenses/> + * + */ + +#define LIBSOUP_USE_UNSTABLE_REQUEST_API + +#include "e-file-request.h" + +#include <libsoup/soup.h> + +#include <e-util/e-util.h> + +#include <string.h> + +#define d(x) + +#define E_FILE_REQUEST_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE \ + ((obj), E_TYPE_FILE_REQUEST, EFileRequestPrivate)) + +struct _EFileRequestPrivate { + gchar *content_type; + gint content_length; +}; + +G_DEFINE_TYPE (EFileRequest, e_file_request, SOUP_TYPE_REQUEST) + +static void +handle_file_request (GSimpleAsyncResult *res, + GObject *object, + GCancellable *cancellable) +{ + EFileRequest *request = E_FILE_REQUEST (object); + SoupURI *uri; + GInputStream *stream; + gchar *contents; + gsize length; + + if (g_cancellable_is_cancelled (cancellable)) + return; + + uri = soup_request_get_uri (SOUP_REQUEST (request)); + + if (g_file_get_contents (uri->path, &contents, &length, NULL)) { + + request->priv->content_type = g_content_type_guess (uri->path, NULL, 0, NULL); + request->priv->content_length = length; + + stream = g_memory_input_stream_new_from_data ( + contents, length, (GDestroyNotify) g_free); + g_simple_async_result_set_op_res_gpointer (res, stream, NULL); + } +} + +static void +file_request_finalize (GObject *object) +{ + EFileRequest *request = E_FILE_REQUEST (object); + + if (request->priv->content_type) { + g_free (request->priv->content_type); + request->priv->content_type = NULL; + } + + G_OBJECT_CLASS (e_file_request_parent_class)->finalize (object); +} + +static gboolean +file_request_check_uri (SoupRequest *request, + SoupURI *uri, + GError **error) +{ + return (strcmp (uri->scheme, "evo-file") == 0); +} + +static void +file_request_send_async (SoupRequest *request, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + + d(printf("received request for %s\n", soup_uri_to_string (uri, FALSE))); + + /* WebKit won't allow us to load data through local file:// protocol + * when using "remote" mail:// protocol, so we have evo-file:// + * which WebKit thinks it's remote, but in fact it behaves like + * oridnary file:// */ + + result = g_simple_async_result_new (G_OBJECT (request), callback, + user_data, file_request_send_async); + g_simple_async_result_run_in_thread (result, handle_file_request, + G_PRIORITY_DEFAULT, cancellable); +} + +static GInputStream * +file_request_send_finish (SoupRequest *request, + GAsyncResult *result, + GError **error) +{ + GInputStream *stream; + + stream = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result)); + g_object_unref (result); + + /* 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 (!stream) /* We must always return something */ + stream = g_memory_input_stream_new (); + + return stream; +} + +static goffset +file_request_get_content_length (SoupRequest *request) +{ + EFileRequest *efr = E_FILE_REQUEST (request); + + d(printf("Content-Length: %d bytes\n", efr->priv->content_length)); + return efr->priv->content_length; +} + +static const gchar * +file_request_get_content_type (SoupRequest *request) +{ + EFileRequest *efr = E_FILE_REQUEST (request); + + d(printf("Content-Type: %s\n", efr->priv->content_type)); + + return efr->priv->content_type; +} + +static const char *data_schemes[] = { "evo-file", NULL }; + +static void +e_file_request_class_init (EFileRequestClass *class) +{ + GObjectClass *object_class; + SoupRequestClass *request_class; + + g_type_class_add_private (class, sizeof (EFileRequestPrivate)); + + object_class = G_OBJECT_CLASS (class); + object_class->finalize = file_request_finalize; + + request_class = SOUP_REQUEST_CLASS (class); + request_class->schemes = data_schemes; + request_class->send_async = file_request_send_async; + request_class->send_finish = file_request_send_finish; + request_class->get_content_type = file_request_get_content_type; + request_class->get_content_length = file_request_get_content_length; + request_class->check_uri = file_request_check_uri; +} + +static void +e_file_request_init (EFileRequest *request) +{ + request->priv = E_FILE_REQUEST_GET_PRIVATE (request); +} + diff --git a/e-util/e-file-request.h b/e-util/e-file-request.h new file mode 100644 index 0000000000..b8dd278c87 --- /dev/null +++ b/e-util/e-file-request.h @@ -0,0 +1,65 @@ +/* + * e-file-request.h + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with the program; if not, see <http://www.gnu.org/licenses/> + * + */ + +#ifndef E_FILE_REQUEST_H +#define E_FILE_REQUEST_H + +#define LIBSOUP_USE_UNSTABLE_REQUEST_API + +#include <libsoup/soup.h> +#include <libsoup/soup-request.h> + +/* Standard GObject macros */ +#define E_TYPE_FILE_REQUEST \ + (e_file_request_get_type ()) +#define E_FILE_REQUEST(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST \ + ((obj), E_TYPE_FILE_REQUEST, EFileRequest)) +#define E_FILE_REQUEST_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_CAST \ + ((cls), E_TYPE_FILE_REQUEST, EFileRequestClass)) +#define E_IS_FILE_REQUEST(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE \ + ((obj), E_TYPE_FILE_REQUEST)) +#define E_IS_FILE_REQUEST_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_TYPE \ + ((cls), E_TYPE_FILE_REQUEST)) +#define E_FILE_REQUEST_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS \ + ((obj), E_TYPE_FILE_REQUEST, EFileRequestClass)) + +G_BEGIN_DECLS + +typedef struct _EFileRequest EFileRequest; +typedef struct _EFileRequestClass EFileRequestClass; +typedef struct _EFileRequestPrivate EFileRequestPrivate; + +struct _EFileRequest { + SoupRequest parent; + EFileRequestPrivate *priv; +}; + +struct _EFileRequestClass { + SoupRequestClass parent; +}; + +GType e_file_request_get_type (void) G_GNUC_CONST; + +G_END_DECLS + +#endif /* E_FILE_REQUEST_H */ diff --git a/e-util/e-stock-request.c b/e-util/e-stock-request.c new file mode 100644 index 0000000000..bdd12be358 --- /dev/null +++ b/e-util/e-stock-request.c @@ -0,0 +1,257 @@ +/* + * e-stock-request.c + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with the program; if not, see <http://www.gnu.org/licenses/> + * + */ + +#define LIBSOUP_USE_UNSTABLE_REQUEST_API + +#include "e-stock-request.h" + +#include <libsoup/soup.h> + +#include <e-util/e-util.h> + +#include <string.h> + +#define d(x) + +#define E_STOCK_REQUEST_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE \ + ((obj), E_TYPE_STOCK_REQUEST, EStockRequestPrivate)) + +struct _EStockRequestPrivate { + gchar *content_type; + gint content_length; +}; + +G_DEFINE_TYPE (EStockRequest, e_stock_request, SOUP_TYPE_REQUEST) + +static void +handle_stock_request (GSimpleAsyncResult *res, + 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; + GtkStyleContext *context; + GtkWidgetPath *path; + GtkIconSet *set; + EStockRequest *request; + + request = E_STOCK_REQUEST (object); + uri = soup_request_get_uri (SOUP_REQUEST (object)); + + query = NULL; + if (uri->query) + query = soup_form_decode (uri->query); + + if (query) { + a_size = g_hash_table_lookup (query, "size"); + g_hash_table_destroy (query); + } else { + a_size = NULL; + } + + if (!a_size) { + size = GTK_ICON_SIZE_BUTTON; + } else { + size = atoi (a_size); + } + + /* Try style context first */ + context = gtk_style_context_new (); + path = gtk_widget_path_new (); + 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); + + 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; + } + + 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; + } + + } else { + GdkPixbuf *pixbuf; + + pixbuf = gtk_icon_info_get_builtin_pixbuf (icon_info); + if (pixbuf) { + 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; + + 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); + + request->priv->content_type = g_strdup("image/png"); + request->priv->content_length = buff_len; + + g_object_unref (pixbuf); + } + + if (buffer) { + 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); + } + + gtk_widget_path_free (path); + g_object_unref (context); + +} +static void +stock_request_finalize (GObject *object) +{ + EStockRequest *request = E_STOCK_REQUEST (object); + + if (request->priv->content_type) { + g_free (request->priv->content_type); + request->priv->content_type = NULL; + } + + G_OBJECT_CLASS (e_stock_request_parent_class)->finalize (object); +} + +static gboolean +stock_request_check_uri (SoupRequest *request, + 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) +{ + GSimpleAsyncResult *result; + + d(printf("received request for %s\n", soup_uri_to_string (uri, FALSE))); + + result = g_simple_async_result_new (G_OBJECT (request), callback, + user_data, stock_request_send_async); + g_simple_async_result_run_in_thread (result, handle_stock_request, + G_PRIORITY_DEFAULT, cancellable); +} + +static GInputStream * +stock_request_send_finish (SoupRequest *request, + GAsyncResult *result, + GError **error) +{ + GInputStream *stream; + + stream = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result)); + g_object_unref (result); + + /* 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 (!stream) /* We must always return something */ + stream = g_memory_input_stream_new (); + + return stream; +} + +static goffset +stock_request_get_content_length (SoupRequest *request) +{ + EStockRequest *esr = E_STOCK_REQUEST (request); + + d(printf("Content-Length: %d bytes\n", esr->priv->content_length)); + return esr->priv->content_length; +} + +static const gchar * +stock_request_get_content_type (SoupRequest *request) +{ + EStockRequest *esr = E_STOCK_REQUEST (request); + + d(printf("Content-Type: %s\n", esr->priv->content_type)); + + return esr->priv->content_type; +} + +static const char *data_schemes[] = { "gtk-stock", NULL }; + +static void +e_stock_request_class_init (EStockRequestClass *class) +{ + GObjectClass *object_class; + SoupRequestClass *request_class; + + g_type_class_add_private (class, sizeof (EStockRequestPrivate)); + + object_class = G_OBJECT_CLASS (class); + object_class->finalize = stock_request_finalize; + + request_class = SOUP_REQUEST_CLASS (class); + request_class->schemes = data_schemes; + 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; +} + +static void +e_stock_request_init (EStockRequest *request) +{ + request->priv = E_STOCK_REQUEST_GET_PRIVATE (request); +} + diff --git a/e-util/e-stock-request.h b/e-util/e-stock-request.h new file mode 100644 index 0000000000..39a22ba424 --- /dev/null +++ b/e-util/e-stock-request.h @@ -0,0 +1,65 @@ +/* + * e-stock-request.h + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with the program; if not, see <http://www.gnu.org/licenses/> + * + */ + +#ifndef E_STOCK_REQUEST_H +#define E_STOCK_REQUEST_H + +#define LIBSOUP_USE_UNSTABLE_REQUEST_API + +#include <libsoup/soup.h> +#include <libsoup/soup-request.h> + +/* Standard GObject macros */ +#define E_TYPE_STOCK_REQUEST \ + (e_stock_request_get_type ()) +#define E_STOCK_REQUEST(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST \ + ((obj), E_TYPE_STOCK_REQUEST, EStockRequest)) +#define E_STOCK_REQUEST_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_CAST \ + ((cls), E_TYPE_STOCK_REQUEST, EStockRequestClass)) +#define E_IS_STOCK_REQUEST(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE \ + ((obj), E_TYPE_STOCK_REQUEST)) +#define E_IS_STOCK_REQUEST_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_TYPE \ + ((cls), E_TYPE_STOCK_REQUEST)) +#define E_STOCK_REQUEST_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS \ + ((obj), E_TYPE_STOCK_REQUEST, EStockRequestClass)) + +G_BEGIN_DECLS + +typedef struct _EStockRequest EStockRequest; +typedef struct _EStockRequestClass EStockRequestClass; +typedef struct _EStockRequestPrivate EStockRequestPrivate; + +struct _EStockRequest { + SoupRequest parent; + EStockRequestPrivate *priv; +}; + +struct _EStockRequestClass { + SoupRequestClass parent; +}; + +GType e_stock_request_get_type (void) G_GNUC_CONST; + +G_END_DECLS + +#endif /* E_STOCK_REQUEST_H */ |