obex-client: Port MAP module to use D-Bus properties interface

This commit is contained in:
Luiz Augusto von Dentz 2012-12-14 23:31:57 +02:00 committed by Johan Hedberg
parent 07c8df9844
commit 82a76393fe

View File

@ -107,7 +107,7 @@ struct map_msg {
uint64_t size; uint64_t size;
char *status; char *status;
uint8_t flags; uint8_t flags;
DBusMessage *msg; GDBusPendingPropertySet pending;
}; };
struct map_parser { struct map_parser {
@ -421,176 +421,205 @@ static void set_message_status_cb(struct obc_session *session,
GError *err, void *user_data) GError *err, void *user_data)
{ {
struct map_msg *msg = user_data; struct map_msg *msg = user_data;
DBusMessage *reply;
if (err != NULL) { if (err != NULL) {
reply = g_dbus_create_error(msg->msg, g_dbus_pending_property_error(msg->pending,
ERROR_INTERFACE ".Failed", ERROR_INTERFACE ".Failed",
"%s", err->message); "%s", err->message);
goto done; goto done;
} }
reply = dbus_message_new_method_return(msg->msg); g_dbus_pending_property_success(msg->pending);
if (reply == NULL) {
reply = g_dbus_create_error(msg->msg,
ERROR_INTERFACE ".Failed",
"%s", err->message);
}
done: done:
g_dbus_send_message(conn, reply); msg->pending = 0;
dbus_message_unref(msg->msg);
msg->msg = NULL;
} }
static DBusMessage *map_msg_set_property(DBusConnection *connection, static gboolean get_subject(const GDBusPropertyTable *property,
DBusMessage *message, DBusMessageIter *iter, void *data)
void *user_data)
{ {
struct map_msg *msg = user_data; struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &msg->subject);
return TRUE;
}
static gboolean get_timestamp(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &msg->timestamp);
return TRUE;
}
static gboolean get_sender(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &msg->sender);
return TRUE;
}
static gboolean get_sender_address(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
&msg->sender_address);
return TRUE;
}
static gboolean get_replyto(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &msg->replyto);
return TRUE;
}
static gboolean get_recipient(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &msg->recipient);
return TRUE;
}
static gboolean get_recipient_address(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
&msg->recipient_address);
return TRUE;
}
static gboolean get_type(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &msg->type);
return TRUE;
}
static gboolean get_size(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct map_msg *msg = data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &msg->size);
return TRUE;
}
static gboolean get_flag(const GDBusPropertyTable *property,
DBusMessageIter *iter, uint8_t flag,
void *data)
{
struct map_msg *msg = data;
dbus_bool_t value = (msg->flags & flag) != 0;
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &value);
return TRUE;
}
static gboolean get_priority(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
return get_flag(property, iter, MAP_MSG_FLAG_PRIORITY, data);
}
static gboolean get_read(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
return get_flag(property, iter, MAP_MSG_FLAG_READ, data);
}
static gboolean get_sent(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
return get_flag(property, iter, MAP_MSG_FLAG_SENT, data);
}
static gboolean get_protected(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
return get_flag(property, iter, MAP_MSG_FLAG_PROTECTED, data);
}
static void set_status(const GDBusPropertyTable *property,
DBusMessageIter *iter, GDBusPendingPropertySet id,
uint8_t status, void *data)
{
struct map_msg *msg = data;
struct obc_transfer *transfer; struct obc_transfer *transfer;
char *property; gboolean value;
gboolean status;
GError *err = NULL; GError *err = NULL;
DBusMessage *reply;
GObexApparam *apparam; GObexApparam *apparam;
char contents[2]; char contents[2];
int op;
DBusMessageIter args, variant;
dbus_message_iter_init(message, &args); if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_BOOLEAN)
if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_STRING) return g_dbus_pending_property_error(id,
return g_dbus_create_error(message, ERROR_INTERFACE ".InvalidArguments",
ERROR_INTERFACE ".InvalidArguments", NULL); "Invalid arguments in method call");
dbus_message_iter_get_basic(&args, &property); dbus_message_iter_get_basic(iter, &value);
dbus_message_iter_next(&args);
if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_VARIANT)
return g_dbus_create_error(message,
ERROR_INTERFACE ".InvalidArguments", NULL);
dbus_message_iter_recurse(&args, &variant);
if (dbus_message_iter_get_arg_type(&variant) != DBUS_TYPE_BOOLEAN)
return g_dbus_create_error(message,
ERROR_INTERFACE ".InvalidArguments", NULL);
dbus_message_iter_get_basic(&variant, &status);
/* MAP supports modifying only these two properties. */
if (property && strcasecmp(property, "Read") == 0) {
op = STATUS_READ;
if (status)
msg->flags |= MAP_MSG_FLAG_READ;
else
msg->flags &= ~MAP_MSG_FLAG_READ;
} else if (property && strcasecmp(property, "Deleted") == 0)
op = STATUS_DELETE;
else {
return g_dbus_create_error(message,
ERROR_INTERFACE ".InvalidArguments", NULL);
}
contents[0] = FILLER_BYTE; contents[0] = FILLER_BYTE;
contents[1] = '\0'; contents[1] = '\0';
transfer = obc_transfer_put("x-bt/messageStatus", msg->handle, NULL, transfer = obc_transfer_put("x-bt/messageStatus", msg->handle, NULL,
contents, contents, sizeof(contents), &err);
sizeof(contents), &err);
if (transfer == NULL) if (transfer == NULL)
goto fail; goto fail;
apparam = g_obex_apparam_set_uint8(NULL, MAP_AP_STATUSINDICATOR, apparam = g_obex_apparam_set_uint8(NULL, MAP_AP_STATUSINDICATOR,
op);
apparam = g_obex_apparam_set_uint8(apparam, MAP_AP_STATUSVALUE,
status); status);
apparam = g_obex_apparam_set_uint8(apparam, MAP_AP_STATUSVALUE,
value);
obc_transfer_set_apparam(transfer, apparam); obc_transfer_set_apparam(transfer, apparam);
if (!obc_session_queue(msg->data->session, transfer, if (!obc_session_queue(msg->data->session, transfer,
set_message_status_cb, msg, &err)) set_message_status_cb, msg, &err))
goto fail; goto fail;
msg->msg = dbus_message_ref(message); msg->pending = id;
return NULL; return;
fail: fail:
reply = g_dbus_create_error(message, ERROR_INTERFACE ".Failed", "%s", g_dbus_pending_property_error(id, ERROR_INTERFACE ".Failed", "%s",
err->message); err->message);
g_error_free(err); g_error_free(err);
return reply;
} }
static DBusMessage *map_msg_get_properties(DBusConnection *connection, static void set_read(const GDBusPropertyTable *property,
DBusMessage *message, DBusMessageIter *iter, GDBusPendingPropertySet id,
void *user_data) void *data)
{ {
struct map_msg *msg = user_data; set_status(property, iter, id, STATUS_READ, data);
GError *err = NULL; }
DBusMessage *reply;
DBusMessageIter iter, data_array;
gboolean flag;
reply = dbus_message_new_method_return(message); static void set_deleted(const GDBusPropertyTable *property,
if (reply == NULL) { DBusMessageIter *iter, GDBusPendingPropertySet id,
reply = g_dbus_create_error(message, void *data)
ERROR_INTERFACE ".Failed", {
NULL); set_status(property, iter, id, STATUS_DELETE, data);
goto done;
}
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_VARIANT_AS_STRING
DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
&data_array);
obex_dbus_dict_append(&data_array, "Subject",
DBUS_TYPE_STRING, &msg->subject);
obex_dbus_dict_append(&data_array, "Timestamp",
DBUS_TYPE_STRING, &msg->timestamp);
obex_dbus_dict_append(&data_array, "Sender",
DBUS_TYPE_STRING, &msg->sender);
obex_dbus_dict_append(&data_array, "SenderAddress",
DBUS_TYPE_STRING, &msg->sender_address);
obex_dbus_dict_append(&data_array, "ReplyTo",
DBUS_TYPE_STRING, &msg->replyto);
obex_dbus_dict_append(&data_array, "Recipient",
DBUS_TYPE_STRING, &msg->recipient);
obex_dbus_dict_append(&data_array, "RecipientAddress",
DBUS_TYPE_STRING, &msg->recipient_address);
obex_dbus_dict_append(&data_array, "Type",
DBUS_TYPE_STRING, &msg->type);
obex_dbus_dict_append(&data_array, "Status",
DBUS_TYPE_STRING, &msg->status);
obex_dbus_dict_append(&data_array, "Size",
DBUS_TYPE_UINT64, &msg->size);
flag = (msg->flags & MAP_MSG_FLAG_PRIORITY) != 0;
obex_dbus_dict_append(&data_array, "Priority",
DBUS_TYPE_BOOLEAN, &flag);
flag = (msg->flags & MAP_MSG_FLAG_READ) != 0;
obex_dbus_dict_append(&data_array, "Read",
DBUS_TYPE_BOOLEAN, &flag);
flag = (msg->flags & MAP_MSG_FLAG_SENT) != 0;
obex_dbus_dict_append(&data_array, "Sent",
DBUS_TYPE_BOOLEAN, &flag);
flag = (msg->flags & MAP_MSG_FLAG_PROTECTED) != 0;
obex_dbus_dict_append(&data_array, "Protected",
DBUS_TYPE_BOOLEAN, &flag);
dbus_message_iter_close_container(&iter, &data_array);
done:
if (err)
g_error_free(err);
return reply;
} }
static const GDBusMethodTable map_msg_methods[] = { static const GDBusMethodTable map_msg_methods[] = {
@ -600,13 +629,24 @@ static const GDBusMethodTable map_msg_methods[] = {
GDBUS_ARGS({ "transfer", "o" }, GDBUS_ARGS({ "transfer", "o" },
{ "properties", "a{sv}" }), { "properties", "a{sv}" }),
map_msg_get) }, map_msg_get) },
{ GDBUS_METHOD("GetProperties", { }
NULL, };
GDBUS_ARGS({ "properties", "a{sv}" }),
map_msg_get_properties) }, static const GDBusPropertyTable map_msg_properties[] = {
{ GDBUS_ASYNC_METHOD("SetProperty", { "Subject", "s", get_subject },
GDBUS_ARGS({ "property", "sv" }), NULL, { "Timestamp", "s", get_timestamp },
map_msg_set_property) }, { "Sender", "s", get_sender },
{ "SenderAddress", "s", get_sender_address },
{ "ReplyTo", "s", get_replyto },
{ "Recipient", "s", get_recipient },
{ "RecipientAddress", "s", get_recipient_address },
{ "Type", "s", get_type },
{ "Size", "t", get_size },
{ "Priority", "b", get_priority },
{ "Read", "b", get_read, set_read },
{ "Sent", "b", get_sent },
{ "Protected", "b", get_sent },
{ "Deleted", "b", NULL, set_deleted },
{ } { }
}; };
@ -621,7 +661,8 @@ static struct map_msg *map_msg_create(struct map_data *data, const char *handle)
handle); handle);
if (!g_dbus_register_interface(conn, msg->path, MAP_MSG_INTERFACE, if (!g_dbus_register_interface(conn, msg->path, MAP_MSG_INTERFACE,
map_msg_methods, NULL, NULL, map_msg_methods, NULL,
map_msg_properties,
msg, map_msg_free)) { msg, map_msg_free)) {
map_msg_free(msg); map_msg_free(msg);
return NULL; return NULL;