#include <glib.h>
#include <dbus/dbus-glib-bindings.h>
#include <libgnomevfs/gnome-vfs-utils.h>

/* gcc `pkg-config --cflags --libs glib-2.0 dbus-glib-1` reload.c -o reload */


gint main (gint argc, gchar **argv)
{
	GError           *error = NULL;
	DBusGConnection  *connection;
	DBusGPendingCall *call;
	DBusGProxy       *remote_object;
	gchar            *uri = NULL;

	g_type_init ();
	
	if (argc != 2) {
		g_print ("Usage: reload uri\n");

		return 1;
	}

	uri = gnome_vfs_make_uri_from_shell_arg (argv[1]);
	
	connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);

	if (connection == NULL) {
		g_warning (error->message);
		g_error_free (error);
		g_free (uri);
		
		return 1;
	}

	remote_object = dbus_g_proxy_new_for_name (connection,
						   "org.gnome.evince.ApplicationService",
						   "/org/gnome/evince/Evince",
						   "org.gnome.evince.Application");

	call = dbus_g_proxy_begin_call (remote_object, "ReloadURI",
					G_TYPE_STRING, uri,
					G_TYPE_INVALID);

	if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
		g_warning (error->message);
		g_clear_error (&error);
		g_free (uri);

		return 1;
	}

	g_free (uri);

	return 0;
}
