[balsa] More cleanups



commit f2745b65f2fd43972f59a2c0682ddb20a076ca5d
Author: Albrecht Dreß <albrecht dress arcor de>
Date:   Sun Apr 16 09:35:28 2017 -0400

    More cleanups
    
    * src/sendmsg-window.c: replace libbalsa_readfile() by g_file_get_contents(),
      including better error reporting; remove unused #define
    * libbalsa/identity.c: replace libbalsa_readfile_nostat() by g_file_get_contents()
      for reading a signature file, including better error checking
    * libbalsa/misc.[hc]: libbalsa_get_hostname() and libbalsa_readfile() are never used,
      remove them
    
    Signed-off-by: Peter Bloomfield <PeterBloomfield bellsouth net>

 libbalsa/identity.c  |   20 +++-------
 libbalsa/misc.c      |   66 -----------------------------------
 libbalsa/misc.h      |    2 -
 src/sendmsg-window.c |   94 +++++++++++++++++++++++--------------------------
 4 files changed, 50 insertions(+), 132 deletions(-)
---
diff --git a/libbalsa/identity.c b/libbalsa/identity.c
index 0f8085b..3d85b5a 100644
--- a/libbalsa/identity.c
+++ b/libbalsa/identity.c
@@ -340,23 +340,15 @@ libbalsa_identity_get_signature(LibBalsaIdentity* identity, GtkWindow *parent)
                  _("Error executing signature generator %s"),
                  identity->signature_path);
     } else {
-        FILE *fp;
+       GError *error = NULL;
 
-        /* sign is normal file */
-        fp = fopen(path, "r");
-        if (fp) {
-            libbalsa_readfile_nostat(fp, &ret);
-            fclose(fp);
-        } else
+       if (!g_file_get_contents(path, &ret, NULL, &error)) {
             libbalsa_information_parented(parent, LIBBALSA_INFORMATION_ERROR,
-                                          _("Cannot open signature file “%s” "
-                                            "for reading"),
-                                          identity->signature_path);
+               _("Cannot read signature file “%s”: %s"), identity->signature_path, error->message);
+               g_error_free(error);
+       }
     }
