src/compose.[ch]
[claws.git] / src / compose.c
index b7fcada67dc467ca37be794b5453abb5715caa07..59a35289ff85a1e1d5bb02bbe166c39311a2ad20 100644 (file)
@@ -138,6 +138,15 @@ typedef enum
        COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END
 } ComposeCallGtkSTextAction;
 
+typedef enum
+{
+       PRIORITY_HIGHEST = 1,
+       PRIORITY_HIGH,
+       PRIORITY_NORMAL,
+       PRIORITY_LOW,
+       PRIORITY_LOWEST
+} PriorityLevel;
+
 #define B64_LINE_SIZE          57
 #define B64_BUFFSIZE           77
 
@@ -201,7 +210,8 @@ static PrefsAccount *compose_current_mail_account(void);
 /* static gint compose_send                    (Compose        *compose); */
 static gboolean compose_check_for_valid_recipient
                                                (Compose        *compose);
-static gboolean compose_check_entries          (Compose        *compose);
+static gboolean compose_check_entries          (Compose        *compose,
+                                                gboolean       check_subject);
 static gint compose_write_to_file              (Compose        *compose,
                                                 const gchar    *file,
                                                 gboolean        is_draft);
@@ -211,6 +221,10 @@ static gint compose_remove_reedit_target   (Compose        *compose);
 static gint compose_queue                      (Compose        *compose,
                                                 gint           *msgnum,
                                                 FolderItem     **item);
