/* Returns the MIME_type structure for the given type name. It is looked * up in type_hash and returned if found. If not found (and can_create is * TRUE) then a new MIME_type is made, added to type_hash and returned. * NULL is returned if type_name is not in type_hash and can_create is * FALSE, or if type_name does not contain a '/' character. */ static MIME_type* get_mime_type(const gchar *type_name, gboolean can_create) { g_return_val_if_fail(type_name, NULL); gchar *slash; MIME_type* mtype = g_hash_table_lookup(type_hash, type_name); if (mtype || !can_create) return mtype; dbg(2, "not found in cache: %s", type_name); slash = strchr(type_name, '/'); if (slash == NULL) { g_warning("MIME type '%s' does not contain a '/' character!", type_name); return NULL; } mtype = g_new(MIME_type, 1); mtype->media_type = g_strndup(type_name, slash - type_name); mtype->subtype = g_strdup(slash + 1); mtype->image = NULL; mtype->comment = NULL; mtype->executable = xdg_mime_mime_type_subclass(type_name, "application/x-executable"); g_hash_table_insert(type_hash, g_strdup(type_name), mtype); return mtype; }
bool MimeType::subclass_of(const char* mime) { E_ASSERT(mime != NULL); if(!(status & MIME_LOADED)) return false; return (xdg_mime_mime_type_subclass(mtype.c_str(), mime) == 1); }
static PyObject *mime_type_subclass (PyObject *self, PyObject *args) { const char *mime_a, *mime_b; if (!PyArg_ParseTuple(args, "ss", &mime_a, &mime_b)) return NULL; return PyBool_FromLong(xdg_mime_mime_type_subclass(mime_a, mime_b)); }
/** * g_content_type_is_a: * @type: a content type string * @supertype: a content type string * * Determines if @type is a subset of @supertype. * * Returns: %TRUE if @type is a kind of @supertype, * %FALSE otherwise. */ gboolean g_content_type_is_a (const gchar *type, const gchar *supertype) { gboolean res; g_return_val_if_fail (type != NULL, FALSE); g_return_val_if_fail (supertype != NULL, FALSE); G_LOCK (gio_xdgmime); res = xdg_mime_mime_type_subclass (type, supertype); G_UNLOCK (gio_xdgmime); return res; }
static void test_subclass (const char *mime_a, const char *mime_b, int expected) { int actual; actual = xdg_mime_mime_type_subclass (mime_a, mime_b); if (actual != expected) { printf ("Test Failed: %s is %s of %s\n", mime_a, actual ? "subclass" : "not subclass", mime_b); } }
/** * g_content_type_guess: * @filename: (allow-none): a string, or %NULL * @data: (allow-none) (array length=data_size): a stream of data, or %NULL * @data_size: the size of @data * @result_uncertain: (allow-none) (out): return location for the certainty * of the result, or %NULL * * Guesses the content type based on example data. If the function is * uncertain, @result_uncertain will be set to %TRUE. Either @filename * or @data may be %NULL, in which case the guess will be based solely * on the other argument. * * Returns: a string indicating a guessed content type for the * given data. Free with g_free() */ gchar * g_content_type_guess (const gchar *filename, const guchar *data, gsize data_size, gboolean *result_uncertain) { char *basename; const char *name_mimetypes[10], *sniffed_mimetype; char *mimetype; int i; int n_name_mimetypes; int sniffed_prio; sniffed_prio = 0; n_name_mimetypes = 0; sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN; if (result_uncertain) *result_uncertain = FALSE; /* our test suite and potentially other code used -1 in the past, which is * not documented and not allowed; guard against that */ g_return_val_if_fail (data_size != (gsize) -1, g_strdup (XDG_MIME_TYPE_UNKNOWN)); G_LOCK (gio_xdgmime); if (filename) { i = strlen (filename); if (filename[i - 1] == '/') { name_mimetypes[0] = "inode/directory"; name_mimetypes[1] = NULL; n_name_mimetypes = 1; if (result_uncertain) *result_uncertain = TRUE; } else { basename = g_path_get_basename (filename); n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10); g_free (basename); } } /* Got an extension match, and no conflicts. This is it. */ if (n_name_mimetypes == 1) { gchar *s = g_strdup (name_mimetypes[0]); G_UNLOCK (gio_xdgmime); return s; } if (data) { sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio); if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN && data && looks_like_text (data, data_size)) sniffed_mimetype = "text/plain"; /* For security reasons we don't ever want to sniff desktop files * where we know the filename and it doesn't have a .desktop extension. * This is because desktop files allow executing any application and * we don't want to make it possible to hide them looking like something * else. */ if (filename != NULL && strcmp (sniffed_mimetype, "application/x-desktop") == 0) sniffed_mimetype = "text/plain"; } if (n_name_mimetypes == 0) { if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN && result_uncertain) *result_uncertain = TRUE; mimetype = g_strdup (sniffed_mimetype); } else { mimetype = NULL; if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN) { if (sniffed_prio >= 80) /* High priority sniffing match, use that */ mimetype = g_strdup (sniffed_mimetype); else { /* There are conflicts between the name matches and we * have a sniffed type, use that as a tie breaker. */ for (i = 0; i < n_name_mimetypes; i++) { if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype)) { /* This nametype match is derived from (or the same as) * the sniffed type). This is probably it. */ mimetype = g_strdup (name_mimetypes[i]); break; } } } } if (mimetype == NULL) { /* Conflicts, and sniffed type was no help or not there. * Guess on the first one */ mimetype = g_strdup (name_mimetypes[0]); if (result_uncertain) *result_uncertain = TRUE; } } G_UNLOCK (gio_xdgmime); return mimetype; }