2004-11-23 [colin] 0.9.12cvs158.9
[claws.git] / src / compose.c
index 56a033f8e1420c996392df4ea4a806782ce5d145..c00aef4e90045ac0c148dbaa058063235d47633f 100644 (file)
@@ -377,9 +377,9 @@ static void compose_allsel_cb               (Compose        *compose);
 static void compose_advanced_action_cb (Compose                   *compose,
                                         ComposeCallAdvancedAction  action);
 
-static gboolean compose_grab_focus_cb  (GtkWidget      *widget,
+static void compose_grab_focus_cb      (GtkWidget      *widget,
                                         Compose        *compose);
-static gboolean compose_grab_focus_before_cb   (GtkWidget      *widget,
+static void compose_grab_focus_before_cb(GtkWidget     *widget,
                                         Compose        *compose);
 
 static void compose_changed_cb         (GtkTextBuffer  *textbuf,
@@ -990,18 +990,13 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
        if (account->auto_sig)
                compose_insert_sig(compose, FALSE);
 
-       if (quote && prefs_common.linewrap_quote)
-               compose_wrap_line_all(compose);
-
        cursor_pos = quote_fmt_get_cursor_pos();
        gtk_text_buffer_get_start_iter(textbuf, &iter);
        gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cursor_pos);
        gtk_text_buffer_place_cursor(textbuf, &iter);
-
-       if (quote && prefs_common.linewrap_quote) {
+       
+       if (quote && prefs_common.linewrap_quote)
                compose_wrap_line_all(compose);
-               gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), TRUE);
-       }
 
        gtk_widget_grab_focus(compose->text);
 
@@ -1295,6 +1290,10 @@ void compose_reedit(MsgInfo *msginfo)
        g_signal_handlers_block_by_func(G_OBJECT(textbuf),
                                        G_CALLBACK(compose_changed_cb),
                                        compose);
+                                       
+       g_signal_handlers_block_by_func(G_OBJECT(textbuf),
+                                       G_CALLBACK(text_inserted),
+                                       compose);
 
        if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
                g_warning("Can't get text part\n");
@@ -1302,12 +1301,16 @@ void compose_reedit(MsgInfo *msginfo)
                while (fgets(buf, sizeof(buf), fp) != NULL) {
                        strcrchomp(buf);
                        gtk_text_buffer_insert(textbuf, &iter, buf, -1);
+                       gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
                }
                fclose(fp);
        }
        
        compose_attach_parts(compose, msginfo);
 
+       g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
+                                       G_CALLBACK(text_inserted),
+                                       compose);
        g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
                                        G_CALLBACK(compose_changed_cb),
                                        compose);
