Added comments explaining the use of gtk_container_set_focus_chain(), for future...
[claws.git] / src / compose.c
index aff17233ca6749c1faa95210c2064cf78b7433e2..28f13dbcc739048917705728ba90075a3c3f2127 100644 (file)
@@ -182,6 +182,9 @@ typedef enum {
 
 #define MAX_REFERENCES_LEN     999
 
+#define COMPOSE_DRAFT_TIMEOUT_UNSET -1
+#define COMPOSE_DRAFT_TIMEOUT_FORBIDDEN -2
+
 static GList *compose_list = NULL;
 static GSList *extra_headers = NULL;
 
@@ -365,7 +368,7 @@ static void compose_add_field_list  ( Compose *compose,
 
 static void compose_notebook_size_alloc (GtkNotebook *notebook,
                                         GtkAllocation *allocation,
-                                        Compose *compose);
+                                        GtkPaned *paned);
 static gboolean compose_edit_size_alloc (GtkEditable   *widget,
                                         GtkAllocation  *allocation,
                                         GtkSHRuler     *shruler);
@@ -760,8 +763,8 @@ static GtkTargetEntry compose_mime_types[] =
 
 static gboolean compose_put_existing_to_front(MsgInfo *info)
 {
-       GList *compose_list = compose_get_compose_list();
-       GList *elem = NULL;
+       const GList *compose_list = compose_get_compose_list();
+       const GList *elem = NULL;
        
        if (compose_list) {
                for (elem = compose_list; elem != NULL && elem->data != NULL; 
@@ -1203,7 +1206,7 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
 
                for (curr = attach_files ; curr != NULL ; curr = curr->next) {
                        ainfo = (AttachInfo *) curr->data;
-                       compose_attach_append(compose, ainfo->file, ainfo->name,
+                       compose_attach_append(compose, ainfo->file, ainfo->file,
                                        ainfo->content_type, ainfo->charset);
                }
        }
@@ -1266,7 +1269,8 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        if (prefs_common.auto_exteditor)
                compose_exec_ext_editor(compose);
 
-       compose->draft_timeout_tag = -1;
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET;
+
        SCROLL_TO_CURSOR(compose);
 
        compose->modified = FALSE;
@@ -1707,7 +1711,7 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
        compose_set_title(compose);
 
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
        
        if (compose->deferred_destroy) {
@@ -1910,7 +1914,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
        compose_set_title(compose);
 
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
@@ -2058,7 +2062,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
        compose_set_title(compose);
 
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
@@ -2105,6 +2109,44 @@ static gboolean compose_is_sig_separator(Compose *compose, GtkTextBuffer *textbu
        return FALSE;
 }
 
+static gboolean compose_update_folder_hook(gpointer source, gpointer data)
+{
+       FolderUpdateData *hookdata = (FolderUpdateData *)source;
+       Compose *compose = (Compose *)data;
+       FolderItem *old_item = NULL;
+       FolderItem *new_item = NULL;
+       gchar *old_id, *new_id;
+
+       if (!(hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
+        && !(hookdata->update_flags & FOLDER_MOVE_FOLDERITEM))
+               return FALSE;
+
+       old_item = hookdata->item;
+       new_item = hookdata->item2;
+
+       old_id = folder_item_get_identifier(old_item);
+       new_id = new_item ? folder_item_get_identifier(new_item) : g_strdup("NULL");
+
+       if (compose->targetinfo && compose->targetinfo->folder == old_item) {
+               debug_print("updating targetinfo folder: %s -> %s\n", old_id, new_id);
+               compose->targetinfo->folder = new_item;
+       }
+
+       if (compose->replyinfo && compose->replyinfo->folder == old_item) {
+               debug_print("updating replyinfo folder: %s -> %s\n", old_id, new_id);
+               compose->replyinfo->folder = new_item;
+       }
+
+       if (compose->fwdinfo && compose->fwdinfo->folder == old_item) {
+               debug_print("updating fwdinfo folder: %s -> %s\n", old_id, new_id);
+               compose->fwdinfo->folder = new_item;
+       }
+
+       g_free(old_id);
+       g_free(new_id);
+       return FALSE;
+}
+
 static void compose_colorize_signature(Compose *compose)
 {
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
@@ -2131,21 +2173,21 @@ static void compose_colorize_signature(Compose *compose)
                                G_CALLBACK(text_inserted),              \
                                compose);                               \
 }
-#define UNBLOCK_WRAP() {                                               \
-       compose->autowrap = prev_autowrap;                              \
-       if (compose->autowrap) {                                        \
-               gint old = compose->draft_timeout_tag;                  \
-               compose->draft_timeout_tag = -2;                        \
-               compose_wrap_all(compose);                              \
-               compose->draft_timeout_tag = old;                       \
-       }                                                               \
-                                                                       \
-       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),             \
-                               G_CALLBACK(compose_changed_cb),         \
-                               compose);                               \
-       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),             \
-                               G_CALLBACK(text_inserted),              \
-                               compose);                               \
+#define UNBLOCK_WRAP() {                                                       \
+       compose->autowrap = prev_autowrap;                                      \
+       if (compose->autowrap) {                                                \
+               gint old = compose->draft_timeout_tag;                          \
+               compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_FORBIDDEN;   \
+               compose_wrap_all(compose);                                      \
+               compose->draft_timeout_tag = old;                               \
+       }                                                                       \
+                                                                               \
+       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),                     \
+                               G_CALLBACK(compose_changed_cb),                 \
+                               compose);                                       \
+       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),                     \
+                               G_CALLBACK(text_inserted),                      \
+                               compose);                                       \
 }
 
 Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
