[glib/wip/resources] resources: Implement querying the content type for resources



commit c1b7069c8cc3ad630c4001d133714fcc70ff7559
Author: Christian Persch <chpe gnome org>
Date:   Wed Jan 11 14:00:17 2012 +0100

    resources: Implement querying the content type for resources

 gio/gresourcefile.c   |   33 +++++++++++++++++++++++++++++++++
 gio/tests/resources.c |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 0 deletions(-)
---
diff --git a/gio/gresourcefile.c b/gio/gresourcefile.c
index bfa1e6e..8299327 100644
--- a/gio/gresourcefile.c
+++ b/gio/gresourcefile.c
@@ -34,6 +34,7 @@
 #include "gfileinputstream.h"
 #include "gfileinfo.h"
 #include "gfileenumerator.h"
+#include "gcontenttype.h"
 #include "gioerror.h"
 #include <glib/gstdio.h>
 #include "glibintl.h"
@@ -420,6 +421,7 @@ g_resource_file_query_info (GFile                *file,
   GResourceFile *resource = G_RESOURCE_FILE (file);
   GError *my_error = NULL;
   GFileInfo *info;
+  GFileAttributeMatcher *matcher;
   gboolean res;
   gsize size;
   guint32 resource_flags;
@@ -458,6 +460,7 @@ g_resource_file_query_info (GFile                *file,
 	}
     }
 
+  matcher = g_file_attribute_matcher_new (attributes);
 
   info = g_file_info_new ();
   base = g_resource_file_get_basename (file);
@@ -476,10 +479,40 @@ g_resource_file_query_info (GFile                *file,
     }
   else
     {
+      GBytes *bytes;
+      char *content_type;
+
       g_file_info_set_file_type (info, G_FILE_TYPE_REGULAR);
       g_file_info_set_size (info, size);
+
+      if ((_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
+           ((~resource_flags & G_RESOURCE_FLAGS_COMPRESSED) && 
+            _g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))) &&
+          (bytes = g_resources_lookup_data (resource->path, 0, NULL)))
+        {
+          const guchar *data;
+          gsize data_size;
+
+          data = g_bytes_get_data (bytes, &data_size);
+          content_type = g_content_type_guess (base, data, data_size, NULL);
+
+          g_bytes_unref (bytes);
+        }
+      else
+        content_type = NULL;
+
+      if (content_type)
+        {
+          _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE, content_type);
+          _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
+
+          g_free (content_type);
+        }
     }
 
+  g_free (base);
+  g_file_attribute_matcher_unref (matcher);
+
   return info;
 }
 
diff --git a/gio/tests/resources.c b/gio/tests/resources.c
index 9d84926..bf3bd9f 100644
--- a/gio/tests/resources.c
+++ b/gio/tests/resources.c
@@ -465,6 +465,45 @@ test_resource_module (void)
     }
 }
 
+static void
+test_uri_query_info (void)
+{
+  GResource *resource;
+  GError *error = NULL;
+  gboolean loaded_file;
+  char *content;
+  gsize content_size;
+  GBytes *data;
+  GFile *file;
+  GFileInfo *info;
+  const char *content_type;
+
+  loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
+                                     NULL);
+  g_assert (loaded_file);
+
+  data = g_bytes_new_take (content, content_size);
+  resource = g_resource_new_from_data (data, &error);
+  g_assert (resource != NULL);
+  g_assert_no_error (error);
+
+  g_resources_register (resource);
+
+  file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
+
+  info = g_file_query_info (file, "*", 0, NULL, &error);
+  g_assert_no_error (error);
+  g_object_unref (file);
+
+  content_type = g_file_info_get_content_type (info);
+  g_assert (content_type);
+  g_assert_cmpstr (content_type, ==, "text/plain");
+
+  g_object_unref (info);
+
+  g_resources_unregister (resource);
+  g_resource_unref (resource);
+}
 
 int
 main (int   argc,
@@ -484,6 +523,7 @@ main (int   argc,
   /* This only uses automatic resources too, so it tests the constructors and destructors */
   g_test_add_func ("/resource/module", test_resource_module);
 #endif
+  g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
 
   return g_test_run();
 }



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