Re: About tny-mime-part-write-to-stream



This patch should solve the issues that you just pointed out.


O Xov, 20-12-2007 ás 21:20 +0100, Philip Van Hoof escribiu:
> Shouldn't functions like these also return a gssize?
> 
> On Thu, 2007-12-20 at 19:58 +0100, Felipe Erias Morandeira wrote:
> >  static void
> > -tny_camel_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream)
> > +tny_camel_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream, GError **err)
> >  {
> > -       TNY_CAMEL_MIME_PART_GET_CLASS (self)->decode_to_stream_func (self, stream);
> > +       TNY_CAMEL_MIME_PART_GET_CLASS (self)->decode_to_stream_func (self, stream, err);
> >         return;
> >  }
> 
Index: tinymail/libtinymail-test/tny-mime-part-test.c
===================================================================
--- tinymail/libtinymail-test/tny-mime-part-test.c	(revision 3168)
+++ tinymail/libtinymail-test/tny-mime-part-test.c	(working copy)
@@ -132,7 +132,7 @@
 /* TODO (this one crashes)
 
 	tny_mime_part_construct_from_stream (iface, from, "text/plain");
-	tny_mime_part_write_to_stream (iface, to);
+	tny_mime_part_write_to_stream (iface, to, NULL);
 
 	while (!tny_stream_is_eos (to) && n < 1)
 	{
Index: tinymail/libtinymail-camel/tny-camel-mime-part.c
===================================================================
--- tinymail/libtinymail-camel/tny-camel-mime-part.c	(revision 3168)
+++ tinymail/libtinymail-camel/tny-camel-mime-part.c	(working copy)
@@ -26,6 +26,7 @@
 #endif
 
 #include <string.h>
+#include <errno.h>
 
 #include <tny-mime-part.h>
 #include <tny-camel-mime-part.h>
@@ -37,6 +38,7 @@
 #include <tny-list.h>
 #include <tny-simple-list.h>
 #include <tny-camel-msg.h>
+#include <tny-error.h>
 
 static GObjectClass *parent_class = NULL;
 
@@ -244,7 +246,7 @@
 {
 	DecodeAsyncInfo *info = g_slice_new0 (DecodeAsyncInfo);
 
-	tny_mime_part_decode_to_stream (self, stream);
+	tny_mime_part_decode_to_stream (self, stream, NULL);
 
 	info->self = g_object_ref (self);
 	info->stream = g_object_ref (stream);
@@ -537,15 +539,14 @@
 	return FALSE;
 }
 
-static void
-tny_camel_mime_part_write_to_stream (TnyMimePart *self, TnyStream *stream)
+static gssize
+tny_camel_mime_part_write_to_stream (TnyMimePart *self, TnyStream *stream, GError **err)
 {
-	TNY_CAMEL_MIME_PART_GET_CLASS (self)->write_to_stream_func (self, stream);
-	return;
+	return TNY_CAMEL_MIME_PART_GET_CLASS (self)->write_to_stream_func (self, stream, err);
 }
 
-static void
-tny_camel_mime_part_write_to_stream_default (TnyMimePart *self, TnyStream *stream)
+static gssize
+tny_camel_mime_part_write_to_stream_default (TnyMimePart *self, TnyStream *stream, GError **err)
 {
 	TnyCamelMimePartPriv *priv = TNY_CAMEL_MIME_PART_GET_PRIVATE (self);
 	CamelDataWrapper *wrapper;
@@ -567,24 +568,34 @@
 		g_error (_("Mime part does not yet have a source stream, use "
 			"tny_mime_part_construct_from_stream first"));
 		camel_object_unref (cstream);
-		return;
+		g_set_error (err, TNY_FOLDER_ERROR, 
+				TNY_ERROR_UNSPEC,
+				_("Mime part does not yet have a source stream, use "
+				"tny_mime_part_construct_from_stream first"));
+		return -1;
 	}
 
 	/* This should work but doesn't . . .
 	camel_data_wrapper_write_to_stream (wrapper, cstream); */
 
 	camel_stream_reset (wrapper->stream);
-	camel_stream_write_to_stream (wrapper->stream, cstream);
+	gssize bytes = (gssize) camel_stream_write_to_stream (wrapper->stream, cstream);
 
 	camel_object_unref (cstream);
 	camel_object_unref (medium);
 
-	return;
+	if (bytes < 0 && err != NULL) {
+		g_set_error (err, TNY_FOLDER_ERROR, 
+				TNY_ERROR_UNSPEC,
+				strerror (errno));
+	}
+	
+	return bytes;
 }
 
 
 
