[epiphany] e-download: add ephy_download_get_content_type



commit 89c1e042193df2a76eb9aecb2c6b5b25e6e6e1a2
Author: Diego Escalante Urrelo <descalante igalia com>
Date:   Mon Oct 10 19:46:17 2011 -0500

    e-download: add ephy_download_get_content_type
    
    This new API gets the content-type of a download using GIO, or Soup if
    GIO is still not available for the download.
    
    GIO is our first option since its local guesses are more reliable. Soup
    can be cheated by servers or confused by still too incomplete downloads.
    
    Bug #662059

 embed/ephy-download.c              |   50 ++++++++++++++++++++++++++++++++++-
 embed/ephy-download.h              |    2 +-
 lib/widgets/ephy-download-widget.c |   14 +++------
 3 files changed, 54 insertions(+), 12 deletions(-)
---
diff --git a/embed/ephy-download.c b/embed/ephy-download.c
index aa506ef..81a0446 100644
--- a/embed/ephy-download.c
+++ b/embed/ephy-download.c
@@ -140,6 +140,50 @@ ephy_download_set_property (GObject      *object,
   }
 }
 
+char *
+ephy_download_get_content_type (EphyDownload *download)
+{
+  WebKitNetworkResponse *response;
+  SoupMessage *message;
+  char *content_type = NULL;
+
+  GFile *destination;
+  GFileInfo *info;
+  GError *error = NULL;
+
+  destination = g_file_new_for_uri (download->priv->destination);
+  info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                            G_FILE_QUERY_INFO_NONE, NULL, &error);
+
+  if (error) {
+    LOG ("ephy_download_get_content_type: error getting file "
+         "content-type: %s", error->message);
+    g_error_free (error);
+
+    /* Fallback to Soup */
+    response = webkit_download_get_network_response (download->priv->download);
+    message = webkit_network_response_get_message (response);
+
+    if (message != NULL)
+       content_type = g_strdup (soup_message_headers_get_content_type (message->response_headers, NULL));
+
+    LOG ("ephy_download_get_content_type: Soup: %s", content_type);
+  } else {
+    content_type = g_strdup (g_file_info_get_content_type (info));
+    LOG ("ephy_download_get_content_type: GIO: %s", content_type);
+  }
+
+  if (info)
+    g_object_unref (info);
+
+  if (destination)
+    g_object_unref (destination);
+
+  LOG ("ephy_download_get_content_type: %s", content_type);
+
+  return content_type;
+}
+
 static EphyDownloadActionType
 decide_action_from_mime (EphyDownload *ephy_download)
 {
@@ -156,7 +200,7 @@ decide_action_from_mime (EphyDownload *ephy_download)
   message = webkit_network_response_get_message (response);
 
   if (message) {
-    const char *content_type = soup_message_headers_get_content_type (message->response_headers, NULL);
+    char *content_type = ephy_download_get_content_type (ephy_download);
 
     if (content_type) {
       mime_description = g_content_type_get_description (content_type);
@@ -164,7 +208,9 @@ decide_action_from_mime (EphyDownload *ephy_download)
 
       if (helper_app)
         action = EPHY_DOWNLOAD_ACTION_OPEN;
-     }
+
+      g_free (content_type);
+    }
   }
 
   if (mime_description == NULL) {
diff --git a/embed/ephy-download.h b/embed/ephy-download.h
index 8c84bbe..466da40 100644
--- a/embed/ephy-download.h
+++ b/embed/ephy-download.h
@@ -92,7 +92,7 @@ WebKitDownload *ephy_download_get_webkit_download (EphyDownload *download);
 
 const char   *ephy_download_get_destination_uri   (EphyDownload *download);
 const char   *ephy_download_get_source_uri        (EphyDownload *download);
-
+char         *ephy_download_get_content_type      (EphyDownload *download);
 
 guint32       ephy_download_get_start_time        (EphyDownload *download);
 
diff --git a/lib/widgets/ephy-download-widget.c b/lib/widgets/ephy-download-widget.c
index 0841168..a8d3403 100644
--- a/lib/widgets/ephy-download-widget.c
+++ b/lib/widgets/ephy-download-widget.c
@@ -54,21 +54,17 @@ enum
 };
 
 static GdkPixbuf *
-get_icon_from_download (WebKitDownload *download)
+get_icon_from_download (EphyDownload *ephy_download)
 {
-  WebKitNetworkResponse *response;
-  SoupMessage *message;
-  const char *content_type;
-
+  char *content_type = NULL;
   GIcon *gicon;
   GtkIconInfo *icon_info;
 
-  response = webkit_download_get_network_response (download);
-  message = webkit_network_response_get_message (response);
-  content_type = soup_message_headers_get_content_type (message->response_headers, NULL);
+  content_type = ephy_download_get_content_type (ephy_download);
 
   if (content_type != NULL) {
     gicon = g_content_type_get_icon (content_type);
+    g_free (content_type);
   } else {
     gicon = g_icon_new_for_string ("package-x-generic", NULL);
   }
@@ -450,7 +446,7 @@ ephy_download_widget_new (EphyDownload *ephy_download)
                          "download", ephy_download, NULL);
   download = ephy_download_get_webkit_download (ephy_download);
 
-  pixbuf = get_icon_from_download (download);
+  pixbuf = get_icon_from_download (ephy_download);
   basename = g_filename_display_basename (webkit_download_get_destination_uri (download));
   dest = g_uri_unescape_string (basename, NULL);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]