fix bug 4257, 'claws-mail 3.17.4 breaks copy-pasting from emacs-gtk3'
[claws.git] / src / compose.c
index 487ec1c984c98b3b9e90611240eedfd1d7d08108..fc89629e35e9dc320bfca8157c4b74cbe44c1304 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2018 the Claws Mail team and Hiroyuki Yamamoto
+ * Copyright (C) 1999-2019 the Claws Mail team and Hiroyuki Yamamoto
  *
  * 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
@@ -2894,7 +2894,7 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
 
        cm_return_val_if_fail(msginfo != NULL, -1);
 
-       if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
+       if ((fp = procmsg_open_message(msginfo, FALSE)) == NULL) return -1;
        procheader_get_header_fields(fp, hentry);
        claws_fclose(fp);
 
@@ -3019,7 +3019,7 @@ static gint compose_parse_manual_headers(Compose *compose, MsgInfo *msginfo, Hea
 
        cm_return_val_if_fail(msginfo != NULL, -1);
 
-       if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
+       if ((fp = procmsg_open_message(msginfo, FALSE)) == NULL) return -1;
        procheader_get_header_fields(fp, entries);
        claws_fclose(fp);
 
@@ -4256,10 +4256,16 @@ static gboolean compose_get_line_break_pos(GtkTextBuffer *buffer,
 
        for (; *p != '\0' && i < len; i++) {
                PangoLogAttr *attr = attrs + i;
-               gunichar wc;
+               gunichar wc = g_utf8_get_char(p);
                gint uri_len;
 
-               if (attr->is_line_break && can_break && was_white && !prev_dont_break)
+               /* attr->is_line_break will be false for some characters that
+                * we want to break a line before, like '/' or ':', so we
+                * also allow breaking on any non-wide character. The
+                * mentioned pango attribute is still useful to decide on
+                * line breaks when wide characters are involved. */
+               if ((!g_unichar_iswide(wc) || attr->is_line_break)
+                               && can_break && was_white && !prev_dont_break)
                        pos = i;
                
                was_white = attr->is_white;
@@ -4277,7 +4283,6 @@ static gboolean compose_get_line_break_pos(GtkTextBuffer *buffer,
                        continue;
                }
 
-               wc = g_utf8_get_char(p);
                if (g_unichar_iswide(wc)) {
                        col += 2;
                        if (prev_dont_break && can_break && attr->is_line_break)
@@ -4331,7 +4336,7 @@ static gboolean compose_join_next_line(Compose *compose,
        next_quote_str = compose_get_quote_str(buffer, &iter_, &quote_len);
 
        if ((quote_str || next_quote_str) &&
-           strcmp2(quote_str, next_quote_str) != 0) {
+           g_strcmp0(quote_str, next_quote_str) != 0) {
                g_free(next_quote_str);
                return FALSE;
        }
@@ -6453,7 +6458,7 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
                }
                if (ainfo->name && mimepart->type != MIMETYPE_MESSAGE) {
                        if (mimepart->type == MIMETYPE_APPLICATION && 
-                          !strcmp2(mimepart->subtype, "octet-stream"))
+                          !g_strcmp0(mimepart->subtype, "octet-stream"))
                                g_hash_table_insert(mimepart->typeparameters,
                                                g_strdup("name"), g_strdup(ainfo->name));
                        g_hash_table_insert(mimepart->dispositionparameters,
@@ -6464,7 +6469,7 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
                if (mimepart->type == MIMETYPE_MESSAGE
                    || mimepart->type == MIMETYPE_MULTIPART)
                        ainfo->encoding = ENC_BINARY;
-               else if (compose->use_signing) {
+               else if (compose->use_signing || compose->fwdinfo != NULL) {
                        if (ainfo->encoding == ENC_7BIT)
                                ainfo->encoding = ENC_QUOTED_PRINTABLE;
                        else if (ainfo->encoding == ENC_8BIT)
@@ -7535,7 +7540,7 @@ static GtkWidget *compose_create_others(Compose *compose)
        g_signal_connect_after(G_OBJECT(savemsg_combo), "grab_focus",
                         G_CALLBACK(compose_grab_focus_cb), compose);
        if (account_get_special_folder(compose->account, F_OUTBOX)) {
-               if (compose->account->set_sent_folder)
+               if (compose->account->set_sent_folder || prefs_common.savemsg)
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(savemsg_checkbtn), TRUE);
                else
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(savemsg_checkbtn), FALSE);
@@ -9449,6 +9454,7 @@ static void compose_attach_property_create(gboolean *cancelled)
        gtk_container_set_border_width(GTK_CONTAINER(window), 8);
        gtk_window_set_title(GTK_WINDOW(window), _("Properties"));
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
+       gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
        g_signal_connect(G_OBJECT(window), "delete_event",
                         G_CALLBACK(attach_property_delete_event),
                         cancelled);
@@ -9481,11 +9487,11 @@ static void compose_attach_property_create(gboolean *cancelled)
 
                tmp = g_strdup_printf("%s/%s", type->type, type->sub_type);
 
-               if (g_list_find_custom(strlist, tmp, (GCompareFunc)strcmp2))
+               if (g_list_find_custom(strlist, tmp, (GCompareFunc)g_strcmp0))
                        g_free(tmp);
                else
                        strlist = g_list_insert_sorted(strlist, (gpointer)tmp,
-                                       (GCompareFunc)strcmp2);
+                                       (GCompareFunc)g_strcmp0);
        }
 
        for (mime_type_list = strlist; mime_type_list != NULL; 
@@ -10145,7 +10151,7 @@ static void account_activated(GtkComboBox *optmenu, gpointer data)
                compose_set_save_to(compose, folderidentifier);
                g_free(folderidentifier);
        } else if (account_get_special_folder(compose->account, F_OUTBOX)) {
-               if (compose->account->set_sent_folder)
+               if (compose->account->set_sent_folder || prefs_common.savemsg)
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
                else
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), FALSE);
@@ -10892,58 +10898,184 @@ static void entry_copy_clipboard(GtkWidget *entry)
                        gtk_clipboard_get(GDK_SELECTION_CLIPBOARD));
 }
 
+static void paste_text(Compose *compose, GtkWidget *entry,
+                      gboolean wrap, GdkAtom clip, GtkTextIter *insert_place,
+                      const gchar *contents)
+{
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry));
+       GtkTextMark *mark_start = gtk_text_buffer_get_insert(buffer);
+       GtkTextIter start_iter, end_iter;
+       gint start, end;
+
+       if (contents == NULL)
+               return;
+
+       /* we shouldn't delete the selection when middle-click-pasting, or we
+        * can't mid-click-paste our own selection */
+       if (clip != GDK_SELECTION_PRIMARY) {
+               undo_paste_clipboard(GTK_TEXT_VIEW(compose->text), compose->undostruct);
+               gtk_text_buffer_delete_selection(buffer, FALSE, TRUE);
+       }
+
+       if (insert_place == NULL) {
+               /* if insert_place isn't specified, insert at the cursor.
+                * used for Ctrl-V pasting */
+               gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark_start);
+               start = gtk_text_iter_get_offset(&start_iter);
+               gtk_text_buffer_insert(buffer, &start_iter, contents, strlen(contents));
+       } else {
+               /* if insert_place is specified, paste here.
+                * used for mid-click-pasting */
+               start = gtk_text_iter_get_offset(insert_place);
+               gtk_text_buffer_insert(buffer, insert_place, contents, strlen(contents));
+               if (prefs_common.primary_paste_unselects)
+                       gtk_text_buffer_select_range(buffer, insert_place, insert_place);
+       }
+
+       if (!wrap) {
+               /* paste unwrapped: mark the paste so it's not wrapped later */
+               end = start + strlen(contents);
+               gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start);
+               gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end);
+               gtk_text_buffer_apply_tag_by_name(buffer, "no_wrap", &start_iter, &end_iter);
+       } else if (wrap && clip == GDK_SELECTION_PRIMARY) {
+               /* rewrap paragraph now (after a mid-click-paste) */
+               mark_start = gtk_text_buffer_get_insert(buffer);
+               gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark_start);
+               gtk_text_iter_backward_char(&start_iter);
+               compose_beautify_paragraph(compose, &start_iter, TRUE);
+       }
+       compose->modified = TRUE;
+}
+
+static void attach_uri_list(Compose *compose, GtkSelectionData *data)
+{
+       GList *list, *tmp;
+
+       list = uri_list_extract_filenames(
+               (const gchar *)gtk_selection_data_get_data(data));
+       for (tmp = list; tmp != NULL; tmp = tmp->next) {
+               gchar *utf8_filename = conv_filename_to_utf8((const gchar *)tmp->data);
+               compose_attach_append
+                       (compose, (const gchar *)tmp->data,
+                        utf8_filename, NULL, NULL);
+               g_free(utf8_filename);
+       }
+       if (list)
+               compose_changed_cb(NULL, compose);
+
+       list_free_strings_full(list);
+}
+
+int attach_image(Compose *compose, GtkSelectionData *data, const gchar *subtype)
+{
+       FILE *fp;
+       const guchar *contents;
+       gchar *file;
+       gchar *type;
+       size_t len;
+       int r;
+
+       cm_return_val_if_fail(data != NULL, -1);
+
+       contents = gtk_selection_data_get_data(data);
+       len = gtk_selection_data_get_length(data);
+
+       file = g_strconcat(get_tmp_file(), "-image.", subtype, NULL);
+
+       debug_print("writing image to %s\n", file);
+
+       if ((fp = claws_fopen(file, "wb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
+               return -1;
+       }
+
+       if (claws_fwrite(contents, 1, len, fp) != len) {
+               FILE_OP_ERROR(file, "claws_fwrite");
+               claws_fclose(fp);
+               claws_unlink(file);
+               return -1;
+       }
+
+       r = claws_safe_fclose(fp);
+
+       if (r == EOF) {
+               FILE_OP_ERROR(file, "claws_fclose");
+               claws_unlink(file);
+               return -1;
+       }
+
+       type = g_strconcat("image/", subtype, NULL);
+
+       compose_attach_append(compose, (const gchar *)file, 
+               (const gchar *)file, type, NULL);
+
+       alertpanel_notice(_("The pasted image has been attached as: \n%s"), file);
+
+       g_free(file);
+       g_free(type);
+
+       return 0;
+}
+
 static void entry_paste_clipboard(Compose *compose, GtkWidget *entry, 
                                  gboolean wrap, GdkAtom clip, GtkTextIter *insert_place)
 {
        if (GTK_IS_TEXT_VIEW(entry)) {
-               GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry));
-               GtkTextMark *mark_start = gtk_text_buffer_get_insert(buffer);
-               GtkTextIter start_iter, end_iter;
-               gint start, end;
-               gchar *contents = gtk_clipboard_wait_for_text(gtk_clipboard_get(clip));
+               GdkAtom types = gdk_atom_intern ("MULTIPLE", FALSE);
+               GdkAtom *targets = NULL;
+               int n_targets = 0, i;
+               gboolean paste_done = FALSE;
+               GtkClipboard *clipboard = gtk_clipboard_get(clip);
+
+               GtkSelectionData *contents = gtk_clipboard_wait_for_contents(
+                                               clipboard, types);
+
+               if (contents != NULL) {
+                       gtk_selection_data_get_targets(contents, &targets, &n_targets);
+                       gtk_selection_data_free(contents);
+               }
+
+               for (i = 0; i < n_targets; i++) {
+                       GdkAtom atom = targets[i];
+                       gchar *atom_type = gdk_atom_name(atom);
+
+                       if (atom_type != NULL) {
+                               GtkSelectionData *data = gtk_clipboard_wait_for_contents(
+                                               clipboard, atom);
+                               debug_print("got contents of type %s\n", atom_type);
+                               if (!strcmp(atom_type, "text/plain")) {
+                                       /* let the default text handler handle it */
+                    break;
+                               } else if (!strcmp(atom_type, "text/uri-list")) {
+                                       attach_uri_list(compose, data);
+
+                                       paste_done = TRUE;
+                                       break;
+                               } else if (!strncmp(atom_type, "image/", strlen("image/"))) {
+                                       gchar *subtype = g_strdup((gchar *)(strstr(atom_type, "/")+1));
+                                       debug_print("image of type %s\n", subtype);
 
-               if (contents == NULL)
-                       return;
-       
-               /* we shouldn't delete the selection when middle-click-pasting, or we
-                * can't mid-click-paste our own selection */
-               if (clip != GDK_SELECTION_PRIMARY) {
-                       undo_paste_clipboard(GTK_TEXT_VIEW(compose->text), compose->undostruct);
-                       gtk_text_buffer_delete_selection(buffer, FALSE, TRUE);
-               }
-               
-               if (insert_place == NULL) {
-                       /* if insert_place isn't specified, insert at the cursor.
-                        * used for Ctrl-V pasting */
-                       gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark_start);
-                       start = gtk_text_iter_get_offset(&start_iter);
-                       gtk_text_buffer_insert(buffer, &start_iter, contents, strlen(contents));
-               } else {
-                       /* if insert_place is specified, paste here.
-                        * used for mid-click-pasting */
-                       start = gtk_text_iter_get_offset(insert_place);
-                       gtk_text_buffer_insert(buffer, insert_place, contents, strlen(contents));
-                       if (prefs_common.primary_paste_unselects)
-                               gtk_text_buffer_select_range(buffer, insert_place, insert_place);
+                                       attach_image(compose, data, subtype);
+                                       g_free(subtype);
+
+                                       paste_done = TRUE;
+                                       break;
+                               }
+                       }
                }
-               
-               if (!wrap) {
-                       /* paste unwrapped: mark the paste so it's not wrapped later */
-                       end = start + strlen(contents);
-                       gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start);
-                       gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end);
-                       gtk_text_buffer_apply_tag_by_name(buffer, "no_wrap", &start_iter, &end_iter);
-               } else if (wrap && clip == GDK_SELECTION_PRIMARY) {
-                       /* rewrap paragraph now (after a mid-click-paste) */
-                       mark_start = gtk_text_buffer_get_insert(buffer);
-                       gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark_start);
-                       gtk_text_iter_backward_char(&start_iter);
-                       compose_beautify_paragraph(compose, &start_iter, TRUE);
+               if (!paste_done) {
+                       gchar *def_text = gtk_clipboard_wait_for_text(clipboard);
+                       paste_text(compose, entry, wrap, clip,
+                                  insert_place, def_text);
+                       g_free(def_text);
                }
