aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-ops.c
diff options
context:
space:
mode:
Diffstat (limited to 'mail/mail-ops.c')
-rw-r--r--mail/mail-ops.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/mail/mail-ops.c b/mail/mail-ops.c
index 5b60be09f8..49b36f3eee 100644
--- a/mail/mail-ops.c
+++ b/mail/mail-ops.c
@@ -2528,3 +2528,125 @@ mail_do_view_message_sources (CamelFolder *folder, GPtrArray *uids,
mail_operation_queue (&op_view_message_sources, input, TRUE);
}
+
+/* ** SAVE MESSAGES ******************************************************* */
+
+typedef struct save_messages_input_s {
+ CamelFolder *folder;
+ GPtrArray *uids;
+ gchar *path;
+} save_messages_input_t;
+
+typedef struct save_messages_data_s {
+ GPtrArray *messages;
+} save_messages_data_t;
+
+static gchar *
+describe_save_messages (gpointer in_data, gboolean gerund)
+{
+ save_messages_input_t *input = (save_messages_input_t *) in_data;
+
+ if (gerund)
+ return g_strdup_printf (_("Saving messages from folder \"%s\""),
+ mail_tool_get_folder_name (input->folder));
+ else
+ return g_strdup_printf (_("Saving messages from \"%s\""),
+ mail_tool_get_folder_name (input->folder));
+}
+
+static void
+setup_save_messages (gpointer in_data, gpointer op_data, CamelException *ex)
+{
+ save_messages_input_t *input = (save_messages_input_t *) in_data;
+
+ camel_object_ref (CAMEL_OBJECT (input->folder));
+}
+
+static void
+do_save_messages (gpointer in_data, gpointer op_data, CamelException *ex)
+{
+ save_messages_input_t *input = (save_messages_input_t *) in_data;
+ save_messages_data_t *data = (save_messages_data_t *) op_data;
+ int i;
+
+ data->messages = g_ptr_array_new ();
+
+ for (i = 0; i < input->uids->len; i++) {
+ CamelMimeMessage *message;
+
+ mail_op_set_message (_("Retrieving message %d of %d (uid \"%s\")"),
+ i + 1, input->uids->len, (char *)input->uids->pdata[i]);
+
+ mail_tool_camel_lock_up ();
+ message = camel_folder_get_message (input->folder, input->uids->pdata[i], ex);
+ mail_tool_camel_lock_down ();
+
+ g_ptr_array_add (data->messages, message);
+ }
+}
+
+static void
+cleanup_save_messages (gpointer in_data, gpointer op_data,
+ CamelException *ex)
+{
+ save_messages_input_t *input = (save_messages_input_t *) in_data;
+ save_messages_data_t *data = (save_messages_data_t *) op_data;
+ int i;
+
+ for (i = 0; i < data->messages->len; i++) {
+ CamelMimeMessage *msg;
+ CamelStream *stream;
+ char *filename;
+
+ if (data->messages->pdata[i] == NULL)
+ continue;
+
+ msg = data->messages->pdata[i];
+
+ /* if we are saving a single message, use the filename the user selected */
+ if (input->uids->len == 1)
+ filename = g_strdup (input->path);
+ else
+ filename = g_strdup_printf ("%s/[%s] %s.eml", input->path,
+ camel_mime_message_get_subject (msg),
+ (char *) input->uids->pdata[i]);
+
+ stream = camel_stream_fs_new_with_name (filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+ camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (msg), stream);
+ camel_stream_flush (stream);
+ g_free (filename);
+
+ camel_object_unref (CAMEL_OBJECT (stream));
+ camel_object_unref (CAMEL_OBJECT (data->messages->pdata[i]));
+ g_free (input->uids->pdata[i]);
+ }
+
+ g_ptr_array_free (input->uids, TRUE);
+ g_ptr_array_free (data->messages, TRUE);
+ camel_object_unref (CAMEL_OBJECT (input->folder));
+}
+
+static const mail_operation_spec op_save_messages = {
+ describe_save_messages,
+ sizeof (save_messages_data_t),
+ setup_save_messages,
+ do_save_messages,
+ cleanup_save_messages
+};
+
+void
+mail_do_save_messages (CamelFolder *folder, GPtrArray *uids, gchar *path)
+{
+ save_messages_input_t *input;
+
+ g_return_if_fail (CAMEL_IS_FOLDER (folder));
+ g_return_if_fail (uids != NULL);
+ g_return_if_fail (path != NULL);
+
+ input = g_new (save_messages_input_t, 1);
+ input->folder = folder;
+ input->uids = uids;
+ input->path = path;
+
+ mail_operation_queue (&op_save_messages, input, TRUE);
+}