Esempio n. 1
0
NS_IMETHODIMP
nsGIOService::GetMimeTypeFromExtension(const nsACString& aExtension,
                                             nsACString& aMimeType)
{
  nsCAutoString fileExtToUse("file.");
  fileExtToUse.Append(aExtension);

  gboolean result_uncertain;
  char *content_type = g_content_type_guess(fileExtToUse.get(),
                                            NULL,
                                            0,
                                            &result_uncertain);
  if (!content_type)
    return NS_ERROR_FAILURE;

  char *mime_type = g_content_type_get_mime_type(content_type);
  if (!mime_type) {
    g_free(content_type);
    return NS_ERROR_FAILURE;
  }

  aMimeType.Assign(mime_type);

  g_free(mime_type);
  g_free(content_type);

  return NS_OK;
}
Esempio n. 2
0
/**
 * Returns the inferred MIME type of the given download.
 *
 * \param L The Lua VM state.
 * \param download The \ref download_t of the download.
 *
 * \luastack
 * \lparam A \c download object.
 * \lreturn The inferred MIME type of the download as a string.
 */
static gint
luaH_download_get_mime_type(lua_State *L, download_t *download)
{
    GError *error = NULL;
    const gchar *destination = webkit_download_get_destination_uri(
            download->webkit_download);
    GFile *file = g_file_new_for_uri(destination);
    GFileInfo *info = g_file_query_info(file, "standard::*", 0, NULL, &error);

    if (error) {
        if (download->error)
            g_free(download->error);
        download->error = g_strdup(error->message);
        luaH_warn(L, "%s", download->error);
        return 0;
    }

    const gchar *content_type = g_file_info_get_content_type(info);
    const gchar *mime_type = g_content_type_get_mime_type(content_type);

    g_object_unref(file);
    if (mime_type) {
        lua_pushstring(L, mime_type);
        return 1;
    }
    return 0;
}
/**
 * Create file stream and set mime type for channel
 * @param info file info used to determine mime type
 * @return NS_OK when file stream created successfuly, error code otherwise
 */
nsresult
nsGIOInputStream::DoOpenFile(GFileInfo *info)
{
  GError *error = nullptr;

  mStream = g_file_read(mHandle, nullptr, &error);
  if (!mStream) {
    nsresult rv = MapGIOResult(error);
    g_warning("Cannot read from file: %s", error->message);
    g_error_free(error);
    return rv;
  }

  const char * content_type = g_file_info_get_content_type(info);
  if (content_type) {
    char *mime_type = g_content_type_get_mime_type(content_type);
    if (mime_type) {
      if (strcmp(mime_type, APPLICATION_OCTET_STREAM) != 0) {
        SetContentTypeOfChannel(mime_type);
      }
      g_free(mime_type);
    }
  } else {
    g_warning("Missing content type.");
  }

  mBytesRemaining = g_file_info_get_size(info);
  // Update the content length attribute on the channel.  We do this
  // synchronously without proxying.  This hack is not as bad as it looks!
  mChannel->SetContentLength(mBytesRemaining);

  return NS_OK;
}
Esempio n. 4
0
/*
 * Next based on evince code.
 * See https://git.gnome.org/browse/evince/tree/libdocument/ev-file-helpers.c
 */
