1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2014 Patrick Griffis
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "gcontenttype.h"
24 #include "gthemedicon.h"
26 #include <CoreServices/CoreServices.h>
28 #define XDG_PREFIX _gio_xdg
29 #include "xdgmime/xdgmime.h"
31 /* We lock this mutex whenever we modify global state in this module. */
32 G_LOCK_DEFINE_STATIC (gio_xdgmime);
36 * create_cfstring_from_cstr:
39 * Converts a cstr to a utf8 cfstring
40 * It must be CFReleased()'d.
44 create_cfstring_from_cstr (const gchar *cstr)
46 return CFStringCreateWithCString (NULL, cstr, kCFStringEncodingUTF8);
50 * create_cstr_from_cfstring:
51 * @str: a #CFStringRef
53 * Converts a cfstring to a utf8 cstring.
54 * The incoming cfstring is released for you.
55 * The returned string must be g_free()'d.
59 create_cstr_from_cfstring (CFStringRef str)
61 g_return_val_if_fail (str != NULL, NULL);
63 CFIndex length = CFStringGetLength (str);
64 CFIndex maxlen = CFStringGetMaximumSizeForEncoding (length, kCFStringEncodingUTF8);
65 gchar *buffer = g_malloc (maxlen + 1);
66 Boolean success = CFStringGetCString (str, (char *) buffer, maxlen,
67 kCFStringEncodingUTF8);
79 * create_cstr_from_cfstring_with_fallback:
80 * @str: a #CFStringRef
83 * Tries to convert a cfstring to a utf8 cstring.
84 * If @str is NULL or conversion fails @fallback is returned.
85 * The incoming cfstring is released for you.
86 * The returned string must be g_free()'d.
90 create_cstr_from_cfstring_with_fallback (CFStringRef str,
91 const gchar *fallback)
96 cstr = create_cstr_from_cfstring (str);
98 return g_strdup (fallback);
105 g_content_type_set_mime_dirs (const gchar * const *dirs)
111 const gchar * const *
112 g_content_type_get_mime_dirs (void)
114 const gchar * const *mime_dirs = { NULL };
119 g_content_type_equals (const gchar *type1,
122 CFStringRef str1, str2;
125 g_return_val_if_fail (type1 != NULL, FALSE);
126 g_return_val_if_fail (type2 != NULL, FALSE);
128 if (g_ascii_strcasecmp (type1, type2) == 0)
131 str1 = create_cfstring_from_cstr (type1);
132 str2 = create_cfstring_from_cstr (type2);
134 ret = UTTypeEqual (str1, str2);
143 g_content_type_is_a (const gchar *ctype,
144 const gchar *csupertype)
146 CFStringRef type, supertype;
149 g_return_val_if_fail (ctype != NULL, FALSE);
150 g_return_val_if_fail (csupertype != NULL, FALSE);
152 type = create_cfstring_from_cstr (ctype);
153 supertype = create_cfstring_from_cstr (csupertype);
155 ret = UTTypeConformsTo (type, supertype);
158 CFRelease (supertype);
164 g_content_type_is_mime_type (const gchar *type,
165 const gchar *mime_type)
170 g_return_val_if_fail (type != NULL, FALSE);
171 g_return_val_if_fail (mime_type != NULL, FALSE);
173 content_type = g_content_type_from_mime_type (mime_type);
174 ret = g_content_type_is_a (type, content_type);
175 g_free (content_type);
181 g_content_type_is_unknown (const gchar *type)
183 g_return_val_if_fail (type != NULL, FALSE);
185 /* Should dynamic types be considered "unknown"? */
186 if (g_str_has_prefix (type, "dyn."))
188 /* application/octet-stream */
189 else if (g_strcmp0 (type, "public.data") == 0)
196 g_content_type_get_description (const gchar *type)
199 CFStringRef desc_str;
201 g_return_val_if_fail (type != NULL, NULL);
203 str = create_cfstring_from_cstr (type);
204 desc_str = UTTypeCopyDescription (str);
207 return create_cstr_from_cfstring_with_fallback (desc_str, "unknown");
211 * _get_generic_icon_name_from_mime_type
213 * This function produces a generic icon name from a @mime_type.
214 * If no generic icon name is found in the xdg mime database, the
215 * generic icon name is constructed.
218 * generic-icon elements specify the icon to use as a generic icon for this
219 * particular mime-type, given by the name attribute. This is used if there
220 * is no specific icon (see icon for how these are found). These are used
221 * for categories of similar types (like spreadsheets or archives) that can
222 * use a common icon. The Icon Naming Specification lists a set of such
223 * icon names. If this element is not specified then the mimetype is used
224 * to generate the generic icon by using the top-level media type
225 * (e.g. "video" in "video/ogg") and appending "-x-generic"
226 * (i.e. "video-x-generic" in the previous example).
228 * From: https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html
232 _get_generic_icon_name_from_mime_type (const gchar *mime_type)
234 const gchar *xdg_icon_name;
237 G_LOCK (gio_xdgmime);
238 xdg_icon_name = xdg_mime_get_generic_icon (mime_type);
239 G_UNLOCK (gio_xdgmime);
241 if (xdg_icon_name == NULL)
244 const char *suffix = "-x-generic";
247 p = strchr (mime_type, '/');
249 prefix_len = strlen (mime_type);
251 prefix_len = p - mime_type;
253 icon_name = g_malloc (prefix_len + strlen (suffix) + 1);
254 memcpy (icon_name, mime_type, prefix_len);
255 memcpy (icon_name + prefix_len, suffix, strlen (suffix));
256 icon_name[prefix_len + strlen (suffix)] = 0;
260 icon_name = g_strdup (xdg_icon_name);
268 g_content_type_get_icon_internal (const gchar *uti,
273 char *generic_mimetype_icon = NULL;
278 const char *xdg_icon;
281 g_return_val_if_fail (uti != NULL, NULL);
283 mime_type = g_content_type_get_mime_type (uti);
285 G_LOCK (gio_xdgmime);
286 xdg_icon = xdg_mime_get_icon (mime_type);
287 G_UNLOCK (gio_xdgmime);
290 icon_names[n++] = g_strdup (xdg_icon);
292 mimetype_icon = g_strdup (mime_type);
293 while ((q = strchr (mimetype_icon, '/')) != NULL)
296 icon_names[n++] = mimetype_icon;
298 generic_mimetype_icon = _get_generic_icon_name_from_mime_type (mime_type);
300 if (generic_mimetype_icon)
301 icon_names[n++] = generic_mimetype_icon;
305 for (i = 0; i < n; i++)
307 icon_names[n + i] = icon_names[i];
308 icon_names[i] = g_strconcat (icon_names[i], "-symbolic", NULL);
314 themed_icon = g_themed_icon_new_from_names (icon_names, n);
316 for (i = 0; i < n; i++)
317 g_free (icon_names[i]);
325 g_content_type_get_icon (const gchar *type)
327 return g_content_type_get_icon_internal (type, FALSE);
331 g_content_type_get_symbolic_icon (const gchar *type)
333 return g_content_type_get_icon_internal (type, TRUE);
337 g_content_type_get_generic_icon_name (const gchar *type)
343 g_content_type_can_be_executable (const gchar *type)
346 gboolean ret = FALSE;
348 g_return_val_if_fail (type != NULL, FALSE);
350 uti = create_cfstring_from_cstr (type);
352 if (UTTypeConformsTo (uti, kUTTypeApplication))
354 else if (UTTypeConformsTo (uti, CFSTR("public.executable")))
356 else if (UTTypeConformsTo (uti, CFSTR("public.script")))
358 /* Our tests assert that all text can be executable... */
359 else if (UTTypeConformsTo (uti, CFSTR("public.text")))
367 g_content_type_from_mime_type (const gchar *mime_type)
369 CFStringRef mime_str;
372 g_return_val_if_fail (mime_type != NULL, NULL);
374 /* Their api does not handle globs but they are common. */
375 if (g_str_has_suffix (mime_type, "*"))
377 if (g_str_has_prefix (mime_type, "audio"))
378 return g_strdup ("public.audio");
379 if (g_str_has_prefix (mime_type, "image"))
380 return g_strdup ("public.image");
381 if (g_str_has_prefix (mime_type, "text"))
382 return g_strdup ("public.text");
383 if (g_str_has_prefix (mime_type, "video"))
384 return g_strdup ("public.movie");
387 /* Some exceptions are needed for gdk-pixbuf.
388 * This list is not exhaustive.
390 if (g_str_has_prefix (mime_type, "image"))
392 if (g_str_has_suffix (mime_type, "x-icns"))
393 return g_strdup ("com.apple.icns");
394 if (g_str_has_suffix (mime_type, "x-tga"))
395 return g_strdup ("com.truevision.tga-image");
396 if (g_str_has_suffix (mime_type, "x-ico"))
397 return g_strdup ("com.microsoft.ico ");
400 /* These are also not supported...
401 * Used in glocalfileinfo.c
403 if (g_str_has_prefix (mime_type, "inode"))
405 if (g_str_has_suffix (mime_type, "directory"))
406 return g_strdup ("public.folder");
407 if (g_str_has_suffix (mime_type, "symlink"))
408 return g_strdup ("public.symlink");
411 /* This is correct according to the Apple docs:
412 https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
414 if (strcmp (mime_type, "text/plain") == 0)
415 return g_strdup ("public.text");
417 /* Non standard type */
418 if (strcmp (mime_type, "application/x-executable") == 0)
419 return g_strdup ("public.executable");
421 mime_str = create_cfstring_from_cstr (mime_type);
422 uti_str = UTTypeCreatePreferredIdentifierForTag (kUTTagClassMIMEType, mime_str, NULL);
424 CFRelease (mime_str);
425 return create_cstr_from_cfstring_with_fallback (uti_str, "public.data");
429 g_content_type_get_mime_type (const gchar *type)
432 CFStringRef mime_str;
434 g_return_val_if_fail (type != NULL, NULL);
436 /* We must match the additions above
437 * so conversions back and forth work.
439 if (g_str_has_prefix (type, "public"))
441 if (g_str_has_suffix (type, ".image"))
442 return g_strdup ("image/*");
443 if (g_str_has_suffix (type, ".movie"))
444 return g_strdup ("video/*");
445 if (g_str_has_suffix (type, ".text"))
446 return g_strdup ("text/*");
447 if (g_str_has_suffix (type, ".audio"))
448 return g_strdup ("audio/*");
449 if (g_str_has_suffix (type, ".folder"))
450 return g_strdup ("inode/directory");
451 if (g_str_has_suffix (type, ".symlink"))
452 return g_strdup ("inode/symlink");
453 if (g_str_has_suffix (type, ".executable"))
454 return g_strdup ("application/x-executable");
457 uti_str = create_cfstring_from_cstr (type);
458 mime_str = UTTypeCopyPreferredTagWithClass(uti_str, kUTTagClassMIMEType);
461 return create_cstr_from_cfstring_with_fallback (mime_str, "application/octet-stream");
465 looks_like_text (const guchar *data,
471 for (i = 0; i < data_size; i++)
474 if (g_ascii_iscntrl (c) && !g_ascii_isspace (c) && c != '\b')
481 g_content_type_guess (const gchar *filename,
484 gboolean *result_uncertain)
486 CFStringRef uti = NULL;
488 CFStringRef extension;
491 g_return_val_if_fail (data_size != (gsize) -1, NULL);
493 if (filename && *filename)
495 gchar *basename = g_path_get_basename (filename);
496 gchar *dirname = g_path_get_dirname (filename);
497 gsize i = strlen (filename);
499 if (filename[i - 1] == '/')
501 if (g_strcmp0 (dirname, "/Volumes") == 0)
503 uti = CFStringCreateCopy (NULL, kUTTypeVolume);
505 else if ((cextension = strrchr (basename, '.')) != NULL)
508 extension = create_cfstring_from_cstr (cextension);
509 uti = UTTypeCreatePreferredIdentifierForTag (kUTTagClassFilenameExtension,
511 CFRelease (extension);
513 if (CFStringHasPrefix (uti, CFSTR ("dyn.")))
516 uti = CFStringCreateCopy (NULL, kUTTypeFolder);
522 uti = CFStringCreateCopy (NULL, kUTTypeFolder);
523 uncertain = TRUE; /* Matches Unix backend */
528 /* GTK needs this... */
529 if (g_str_has_suffix (basename, ".ui"))
531 uti = CFStringCreateCopy (NULL, kUTTypeXML);
533 else if (g_str_has_suffix (basename, ".txt"))
535 uti = CFStringCreateCopy (NULL, CFSTR ("public.text"));
537 else if ((cextension = strrchr (basename, '.')) != NULL)
540 extension = create_cfstring_from_cstr (cextension);
541 uti = UTTypeCreatePreferredIdentifierForTag (kUTTagClassFilenameExtension,
543 CFRelease (extension);
549 if (data && (!filename || !uti ||
550 CFStringCompare (uti, CFSTR ("public.data"), 0) == kCFCompareEqualTo))
552 const char *sniffed_mimetype;
553 G_LOCK (gio_xdgmime);
554 sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, NULL);
555 G_UNLOCK (gio_xdgmime);
556 if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
558 gchar *uti_str = g_content_type_from_mime_type (sniffed_mimetype);
559 uti = create_cfstring_from_cstr (uti_str);
562 if (!uti && looks_like_text (data, data_size))
564 if (g_str_has_prefix ((const gchar*)data, "#!/"))
565 uti = CFStringCreateCopy (NULL, CFSTR ("public.script"));
567 uti = CFStringCreateCopy (NULL, CFSTR ("public.text"));
573 /* Generic data type */
574 uti = CFStringCreateCopy (NULL, CFSTR ("public.data"));
575 if (result_uncertain)
576 *result_uncertain = TRUE;
578 else if (result_uncertain)
580 *result_uncertain = uncertain == -1 ? FALSE : uncertain;
583 return create_cstr_from_cfstring (uti);
587 g_content_types_get_registered (void)
589 /* TODO: UTTypeCreateAllIdentifiersForTag? */
594 g_content_type_guess_for_tree (GFile *root)