-static void
+static ssize_t
 camel_stream_format_text (CamelDataWrapper *dw, CamelStream *stream)
 {
 	/* Stolen from evolution, evil evil me!! moehahah */
@@ -593,6 +604,7 @@
 	CamelMimeFilterCharset *filter;
 	const char *charset = "UTF-8"; /* I default to UTF-8, like it or not */
 	CamelMimeFilterWindows *windows = NULL;
+	ssize_t bytes = -1;
 
 	if (dw->mime_type && (charset = camel_content_type_param 
 			(dw->mime_type, "charset")) && 
@@ -623,25 +635,24 @@
 		camel_object_unref (filter);
 	}
 
-	camel_data_wrapper_decode_to_stream (dw, (CamelStream *)filter_stream);
+	bytes = camel_data_wrapper_decode_to_stream (dw, (CamelStream *)filter_stream);
 	camel_stream_flush ((CamelStream *)filter_stream);
 	camel_object_unref (filter_stream);
 
 	if (windows)
 		camel_object_unref(windows);
 
-	return;
+	return bytes;
 }
 
-static void
-tny_camel_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream)
+static gssize
+tny_camel_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream, GError **err)
 {
-	TNY_CAMEL_MIME_PART_GET_CLASS (self)->decode_to_stream_func (self, stream);
-	return;
+	return TNY_CAMEL_MIME_PART_GET_CLASS (self)->decode_to_stream_func (self, stream, err);
 }
 
-static void
-tny_camel_mime_part_decode_to_stream_default (TnyMimePart *self, TnyStream *stream)
+static gssize
+tny_camel_mime_part_decode_to_stream_default (TnyMimePart *self, TnyStream *stream, GError **err)
 {
 
 	TnyCamelMimePartPriv *priv = TNY_CAMEL_MIME_PART_GET_PRIVATE (self);
@@ -667,15 +678,23 @@
 		return;
 	}
 
+	gssize bytes = -1;
+	
 	if (camel_content_type_is (wrapper->mime_type, "text", "*"))
-		camel_stream_format_text (wrapper, cstream);
+		bytes = (gssize) camel_stream_format_text (wrapper, cstream);
 	else
-		camel_data_wrapper_decode_to_stream (wrapper, cstream);
+		bytes = (gssize) camel_data_wrapper_decode_to_stream (wrapper, cstream);
 
 	camel_object_unref (cstream);
 	camel_object_unref (medium);
+	
+	if (bytes < 0 && err != NULL) {
+		g_set_error (err, TNY_FOLDER_ERROR, 
+				TNY_ERROR_UNSPEC,
+				strerror (errno));
+	}
 
-	return;
+	return bytes;
 }
 
 static gint
Index: tinymail/libtinymail-camel/tny-camel-mime-part.h
===================================================================
--- tinymail/libtinymail-camel/tny-camel-mime-part.h	(revision 3168)
+++ tinymail/libtinymail-camel/tny-camel-mime-part.h	(working copy)
@@ -52,8 +52,8 @@
 	const gchar* (*get_content_type_func) (TnyMimePart *self);
 	gboolean (*content_type_is_func) (TnyMimePart *self, const gchar *content_type);
 	TnyStream* (*get_stream_func) (TnyMimePart *self);
-	void (*decode_to_stream_func) (TnyMimePart *self, TnyStream *stream);
-	void (*write_to_stream_func) (TnyMimePart *self, TnyStream *stream);
+	gssize (*decode_to_stream_func) (TnyMimePart *self, TnyStream *stream, GError **err);
+	gssize (*write_to_stream_func) (TnyMimePart *self, TnyStream *stream, GError **err);
 	gint (*construct_from_stream_func) (TnyMimePart *self, TnyStream *stream, const gchar *type);
 	const gchar* (*get_filename_func) (TnyMimePart *self);
 	const gchar* (*get_content_id_func) (TnyMimePart *self);
Index: tinymail/libtinymail-camel/camel-lite/camel/providers/local/camel-maildir-store.c
===================================================================
--- tinymail/libtinymail-camel/camel-lite/camel/providers/local/camel-maildir-store.c	(revision 3168)
+++ tinymail/libtinymail-camel/camel-lite/camel/providers/local/camel-maildir-store.c	(working copy)
@@ -487,8 +487,8 @@
 		if (dir == NULL) {
 			g_free(name);
 			camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
-					     _("Could not scan folder `%s': %s"),
-					     root, g_strerror(errno));
+					     _("Could not scan folder `%s', opendir(`%s') failed: %s"),
+					     root, name, g_strerror(errno));
 			goto fail;
 		}
 
