[balsa/64-save-empty-attachment: 69/69] body: implement libbalsa_message_body_check_if_empty()
- From: Peter Bloomfield <peterb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [balsa/64-save-empty-attachment: 69/69] body: implement libbalsa_message_body_check_if_empty()
- Date: Sat, 16 Apr 2022 23:19:16 +0000 (UTC)
commit 254b38565553cb9d84a0c774c3e8a8880731938f
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date: Mon Jan 17 17:20:48 2022 -0500
body: implement libbalsa_message_body_check_if_empty()
balsa-mime-widget-callbacks: Warn about empty part
Do not try to save an empty attachment.
libbalsa/body.c | 51 +++++++++++++++++++++++++++++++++++++++
libbalsa/body.h | 3 +++
src/balsa-mime-widget-callbacks.c | 20 +++++++++++++++
3 files changed, 74 insertions(+)
---
diff --git a/libbalsa/body.c b/libbalsa/body.c
index 11da495d6..6a24cfa83 100644
--- a/libbalsa/body.c
+++ b/libbalsa/body.c
@@ -779,6 +779,57 @@ libbalsa_message_body_save_stream(LibBalsaMessageBody * body,
return len >= 0;
}
+/*
+ * libbalsa_message_body_check_if_empty
+ *
+ * body: the message body to check
+ * is_empty: location to store the result
+ * err: a GError
+ *
+ * Returns FALSE if the body could not be checked; the error is reported
+ * in err.
+ *
+ * This seems complicated, but see https://gitlab.gnome.org/GNOME/glib/-/blob/main/glib/gerror.c#L358
+ */
+gboolean
+libbalsa_message_body_check_if_empty(LibBalsaMessageBody * body,
+ gboolean *is_empty,
+ GError **err)
+{
+ GMimeStream *body_stream;
+ gboolean retval = FALSE;
+
+ g_return_val_if_fail(body != NULL, FALSE);
+
+ body_stream = libbalsa_message_body_get_stream(body, err);
+
+ if (body_stream != NULL) {
+ gint64 len;
+
+ len = g_mime_stream_length(body_stream);
+
+ if (len < 0) {
+ char buf[1];
+
+ len = g_mime_stream_read(body_stream, buf, 1);
+ }
+
+ if (len < 0) {
+ g_set_error(err, LIBBALSA_ERROR_QUARK, 1,
+ _("Failed to read stream"));
+ } else {
+ if (is_empty != NULL)
+ *is_empty = (len == 0);
+
+ retval = TRUE;
+ }
+
+ g_object_unref(body_stream);
+ }
+
+ return retval;
+}
+
gchar *
libbalsa_message_body_get_mime_type(LibBalsaMessageBody * body)
{
diff --git a/libbalsa/body.h b/libbalsa/body.h
index d201ed120..d45636fb2 100644
--- a/libbalsa/body.h
+++ b/libbalsa/body.h
@@ -107,6 +107,9 @@ gboolean libbalsa_message_body_save_stream(LibBalsaMessageBody * body,
GMimeStream * dest,
gboolean filter_crlf,
GError **err);
+gboolean libbalsa_message_body_check_if_empty(LibBalsaMessageBody * body,
+ gboolean *is_empty,
+ GError **err);
gboolean libbalsa_message_body_save(LibBalsaMessageBody * body,
const gchar * filename, mode_t mode,
gboolean filter_crlf, GError **err);
diff --git a/src/balsa-mime-widget-callbacks.c b/src/balsa-mime-widget-callbacks.c
index c26e54773..7c3776971 100644
--- a/src/balsa-mime-widget-callbacks.c
+++ b/src/balsa-mime-widget-callbacks.c
@@ -67,6 +67,7 @@ balsa_mime_widget_ctx_menu_save(GtkWidget * parent_widget,
LibBalsaMessageBody * mime_body)
{
gchar *cont_type, *title;
+ gboolean is_empty;
GtkWidget *save_dialog;
gchar *file_uri;
LibbalsaVfs *save_file;
@@ -76,6 +77,25 @@ balsa_mime_widget_ctx_menu_save(GtkWidget * parent_widget,
g_return_if_fail(mime_body != NULL);
cont_type = libbalsa_message_body_get_mime_type(mime_body);
+
+
+ if (!libbalsa_message_body_check_if_empty(mime_body, &is_empty, &err)) {
+ balsa_information(LIBBALSA_INFORMATION_ERROR,
+ _("Could not access %s MIME Part: %s"), cont_type, err->message);
+ g_error_free(err);
+ g_free(cont_type);
+
+ return;
+ }
+
+ if (is_empty) {
+ balsa_information(LIBBALSA_INFORMATION_WARNING,
+ _("Empty %s MIME Part was not saved"), cont_type);
+ g_free(cont_type);
+
+ return;
+ }
+
title = g_strdup_printf(_("Save %s MIME Part"), cont_type);
save_dialog =
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]