/*
 * test_open_file.c
 *
 * Open the specified file with the default application registered for this type of file.
 * Example:
 * $ ./test_open_file document.pdf
 *
 * Compile me with:
 * $ gcc $(pkg-config --cflags --libs gtk+-3.0) -o test_open_file test_open_file.c
 */

#include <stdlib.h>
#include <gtk/gtk.h>

int
main (int argc, char **argv)
{
	GFile *file;
	GAppInfo *app_info;
	GError *error = NULL;
	gchar *uri;

	gtk_init (&argc, &argv);

	if (argc != 2)
	{
		g_printerr ("Usage: %s <file>\n", argv[0]);
		return EXIT_FAILURE;
	}

	file = g_file_new_for_commandline_arg (argv[1]);
	app_info = g_file_query_default_handler (file, NULL, &error);

	if (error != NULL)
	{
		g_printerr ("Error app info: %s\n", error->message);
		g_error_free (error);
		return EXIT_FAILURE;
	}

	g_print ("Default application\n");
	g_print ("Name: %s\n", g_app_info_get_display_name (app_info));
	g_print ("Command line: %s\n", g_app_info_get_commandline (app_info));

	uri = g_file_get_uri (file);
	g_print ("\nOpen URI %s\n", uri);
	gtk_show_uri (NULL, uri, GDK_CURRENT_TIME, &error);

	if (error != NULL)
	{
		g_printerr ("Error show uri: %s\n", error->message);
		g_error_free (error);
		return EXIT_FAILURE;
	}

	g_object_unref (app_info);
	g_object_unref (file);

	return EXIT_SUCCESS;
}
