2007-07-03 [paul] 2.10.0cvs3
[claws.git] / src / compose.c
index 8493f011dc15495eac6654c8c4d2c06594bcf4ee..3503431b8a1f8e2af60a1b3ad5666d63856a1610 100644 (file)
@@ -199,6 +199,7 @@ static Compose *compose_generic_new                 (PrefsAccount   *account,
                                                 GList          *listAddress );
 
 static Compose *compose_create                 (PrefsAccount   *account,
+                                                FolderItem              *item,
                                                 ComposeMode     mode,
                                                 gboolean batch);
 
@@ -980,8 +981,8 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        GtkTextBuffer *textbuf;
        GtkTextIter iter;
        GtkItemFactory *ifactory;
-       gchar *subject_format = NULL;
-       gchar *body_format = NULL;
+       const gchar *subject_format = NULL;
+       const gchar *body_format = NULL;
 
        if (item && item->prefs && item->prefs->enable_default_account)
                account = account_find_from_id(item->prefs->default_account);
@@ -989,7 +990,7 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        if (!account) account = cur_account;
        g_return_val_if_fail(account != NULL, NULL);
 
-       compose = compose_create(account, COMPOSE_NEW, FALSE);
+       compose = compose_create(account, item, COMPOSE_NEW, FALSE);
 
        ifactory = gtk_item_factory_from_widget(compose->menubar);
 
@@ -1141,6 +1142,7 @@ 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->modified = FALSE;
        compose_set_title(compose);
         return compose;
@@ -1401,8 +1403,8 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
        GtkTextView *textview;
        GtkTextBuffer *textbuf;
        gboolean quote = FALSE;
-       gchar *qmark = NULL;
-       gchar *body_fmt = NULL;
+       const gchar *qmark = NULL;
+       const gchar *body_fmt = NULL;
 
        g_return_val_if_fail(msginfo != NULL, NULL);
        g_return_val_if_fail(msginfo->folder != NULL, NULL);
@@ -1411,7 +1413,7 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
 
        g_return_val_if_fail(account != NULL, NULL);
 
-       compose = compose_create(account, COMPOSE_REPLY, FALSE);
+       compose = compose_create(account, msginfo->folder, COMPOSE_REPLY, FALSE);
 
        compose->updating = TRUE;
 
@@ -1504,6 +1506,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 */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -1538,7 +1541,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                                (msginfo)))
                account = cur_account;
 
-       compose = compose_create(account, COMPOSE_FORWARD, batch);
+       compose = compose_create(account, msginfo->folder, COMPOSE_FORWARD, batch);
 
        compose->updating = TRUE;
        compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
@@ -1578,8 +1581,8 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 
                g_free(msgfile);
        } else {
-               gchar *qmark = NULL;
-               gchar *body_fmt = prefs_common.fw_quotefmt;
+               const gchar *qmark = NULL;
+               const gchar *body_fmt = prefs_common.fw_quotefmt;
                MsgInfo *full_msginfo;
 
                full_msginfo = procmsg_msginfo_get_full_info(msginfo);
@@ -1604,7 +1607,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                }
 
                /* empty quotemark is not allowed */
-               if (qmark && !*qmark)
+               if (qmark == NULL || *qmark == '\0')
                        qmark = "> ";
 
                compose_quote_fmt(compose, full_msginfo,
@@ -1649,6 +1652,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 */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -1692,7 +1696,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
                MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
        }
 
-       compose = compose_create(account, COMPOSE_FORWARD, FALSE);
+       compose = compose_create(account, ((MsgInfo *)msginfo_list->data)->folder, COMPOSE_FORWARD, FALSE);
 
        compose->updating = TRUE;
 
@@ -1750,6 +1754,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 */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -1932,7 +1937,7 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
         }
        g_return_val_if_fail(account != NULL, NULL);
 
-       compose = compose_create(account, COMPOSE_REEDIT, batch);
+       compose = compose_create(account, msginfo->folder, COMPOSE_REEDIT, batch);
        
        compose->replyinfo = replyinfo;
        compose->fwdinfo = fwdinfo;
