/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <glib.h>
#include <gio/gio.h>

#include "gpm-disks.h"

static void     gpm_disks_finalize   (GObject	  *object);

#define GPM_DISKS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GPM_TYPE_DISKS, GpmDisksPrivate))

struct GpmDisksPrivate
{
	GDBusProxy		*proxy;
	gchar			*cookie;
};

static gpointer gpm_disks_object = NULL;

G_DEFINE_TYPE (GpmDisks, gpm_disks, G_TYPE_OBJECT)

/**
 * gpm_disks_unregister:
 **/
static gboolean
gpm_disks_unregister (GpmDisks *disks)
{
	gboolean ret = FALSE;
	GVariant *retval = NULL;
	GError *error = NULL;

	/* no UDisks */
	if (disks->priv->proxy == NULL) {
		g_warning ("no UDisks");
		goto out;
	}

	/* set spindown timeouts */
	retval = g_dbus_proxy_call_sync (disks->priv->proxy,
					 "DriveUnsetAllSpindownTimeouts",
					g_variant_new ("(s)",
						       disks->priv->cookie),
					G_DBUS_CALL_FLAGS_NONE,
					-1, NULL, &error);
	if (retval == NULL) {
		/* abort as the DBUS method failed */
		g_warning ("failed to clear spindown timeout: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* sucess */
	ret = TRUE;
out:
	if (retval != NULL)
		g_variant_unref (retval);
	return ret;
}

/**
 * gpm_disks_register:
 **/
static gboolean
gpm_disks_register (GpmDisks *disks, gint timeout)
{
	gboolean ret = FALSE;
	GVariant *retval = NULL;
	GVariantBuilder array;
	GError *error = NULL;

	/* no UDisks */
	if (disks->priv->proxy == NULL) {
		g_warning ("no UDisks");
		goto out;
	}

	/* set spindown timeouts */
	g_variant_builder_init (&array, G_VARIANT_TYPE ("as"));
	retval = g_dbus_proxy_call_sync (disks->priv->proxy,
					 "DriveSetAllSpindownTimeouts",
					g_variant_new ("(ias)",
						       timeout,
						       &array),
					G_DBUS_CALL_FLAGS_NONE,
					-1, NULL, &error);
	if (retval == NULL) {
		/* abort as the DBUS method failed */
		g_warning ("failed to set spindown timeout: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* sucess */
	ret = TRUE;
	g_variant_get (retval, "(s)", &disks->priv->cookie);
out:
	if (retval != NULL)
		g_variant_unref (retval);
	return ret;
}

/**
 * gpm_disks_set_spindown_timeout:
 **/
gboolean
gpm_disks_set_spindown_timeout (GpmDisks *disks, gint timeout)
{
	gboolean ret = TRUE;

	/* get rid of last request */
	if (disks->priv->cookie != NULL) {
		g_debug ("unregistering %s", disks->priv->cookie);
		gpm_disks_unregister (disks);
	}

	/* is not enabled? */
	if (timeout == 0) {
		g_debug ("disk spindown disabled");
		goto out;
	}

	/* register */
	ret = gpm_disks_register (disks, timeout);
	if (ret)
		g_debug ("registered %s (%i)", disks->priv->cookie, timeout);
out:
	return ret;
}

/**
 * gpm_disks_init:
 */
static void
gpm_disks_init (GpmDisks *disks)
{
	GError *error = NULL;
	GDBusConnection *connection;

	disks->priv = GPM_DISKS_GET_PRIVATE (disks);

	/* get proxy to interface */
	connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
	disks->priv->proxy =
		g_dbus_proxy_new_sync (connection,
			G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
			G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
			NULL,
			"org.freedesktop.UDisks",
			"/org/freedesktop/UDisks",
			"org.freedesktop.UDisks",
			NULL, &error);
	if (disks->priv->proxy == NULL) {
		g_warning ("failed to setup disks proxy: %s", error->message);
		g_error_free (error);
	}
}

/**
 * gpm_disks_coldplug:
 *
 * @object: This disks instance
 */
static void
gpm_disks_finalize (GObject *object)
{
	GpmDisks *disks;
	g_return_if_fail (object != NULL);
	g_return_if_fail (GPM_IS_DISKS (object));
	disks = GPM_DISKS (object);
	g_return_if_fail (disks->priv != NULL);

	g_free (disks->priv->cookie);
	g_object_unref (disks->priv->proxy);

	G_OBJECT_CLASS (gpm_disks_parent_class)->finalize (object);
}

/**
 * gpm_disks_class_init:
 * @klass: This class instance
 **/
static void
gpm_disks_class_init (GpmDisksClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = gpm_disks_finalize;
	g_type_class_add_private (klass, sizeof (GpmDisksPrivate));
}

/**
 * gpm_disks_new:
 * Return value: new #GpmDisks instance.
 **/
GpmDisks *
gpm_disks_new (void)
{
	if (gpm_disks_object != NULL) {
		g_object_ref (gpm_disks_object);
	} else {
		gpm_disks_object = g_object_new (GPM_TYPE_DISKS, NULL);
		g_object_add_weak_pointer (gpm_disks_object, &gpm_disks_object);
	}
	return GPM_DISKS (gpm_disks_object);
}