Index: tinymail/libtinymail-camel/tny-camel-bs-mime-part.c
===================================================================
--- tinymail/libtinymail-camel/tny-camel-bs-mime-part.c	(revision 3168)
+++ tinymail/libtinymail-camel/tny-camel-bs-mime-part.c	(working copy)
@@ -124,13 +124,14 @@
 	return ret;
 }
 
-static void
+static gssize
 bs_camel_stream_format_text (CamelStream *from_stream, CamelStream *stream, const gchar *charset, const gchar *encoding)
 {
 	CamelStreamFilter *filter_stream;
 	CamelMimeFilterCharset *filter;
 	CamelMimeFilterWindows *windows = NULL;
-
+	gssize bytes_written = -1;
+		
 	if (g_ascii_strncasecmp (charset, "iso-8859-", 9) == 0) {
 		CamelStream *null;
 
@@ -161,19 +162,20 @@
 		camel_object_unref (filter);
 	}
 
-	decode_to_stream (from_stream, (CamelStream *)filter_stream, encoding, TRUE);
+	bytes_written = (gssize) decode_to_stream (from_stream, (CamelStream *)filter_stream, encoding, TRUE);
 	camel_stream_flush ((CamelStream *)filter_stream);
 	camel_object_unref (filter_stream);
 
 	if (windows)
 		camel_object_unref(windows);
 
-	return;
+	return bytes_written;
 }
 
-static void 
+static gssize 
 decode_from_stream_to (TnyMimePart *self, TnyStream *from_stream, TnyStream *stream, gboolean binary, gboolean decode_text)
 {
+	gssize bytes_written = -1;
 	TnyCamelBsMimePartPriv *priv = TNY_CAMEL_BS_MIME_PART_GET_PRIVATE (self);
 
 	if (decode_text && camel_strcase_equal (priv->bodystructure->content.type, "TEXT")) 
@@ -189,33 +191,35 @@
 		if (!charset)
 			charset = "UTF-8";
 
-		bs_camel_stream_format_text (cfrom_stream, cto_stream, charset, encoding);
+		bytes_written = (gssize) bs_camel_stream_format_text (cfrom_stream, cto_stream, charset, encoding);
 
 		camel_object_unref (cfrom_stream);
 		camel_object_unref (cto_stream);
 	} else {
 		if (binary)
-			tny_stream_write_to_stream (from_stream, stream);
+			bytes_written = tny_stream_write_to_stream (from_stream, stream);
 		else {
 			CamelStream *cfrom_stream = tny_stream_camel_new (from_stream);
 			CamelStream *cto_stream = tny_stream_camel_new (stream);
 			gchar *encoding = priv->bodystructure->encoding;
 
-			decode_to_stream (cfrom_stream, cto_stream, encoding, FALSE);
+			bytes_written = (gssize) decode_to_stream (cfrom_stream, cto_stream, encoding, FALSE);
 
 			camel_object_unref (cfrom_stream);
 			camel_object_unref (cto_stream);
 		}
 	}
+	return bytes_written;
 }
 
-static void 
+static gssize 
 fetch_part (TnyMimePart *self, TnyStream *stream, gboolean decode_text)
 {
 	TnyCamelBsMimePartPriv *priv = TNY_CAMEL_BS_MIME_PART_GET_PRIVATE (self);
 	GError *err = NULL;
 	gboolean binary = TRUE;
 	TnyStream *from_stream;
+	gssize return_value = 0;
 
 	/* binary = !camel_strcase_equal (priv->bodystructure->content.type, "TEXT"); */
 	from_stream = tny_camel_bs_msg_receive_strategy_start_receiving_part (
@@ -224,14 +228,15 @@
 	if (err) {
 		g_warning ("Error while fetching part: %s", err->message);
 		g_error_free (err);
+		return_value = -1;
 	} else if (from_stream)
-		decode_from_stream_to (self, from_stream, stream, binary, decode_text);
+		return_value = decode_from_stream_to (self, from_stream, stream, binary, decode_text);
 
 	if (from_stream)
 		g_object_unref (from_stream);
 
 
-	return;
+	return return_value;
 }
 
 static void 
@@ -402,18 +407,16 @@
 
 }
 
