client: Change MessageAccess.GetFolderListing to not return raw xml
Luiz Augusto von Dentz
maintainerPATCH 1/10 client: Change MessageAccess.GetFolderListing to not return raw xml
From: Luiz Augusto von Dentz <[email protected]>
This parses the response and return as a list of dictionary where each entry is a folder and its properties, similar to what FileTransfer.ListFolder does.
--- client/map.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 2 deletions(-)
client/map.c +89 -2
@@ -29,6 +29,7 @@#include <glib.h> #include <gdbus.h> +#include "dbus.h" #include "log.h" #include "map.h"
@@ -132,6 +133,91 @@ done:dbus_message_unref (map->msg); } +static void folder_element (GMarkupParseContext *ctxt,const gchar *element, +const gchar **names,const gchar **values, +gpointer user_data,GError **gerr) +{ +DBusMessageIter dict, *iter = user_data; +gchar *key; +gint i; + +if (strcasecmp ("folder" , element) !=0 ) +return ; + +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, &dict); + + i =0 ; +for (key = (gchar *) names[i]; key; key = (gchar *) names[++i]) { + key[0 ] =g_ascii_toupper (key[0 ]); + +if (!g_str_equal ("Name" , key)) +continue ; + +obex_dbus_dict_append (&dict, key,DBUS_TYPE_STRING , + &values[i]); + } + +dbus_message_iter_close_container (iter, &dict); +} + +static const GMarkupParser folder_parser = { + folder_element, +NULL , +NULL , +NULL , +NULL +}; + +static void folder_listing_cb (struct obc_session *session, +struct obc_transfer *transfer, +GError *err,void *user_data) +{ +struct map_data *map = user_data; +GMarkupParseContext *ctxt; +DBusMessage *reply; +DBusMessageIter iter, array; +char *contents; +size_t size; +int perr; + +if (err !=NULL ) { + reply =g_dbus_create_error (map->msg, +ERROR_INTERFACE ".Failed" , +"%s" , err->message); + goto done; + } + + perr =obc_transfer_get_contents (transfer, &contents, &size); +if (perr <0 ) { + reply =g_dbus_create_error (map->msg, +ERROR_INTERFACE ".Failed" , +"Error reading contents: %s" , +strerror (-perr)); + goto done; + } + + reply =dbus_message_new_method_return (map->msg); + +dbus_message_iter_init_append (reply, &iter); +dbus_message_iter_open_container (&iter,DBUS_TYPE_ARRAY , + DBUS_TYPE_ARRAY_AS_STRING + DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING + DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING +DBUS_DICT_ENTRY_END_CHAR_AS_STRING , &array); + ctxt =g_markup_parse_context_new (&folder_parser,0 , &array,NULL ); +g_markup_parse_context_parse (ctxt, contents, size,NULL ); +g_markup_parse_context_free (ctxt); +dbus_message_iter_close_container (&iter, &array); +g_free (contents); + +done: +g_dbus_send_message (conn, reply); +dbus_message_unref (map->msg); +} +static DBusMessage *map_get_folder_listing (DBusConnection *connection,DBusMessage *message,void *user_data) {
@@ -144,7 +230,8 @@ static DBusMessage *map_get_folder_listing(DBusConnection *connection,if (transfer ==NULL ) goto fail; -if (obc_session_queue (map->session, transfer, buffer_cb, map, &err)) { +if (obc_session_queue (map->session, transfer, folder_listing_cb, map, + &err)) { map->msg =dbus_message_ref (message);return NULL ; }
@@ -196,7 +283,7 @@ static const GDBusMethodTable map_methods[] = { map_setpath) }, {GDBUS_ASYNC_METHOD ("GetFolderListing" , GDBUS_ARGS({ "dummy", "a{ss}" }), - GDBUS_ARGS({ "content", "s" }), + GDBUS_ARGS({ "content", "aa{sv}" }), map_get_folder_listing) }, { GDBUS_ASYNC_METHOD("GetMessageListing", GDBUS_ARGS({ "folder", "s" }, { "dummy", "a{ss}" }),
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 2/10 test: Update map-client to the changes in GetFolderListing
From: Luiz Augusto von Dentz <[email protected]>
This makes map-client to print only the name of the folders similar to what ftp-client does.
--- test/map-client | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
test/map-client +2 -1
@@ -50,7 +50,8 @@ if __name__ == '__main__': set_folder(map, options.new_dir) if options.ls_dir: - print map.GetFolderListing(dict()) + for i in map.GetFolderListing(dict()): + print "%s/" % (i["Name"]) if options.ls_msg is not None: print map.GetMessageListing(options.ls_msg, dict())
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 3/10 client-doc: Add documentation of MessageAccess.GetFolderListing
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
--- doc/client-api.txt | 9 +++++++++ 1 file changed, 9 insertions(+)
doc/client-api.txt +9 -0
@@ -352,6 +352,15 @@ Methods void SetFolder(string name) Set working directory for current session, *name* may be the directory name or '..[/dir]'. + array{dict} GetFolderListing(dict filter) + + Returns a dictionary containing information about + the current folder content. + + The following keys are defined: + + string Name : Folder name + Transfer hierarchy ==================
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 4/10 client: Change MessageAccess.GetMessageListing to not return raw xml
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
This parses the response and return as a list of dictionary where each entry is a message and its properties,
--- client/map.c | 175 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 138 insertions(+), 37 deletions(-)
client/map.c +138 -37
@@ -98,41 +98,6 @@ static DBusMessage *map_setpath(DBusConnection *connection,return NULL ; } -static void buffer_cb (struct obc_session *session, -struct obc_transfer *transfer, -GError *err,void *user_data) -{ -struct map_data *map = user_data; -DBusMessage *reply; -char *contents; -size_t size; -int perr; - -if (err !=NULL ) { - reply =g_dbus_create_error (map->msg, -ERROR_INTERFACE ".Failed" , -"%s" , err->message); - goto done; - } - - perr =obc_transfer_get_contents (transfer, &contents, &size); -if (perr <0 ) { - reply =g_dbus_create_error (map->msg, -ERROR_INTERFACE ".Failed" , -"Error reading contents: %s" , -strerror (-perr)); - goto done; - } - - reply =g_dbus_create_reply (map->msg,DBUS_TYPE_STRING , &contents, -DBUS_TYPE_INVALID ); - -g_free (contents); -done: -g_dbus_send_message (conn, reply); -dbus_message_unref (map->msg); -} -static void folder_element (GMarkupParseContext *ctxt,const gchar *element,const gchar **names,const gchar **values,gpointer user_data,GError **gerr)
@@ -243,6 +208,141 @@ fail:return reply; } +static void msg_element (GMarkupParseContext *ctxt,const gchar *element, +const gchar **names,const gchar **values, +gpointer user_data,GError **gerr) +{ +DBusMessageIter dict, *iter = user_data; +gchar *key; +gint i; + +if (strcasecmp ("msg" , element) !=0 ) +return ; + +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, &dict); + + i =0 ; +for (key = (gchar *) names[i]; key; key = (gchar *) names[++i]) { +if (strcasecmp (key,"handle" ) ==0 ) { +obex_dbus_dict_append (&dict,"Handle" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"subject" ) ==0 ) { +obex_dbus_dict_append (&dict,"Subject" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"datetime" ) ==0 ) { +obex_dbus_dict_append (&dict,"Timestamp" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"sender_name" ) ==0 ) { +obex_dbus_dict_append (&dict,"Sender" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"sender_addressing" ) ==0 ) { +obex_dbus_dict_append (&dict,"SenderAddress" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"replyto_addressing" ) ==0 ) { +obex_dbus_dict_append (&dict,"ReplyTo" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"recipient_name" ) ==0 ) { +obex_dbus_dict_append (&dict,"Recipient" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"recipient_addressing" ) ==0 ) { +obex_dbus_dict_append (&dict,"RecipientAddress" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"type" ) ==0 ) { +obex_dbus_dict_append (&dict,"Type" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"reception_status" ) ==0 ) { +obex_dbus_dict_append (&dict,"Status" , +DBUS_TYPE_STRING , &values[i]); + }else if (strcasecmp (key,"size" ) ==0 ) { +guint64 value =g_ascii_strtoll (values[i],NULL ,10 ); + +obex_dbus_dict_append (&dict,"Size" , +DBUS_TYPE_UINT64 , &value); + }else if (strcasecmp (key,"priority" ) ==0 ) { +gboolean value =strcasecmp (values[i],"no" ); + +obex_dbus_dict_append (&dict,"Priority" , +DBUS_TYPE_BOOLEAN , &value); + }else if (strcasecmp (key,"read" ) ==0 ) { +gboolean value =strcasecmp (values[i],"no" ); + +obex_dbus_dict_append (&dict,"Read" , +DBUS_TYPE_BOOLEAN , &value); + }else if (strcasecmp (key,"sent" ) ==0 ) { +gboolean value =strcasecmp (values[i],"no" ); + +obex_dbus_dict_append (&dict,"Sent" , +DBUS_TYPE_BOOLEAN , &value); + }else if (strcasecmp (key,"protected" ) ==0 ) { +gboolean value =strcasecmp (values[i],"no" ); + +obex_dbus_dict_append (&dict,"Protected" , +DBUS_TYPE_BOOLEAN , &value); + } + + } + +dbus_message_iter_close_container (iter, &dict); +} + +static const GMarkupParser msg_parser = { + msg_element, +NULL , +NULL , +NULL , +NULL +}; + +static void message_listing_cb (struct obc_session *session, +struct obc_transfer *transfer, +GError *err,void *user_data) +{ +struct map_data *map = user_data; +GMarkupParseContext *ctxt; +DBusMessage *reply; +DBusMessageIter iter, array; +char *contents; +size_t size; +int perr; + +if (err !=NULL ) { + reply =g_dbus_create_error (map->msg, +ERROR_INTERFACE ".Failed" , +"%s" , err->message); + goto done; + } + + perr =obc_transfer_get_contents (transfer, &contents, &size); +if (perr <0 ) { + reply =g_dbus_create_error (map->msg, +ERROR_INTERFACE ".Failed" , +"Error reading contents: %s" , +strerror (-perr)); + goto done; + } + + reply =dbus_message_new_method_return (map->msg); + +dbus_message_iter_init_append (reply, &iter); +dbus_message_iter_open_container (&iter,DBUS_TYPE_ARRAY , + DBUS_TYPE_ARRAY_AS_STRING + DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING + DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING +DBUS_DICT_ENTRY_END_CHAR_AS_STRING , &array); + ctxt =g_markup_parse_context_new (&msg_parser,0 , &array,NULL ); +g_markup_parse_context_parse (ctxt, contents, size,NULL ); +g_markup_parse_context_free (ctxt); +dbus_message_iter_close_container (&iter, &array); +g_free (contents); + +done: +g_dbus_send_message (conn, reply); +dbus_message_unref (map->msg); +} +static DBusMessage *map_get_message_listing (DBusConnection *connection,DBusMessage *message,void *user_data) {
@@ -265,7 +365,8 @@ static DBusMessage *map_get_message_listing(DBusConnection *connection,if (transfer ==NULL ) goto fail; -if (obc_session_queue (map->session, transfer, buffer_cb, map, &err)) { +if (obc_session_queue (map->session, transfer, message_listing_cb, map, + &err)) { map->msg =dbus_message_ref (message);return NULL ; }
@@ -287,7 +388,7 @@ static const GDBusMethodTable map_methods[] = { map_get_folder_listing) }, {GDBUS_ASYNC_METHOD ("GetMessageListing" , GDBUS_ARGS({ "folder", "s" }, { "dummy", "a{ss}" }), - GDBUS_ARGS({ "messages", "s" }), + GDBUS_ARGS({ "messages", "aa{sv}" }), map_get_message_listing) }, { } };
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 5/10 test: Update map-client to the changes in GetMessageListing
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
This makes map-client to print the message properties dictionary
--- test/map-client | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
test/map-client +4 -1
@@ -54,6 +54,9 @@ if __name__ == '__main__': print "%s/" % (i["Name"]) if options.ls_msg is not None: - print map.GetMessageListing(options.ls_msg, dict()) + for i in map.GetMessageListing(options.ls_msg, dict()): + for (key, value) in i.items(): + print("%s = %s" % (key, value)) + print "\n" mainloop.run()
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 6/10 client-doc: Add documentation of MessageAccess.GetMessageListing
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
--- doc/client-api.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
doc/client-api.txt +24 -0
@@ -361,6 +361,30 @@ Methods void SetFolder(string name) string Name : Folder name + array{dict} GetMessageListing(string folder, dict filter) + + Returns a dictionary containing information about + the messages in the given folder. + + The following keys are defined: + + string Handle : Message handle + string Subject : Message subject + string Timestamp: Message timestamp + string Sender: Message sender name + string SenderAddress: Message sender address + string ReplyTo: Message Reply-To address + string Recipient: Message recipient name + string RecipientAddress: Message recipient + address + string Type: Message type + uint64 Size: Message size in bytes + string Status: Message reception status + boolean Priority: Message priority flag + boolean Read: Message read flag + boolean Sent: Message sent flag + boolean Protected: Message protected flag + Transfer hierarchy ==================
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 7/10 client: Use filter instead of dummy as argument name in MAP
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
This is aligned with the documentation that uses filter as well.
--- client/map.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
client/map.c +2 -2
@@ -383,11 +383,11 @@ static const GDBusMethodTable map_methods[] = { GDBUS_ARGS({ "name", "s" }), NULL, map_setpath) }, {GDBUS_ASYNC_METHOD ("GetFolderListing" , - GDBUS_ARGS({ "dummy", "a{ss}" }), + GDBUS_ARGS({ "filter", "a{ss}" }), GDBUS_ARGS({ "content", "aa{sv}" }), map_get_folder_listing) }, { GDBUS_ASYNC_METHOD("GetMessageListing", - GDBUS_ARGS({ "folder", "s" }, { "dummy", "a{ss}" }), + GDBUS_ARGS({ "folder", "s" }, { "filter", "a{ss}" }), GDBUS_ARGS({ "messages", "aa{sv}" }), map_get_message_listing) }, { }
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 8/10 client-doc: Add documentation of MessageAccess.GetMessage
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
GetMessage takes the message handle, which can be discovered using GetMessageListing, and file location where the contents of the message will be stored.
It returns the transfer object and its properties so the user application can track progress of the operation.
--- doc/client-api.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
doc/client-api.txt +16 -0
@@ -385,6 +385,22 @@ Methods void SetFolder(string name) boolean Sent: Message sent flag boolean Protected: Message protected flag + object, dict GetMessage(string handle, string targetfile) + + Download message by its handle, the message handle can + be discovered using GetMessageListing, and store it + in the target file. + + If an empty target file is given, a temporary file + will be automatically generated. + + The returned path represents the newly created transfer, + which should be used to find out if the content has been + successfully transferred or if the operation fails. + + The properties of this transfer are also returned along + with the object path, to avoid a call to GetProperties. + Transfer hierarchy ==================
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 9/10 client: Add MessageAccess.GetMessage implementation
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
--- client/map.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
client/map.c +37 -0
@@ -378,6 +378,38 @@ fail:return reply; } +static DBusMessage *map_get_message (DBusConnection *connection, +DBusMessage *message,void *user_data) +{ +struct map_data *map = user_data; +struct obc_transfer *transfer; +const char *handle, *target_file; +GError *err =NULL ; +DBusMessage *reply; + +if (dbus_message_get_args (message,NULL , +DBUS_TYPE_STRING , &handle, +DBUS_TYPE_STRING , &target_file, +DBUS_TYPE_INVALID ) == FALSE) +return g_dbus_create_error (message, +ERROR_INTERFACE ".InvalidArguments" ,NULL ); + + transfer =obc_transfer_get ("x-bt/message" , handle, target_file, &err); +if (transfer ==NULL ) + goto fail; + +if (!obc_session_queue (map->session, transfer,NULL ,NULL , &err)) + goto fail; + +return obc_transfer_create_dbus_reply (transfer, message); + +fail: + reply =g_dbus_create_error (message,ERROR_INTERFACE ".Failed" ,"%s" , + err->message); +g_error_free (err); +return reply; +} +static const GDBusMethodTable map_methods[] = { { GDBUS_ASYNC_METHOD("SetFolder", GDBUS_ARGS({ "name", "s" }), NULL,
@@ -390,6 +422,11 @@ static const GDBusMethodTable map_methods[] = { GDBUS_ARGS({ "folder", "s" }, { "filter", "a{ss}" }), GDBUS_ARGS({ "messages", "aa{sv}" }), map_get_message_listing) }, + {GDBUS_METHOD ("GetMessage" , + GDBUS_ARGS({ "handle", "s" }, { "file", "s" }), + GDBUS_ARGS({ "transfer", "o" }, + { "properties", "a{sv}" }), + map_get_message) }, { } };
Signature · 1 line
1.7.10.2
Luiz Augusto von Dentz
maintainerPATCH 10/10 test: Add support for MessageAccess.GetMessage to map-client
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
This adds -g/--get <handle> option to map-client so it downloads and prints the contents of the message.
In addition add MapClient class to handle transfer signals similar to what other scripts do.
--- test/map-client | 93 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 10 deletions(-)
test/map-client +83 -10
@@ -2,6 +2,8 @@ import gobject +import sys +import os import dbus import dbus.mainloop.glib from optparse import OptionParser
@@ -16,12 +18,85 @@ def parse_options(): parser.add_option("-v", "--verbose", action="store_true", dest="verbose") parser.add_option("-L", "--lsmsg", action="store", dest="ls_msg", help="List messages in supplied CWD subdir") + parser.add_option("-g", "--get", action="store", dest="get_msg", + help="List messages in supplied CWD subdir") return parser.parse_args() def set_folder(session, new_dir): session.SetFolder(new_dir) +class MapClient: + def __init__(self, session_path, verbose=False): + self.progress = 0 + self.transfer_path = None + self.props = dict() + self.verbose = verbose + bus = dbus.SessionBus() + obj = bus.get_object("org.bluez.obex.client", session_path) + self.session = dbus.Interface(obj, "org.bluez.obex.Session") + self.map = dbus.Interface(obj, "org.bluez.obex.MessageAccess") + bus.add_signal_receiver(self.transfer_complete, + dbus_interface="org.bluez.obex.Transfer", + signal_name="Complete", + path_keyword="path") + bus.add_signal_receiver(self.transfer_error, + dbus_interface="org.bluez.obex.Transfer", + signal_name="Error", + path_keyword="path") + + def create_transfer_reply(self, reply): + (path, properties) = reply + self.transfer_path = path + self.props[path] = properties + if self.verbose: + print "Transfer created: %s (file %s)" % (path, + properties["Filename"]) + + def generic_reply(self): + if self.verbose: + print "Operation succeeded" + + def error(self, err): + print err + mainloop.quit() + + def transfer_complete(self, path): + if path != self.transfer_path: + return + if self.verbose: + print "Transfer finished" + properties = self.props.get(path) + if properties == None: + return + f = open(properties["Filename"], "r") + os.remove(properties["Filename"]) + print f.readlines() + + def transfer_error(self, code, message, path): + if path != self.transfer_path: + return + print "Transfer finished with error %s: %s" % (code, message) + mainloop.quit() + + def set_folder(self, new_dir): + self.map.SetFolder(new_dir) + + def list_folders(self): + for i in self.map.GetFolderListing(dict()): + print "%s/" % (i["Name"]) + + def list_messages(self, folder): + for i in self.map.GetMessageListing(folder, dict()): + for (key, value) in i.items(): + print("%s = %s" % (key, value)) + print "\n" + + def get_message(self, handle): + self.map.GetMessage(handle, "", + reply_handler=self.create_transfer_reply, + error_handler=self.error) + if __name__ == '__main__': dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
@@ -40,23 +115,21 @@ if __name__ == '__main__': client = dbus.Interface(bus.get_object("org.bluez.obex.client", "/"), "org.bluez.obex.Client") + print "Creating Session" path = client.CreateSession(options.device, { "Target": "map" }) - obj = bus.get_object("org.bluez.obex.client", path) - session = dbus.Interface(obj, "org.bluez.obex.Session") - map = dbus.Interface(obj, "org.bluez.obex.MessageAccess") + map_client = MapClient(path, options.verbose) if options.new_dir: - set_folder(map, options.new_dir) + map_client.set_folder(options.new_dir) if options.ls_dir: - for i in map.GetFolderListing(dict()): - print "%s/" % (i["Name"]) + map_client.list_folders() if options.ls_msg is not None: - for i in map.GetMessageListing(options.ls_msg, dict()): - for (key, value) in i.items(): - print("%s = %s" % (key, value)) - print "\n" + map_client.list_messages(options.ls_msg) + + if options.get_msg is not None: + map_client.get_message(options.get_msg) mainloop.run()
Signature · 1 line
1.7.10.2