@@ -2035,6 +2040,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 */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -2061,7 +2067,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
                                        prefs_common.reply_account_autosel);
        g_return_val_if_fail(account != NULL, NULL);
 
-       compose = compose_create(account, COMPOSE_REDIRECT, batch);
+       compose = compose_create(account, msginfo->folder, COMPOSE_REDIRECT, batch);
 
        compose->updating = TRUE;
 
@@ -2143,6 +2149,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 */
 
        if (compose->deferred_destroy) {
                compose_destroy(compose);
@@ -6286,7 +6293,9 @@ static gboolean compose_popup_menu(GtkWidget *widget, gpointer data)
 }
 
 static gboolean compose_force_window_origin = TRUE;
-static Compose *compose_create(PrefsAccount *account, ComposeMode mode,
+static Compose *compose_create(PrefsAccount *account,
+                                                FolderItem *folder,
+                                                ComposeMode mode,
                                                 gboolean batch)
 {
        Compose   *compose;
@@ -6342,6 +6351,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode,
 
        compose->batch = batch;
        compose->account = account;
+       compose->folder = folder;
        
        compose->mutex = g_mutex_new();
        compose->set_cursor_pos = -1;
@@ -6619,7 +6629,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode,
        compose->exteditor_file    = NULL;
        compose->exteditor_pid     = -1;
        compose->exteditor_tag     = -1;
-       compose->draft_timeout_tag = -1;
+       compose->draft_timeout_tag = -2; /* inhibit auto-drafting while loading */
 
 #if USE_ASPELL
        menu_set_sensitive(ifactory, "/Spelling", FALSE);
@@ -7107,6 +7117,44 @@ void compose_reflect_prefs_pixmap_theme(void)
        }
 }
 
+static const gchar *compose_quote_char_from_context(Compose *compose)
+{
+       const gchar *qmark = NULL;
+
+       g_return_val_if_fail(compose != NULL, NULL);
+
+       switch (compose->mode) {
+               /* use forward-specific quote char */
+               case COMPOSE_FORWARD:
+               case COMPOSE_FORWARD_AS_ATTACH:
+               case COMPOSE_FORWARD_INLINE:
+                       if (compose->folder && compose->folder->prefs &&
+                                       compose->folder->prefs->forward_with_format)
+                               qmark = compose->folder->prefs->forward_quotemark;
+                       else if (compose->account->forward_with_format)
+                               qmark = compose->account->forward_quotemark;
+                       else
+                               qmark = prefs_common.fw_quotemark;
+                       break;
+
+               /* use reply-specific quote char in all other modes */
+               default:
+                       if (compose->folder && compose->folder->prefs &&
+                                       compose->folder->prefs->reply_with_format)
+                               qmark = compose->folder->prefs->reply_quotemark;
+                       else if (compose->account->reply_with_format)
+                               qmark = compose->account->reply_quotemark;
+                       else
+                               qmark = prefs_common.quotemark;
+                       break;
+       }
+
+       if (qmark == NULL || *qmark == '\0')
+               qmark = "> ";
+
+       return qmark;
+}
+
 static void compose_template_apply(Compose *compose, Template *tmpl,
                                   gboolean replace)
 {
@@ -7114,7 +7162,7 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
        GtkTextBuffer *buffer;
        GtkTextMark *mark;
        GtkTextIter iter;
-       gchar *qmark;
+       const gchar *qmark;
        gchar *parsed_str = NULL;
        gint cursor_pos = 0;
        const gchar *err_msg = _("Template body format error at line %d.");
@@ -7126,10 +7174,7 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
        buffer = gtk_text_view_get_buffer(text);
 
        if (tmpl->value) {
-               if (prefs_common.quotemark && *prefs_common.quotemark)
-                       qmark = prefs_common.quotemark;
-               else
-                       qmark = "> ";
+               qmark = compose_quote_char_from_context(compose);
 
                if (compose->replyinfo != NULL) {
 
@@ -8179,7 +8224,7 @@ static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
                  "to send this email.")))
                return;
        
