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

No issues found

  1 /*
  2  * e-file-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-file-request.h"
 22 
 23 #include <libsoup/soup.h>
 24 
 25 #include <e-util/e-util.h>
 26 
 27 #include <string.h>
 28 
 29 #define d(x)
 30 
 31 #define E_FILE_REQUEST_GET_PRIVATE(obj) \
 32 	(G_TYPE_INSTANCE_GET_PRIVATE \
 33 	((obj), E_TYPE_FILE_REQUEST, EFileRequestPrivate))
 34 
 35 struct _EFileRequestPrivate {
 36 	gchar *content_type;
 37 	gint content_length;
 38 };
 39 
 40 G_DEFINE_TYPE (EFileRequest, e_file_request, SOUP_TYPE_REQUEST)
 41 
 42 static void
 43 handle_file_request (GSimpleAsyncResult *res,
 44                      GObject *object,
 45                      GCancellable *cancellable)
 46 {
 47 	EFileRequest *request = E_FILE_REQUEST (object);
 48 	SoupURI *uri;
 49 	GInputStream *stream;
 50 	gchar *contents;
 51 	gsize length;
 52 
 53 	if (g_cancellable_is_cancelled (cancellable))
 54 		return;
 55 
 56 	uri = soup_request_get_uri (SOUP_REQUEST (request));
 57 
 58 	if (g_file_get_contents (uri->path, &contents, &length, NULL)) {
 59 
 60 		request->priv->content_type = g_content_type_guess (uri->path, NULL, 0, NULL);
 61 		request->priv->content_length = length;
 62 
 63 		stream = g_memory_input_stream_new_from_data (
 64 				contents, length, (GDestroyNotify) g_free);
 65 		g_simple_async_result_set_op_res_gpointer (res, stream, NULL);
 66 	}
 67 }
 68 
 69 static void
 70 file_request_finalize (GObject *object)
 71 {
 72 	EFileRequest *request = E_FILE_REQUEST (object);
 73 
 74 	if (request->priv->content_type) {
 75 		g_free (request->priv->content_type);
 76 		request->priv->content_type = NULL;
 77 	}
 78 
 79 	G_OBJECT_CLASS (e_file_request_parent_class)->finalize (object);
 80 }
 81 
 82 static gboolean
 83 file_request_check_uri (SoupRequest *request,
 84                        SoupURI *uri,
 85                        GError **error)
 86 {
 87 	return (strcmp (uri->scheme, "evo-file") == 0);
 88 }
 89 
 90 static void
 91 file_request_send_async (SoupRequest *request,
 92                          GCancellable *cancellable,
 93                          GAsyncReadyCallback callback,
 94                          gpointer user_data)
 95 {
 96 	GSimpleAsyncResult *simple;
 97 
 98 	d (printf ("received request for %s\n", soup_uri_to_string (uri, FALSE)));
 99 
100 	/* WebKit won't allow us to load data through local file:// protocol
101 	 * when using "remote" mail:// protocol, so we have evo-file://
102 	 * which WebKit thinks it's remote, but in fact it behaves like
103 	 * oridnary file:// */
104 
105 	simple = g_simple_async_result_new (
106 		G_OBJECT (request), callback, user_data,
107 		file_request_send_async);
108 
109 	g_simple_async_result_set_check_cancellable (simple, cancellable);
110 
111 	g_simple_async_result_run_in_thread (
112 		simple, handle_file_request,
113 		G_PRIORITY_DEFAULT, cancellable);
114 
115 	g_object_unref (simple);
116 }
117 
118 static GInputStream *
119 file_request_send_finish (SoupRequest *request,
120                           GAsyncResult *result,
121                           GError **error)
122 {
123 	GSimpleAsyncResult *simple;
124 	GInputStream *stream;
125 
126 	simple = G_SIMPLE_ASYNC_RESULT (result);
127 	stream = g_simple_async_result_get_op_res_gpointer (simple);
128 
129 	/* Reset the stream before passing it back to webkit */
130 	if (stream && G_IS_SEEKABLE (stream))
131 		g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, NULL);
132 
133 	if (!stream) /* We must always return something */
134 		stream = g_memory_input_stream_new ();
135 
136 	return stream;
137 }
138 
139 static goffset
140 file_request_get_content_length (SoupRequest *request)
141 {
142 	EFileRequest *efr = E_FILE_REQUEST (request);
143 
144 	d (printf ("Content-Length: %d bytes\n", efr->priv->content_length));
145 
146 	return efr->priv->content_length;
147 }
148 
149 static const gchar *
150 file_request_get_content_type (SoupRequest *request)
151 {
152 	EFileRequest *efr = E_FILE_REQUEST (request);
153 
154 	d (printf ("Content-Type: %s\n", efr->priv->content_type));
155 
156 	return efr->priv->content_type;
157 }
158 
159 static const gchar *data_schemes[] = { "evo-file", NULL };
160 
161 static void
162 e_file_request_class_init (EFileRequestClass *class)
163 {
164 	GObjectClass *object_class;
165 	SoupRequestClass *request_class;
166 
167 	g_type_class_add_private (class, sizeof (EFileRequestPrivate));
168 
169 	object_class = G_OBJECT_CLASS (class);
170 	object_class->finalize = file_request_finalize;
171 
172 	request_class = SOUP_REQUEST_CLASS (class);
173 	request_class->schemes = data_schemes;
174 	request_class->send_async = file_request_send_async;
175 	request_class->send_finish = file_request_send_finish;
176 	request_class->get_content_type = file_request_get_content_type;
177 	request_class->get_content_length = file_request_get_content_length;
178 	request_class->check_uri = file_request_check_uri;
179 }
180 
181 static void
182 e_file_request_init (EFileRequest *request)
183 {
184 	request->priv = E_FILE_REQUEST_GET_PRIVATE (request);
185 }