@@ -2173,6 +2176,7 @@ static void compose_attach_append(Compose *compose, const gchar *file,
        FILE *fp;
        off_t size;
        gint row;
+       gchar *name;
 
        if (!is_file_exist(file)) {
                g_warning("File %s doesn't exist\n", file);
@@ -2208,31 +2212,30 @@ static void compose_attach_append(Compose *compose, const gchar *file,
                if (!g_ascii_strcasecmp(content_type, "message/rfc822")) {
                        MsgInfo *msginfo;
                        MsgFlags flags = {0, 0};
-                       const gchar *name;
 
-                       if (procmime_get_encoding_for_file(file) == ENC_7BIT)
+                       if (procmime_get_encoding_for_text_file(file) == ENC_7BIT)
                                ainfo->encoding = ENC_7BIT;
                        else
                                ainfo->encoding = ENC_8BIT;
 
                        msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
                        if (msginfo && msginfo->subject)
-                               name = msginfo->subject;
+                               name = g_strdup(msginfo->subject);
                        else
-                               name = g_basename(filename ? filename : file);
+                               name = g_path_get_basename(filename ? filename : file);
 
                        ainfo->name = g_strdup_printf(_("Message: %s"), name);
 
                        procmsg_msginfo_free(msginfo);
                } else {
                        if (!g_ascii_strncasecmp(content_type, "text", 4))
-                               ainfo->encoding =
-                                       procmime_get_encoding_for_file(file);
+                               ainfo->encoding = procmime_get_encoding_for_text_file(file);
                        else
                                ainfo->encoding = ENC_BASE64;
-                       ainfo->name = g_strdup
-                               (g_basename(filename ? filename : file));
+                       name = g_path_get_basename(filename ? filename : file);
+                       ainfo->name = g_strdup(name);
                }
+               g_free(name);
        } else {
                ainfo->content_type = procmime_get_mime_type(file);
                if (!ainfo->content_type) {
@@ -2240,10 +2243,13 @@ static void compose_attach_append(Compose *compose, const gchar *file,
                                g_strdup("application/octet-stream");
                        ainfo->encoding = ENC_BASE64;
                } else if (!g_ascii_strncasecmp(ainfo->content_type, "text", 4))
-                       ainfo->encoding = procmime_get_encoding_for_file(file);
+                       ainfo->encoding =
+                               procmime_get_encoding_for_text_file(file);
                else
                        ainfo->encoding = ENC_BASE64;
-               ainfo->name = g_strdup(g_basename(filename ? filename : file)); 
+               name = g_path_get_basename(filename ? filename : file);
+               ainfo->name = g_strdup(name);   
+               g_free(name);
        }
 
        if (!strcmp(ainfo->content_type, "unknown")) {
@@ -2588,16 +2594,17 @@ static void compose_wrap_line(Compose *compose)
 #undef WRAP_DEBUG
 #ifdef WRAP_DEBUG
 /* Darko: used when I debug wrapping */
-void dump_text(GtkTextBuffer textbuf, int pos, int tlen, int breakoncr)
+void dump_text(GtkTextBuffer *textbuf, int pos, int tlen, int breakoncr)
 {
        gint i, clen;
        gchar cbuf[CHAR_BUF_SIZE];
+       GtkTextIter iter, end_iter;
 
        printf("%d [", pos);
        gtk_text_buffer_get_iter_at_offset(textbuf, &iter, pos);
        gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + tlen);
        for (; gtk_text_iter_forward_char(&iter) &&
-                    gtk_text_iter_compare(&iter, &end_iter) < 0;)
+                    gtk_text_iter_compare(&iter, &end_iter) < 0;) {
                GET_CHAR(&iter, cbuf, clen);
                if (clen < 0) break;
                if (breakoncr && clen == 1 && cbuf[0] == '\n')
@@ -2904,7 +2911,7 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
                                is_new_line = TRUE;
 #ifdef WRAP_DEBUG
                                g_print("after delete l_pos=");
-                               dump_text(text, line_pos, tlen, 1);
+                               dump_text(textbuf, line_pos, tlen, 1);
 #endif
                                /* move beginning of line if we are on LF */
                                gtk_text_buffer_get_iter_at_offset(textbuf,
@@ -2945,8 +2952,8 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
 
 #ifdef WRAP_DEBUG
                        g_print("should wrap cur_pos=%d ", cur_pos);
-                       dump_text(text, p_pos, tlen, 1);
-                       dump_text(text, line_pos, tlen, 1);
+                       dump_text(textbuf, p_pos, tlen, 1);
+                       dump_text(textbuf, line_pos, tlen, 1);
 #endif
                        /* force wrapping if it is one long word but not URL */
                        if (line_pos - p_pos <= i_len)
@@ -2998,7 +3005,7 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
                            gtkut_text_buffer_is_uri_string(textbuf, line_pos, tlen)) {
 #ifdef WRAP_DEBUG
                                g_print("found URL at ");
-                               dump_text(text, line_pos, tlen, 1);
+                               dump_text(textbuf, line_pos, tlen, 1);
 #endif
                                continue;
                        }
@@ -3024,8 +3031,8 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
                                do_delete = FALSE;
 #ifdef WRAP_DEBUG
                        g_print("after CR insert ");
-                       dump_text(text, line_pos, tlen, 1);
-                       dump_text(text, cur_pos, tlen, 1);
+                       dump_text(textbuf, line_pos, tlen, 1);
+                       dump_text(textbuf, cur_pos, tlen, 1);
 #endif
 
                        /* should we insert quotation ? */
@@ -3045,7 +3052,7 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
                                        }
 #ifdef WRAP_DEBUG
                                        g_print("after quote insert ");
-                                       dump_text(text, line_pos, tlen, 1);
+                                       dump_text(textbuf, line_pos, tlen, 1);
 #endif
                                }
                        }
@@ -3309,7 +3316,16 @@ gint compose_send(Compose *compose)
                folder_item_scan(folder);
                if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS)
                        gtk_widget_destroy(compose->window);
-       }
+       } else {
+               alertpanel_error(_("The message was queued but could not be "
+                                  "sent.\nUse \"Send queued messages\" from "
+                                  "the main window to retry."));
+               if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
+                       compose_allow_user_actions (compose, TRUE);
+                       compose->sending = FALSE;               
+               }
+               return -1;
+       }
 
        return 0;
 
@@ -3426,7 +3442,7 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
 
        /* Resent-Message-ID */
        if (compose->account->gen_msgid) {
-               generate_msgid(compose->account->address, buf, sizeof(buf));
+               generate_msgid(buf, sizeof(buf));
                fprintf(fp, "Resent-Message-ID: <%s>\n", buf);
                compose->msgid = g_strdup(buf);
        }
@@ -3621,8 +3637,9 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action)
        } else
                g_node_append(mimemsg->node, mimetext->node);
 
-       /* sign message */
-       if (compose->use_signing && privacy_system_can_sign(compose->privacy_system))
+       /* sign message if sending */
+       if (action == COMPOSE_WRITE_FOR_SEND && compose->use_signing && 
+           privacy_system_can_sign(compose->privacy_system))
                if (!privacy_sign(compose->privacy_system, mimemsg))
                        return -1;
 
