* src/compose.c
[claws.git] / src / compose.c
index 61a3a6fe0e9cfbd8fcedea38d1c1a2720b63fce8..98adacbefaa64c7e74b5c31c91979e68cf2524c9 100644 (file)
@@ -205,6 +205,8 @@ static void compose_attach_parts            (Compose        *compose,
                                                 MsgInfo        *msginfo);
 static void compose_wrap_line                  (Compose        *compose);
 static void compose_wrap_line_all              (Compose        *compose);
+static void compose_wrap_line_all_full         (Compose        *compose,
+                                                gboolean        autowrap);
 static void compose_set_title                  (Compose        *compose);
 
 static PrefsAccount *compose_current_mail_account(void);
@@ -219,6 +221,7 @@ static gint compose_write_to_file           (Compose        *compose,
 static gint compose_write_body_to_file         (Compose        *compose,
                                                 const gchar    *file);
 static gint compose_remove_reedit_target       (Compose        *compose);
+void compose_remove_draft                      (Compose        *compose);
 static gint compose_queue                      (Compose        *compose,
                                                 gint           *msgnum,
                                                 FolderItem     **item);
@@ -478,7 +481,7 @@ void compose_headerentry_key_press_event_cb(GtkWidget              *entry,
 
 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
 
-#if USE_PSPELL
+#if USE_ASPELL
 static void compose_check_all             (Compose *compose);
 static void compose_highlight_all         (Compose *compose);
 static void compose_check_backwards       (Compose *compose);
@@ -597,7 +600,7 @@ static GtkItemFactoryEntry compose_entries[] =
                                        "<control><alt>L", compose_wrap_line_all, 0, NULL},
        {N_("/_Edit/Edit with e_xternal editor"),
                                        "<shift><control>X", compose_ext_editor_cb, 0, NULL},
-#if USE_PSPELL
+#if USE_ASPELL
        {N_("/_Spelling"),              NULL, NULL, 0, "<Branch>"},
        {N_("/_Spelling/_Check all or check selection"),
                                        NULL, compose_check_all, 0, NULL},
@@ -673,58 +676,6 @@ static GtkTargetEntry compose_mime_types[] =
        {"text/uri-list", 0, 0}
 };
 
-Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
-{
-       Compose *c;
-       gchar *filename;
-       GtkItemFactory *ifactory;
-       
-       c = compose_generic_new(account, NULL, NULL, NULL);
-
-       filename = procmsg_get_message_file(msginfo);
-       if (filename == NULL)
-               return NULL;
-
-       c->redirect_filename = filename;
-
-       if (msginfo->subject)
-               gtk_entry_set_text(GTK_ENTRY(c->subject_entry),
-                                  msginfo->subject);
-       gtk_editable_set_editable(GTK_EDITABLE(c->subject_entry), FALSE);
-
-       compose_quote_fmt(c, msginfo, "%M", NULL, NULL);
-       gtk_editable_set_editable(GTK_EDITABLE(c->text), FALSE);
-
-       ifactory = gtk_item_factory_from_widget(c->popupmenu);
-       menu_set_sensitive(ifactory, "/Add...", FALSE);
-       menu_set_sensitive(ifactory, "/Remove", FALSE);
-       menu_set_sensitive(ifactory, "/Property...", FALSE);
-
-       ifactory = gtk_item_factory_from_widget(c->menubar);
-       menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
-       menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
-       menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
-       menu_set_sensitive(ifactory, "/Edit/Paste", FALSE);
-       menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", FALSE);
-       menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", FALSE);
-       menu_set_sensitive(ifactory, "/Edit/Edit with external editor", FALSE);
-       menu_set_sensitive(ifactory, "/Message/Attach", FALSE);
-#if USE_GPGME
-       menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
-       menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
-#endif
-       menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
-       menu_set_sensitive(ifactory, "/Tools/Template", FALSE);
-       
-       gtk_widget_set_sensitive(c->insert_btn, FALSE);
-       gtk_widget_set_sensitive(c->attach_btn, FALSE);
-       gtk_widget_set_sensitive(c->sig_btn, FALSE);
-       gtk_widget_set_sensitive(c->exteditor_btn, FALSE);
-       gtk_widget_set_sensitive(c->linewrap_btn, FALSE);
-
-       return c;
-}
-
 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
                     GPtrArray *attach_files)
 {
@@ -1166,6 +1117,8 @@ Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
        gtk_stext_set_point(GTK_STEXT(compose->text), 0);
 
        gtk_stext_thaw(text);
+       gtk_widget_grab_focus(compose->header_last->entry);
+       
 #if 0 /* NEW COMPOSE GUI */
        if (account->protocol != A_NNTP)
                gtk_widget_grab_focus(compose->to_entry);
@@ -1257,6 +1210,73 @@ void compose_reedit(MsgInfo *msginfo)
                compose_exec_ext_editor(compose);
 }
 
+Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
+{
+       Compose *compose;
+       gchar *filename;
+       GtkItemFactory *ifactory;
+
+       g_return_val_if_fail(msginfo != NULL, NULL);
+
+       if(!account)
+               account = cur_account;
+       g_return_val_if_fail(account != NULL, NULL);
+
+       compose = compose_create(account, COMPOSE_REDIRECT);
+       ifactory = gtk_item_factory_from_widget(compose->menubar);
+
+       compose->replyinfo = NULL;
+
+       compose_show_first_last_header(compose, TRUE);
+
+       gtk_widget_grab_focus(compose->header_last->entry);
+
+       filename = procmsg_get_message_file(msginfo);
+       if (filename == NULL)
+               return NULL;
+
+       compose->redirect_filename = filename;
+
+       if (msginfo->subject)
+               gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
+                                  msginfo->subject);
+       gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
+
+       gtk_stext_freeze(GTK_STEXT(compose->text));
+       compose_quote_fmt(compose, msginfo, "%M", NULL, NULL);
+       gtk_editable_set_editable(GTK_EDITABLE(compose->text), FALSE);
+       gtk_stext_thaw(GTK_STEXT(compose->text));
+
+       ifactory = gtk_item_factory_from_widget(compose->popupmenu);
+       menu_set_sensitive(ifactory, "/Add...", FALSE);
+       menu_set_sensitive(ifactory, "/Remove", FALSE);
+       menu_set_sensitive(ifactory, "/Property...", FALSE);
+
+       ifactory = gtk_item_factory_from_widget(compose->menubar);
+       menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
+       menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
+       menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
+       menu_set_sensitive(ifactory, "/Edit/Paste", FALSE);
+       menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", FALSE);
+       menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", FALSE);
+       menu_set_sensitive(ifactory, "/Edit/Edit with external editor", FALSE);
+       menu_set_sensitive(ifactory, "/Message/Attach", FALSE);
+#if USE_GPGME
+       menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
+       menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
+#endif
+       menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
+       menu_set_sensitive(ifactory, "/Tools/Template", FALSE);
+       
+       gtk_widget_set_sensitive(compose->insert_btn, FALSE);
+       gtk_widget_set_sensitive(compose->attach_btn, FALSE);
+       gtk_widget_set_sensitive(compose->sig_btn, FALSE);
+       gtk_widget_set_sensitive(compose->exteditor_btn, FALSE);
+       gtk_widget_set_sensitive(compose->linewrap_btn, FALSE);
+
+        return compose;
+}
+
 GList *compose_get_compose_list(void)
 {
        return compose_list;
@@ -2326,7 +2346,7 @@ static guint ins_quote(GtkSText *text, guint indent_len,
 
 /* check if we should join the next line */
 static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
-                              guint prev_ilen)
+                              guint prev_ilen, gboolean autowrap)
 {
        guint indent_len, ch_len;
        gboolean do_join = FALSE;
@@ -2334,7 +2354,7 @@ static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
 
        indent_len = get_indent_length(text, start_pos, tlen);
 
-       if (indent_len == prev_ilen) {
+       if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
                GET_CHAR(start_pos + indent_len, cbuf, ch_len);
                if (ch_len > 0 && (cbuf[0] != '\n'))
                        do_join = TRUE;
@@ -2343,10 +2363,15 @@ static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
        return do_join;
 }
 
+static void compose_wrap_line_all(Compose *compose)
+{
+       compose_wrap_line_all_full(compose, FALSE);
+}
+
 #define STEXT_FREEZE() \
        if (!frozen) { gtk_stext_freeze(text); frozen = TRUE; }
 
-static void compose_wrap_line_all(Compose *compose)
+static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
 {
        GtkSText *text = GTK_STEXT(compose->text);
        guint tlen;
@@ -2405,8 +2430,9 @@ static void compose_wrap_line_all(Compose *compose)
                        gchar cb[MB_LEN_MAX];
 
                        /* should we join the next line */
-                       if (do_delete &&
-                           join_next_line(text, cur_pos + 1, tlen, i_len))
+                       if ((autowrap || i_len != cur_len) && do_delete &&
+                           join_next_line
+                               (text, cur_pos + 1, tlen, i_len, autowrap))
                                do_delete = TRUE;
                        else
                                do_delete = FALSE;
@@ -2554,9 +2580,11 @@ static void compose_wrap_line_all(Compose *compose)
                        cur_pos = line_pos - 1;
                        /* start over with current line */
                        is_new_line = TRUE;
-                       line_len = 0;
-                       cur_len = 0;
-                       do_delete = TRUE;
+                       line_len = cur_len = 0;
+                       if (autowrap || i_len > 0)
+                               do_delete = TRUE;
+                       else
+                               do_delete = FALSE;
 #ifdef WRAP_DEBUG
                        g_print("after CR insert ");
                        dump_text(text, line_pos, tlen, 1);
@@ -3348,6 +3376,19 @@ static gint compose_remove_reedit_target(Compose *compose)
        return 0;
 }
 
+void compose_remove_draft(Compose *compose)
+{
+       FolderItem *drafts;
+       MsgInfo *msginfo = compose->targetinfo;
+       drafts = account_get_special_folder(compose->account, F_DRAFT);
+
+       if (procmsg_msg_exist(msginfo)) {
+               folder_item_remove_msg(drafts, msginfo->msgnum);
+               folderview_update_item(drafts, TRUE);
+       }
+
+}
+
 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
 {
        return compose_queue_sub (compose, msgnum, item, FALSE);
@@ -4400,8 +4441,8 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        GtkWidget *tmpl_menu;
        gint n_entries;
 
-#if USE_PSPELL
-        GtkPspell * gtkpspell = NULL;
+#if USE_ASPELL
+        GtkAspell * gtkaspell = NULL;
 #endif
 
        static GdkGeometry geometry;
@@ -4717,31 +4758,31 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
 
        compose->redirect_filename = NULL;
        compose->undostruct = undostruct;
-#if USE_PSPELL
+#if USE_ASPELL
        
        menu_set_sensitive(ifactory, "/Spelling", FALSE);
-        if (prefs_common.enable_pspell) {
-               gtkpspell = gtkpspell_new((const gchar*)prefs_common.dictionary,
+        if (prefs_common.enable_aspell) {
+               gtkaspell = gtkaspell_new((const gchar*)prefs_common.dictionary,
                                          conv_get_current_charset_str(),
                                          prefs_common.misspelled_col,
                                          prefs_common.check_while_typing,
                                          prefs_common.use_alternate,
                                          GTK_STEXT(text));
-               if (!gtkpspell) {
-                       alertpanel_error(_("Spell checker could not be started.\n%s"), gtkpspellcheckers->error_message);
-                       gtkpspell_checkers_reset_error();
+               if (!gtkaspell) {
+                       alertpanel_error(_("Spell checker could not be started.\n%s"), gtkaspellcheckers->error_message);
+                       gtkaspell_checkers_reset_error();
                } else {
 
                        GtkWidget *menuitem;
 
-                       if (!gtkpspell_set_sug_mode(gtkpspell, prefs_common.pspell_sugmode)) {
-                               debug_print("Pspell: could not set suggestion mode %s\n",
-                                   gtkpspellcheckers->error_message);
-                               gtkpspell_checkers_reset_error();
+                       if (!gtkaspell_set_sug_mode(gtkaspell, prefs_common.aspell_sugmode)) {
+                               debug_print("Aspell: could not set suggestion mode %s\n",
+                                   gtkaspellcheckers->error_message);
+                               gtkaspell_checkers_reset_error();
                        }
 
                        menuitem = gtk_item_factory_get_item(ifactory, "/Spelling/Spelling Configuration");
-                       gtkpspell_populate_submenu(gtkpspell, menuitem);
+                       gtkaspell_populate_submenu(gtkaspell, menuitem);
                        menu_set_sensitive(ifactory, "/Spelling", TRUE);
                        }
         }
@@ -4755,8 +4796,8 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        compose->use_followupto = FALSE;
 #endif
 
-#if USE_PSPELL
-        compose->gtkpspell      = gtkpspell;
+#if USE_ASPELL
+        compose->gtkaspell      = gtkaspell;
 #endif
 
 #if 0 /* NEW COMPOSE GUI */
@@ -5206,9 +5247,9 @@ static void compose_destroy(Compose *compose)
        if (addressbook_get_target_compose() == compose)
                addressbook_set_target_compose(NULL);
 
-#if USE_PSPELL
-        if (compose->gtkpspell) {
-               gtkpspell_delete(compose->gtkpspell);
+#if USE_ASPELL
+        if (compose->gtkaspell) {
+               gtkaspell_delete(compose->gtkaspell);
         }
 #endif
 
@@ -6253,7 +6294,7 @@ static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
 {
        Compose *compose = (Compose *)data;
        AlertValue val;
-
+       
        if (compose->exteditor_tag != -1) {
                if (!compose_ext_editor_kill(compose))
                        return;
@@ -6266,6 +6307,8 @@ static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
 
                switch (val) {
                case G_ALERTDEFAULT:
+                       if (prefs_common.autosave)
+                               compose_remove_draft(compose);                  
                        break;
                case G_ALERTALTERNATE:
                        compose_draft_cb(data, 0, NULL);
@@ -6274,6 +6317,7 @@ static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
                        return;
                }
        }
+
        gtk_widget_destroy(compose->window);
 }
 
@@ -6372,60 +6416,35 @@ static void compose_allsel_cb(Compose *compose)
                        (GTK_EDITABLE(compose->focused_editable), 0, -1);
 }
 
-static void compose_gtk_stext_action_cb(Compose *compose, ComposeCallGtkSTextAction action)
+static void compose_gtk_stext_action_cb(Compose *compose,
+                                       ComposeCallGtkSTextAction action)
 {
-       if (!(compose->focused_editable && GTK_WIDGET_HAS_FOCUS(compose->focused_editable))) return;
-               
-       switch (action) {
-               case COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE:
-                       gtk_stext_move_beginning_of_line(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER:
-                       gtk_stext_move_forward_character(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER:
-                       gtk_stext_move_backward_character(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD:
-                       gtk_stext_move_forward_word(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD:
-                       gtk_stext_move_backward_word(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE:
-                       gtk_stext_move_end_of_line(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE:
-                       gtk_stext_move_next_line(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE:
-                       gtk_stext_move_previous_line(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER:
-                       gtk_stext_delete_forward_character(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER:
-                       gtk_stext_delete_backward_character(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD:
-                       gtk_stext_delete_forward_word(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD:
-                       gtk_stext_delete_backward_word(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_DELETE_LINE:
-                       gtk_stext_delete_line(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N:
-                       gtk_stext_delete_line(GTK_STEXT(compose->focused_editable));
-                       gtk_stext_delete_forward_character(GTK_STEXT(compose->focused_editable));
-                       break;
-               case COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END:
-                       gtk_stext_delete_to_line_end(GTK_STEXT(compose->focused_editable));
-                       break;
-               default:
-                       break;
-       }
+       GtkSText *text = GTK_STEXT(compose->text);
+       static struct {
+               void (*do_action) (GtkSText *text);
+       } action_table[] = {
+               {gtk_stext_move_beginning_of_line},
+               {gtk_stext_move_forward_character},
+               {gtk_stext_move_backward_character},
+               {gtk_stext_move_forward_word},
+               {gtk_stext_move_backward_word},
+               {gtk_stext_move_end_of_line},
+               {gtk_stext_move_next_line},
+               {gtk_stext_move_previous_line},
+               {gtk_stext_delete_forward_character},
+               {gtk_stext_delete_backward_character},
+               {gtk_stext_delete_forward_word},
+               {gtk_stext_delete_backward_word},
+               {gtk_stext_delete_line},
+               {gtk_stext_delete_line}, /* gtk_stext_delete_line_n */
+               {gtk_stext_delete_to_line_end}
+       };
+
+       if (!GTK_WIDGET_HAS_FOCUS(text)) return;
+
+       if (action >= COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE &&
+           action <= COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END)
+               action_table[action].do_action(text);
 }
 
 static void compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
@@ -6821,6 +6840,9 @@ static void text_activated(GtkWidget *widget, Compose *compose)
        compose_send_control_enter(compose);
 }
 
+#define EDITABLE_LENGTH(x) \
+       strlen(gtk_editable_get_chars(x,0,-1))
+
 static void text_inserted(GtkWidget *widget, const gchar *text,
                          gint length, gint *position, Compose *compose)
 {
@@ -6849,12 +6871,17 @@ static void text_inserted(GtkWidget *widget, const gchar *text,
                gtk_editable_insert_text(editable, text, length, position);
 
        if (prefs_common.autowrap)
-               compose_wrap_line_all(compose);
+               compose_wrap_line_all_full(compose, TRUE);
 
        gtk_signal_handler_unblock_by_func(GTK_OBJECT(widget),
                                           GTK_SIGNAL_FUNC(text_inserted),
                                           compose);
        gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
+
+       
+       if (prefs_common.autosave && 
+           EDITABLE_LENGTH(editable) % prefs_common.autosave_length == 0)
+               compose_draft_cb((gpointer)compose, 1, NULL);
 }
 
 static gboolean compose_send_control_enter(Compose *compose)
@@ -6890,23 +6917,23 @@ static gboolean compose_send_control_enter(Compose *compose)
        return FALSE;
 }
 
-#if USE_PSPELL
+#if USE_ASPELL
 static void compose_check_all(Compose *compose)
 {
-       if (compose->gtkpspell)
-               gtkpspell_check_all(compose->gtkpspell);
+       if (compose->gtkaspell)
+               gtkaspell_check_all(compose->gtkaspell);
 }
 
 static void compose_highlight_all(Compose *compose)
 {
-       if (compose->gtkpspell)
-               gtkpspell_highlight_all(compose->gtkpspell);
+       if (compose->gtkaspell)
+               gtkaspell_highlight_all(compose->gtkaspell);
 }
 
 static void compose_check_backwards(Compose *compose)
 {
-       if (compose->gtkpspell) 
-               gtkpspell_check_backwards(compose->gtkpspell);
+       if (compose->gtkaspell) 
+               gtkaspell_check_backwards(compose->gtkaspell);
        else {
                GtkItemFactory *ifactory;
                ifactory = gtk_item_factory_from_widget(compose->popupmenu);
@@ -6917,8 +6944,8 @@ static void compose_check_backwards(Compose *compose)
 
 static void compose_check_forwards_go(Compose *compose)
 {
-       if (compose->gtkpspell) 
-               gtkpspell_check_forwards_go(compose->gtkpspell);
+       if (compose->gtkaspell) 
+               gtkaspell_check_forwards_go(compose->gtkaspell);
        else {
                GtkItemFactory *ifactory;
                ifactory = gtk_item_factory_from_widget(compose->popupmenu);