-static void
-tny_camel_bs_mime_part_write_to_stream (TnyMimePart *self, TnyStream *stream)
+static gssize
+tny_camel_bs_mime_part_write_to_stream (TnyMimePart *self, TnyStream *stream, GError **err)
 {
-	TNY_CAMEL_BS_MIME_PART_GET_CLASS (self)->write_to_stream_func (self, stream);
-	return;
+	return TNY_CAMEL_BS_MIME_PART_GET_CLASS (self)->write_to_stream_func (self, stream, err);
 }
 
-static void
-tny_camel_bs_mime_part_write_to_stream_default (TnyMimePart *self, TnyStream *stream)
+static gssize
+tny_camel_bs_mime_part_write_to_stream_default (TnyMimePart *self, TnyStream *stream, GError **err)
 {
-	fetch_part (self, stream, FALSE);
-	return;
+	return fetch_part (self, stream, FALSE);
 }
 
 
@@ -619,18 +622,16 @@
 }
 
 
-static void
-tny_camel_bs_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream)
+static gssize
+tny_camel_bs_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream, GError **err)
 {
-	TNY_CAMEL_BS_MIME_PART_GET_CLASS (self)->decode_to_stream_func (self, stream);
-	return;
+	return TNY_CAMEL_BS_MIME_PART_GET_CLASS (self)->decode_to_stream_func (self, stream, err);
 }
 
-static void
-tny_camel_bs_mime_part_decode_to_stream_default (TnyMimePart *self, TnyStream *stream)
+static gssize
+tny_camel_bs_mime_part_decode_to_stream_default (TnyMimePart *self, TnyStream *stream, GError **err)
 {
-	fetch_part (self, stream, TRUE);
-	return;
+	return fetch_part (self, stream, TRUE);
 }
 
 static gint
Index: tinymail/libtinymail-camel/tny-camel-bs-mime-part.h
===================================================================
--- tinymail/libtinymail-camel/tny-camel-bs-mime-part.h	(revision 3168)
+++ tinymail/libtinymail-camel/tny-camel-bs-mime-part.h	(working copy)
@@ -50,8 +50,8 @@
 	const gchar* (*get_content_type_func) (TnyMimePart *self);
 	gboolean (*content_type_is_func) (TnyMimePart *self, const gchar *content_type);
 	TnyStream* (*get_stream_func) (TnyMimePart *self);
-	void (*decode_to_stream_func) (TnyMimePart *self, TnyStream *stream);
-	void (*write_to_stream_func) (TnyMimePart *self, TnyStream *stream);
+	gssize (*decode_to_stream_func) (TnyMimePart *self, TnyStream *stream, GError **err);
+	gssize (*write_to_stream_func) (TnyMimePart *self, TnyStream *stream, GError **err);
 	gint (*construct_from_stream_func) (TnyMimePart *self, TnyStream *stream, const gchar *type);
 	const gchar* (*get_filename_func) (TnyMimePart *self);
 	const gchar* (*get_content_id_func) (TnyMimePart *self);
Index: tinymail/libtinymailui-webkit/tny-webkit-html-mime-part-view.c
===================================================================
--- tinymail/libtinymailui-webkit/tny-webkit-html-mime-part-view.c	(revision 3168)
+++ tinymail/libtinymailui-webkit/tny-webkit-html-mime-part-view.c	(working copy)
@@ -88,7 +88,7 @@
 
 		dest = tny_webkit_stream_new (GTK_WEBKIT (self));
 		tny_stream_reset (dest);
-		tny_mime_part_decode_to_stream (part, dest);
+		tny_mime_part_decode_to_stream (part, dest, NULL);
 		g_object_unref (G_OBJECT (dest));
 
 		g_object_ref (G_OBJECT (part));
Index: tinymail/libtinymailui/tny-mime-part-save-strategy.c
===================================================================
--- tinymail/libtinymailui/tny-mime-part-save-strategy.c	(revision 3168)
+++ tinymail/libtinymailui/tny-mime-part-save-strategy.c	(working copy)
@@ -78,7 +78,7 @@
  *            fd = open (uri, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
  *            if (fd != -1) {
  *                      TnyStream *stream = tny_fs_stream_new (fd);
- *                      tny_mime_part_decode_to_stream (part, stream);
+ *                      tny_mime_part_decode_to_stream (part, stream, NULL);
  *                      g_object_unref (stream);
  *            }
  *      }