@@ -3840,7 +3857,9 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                        gchar *encdata;
 
                        encdata = privacy_get_encrypt_data(compose->privacy_system, compose->to_list);
-                       fprintf(fp, "X-Sylpheed-Encrypt-Data:%s\n", encdata);
+                       if (encdata != NULL)
+                               fprintf(fp, "X-Sylpheed-Encrypt-Data:%s\n", 
+                                       encdata);
                        g_free(encdata);
                }
        }
@@ -4108,27 +4127,20 @@ static gchar *compose_get_header(Compose *compose)
        compose_add_headerfield_from_headerlist(compose, header, "Bcc", ", ");
 
        /* Subject */
-       str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
+       str = gtk_editable_get_chars(GTK_EDITABLE(compose->subject_entry), 0, -1);
        if (*str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
-               gchar *tmpstr;
-
-               tmpstr = g_strdup(str);
-               if (tmpstr == NULL) {
-                       g_string_free(header, TRUE);
-                       return NULL;
-               }
                g_strstrip(str);
                if (*str != '\0') {
                        compose_convert_header(buf, sizeof(buf), str,
                                               strlen("Subject: "), FALSE);
                        g_string_append_printf(header, "Subject: %s\n", buf);
                }
-               g_free(tmpstr);
        }
+       g_free(str);
 
        /* Message-ID */
        if (compose->account->gen_msgid) {
-               generate_msgid(compose->account->address, buf, sizeof(buf));
+               generate_msgid(buf, sizeof(buf));
                g_string_append_printf(header, "Message-ID: <%s>\n", buf);
                compose->msgid = g_strdup(buf);
        }
@@ -4258,7 +4270,7 @@ static gchar *compose_get_header(Compose *compose)
                g_free(tmp);
                
                entry_str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
-               Xstrdup_a(headervalue, entry_str, return -1);
+               Xstrdup_a(headervalue, entry_str, return NULL);
                subst_char(headervalue, '\r', ' ');
                subst_char(headervalue, '\n', ' ');
                string = std_headers;
@@ -6385,7 +6397,7 @@ static void compose_insert_file_cb(gpointer data, guint action,
                for ( tmp = file_list; tmp; tmp = tmp->next) {
                        gchar *file = (gchar *) tmp->data;
                        gchar *filedup = g_strdup(file);
-                       const gchar *shortfile = g_basename(filedup);
+                       gchar *shortfile = g_path_get_basename(filedup);
                        ComposeInsertResult res;
 
                        res = compose_insert_file(compose, file);
@@ -6395,6 +6407,7 @@ static void compose_insert_file_cb(gpointer data, guint action,
                                alertpanel_error(_("File '%s' contained invalid characters\n"
                                                   "for the current encoding, insertion may be incorrect."), shortfile);
                        }
+                       g_free(shortfile);
                        g_free(filedup);
                        g_free(file);
                }
@@ -6897,19 +6910,20 @@ static void compose_advanced_action_cb(Compose *compose,
 
 static gchar *cliptext = NULL;
 
-static gboolean compose_grab_focus_before_cb(GtkWidget *widget, Compose *compose)
+static void compose_grab_focus_before_cb(GtkWidget *widget, Compose *compose)
 {
        gchar *str = NULL;
        GtkClipboard *clip = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
-       if (cliptext)
+       if (cliptext) {
                g_free(cliptext);
+               cliptext = NULL;
+       }
+
        if (gtk_clipboard_wait_is_text_available(clip))
                cliptext = gtk_clipboard_wait_for_text(clip);
-       
-       return FALSE;
 }
 
-static gboolean compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
+static void compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
 {
        gchar *str = NULL;
        GtkClipboard *clip = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
@@ -6925,8 +6939,6 @@ static gboolean compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
 
        if (GTK_IS_EDITABLE(widget) || GTK_IS_TEXT_VIEW(widget))
                compose->focused_editable = widget;
-       
-       return TRUE;
 }
 
 static void compose_changed_cb(GtkTextBuffer *textbuf, Compose *compose)
@@ -6970,12 +6982,8 @@ static void compose_toggle_encrypt_cb(gpointer data, guint action,
 
 static void activate_privacy_system(Compose *compose, PrefsAccount *account) 
 {
-       /* TODO
-       if (account->default_gnupg_mode)
-               compose->gnupg_mode = GNUPG_MODE_INLINE;
-       else
-               compose->gnupg_mode = GNUPG_MODE_DETACH;
-       */              
+       g_free(compose->privacy_system);
+       compose->privacy_system = g_strdup(account->default_privacy_system);
        compose_update_privacy_system_menu_item(compose);
 }
 
@@ -7221,7 +7229,7 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
        } else
                gtk_text_buffer_insert(buffer, iter, text, len);
 
-       if (prefs_common.autowrap)
+       if (compose->autowrap)
                compose_wrap_line_all_full(compose, TRUE);
 
        g_signal_handlers_unblock_by_func(G_OBJECT(buffer),