-    if(!ret)
-        libbalsa_information_parented(parent, LIBBALSA_INFORMATION_ERROR,
-                                   _("Error reading signature from %s"), path);
-    else {
+    if (ret != NULL) {
         if(!libbalsa_utf8_sanitize(&ret, FALSE, NULL))
             libbalsa_information_parented
                 (parent, LIBBALSA_INFORMATION_ERROR,
diff --git a/libbalsa/misc.c b/libbalsa/misc.c
index 997b22a..8aed2d2 100644
--- a/libbalsa/misc.c
+++ b/libbalsa/misc.c
@@ -48,20 +48,6 @@ static const gchar *libbalsa_get_codeset_name(const gchar *txt,
                                              LibBalsaCodeset Codeset);
 static int getdnsdomainname(char *s, size_t l);
 
-gchar *
-libbalsa_get_hostname(void)
-{
-    struct utsname utsname;
-    gchar *p;
-    uname(&utsname);
-
-    /* Some systems return Fqdn rather than just the hostname */
-    if ((p = strchr (utsname.nodename, '.')))
-       *p = 0;
-
-    return g_strdup (utsname.nodename);
-}
-
 static int 
 getdnsdomainname (char *s, size_t l)
 {
@@ -129,58 +115,6 @@ libbalsa_get_domainname(void)
     return NULL;
 }
 
-/* readfile allocates enough space for the ending '\0' characeter as well.
-   returns the number of read characters.
-*/
-size_t libbalsa_readfile(FILE * fp, char **buf)
-{
-    size_t size;
-    off_t offset;
-    int r;
-    int fd;
-    struct stat statbuf;
-
-    *buf = NULL;
-    if (!fp)
-       return 0;
-
-    fd = fileno(fp);
-    if (fstat(fd, &statbuf) == -1)
-       return -1;
-
-    size = statbuf.st_size;
-
-    if (!size) {
-       *buf = NULL;
-       return size;
-    }
-
-    lseek(fd, 0, SEEK_SET);
-
-    *buf = (char *) g_malloc(size + 1);
-    if (*buf == NULL)
-       return -1;
-
-    offset = 0;
-    while ((size_t)offset < size) {
-       r = read(fd, *buf + offset, size - offset);
-       if (r == 0) { /* proper EOF */
-            (*buf)[offset] = '\0';
-           return offset;
-        }
-       if (r > 0) {
-           offset += r;
-       } else if ((errno != EAGAIN) && (errno != EINTR)) {
-           perror("Error reading file:");
-            (*buf)[offset] = '\0';
-           return -1;
-       }
-    }
-    (*buf)[size] = '\0';
-
-    return size;
-}
-
 /* readfile_nostat is identical to readfile except it reads to EOF.
    This enables the use of pipes to programs for such things as
    the signature file.
diff --git a/libbalsa/misc.h b/libbalsa/misc.h
index 755bcf7..ed21667 100644
--- a/libbalsa/misc.h
+++ b/libbalsa/misc.h
@@ -91,10 +91,8 @@ GtkWidget *libbalsa_charset_button_new(void);
 LibBalsaTextAttribute libbalsa_text_attr_string(const gchar * string);
 LibBalsaTextAttribute libbalsa_text_attr_file(const gchar * filename);
 
-size_t libbalsa_readfile(FILE * fp, char **buf);
 size_t libbalsa_readfile_nostat(FILE * fp, char **buf);
 
-gchar *libbalsa_get_hostname(void);
 gchar *libbalsa_get_domainname(void);
 #define libbalsa_urlencode(str) (g_uri_escape_string((str), NULL, FALSE))
 #define libbalsa_urldecode(str) (g_uri_unescape_string((str), NULL))
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index 6006fbd..3518e04 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -81,7 +81,6 @@
 #include <gtksourceview/gtksource.h>
 #endif                          /* HAVE_GTKSOURCEVIEW */
 
-#define GNOME_MIME_BUG_WORKAROUND 1
 typedef struct {
     pid_t pid_editor;
     gchar *filename;
@@ -4625,67 +4624,62 @@ do_insert_string_select_ch(BalsaSendmsg* bsmsg, GtkTextBuffer *buffer,
 }
 
 static void
-insert_file_response(GtkWidget * selector, gint response,
-                    BalsaSendmsg * bsmsg)
+insert_file_response(GtkWidget    *selector,
+                                        gint          response,
+                                        BalsaSendmsg *bsmsg)
 {
     GtkFileChooser *fc;
     gchar *fname;
-    FILE *fl;
-    GtkTextBuffer *buffer;
-    gchar * string;
-    size_t len;
 
     if (response != GTK_RESPONSE_OK) {
-       gtk_widget_destroy(selector);
-       return;
+       gtk_widget_destroy(selector);
+       return;
     }
 
     fc = GTK_FILE_CHOOSER(selector);
     fname = gtk_file_chooser_get_filename(fc);
+    if (fname != NULL) {
+        gchar *string;
+        gsize len;
+        GError *error = NULL;
+
+       if (g_file_get_contents(fname, &string, &len, &error)) {
+            LibBalsaTextAttribute attr;
+            GtkTextBuffer *buffer;
+
+            buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsmsg->text));
+            attr = libbalsa_text_attr_string(string);
+            if (!attr || (attr & LIBBALSA_TEXT_HI_UTF8)) {
+                /* Ascii or utf-8 */
+                gtk_text_buffer_insert_at_cursor(buffer, string, -1);
+            } else {
+                /* Neither ascii nor utf-8... */
+                gchar *s = NULL;
+                const gchar *charset = sw_preferred_charset(bsmsg);
+
+                if (sw_can_convert(string, -1, "UTF-8", charset, &s)) {
+                    /* ...but seems to be in current charset. */
+                    gtk_text_buffer_insert_at_cursor(buffer, s, -1);
+                    g_free(s);
+                } else
+                    /* ...and can't be decoded from current charset. */
+                    do_insert_string_select_ch(bsmsg, buffer, string, len, fname);
+            }
+            g_free(string);
 
-    if ((fl = fopen(fname, "rt")) ==NULL) {
-       balsa_information_parented(GTK_WINDOW(bsmsg->window),
-                                   LIBBALSA_INFORMATION_WARNING,
-                                   _("Could not open the file %s.\n"), fname);
-       g_free(fname);
-       return;
-    }
-
-    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsmsg->text));
-    string = NULL;
-    len = libbalsa_readfile(fl, &string);
-    fclose(fl);
-
-    if (string) {
-        LibBalsaTextAttribute attr;
+            /* Use the same folder as for attachments. */
+            g_free(balsa_app.attach_dir);
+            balsa_app.attach_dir = gtk_file_chooser_get_current_folder(fc);
+       } else {
+               balsa_information_parented(GTK_WINDOW(bsmsg->window),
+                       LIBBALSA_INFORMATION_WARNING, _("Cannot not read the file “%s”: %s"), fname, 
error->message);
+               g_error_free(error);
+       }
 
-        attr = libbalsa_text_attr_string(string);
-        if (!attr || attr & LIBBALSA_TEXT_HI_UTF8)
-            /* Ascii or utf-8 */
-            gtk_text_buffer_insert_at_cursor(buffer, string, -1);
-        else {
-            /* Neither ascii nor utf-8... */
-            gchar *s = NULL;
-            const gchar *charset = sw_preferred_charset(bsmsg);
-
-            if (sw_can_convert(string, -1, "UTF-8", charset, &s)) {
-                /* ...but seems to be in current charset. */
-                gtk_text_buffer_insert_at_cursor(buffer, s, -1);
-                g_free(s);
-            } else
-                /* ...and can't be decoded from current charset. */
-                do_insert_string_select_ch(bsmsg, buffer, string, len,
-                                           fname);
-        }
-        g_free(string);
+        g_free(fname);
     }
 
-    /* Use the same folder as for attachments. */
-    g_free(balsa_app.attach_dir);
-    balsa_app.attach_dir = gtk_file_chooser_get_current_folder(fc);
-
     gtk_widget_destroy(selector);
-    g_free(fname);
 }
 
 static void


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