2007-02-23 [colin] 2.7.2cvs68
[claws.git] / src / compose.c
index 4cd812676b762c07fb3d9bcc7136f4dc22915539..a3225687cf5ac9e0c47d368c5764c6bc7e0ac473 100644 (file)
@@ -171,13 +171,6 @@ typedef enum
        COMPOSE_INSERT_NO_FILE
 } ComposeInsertResult;
 
-typedef enum
-{
-       COMPOSE_QUIT_EDITING,
-       COMPOSE_KEEP_EDITING,
-       COMPOSE_AUTO_SAVE
-} ComposeDraftAction;
-
 typedef enum
 {
        COMPOSE_WRITE_FOR_SEND,
@@ -191,7 +184,7 @@ typedef enum
 
 static GList *compose_list = NULL;
 
-Compose *compose_generic_new                   (PrefsAccount   *account,
+static Compose *compose_generic_new                    (PrefsAccount   *account,
                                                 const gchar    *to,
                                                 FolderItem     *item,
                                                 GPtrArray      *attach_files,
@@ -201,6 +194,27 @@ static Compose *compose_create                     (PrefsAccount   *account,
                                                 ComposeMode     mode,
                                                 gboolean batch);
 
+static void compose_entry_mark_default_to      (Compose          *compose,
+                                        const gchar      *address);
+static Compose *compose_followup_and_reply_to  (MsgInfo        *msginfo,
+                                        gboolean        quote,
+                                        gboolean        to_all,
+                                        gboolean        to_sender,
+                                        const gchar    *body);
+static Compose *compose_forward_multiple       (PrefsAccount   *account, 
+                                        GSList         *msginfo_list);
+static Compose *compose_reply                  (MsgInfo        *msginfo,
+                                        gboolean        quote,
+                                        gboolean        to_all,
+                                        gboolean        to_ml,
+                                        gboolean        to_sender,
+                                        const gchar    *body);
+static Compose *compose_reply_mode             (ComposeMode     mode, 
+                                        GSList         *msginfo_list, 
+                                        gchar          *body);
+static void compose_template_apply_fields(Compose *compose, Template *tmpl);
+static void compose_update_privacy_systems_menu(Compose        *compose);
+
 static GtkWidget *compose_account_option_menu_create
                                                (Compose        *compose);
 static void compose_set_out_encoding           (Compose        *compose);
@@ -275,7 +289,7 @@ static gint compose_write_body_to_file              (Compose        *compose,
                                                 const gchar    *file);
 static gint compose_remove_reedit_target       (Compose        *compose,
                                                 gboolean        force);
-void compose_remove_draft                      (Compose        *compose);
+static void compose_remove_draft                       (Compose        *compose);
 static gint compose_queue_sub                  (Compose        *compose,
                                                 gint           *msgnum,
                                                 FolderItem     **item,
@@ -495,9 +509,9 @@ static Compose *compose_generic_reply(MsgInfo *msginfo, gboolean quote,
                                  gboolean followup_and_reply_to,
                                  const gchar *body);
 
-gboolean compose_headerentry_changed_cb           (GtkWidget          *entry,
+static gboolean compose_headerentry_changed_cb    (GtkWidget          *entry,
                                            ComposeHeaderEntry *headerentry);
-gboolean compose_headerentry_key_press_event_cb(GtkWidget             *entry,
+static gboolean compose_headerentry_key_press_event_cb(GtkWidget              *entry,
                                            GdkEventKey        *event,
                                            ComposeHeaderEntry *headerentry);
 
@@ -533,7 +547,7 @@ static GtkItemFactoryEntry compose_popup_entries[] =
 static GtkItemFactoryEntry compose_entries[] =
 {
        {N_("/_Message"),                               NULL, NULL, 0, "<Branch>"},
-       {N_("/_Message/_Send"),         "<control>Return",
+       {N_("/_Message/S_end"),         "<control>Return",
                                        compose_send_cb, 0, NULL},
        {N_("/_Message/Send _later"),   "<shift><control>S",
                                        compose_send_later_cb,  0, NULL},
@@ -1154,7 +1168,7 @@ static void compose_force_signing(Compose *compose, PrefsAccount *account)
        }
 }      
 
-Compose *compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
+static Compose *compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
 {
        MsgInfo *msginfo;
        guint list_len;
@@ -1284,7 +1298,7 @@ Compose *compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
        return compose;
 }
 
-Compose *compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
+static Compose *compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
                   gboolean to_ml, gboolean to_sender, 
                   const gchar *body)
 {
@@ -1292,7 +1306,7 @@ Compose *compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
                              to_sender, FALSE, body);
 }
 
-Compose *compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
+static Compose *compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
                                   gboolean to_all,
                                   gboolean to_sender,
                                   const gchar *body)
@@ -1592,7 +1606,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 
 #undef INSERT_FW_HEADER
 
-Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
+static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
 {
        Compose *compose;
        GtkTextView *textview;
@@ -2154,7 +2168,7 @@ void compose_entry_append(Compose *compose, const gchar *address,
        }
 }
 
-void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
+static void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
 {
        static GdkColor yellow;
        static GdkColor black;
@@ -3111,6 +3125,17 @@ static gboolean compose_attach_append(Compose *compose, const gchar *file,
        gboolean has_binary = FALSE;
 
        if (!is_file_exist(file)) {
+               gchar *file_from_uri = g_filename_from_uri(file, NULL, NULL);
+               gboolean result = FALSE;
+               if (file_from_uri && is_file_exist(file_from_uri)) {
+                       result = compose_attach_append(
+                                               compose, file_from_uri,
+                                               filename,
+                                               content_type);
+               }
+               g_free(file_from_uri);
+               if (result)
+                       return TRUE;
                alertpanel_error("File %s doesn't exist\n", filename);
                return FALSE;
        }
@@ -3756,12 +3781,6 @@ static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter,
                quote_str = compose_get_quote_str(buffer, &iter, &quote_len);
 
                if (quote_str) {
-                       if (!wrap_quote) {
-                               if (startq_offset == -1) {
-                                       startq_offset = gtk_text_iter_get_offset(&iter);
-                               }
-                               goto colorize;
-                       }
                        debug_print("compose_beautify_paragraph(): quote_str = '%s'\n", quote_str);
                        if (startq_offset == -1) 
                                startq_offset = gtk_text_iter_get_offset(&iter);
@@ -3773,6 +3792,9 @@ static void compose_beautify_paragraph(Compose *compose, GtkTextIter *par_iter,
                                else
                                        quotelevel = 2;
                        }
+                       if (!wrap_quote) {
+                               goto colorize;
+                       }
                } else {
                        if (startq_offset == -1)
                                noq_offset = gtk_text_iter_get_offset(&iter);
@@ -3890,6 +3912,7 @@ colorize:
                        gtk_text_buffer_get_iter_at_offset(
                                buffer, &startquote, startq_offset);
                        endquote = iter;
+
                        switch (quotelevel) {
                        case 0: gtk_text_buffer_apply_tag_by_name(
                                        buffer, "quote0", &startquote, &endquote);
@@ -4931,7 +4954,7 @@ static gint compose_remove_reedit_target(Compose *compose, gboolean force)
        return 0;
 }
 
-void compose_remove_draft(Compose *compose)
+static void compose_remove_draft(Compose *compose)
 {
        FolderItem *drafts;
        MsgInfo *msginfo = compose->targetinfo;
@@ -5786,7 +5809,7 @@ static GtkWidget *compose_create_header(Compose *compose)
        return header_scrolledwin ;
 }
 
-GtkWidget *compose_create_attach(Compose *compose)
+static GtkWidget *compose_create_attach(Compose *compose)
 {
        GtkWidget *attach_scrwin;
        GtkWidget *attach_clist;
@@ -6130,7 +6153,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode,
 
        compose->tooltips = gtk_tooltips_new();
 
-       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+       window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "compose");
        gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
        gtk_widget_set_size_request(window, -1, prefs_common.compose_height);
 
@@ -6791,7 +6814,7 @@ void compose_update_actions_menu(Compose *compose)
        action_update_compose_menu(ifactory, "/Tools/Actions", compose);
 }
 
-void compose_update_privacy_systems_menu(Compose *compose)
+static void compose_update_privacy_systems_menu(Compose *compose)
 {
        static gchar *branch_path = "/Options/Privacy System";
        GtkItemFactory *ifactory;
@@ -6958,7 +6981,7 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
        compose_changed_cb(NULL, compose);
 }
 
-void compose_template_apply_fields(Compose *compose, Template *tmpl)
+static void compose_template_apply_fields(Compose *compose, Template *tmpl)
 {
        MsgInfo* dummyinfo = NULL;
        MsgInfo *msginfo = NULL;
@@ -7329,7 +7352,7 @@ static void compose_attach_property_create(gboolean *cancelled)
 
        debug_print("Creating attach_property window...\n");
 
-       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+       window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "compose_attach_property");
        gtk_widget_set_size_request(window, 480, -1);
        gtk_container_set_border_width(GTK_CONTAINER(window), 8);
        gtk_window_set_title(GTK_WINDOW(window), _("Properties"));
@@ -7952,9 +7975,62 @@ static void compose_send_later_cb(gpointer data, guint action,
        toolbar_main_set_sensitive(mainwindow_get_mainwindow());
 }
 
-void compose_draft (gpointer data) 
+void compose_draft (gpointer data, guint action
 {
-       compose_draft_cb(data, COMPOSE_QUIT_EDITING, NULL);     
+       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,
+                                     DRAFTED_AT_EXIT, NULL);
+       FILE *fp = fopen(filepath, "ab");
+       
+       if (fp) {
+               fprintf(fp, "%s\t%d\n", folder_item_get_identifier(info->folder), 
+                               info->msgnum);
+               fclose(fp);
+       }
+               
+       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)
@@ -8077,12 +8153,15 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
                        procmsg_msginfo_set_flags(newmsginfo, 0,
                                                  MSG_HAS_ATTACHMENT);
 
+               if (action == COMPOSE_DRAFT_FOR_EXIT) {
+                       compose_register_draft(newmsginfo);
+               }
                procmsg_msginfo_free(newmsginfo);
        }
        
        folder_item_scan(draft);
        
-       if (action == COMPOSE_QUIT_EDITING) {
+       if (action == COMPOSE_QUIT_EDITING || action == COMPOSE_DRAFT_FOR_EXIT) {
                lock = FALSE;
                g_mutex_unlock(compose->mutex); /* must be done before closing */
                compose_close(compose);
@@ -8891,10 +8970,13 @@ static void compose_attach_drag_received_cb (GtkWidget          *widget,
            && gtk_drag_get_source_widget(context) != 
                summary_get_main_widget(mainwindow_get_mainwindow()->summaryview)) {
                list = uri_list_extract_filenames((const gchar *)data->data);
-               for (tmp = list; tmp != NULL; tmp = tmp->next)
+               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,
-                                (const gchar *)tmp->data, NULL);
+                                utf8_filename, NULL);
+                       g_free(utf8_filename);
+               }
                if (list) compose_changed_cb(NULL, compose);
                list_free_strings(list);
                g_list_free(list);
@@ -9064,7 +9146,7 @@ static void compose_toggle_remove_refs_cb(gpointer data, guint action,
                compose->remove_references = FALSE;
 }
 
-gboolean compose_headerentry_key_press_event_cb(GtkWidget *entry,
+static gboolean compose_headerentry_key_press_event_cb(GtkWidget *entry,
                                            GdkEventKey *event,
                                            ComposeHeaderEntry *headerentry)
 {
@@ -9096,7 +9178,7 @@ gboolean compose_headerentry_key_press_event_cb(GtkWidget *entry,
        return FALSE;
 }
 
-gboolean compose_headerentry_changed_cb(GtkWidget *entry,
+static gboolean compose_headerentry_changed_cb(GtkWidget *entry,
                                    ComposeHeaderEntry *headerentry)
 {
        if (strlen(gtk_entry_get_text(GTK_ENTRY(entry))) != 0) {