evolution-3.6.4/e-util/e-stock-request.c

No issues found

  1 /*
  2  * e-stock-request.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  */
 18 
 19 #define LIBSOUP_USE_UNSTABLE_REQUEST_API
 20 
 21 #include "e-stock-request.h"
 22 
 23 #include <stdlib.h>
 24 #include <libsoup/soup.h>
 25 
 26 #include <e-util/e-util.h>
 27 
 28 #include <string.h>
 29 
 30 #define d(x)
 31 
 32 #define E_STOCK_REQUEST_GET_PRIVATE(obj) \
 33 	(G_TYPE_INSTANCE_GET_PRIVATE \
 34 	((obj), E_TYPE_STOCK_REQUEST, EStockRequestPrivate))
 35 
 36 struct _EStockRequestPrivate {
 37 	gchar *content_type;
 38 	gint content_length;
 39 };
 40 
 41 G_DEFINE_TYPE (EStockRequest, e_stock_request, SOUP_TYPE_REQUEST)
 42 
 43 static void
 44 handle_stock_request (GSimpleAsyncResult *res,
 45                       GObject *object,
 46                       GCancellable *cancellable)
 47 {
 48 	SoupURI *uri;
 49 	GHashTable *query;
 50 	GtkIconTheme *icon_theme;
 51 	GtkIconInfo *icon_info;
 52 	const gchar *file;
 53 	gchar *a_size;
 54 	gssize size;
 55 	gchar *buffer;
 56 	gsize buff_len;
 57 	GtkStyleContext *context;
 58 	GtkWidgetPath *path;
 59 	GtkIconSet *set;
 60 	EStockRequest *request;
 61 
 62 	request = E_STOCK_REQUEST (object);
 63 	uri = soup_request_get_uri (SOUP_REQUEST (object));
 64 
 65 	query = NULL;
 66 	if (uri->query)
 67 		query = soup_form_decode (uri->query);
 68 
 69 	if (query) {
 70 		a_size = g_hash_table_lookup (query, "size");
 71 		if (a_size) {
 72 			size = atoi (a_size);
 73 		} else {
 74 			size = GTK_ICON_SIZE_BUTTON;
 75 		}
 76 		g_hash_table_destroy (query);
 77 	} else {
 78 		size = GTK_ICON_SIZE_BUTTON;
 79 	}
 80 
 81 	/* Try style context first */
 82 	context = gtk_style_context_new ();
 83 	path = gtk_widget_path_new ();
 84 	gtk_widget_path_append_type (path, GTK_TYPE_WINDOW);
 85 	gtk_widget_path_append_type (path, GTK_TYPE_BUTTON);
 86 	gtk_style_context_set_path (context, path);
 87 
 88 	set = gtk_style_context_lookup_icon_set (context, uri->host);
 89 	if (!set) {
 90 		/* Fallback to icon theme */
 91 		icon_theme = gtk_icon_theme_get_default ();
 92 		icon_info = gtk_icon_theme_lookup_icon (
 93 				icon_theme, uri->host, size,
 94 				GTK_ICON_LOOKUP_USE_BUILTIN);
 95 		if (!icon_info) {
 96 			gtk_widget_path_free (path);
 97 			g_object_unref (context);
 98 			return;
 99 		}
100 
101 		file = gtk_icon_info_get_filename (icon_info);
102 		buffer = NULL;
103 		if (file) {
104 			if (g_file_get_contents (file, &buffer, &buff_len, NULL)) {
105 
106 				request->priv->content_type =
107 					g_content_type_guess (file, NULL, 0, NULL);
108 				request->priv->content_length = buff_len;
109 			}
110 
111 		} else {
112 			GdkPixbuf *pixbuf;
113 
114 			pixbuf = gtk_icon_info_get_builtin_pixbuf (icon_info);
115 			if (pixbuf) {
116 				gdk_pixbuf_save_to_buffer (
117 					pixbuf, &buffer,
118 					&buff_len, "png", NULL, NULL);
119 
120 				request->priv->content_type = g_strdup ("image/png");
121 				request->priv->content_length = buff_len;
122 
123 				g_object_unref (pixbuf);
124 			}
125 		}
126 
127 		gtk_icon_info_free (icon_info);
128 
129 	} else {
130 		GdkPixbuf *pixbuf;
131 
132 		pixbuf = gtk_icon_set_render_icon_pixbuf (set, context, size);
133 				gdk_pixbuf_save_to_buffer (
134 					pixbuf, &buffer,
135 					&buff_len, "png", NULL, NULL);
136 
137 		request->priv->content_type = g_strdup ("image/png");
138 		request->priv->content_length = buff_len;
139 
140 		g_object_unref (pixbuf);
141 	}
142 
143 	if (buffer) {
144 		GInputStream *stream;
145 		stream = g_memory_input_stream_new_from_data (
146 				buffer, buff_len, (GDestroyNotify) g_free);
147 		g_simple_async_result_set_op_res_gpointer (res, stream, NULL);
148 	}
149 
150 	gtk_widget_path_free (path);
151 	g_object_unref (context);
152 
153 }
154 static void
155 stock_request_finalize (GObject *object)
156 {
157 	EStockRequest *request = E_STOCK_REQUEST (object);
158 
159 	if (request->priv->content_type) {
160 		g_free (request->priv->content_type);
161 		request->priv->content_type = NULL;
162 	}
163 
164 	G_OBJECT_CLASS (e_stock_request_parent_class)->finalize (object);
165 }
166 
167 static gboolean
168 stock_request_check_uri (SoupRequest *request,
169                        SoupURI *uri,
170                        GError **error)
171 {
172 	return (strcmp (uri->scheme, "gtk-stock") == 0);
173 }
174 
175 static void
176 stock_request_send_async (SoupRequest *request,
177                          GCancellable *cancellable,
178                          GAsyncReadyCallback callback,
179                          gpointer user_data)
180 {
181 	GSimpleAsyncResult *simple;
182 
183 	d (printf ("received request for %s\n", soup_uri_to_string (uri, FALSE)));
184 
185 	simple = g_simple_async_result_new (
186 		G_OBJECT (request), callback, user_data,
187 		stock_request_send_async);
188 
189 	g_simple_async_result_set_check_cancellable (simple, cancellable);
190 
191 	g_simple_async_result_run_in_thread (
192 		simple, handle_stock_request,
193 		G_PRIORITY_DEFAULT, cancellable);
194 
195 	g_object_unref (simple);
196 }
197 
198 static GInputStream *
199 stock_request_send_finish (SoupRequest *request,
200                            GAsyncResult *result,
201                            GError **error)
202 {
203 	GSimpleAsyncResult *simple;
204 	GInputStream *stream;
205 
206 	simple = G_SIMPLE_ASYNC_RESULT (result);
207 	stream = g_simple_async_result_get_op_res_gpointer (simple);
208 
209 	/* Reset the stream before passing it back to webkit */
210 	if (stream && G_IS_SEEKABLE (stream))
211 		g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, NULL);
212 
213 	if (!stream) /* We must always return something */
214 		stream = g_memory_input_stream_new ();
215 
216 	return stream;
217 }
218 
219 static goffset
220 stock_request_get_content_length (SoupRequest *request)
221 {
222 	EStockRequest *esr = E_STOCK_REQUEST (request);
223 
224 	d (printf ("Content-Length: %d bytes\n", esr->priv->content_length));
225 	return esr->priv->content_length;
226 }
227 
228 static const gchar *
229 stock_request_get_content_type (SoupRequest *request)
230 {
231 	EStockRequest *esr = E_STOCK_REQUEST (request);
232 
233 	d (printf ("Content-Type: %s\n", esr->priv->content_type));
234 
235 	return esr->priv->content_type;
236 }
237 
238 static const gchar *data_schemes[] = { "gtk-stock", NULL };
239 
240 static void
241 e_stock_request_class_init (EStockRequestClass *class)
242 {
243 	GObjectClass *object_class;
244 	SoupRequestClass *request_class;
245 
246 	g_type_class_add_private (class, sizeof (EStockRequestPrivate));
247 
248 	object_class = G_OBJECT_CLASS (class);
249 	object_class->finalize = stock_request_finalize;
250 
251 	request_class = SOUP_REQUEST_CLASS (class);
252 	request_class->schemes = data_schemes;
253 	request_class->send_async = stock_request_send_async;
254 	request_class->send_finish = stock_request_send_finish;
255 	request_class->get_content_type = stock_request_get_content_type;
256 	request_class->get_content_length = stock_request_get_content_length;
257 	request_class->check_uri = stock_request_check_uri;
258 }
259 
260 static void
261 e_stock_request_init (EStockRequest *request)
262 {
263 	request->priv = E_STOCK_REQUEST_GET_PRIVATE (request);
264 }