Fixes NB76286
authorFelipe Erias Morandeira <femorandeira@igalia.com>
Tue, 20 Nov 2007 17:35:25 +0000 (17:35 +0000)
committerFelipe Erias Morandeira <femorandeira@igalia.com>
Tue, 20 Nov 2007 17:35:25 +0000 (17:35 +0000)
If the attachment has already been downloaded, it uses the existing file and doesn't try to create it again. This prevents a problem that happened when the user tried to open an attachment for the second time, but the existing file was read-only and couldn't be overwritten.

pmo-trunk-r3785

src/maemo/modest-maemo-utils.c
src/maemo/modest-maemo-utils.h
src/maemo/modest-msg-view-window.c

index 47d980e..a28a8d9 100644 (file)
@@ -299,15 +299,26 @@ modest_maemo_utils_create_temp_stream (const gchar *orig_name, const gchar *hash
                return NULL;
        }
 
-       /* try to write the file there */
        filepath = g_strconcat (tmpdir, "/", orig_name, NULL);
-       fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
-       if (fd == -1) {
-               g_warning ("%s: failed to create '%s': %s",
-                          __FUNCTION__, filepath, g_strerror(errno));
+       /* don't overwrite if it already exists, even if it is writable */
+       if (modest_maemo_utils_file_exists (filepath)) {
+               if (path!=NULL) {
+                       *path = filepath;
+               } else {
+                       g_free (filepath);
+               }
                g_free (tmpdir);
-               g_free (filepath);
                return NULL;
+       } else {
+               /* try to write the file there */
+               fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+               if (fd == -1) {
+                       g_warning ("%s: failed to create '%s': %s",
+                                       __FUNCTION__, filepath, g_strerror(errno));                     
+                       g_free (filepath);
+                       g_free (tmpdir);
+                       return NULL;
+               }
        }
 
        g_free (tmpdir);
index f2661cb..a574aa3 100644 (file)
@@ -94,11 +94,13 @@ gboolean modest_maemo_utils_file_exists (const gchar *filename);
  * @orig_name: a string with the original name of the extension, or %NULL
  * @hash_base: if %NULL, subdir will be random. If not, it will be a hash
  * of this.
- * @path: a string with the created file path
+ * @path: a string with the created file path. 
  *
  * Creates a temporary fs stream, in a random subdir of /tmp or /var/tmp.
  *
- * Returns: a #TnyFsStream, or %NULL if operation failed.
+ * Returns: a #TnyFsStream, or %NULL if operation failed.  Note that it is 
+ * possible that the file already exists but it is not writable. In that case,
+ * the function would return NULL and @path would contain its path.
  */
 TnyFsStream *modest_maemo_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base, gchar **path);
 
index 2c669d9..aa61dd4 100644 (file)
@@ -2239,12 +2239,12 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart
        if (!TNY_IS_MSG (mime_part)) {
                gchar *filepath = NULL;
                const gchar *att_filename = tny_mime_part_get_filename (mime_part);
+               const gchar *content_type;
                TnyFsStream *temp_stream = NULL;
                temp_stream = modest_maemo_utils_create_temp_stream (att_filename, attachment_uid,
                                                                     &filepath);
                
-               if (temp_stream) {
-                       const gchar *content_type;
+               if (temp_stream != NULL) {
                        content_type = tny_mime_part_get_content_type (mime_part);
                        tny_mime_part_decode_to_stream (mime_part, TNY_STREAM (temp_stream));
 
@@ -2258,6 +2258,12 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart
                        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 {
                /* message attachment */