+ ModestMsgViewWindowPrivate *priv;
+ const gchar *msg_uid;
+ gchar *attachment_uid = NULL;
+ gint attachment_index = 0;
+ TnyList *attachments;
+
+ g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
+ g_return_if_fail (TNY_IS_MIME_PART (mime_part) || (mime_part == NULL));
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+
+ msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (window));
+ attachments = modest_msg_view_get_attachments (MODEST_MSG_VIEW (priv->msg_view));
+ attachment_index = modest_list_index (attachments, (GObject *) mime_part);
+ g_object_unref (attachments);
+
+ if (msg_uid && attachment_index >= 0) {
+ attachment_uid = g_strdup_printf ("%s/%d", msg_uid, attachment_index);
+ }
+
+ if (mime_part == NULL) {
+ gboolean error = FALSE;
+ TnyList *selected_attachments = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
+ if (selected_attachments == NULL || tny_list_get_length (selected_attachments) == 0) {
+ error = TRUE;
+ } else if (tny_list_get_length (selected_attachments) > 1) {
+ hildon_banner_show_information (NULL, NULL, _("mcen_ib_unable_to_display_more"));
+ error = TRUE;
+ } else {
+ TnyIterator *iter;
+ iter = tny_list_create_iterator (selected_attachments);
+ mime_part = (TnyMimePart *) tny_iterator_get_current (iter);
+ g_object_unref (iter);
+ }
+ g_object_unref (selected_attachments);
+
+ if (error)
+ return;
+ } else {
+ g_object_ref (mime_part);
+ }
+
+ if (tny_mime_part_is_purged (mime_part)) {
+ g_object_unref (mime_part);
+ return;
+ }
+
+ if (!TNY_IS_MSG (mime_part)) {
+ gchar *filepath = NULL;
+ const gchar *att_filename = tny_mime_part_get_filename (mime_part);
+ const gchar *content_type;
+ gboolean show_error_banner = FALSE;
+ GError *err;
+ TnyFsStream *temp_stream = NULL;
+ temp_stream = modest_utils_create_temp_stream (att_filename, attachment_uid,
+ &filepath);
+
+ if (temp_stream != NULL) {
+ content_type = tny_mime_part_get_content_type (mime_part);
+ if (tny_mime_part_decode_to_stream (mime_part, TNY_STREAM (temp_stream), &err) >= 0) {
+ /* make the file read-only */
+ if (g_chmod(filepath, 0444) != 0)
+ g_warning ("%s: failed to set file '%s' to read-only: %s",
+ __FUNCTION__, filepath, strerror(errno));
+
+ modest_platform_activate_file (filepath, content_type);
+ } else {
+ /* error while saving attachment, maybe cerm_device_memory_full */
+ show_error_banner = TRUE;
+ if (err != NULL) {
+ g_warning ("%s: tny_mime_part_decode_to_stream failed (%s)", __FUNCTION__, err->message);
+ g_error_free (err);
+ }
+ }
+ g_object_unref (temp_stream);
+ g_free (filepath);
+ /* NOTE: files in the temporary area will be automatically
+ * cleaned after some time if they are no longer in use */
+ } else {
+ if (filepath != NULL) {
+ /* the file may already exist but it isn't writable,
+ * let's try to open it anyway */
+ content_type = tny_mime_part_get_content_type (mime_part);
+ modest_platform_activate_file (filepath, content_type);
+ g_free (filepath);
+ } else {
+ g_warning ("%s: modest_utils_create_temp_stream failed", __FUNCTION__);
+ show_error_banner = TRUE;
+ }
+ }
+ if (show_error_banner)
+ modest_platform_information_banner (NULL, NULL, _("mail_ib_file_operation_failed"));
+ } else {
+ /* message attachment */
+ TnyHeader *header = NULL;
+ ModestWindowMgr *mgr;
+ ModestWindow *msg_win = NULL;
+ gboolean found;
+
+ header = tny_msg_get_header (TNY_MSG (mime_part));
+ mgr = modest_runtime_get_window_mgr ();
+ found = modest_window_mgr_find_registered_header (mgr, header, &msg_win);
+
+ if (found) {
+ if (msg_win) /* there is already a window for this uid; top it */
+ gtk_window_present (GTK_WINDOW(msg_win));
+ else
+ /* if it's found, but there is no msg_win, it's probably in the process of being created;
+ * thus, we don't do anything */
+ g_warning ("window for is already being created");
+ } else {
+ /* it's not found, so create a new window for it */
+ modest_window_mgr_register_header (mgr, header, attachment_uid); /* register the uid before building the window */
+ gchar *account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (window)));
+ if (!account)
+ account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
+ msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (mime_part), account, attachment_uid);
+ modest_window_set_zoom (MODEST_WINDOW (msg_win),
+ modest_window_get_zoom (MODEST_WINDOW (window)));
+ modest_window_mgr_register_window (mgr, msg_win);
+ gtk_widget_show_all (GTK_WIDGET (msg_win));
+ }
+ }
+ g_object_unref (mime_part);
+}
+
+typedef struct
+{
+ gchar *filename;
+ TnyMimePart *part;
+} SaveMimePartPair;
+
+typedef struct
+{
+ GList *pairs;
+ GtkWidget *banner;
+ gboolean result;
+} SaveMimePartInfo;
+
+static void save_mime_part_info_free (SaveMimePartInfo *info, gboolean with_struct);
+static gboolean idle_save_mime_part_show_result (SaveMimePartInfo *info);
+static gpointer save_mime_part_to_file (SaveMimePartInfo *info);
+static void save_mime_parts_to_file_with_checks (SaveMimePartInfo *info);
+
+static void
+save_mime_part_info_free (SaveMimePartInfo *info, gboolean with_struct)
+{
+
+ GList *node;
+ for (node = info->pairs; node != NULL; node = g_list_next (node)) {
+ SaveMimePartPair *pair = (SaveMimePartPair *) node->data;
+ g_free (pair->filename);
+ g_object_unref (pair->part);
+ g_slice_free (SaveMimePartPair, pair);
+ }
+ g_list_free (info->pairs);
+ info->pairs = NULL;
+ if (with_struct) {
+ gtk_widget_destroy (info->banner);
+ g_object_unref (info->banner);
+ g_slice_free (SaveMimePartInfo, info);
+ }
+}
+
+static gboolean
+idle_save_mime_part_show_result (SaveMimePartInfo *info)
+{
+ if (info->pairs != NULL) {
+ save_mime_part_to_file (info);
+ } else {
+ gboolean result;
+ result = info->result;
+
+ /* This is a GDK lock because we are an idle callback and
+ * hildon_banner_show_information is or does Gtk+ code */
+
+ gdk_threads_enter (); /* CHECKED */
+ save_mime_part_info_free (info, TRUE);
+ if (result) {
+ hildon_banner_show_information (NULL, NULL, _CS("sfil_ib_saved"));
+ } else {
+ hildon_banner_show_information (NULL, NULL, _("mail_ib_file_operation_failed"));
+ }
+ gdk_threads_leave (); /* CHECKED */
+ }
+
+ return FALSE;
+}
+
+static gpointer
+save_mime_part_to_file (SaveMimePartInfo *info)
+{
+ GnomeVFSResult result;
+ GnomeVFSHandle *handle;
+ TnyStream *stream;
+ SaveMimePartPair *pair = (SaveMimePartPair *) info->pairs->data;
+ gboolean decode_result = TRUE;
+
+ result = gnome_vfs_create (&handle, pair->filename, GNOME_VFS_OPEN_WRITE, FALSE, 0644);
+ if (result == GNOME_VFS_OK) {
+ stream = tny_vfs_stream_new (handle);
+ if (tny_mime_part_decode_to_stream (pair->part, stream, NULL) < 0) {
+ decode_result = FALSE;
+ }
+ g_object_unref (G_OBJECT (stream));
+ g_object_unref (pair->part);
+ g_slice_free (SaveMimePartPair, pair);
+ info->pairs = g_list_delete_link (info->pairs, info->pairs);
+ info->result = decode_result;
+ } else {
+ save_mime_part_info_free (info, FALSE);
+ info->result = FALSE;
+ }
+
+ g_idle_add ((GSourceFunc) idle_save_mime_part_show_result, info);
+ return NULL;
+}
+
+static void
+save_mime_parts_to_file_with_checks (SaveMimePartInfo *info)
+{
+ gboolean is_ok = TRUE;
+ gint replaced_files = 0;
+ const GList *files = info->pairs;
+ const GList *iter;
+
+ for (iter = files; (iter != NULL) && (replaced_files < 2); iter = g_list_next(iter)) {
+ SaveMimePartPair *pair = iter->data;
+ if (modest_utils_file_exists (pair->filename)) {
+ replaced_files++;
+ }
+ }
+ if (replaced_files) {
+ GtkWidget *confirm_overwrite_dialog;
+ const gchar *message = (replaced_files == 1) ?
+ _FM("docm_nc_replace_file") : _FM("docm_nc_replace_multiple");
+ confirm_overwrite_dialog = hildon_note_new_confirmation (NULL, message);
+ if (gtk_dialog_run (GTK_DIALOG (confirm_overwrite_dialog)) != GTK_RESPONSE_OK) {
+ is_ok = FALSE;
+ }
+ gtk_widget_destroy (confirm_overwrite_dialog);
+ }
+
+ if (!is_ok) {
+ save_mime_part_info_free (info, TRUE);
+ } else {
+ GtkWidget *banner = hildon_banner_show_animation (NULL, NULL,
+ _CS("sfil_ib_saving"));
+ info->banner = g_object_ref (banner);
+ g_thread_create ((GThreadFunc)save_mime_part_to_file, info, FALSE, NULL);
+ g_object_unref (banner);
+ }
+
+}
+
+
+void
+modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, TnyList *mime_parts)
+{
+ ModestMsgViewWindowPrivate *priv;
+ GList *files_to_save = NULL;
+ GtkWidget *save_dialog = NULL;
+ gchar *folder = NULL;
+ gboolean canceled = FALSE;
+ const gchar *filename = NULL;
+ gchar *save_multiple_str = NULL;
+
+ g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+
+ if (mime_parts == NULL) {
+ mime_parts = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
+ if (mime_parts == NULL || tny_list_get_length (mime_parts) == 0)
+ return;
+ } else {
+ g_object_ref (mime_parts);
+ }
+
+ /* prepare dialog */
+ if (tny_list_get_length (mime_parts) == 1) {
+ TnyIterator *iter;
+ /* only one attachment selected */
+ iter = tny_list_create_iterator (mime_parts);
+ TnyMimePart *mime_part = (TnyMimePart *) tny_iterator_get_current (iter);
+ g_object_unref (iter);
+ if (!TNY_IS_MSG (mime_part) && tny_mime_part_is_attachment (mime_part)) {
+ filename = tny_mime_part_get_filename (mime_part);
+ } else {
+ g_warning ("Tried to save a non-file attachment");
+ canceled = TRUE;
+ }
+ g_object_unref (mime_part);
+ } else {
+ save_multiple_str = g_strdup_printf (_FM("sfil_va_number_of_objects_attachments"),
+ tny_list_get_length (mime_parts));
+ }
+
+ save_dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window),
+ GTK_FILE_CHOOSER_ACTION_SAVE);
+
+ /* set folder */
+ folder = g_build_filename (g_get_home_dir (), DEFAULT_FOLDER, NULL);
+ gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (save_dialog), folder);
+ g_free (folder);
+
+ /* set filename */
+ if (filename != NULL)
+ gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (save_dialog),
+ filename);
+
+ /* if multiple, set multiple string */
+ if (save_multiple_str) {
+ g_object_set (G_OBJECT (save_dialog), "save-multiple", save_multiple_str, NULL);
+ }
+
+ /* show dialog */
+ if (gtk_dialog_run (GTK_DIALOG (save_dialog)) == GTK_RESPONSE_OK) {
+ gchar *chooser_uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (save_dialog));
+
+ if (!modest_utils_folder_writable (chooser_uri)) {
+ hildon_banner_show_information
+ (NULL, NULL, dgettext("hildon-fm", "sfil_ib_readonly_location"));
+ } else {
+ TnyIterator *iter;
+
+ iter = tny_list_create_iterator (mime_parts);
+ while (!tny_iterator_is_done (iter)) {
+ TnyMimePart *mime_part = (TnyMimePart *) tny_iterator_get_current (iter);
+
+ tny_iterator_next (iter);
+ if (tny_mime_part_is_attachment (mime_part)) {
+ SaveMimePartPair *pair;
+
+ if ((!tny_iterator_is_done (iter)) &&
+ (tny_mime_part_get_filename (mime_part) == NULL)) {
+ g_object_unref (mime_part);
+ continue;
+ }
+
+ pair = g_slice_new0 (SaveMimePartPair);
+ if (tny_iterator_is_done (iter)) {
+ pair->filename = g_strdup (chooser_uri);
+ } else {
+ pair->filename =
+ g_build_filename (chooser_uri,
+ tny_mime_part_get_filename (mime_part), NULL);
+ }
+ pair->part = mime_part;
+ files_to_save = g_list_prepend (files_to_save, pair);
+ }
+ }
+ g_object_unref (iter);
+ }
+ g_free (chooser_uri);
+ }
+
+ gtk_widget_destroy (save_dialog);
+
+ g_object_unref (mime_parts);
+
+ if (files_to_save != NULL) {
+ SaveMimePartInfo *info = g_slice_new0 (SaveMimePartInfo);
+ info->pairs = files_to_save;
+ info->result = TRUE;
+ save_mime_parts_to_file_with_checks (info);
+ }
+}
+
+static gboolean
+show_remove_attachment_information (gpointer userdata)
+{
+ ModestMsgViewWindow *window = (ModestMsgViewWindow *) userdata;
+ ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+
+ /* We're outside the main lock */
+ gdk_threads_enter ();
+
+ if (priv->remove_attachment_banner != NULL) {
+ gtk_widget_destroy (priv->remove_attachment_banner);
+ g_object_unref (priv->remove_attachment_banner);
+ }
+
+ priv->remove_attachment_banner = g_object_ref (
+ hildon_banner_show_animation (NULL, NULL, _("mcen_ib_removing_attachment")));
+
+ gdk_threads_leave ();
+
+ return FALSE;
+}
+
+void
+modest_msg_view_window_remove_attachments (ModestMsgViewWindow *window, gboolean get_all)
+{
+ ModestMsgViewWindowPrivate *priv;
+ TnyList *mime_parts = NULL;
+ gchar *confirmation_message;
+ gint response;
+ gint n_attachments;
+ TnyMsg *msg;
+ TnyIterator *iter;
+/* TnyFolder *folder; */
+
+ g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+
+ if (get_all)
+ mime_parts = modest_msg_view_get_attachments (MODEST_MSG_VIEW (priv->msg_view));
+ else
+ mime_parts = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));