+ modest_window_check_dimming_rules_group (MODEST_WINDOW (window), MODEST_DIMMING_RULES_CLIPBOARD);
+}
+
+gboolean
+modest_msg_view_window_transfer_mode_enabled (ModestMsgViewWindow *self)
+{
+ ModestMsgViewWindowPrivate *priv;
+
+ g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE);
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
+
+ return priv->current_toolbar_mode == TOOLBAR_MODE_TRANSFER;
+}
+
+static void
+cancel_progressbar (GtkToolButton *toolbutton,
+ ModestMsgViewWindow *self)
+{
+ GSList *tmp;
+ ModestMsgViewWindowPrivate *priv;
+
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
+
+ /* Get operation observers and cancel its current operation */
+ tmp = priv->progress_widgets;
+ while (tmp) {
+ modest_progress_object_cancel_current_operation (MODEST_PROGRESS_OBJECT(tmp->data));
+ tmp=g_slist_next(tmp);
+ }
+}
+static gboolean
+observers_empty (ModestMsgViewWindow *self)
+{
+ GSList *tmp = NULL;
+ ModestMsgViewWindowPrivate *priv;
+ gboolean is_empty = TRUE;
+ guint pending_ops = 0;
+
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
+ tmp = priv->progress_widgets;
+
+ /* Check all observers */
+ while (tmp && is_empty) {
+ pending_ops = modest_progress_object_num_pending_operations (MODEST_PROGRESS_OBJECT(tmp->data));
+ is_empty = pending_ops == 0;
+
+ tmp = g_slist_next(tmp);
+ }
+
+ return is_empty;
+}
+
+static void
+on_account_removed (TnyAccountStore *account_store,
+ TnyAccount *account,
+ gpointer user_data)
+{
+ /* Do nothing if it's a transport account, because we only
+ show the messages of a store account */
+ if (tny_account_get_account_type(account) == TNY_ACCOUNT_TYPE_STORE) {
+ const gchar *parent_acc = NULL;
+ const gchar *our_acc = NULL;
+
+ our_acc = modest_window_get_active_account (MODEST_WINDOW (user_data));
+ parent_acc = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
+
+ /* Close this window if I'm showing a message of the removed account */
+ if (strcmp (parent_acc, our_acc) == 0)
+ modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (user_data));
+ }
+}
+
+static void
+on_mail_operation_started (ModestMailOperation *mail_op,
+ gpointer user_data)
+{
+ ModestMsgViewWindow *self;
+ ModestMailOperationTypeOperation op_type;
+ GSList *tmp;
+ ModestMsgViewWindowPrivate *priv;
+ GObject *source = NULL;
+
+ self = MODEST_MSG_VIEW_WINDOW (user_data);
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+ op_type = modest_mail_operation_get_type_operation (mail_op);
+ tmp = priv->progress_widgets;
+ source = modest_mail_operation_get_source(mail_op);
+ if (G_OBJECT (self) == source) {
+ if (op_type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || op_type == MODEST_MAIL_OPERATION_TYPE_OPEN ) {
+ set_toolbar_transfer_mode(self);
+ while (tmp) {
+ modest_progress_object_add_operation (
+ MODEST_PROGRESS_OBJECT (tmp->data),
+ mail_op);
+ tmp = g_slist_next (tmp);
+ }
+ }
+ }
+ g_object_unref (source);
+}
+
+static void
+on_mail_operation_finished (ModestMailOperation *mail_op,
+ gpointer user_data)
+{
+ ModestMsgViewWindow *self;
+ ModestMailOperationTypeOperation op_type;
+ GSList *tmp;
+ ModestMsgViewWindowPrivate *priv;
+
+ self = MODEST_MSG_VIEW_WINDOW (user_data);
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+ op_type = modest_mail_operation_get_type_operation (mail_op);
+ tmp = priv->progress_widgets;
+
+ if (op_type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || op_type == MODEST_MAIL_OPERATION_TYPE_OPEN ) {
+ while (tmp) {
+ modest_progress_object_remove_operation (
+ MODEST_PROGRESS_OBJECT (tmp->data),
+ mail_op);
+ tmp = g_slist_next (tmp);
+ }
+
+ /* If no more operations are being observed, NORMAL mode is enabled again */
+ if (observers_empty (self)) {
+ set_toolbar_mode (self, TOOLBAR_MODE_NORMAL);
+ }
+ }
+}
+
+static void
+on_queue_changed (ModestMailOperationQueue *queue,
+ ModestMailOperation *mail_op,
+ ModestMailOperationQueueNotification type,
+ ModestMsgViewWindow *self)
+{
+ ModestMsgViewWindowPrivate *priv;
+
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+
+ /* If this operations was created by another window, do nothing */
+ if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self)))
+ return;
+
+ if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
+ priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (mail_op),
+ "operation-started",
+ G_CALLBACK (on_mail_operation_started),
+ self);
+ priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+ G_OBJECT (mail_op),
+ "operation-finished",
+ G_CALLBACK (on_mail_operation_finished),
+ self);
+ } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
+ priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
+ G_OBJECT (mail_op),
+ "operation-started");
+ priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
+ G_OBJECT (mail_op),
+ "operation-finished");
+ }
+}
+
+TnyList *
+modest_msg_view_window_get_attachments (ModestMsgViewWindow *win)
+{
+ ModestMsgViewWindowPrivate *priv;
+ TnyList *selected_attachments = NULL;
+
+ g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), NULL);
+ priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (win);
+
+ selected_attachments = modest_msg_view_get_selected_attachments (MODEST_MSG_VIEW (priv->msg_view));
+
+ return selected_attachments;
+}
+
+void
+modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart *mime_part)
+{
+ 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);