legacy/experimental/ecdb/trunk/src/ecdb_hal.c

254 lines
6.2 KiB
C
Raw Normal View History

/* vim: set sw=3 ts=3 sts=3 expandtab: */
#include "ecdb.h"
void _ecdb_hal_device_all_cb(void *data, void *reply_data, DBusError *error);
void _ecdb_hal_device_handle(void *data, void *reply_data, DBusError *error);
void _ecdb_hal_volume_removed_cb(void *data, DBusMessage *msg);
void _ecdb_hal_volume_added_cb(void *data, DBusMessage *msg);
void _ecdb_hal_volume_cb(void *data, void *reply_data, DBusError *error);
void _ecdb_hal_volume_handle(void *data, void *reply_data, DBusError *error);
void _ecdb_hal_unmount_cb(void *data, void *reply_data, DBusError *error);
void
_ecdb_hal_volume_handle(void *data, void *reply_data, DBusError *error)
{
E_Hal_Device_Get_All_Properties_Return *ret;
char *udi, *str;
int err;
ret = reply_data;
udi = data;
if ((!ret) || (!udi))
{
EINA_ERROR_PWARN("NULL udi or reply!\n");
return;
}
if (dbus_error_is_set(error))
{
/* XXX Handle this error here */
dbus_error_free(error);
return;
}
if ((e_hal_property_bool_get(ret, "volume.is_disc", &err)) && (!err))
{
str = e_hal_property_string_get(ret, "info.parent", &err);
if ((str) && (!err))
{
e_hal_device_get_all_properties(em->conn, str, _ecdb_hal_device_handle,
str);
}
}
FREE(udi);
}
void
_ecdb_hal_device_handle(void *data, void *reply_data, DBusError *error)
{
E_Hal_Device_Get_All_Properties_Return *ret;
Eina_List *l;
Ecdb_Drive_Info *drive;
char *udi, *str;
int err = 0;
ret = reply_data;
udi = data;
if ((!ret) || (!udi))
{
EINA_ERROR_PWARN("NULL udi or reply!\n");
return;
}
if (dbus_error_is_set(error))
{
/* XXX Handle this here */
dbus_error_free(error);
return;
}
str = e_hal_property_string_get(ret, "block.device", &err);
if (!str)
return;
if (!err)
{
EINA_LIST_FOREACH(em->drives, l, drive)
{
if (!strcmp(drive->location, str))
{
if (!ecdb_update_drive_info(drive, ret, udi))
EINA_ERROR_PWARN("Couldn't update old drive info!\n");
printf("Recreating old...\n");
ecdb_print_drive_info();
return;
}
}
/*
* Create the drive information here, as we will have already
* returned if it previously exists
*/
if (!ecdb_aquire_drive_info(ret, str, udi))
{
EINA_ERROR_PWARN("Couldn't create new drive info!\n");
return;
}
printf("Creating new...\n");
ecdb_print_drive_info();
}
}
void
_ecdb_hal_device_removed_cb(void *data __UNUSED__, DBusMessage *msg)
{
DBusError err;
Eina_List *l;
Ecdb_Drive_Info *drive;
char *udi;
dbus_error_init(&err);
dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &udi,
DBUS_TYPE_INVALID);
printf("Removed: %s\n", udi);
/*
* 1. Find the drive that has been removed.
* 2. Update it with libburn
*/
}
void
_ecdb_hal_device_added_cb(void *data __UNUSED__, DBusMessage *msg)
{
DBusError err;
char *udi;
dbus_error_init(&err);
dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &udi,
DBUS_TYPE_INVALID);
/* XXX Do something with the error above? */
e_hal_device_query_capability(em->conn, udi, "volume",
_ecdb_hal_volume_cb,
(void *)strdup(udi));
}
void
_ecdb_hal_volume_cb(void *data, void *reply_data, DBusError *error)
{
char *udi;
E_Hal_Device_Query_Capability_Return *ret;
udi = data;
ret = reply_data;
if (!ret)
return;
if (dbus_error_is_set(error))
{
/* XXX Handle this here */
dbus_error_free(error);
return;
}
if (ret->boolean)
{
e_hal_device_get_all_properties(em->conn, udi, _ecdb_hal_device_handle,
udi);
}
}
void
_ecdb_hal_device_all_cb(void *data, void *reply_data, DBusError *error)
{
char *udi;
Eina_List *l;
E_Hal_Manager_Find_Device_By_Capability_Return *ret;
udi = data;
ret = reply_data;
if ((!ret) || (!ret->strings))
return;
if (dbus_error_is_set(error))
{
/* XXX Handle this here */
dbus_error_free(error);
return;
}
EINA_LIST_FOREACH(ret->strings, l, udi)
{
e_hal_device_get_all_properties(em->conn, udi, _ecdb_hal_device_handle,
(void *)strdup(udi));
}
}
int
ecdb_hal_init(void)
{
em->conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
if (!em->conn)
{
EINA_ERROR_PWARN("Error connecting to system bus!\n");
return 0;
}
em->dev_added = e_dbus_signal_handler_add(em->conn, "org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
"DeviceAdded",
_ecdb_hal_device_added_cb, NULL);
em->dev_removed = e_dbus_signal_handler_add(em->conn,
"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
"DeviceRemoved",
_ecdb_hal_device_removed_cb, NULL);
e_hal_manager_find_device_by_capability(em->conn, "storage.cdrom",
_ecdb_hal_device_all_cb, NULL);
return 1;
}
void
ecdb_hal_shutdown(void)
{
e_dbus_signal_handler_del(em->conn, em->dev_added);
e_dbus_signal_handler_del(em->conn, em->dev_removed);
e_dbus_connection_close(em->conn);
}
void
ecdb_hal_request_unmount(Ecdb_Drive_Info *drive)
{
e_hal_device_volume_unmount(em->conn, drive->udi, NULL,
_ecdb_hal_unmount_cb, drive);
}
void
_ecdb_hal_unmount_cb(void *data, void *reply_data __UNUSED__, DBusError *error)
{
Ecdb_Drive_Info *drive;
drive = data;
if (dbus_error_is_set(error))
{
EINA_ERROR_PWARN("Cannot unmount volume!\n");
dbus_error_free(error);
}
}