@@ -2408,7 +2450,7 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
        compose_set_title(compose);
 
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
@@ -2518,7 +2560,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        compose->modified = FALSE;
        compose_set_title(compose);
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
@@ -2531,7 +2573,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        return compose;
 }
 
-GList *compose_get_compose_list(void)
+const GList *compose_get_compose_list(void)
 {
        return compose_list;
 }
@@ -3165,7 +3207,7 @@ static gboolean is_subscription(const gchar *ml_post, const gchar *from)
                *(strstr(left_from, "@")) = '\0';
        }
        
-       if (left_ml && left_from && right_ml && right_from
+       if (right_ml && right_from
        &&  !strncmp(left_from, left_ml, strlen(left_ml))
        &&  !strcmp(right_from, right_ml)) {
                result = TRUE;
@@ -3533,7 +3575,6 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        gint len;
        FILE *fp;
        gboolean prev_autowrap;
-       gboolean badtxt = FALSE;
        struct stat file_stat;
        int ret;
        GString *file_contents = NULL;
@@ -3633,10 +3674,7 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
 
        fclose(fp);
 
-       if (badtxt)
-               return COMPOSE_INSERT_INVALID_CHARACTER;
-       else 
-               return COMPOSE_INSERT_SUCCESS;
+       return COMPOSE_INSERT_SUCCESS;
 }
 
 static gboolean compose_attach_append(Compose *compose, const gchar *file,
@@ -4293,7 +4331,7 @@ static gboolean compose_beautify_paragraph(Compose *compose, GtkTextIter *par_it
        GtkTextIter iter, break_pos, end_of_line;
        gchar *quote_str = NULL;
        gint quote_len;
-       gboolean wrap_quote = prefs_common.linewrap_quote;
+       gboolean wrap_quote = force || prefs_common.linewrap_quote;
        gboolean prev_autowrap = compose->autowrap;
        gint startq_offset = -1, noq_offset = -1;
        gint uri_start = -1, uri_stop = -1;
@@ -4312,7 +4350,7 @@ static gboolean compose_beautify_paragraph(Compose *compose, GtkTextIter *par_it
        if (force) {
                modified = TRUE;
        }
-       if (compose->draft_timeout_tag == -2) {
+       if (compose->draft_timeout_tag == COMPOSE_DRAFT_TIMEOUT_FORBIDDEN) {
                modified = TRUE;
        }
 
@@ -4329,7 +4367,7 @@ static gboolean compose_beautify_paragraph(Compose *compose, GtkTextIter *par_it
        }
 
 
-       if (compose->draft_timeout_tag == -2) {
+       if (compose->draft_timeout_tag == COMPOSE_DRAFT_TIMEOUT_FORBIDDEN) {
                if (gtk_text_iter_ends_line(&iter)) {
                        while (gtk_text_iter_ends_line(&iter) &&
                               gtk_text_iter_forward_line(&iter))
@@ -4691,9 +4729,14 @@ static void compose_wrap_all_full(Compose *compose, gboolean force)
        buffer = gtk_text_view_get_buffer(text);
 
        gtk_text_buffer_get_start_iter(buffer, &iter);
+
+       undo_wrapping(compose->undostruct, TRUE);
+
        while (!gtk_text_iter_is_end(&iter) && modified)
                modified = compose_beautify_paragraph(compose, &iter, force);
 
+       undo_wrapping(compose->undostruct, FALSE);
+
 }
 
 static void compose_set_title(Compose *compose)
@@ -5348,6 +5391,8 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
                }
                generate_msgid(buf, sizeof(buf), addr);
                err |= (fprintf(fp, "Resent-Message-ID: <%s>\n", buf) < 0);
+               if (compose->msgid)
+                       g_free(compose->msgid);
                compose->msgid = g_strdup(buf);
        } else {
                compose->msgid = NULL;
@@ -5708,8 +5753,11 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
                (tmp, CS_INTERNAL, conv_get_locale_charset_str());
 
        g_free(tmp);
-       if (!chars) return -1;
-
+       if (!chars) {
+               fclose(fp);
+               claws_unlink(file);
+               return -1;
+       }
        /* write body */
        len = strlen(chars);
        if (fwrite(chars, sizeof(gchar), len, fp) != len) {
@@ -5965,17 +6013,25 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        }
        /* Message-ID of message replying to */
        if ((compose->replyinfo != NULL) && (compose->replyinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->replyinfo->folder)
+                       folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "RMID:%s\t%d\t%s\n", folderid, compose->replyinfo->msgnum, compose->replyinfo->msgid) < 0);
                g_free(folderid);
        }
        /* Message-ID of message forwarding to */
        if ((compose->fwdinfo != NULL) && (compose->fwdinfo->msgid != NULL)) {
-               gchar *folderid;
+               gchar *folderid = NULL;
                
-               folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (compose->fwdinfo->folder)
+                       folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "FMID:%s\t%d\t%s\n", folderid, compose->fwdinfo->msgnum, compose->fwdinfo->msgid) < 0);
                g_free(folderid);
        }
@@ -6082,13 +6138,14 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
                        }
                        continue;
                }
