Work to improve mime type detection in attachment view
authorJose Dapena Paz <jdapena@igalia.com>
Tue, 5 Jun 2007 10:14:07 +0000 (10:14 +0000)
committerJose Dapena Paz <jdapena@igalia.com>
Tue, 5 Jun 2007 10:14:07 +0000 (10:14 +0000)
(fixes NB#57213, NB#57671, NB#57575).
* src/modest-platform.h:
        * (modest_platform_activate_file): now it gets a mime_type parameter,
          in case it was detected previously.
* src/gnome/modest-platform.c:
        * (modest_platform_activate_file): updated API to get mime type
* src/maemo/modest-platform.c:
        * (guess_mime_type_from_file): fixed a trivial bug, that broke the
          detection.
        * (modest_platform_activate_file): use mime type to open the file
          if available, or uses guess_mime_type if no type is provided.
* src/maemo/modest-msg-view-window.c:
        * (..._view_attachment). Preserve the extension on saving the
          attachments to temporary file to view it.
        * (..._view_attachment). Get the mime type of the mime part, and
          pass it to the activate_file call.
* src/maemo/modest-maemo-utils.c:
        * (..._create_temp_stream): now accepts a preferred extension for
          the temporary file.

pmo-trunk-r2067

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

index 5e78371..036829a 100644 (file)
@@ -90,7 +90,7 @@ modest_platform_activate_uri (const gchar *uri)
 }
 
 gboolean 
 }
 
 gboolean 
-modest_platform_activate_file (const gchar *path)
+modest_platform_activate_file (const gchar *path, const gchar *mime_type)
 {
        modest_runtime_not_implemented (NULL);
        return FALSE;
 {
        modest_runtime_not_implemented (NULL);
        return FALSE;
index d085049..7754685 100644 (file)
@@ -229,13 +229,18 @@ modest_maemo_utils_file_exists (const gchar *filename)
 }
 
 TnyFsStream *
 }
 
 TnyFsStream *