+static gint compose_queue_sub                  (Compose        *compose,
+                                                gint           *msgnum,
+                                                FolderItem     **item,
+                                                gboolean       check_subject);
 static void compose_write_attach               (Compose        *compose,
                                                 FILE           *fp);
 static gint compose_write_headers              (Compose        *compose,
@@ -263,6 +277,7 @@ static gint calc_cursor_xpos        (GtkSText       *text,
 
 static void compose_create_header_entry        (Compose *compose);
 static void compose_add_header_entry   (Compose *compose, gchar *header, gchar *text);
+static void compose_update_priority_menu_item(Compose * compose);
 
 /* callback functions */
 
@@ -402,6 +417,9 @@ static void compose_toggle_encrypt_cb       (gpointer        data,
 #endif
 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
                                             GtkWidget *widget);
+static void compose_set_priority_cb    (gpointer        data,
+                                        guint           action,
+                                        GtkWidget      *widget);
 
 static void compose_attach_drag_received_cb (GtkWidget         *widget,
                                             GdkDragContext     *drag_context,
@@ -623,6 +641,13 @@ static GtkItemFactoryEntry compose_entries[] =
        {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
        {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
 #endif /* USE_GPGME */
+       {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
+       {N_("/_Message/Priority"),      NULL,           NULL,   0, "<Branch>"},
+       {N_("/_Message/Priority/Highest"), NULL, compose_set_priority_cb, PRIORITY_HIGHEST, "<RadioItem>"},
+       {N_("/_Message/Priority/High"),    NULL, compose_set_priority_cb, PRIORITY_HIGH, "/Message/Priority/Highest"},
+       {N_("/_Message/Priority/Normal"),  NULL, compose_set_priority_cb, PRIORITY_NORMAL, "/Message/Priority/Highest"},
+       {N_("/_Message/Priority/Low"),     NULL, compose_set_priority_cb, PRIORITY_LOW, "/Message/Priority/Highest"},
+       {N_("/_Message/Priority/Lowest"),  NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Message/Priority/Highest"},
        {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
        {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
        {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
@@ -1395,6 +1420,7 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
                                       {"Followup-To:",    NULL, FALSE},
                                       {"X-Mailing-List:", NULL, FALSE},
                                       {"X-BeenThere:",    NULL, FALSE},
+                                      {"X-Priority:",     NULL, FALSE},
                                       {NULL,              NULL, FALSE}};
 
        enum
@@ -1406,7 +1432,8 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
                H_NEWSGROUPS     = 4,
                H_FOLLOWUP_TO    = 5,
                H_X_MAILING_LIST = 6,
-               H_X_BEENTHERE    = 7
+               H_X_BEENTHERE    = 7,
+               H_X_PRIORITY     = 8
        };
 
        FILE *fp;
@@ -1475,6 +1502,22 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
                hentry[H_FOLLOWUP_TO].body = NULL;
        }
 
+       if (compose->mode == COMPOSE_REEDIT)
+               if (hentry[H_X_PRIORITY].body != NULL) {
+                       gint priority;
+                       
+                       priority = atoi(hentry[H_X_PRIORITY].body);
+                       g_free(hentry[H_X_PRIORITY].body);
+                       
+                       hentry[H_X_PRIORITY].body = NULL;
+                       
+                       if (priority < PRIORITY_HIGHEST || 
+                           priority > PRIORITY_LOWEST)
+                               priority = PRIORITY_NORMAL;
+                       
+                       compose->priority =  priority;
+               }
        if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
                compose->inreplyto = g_strdup(msginfo->inreplyto);
        else if (compose->mode != COMPOSE_REEDIT &&
@@ -1726,6 +1769,8 @@ static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
        SET_ADDRESS(COMPOSE_BCC, compose->bcc);
        SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
 
+       compose_update_priority_menu_item(compose);
+
        compose_show_first_last_header(compose, TRUE);
 
 #if 0 /* NEW COMPOSE GUI */
@@ -2041,7 +2086,8 @@ static void compose_wrap_line(Compose *compose)
                        }
                        line_end = 1;
                } else {
-                       if (ch_len == 1 && strchr(INDENT_CHARS, *cbuf))
+                       if (ch_len == 1 
+                           && strchr(prefs_common.quote_chars, *cbuf))
                                quoted = 1;
                        else if (ch_len != 1 || !isspace(*cbuf))
                                quoted = 0;
@@ -2064,7 +2110,7 @@ static void compose_wrap_line(Compose *compose)
                        line_end = 1;
                } else {
                        if (line_end && ch_len == 1 &&
-                           strchr(INDENT_CHARS, *cbuf))
+                           strchr(prefs_common.quote_chars, *cbuf))
                                goto compose_end; /* quoted part */
 
                        line_end = 0;
@@ -2209,7 +2255,7 @@ static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
                if (cbuf[0] == '\n')
                        break;
 
-               is_indent = strchr(INDENT_CHARS, cbuf[0]) ? TRUE : FALSE;
+               is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
                is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
 
                switch (state) {
@@ -2237,6 +2283,9 @@ static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
                        if (is_indent == FALSE && !isupper(cbuf[0]))
                                goto out;
                        if (is_indent == TRUE) {
+                               if (alnum_cnt > 0 
+                                   && !strchr(prefs_common.quote_chars, cbuf[0]))
+                                       goto out;
                                alnum_cnt = 0;
                                state = WAIT_FOR_SPACE;
                        } else {
@@ -2627,7 +2676,7 @@ gboolean compose_check_for_valid_recipient(Compose *compose) {
        return recipient_found;
 }
 
-static gboolean compose_check_entries(Compose *compose)
+static gboolean compose_check_entries(Compose *compose, gboolean check_subject)
 {
        gchar *str;
 
@@ -2637,7 +2686,7 @@ static gboolean compose_check_entries(Compose *compose)
        }
 
        str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
-       if (*str == '\0') {
+       if (*str == '\0' && check_subject == TRUE) {
                AlertValue aval;
 
                aval = alertpanel(_("Send"),
@@ -2656,7 +2705,7 @@ gint compose_send(Compose *compose)
        FolderItem *folder;
        gint val;
 
-       if (compose_check_entries(compose) == FALSE)
+       if (compose_check_entries(compose, TRUE) == FALSE)
                return -1;
 
        val = compose_queue(compose, &msgnum, &folder);
@@ -2687,7 +2736,7 @@ gint compose_send(Compose *compose)
 
        lock = TRUE;
 
-       if (compose_check_entries(compose) == FALSE) {
+       if (compose_check_entries(compose, TRUE) == FALSE) {
                lock = FALSE;
                return 1;
        }
@@ -2786,7 +2835,7 @@ gint compose_send(Compose *compose)
                                (compose->account, F_OUTBOX);
                        if (procmsg_save_to_outbox(outbox, tmp, FALSE) < 0)
                                alertpanel_error
-                                       (_("Can't save the message to outbox."));
+                                       (_("Can't save the message to Sent."));
                        else
                                folderview_update_item(outbox, TRUE);
                }
@@ -3209,11 +3258,14 @@ static gint compose_remove_reedit_target(Compose *compose)
        return 0;
 }
 
-
 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
+{
+       return compose_queue_sub (compose, msgnum, item, FALSE);
+}
+static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item, gboolean check_subject)
 {
        FolderItem *queue;
-       gchar *tmp, *tmp2, *queue_path;
+       gchar *tmp, *tmp2;
        FILE *fp, *src_fp;
        GSList *cur;
        gchar buf[BUFFSIZE];
@@ -3228,7 +3280,7 @@ static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
 
         lock = TRUE;
        
-       if (compose_check_entries(compose) == FALSE) {
+       if (compose_check_entries(compose, check_subject) == FALSE) {
                 lock = FALSE;
                 return -1;
        }
@@ -3387,15 +3439,17 @@ static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
        }
 
        queue = account_get_special_folder(compose->account, F_QUEUE);
+       if (!queue) {
+               g_warning(_("can't find queue folder\n"));
+               unlink(tmp);
+               g_free(tmp);
+               return -1;
+       }
        folder_item_scan(queue);
-       queue_path = folder_item_get_path(queue);
-       if (!is_dir_exist(queue_path))
-               make_dir_hier(queue_path);
        if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
                g_warning(_("can't queue the message\n"));
                unlink(tmp);
                g_free(tmp);
-               g_free(queue_path);
                return -1;
        }
        unlink(tmp);
@@ -3495,6 +3549,36 @@ static void compose_write_attach(Compose *compose, FILE *fp)
        fprintf(fp, "\n--%s--\n", compose->boundary);
 }
 
+#define QUOTE_IF_REQUIRED(out, str)                    \
+{                                                      \
+       if (*str != '"' && strchr(str, ',')) {          \
+               gchar *__tmp;                           \
+               gint len;                               \
+                                                       \
+               len = strlen(str) + 3;                  \
+               Xalloca(__tmp, len, return -1);         \
+               g_snprintf(__tmp, len, "\"%s\"", str);  \
+               out = __tmp;                            \
+       } else {                                        \
+               Xstrdup_a(out, str, return -1);         \
+       }                                               \
+}
+
+#define PUT_RECIPIENT_HEADER(header, str)                                   \
+{                                                                           \
+       if (*str != '\0') {                                                  \
+               Xstrdup_a(str, str, return -1);                              \
+               g_strstrip(str);                                             \
+               if (*str != '\0') {                                          \
+                       compose->to_list = address_list_append               \
+                               (compose->to_list, str);                     \
+                       compose_convert_header                               \
+                               (buf, sizeof(buf), str, strlen(header) + 2); \
+                       fprintf(fp, "%s: %s\n", header, buf);                \
+               }                                                            \
+       }                                                                    \
+}
+
 #define IS_IN_CUSTOM_HEADER(header) \
        (compose->account->add_customhdr && \
         custom_header_find(compose->account->customhdr_list, header) != NULL)
@@ -3557,6 +3641,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
 {
        gchar buf[BUFFSIZE];
        gchar *str;
+       gchar *name;
        /* struct utsname utsbuf; */
 
        g_return_val_if_fail(fp != NULL, -1);
@@ -3576,8 +3661,9 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                        compose_convert_header
                                (buf, sizeof(buf), compose->account->name,
                                 strlen("From: "));
+                       QUOTE_IF_REQUIRED(name, buf);
                        fprintf(fp, "From: %s <%s>\n",
-                               buf, compose->account->address);
+                               name, compose->account->address);
                } else
                        fprintf(fp, "From: %s\n", compose->account->address);
        }
@@ -3587,20 +3673,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_to) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
-               if (*str != '\0') {
-                       Xstrdup_a(str, str, return -1);
-                       g_strstrip(str);
-                       if (*str != '\0') {
-                               compose->to_list = address_list_append
-                                       (compose->to_list, str);
-                               if (!IS_IN_CUSTOM_HEADER("To")) {
-                                       compose_convert_header
-                                               (buf, sizeof(buf), str,
-                                                strlen("To: "));
-                                       fprintf(fp, "To: %s\n", buf);
-                               }
-                       }
-               }
+               PUT_RECIPIENT_HEADER("To", str);
        }
 #endif
 
@@ -3616,11 +3689,9 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                        compose->newsgroup_list =
                                newsgroup_list_append(compose->newsgroup_list,
                                                      str);
-                       if (!IS_IN_CUSTOM_HEADER("Newsgroups")) {
-                               compose_convert_header(buf, sizeof(buf), str,
-                                                      strlen("Newsgroups: "));
-                               fprintf(fp, "Newsgroups: %s\n", buf);
-                       }
+                       compose_convert_header(buf, sizeof(buf), str,
+                                              strlen("Newsgroups: "));
+                       fprintf(fp, "Newsgroups: %s\n", buf);
                }
        }
 #endif
@@ -3629,20 +3700,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_cc) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
-               if (*str != '\0') {
-                       Xstrdup_a(str, str, return -1);
-                       g_strstrip(str);
-                       if (*str != '\0') {
-                               compose->to_list = address_list_append
-                                       (compose->to_list, str);
-                               if (!IS_IN_CUSTOM_HEADER("Cc")) {
-                                       compose_convert_header
-                                               (buf, sizeof(buf), str,
-                                                strlen("Cc: "));
-                                       fprintf(fp, "Cc: %s\n", buf);
-                               }
-                       }
-               }
+               PUT_RECIPIENT_HEADER("Cc", str);
        }
 #endif
        /* Bcc */