+               if (g_stat(ainfo->file, &statbuf) < 0)
+                       return -1;
+
                mimepart = procmime_mimeinfo_new();
                mimepart->content = MIMECONTENT_FILE;
                mimepart->data.filename = g_strdup(ainfo->file);
                mimepart->tmp = FALSE; /* or we destroy our attachment */
                mimepart->offset = 0;
-
-               g_stat(ainfo->file, &statbuf);
                mimepart->length = statbuf.st_size;
 
                type = g_strdup(ainfo->content_type);
@@ -6424,6 +6481,8 @@ static gchar *compose_get_header(Compose *compose)
                }
                generate_msgid(buf, sizeof(buf), addr);
                g_string_append_printf(header, "Message-ID: <%s>\n", buf);
+               if (compose->msgid)
+                       g_free(compose->msgid);
                compose->msgid = g_strdup(buf);
        } else {
                compose->msgid = NULL;
@@ -6769,9 +6828,6 @@ static void compose_create_header_entry(Compose *compose)
        gboolean standard_header = FALSE;
        GtkListStore *model;
        GtkTreeIter iter;
-#if !(GTK_CHECK_VERSION(2,12,0))
-       GtkTooltips *tips = compose->tooltips;
-#endif
        
        headerentry = g_new0(ComposeHeaderEntry, 1);
 
@@ -6806,7 +6862,11 @@ static void compose_create_header_entry(Compose *compose)
        g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN(combo))), "grab_focus",
                         G_CALLBACK(compose_grab_focus_cb), compose);
        gtk_widget_show(combo);