-       } else if (GTK_IS_EDITABLE(entry))
-               gtk_editable_paste_clipboard (GTK_EDITABLE(entry));
+               g_free(targets);
 
-       compose->modified = TRUE;
+       } else if (GTK_IS_EDITABLE(entry)) {
+               gtk_editable_paste_clipboard (GTK_EDITABLE(entry));
+               compose->modified = TRUE;
+       }
 }
 
 static void entry_allsel(GtkWidget *entry)
@@ -11603,25 +11735,13 @@ static void compose_attach_drag_received_cb (GtkWidget                *widget,
                                             gpointer            user_data)
 {
        Compose *compose = (Compose *)user_data;
-       GList *list, *tmp;
        GdkAtom type;
 
        type = gtk_selection_data_get_data_type(data);
        if ((gdk_atom_name(type) && !strcmp(gdk_atom_name(type), "text/uri-list"))
           && gtk_drag_get_source_widget(context) !=
                summary_get_main_widget(mainwindow_get_mainwindow()->summaryview)) {
-               list = uri_list_extract_filenames(
-                       (const gchar *)gtk_selection_data_get_data(data));
-               for (tmp = list; tmp != NULL; tmp = tmp->next) {
-                       gchar *utf8_filename = conv_filename_to_utf8((const gchar *)tmp->data);
-                       compose_attach_append
-                               (compose, (const gchar *)tmp->data,
-                                utf8_filename, NULL, NULL);
-                       g_free(utf8_filename);
-               }
-               if (list)
-                       compose_changed_cb(NULL, compose);
-               list_free_strings_full(list);
+               attach_uri_list(compose, data);
        } else if (gtk_drag_get_source_widget(context)
                   == summary_get_main_widget(mainwindow_get_mainwindow()->summaryview)) {
                /* comes from our summaryview */