-modest_maemo_utils_create_temp_stream (gchar **path)
+modest_maemo_utils_create_temp_stream (const gchar *extension, gchar **path)
 {
 {
-       TnyStream *tmp_fs_stream;
+       TnyStream *tmp_fs_stream = NULL;
        gint fd;
        gint fd;
-       gchar *filepath;
+       gchar *filepath = NULL;
+       gchar *template = NULL;
 
 
-       fd = g_file_open_tmp (NULL, &filepath, NULL);
+       if (extension != NULL)
+               template = g_strdup_printf ("XXXXXX.%s", extension);
+
+       fd = g_file_open_tmp (template, &filepath, NULL);
+       g_free (template);
        if (path != NULL)
                *path = filepath;
        if (fd == -1) {
        if (path != NULL)
                *path = filepath;
        if (fd == -1) {
index 9f1735f..186c7fe 100644 (file)
@@ -78,13 +78,14 @@ gboolean modest_maemo_utils_file_exists (const gchar *filename);
 
 /**
  * modest_maemo_utils_create_temp_stream:
 
 /**
  * modest_maemo_utils_create_temp_stream:
+ * @extension: a string with the extension the file should get, or %NULL
  * @path: a string with the created file path
  *
  * Creates a temporary fs stream 
  *
  * Returns: a #TnyFsStream, or %NULL if operation failed.
  */
  * @path: a string with the created file path
  *
  * Creates a temporary fs stream 
  *
  * Returns: a #TnyFsStream, or %NULL if operation failed.
  */
-TnyFsStream *modest_maemo_utils_create_temp_stream (gchar **path);
+TnyFsStream *modest_maemo_utils_create_temp_stream (const gchar *extension, gchar **path);
 
 /**
  * modest_protocol_info_protocol_is_local_store:
 
 /**
  * modest_protocol_info_protocol_is_local_store:
index 949cba0..35b6bbb 100644 (file)
@@ -1531,11 +1531,24 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart
 
        if (!TNY_IS_MSG (mime_part)) {
                gchar *filepath = NULL;
 
        if (!TNY_IS_MSG (mime_part)) {
                gchar *filepath = NULL;
-               TnyFsStream *temp_stream = modest_maemo_utils_create_temp_stream (&filepath);
+               const gchar *att_filename = tny_mime_part_get_filename (mime_part);
+               gchar *extension = NULL;
+               TnyFsStream *temp_stream = NULL;
+
+               if (att_filename) {
+                       extension = g_strrstr (att_filename, ".");
+                       if (extension != NULL)
+                               extension++;
+               }
+
+               temp_stream = modest_maemo_utils_create_temp_stream (extension, &filepath);
 
                if (temp_stream) {
 
                if (temp_stream) {
+                       const gchar *content_type;
+                       content_type = tny_mime_part_get_content_type (mime_part);
                        tny_mime_part_decode_to_stream (mime_part, TNY_STREAM (temp_stream));
                        tny_mime_part_decode_to_stream (mime_part, TNY_STREAM (temp_stream));
-                       modest_platform_activate_file (filepath);
+                       
+                       modest_platform_activate_file (filepath, content_type);
                        g_object_unref (temp_stream);
                        g_free (filepath);
                        /* TODO: delete temporary file */
                        g_object_unref (temp_stream);
                        g_free (filepath);
                        /* TODO: delete temporary file */
index 4a6a6a3..455e12b 100644 (file)
@@ -171,7 +171,7 @@ guess_mime_type_from_name (const gchar* name)
                return octet_stream;
        
        for (i = 0; i != G_N_ELEMENTS(mime_map); ++i) {
                return octet_stream;
        
        for (i = 0; i != G_N_ELEMENTS(mime_map); ++i) {
-               if (g_ascii_strcasecmp (mime_map[i][0], ext + 1)) /* +1: ignore '.'*/
+               if (!g_ascii_strcasecmp (mime_map[i][0], ext + 1)) /* +1: ignore '.'*/
                        return mime_map[i][1];
        }
        return octet_stream;
                        return mime_map[i][1];
        }
        return octet_stream;
@@ -189,7 +189,7 @@ modest_platform_get_file_icon_name (const gchar* name, const gchar* mime_type,
        
        g_return_val_if_fail (name || mime_type, NULL);
 
        
        g_return_val_if_fail (name || mime_type, NULL);
 
-       if (!mime_type || g_ascii_strcasecmp (mime_type, "application/octet-stream")) 
+       if (!mime_type || !g_ascii_strcasecmp (mime_type, "application/octet-stream")) 
                mime_str = g_string_new (guess_mime_type_from_name(name));
        else {
                mime_str = g_string_new (mime_type);
                mime_str = g_string_new (guess_mime_type_from_name(name));
        else {
                mime_str = g_string_new (mime_type);
@@ -233,23 +233,33 @@ modest_platform_activate_uri (const gchar *uri)
 }
 
 gboolean 
 }
 
 gboolean 
-modest_platform_activate_file (const gchar *path)
+modest_platform_activate_file (const gchar *path, const gchar *mime_type)
 {
        gint result;
        DBusConnection *con;
        gchar *uri_path = NULL;
 {
        gint result;
        DBusConnection *con;
        gchar *uri_path = NULL;
+       GString *mime_str = NULL;
+
+       if (!mime_type || !g_ascii_strcasecmp (mime_type, "application/octet-stream")) 
+               mime_str = g_string_new (guess_mime_type_from_name(path));
+       else {
+               mime_str = g_string_new (mime_type);
+               g_string_ascii_down (mime_str);
+       }
 
        uri_path = g_strconcat ("file://", path, NULL);
        
        con = osso_get_dbus_connection (osso_context);
 #ifdef MODEST_HILDON_VERSION_0
 
        uri_path = g_strconcat ("file://", path, NULL);
        
        con = osso_get_dbus_connection (osso_context);
 #ifdef MODEST_HILDON_VERSION_0
-       result = osso_mime_open_file (con, uri_path);
+       result = osso_mime_open_file_with_mime_type (con, uri_path, mime_str->str);
+       g_string_free (mime_str, TRUE);
 
        if (result != 1)
                hildon_banner_show_information (NULL, NULL, _("mcen_ni_noregistered_viewer"));
        return result != 1;
 #else
 
        if (result != 1)
                hildon_banner_show_information (NULL, NULL, _("mcen_ni_noregistered_viewer"));
        return result != 1;
 #else
-       result = hildon_mime_open_file (con, uri_path);
+       result = hildon_mime_open_file_with_mime_type (con, uri_path, mime_str->str);
+       g_string_free (mime_str, TRUE);
 
        if (result != 1)
                hildon_banner_show_information (NULL, NULL, _("mcen_ni_noregistered_viewer"));
 
        if (result != 1)
                hildon_banner_show_information (NULL, NULL, _("mcen_ni_noregistered_viewer"));
index 8eb4adc..c0a1922 100644 (file)
@@ -95,12 +95,13 @@ gboolean modest_platform_activate_uri (const gchar *uri);
 /**
  * modest_platform_activate_file:
  * @path: the path to activate
 /**
  * modest_platform_activate_file:
  * @path: the path to activate
+ * @mime_type: the mime type of the path, or %NULL to guess
  *
  * This function activates a file
  *
  * Returns: %TRUE if successful, %FALSE if not.
  **/
  *
  * This function activates a file
  *
  * Returns: %TRUE if successful, %FALSE if not.
  **/
-gboolean modest_platform_activate_file (const gchar *path);
+gboolean modest_platform_activate_file (const gchar *path, const gchar *mime_type);
 
 /**
  * modest_platform_show_uri_popup:
 
 /**
  * modest_platform_show_uri_popup: