toolbar reply refactoring: part 2
[claws.git] / src / compose.c
index 0396262bfe50750fd8e7f3decdbb60dd281d0b26..743b47ca33a6e2ad3030bd49fc4b892fa421c5dc 100644 (file)
@@ -199,6 +199,8 @@ static void compose_reedit_set_entry                (Compose        *compose,
 static void compose_insert_sig                 (Compose        *compose);
 static void compose_insert_file                        (Compose        *compose,
                                                 const gchar    *file);
+static void compose_insert_command_output      (Compose        *compose,
+                                                const gchar    *cmdline);
 static void compose_attach_append              (Compose        *compose,
                                                 const gchar    *file,
                                                 const gchar    *type,
@@ -785,6 +787,93 @@ Compose *compose_new_followup_and_replyto(PrefsAccount *account,
 }
 */
 
+void compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
+{
+       MsgInfo *msginfo;
+       guint list_len;
+
+       g_return_if_fail(msginfo_list != NULL);
+
+       msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
+       g_return_if_fail(msginfo != NULL);
+
+       list_len = g_slist_length(msginfo_list);
+
+       switch (mode) {
+       case COMPOSE_REPLY:
+               compose_reply(msginfo, prefs_common.reply_with_quote,
+                             FALSE, prefs_common.default_reply_list, FALSE, body);
+               break;
+       case COMPOSE_REPLY_WITH_QUOTE:
+               compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, body);
+               break;
+       case COMPOSE_REPLY_WITHOUT_QUOTE:
+               compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
+               break;
+       case COMPOSE_REPLY_TO_SENDER:
+               compose_reply(msginfo, prefs_common.reply_with_quote,
+                             FALSE, FALSE, TRUE, body);
+               break;
+       case COMPOSE_FOLLOWUP_AND_REPLY_TO:
+               compose_followup_and_reply_to(msginfo,
+                                             prefs_common.reply_with_quote,
+                                             FALSE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
+               compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, body);
+               break;
+       case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
+               compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
+               break;
+       case COMPOSE_REPLY_TO_ALL:
+               compose_reply(msginfo, prefs_common.reply_with_quote,
+                             TRUE, FALSE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
+               compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
+               compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
+               break;
+       case COMPOSE_REPLY_TO_LIST:
+               compose_reply(msginfo, prefs_common.reply_with_quote,
+                             FALSE, TRUE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
+               compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
+               compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
+               break;
+       case COMPOSE_FORWARD:
+               if (prefs_common.forward_as_attachment) {
+                       compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
+                       return;
+               } else {
+                       compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
+                       return;
+               }
+               break;
+       case COMPOSE_FORWARD_INLINE:
+               /* check if we reply to more than one Message */
+               if (list_len == 1) {
+                       compose_forward(NULL, msginfo, FALSE, body);
+                       break;
+               } 
+               /* more messages FALL THROUGH */
+       case COMPOSE_FORWARD_AS_ATTACH:
+               compose_forward_multiple(NULL, msginfo_list);
+               break;
+       case COMPOSE_REDIRECT:
+               compose_redirect(NULL, msginfo);
+               break;
+       default:
+               g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
+       }
+       
+       //summary_set_marks_selected(summaryview);
+}
+
 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
                   gboolean to_ml, gboolean to_sender, 
                   const gchar *body)
@@ -1681,10 +1770,6 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
                                 compose->newsgroups ? compose->newsgroups : "",
                                 COMPOSE_NEWSGROUPS);
                } 