static gchar *
get_mime_type_from_uri (const gchar *uri, GError **error)
{
	GFile *file;
	GFileInfo *file_info;
	const gchar *content_type;
	gchar *mime_type = NULL;

	file = g_file_new_for_uri (uri);
	file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
	                               0, NULL, error);
	g_object_unref (file);

	if (file_info == NULL)
		return NULL;

	content_type = g_file_info_get_content_type (file_info);
	if (content_type != NULL) {
		mime_type = g_content_type_get_mime_type (content_type);
	}
	if (mime_type == NULL) {
		g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unknown MIME Type");
	}

	g_object_unref (file_info);

	return mime_type;
}
Esempio n. 5
0
static const char *
create_res_for_file (const char          *file_path,
                     GUPnPDIDLLiteObject *object)
{
        GUPnPDIDLLiteResource *res;
        GUPnPProtocolInfo *info;
        char *content_type;
        char *mime_type;
        const char *upnp_class;

        res = gupnp_didl_lite_object_add_resource (object);
        gupnp_didl_lite_resource_set_uri (res, "");

        content_type = g_content_type_guess (file_path, NULL, 0, NULL);
        mime_type = g_content_type_get_mime_type (content_type);
        upnp_class = guess_upnp_class_for_mime_type (mime_type);

        info = gupnp_protocol_info_new ();
        gupnp_protocol_info_set_mime_type (info, mime_type);
        gupnp_protocol_info_set_protocol (info, "*");

        gupnp_didl_lite_resource_set_protocol_info (res, info);

        g_object_unref (info);
        g_object_unref (res);
        g_free (mime_type);
        g_free (content_type);

        return upnp_class;
}
Esempio n. 6
0
static char *
rsvg_acquire_file_data (const char *filename,
                        const char *base_uri,
                        char **out_mime_type,
                        gsize *out_len,
                        GCancellable *cancellable,
                        GError **error)
{
    gchar *path, *data;
    gsize len;
    char *content_type;

    rsvg_return_val_if_fail (filename != NULL, NULL, error);
    g_assert (out_len != NULL);

    path = _rsvg_io_get_file_path (filename, base_uri);
    if (path == NULL)
        return NULL;

    if (!g_file_get_contents (path, &data, &len, error)) {
        g_free (path);
        return NULL;
    }

    if (out_mime_type &&
        (content_type = g_content_type_guess (path, (guchar *) data, len, NULL))) {
        *out_mime_type = g_content_type_get_mime_type (content_type);
        g_free (content_type);
    }

    g_free (path);

    *out_len = len;
    return data;
}
Esempio n. 7
0
static gchar *
guess_mime_type(const char *name, GInputStream *stream, gboolean *uncertain)
{
    gchar *content_type = NULL;
    gchar *mime_type = NULL;
    guchar data[1024];
    gsize data_length;
    gboolean text_p;

    if (read_head_data(stream, data, sizeof(data), &data_length))
        content_type = g_content_type_guess(name, data, data_length, uncertain);

    if (!content_type)
        content_type = g_content_type_guess(name, NULL, 0, uncertain);

    mime_type = g_content_type_get_mime_type(content_type);

    g_free(content_type);

    text_p = (mime_type && g_str_has_prefix(mime_type, "text/"));
    if (!text_p && data_length > 0) {
        gchar *encrypted_mime_type;
        encrypted_mime_type = guess_encrypted_mime_type(name, stream,
                              data, data_length,
                              uncertain);
        if (encrypted_mime_type) {
            g_free(mime_type);
            mime_type = encrypted_mime_type;
        }
    }

    return mime_type;
}
Esempio n. 8
0
static dlr_host_file_t *prv_host_file_new(const gchar *file, unsigned int id,
					  GError **error)
{
	dlr_host_file_t *hf = NULL;
	gchar *extension;
	gchar *content_type = NULL;

	if (!g_file_test(file, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS)) {
		*error = g_error_new(DLEYNA_SERVER_ERROR,
				     DLEYNA_ERROR_OBJECT_NOT_FOUND,
				     "File %s does not exist or is not a regular file",
				     file);
		goto on_error;
	}

	hf = g_new0(dlr_host_file_t, 1);
	hf->id = id;
	hf->clients = g_ptr_array_new_with_free_func(g_free);

	content_type = g_content_type_guess(file, NULL, 0, NULL);

	if (!content_type) {
		*error = g_error_new(DLEYNA_SERVER_ERROR, DLEYNA_ERROR_BAD_MIME,
				     "Unable to determine Content Type for %s",
				     file);
		goto on_error;
	}

	hf->mime_type =  g_content_type_get_mime_type(content_type);

	if (!hf->mime_type) {
		*error = g_error_new(DLEYNA_SERVER_ERROR, DLEYNA_ERROR_BAD_MIME,
				     "Unable to determine MIME Type for %s",
				     file);
		goto on_error;
	}

	g_free(content_type);

	extension = strrchr(file, '.');
	hf->path = g_strdup_printf(DLR_HOST_SERVICE_ROOT"/%d%s",
				   hf->id, extension ? extension : "");

	hf->dlna_header = prv_compute_dlna_header(file);

	return hf;

on_error:

	g_free(content_type);
	prv_host_file_delete(hf);

	return NULL;
}
Esempio n. 9
0
/**
 * e_icon_factory_create_thumbnail
 * @filename: the file name to create the thumbnail for
 *
 * Creates system thumbnail for @filename.
 *
 * Returns: Path to system thumbnail of the file; %NULL if couldn't
 *          create it. Free it with g_free().
 **/
gchar *
e_icon_factory_create_thumbnail (const gchar *filename)
{
#ifdef HAVE_GNOME_DESKTOP
	static GnomeDesktopThumbnailFactory *thumbnail_factory = NULL;
	struct stat file_stat;
	gchar *thumbnail = NULL;

	g_return_val_if_fail (filename != NULL, NULL);

	if (thumbnail_factory == NULL) {
		thumbnail_factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_NORMAL);
	}

	if (g_stat (filename, &file_stat) != -1 && S_ISREG (file_stat.st_mode)) {
		gchar *content_type, *mime = NULL;
		gboolean uncertain = FALSE;

		content_type = g_content_type_guess (filename, NULL, 0, &uncertain);
		if (content_type)
			mime = g_content_type_get_mime_type (content_type);

		if (mime) {
			gchar *uri = g_filename_to_uri (filename, NULL, NULL);

			g_return_val_if_fail (uri != NULL, NULL);

			thumbnail = gnome_desktop_thumbnail_factory_lookup (thumbnail_factory, uri, file_stat.st_mtime);
			if (!thumbnail && gnome_desktop_thumbnail_factory_can_thumbnail (thumbnail_factory, uri, mime, file_stat.st_mtime)) {
				GdkPixbuf *pixbuf;

				pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (thumbnail_factory, uri, mime);

				if (pixbuf) {
					gnome_desktop_thumbnail_factory_save_thumbnail (thumbnail_factory, pixbuf, uri, file_stat.st_mtime);
					g_object_unref (pixbuf);

					thumbnail = gnome_desktop_thumbnail_factory_lookup (thumbnail_factory, uri, file_stat.st_mtime);
				}
			}

			g_free (uri);
		}

		g_free (content_type);
		g_free (mime);
	}

	return thumbnail;
#else
	return NULL;
#endif /* HAVE_GNOME_DESKTOP */
}
Esempio n. 10
0
static GInputStream *
rsvg_acquire_gvfs_stream (const char *uri, 
                          const char *base_uri, 
                          char **out_mime_type,
                          GCancellable *cancellable,
                          GError **error)
{
    GFile *base, *file;
    GFileInputStream *stream;
    GError *err = NULL;

    file = g_file_new_for_uri (uri);

    stream = g_file_read (file, cancellable, &err);
    g_object_unref (file);

    if (stream == NULL &&
        g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
        g_clear_error (&err);

        base = g_file_new_for_uri (base_uri);
        file = g_file_resolve_relative_path (base, uri);
        g_object_unref (base);

        stream = g_file_read (file, cancellable, &err);
        g_object_unref (file);
    }

    if (stream == NULL) {
        g_propagate_error (error, err);
        return NULL;
    }

    if (out_mime_type) {
        GFileInfo *file_info;
        const char *content_type;

        file_info = g_file_input_stream_query_info (stream, 
                                                    G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
                                                    cancellable,
                                                    NULL /* error */);
        if (file_info &&
            (content_type = g_file_info_get_content_type (file_info)))
            *out_mime_type = g_content_type_get_mime_type (content_type);
        else
            *out_mime_type = NULL;

        if (file_info)
            g_object_unref (file_info);
    }

    return G_INPUT_STREAM (stream);
}
Esempio n. 11
0
/**
 * gedit_document_get_mime_type:
 * @doc: a #GeditDocument.
 *
 * Note: this never returns %NULL.
 **/