Index: tinymail/libtinymail/tny-mime-part.c
===================================================================
--- tinymail/libtinymail/tny-mime-part.c	(revision 3168)
+++ tinymail/libtinymail/tny-mime-part.c	(working copy)
@@ -618,11 +618,13 @@
  * }
  * </programlisting></informalexample>
  *
+ * returns: Returns %-1 on error, or the number of bytes succesfully
+ * copied across streams.
  * since: 1.0
  * audience: application-developer
  **/
-void
-tny_mime_part_write_to_stream (TnyMimePart *self, TnyStream *stream)
+gssize
+tny_mime_part_write_to_stream (TnyMimePart *self, TnyStream *stream, GError **err)
 {
 #ifdef DBC /* require */
 	g_assert (TNY_IS_MIME_PART (self));
@@ -631,8 +633,7 @@
 	g_assert (TNY_MIME_PART_GET_IFACE (self)->write_to_stream_func != NULL);
 #endif
 
-	TNY_MIME_PART_GET_IFACE (self)->write_to_stream_func (self, stream);
-	return;
+	return TNY_MIME_PART_GET_IFACE (self)->write_to_stream_func (self, stream, err);
 }
 
 
@@ -674,11 +675,13 @@
  * }
  * </programlisting></informalexample>
  *
+ * returns: Returns %-1 on error, or the number of bytes succesfully
+ * copied across streams.
  * since: 1.0
  * audience: application-developer
  **/
-void
-tny_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream)
+gssize
+tny_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream, GError **err)
 {
 #ifdef DBC /* require */
 	g_assert (TNY_IS_MIME_PART (self));
@@ -687,8 +690,7 @@
 	g_assert (TNY_MIME_PART_GET_IFACE (self)->decode_to_stream_func != NULL);
 #endif
 
-	TNY_MIME_PART_GET_IFACE (self)->decode_to_stream_func (self, stream);
-	return;
+	return TNY_MIME_PART_GET_IFACE (self)->decode_to_stream_func (self, stream, err);
 }
 
 
Index: tinymail/libtinymail/tny-mime-part.h
===================================================================
--- tinymail/libtinymail/tny-mime-part.h	(revision 3168)
+++ tinymail/libtinymail/tny-mime-part.h	(working copy)
@@ -46,8 +46,8 @@
 	const gchar* (*get_content_type_func) (TnyMimePart *self);
 	gboolean (*content_type_is_func) (TnyMimePart *self, const gchar *content_type);
 	TnyStream* (*get_stream_func) (TnyMimePart *self);
-	void (*decode_to_stream_func) (TnyMimePart *self, TnyStream *stream);
-	void (*write_to_stream_func) (TnyMimePart *self, TnyStream *stream);
+	gssize (*decode_to_stream_func) (TnyMimePart *self, TnyStream *stream, GError **err);
+	gssize (*write_to_stream_func) (TnyMimePart *self, TnyStream *stream, GError **err);
 	gint (*construct_from_stream_func) (TnyMimePart *self, TnyStream *stream, const gchar *type);
 	const gchar* (*get_filename_func) (TnyMimePart *self);
 	const gchar* (*get_content_id_func) (TnyMimePart *self);
@@ -75,7 +75,7 @@
 const gchar* tny_mime_part_get_content_type (TnyMimePart *self);
 gboolean tny_mime_part_content_type_is (TnyMimePart *self, const gchar *type);
 TnyStream* tny_mime_part_get_stream (TnyMimePart *self);
-void tny_mime_part_write_to_stream (TnyMimePart *self, TnyStream *stream);
+gssize tny_mime_part_write_to_stream (TnyMimePart *self, TnyStream *stream, GError **err);
 gint tny_mime_part_construct_from_stream (TnyMimePart *self, TnyStream *stream, const gchar *type);
 const gchar* tny_mime_part_get_filename (TnyMimePart *self);
 const gchar* tny_mime_part_get_content_id (TnyMimePart *self);
@@ -89,7 +89,7 @@
 void tny_mime_part_set_content_type (TnyMimePart *self, const gchar *contenttype);
 void tny_mime_part_set_purged (TnyMimePart *self);
 gboolean tny_mime_part_is_attachment (TnyMimePart *self);
-void tny_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream);
+gssize tny_mime_part_decode_to_stream (TnyMimePart *self, TnyStream *stream, GError **err);
 void tny_mime_part_get_parts (TnyMimePart *self, TnyList *list);
 gint tny_mime_part_add_part (TnyMimePart *self, TnyMimePart *part);
 void tny_mime_part_del_part (TnyMimePart *self, TnyMimePart *part);


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