Fix more compile warnings for Windows in socket.c.
[claws.git] / src / textview.c
index ef50dd8f0350085b51d8c594bb5b432fecdeb6b7..916b8d228f81813f05263101438b060a86f762b5 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -49,7 +48,7 @@
 #include "html.h"
 #include "enriched.h"
 #include "compose.h"
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        #include "addressbook.h"
        #include "addrindex.h"
 #else
@@ -63,7 +62,6 @@
 #include "menu.h"
 #include "image_viewer.h"
 #include "filesel.h"
-#include "base64.h"
 #include "inputdialog.h"
 #include "timing.h"
 #include "tags.h"
@@ -631,34 +629,25 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
        cm_return_if_fail(mimeinfo != NULL);
        cm_return_if_fail(fp != NULL);
 
-       if ((mimeinfo->type == MIMETYPE_MULTIPART) ||
-           ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822"))) {
-               textview->loading = TRUE;
-               textview->stop_loading = FALSE;
-               
-               textview_clear(textview);
-               textview_add_parts(textview, mimeinfo);
-
-               textview->loading = FALSE;
-               textview->stop_loading = FALSE;
-               END_TIMING();
-               return;
-       }
        textview->loading = TRUE;
        textview->stop_loading = FALSE;
 
-       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
-               perror("fseek");
-
        textview_clear(textview);
 
-       if (mimeinfo->type == MIMETYPE_MULTIPART)
+       if (mimeinfo->type == MIMETYPE_MULTIPART ||
+           (mimeinfo->type == MIMETYPE_MESSAGE && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822"))) {
                textview_add_parts(textview, mimeinfo);
-       else
+       } else {
+               if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
+                       perror("fseek");
+
                textview_write_body(textview, mimeinfo);
+       }
 
        textview->loading = FALSE;
        textview->stop_loading = FALSE;
+       textview_set_position(textview, 0);
+
        END_TIMING();
 }
 
@@ -673,6 +662,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
        const gchar *name;
        gchar *content_type;
        gint charcount;
+
        START_TIMING("");
 
        cm_return_if_fail(mimeinfo != NULL);
@@ -702,7 +692,12 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        END_TIMING();
                        return;
                }
-               fseek(fp, mimeinfo->offset, SEEK_SET);
+               if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+                       fclose(fp);
+                       END_TIMING();
+                       return;
+               }
                headers = textview_scan_header(textview, fp);
                if (headers) {
                        if (charcount > 0)
@@ -751,7 +746,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        filename = procmime_get_tmp_file_name(mimeinfo);
 
                        if ((err = procmime_get_part(filename, mimeinfo)) < 0) {
-                               g_warning("Can't get the image file.(%s)", strerror(-err));
+                               g_warning("Can't get the image file.(%s)", g_strerror(-err));
                                g_free(filename);
                                END_TIMING();
                                return;
@@ -762,7 +757,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                                return;
                        }
                        if (error != NULL) {
-                               g_warning("%s\n", error->message);
+                               g_warning("%s", error->message);
                                g_error_free(error);
                        }
                        if (!pixbuf) {
@@ -818,7 +813,17 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                if (prefs_common.display_header && (charcount > 0))
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
 
+               if (!gtk_text_buffer_get_mark(buffer, "body_start")) {
+                       gtk_text_buffer_get_end_iter(buffer, &iter);
+                       gtk_text_buffer_create_mark(buffer, "body_start", &iter, TRUE);
+               }
+
                textview_write_body(textview, mimeinfo);
+
+               if (!gtk_text_buffer_get_mark(buffer, "body_end")) {
+                       gtk_text_buffer_get_end_iter(buffer, &iter);
+                       gtk_text_buffer_create_mark(buffer, "body_end", &iter, TRUE);
+               }
        }
        END_TIMING();
 }