gchar *
gedit_document_get_mime_type (GeditDocument *doc)
{
	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), g_strdup ("text/plain"));

	if (doc->priv->content_type != NULL &&
	    !g_content_type_is_unknown (doc->priv->content_type))
	{
		return g_content_type_get_mime_type (doc->priv->content_type);
	}

	return g_strdup ("text/plain");
}
Esempio n. 12
0
static GInputStream *
webkit_soup_request_file_send (WebKitSoupRequest          *request,
			       GCancellable         *cancellable,
			       GError              **error)
{
	WebKitSoupRequestFile *file = WEBKIT_SOUP_REQUEST_FILE (request);
	GInputStream *stream;
	GError *my_error = NULL;

	if (!webkit_soup_request_file_ensure_file (file, cancellable, error))
		return NULL;

	stream = G_INPUT_STREAM (g_file_read (file->priv->gfile,
					      cancellable, &my_error));
	if (stream == NULL) {
		if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY)) {
			GFileEnumerator *enumerator;
			g_clear_error (&my_error);
			enumerator = g_file_enumerate_children (file->priv->gfile,
								"*",
								G_FILE_QUERY_INFO_NONE,
								cancellable,
								error);
			if (enumerator) {
				stream = webkit_soup_directory_input_stream_new (enumerator,
										 webkit_soup_request_get_uri (request));
				g_object_unref (enumerator);
				file->priv->mime_type = g_strdup ("text/html");
			}
		} else {
			g_propagate_error (error, my_error);
		}
	} else {
		GFileInfo *info = g_file_query_info (file->priv->gfile,
						     G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
						     G_FILE_ATTRIBUTE_STANDARD_SIZE,
						     0, cancellable, NULL);
		if (info) {
			const char *content_type;
			file->priv->size = g_file_info_get_size (info);
			content_type = g_file_info_get_content_type (info);

			if (content_type)
				file->priv->mime_type = g_content_type_get_mime_type (content_type);
			g_object_unref (info);
		}
	}

	return stream;
}
Esempio n. 13
0
char* GIOGetMimeType(const char *path) {
    /* Get file mime type by sniffing a portion of the file */
    /* If that does not work, then depend on guessing type. */
#define	sniff_length	4096
    char *content_type=NULL,*mime=0;

    /*
       Doing this on Windows is horrendously slow. glib on Windows also only uses
       the sniff buffer iff MIME detection via file extension fails, and even
       then, only for determining if it /looks like/ a text file.
    */
#ifndef __MINGW32__
    FILE *fp;

    if ( (fp=fopen(path,"rb"))!=NULL ) {
        guchar sniff_buffer[sniff_length];
        gboolean uncertain;
        size_t res=fread(sniff_buffer,1,sniff_length,fp);
        fclose (fp);
        if ( res>=0 ) {
            // first force guessing file type from the content only by passing
            // NULL for file name, if the result is not certain try again with
            // file name
            content_type=g_content_type_guess(NULL,sniff_buffer,res,&uncertain);
            if (uncertain) {
                if (content_type!=NULL)
                    g_free(content_type);
                content_type=g_content_type_guess(path,sniff_buffer,res,NULL);
            }
        }
    }
#endif

    if ( content_type==NULL )
        /* if sniffing failed - then try and guess the file type */
        content_type=g_content_type_guess(path,NULL,0,NULL);

    if ( content_type!=NULL ) {
        char *temp=g_content_type_get_mime_type(content_type);
        g_free(content_type);

        if ( temp!=NULL ) {
            mime=copy(temp);	/* ...convert to generic malloc/free */
            g_free(temp);
        }
    }

    return( mime );
}
Esempio n. 14
0
static char *
rsvg_acquire_gvfs_data (const char *uri,
                        const char *base_uri,
                        char **out_mime_type,
                        gsize *out_len,
                        GCancellable *cancellable,
                        GError **error)
{
    GFile *base, *file;
    GError *err;
    char *data;
    gsize len;
    char *content_type;
    gboolean res;

    file = g_file_new_for_uri (uri);

    err = NULL;
    data = NULL;
    if (!(res = g_file_load_contents (file, cancellable, &data, &len, NULL, &err)) &&
        g_error_matches (err, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
        base_uri != NULL) {
        g_clear_error (&err);
        g_object_unref (file);

        base = g_file_new_for_uri (base_uri);
        file = g_file_resolve_relative_path (base, uri);
        g_object_unref (base);

        res = g_file_load_contents (file, cancellable, &data, &len, NULL, &err);
    }

    g_object_unref (file);

    if (err) {
        g_propagate_error (error, err);
        return NULL;
    }

    if (out_mime_type &&
        (content_type = g_content_type_guess (uri, (guchar *) data, len, NULL))) {
        *out_mime_type = g_content_type_get_mime_type (content_type);
        g_free (content_type);
    }

    *out_len = len;
    return data;
}
Esempio n. 15
0
/**
 * ev_document_factory_get_document_for_stream:
 * @stream: a #GInputStream
 * @mime_type: (allow-none): a mime type hint
 * @flags: flags from #EvDocumentLoadFlags
 * @cancellable: (allow-none): a #GCancellable, or %NULL
 * @error: (allow-none): a #GError location to store an error, or %NULL
 *
 * Synchronously creates a #EvDocument for the document from @stream; or, if no
 * backend handling the document's type is found, or an error occurred
 * on opening the document, returns %NULL and fills in @error.
 * If the document is encrypted, it is returned but also @error is set to
 * %EV_DOCUMENT_ERROR_ENCRYPTED.
 *
 * If @mime_type is non-%NULL, this overrides any type inferred from the stream.
 * If the mime type cannot be inferred from the stream, and @mime_type is %NULL,
 * an error is returned.
 *
 * Returns: (transfer full): a new #EvDocument, or %NULL
 *
 * Since: 3.6
 */
EvDocument*
ev_document_factory_get_document_for_stream (GInputStream *stream,
                                             const char *mime_type,
                                             EvDocumentLoadFlags flags,
                                             GCancellable *cancellable,
                                             GError **error)
{
        EvDocument *document;
        char *mime = NULL;

        g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);

        if (mime_type == NULL && G_IS_FILE_INPUT_STREAM (stream)) {
                GFileInfo *file_info;
                const char *content_type;

                file_info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
                                                            G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
                                                            cancellable,
                                                            error);
                if (file_info != NULL) {
                        content_type = g_file_info_get_content_type (file_info);
                        if (content_type)
                                mime_type = mime = g_content_type_get_mime_type (content_type);
                        g_object_unref (file_info);
                }
        }

        if (mime_type == NULL) {
                g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                                     "Cannot query mime type from stream");
                return NULL;
        }

        document = ev_document_factory_new_document_for_mime_type (mime_type, error);
        g_free (mime);

        if (document == NULL)
                return NULL;

        if (!ev_document_load_stream (document, stream, flags, cancellable, error)) {
                g_object_unref (document);
                return NULL;
        }

        return document;
}
Esempio n. 16
0
char* get_mime(const char* fileName)
{
  char *res;
  char *content_type = g_content_type_guess (fileName, NULL, 0, NULL);
  if (content_type == NULL)
    return default_mime_type();
  char *mime_type = g_content_type_get_mime_type (content_type);
  g_free(content_type);
  if (mime_type == NULL)
    return default_mime_type();

  res = g_malloc(sizeof(char) * (strlen(mime_type)+1));
  strcpy(res, mime_type);

  g_free(mime_type);
  return res;
}
Esempio n. 17
0
/**
 * gedit_document_get_mime_type:
 * @doc: a #GeditDocument.
 *
 * Note: this never returns %NULL.
 **/