-       
+
+       /* Putting only the combobox child into focus chain of its parent causes
+        * the parent to be skipped when changing focus via Tab or Shift+Tab.
+        * This eliminates need to pres Tab twice in order to really get from the
+        * combobox to next widget. */
        GList *l = NULL;
        l = g_list_prepend(l, gtk_bin_get_child(GTK_BIN(combo)));
        gtk_container_set_focus_chain(GTK_CONTAINER(combo), l);
@@ -6815,7 +6875,7 @@ static void compose_create_header_entry(Compose *compose)
        gtk_table_attach(GTK_TABLE(compose->header_table), combo, 0, 1,
                        compose->header_nextrow, compose->header_nextrow+1,
                        GTK_SHRINK, GTK_FILL, 0, 0);
-       if (compose->header_last && (compose->draft_timeout_tag != -2)) {
+       if (compose->header_last && (compose->draft_timeout_tag != COMPOSE_DRAFT_TIMEOUT_FORBIDDEN)) {
                const gchar *last_header_entry = gtk_entry_get_text(
                                GTK_ENTRY(gtk_bin_get_child(GTK_BIN((compose->header_last->combo)))));
                string = headers;
@@ -7316,8 +7376,9 @@ static void compose_dict_changed(void *data)
 {
        Compose *compose = (Compose *) data;
 
-       if(compose->gtkaspell && 
-                  compose->gtkaspell->recheck_when_changing_dict == FALSE)
+       if(!compose->gtkaspell)
+               return; 
+       if(compose->gtkaspell->recheck_when_changing_dict == FALSE)
                return;
 
        gtkaspell_highlight_all(compose->gtkaspell);
@@ -7397,10 +7458,6 @@ static Compose *compose_create(PrefsAccount *account,
        compose->mutex = cm_mutex_new();
        compose->set_cursor_pos = -1;
 
-#if !(GTK_CHECK_VERSION(2,12,0))
-       compose->tooltips = tips;
-#endif
-
        window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "compose");
 
        gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
@@ -7649,7 +7706,6 @@ static Compose *compose_create(PrefsAccount *account,
        
        /* Notebook */
        notebook = gtk_notebook_new();
-       gtk_widget_set_size_request(notebook, -1, prefs_common.compose_notebook_height);
        gtk_widget_show(notebook);
 
        /* header labels and entries */
@@ -7689,7 +7745,7 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_container_set_border_width(GTK_CONTAINER(subject), 0);
        gtk_widget_show(subject);
 
-       label = gtk_label_new(_("Subject:"));
+       label = gtk_label_new_with_mnemonic(_("Subject:"));
        gtk_box_pack_start(GTK_BOX(subject), label, FALSE, FALSE, 0);
        gtk_widget_show(label);
 
@@ -7701,6 +7757,7 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_box_pack_start(GTK_BOX(subject), subject_entry, TRUE, TRUE, 0);
        g_signal_connect_after(G_OBJECT(subject_entry), "grab_focus",
                         G_CALLBACK(compose_grab_focus_cb), compose);
+       gtk_label_set_mnemonic_widget(GTK_LABEL(label), subject_entry);
        gtk_widget_show(subject_entry);
        compose->subject_entry = subject_entry;
        gtk_container_add(GTK_CONTAINER(subject_frame), subject);
@@ -7739,8 +7796,6 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
        
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
-       g_signal_connect(G_OBJECT(notebook), "size_allocate",
-                        G_CALLBACK(compose_notebook_size_alloc), compose);     
        g_signal_connect_after(G_OBJECT(text), "size_allocate",
                               G_CALLBACK(compose_edit_size_alloc),
                               ruler);
@@ -7777,8 +7832,12 @@ static Compose *compose_create(PrefsAccount *account,
        /* pane between attach clist and text */
        paned = gtk_vpaned_new();
        gtk_container_add(GTK_CONTAINER(vbox2), paned);
-       gtk_paned_add1(GTK_PANED(paned), notebook);
-       gtk_paned_add2(GTK_PANED(paned), edit_vbox);
+       gtk_paned_pack1(GTK_PANED(paned), notebook, FALSE, FALSE);
+       gtk_paned_pack2(GTK_PANED(paned), edit_vbox, TRUE, FALSE);
+       gtk_paned_set_position(GTK_PANED(paned), prefs_common.compose_notebook_height);
+       g_signal_connect(G_OBJECT(notebook), "size_allocate",
+                        G_CALLBACK(compose_notebook_size_alloc), paned);
+
        gtk_widget_show_all(paned);
 
 
@@ -7882,7 +7941,12 @@ static Compose *compose_create(PrefsAccount *account,
        compose->exteditor_file    = NULL;
        compose->exteditor_pid     = -1;
        compose->exteditor_tag     = -1;
-       compose->draft_timeout_tag = -2; /* inhibit auto-drafting while loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_FORBIDDEN; /* inhibit auto-drafting while loading */
+
+       compose->folder_update_callback_id =
+               hooks_register_hook(FOLDER_UPDATE_HOOKLIST,
+                               compose_update_folder_hook,
+                               (gpointer) compose);
 
 #if USE_ENCHANT
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Spelling", FALSE);
@@ -7987,9 +8051,6 @@ static GtkWidget *compose_account_option_menu_create(Compose *compose)
        GtkListStore *menu;
        GtkTreeIter iter;
        GtkWidget *from_name = NULL;
-#if !(GTK_CHECK_VERSION(2,12,0))
-       GtkTooltips *tips = compose->tooltips;
-#endif
 
        gint num = 0, def_menu = 0;
        
@@ -8044,6 +8105,15 @@ static GtkWidget *compose_account_option_menu_create(Compose *compose)
 
        gtk_box_pack_start(GTK_BOX(hbox), optmenubox, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(hbox), from_name, TRUE, TRUE, 0);
+
+       /* Putting only the GtkEntry into focus chain of parent hbox causes
+        * the account selector combobox next to it to be unreachable when
+        * navigating widgets in GtkTable with up/down arrow keys.
+        * Note: gtk_widget_set_can_focus() was not enough. */
+       GList *l = NULL;
+       l = g_list_prepend(l, from_name);
+       gtk_container_set_focus_chain(GTK_CONTAINER(hbox), l);
+       g_list_free(l);
        
        CLAWS_SET_TIP(optmenubox,
                _("Account to use for this email"));
@@ -8624,6 +8694,7 @@ static void compose_destroy(Compose *compose)
                compose->deferred_destroy = TRUE;
                return;
        }
+
        /* NOTE: address_completion_end() does nothing with the window
         * however this may change. */
        address_completion_end(compose->window);
@@ -8639,6 +8710,9 @@ static void compose_destroy(Compose *compose)
 
        g_hash_table_destroy(compose->email_hashtable);
 
+       hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST,
+                       compose->folder_update_callback_id);
+
        procmsg_msginfo_free(compose->targetinfo);
        procmsg_msginfo_free(compose->replyinfo);
        procmsg_msginfo_free(compose->fwdinfo);
@@ -8854,7 +8928,7 @@ static void compose_attach_property(GtkAction *action, gpointer data)
                gtk_widget_hide(attach_prop.window);
                gtk_window_set_modal(GTK_WINDOW(attach_prop.window), FALSE);
                
-               if (cancelled) 
+               if (cancelled)
                        break;
 
                entry_text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
@@ -9402,9 +9476,9 @@ static void compose_undo_state_changed(UndoMain *undostruct, gint undo_state,
 
 static void compose_notebook_size_alloc(GtkNotebook *notebook,
                                        GtkAllocation *allocation,
-                                       Compose *compose)
+                                       GtkPaned *paned)
 {
-       prefs_common.compose_notebook_height = allocation->height;
+       prefs_common.compose_notebook_height = gtk_paned_get_position(paned);
 }
 
 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
@@ -9460,7 +9534,7 @@ static void account_activated(GtkComboBox *optmenu, gpointer data)
 
        /* Get ID of active account in the combo box */
        menu = gtk_combo_box_get_model(optmenu);
-       gtk_combo_box_get_active_iter(optmenu, &iter);
+       cm_return_if_fail(gtk_combo_box_get_active_iter(optmenu, &iter));
        gtk_tree_model_get(menu, &iter, 1, &account_id, -1);
 
        ac = account_find_from_id(account_id);
@@ -9627,7 +9701,7 @@ static void compose_send_cb(GtkAction *action, gpointer data)
        
        if (compose->draft_timeout_tag >= 0) { /* CLAWS: disable draft timeout */
                g_source_remove(compose->draft_timeout_tag);
-               compose->draft_timeout_tag = -1;
+               compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET;
        }
 
        compose_send(compose);
@@ -9712,7 +9786,7 @@ gboolean compose_draft (gpointer data, guint action)
                debug_print("couldn't lock mutex, probably sending\n");
                return FALSE;
        }
-       
+
        lock = TRUE;
 
        tmp = g_strdup_printf("%s%cdraft.%p", get_tmp_dir(),
@@ -9750,17 +9824,25 @@ gboolean compose_draft (gpointer data, guint action)
 
        /* Message-ID of message replying to */
        if ((compose->replyinfo != NULL) && (compose->replyinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->replyinfo->folder)
+                       folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "RMID:%s\t%d\t%s\n", folderid, compose->replyinfo->msgnum, compose->replyinfo->msgid) < 0);
                g_free(folderid);
        }
        /* Message-ID of message forwarding to */
        if ((compose->fwdinfo != NULL) && (compose->fwdinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->fwdinfo->folder)
+                       folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "FMID:%s\t%d\t%s\n", folderid, compose->fwdinfo->msgnum, compose->fwdinfo->msgid) < 0);
                g_free(folderid);
        }