@@ -841,40 +846,40 @@ static void recursive_add_parts(TextView *textview, GNode *node)
                 return;
         }
         if (g_ascii_strcasecmp(mimeinfo->subtype, "alternative") == 0) {
-                GNode * prefered_body;
-                int prefered_score;
-                
+                GNode * preferred_body;
+                int preferred_score;
+
                 /*
                   text/plain : score 3
                   text/ *    : score 2
                   other      : score 1
                 */
-                prefered_body = NULL;
-                prefered_score = 0;
-                
+                preferred_body = NULL;
+                preferred_score = 0;
+
                 for (iter = g_node_first_child(node) ; iter != NULL ;
                      iter = g_node_next_sibling(iter)) {
                         int score;
                         MimeInfo * submime;
-                        
+
                         score = 1;
                         submime = (MimeInfo *) iter->data;
                         if (submime->type == MIMETYPE_TEXT)
                                 score = 2;
-                        
                         if (submime->subtype != NULL) {
                                 if (g_ascii_strcasecmp(submime->subtype, "plain") == 0)
                                         score = 3;
                         }
-                        
-                        if (score > prefered_score) {
-                                prefered_score = score;
-                                prefered_body = iter;
+
+                        if (score > preferred_score) {
+                                preferred_score = score;
+                                preferred_body = iter;
                         }
                 }
-                
-                if (prefered_body != NULL) {
-                        recursive_add_parts(textview, prefered_body);
+
+                if (preferred_body != NULL) {
+                        recursive_add_parts(textview, preferred_body);
                 }
         }
         else {
@@ -889,8 +894,9 @@ static void recursive_add_parts(TextView *textview, GNode *node)
 static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
 {
        cm_return_if_fail(mimeinfo != NULL);
-        
-        recursive_add_parts(textview, mimeinfo->node);
+       cm_return_if_fail(mimeinfo->node != NULL);
+
+       recursive_add_parts(textview, mimeinfo->node);
 }
 
 void textview_show_error(TextView *textview)
@@ -1035,7 +1041,10 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
        FILE *tmpfp;
        gchar buf[BUFFSIZE];
        CodeConverter *conv;
-       const gchar *charset, *p, *cmd;
+       const gchar *charset;
+#ifndef G_OS_WIN32
+       const gchar *p, *cmd;
+#endif
        GSList *cur;
        gboolean continue_write = TRUE;
        size_t wrote = 0, i = 0;
@@ -1073,8 +1082,10 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                filename = procmime_get_tmp_file_name(mimeinfo);
                if (procmime_get_part(filename, mimeinfo) == 0) {
                        tmpfp = g_fopen(filename, "rb");
-                       textview_show_html(textview, tmpfp, conv);
-                       fclose(tmpfp);
+                       if (tmpfp) {
+                               textview_show_html(textview, tmpfp, conv);
+                               fclose(tmpfp);
+                       }
                        claws_unlink(filename);
                }
                g_free(filename);
@@ -1084,8 +1095,10 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                filename = procmime_get_tmp_file_name(mimeinfo);
                if (procmime_get_part(filename, mimeinfo) == 0) {
                        tmpfp = g_fopen(filename, "rb");
-                       textview_show_ertf(textview, tmpfp, conv);
-                       fclose(tmpfp);
+                       if (tmpfp) {
+                               textview_show_ertf(textview, tmpfp, conv);
+                               fclose(tmpfp);
+                       }
                        claws_unlink(filename);
                }
                g_free(filename);
@@ -1105,14 +1118,14 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 
                if (pipe(pfd) < 0) {
                        g_snprintf(buf, sizeof(buf),
-                               "pipe failed for textview\n\n%s\n", strerror(errno));
+                               "pipe failed for textview\n\n%s\n", g_strerror(errno));
                        textview_write_line(textview, buf, conv, TRUE);
                        goto textview_default;
                }
                pid = fork();
                if (pid < 0) {
                        g_snprintf(buf, sizeof(buf),
-                               "fork failed for textview\n\n%s\n", strerror(errno));
+                               "fork failed for textview\n\n%s\n", g_strerror(errno));
                        textview_write_line(textview, buf, conv, TRUE);
                        close(pfd[0]);
                        close(pfd[1]);
@@ -1151,7 +1164,9 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                g_unlink(fname);
 #endif
        } else {
+#ifndef G_OS_WIN32
 textview_default:
+#endif
                if (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
                                || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff"))
                        textview->is_diff = TRUE;
@@ -1164,7 +1179,11 @@ textview_default:
                        FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                        return;
                }
-               fseek(tmpfp, mimeinfo->offset, SEEK_SET);
+               if (fseek(tmpfp, mimeinfo->offset, SEEK_SET) < 0) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+                       fclose(tmpfp);
+                       return;
+               }
                debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
                while (((i = ftell(tmpfp)) < mimeinfo->offset + mimeinfo->length) &&
                       (fgets(buf, sizeof(buf), tmpfp) != NULL)
@@ -1280,7 +1299,7 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
                last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
                last->next = NULL; \
        } else { \
-               g_warning("alloc error scanning URIs\n"); \
+               g_warning("alloc error scanning URIs"); \
                gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, \
                                                         linebuf, -1, \
                                                         fg_tag, NULL); \
@@ -1293,7 +1312,7 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
                last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
                last->next = NULL; \
        } else { \
-               g_warning("alloc error scanning URIs\n"); \
+               g_warning("alloc error scanning URIs"); \
        }
 
 /* textview_make_clickable_parts() - colorizes clickable parts */
