After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 506062 - gtk_recent_manager_add_item does not detect mime type on Windows
gtk_recent_manager_add_item does not detect mime type on Windows
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: Class: GtkRecent
unspecified
Other Windows
: Normal normal
: ---
Assigned To: gtk-bugs
Emmanuele Bassi (:ebassi)
Depends on: 528035
Blocks:
 
 
Reported: 2007-12-28 14:03 UTC by Armin Burgmeier
Modified: 2008-05-31 18:55 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Proposed patch (2.76 KB, patch)
2007-12-28 14:05 UTC, Armin Burgmeier
needs-work Details | Review
Use gio to find mime type (5.80 KB, patch)
2008-01-04 21:49 UTC, Armin Burgmeier
none Details | Review
[PATCH] Use GIO to find the MIME type (5.34 KB, patch)
2008-03-25 11:47 UTC, Emmanuele Bassi (:ebassi)
committed Details | Review
Test case (200 bytes, text/plain)
2008-05-31 17:33 UTC, Torsten Schoenfeld
  Details
Handle content type being NULL (854 bytes, patch)
2008-05-31 17:34 UTC, Torsten Schoenfeld
none Details | Review

Description Armin Burgmeier 2007-12-28 14:03:15 UTC
On Windows, gtk_recent_manager_add_item sets the mime type of the recent info always to application/octet-stream instead of guessing a default, as on Unix.
Comment 1 Armin Burgmeier 2007-12-28 14:05:37 UTC
Created attachment 101729 [details] [review]
Proposed patch

For local files, this patch looks up the file type by its extensions in the registry under HKEY_CLASSES_ROOT, and uses the "Content Type" field of that extension as mime type.
Comment 2 Emmanuele Bassi (:ebassi) 2008-01-03 14:04:21 UTC
thanks for the patch. some comments on the form:

+#elif G_OS_WIN32
+  char* p = strrchr(uri, '.');

wrong code style (also for the other function calls): you need a space between the function name and the parenthesis.

+  if(p != NULL)
+  {

wrong code style: if is not a function and the curly brace must be indented by two spaces

+    if(!res) recent_data.mime_type = g_strdup(GTK_RECENT_DEFAULT_MIME);

ditto as above for the g_strdup() function call, and an if on two lines make this more readable

+  }
+#endif
+#ifdef G_OS_WIN32
+  if (has_case_prefix (uri, "file:/"))

can't these two #ifdef G_OS_WIN32 be merged?

+                  if (RegQueryValueEx (key, "Content Type", ...
+                    {
+                      g_free(recent_data.mime_type);
+                    }
+                  else
+                    {
+                      success = TRUE;
+                    }

a single statement does not require curly braces

this is also a stop-gap patch: the best way to solve this is to make gtk_recent_manager_add_uri() use the GIO API for getting the content-type, assuming that the win32 backend already works.
Comment 3 Armin Burgmeier 2008-01-03 14:49:18 UTC
I'm sorry for the coding style - I normally try to honor the preferred style, but I often fall back to my own without noticing.

I can change this to use gio. I guess this should then use the asynchronous version, and add the recent item when the information about the content type is available? This way gtk_recent_manager_add_item does not guarantee that the item is added immediately.
Comment 4 Emmanuele Bassi (:ebassi) 2008-01-03 14:58:24 UTC
(In reply to comment #3)

> I can change this to use gio. I guess this should then use the asynchronous
> version, and add the recent item when the information about the content type is
> available? This way gtk_recent_manager_add_item does not guarantee that the
> item is added immediately.

the GtkRecentManager doesn't guarantee that new entries are added immediately; it guarantees that, if something changes in the storage, it will emit the ::changed signal. so, as far as I'm concerned, using the asynchronous version is perfectly fine.

Comment 5 Armin Burgmeier 2008-01-04 21:49:42 UTC
Created attachment 102168 [details] [review]
Use gio to find mime type

This uses gio to determine the mime type in gtk_recent_manager_add_item(). Tested on Linux and Windows.
Comment 6 Emmanuele Bassi (:ebassi) 2008-03-25 11:47:28 UTC
Created attachment 107985 [details] [review]
[PATCH] Use GIO to find the MIME type

updated version of the patch inside attachment 102168 [details] [review].

cleaned up the coding style, and handle the eventual errors. I also switched to using the fast mime type query - it's good enough for the add_item() case: if the user wants to be sure, then he can call g_file_query_info() and then gtk_recent_manager_add_full().

unfortunately, I cannot test it right now because I don't have a box with gvfs (need to update the jhbuild checkout); since I'm using the GIO API and comment 5 confirmed it working on two platforms, I'm going to commit this patch to trunk.
Comment 7 Emmanuele Bassi (:ebassi) 2008-04-15 22:00:28 UTC
committed to trunk.

2008-04-15  Emmanuele Bassi  <ebassi@gnome.org>

	Bug 506062 – gtk_recent_manager_add_item does not detect mime
	type on Windows

	* configure.in: Depend on gio-2.0

	* gtk/gtkrecentmanager.c:
	(gtk_recent_manager_add_item_query_info_cb),
	(gtk_recent_manager_add_item): Use GIO to (asynchronously) query
	the MIME type of the passed URI.
Comment 8 Torsten Schoenfeld 2008-05-31 17:33:31 UTC
This change seems to cause problems on my system.  For some reason, g_file_info_get_content_type always returns NULL (called from inside gtk_recent_manager_add_item_query_info_cb) independent of the file that's supposed to be added.  The following assertion occurs with the attached test program:

(recent-test.pl:10312): GLib-GIO-CRITICAL **: g_content_type_get_mime_type: assertion `type != NULL' failed
Gtk-WARNING **: Attempting to add `file:///usr/bin/perl' to the list of recently used resources, but not MIME type was defined at recent-test.pl line 11

And the file is not added to the recently-used-list.  This probably means that there's something wrong with my gio setup, but the recent chooser should just use the default MIME type in this case.  Patch attached.
Comment 9 Torsten Schoenfeld 2008-05-31 17:33:58 UTC
Created attachment 111854 [details]
Test case
Comment 10 Torsten Schoenfeld 2008-05-31 17:34:24 UTC
Created attachment 111855 [details] [review]
Handle content type being NULL
Comment 11 Emmanuele Bassi (:ebassi) 2008-05-31 17:38:32 UTC
(In reply to comment #8)
> This change seems to cause problems on my system.  For some reason,
> g_file_info_get_content_type always returns NULL

it's probably bug 535830. Torsten, can you please try the patch attached there and confirm? otherwise, I'll apply your patch as well.
Comment 12 Torsten Schoenfeld 2008-05-31 18:03:40 UTC
Yep, the patch in bug 535830 does seem to fix my issue as well.
Comment 13 Emmanuele Bassi (:ebassi) 2008-05-31 18:55:27 UTC
(In reply to comment #12)
> Yep, the patch in bug 535830 does seem to fix my issue as well.

thanks. I've just applied that patch to trunk.