-               else if (compose->replyto) 
-                       compose_entry_append
-                               (compose,compose->replyto,
-                                COMPOSE_TO);                                           
                else 
                        compose_entry_append
                                (compose,
@@ -1791,59 +1876,28 @@ static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
 #undef SET_ENTRY
 #undef SET_ADDRESS
 
-static void compose_exec_sig(Compose *compose, gchar *sigfile)
+static void compose_insert_sig(Compose *compose)
 {
-       FILE  *sigprg;
-       gchar  *buf;
-       size_t buf_len = 128;
-       if (strlen(sigfile) < 2)
-         return;
-       sigprg = popen(sigfile+1, "r");
-       if (sigprg) {
-
-               buf = g_malloc(buf_len);
+       static gchar *default_sigfile;
+       gchar *sigfile = NULL;
 
-               if (!buf) {
-                       gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
-                       "Unable to insert signature (malloc failed)\n", -1);
+       g_return_if_fail(compose->account != NULL);
 
-                       pclose(sigprg);
-                       return;
+       if (compose->account->sig_type == SIG_FILE) {
+               if (compose->account->sig_path)
+                       sigfile = compose->account->sig_path;
+               else {
+                       if (!default_sigfile)
+                               default_sigfile = g_strconcat
+                                       (get_home_dir(), G_DIR_SEPARATOR_S,
+                                        DEFAULT_SIGNATURE, NULL);
+                       sigfile = default_sigfile;
                }
 
-               while (!feof(sigprg)) {
-                       bzero(buf, buf_len);
-                       fread(buf, buf_len-1, 1, sigprg);
-                       gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
+               if (!is_file_or_fifo_exist(sigfile)) {
+                       g_warning("can't open signature file: %s\n", sigfile);
+                       return;
                }
-
-               g_free(buf);
-               pclose(sigprg);
-       }
-       else
-       {
-               gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
-               "Can't exec file: ", -1);
-               gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
-               sigfile+1, -1);
-       }
-}
-
-static void compose_insert_sig(Compose *compose)
-{
-       gchar *sigfile;
-
-       if (compose->account && compose->account->sig_path)
-               sigfile = g_strdup(compose->account->sig_path);
-       else
-               sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
-                                     DEFAULT_SIGNATURE, NULL);
-
-       if (!is_file_or_fifo_exist(sigfile) && sigfile[0] != '|') {
-               g_free(sigfile);
-               return;
        }
 
        gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
@@ -1854,11 +1908,12 @@ static void compose_insert_sig(Compose *compose)
                                "\n", 1);
        }
 
-       if (sigfile[0] == '|')
-               compose_exec_sig(compose, sigfile);
-       else
+       if (compose->account->sig_type == SIG_COMMAND) {
+               if (compose->account->sig_path)
+                       compose_insert_command_output
+                               (compose, compose->account->sig_path);
+       } else
                compose_insert_file(compose, sigfile);
-       g_free(sigfile);
 }
 
 static void compose_insert_file(Compose *compose, const gchar *file)
@@ -1894,6 +1949,38 @@ static void compose_insert_file(Compose *compose, const gchar *file)
        fclose(fp);
 }
 
+static void compose_insert_command_output(Compose *compose,
+                                         const gchar *cmdline)
+{
+       GtkSText *text = GTK_STEXT(compose->text);
+       gchar buf[BUFFSIZE];
+       gint len;
+       FILE *fp;
+
+       g_return_if_fail(cmdline != NULL);
+
+       if ((fp = popen(cmdline, "r")) == NULL) {
+               FILE_OP_ERROR(cmdline, "popen");
+               return;
+       }
+
+       gtk_stext_freeze(text);
+
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               strcrchomp(buf);
+               len = strlen(buf);
+               if (len > 0 && buf[len - 1] != '\n') {
+                       while (--len >= 0)
+                               if (buf[len] == '\r') buf[len] = '\n';
+               }
+               gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
+       }
+
+       gtk_stext_thaw(text);
+
+       pclose(fp);
+}
+
 static void compose_attach_append(Compose *compose, const gchar *file,
                                  const gchar *filename,
                                  const gchar *content_type)
@@ -3737,9 +3824,6 @@ static void compose_write_attach(Compose *compose, FILE *fp)
 
        for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
             row++) {
-               gchar buf[BUFFSIZE];
-               gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
-
                if ((attach_fp = fopen(ainfo->file, "rb")) == NULL) {
                        g_warning("Can't open file %s\n", ainfo->file);
                        continue;