@@ -1746,6 +1765,10 @@ void textview_clear(TextView *textview)
 
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_set_text(buffer, "", -1);
+       if (gtk_text_buffer_get_mark(buffer, "body_start"))
+               gtk_text_buffer_delete_mark_by_name(buffer, "body_start");
+       if (gtk_text_buffer_get_mark(buffer, "body_end"))
+               gtk_text_buffer_delete_mark_by_name(buffer, "body_end");
 
        TEXTVIEW_STATUSBAR_POP(textview);
        textview_uri_list_remove_all(textview->uri_list);
@@ -1757,6 +1780,7 @@ void textview_clear(TextView *textview)
        if (textview->image) 
                gtk_widget_destroy(textview->image);
        textview->image = NULL;
+       textview->avatar_type = 0;
 
        if (textview->messageview->mainwin->cursor_count == 0) {
                textview_set_cursor(window, text_cursor);
@@ -1871,27 +1895,6 @@ void textview_set_position(TextView *textview, gint pos)
        gtkut_text_view_set_position(text, pos);
 }
 
-static gboolean header_is_internal(Header *header)
-{
-       const gchar *internal_hdrs[] = 
-               {"AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", 
-                "CFG:", "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", 
-                "R:", "MAID:", "SCF:", "RMID:", "FMID:", "NAID:", 
-                "X-Claws-Account-Id:", "X-Claws-Sign:", "X-Claws-Encrypt:", 
-                "X-Claws-Privacy-System:", "X-Claws-End-Special-Headers:",
-                "X-Sylpheed-Account-Id:", "X-Sylpheed-Sign:", "X-Sylpheed-Encrypt:", 
-                "X-Claws-Auto-Wrapping:", "X-Claws-Auto-Indent:",
-                "X-Sylpheed-Privacy-System:", "X-Sylpheed-End-Special-Headers:",
-                NULL};
-       int i;
-       
-       for (i = 0; internal_hdrs[i]; i++) {
-               if (!strcmp(header->name, internal_hdrs[i]))
-                       return TRUE;
-       }
-       return FALSE;
-}
-
 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
 {
        gchar buf[BUFFSIZE];
@@ -1907,7 +1910,7 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
                sorted_headers = g_ptr_array_new();
                for (i = 0; i < headers->len; i++) {
                        header = g_ptr_array_index(headers, i);
-                       if (!header_is_internal(header))
+                       if (!procheader_header_is_internal(header->name))
                                g_ptr_array_add(sorted_headers, header);
                        else
                                procheader_header_free(header);
@@ -1950,7 +1953,7 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
        if (prefs_common.show_other_header) {
                for (i = 0; i < headers->len; i++) {
                        header = g_ptr_array_index(headers, i);
-                       if (!header_is_internal(header)) {
+                       if (!procheader_header_is_internal(header->name)) {
                                g_ptr_array_add(sorted_headers, header);
                        } else {
                                procheader_header_free(header);
@@ -1987,6 +1990,7 @@ static void textview_show_avatar(TextView *textview)
                gtk_widget_destroy(textview->image);
        
        textview->image = avatarr->image;
+       textview->avatar_type = avatarr->type;
        avatarr->image = NULL; /* avoid destroying */
        avatars_avatarrender_free(avatarr);
 
@@ -2005,6 +2009,7 @@ bail:
        if (textview->image) 
                gtk_widget_destroy(textview->image);
        textview->image = NULL; 
+       textview->avatar_type = 0;
 }
 
 void textview_show_icon(TextView *textview, const gchar *stock_id)
@@ -2035,7 +2040,7 @@ void textview_show_icon(TextView *textview, const gchar *stock_id)
 
 static void textview_save_contact_pic(TextView *textview)
 {
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        MsgInfo *msginfo = textview->messageview->msginfo;
        gchar *filename = NULL;
        GError *error = NULL;
@@ -2044,6 +2049,9 @@ static void textview_save_contact_pic(TextView *textview)
        if (!msginfo->extradata || !msginfo->extradata->avatars)
                return;
 
+       if (textview->avatar_type > AVATAR_FACE)
+               return;
+
        if (textview->image) 
                picture = gtk_image_get_pixbuf(GTK_IMAGE(textview->image));
 
@@ -2053,7 +2061,7 @@ static void textview_save_contact_pic(TextView *textview)
        if (!is_file_exist(filename)) {
                gdk_pixbuf_save(picture, filename, "png", &error, NULL);
                if (error) {
-                       g_warning(_("Failed to save image: \n%s"),
+                       g_warning("Failed to save image: %s",
                                        error->message);
                        g_error_free(error);
                }
@@ -2066,7 +2074,7 @@ static void textview_save_contact_pic(TextView *textview)
 
 static void textview_show_contact_pic(TextView *textview)
 {
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        MsgInfo *msginfo = textview->messageview->msginfo;
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        int x = 0;
@@ -2104,7 +2112,7 @@ static void textview_show_contact_pic(TextView *textview)
                picture = gdk_pixbuf_new_from_file(filename, &error);
 
        if (error) {
-               debug_print("Failed to import image: \n%s",
+               debug_print("Failed to import image: %s\n",
                                error->message);
                g_error_free(error);
                goto bail;
@@ -2132,6 +2140,7 @@ bail:
        if (textview->image) 
                gtk_widget_destroy(textview->image);
        textview->image = NULL;
+       textview->avatar_type = 0;
 #else
        /* new address book */
 #endif 
@@ -2148,7 +2157,7 @@ static gint textview_tag_cmp_list(gconstpointer a, gconstpointer b)
                return tag_b == NULL ? 0:1;
        
        if (tag_b == NULL)
-               return tag_a == NULL ? 0:1;
+               return 1;
 
        return g_utf8_collate(tag_a, tag_b);
 }
@@ -2349,7 +2358,7 @@ void textview_scroll_max(TextView *textview, gboolean up)
 static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                                 TextView *textview)
 {
-       GdkWindow *window;
+       GdkWindow *window = NULL;
        SummaryView *summaryview = NULL;
        MessageView *messageview = textview->messageview;
 
@@ -2395,15 +2404,17 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                        summary_pass_key_press_event(summaryview, event);
                break;
        default:
-               window = gtk_widget_get_window(messageview->mainwin->window);
-               if (summaryview &&
-                   event->window != window) {
-                       GdkEventKey tmpev = *event;
-
-                       tmpev.window = window;
-                       KEY_PRESS_EVENT_STOP();
-                       gtk_widget_event(messageview->mainwin->window,
-                                        (GdkEvent *)&tmpev);
+               if (messageview->mainwin) {
+                       window = gtk_widget_get_window(messageview->mainwin->window);
+                       if (summaryview &&
+                           event->window != window) {
+                               GdkEventKey tmpev = *event;
+
+                               tmpev.window = window;
+                               KEY_PRESS_EVENT_STOP();
+                               gtk_widget_event(messageview->mainwin->window,
+                                                (GdkEvent *)&tmpev);
+                       }
                }
                break;
        }
@@ -2768,9 +2779,11 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                        } 
                        return TRUE;
                } else if (qlink && bevent->button == 1) {
-                       if (prefs_common.hide_quoted)
+                       if (prefs_common.hide_quoted) {
                                textview_toggle_quote(textview, NULL, uri, FALSE);
-                       return TRUE;
+                               return TRUE;
+                       } else
+                               return FALSE;
                } else if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
                        if (bevent->button == 3) {
                                g_object_set_data(
@@ -2882,15 +2895,14 @@ gboolean textview_uri_security_check(TextView *textview, ClickableText *uri)
                gchar *msg;
                AlertValue aval;
 
-               msg = g_markup_printf_escaped(_("The real URL is different from "
-                                               "the displayed URL.\n"
-                                               "\n"
-                                               "<b>Displayed URL:</b> %s\n"
-                                               "\n"
-                                               "<b>Real URL:</b> %s\n"
-                                               "\n"
-                                               "Open it anyway?"),
-                                              visible_str,uri->uri);
+               msg = g_markup_printf_escaped("%s\n\n"
+                                               "<b>%s</b> %s\n\n"
+                                               "<b>%s</b> %s\n\n"
+                                               "%s",
+                                               _("The real URL is different from the displayed URL."),
+                                               _("Displayed URL:"), visible_str,
+                                               _("Real URL:"), uri->uri,
+                                               _("Open it anyway?"));
                aval = alertpanel_full(_("Phishing attempt warning"), msg,
                                       GTK_STOCK_CANCEL, _("_Open URL"), NULL, FALSE,
                                       NULL, ALERT_WARNING, G_ALERTDEFAULT);
@@ -2953,7 +2965,7 @@ static void open_image_cb (GtkAction *action, TextView *textview)
        gchar *cmd = NULL;
        gchar buf[1024];
        const gchar *p;
-       gchar *filename = NULL;
+       gchar *filename = NULL, *filepath = NULL;
        gchar *tmp_filename = NULL;
 
        if (uri == NULL)
@@ -2972,8 +2984,11 @@ static void open_image_cb (GtkAction *action, TextView *textview)
 
        subst_for_filename(filename);
 
+       filepath = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
+                              filename, NULL);
+
        tmp_filename = g_filename_from_uri(uri->uri, NULL, NULL);
-       copy_file(tmp_filename, filename, FALSE);
+       copy_file(tmp_filename, filepath, FALSE);
        g_free(tmp_filename);
 
        cmd = mailcap_get_command_for_type("image/jpeg", filename);
@@ -2992,14 +3007,17 @@ static void open_image_cb (GtkAction *action, TextView *textview)
        }
        if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
            !strchr(p + 2, '%'))
-               g_snprintf(buf, sizeof(buf), cmd, filename);
+               g_snprintf(buf, sizeof(buf), cmd, filepath);
        else {
                g_warning("Image viewer command-line is invalid: '%s'", cmd);
+               g_free(filepath);
+               g_free(filename);
                return;
        }
 
-       execute_command_line(buf, TRUE);
+       execute_command_line(buf, TRUE, NULL);
 
+       g_free(filepath);
        g_free(filename);
        g_free(cmd);
 
@@ -3037,7 +3055,8 @@ static void save_file_cb (GtkAction *action, TextView *textview)
                filepath = g_strconcat(prefs_common.attach_save_dir,
                                       G_DIR_SEPARATOR_S, filename, NULL);
        else
-               filepath = g_strdup(filename);
+               filepath = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
+                                      filename, NULL);
 
        g_free(filename);
 
@@ -3139,7 +3158,7 @@ static void add_uri_to_addrbook_cb (GtkAction *action, TextView *textview)
                avatars_avatarrender_free(avatarr);
        }
 
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        addressbook_add_contact( fromname, fromaddress, NULL, picture);
 #else
        if (addressadd_selection(fromname, fromaddress, NULL, picture)) {