From: Michael Albinus Date: Wed, 26 Nov 2008 06:09:44 +0000 (+0000) Subject: * net/dbus.el (dbus-string-to-byte-array) X-Git-Tag: emacs-pretest-23.0.90~1505 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=82697a456dec3ec1d625edb622d65494d5a8f8f2;p=emacs.git * net/dbus.el (dbus-string-to-byte-array) (dbus-byte-array-to-string, dbus-escape-as-identifier) (dbus-unescape-from-identifier): New defuns. (dbus-handle-event): The result of a message call is a list of arguments, which must be expanded when passing to `dbus-method-return-internal'. --- diff --git a/lisp/net/dbus.el b/lisp/net/dbus.el index 53eb4b57070..5c7e719e305 100644 --- a/lisp/net/dbus.el +++ b/lisp/net/dbus.el @@ -241,6 +241,55 @@ usage: (dbus-name-owner-changed-handler service old-owner new-owner)" :session dbus-service-dbus dbus-path-dbus dbus-interface-dbus "NameOwnerChanged" 'dbus-name-owner-changed-handler)) + +;;; D-Bus type conversion. + +(defun dbus-string-to-byte-array (string) + "Transforms STRING to list (:array :byte c1 :byte c2 ...). +STRING shall be UTF8 coded." + (let (result) + (dolist (elt (string-to-list string) (append '(:array) result)) + (setq result (append result (list :byte elt)))))) + +(defun dbus-byte-array-to-string (byte-array) + "Transforms BYTE-ARRAY into UTF8 coded string. +BYTE-ARRAY must be a list of structure (c1 c2 ...)." + (apply 'string byte-array)) + +(defun dbus-escape-as-identifier (string) + "Escape an arbitrary STRING so it follows the rules for a C identifier. +The escaped string can be used as object path component, interface element +component, bus name component or member name in D-Bus. + +The escaping consists of replacing all non-alphanumerics, and the +first character if it's a digit, with an underscore and two +lower-case hex digits: + + \"0123abc_xyz\\x01\\xff\" -> \"_30123abc_5fxyz_01_ff\" + +i.e. similar to URI encoding, but with \"_\" taking the role of \"%\", +and a smaller allowed set. As a special case, \"\" is escaped to +\"_\". + +Returns the escaped string. Algorithm taken from +telepathy-glib's `tp-escape-as-identifier'." + (if (zerop (length string)) + "_" + (replace-regexp-in-string + "^[0-9]\\|[^A-Za-z0-9]" + (lambda (x) (format "_%2x" (aref x 0))) + string))) + +(defun dbus-unescape-from-identifier (string) + "Retrieve the original string from the encoded STRING. +STRING must have been coded with `dbus-escape-as-identifier'" + (if (string-equal string "_") + "" + (replace-regexp-in-string + "_.." + (lambda (x) (format "%c" (string-to-number (substring x 1) 16))) + string))) + ;;; D-Bus events. @@ -312,7 +361,7 @@ If the HANDLER returns an `dbus-error', it is propagated as return message." ;; Return a message when it is a message call. (when (= dbus-message-type-method-call (nth 2 event)) (dbus-ignore-errors - (dbus-method-return-internal + (apply 'dbus-method-return-internal (nth 1 event) (nth 3 event) (nth 4 event) result)))) ;; Error handling. (dbus-error