@@ -11091,10 +11173,7 @@ static void compose_insert_drag_received_cb (GtkWidget         *widget,
                g_list_free(list);
                gtk_drag_finish(drag_context, TRUE, FALSE, time);
                return;
-       } else {
-               return;
        }
-       gtk_drag_finish(drag_context, TRUE, FALSE, time);
 }
 
 static void compose_header_drag_received_cb (GtkWidget         *widget,
@@ -11213,7 +11292,7 @@ static void compose_headerentry_changed_cb(GtkWidget *entry,
 
 static gboolean compose_defer_auto_save_draft(Compose *compose)
 {
-       compose->draft_timeout_tag = -1;
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET;
        compose_draft((gpointer)compose, COMPOSE_AUTO_SAVE);
        return FALSE;
 }
@@ -11337,7 +11416,7 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
 
        if (compose_can_autosave(compose) && 
            gtk_text_buffer_get_char_count(buffer) % prefs_common.autosave_length == 0 &&
-           compose->draft_timeout_tag != -2 /* disabled while loading */)
+           compose->draft_timeout_tag != COMPOSE_DRAFT_TIMEOUT_FORBIDDEN /* disabled while loading */)
                compose->draft_timeout_tag = g_timeout_add
                        (500, (GSourceFunc) compose_defer_auto_save_draft, compose);
 }
@@ -11449,16 +11528,27 @@ gboolean compose_close(Compose *compose)
 {
        gint x, y;
 
+       cm_return_val_if_fail(compose, FALSE);
+
        if (!g_mutex_trylock(compose->mutex)) {
                /* we have to wait for the (possibly deferred by auto-save)
                 * drafting to be done, before destroying the compose under
                 * it. */
                debug_print("waiting for drafting to finish...\n");
                compose_allow_user_actions(compose, FALSE);
-               g_timeout_add (500, (GSourceFunc) compose_close, compose);
-               return FALSE;
+               if (compose->close_timeout_tag == 0) {
+                       compose->close_timeout_tag = 
+                               g_timeout_add (500, (GSourceFunc) compose_close,
+                               compose);
+               }
+               return TRUE;
        }
-       cm_return_val_if_fail(compose, FALSE);
+
+       if (compose->draft_timeout_tag >= 0) {
+               g_source_remove(compose->draft_timeout_tag);
+               compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_FORBIDDEN;
+       }
+
        gtkut_widget_get_uposition(compose->window, &x, &y);
        if (!compose->batch) {
                prefs_common.compose_x = x;