-       if (compose->draft_timeout_tag != -1) { /* CLAWS: disable draft timeout */
+       if (compose->draft_timeout_tag >= 0) { /* CLAWS: disable draft timeout */
                g_source_remove(compose->draft_timeout_tag);
                compose->draft_timeout_tag = -1;
        }
@@ -8219,22 +8264,7 @@ static void compose_send_later_cb(gpointer data, guint action,
        toolbar_main_set_sensitive(mainwindow_get_mainwindow());
 }
 
-void compose_draft (gpointer data, guint action) 
-{
-       compose_draft_cb(data, action, NULL);   
-}
-
 #define DRAFTED_AT_EXIT "drafted_at_exit"
-void compose_clear_exit_drafts(void)
-{
-       gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                                     DRAFTED_AT_EXIT, NULL);
-       if (is_file_exist(filepath))
-               g_unlink(filepath);
-       
-       g_free(filepath);
-}
-
 static void compose_register_draft(MsgInfo *info)
 {
        gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
@@ -8250,34 +8280,7 @@ static void compose_register_draft(MsgInfo *info)
        g_free(filepath);       
 }
 
-void compose_reopen_exit_drafts(void)
-{
-       gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                                     DRAFTED_AT_EXIT, NULL);
-       FILE *fp = fopen(filepath, "rb");
-       gchar buf[1024];
-       
-       if (fp) {
-               while (fgets(buf, sizeof(buf), fp)) {
-                       gchar **parts = g_strsplit(buf, "\t", 2);
-                       const gchar *folder = parts[0];
-                       int msgnum = parts[1] ? atoi(parts[1]):-1;
-                       
-                       if (folder && *folder && msgnum > -1) {
-                               FolderItem *item = folder_find_item_from_identifier(folder);
-                               MsgInfo *info = folder_item_get_msginfo(item, msgnum);
-                               if (info)
-                                       compose_reedit(info, FALSE);
-                       }
-                       g_strfreev(parts);
-               }       
-               fclose(fp);
-       }       
-       g_free(filepath);
-       compose_clear_exit_drafts();
-}
-
-static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
+gboolean compose_draft (gpointer data, guint action) 
 {
        Compose *compose = (Compose *)data;
        FolderItem *draft;
@@ -8289,10 +8292,13 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
        FILE *fp;
        gboolean target_locked = FALSE;
        
-       if (lock) return;
+       if (lock) return FALSE;
+
+       if (compose->sending)
+               return TRUE;
 
        draft = account_get_special_folder(compose->account, F_DRAFT);
-       g_return_if_fail(draft != NULL);
+       g_return_val_if_fail(draft != NULL, FALSE);
        
        if (!g_mutex_trylock(compose->mutex)) {
                /* we don't want to lock the mutex once it's available,
@@ -8300,7 +8306,7 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
                 * it is compose_close - which means once unlocked,
                 * the compose struct will be freed */
                debug_print("couldn't lock mutex, probably sending\n");
-               return;
+               return FALSE;
        }
        
        lock = TRUE;
@@ -8376,8 +8382,29 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
        if ((msgnum = folder_item_add_msg(draft, tmp, &flag, TRUE)) < 0) {
                g_unlink(tmp);
                g_free(tmp);
-               if (action != COMPOSE_AUTO_SAVE)
-                       alertpanel_error(_("Could not save draft."));
+               if (action != COMPOSE_AUTO_SAVE) {
+                       if (action != COMPOSE_DRAFT_FOR_EXIT)
+                               alertpanel_error(_("Could not save draft."));
+                       else {
+                               AlertValue val;
+                               gtkut_window_popup(compose->window);
+                               val = alertpanel_full(_("Could not save draft"),
+                                       _("Could not save draft.\n"
+                                       "Do you want to cancel exit or discard this email?"),
+                                         _("_Cancel exit"), _("_Discard email"), NULL,
+                                         FALSE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
+                               if (val == G_ALERTALTERNATE) {
+                                       lock = FALSE;
+                                       g_mutex_unlock(compose->mutex); /* must be done before closing */
+                                       compose_close(compose);
+                                       return TRUE;
+                               } else {
+                                       lock = FALSE;
+                                       g_mutex_unlock(compose->mutex); /* must be done before closing */
+                                       return FALSE;
+                               }
+                       }
+               }
                goto unlock;
        }
        g_free(tmp);
@@ -8409,7 +8436,7 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
                lock = FALSE;
                g_mutex_unlock(compose->mutex); /* must be done before closing */
                compose_close(compose);
-               return;
+               return TRUE;
        } else {
                struct stat s;
                gchar *path;
@@ -8445,6 +8472,49 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
 unlock:
        lock = FALSE;
        g_mutex_unlock(compose->mutex);
+       return TRUE;
+}
+
+void compose_clear_exit_drafts(void)
+{
+       gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+                                     DRAFTED_AT_EXIT, NULL);
+       if (is_file_exist(filepath))
+               g_unlink(filepath);
+       
+       g_free(filepath);
+}
+
+void compose_reopen_exit_drafts(void)
+{
+       gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+                                     DRAFTED_AT_EXIT, NULL);
+       FILE *fp = fopen(filepath, "rb");
+       gchar buf[1024];
+       
+       if (fp) {
+               while (fgets(buf, sizeof(buf), fp)) {
+                       gchar **parts = g_strsplit(buf, "\t", 2);
+                       const gchar *folder = parts[0];
+                       int msgnum = parts[1] ? atoi(parts[1]):-1;
+                       
+                       if (folder && *folder && msgnum > -1) {
+                               FolderItem *item = folder_find_item_from_identifier(folder);
+                               MsgInfo *info = folder_item_get_msginfo(item, msgnum);
+                               if (info)
+                                       compose_reedit(info, FALSE);
+                       }
+                       g_strfreev(parts);
+               }       
+               fclose(fp);
+       }       
+       g_free(filepath);
+       compose_clear_exit_drafts();
+}
+
+static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
+{
+       compose_draft(data, action);
 }
 
 static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