gchar *
gedit_document_get_mime_type (GeditDocument *doc)
{
	GeditDocumentPrivate *priv;

	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), g_strdup ("text/plain"));

	priv = gedit_document_get_instance_private (doc);

	if (priv->content_type != NULL &&
	    !g_content_type_is_unknown (priv->content_type))
	{
		return g_content_type_get_mime_type (priv->content_type);
	}

	return g_strdup ("text/plain");
}
Esempio n. 18
0
/**
 * ev_document_factory_get_document_for_gfile:
 * @file: a #GFile
 * @flags: flags from #EvDocumentLoadFlags
 * @cancellable: (allow-none): a #GCancellable, or %NULL
 * @error: (allow-none): a #GError location to store an error, or %NULL
 *
 * Synchronously creates a #EvDocument for the document at @file; or, if no
 * backend handling the document's type is found, or an error occurred on
 * opening the document, returns %NULL and fills in @error.
 * If the document is encrypted, it is returned but also @error is set to
 * %EV_DOCUMENT_ERROR_ENCRYPTED.
 *
 * Returns: (transfer full): a new #EvDocument, or %NULL
 *
 * Since: 3.6
 */
EvDocument*
ev_document_factory_get_document_for_gfile (GFile *file,
                                            EvDocumentLoadFlags flags,
                                            GCancellable *cancellable,
                                            GError **error)
{
        EvDocument *document;
        GFileInfo *file_info;
        const char *content_type;
        char *mime_type = NULL;

        g_return_val_if_fail (G_IS_FILE (file), NULL);
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);


        file_info = g_file_query_info (file,
                                       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
                                       G_FILE_QUERY_INFO_NONE,
                                       cancellable,
                                       error);
        if (file_info == NULL)
                return NULL;

        content_type = g_file_info_get_content_type (file_info);
        if (content_type == NULL) {
                g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                                     "Failed to query file mime type");
                return NULL;
        }

        mime_type = g_content_type_get_mime_type (content_type);
        g_object_unref (file_info);

        document = ev_document_factory_new_document_for_mime_type (mime_type, error);
        g_free (mime_type);
        if (document == NULL)
                return NULL;

        if (!ev_document_load_gfile (document, file, flags, cancellable, error)) {
                g_object_unref (document);
                return NULL;
        }

        return document;
}
Esempio n. 19
0
static char *
sniff_gio (SoupContentSniffer *sniffer, SoupMessage *msg, SoupBuffer *buffer)
{
    SoupURI *uri;
    char *uri_path;
    char *content_type;
    char *mime_type;
    gboolean uncertain;

    uri = soup_message_get_uri (msg);
    uri_path = soup_uri_to_string (uri, TRUE);

    content_type= g_content_type_guess (uri_path, (const guchar*)buffer->data, buffer->length, &uncertain);
    mime_type = g_content_type_get_mime_type (content_type);

    g_free (uri_path);
    g_free (content_type);

    return mime_type;
}
Esempio n. 20
0
GtkSourceLanguage *
gitg_utils_get_language(gchar const *content_type)
{
	if (!gitg_utils_can_display_content_type(content_type))
		return NULL;
	
	gchar *mime = g_content_type_get_mime_type(content_type);
	GtkSourceLanguageManager *manager = gtk_source_language_manager_get_default();
	
	gchar const * const *ids = gtk_source_language_manager_get_language_ids(manager);
	gchar const *ptr;
	GtkSourceLanguage *ret;
	
	while ((ptr = *ids++))
	{
		ret = gtk_source_language_manager_get_language(manager, ptr);
		gchar **mime_types = gtk_source_language_get_mime_types(ret);
		gchar **types = mime_types;
		gchar *m;
		
		if (types)
		{
			while ((m = *types++))
			{
				if (strcmp(mime, m) == 0)
				{
					g_free(mime);
					g_strfreev(mime_types);
					return ret;
				}
			}
		
			g_strfreev(mime_types);
		}

		ret = NULL;
	}
	
	g_free(mime);
	return NULL;
}
Esempio n. 21
0
char *
get_content_type_from_mime_type(const char *mimeType)
{
  GList* contentTypes = g_content_types_get_registered();
  GList* ct_ptr = contentTypes;
  char* foundContentType = NULL;

  while (ct_ptr) {
    char *mimeTypeFromContentType =  g_content_type_get_mime_type((char*)ct_ptr->data);
    if (strcmp(mimeTypeFromContentType, mimeType) == 0) {
      foundContentType = g_strdup((char*)ct_ptr->data);
      g_free(mimeTypeFromContentType);
      break;
    }
    g_free(mimeTypeFromContentType);
    ct_ptr = ct_ptr->next;
  }
  g_list_foreach(contentTypes, (GFunc) g_free, NULL);
  g_list_free(contentTypes);
  return foundContentType;
}
Esempio n. 22
0
CoglBool
rut_file_info_is_asset (GFileInfo *info, const char *name)
{
  const char *content_type = g_file_info_get_content_type (info);
  char *mime_type = g_content_type_get_mime_type (content_type);
  const char *ext;
  if (mime_type)
    {
      if (strncmp (mime_type, "image/", 6) == 0)
        {
          g_free (mime_type);
          return TRUE;
        }
      g_free (mime_type);
    }

  ext = get_extension (name);
  if (ext && strcmp (ext, "ply") == 0)
    return TRUE;

  return FALSE;
}
Esempio n. 23
0
static VALUE
rg_s_get_mime_type(G_GNUC_UNUSED VALUE type)
{
        return CSTR2RVAL_FREE(g_content_type_get_mime_type(RVAL2CSTR(type)));
}
Esempio n. 24
0
static gboolean prv_compute_mime_and_class(dls_task_t *task,
					   dls_async_upload_t *cb_task_data,
					   GError **error)
{
	gchar *content_type = NULL;

	if (!g_file_test(task->ut.upload.file_path,
			 G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS)) {
		DLEYNA_LOG_WARNING(
			"File %s does not exist or is not a regular file",
			task->ut.upload.file_path);

		*error = g_error_new(DLEYNA_SERVER_ERROR,
				     DLEYNA_ERROR_OBJECT_NOT_FOUND,
				     "File %s does not exist or is not a regular file",
				     task->ut.upload.file_path);
		goto on_error;
	}

	content_type = g_content_type_guess(task->ut.upload.file_path, NULL, 0,
					    NULL);

	if (!content_type) {
		DLEYNA_LOG_WARNING("Unable to determine Content Type for %s",
				   task->ut.upload.file_path);

		*error = g_error_new(DLEYNA_SERVER_ERROR, DLEYNA_ERROR_BAD_MIME,
				     "Unable to determine Content Type for %s",
				     task->ut.upload.file_path);
		goto on_error;
	}

	cb_task_data->mime_type = g_content_type_get_mime_type(content_type);
	g_free(content_type);

	if (!cb_task_data->mime_type) {
		DLEYNA_LOG_WARNING("Unable to determine MIME Type for %s",
				   task->ut.upload.file_path);

		*error = g_error_new(DLEYNA_SERVER_ERROR, DLEYNA_ERROR_BAD_MIME,
				     "Unable to determine MIME Type for %s",
				     task->ut.upload.file_path);
		goto on_error;
	}

	if (g_content_type_is_a(cb_task_data->mime_type, "image/*")) {
		cb_task_data->object_class = "object.item.imageItem";
	} else if (g_content_type_is_a(cb_task_data->mime_type, "audio/*")) {
		cb_task_data->object_class = "object.item.audioItem";
	} else if (g_content_type_is_a(cb_task_data->mime_type, "video/*")) {
		cb_task_data->object_class = "object.item.videoItem";
	} else {
		DLEYNA_LOG_WARNING("Unsupported MIME Type %s",
				   cb_task_data->mime_type);

		*error = g_error_new(DLEYNA_SERVER_ERROR, DLEYNA_ERROR_BAD_MIME,
				     "Unsupported MIME Type %s",
				     cb_task_data->mime_type);
		goto on_error;
	}

	return TRUE;

on_error:

	return FALSE;
}
Esempio n. 25
0
GList *
rut_infer_asset_tags (RutContext *ctx, GFileInfo *info, GFile *asset_file)
{
  GFile *assets_dir = g_file_new_for_path (ctx->assets_location);
  GFile *dir = g_file_get_parent (asset_file);
  char *basename;
  const char *content_type = g_file_info_get_content_type (info);
  char *mime_type = g_content_type_get_mime_type (content_type);
  const char *ext;
  GList *inferred_tags = NULL;

  while (dir && !g_file_equal (assets_dir, dir))
    {
      basename = g_file_get_basename (dir);
      inferred_tags =
        g_list_prepend (inferred_tags, (char *)g_intern_string (basename));
      g_free (basename);
      dir = g_file_get_parent (dir);
    }

  if (mime_type)
    {
      if (strncmp (mime_type, "image/", 6) == 0)
        inferred_tags =
          g_list_prepend (inferred_tags, (char *)g_intern_string ("image"));
      inferred_tags =
        g_list_prepend (inferred_tags, (char *)g_intern_string ("img"));

      if (rut_util_find_tag (inferred_tags, "normal-maps"))
        {
          inferred_tags =
            g_list_prepend (inferred_tags,
                            (char *)g_intern_string ("map"));
          inferred_tags =
            g_list_prepend (inferred_tags,
                            (char *)g_intern_string ("normal-map"));
          inferred_tags =
            g_list_prepend (inferred_tags,
                            (char *)g_intern_string ("bump-map"));
        }
      else if (rut_util_find_tag (inferred_tags, "alpha-masks"))
        {
          inferred_tags =
            g_list_prepend (inferred_tags,
                            (char *)g_intern_string ("alpha-mask"));
          inferred_tags =
            g_list_prepend (inferred_tags,
                            (char *)g_intern_string ("mask"));
        }
    }

  basename = g_file_get_basename (asset_file);
  ext = get_extension (basename);
  if (ext && strcmp (ext, "ply") == 0)
    {
      inferred_tags =
        g_list_prepend (inferred_tags, (char *)g_intern_string ("ply"));
      inferred_tags =
        g_list_prepend (inferred_tags, (char *)g_intern_string ("mesh"));
      inferred_tags =
        g_list_prepend (inferred_tags, (char *)g_intern_string ("model"));
    }
  g_free (basename);

  return inferred_tags;
}
Esempio n. 26
0
static void prv_compute_mime_and_dlna_header(const gchar *filename,
					     gchar **mime_type,
					     gchar **dlna_header,
					     GError **error)
{
	gchar *uri;
	GString *header;
	GUPnPDLNAProfile *profile;
	GUPnPDLNAProfileGuesser *guesser;
	gboolean relaxed_mode = TRUE;
	gboolean extended_mode = TRUE;
	const char *profile_name;
	const char *dlna_mime_type;
	GUPnPDLNAOperation operation;
	GUPnPDLNAFlags flags;
	GUPnPDLNAConversion conversion;
	gchar *content_type = NULL;

	*mime_type = NULL;

	*dlna_header = NULL;

	*error = NULL;

	header = g_string_new("");

	guesser = gupnp_dlna_profile_guesser_new(relaxed_mode, extended_mode);

	uri = g_filename_to_uri(filename, NULL, error);
	if (uri == NULL) {
		DLEYNA_LOG_WARNING("Unable to convert filename: %s", filename);

		if (*error) {
			DLEYNA_LOG_WARNING("Error: %s", (*error)->message);

			g_error_free(*error);
			*error = NULL;
		}

		goto on_error;
	}

	profile = gupnp_dlna_profile_guesser_guess_profile_sync(guesser,
								uri,
								5000,
								NULL,
								error);
	if (profile == NULL) {
		DLEYNA_LOG_WARNING("Unable to guess profile for URI: %s", uri);

		if (*error) {
			DLEYNA_LOG_WARNING("Error: %s", (*error)->message);

			g_error_free(*error);
			*error = NULL;
		}

		goto on_error;
	}

	profile_name = gupnp_dlna_profile_get_name(profile);
	if (profile_name != NULL)
		g_string_append_printf(header, "DLNA.ORG_PN=%s;", profile_name);

	operation = GUPNP_DLNA_OPERATION_RANGE;
	g_string_append_printf(header, "DLNA.ORG_OP=%.2x;", operation);

	conversion = GUPNP_DLNA_CONVERSION_NONE;
	g_string_append_printf(header, "DLNA.ORG_CI=%.2x;", conversion);

	dlna_mime_type = gupnp_dlna_profile_get_mime(profile);
	if (dlna_mime_type != NULL) {
		*mime_type = g_strdup(dlna_mime_type);

		flags = GUPNP_DLNA_FLAGS_BACKGROUND_TRANSFER_MODE;
		flags |= GUPNP_DLNA_FLAGS_CONNECTION_STALL;
		flags |= GUPNP_DLNA_FLAGS_DLNA_V15;

		if (g_content_type_is_a(dlna_mime_type, "image/*")) {
			flags |= GUPNP_DLNA_FLAGS_INTERACTIVE_TRANSFER_MODE;
		} else if (g_content_type_is_a(dlna_mime_type, "audio/*") ||
			   g_content_type_is_a(dlna_mime_type, "video/*")) {
			flags |= GUPNP_DLNA_FLAGS_STREAMING_TRANSFER_MODE;
		} else {
			DLEYNA_LOG_WARNING("Unsupported Mime Type: %s",
					   dlna_mime_type);

			goto on_error;
		}

		g_string_append_printf(header, "DLNA.ORG_FLAGS=%.8x", flags);
		g_string_append_printf(header, "000000000000000000000000");
	} else {
		DLEYNA_LOG_WARNING("Unable to discover mime_type");
	}

on_error:

	if (*mime_type == NULL) {
		content_type = g_content_type_guess(filename, NULL, 0, NULL);

		if (content_type != NULL) {
			*mime_type = g_content_type_get_mime_type(content_type);

			if (*mime_type == NULL)
				*error = g_error_new(DLEYNA_SERVER_ERROR,
						     DLEYNA_ERROR_BAD_MIME,
						     "Unable to determine MIME Type for %s",
						     filename);

			g_free(content_type);
		} else {
			*error = g_error_new(DLEYNA_SERVER_ERROR,
					     DLEYNA_ERROR_BAD_MIME,
					     "Unable to determine Content Type for %s",
					     filename);
		}
	}

	DLEYNA_LOG_DEBUG("contentFeatures.dlna.org: %s", header->str);

	g_object_unref(guesser);

	g_free(uri);

	if (*mime_type)
		*dlna_header = g_string_free(header, FALSE);
	else
		(void) g_string_free(header, TRUE);

	return;
}
Esempio n. 27
0
void
filefilter_gui(Tfilter * filter)
{
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	GList *tmplist, *reglist;
	GtkWidget *table, *hbox, *but, *vbox, *scrolwin;
#ifdef WIN32
	GList *mimelist = NULL;
	gchar *last_mime = NULL;
#endif

	Tfilefiltergui *ffg = g_new0(Tfilefiltergui, 1);
	ffg->curfilter = filter;
	if (filter) {
		ffg->origname = g_strdup(filter->name);
	}
	if (!filter) {
		ffg->curfilter = g_new0(Tfilter, 1);
		ffg->curfilter->name = g_strdup(_("New filter"));
	}

	DEBUG_MSG("filefilter_gui, editing filter %p\n", ffg->curfilter);
	ffg->win =
		window_full2(_("Edit filter"), GTK_WIN_POS_MOUSE, 10, G_CALLBACK(filefiltergui_destroy_lcb), ffg,
					 TRUE, NULL);
	gtk_window_set_default_size(GTK_WINDOW(ffg->win), 400, 400);
	ffg->lstore = gtk_list_store_new(3, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN);

	/* fill the list model from the currently known filetypes */
	reglist = g_content_types_get_registered();

#ifdef WIN32
	tmplist = g_list_first(reglist);
	while (tmplist) {
		mimelist = g_list_prepend(mimelist, g_content_type_get_mime_type(tmplist->data));
		tmplist = g_list_next(tmplist);
	}
	mimelist = g_list_reverse(g_list_sort(mimelist, (GCompareFunc) g_strcmp0));
	tmplist = g_list_first(mimelist);
	while (tmplist) {
		if (!last_mime || g_strcmp0(last_mime, tmplist->data) != 0) {
			GtkTreeIter it;
			last_mime = tmplist->data;
			if (MIME_ISDIR(tmplist->data)) {
				gtk_list_store_prepend(ffg->lstore, &it);
				gtk_list_store_set(ffg->lstore, &it, 0, tmplist->data, 2, 0, -1);
			}
		}
		tmplist = g_list_next(tmplist);
	}
/*	GList *winlist = NULL;
	gchar *mimetype;
	gint llen, lpos;
	while(reglist) {
		mimetype = g_content_type_get_mime_type(reglist->data);
		if ((llen = g_list_length(winlist))) {
			tmplist = g_list_copy(winlist);
			for (lpos = 0; llen != -1 && lpos < llen; lpos++) {
				if (!g_strcmp0(mimetype, tmplist->data))
					llen = -1;
				else
					tmplist = g_list_next(tmplist);
			}
			g_list_free(tmplist);
		}
		if (llen != -1)
			winlist = g_list_append(winlist, mimetype);
		reglist = g_list_next(reglist);
	}
	tmplist = g_list_first(g_list_reverse(g_list_sort(winlist, (GCompareFunc) g_strcmp0)));*/
	free_stringlist(mimelist);
#else
	tmplist = g_list_first(g_list_sort(reglist, (GCompareFunc) g_strcmp0));
	while (tmplist) {
		GtkTreeIter it;
		if (!MIME_ISDIR(tmplist->data)) {
			gtk_list_store_prepend(ffg->lstore, &it);
			gtk_list_store_set(ffg->lstore, &it, 0, tmplist->data, 2, 0, -1);
		}
		tmplist = g_list_next(tmplist);
	}
#endif

	g_list_foreach(reglist, (GFunc) g_free, NULL);
	g_list_free(reglist);
	/* add the patterns from the current filter */
	tmplist = g_list_first(ffg->curfilter->patterns);
	while (tmplist) {
		GtkTreeIter it;
		Tfilterpattern *pat = (Tfilterpattern *) tmplist->data;
		gtk_list_store_prepend(ffg->lstore, &it);
		gtk_list_store_set(ffg->lstore, &it, 0, pat->pattern, 1, NULL, 2, 1, -1);
		tmplist = g_list_next(tmplist);
	}

	ffg->in_model = gtk_tree_model_filter_new(GTK_TREE_MODEL(ffg->lstore), NULL);
	gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(ffg->in_model),
										   filefiltergui_infilter_visiblefunc, ffg, NULL);
	ffg->out_model = gtk_tree_model_filter_new(GTK_TREE_MODEL(ffg->lstore), NULL);
	gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(ffg->out_model),
										   filefiltergui_outfilter_visiblefunc, ffg, NULL);

	table = gtk_table_new(5, 4, FALSE);
	gtk_table_set_row_spacings(GTK_TABLE(table), 5);

	ffg->nameentry = dialog_entry_in_table(ffg->curfilter->name, table, 0, 1, 0, 1);

	ffg->inversecheck =
		dialog_check_button_in_table(_("Hide files that match the filter"), !ffg->curfilter->mode, table, 0,
									 1, 1, 2);

	ffg->patentry = dialog_entry_in_table("*.*", table, 2, 3, 1, 2);
	but = gtk_button_new_with_label(_("Add pattern"));
	g_signal_connect(but, "clicked", G_CALLBACK(filefiltergui_addpattern_clicked_lcb), ffg);
	gtk_table_attach(GTK_TABLE(table), but, 3, 4, 1, 2, GTK_FILL, GTK_FILL, 0, 0);

	ffg->in_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ffg->in_model));
	renderer = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes(_("Mime type"), renderer, "text", 0, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(ffg->in_view), column);
	renderer = gtk_cell_renderer_pixbuf_new();
	column = gtk_tree_view_column_new_with_attributes(_("Icon"), renderer, "pixbuf", 1, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(ffg->in_view), column);
	scrolwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(scrolwin), ffg->in_view);
	gtk_table_attach_defaults(GTK_TABLE(table), scrolwin, 2, 4, 2, 3);

	ffg->out_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ffg->out_model));
	renderer = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes(_("Mime type"), renderer, "text", 0, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(ffg->out_view), column);
	renderer = gtk_cell_renderer_pixbuf_new();
	column = gtk_tree_view_column_new_with_attributes(_("Icon"), renderer, "pixbuf", 1, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(ffg->out_view), column);
	scrolwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(scrolwin), ffg->out_view);
	gtk_table_attach_defaults(GTK_TABLE(table), scrolwin, 0, 1, 2, 3);

	vbox = gtk_vbox_new(TRUE, 5);
	but = gtk_button_new_with_label("->");
	g_signal_connect(but, "clicked", G_CALLBACK(filefiltergui_2right_clicked), ffg);
	gtk_box_pack_start(GTK_BOX(vbox), but, TRUE, TRUE, 0);
	but = gtk_button_new_with_label("<-");
	g_signal_connect(but, "clicked", G_CALLBACK(filefiltergui_2left_clicked), ffg);
	gtk_box_pack_start(GTK_BOX(vbox), but, TRUE, TRUE, 0);
	gtk_table_attach(GTK_TABLE(table), vbox, 1, 2, 2, 3, GTK_EXPAND | GTK_FILL, GTK_EXPAND, 5, 5);