@@ -3650,17 +3708,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_bcc) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
-               if (*str != '\0') {
-                       Xstrdup_a(str, str, return -1);
-                       g_strstrip(str);
-                       if (*str != '\0') {
-                               compose->to_list = address_list_append
-                                       (compose->to_list, str);
-                               compose_convert_header(buf, sizeof(buf), str,
-                                                      strlen("Bcc: "));
-                               fprintf(fp, "Bcc: %s\n", buf);
-                       }
-               }
+               PUT_RECIPIENT_HEADER("Bcc", str);
        }
 #endif
 
@@ -3795,6 +3843,25 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                        procmime_get_encoding_str(encoding));
        }
 
+       /* PRIORITY */
+       switch (compose->priority) {
+               case PRIORITY_HIGHEST: fprintf(fp, "Importance: high\n"
+                                                  "X-Priority: 1 (Highest)\n");
+                       break;
+               case PRIORITY_HIGH: fprintf(fp, "Importance: high\n"
+                                               "X-Priority: 2 (High)\n");
+                       break;
+               case PRIORITY_NORMAL: break;
+               case PRIORITY_LOW: fprintf(fp, "Importance: low\n"
+                                              "X-Priority: 4 (Low)\n");
+                       break;
+               case PRIORITY_LOWEST: fprintf(fp, "Importance: low\n"
+                                                 "X-Priority: 5 (Lowest)\n");
+                       break;
+               default: debug_print(_("compose: priority unknown : %d\n"),
+                                    compose->priority);
+       }
+
        /* Request Return Receipt */
        if (!IS_IN_CUSTOM_HEADER("Disposition-Notification-To")) {
                if (compose->return_receipt) {
@@ -4725,6 +4792,10 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        if (!prefs_common.show_ruler)
                gtk_widget_hide(ruler_hbox);
 
+       /* Priority */
+       compose->priority = PRIORITY_NORMAL;
+       compose_update_priority_menu_item(compose);
+
        select_account(compose, account);
        set_toolbar_style(compose);
 
@@ -4885,6 +4956,46 @@ static GtkWidget *compose_account_option_menu_create(Compose *compose)
        return hbox;
 }
 
+static void compose_set_priority_cb(gpointer data,
+                                   guint action,
+                                   GtkWidget *widget)
+{
+       Compose *compose = (Compose *) data;
+       compose->priority = action;
+}
+
+static void compose_update_priority_menu_item(Compose * compose)
+{
+       GtkItemFactory *ifactory;
+       GtkWidget *menuitem;
+
+       ifactory = gtk_item_factory_from_widget(compose->menubar);
+       
+       switch (compose->priority) {
+               case PRIORITY_HIGHEST:
+                       menuitem = gtk_item_factory_get_item
+                               (ifactory, "/Message/Priority/Highest");
+                       break;
+               case PRIORITY_HIGH:
+                       menuitem = gtk_item_factory_get_item
+                               (ifactory, "/Message/Priority/High");
+                       break;
+               case PRIORITY_NORMAL:
+                       menuitem = gtk_item_factory_get_item
+                               (ifactory, "/Message/Priority/Normal");
+                       break;
+               case PRIORITY_LOW:
+                       menuitem = gtk_item_factory_get_item
+                               (ifactory, "/Message/Priority/Low");
+                       break;
+               case PRIORITY_LOWEST:
+                       menuitem = gtk_item_factory_get_item
+                               (ifactory, "/Message/Priority/Lowest");
+                       break;
+       }
+       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
+}      
 static void compose_set_template_menu(Compose *compose)
 {
        GSList *tmpl_list, *cur;
@@ -5937,7 +6048,7 @@ static void compose_send_later_cb(gpointer data, guint action,
        Compose *compose = (Compose *)data;
        gint val;
 
-       val = compose_queue(compose, NULL, NULL);
+       val = compose_queue_sub(compose, NULL, NULL, TRUE);
        if (!val) gtk_widget_destroy(compose->window);
 }