@@ -9527,16 +9597,14 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
                                        compose);
        if (paste_as_quotation) {
                gchar *new_text;
-               gchar *qmark;
+               const gchar *qmark;
 
                if (len < 0)
                        len = strlen(text);
 
                new_text = g_strndup(text, len);
-               if (prefs_common.quotemark && *prefs_common.quotemark)
-                       qmark = prefs_common.quotemark;
-               else
-                       qmark = "> ";
+
+               qmark = compose_quote_char_from_context(compose);
 
                mark = gtk_text_buffer_create_mark(buffer, NULL, iter, FALSE);
                gtk_text_buffer_place_cursor(buffer, iter);
@@ -9574,7 +9642,8 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
        g_signal_stop_emission_by_name(G_OBJECT(buffer), "insert-text");
 
        if (prefs_common.autosave && 
-           gtk_text_buffer_get_char_count(buffer) % prefs_common.autosave_length == 0)
+           gtk_text_buffer_get_char_count(buffer) % prefs_common.autosave_length == 0 &&
+           compose->draft_timeout_tag != -2 /* disabled while loading */)
                compose->draft_timeout_tag = g_timeout_add
                        (500, (GtkFunction) compose_defer_auto_save_draft, compose);
 }
@@ -9680,6 +9749,7 @@ gboolean compose_close(Compose *compose)
                 * 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;
        }
@@ -9736,6 +9806,8 @@ static void compose_reply_from_messageview_real(MessageView *msgview, GSList *ms
                                }
                                tmp_msginfo->folder = orig_msginfo->folder;
                                tmp_msginfo->msgnum = orig_msginfo->msgnum; 
+                               if (orig_msginfo->tags)
+                                       tmp_msginfo->tags = g_slist_copy(orig_msginfo->tags);
                        }
                }
        }