comparison lisp/net/dbus.el @ 99951:b265d0564f99

* 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'.
author Michael Albinus <michael.albinus@gmx.de>
date Wed, 26 Nov 2008 06:09:44 +0000
parents a7acd2697027
children 6d3b706e1a28
comparison
equal deleted inserted replaced
99950:f803581cae44 99951:b265d0564f99
240 (dbus-register-signal 240 (dbus-register-signal
241 :session dbus-service-dbus dbus-path-dbus dbus-interface-dbus 241 :session dbus-service-dbus dbus-path-dbus dbus-interface-dbus
242 "NameOwnerChanged" 'dbus-name-owner-changed-handler)) 242 "NameOwnerChanged" 'dbus-name-owner-changed-handler))
243 243
244 244
245 ;;; D-Bus type conversion.
246
247 (defun dbus-string-to-byte-array (string)
248 "Transforms STRING to list (:array :byte c1 :byte c2 ...).
249 STRING shall be UTF8 coded."
250 (let (result)
251 (dolist (elt (string-to-list string) (append '(:array) result))
252 (setq result (append result (list :byte elt))))))
253
254 (defun dbus-byte-array-to-string (byte-array)
255 "Transforms BYTE-ARRAY into UTF8 coded string.
256 BYTE-ARRAY must be a list of structure (c1 c2 ...)."
257 (apply 'string byte-array))
258
259 (defun dbus-escape-as-identifier (string)
260 "Escape an arbitrary STRING so it follows the rules for a C identifier.
261 The escaped string can be used as object path component, interface element
262 component, bus name component or member name in D-Bus.
263
264 The escaping consists of replacing all non-alphanumerics, and the
265 first character if it's a digit, with an underscore and two
266 lower-case hex digits:
267
268 \"0123abc_xyz\\x01\\xff\" -> \"_30123abc_5fxyz_01_ff\"
269
270 i.e. similar to URI encoding, but with \"_\" taking the role of \"%\",
271 and a smaller allowed set. As a special case, \"\" is escaped to
272 \"_\".
273
274 Returns the escaped string. Algorithm taken from
275 telepathy-glib's `tp-escape-as-identifier'."
276 (if (zerop (length string))
277 "_"
278 (replace-regexp-in-string
279 "^[0-9]\\|[^A-Za-z0-9]"
280 (lambda (x) (format "_%2x" (aref x 0)))
281 string)))
282
283 (defun dbus-unescape-from-identifier (string)
284 "Retrieve the original string from the encoded STRING.
285 STRING must have been coded with `dbus-escape-as-identifier'"
286 (if (string-equal string "_")
287 ""
288 (replace-regexp-in-string
289 "_.."
290 (lambda (x) (format "%c" (string-to-number (substring x 1) 16)))
291 string)))
292
293
245 ;;; D-Bus events. 294 ;;; D-Bus events.
246 295
247 (defun dbus-check-event (event) 296 (defun dbus-check-event (event)
248 "Checks whether EVENT is a well formed D-Bus event. 297 "Checks whether EVENT is a well formed D-Bus event.
249 EVENT is a list which starts with symbol `dbus-event': 298 EVENT is a list which starts with symbol `dbus-event':
310 ;; Apply the handler. 359 ;; Apply the handler.
311 (setq result (apply (nth 8 event) (nthcdr 9 event))) 360 (setq result (apply (nth 8 event) (nthcdr 9 event)))
312 ;; Return a message when it is a message call. 361 ;; Return a message when it is a message call.
313 (when (= dbus-message-type-method-call (nth 2 event)) 362 (when (= dbus-message-type-method-call (nth 2 event))
314 (dbus-ignore-errors 363 (dbus-ignore-errors
315 (dbus-method-return-internal 364 (apply 'dbus-method-return-internal
316 (nth 1 event) (nth 3 event) (nth 4 event) result)))) 365 (nth 1 event) (nth 3 event) (nth 4 event) result))))
317 ;; Error handling. 366 ;; Error handling.
318 (dbus-error 367 (dbus-error
319 ;; Return an error message when it is a message call. 368 ;; Return an error message when it is a message call.
320 (when (= dbus-message-type-method-call (nth 2 event)) 369 (when (= dbus-message-type-method-call (nth 2 event))