#if GTK_CHECK_VERSION(3,0,0)
	hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
#else
	hbox = gtk_hbutton_box_new();
#endif
	gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
	gtk_box_set_spacing(GTK_BOX(hbox), 12);
	but = bf_stock_cancel_button(G_CALLBACK(filefiltergui_cancel_clicked), ffg);
	gtk_box_pack_start(GTK_BOX(hbox), but, FALSE, FALSE, 0);
	but = bf_stock_ok_button(G_CALLBACK(filefiltergui_ok_clicked), ffg);
	gtk_box_pack_start(GTK_BOX(hbox), but, FALSE, FALSE, 0);

	gtk_table_attach(GTK_TABLE(table), hbox, 2, 4, 4, 5, GTK_SHRINK, GTK_SHRINK, 0, 0);

	gtk_container_add(GTK_CONTAINER(ffg->win), table);
	gtk_widget_show_all(ffg->win);
}
Esempio n. 28
0
static void
add_data_tab (const gchar *demoname)
{
  gchar *resource_dir, *resource_name, *content_type, *content_mime;
  gchar **resources;
  GBytes *bytes;
  GtkWidget *widget, *label;
  guint i;

  resource_dir = g_strconcat ("/", demoname, NULL);
  resources = g_resources_enumerate_children (resource_dir, 0, NULL);
  if (resources == NULL)
    {
      g_free (resource_dir);
      return;
    }

  for (i = 0; resources[i]; i++)
    {
      resource_name = g_strconcat (resource_dir, "/", resources[i], NULL);
      bytes = g_resources_lookup_data (resource_name, 0, NULL);
      g_assert (bytes);

      content_type = g_content_type_guess (resource_name,
                                           g_bytes_get_data (bytes, NULL),
                                           g_bytes_get_size (bytes),
                                           NULL);
      content_mime = g_content_type_get_mime_type (content_type);

      /* In theory we should look at all the mime types gdk-pixbuf supports
       * and go from there, but we know what file types we've added.
       */
      if (g_content_type_is_a (content_mime, "image/png") ||
          g_content_type_is_a (content_mime, "image/gif") ||
          g_content_type_is_a (content_mime, "image/jpeg"))
        {
          widget = gtk_image_new_from_resource (resource_name);
        }
      else if (g_content_type_is_a (content_mime, "text/plain") ||
               g_content_type_is_a (content_mime, "application/x-ext-ui") ||
               g_content_type_is_a (content_mime, "text/css"))
        {
          GtkTextBuffer *buffer;
          GtkWidget *textview;

          widget = create_text (&textview, FALSE);
          buffer = gtk_text_buffer_new (NULL);
          gtk_text_buffer_set_text (buffer, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
          gtk_text_view_set_buffer (GTK_TEXT_VIEW (textview), buffer);
        }
      else
        {

          g_warning ("Don't know how to display resource '%s' of type '%s'\n", resource_name, content_mime);
          widget = NULL;
        }

      gtk_widget_show_all (widget);
      label = gtk_label_new (resources[i]);
      gtk_widget_show (label);
      gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label);
      gtk_container_child_set (GTK_CONTAINER (notebook),
                               GTK_WIDGET (widget),
                               "tab-expand", TRUE,
                               NULL);

      g_free (content_mime);
      g_free (content_type);
      g_free (resource_name);
      g_bytes_unref (bytes);
    }

  g_strfreev (resources);
  g_free (resource_dir);
}
Esempio n. 29
0
gboolean
e_composer_selection_is_image_uris (EMsgComposer *composer,
                                    GtkSelectionData *selection)
{
	gboolean all_image_uris = TRUE;
	gchar **uris;
	guint ii;

	g_return_val_if_fail (E_IS_MSG_COMPOSER (composer), FALSE);
	g_return_val_if_fail (selection != NULL, FALSE);

	uris = gtk_selection_data_get_uris (selection);

	if (!uris)
		return FALSE;

	for (ii = 0; uris[ii] != NULL; ii++) {
		GFile *file;
		GFileInfo *file_info;
		GdkPixbufLoader *loader;
		const gchar *attribute;
		const gchar *content_type;
		gchar *mime_type = NULL;

		file = g_file_new_for_uri (uris[ii]);
		attribute = G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE;

		/* XXX This blocks, but we're requesting the fast content
		 *     type (which only inspects filenames), so hopefully
		 *     it won't be noticeable.  Also, this is best effort
		 *     so we don't really care if it fails. */
		file_info = g_file_query_info (
			file, attribute, G_FILE_QUERY_INFO_NONE, NULL, NULL);

		if (file_info == NULL) {
			g_object_unref (file);
			all_image_uris = FALSE;
			break;
		}

		content_type = g_file_info_get_attribute_string (
			file_info, attribute);
		mime_type = g_content_type_get_mime_type (content_type);

		g_object_unref (file_info);
		g_object_unref (file);

		if (mime_type == NULL) {
			all_image_uris = FALSE;
			break;
		}

		/* Easy way to determine if a MIME type is a supported
		 * image format: try creating a GdkPixbufLoader for it. */
		loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, NULL);

		g_free (mime_type);

		if (loader == NULL) {
			all_image_uris = FALSE;
			break;
		}

		gdk_pixbuf_loader_close (loader, NULL);
		g_object_unref (loader);
	}

	g_strfreev (uris);

	return all_image_uris;
}