Add 2010 to copyright years.
[bpt/emacs.git] / lisp / net / tramp-gvfs.el
1 ;;; tramp-gvfs.el --- Tramp access functions for GVFS daemon
2
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Access functions for the GVFS daemon from Tramp. Tested with GVFS
26 ;; 1.0.2 (Ubuntu 8.10, Gnome 2.24). It has been reported also to run
27 ;; with GVFS 0.2.5 (Ubuntu 8.04, Gnome 2.22), but there is an
28 ;; incompatibility with the mount_info structure, which has been
29 ;; worked around.
30
31 ;; All actions to mount a remote location, and to retrieve mount
32 ;; information, are performed by D-Bus messages. File operations
33 ;; themselves are performed via the mounted filesystem in ~/.gvfs.
34 ;; Consequently, GNU Emacs 23.1 with enabled D-Bus bindings is a
35 ;; precondition.
36
37 ;; The GVFS D-Bus interface is said to be instable. There are even no
38 ;; introspection data. The interface, as discovered during
39 ;; development time, is given in respective comments.
40
41 ;; The customer option `tramp-gvfs-methods' contains the list of
42 ;; supported connection methods. Per default, these are "dav",
43 ;; "davs", "obex" and "synce". Note that with "obex" it might be
44 ;; necessary to pair with the other bluetooth device, if it hasn't
45 ;; been done already. There might be also some few seconds delay in
46 ;; discovering available bluetooth devices.
47
48 ;; Other possible connection methods are "ftp", "sftp" and "smb".
49 ;; When one of these methods is added to the list, the remote access
50 ;; for that method is performed via GVFS instead of the native Tramp
51 ;; implementation.
52
53 ;; GVFS offers even more connection methods. The complete list of
54 ;; connection methods of the actual GVFS implementation can be
55 ;; retrieved by:
56 ;;
57 ;; (message
58 ;; "%s"
59 ;; (mapcar
60 ;; 'car
61 ;; (dbus-call-method
62 ;; :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
63 ;; tramp-gvfs-interface-mounttracker "listMountableInfo")))
64
65 ;; Note that all other connection methods are not tested, beside the
66 ;; ones offered for customization in `tramp-gvfs-methods'. If you
67 ;; request an additional connection method to be supported, please
68 ;; drop me a note.
69
70 ;; For hostname completion, information is retrieved either from the
71 ;; bluez daemon (for the "obex" method), the hal daemon (for the
72 ;; "synce" method), or from the zeroconf daemon (for the "dav",
73 ;; "davs", and "sftp" methods). The zeroconf daemon is pre-configured
74 ;; to discover services in the "local" domain. If another domain
75 ;; shall be used for discovering services, the customer option
76 ;; `tramp-gvfs-zeroconf-domain' can be set accordingly.
77
78 ;; Restrictions:
79
80 ;; * The current GVFS implementation does not allow to write on the
81 ;; remote bluetooth device via OBEX.
82 ;;
83 ;; * Two shares of the same SMB server cannot be mounted in parallel.
84
85 ;;; Code:
86
87 ;; D-Bus support in the Emacs core can be disabled with configuration
88 ;; option "--without-dbus". Declare used subroutines and variables.
89 (declare-function dbus-call-method "dbusbind.c")
90 (declare-function dbus-call-method-asynchronously "dbusbind.c")
91 (declare-function dbus-get-unique-name "dbusbind.c")
92 (declare-function dbus-register-method "dbusbind.c")
93 (declare-function dbus-register-signal "dbusbind.c")
94
95 ;; Pacify byte-compiler
96 (eval-when-compile
97 (require 'cl)
98 (require 'custom))
99
100 (require 'tramp)
101 (require 'dbus)
102 (require 'url-parse)
103 (require 'zeroconf)
104
105 (defcustom tramp-gvfs-methods '("dav" "davs" "obex" "synce")
106 "*List of methods for remote files, accessed with GVFS."
107 :group 'tramp
108 :version "23.2"
109 :type '(repeat (choice (const "dav")
110 (const "davs")
111 (const "ftp")
112 (const "obex")
113 (const "sftp")
114 (const "smb")
115 (const "synce"))))
116
117 ;; Add a default for `tramp-default-user-alist'. Rule: For the SYNCE
118 ;; method, no user is chosen.
119 (add-to-list 'tramp-default-user-alist
120 '("synce" nil nil))
121
122 (defcustom tramp-gvfs-zeroconf-domain "local"
123 "*Zeroconf domain to be used for discovering services, like host names."
124 :group 'tramp
125 :version "23.2"
126 :type 'string)
127
128 ;; Add the methods to `tramp-methods', in order to allow minibuffer
129 ;; completion.
130 (eval-after-load "tramp-gvfs"
131 '(when (featurep 'tramp-gvfs)
132 (dolist (elt tramp-gvfs-methods)
133 (unless (assoc elt tramp-methods)
134 (add-to-list 'tramp-methods (cons elt nil))))))
135
136 (defconst tramp-gvfs-mount-point
137 (file-name-as-directory (expand-file-name ".gvfs" "~/"))
138 "The directory name, fuses mounts remote ressources.")
139
140 (defconst tramp-gvfs-path-tramp (concat dbus-path-emacs "/Tramp")
141 "The preceeding object path for own objects.")
142
143 (defconst tramp-gvfs-service-daemon "org.gtk.vfs.Daemon"
144 "The well known name of the GVFS daemon.")
145
146 ;; Check that GVFS is available.
147 (unless (dbus-ping :session tramp-gvfs-service-daemon 100)
148 (throw 'tramp-loading nil))
149
150 (defconst tramp-gvfs-path-mounttracker "/org/gtk/vfs/mounttracker"
151 "The object path of the GVFS daemon.")
152
153 (defconst tramp-gvfs-interface-mounttracker "org.gtk.vfs.MountTracker"
154 "The mount tracking interface in the GVFS daemon.")
155
156 ;; <interface name='org.gtk.vfs.MountTracker'>
157 ;; <method name='listMounts'>
158 ;; <arg name='mount_info_list'
159 ;; type='a{sosssssbay{aya{say}}}'
160 ;; direction='out'/>
161 ;; </method>
162 ;; <method name='mountLocation'>
163 ;; <arg name='mount_spec' type='{aya{say}}' direction='in'/>
164 ;; <arg name='dbus_id' type='s' direction='in'/>
165 ;; <arg name='object_path' type='o' direction='in'/>
166 ;; </method>
167 ;; <signal name='mounted'>
168 ;; <arg name='mount_info'
169 ;; type='{sosssssbay{aya{say}}}'/>
170 ;; </signal>
171 ;; <signal name='unmounted'>
172 ;; <arg name='mount_info'
173 ;; type='{sosssssbay{aya{say}}}'/>
174 ;; </signal>
175 ;; </interface>
176 ;;
177 ;; STRUCT mount_info
178 ;; STRING dbus_id
179 ;; OBJECT_PATH object_path
180 ;; STRING display_name
181 ;; STRING stable_name
182 ;; STRING x_content_types Since GVFS 1.0 only !!!
183 ;; STRING icon
184 ;; STRING prefered_filename_encoding
185 ;; BOOLEAN user_visible
186 ;; ARRAY BYTE fuse_mountpoint
187 ;; STRUCT mount_spec
188 ;; ARRAY BYTE mount_prefix
189 ;; ARRAY
190 ;; STRUCT mount_spec_item
191 ;; STRING key (server, share, type, user, host, port)
192 ;; ARRAY BYTE value
193
194 (defconst tramp-gvfs-interface-mountoperation "org.gtk.vfs.MountOperation"
195 "Used by the dbus-proxying implementation of GMountOperation.")
196
197 ;; <interface name='org.gtk.vfs.MountOperation'>
198 ;; <method name='askPassword'>
199 ;; <arg name='message' type='s' direction='in'/>
200 ;; <arg name='default_user' type='s' direction='in'/>
201 ;; <arg name='default_domain' type='s' direction='in'/>
202 ;; <arg name='flags' type='u' direction='in'/>
203 ;; <arg name='handled' type='b' direction='out'/>
204 ;; <arg name='aborted' type='b' direction='out'/>
205 ;; <arg name='password' type='s' direction='out'/>
206 ;; <arg name='username' type='s' direction='out'/>
207 ;; <arg name='domain' type='s' direction='out'/>
208 ;; <arg name='anonymous' type='b' direction='out'/>
209 ;; <arg name='password_save' type='u' direction='out'/>
210 ;; </method>
211 ;; <method name='askQuestion'>
212 ;; <arg name='message' type='s' direction='in'/>
213 ;; <arg name='choices' type='as' direction='in'/>
214 ;; <arg name='handled' type='b' direction='out'/>
215 ;; <arg name='aborted' type='b' direction='out'/>
216 ;; <arg name='choice' type='u' direction='out'/>
217 ;; </method>
218 ;; </interface>
219
220 ;; The following flags are used in "askPassword". They are defined in
221 ;; /usr/include/glib-2.0/gio/gioenums.h.
222
223 (defconst tramp-gvfs-password-need-password 1
224 "Operation requires a password.")
225
226 (defconst tramp-gvfs-password-need-username 2
227 "Operation requires a username.")
228
229 (defconst tramp-gvfs-password-need-domain 4
230 "Operation requires a domain.")
231
232 (defconst tramp-gvfs-password-saving-supported 8
233 "Operation supports saving settings.")
234
235 (defconst tramp-gvfs-password-anonymous-supported 16
236 "Operation supports anonymous users.")
237
238 (defconst tramp-bluez-service "org.bluez"
239 "The well known name of the BLUEZ service.")
240
241 (defconst tramp-bluez-interface-manager "org.bluez.Manager"
242 "The manager interface of the BLUEZ daemon.")
243
244 ;; <interface name='org.bluez.Manager'>
245 ;; <method name='DefaultAdapter'>
246 ;; <arg type='o' direction='out'/>
247 ;; </method>
248 ;; <method name='FindAdapter'>
249 ;; <arg type='s' direction='in'/>
250 ;; <arg type='o' direction='out'/>
251 ;; </method>
252 ;; <method name='ListAdapters'>
253 ;; <arg type='ao' direction='out'/>
254 ;; </method>
255 ;; <signal name='AdapterAdded'>
256 ;; <arg type='o'/>
257 ;; </signal>
258 ;; <signal name='AdapterRemoved'>
259 ;; <arg type='o'/>
260 ;; </signal>
261 ;; <signal name='DefaultAdapterChanged'>
262 ;; <arg type='o'/>
263 ;; </signal>
264 ;; </interface>
265
266 (defconst tramp-bluez-interface-adapter "org.bluez.Adapter"
267 "The adapter interface of the BLUEZ daemon.")
268
269 ;; <interface name='org.bluez.Adapter'>
270 ;; <method name='GetProperties'>
271 ;; <arg type='a{sv}' direction='out'/>
272 ;; </method>
273 ;; <method name='SetProperty'>
274 ;; <arg type='s' direction='in'/>
275 ;; <arg type='v' direction='in'/>
276 ;; </method>
277 ;; <method name='RequestMode'>
278 ;; <arg type='s' direction='in'/>
279 ;; </method>
280 ;; <method name='ReleaseMode'/>
281 ;; <method name='RequestSession'/>
282 ;; <method name='ReleaseSession'/>
283 ;; <method name='StartDiscovery'/>
284 ;; <method name='StopDiscovery'/>
285 ;; <method name='ListDevices'>
286 ;; <arg type='ao' direction='out'/>
287 ;; </method>
288 ;; <method name='CreateDevice'>
289 ;; <arg type='s' direction='in'/>
290 ;; <arg type='o' direction='out'/>
291 ;; </method>
292 ;; <method name='CreatePairedDevice'>
293 ;; <arg type='s' direction='in'/>
294 ;; <arg type='o' direction='in'/>
295 ;; <arg type='s' direction='in'/>
296 ;; <arg type='o' direction='out'/>
297 ;; </method>
298 ;; <method name='CancelDeviceCreation'>
299 ;; <arg type='s' direction='in'/>
300 ;; </method>
301 ;; <method name='RemoveDevice'>
302 ;; <arg type='o' direction='in'/>
303 ;; </method>
304 ;; <method name='FindDevice'>
305 ;; <arg type='s' direction='in'/>
306 ;; <arg type='o' direction='out'/>
307 ;; </method>
308 ;; <method name='RegisterAgent'>
309 ;; <arg type='o' direction='in'/>
310 ;; <arg type='s' direction='in'/>
311 ;; </method>
312 ;; <method name='UnregisterAgent'>
313 ;; <arg type='o' direction='in'/>
314 ;; </method>
315 ;; <signal name='DeviceCreated'>
316 ;; <arg type='o'/>
317 ;; </signal>
318 ;; <signal name='DeviceRemoved'>
319 ;; <arg type='o'/>
320 ;; </signal>
321 ;; <signal name='DeviceFound'>
322 ;; <arg type='s'/>
323 ;; <arg type='a{sv}'/>
324 ;; </signal>
325 ;; <signal name='PropertyChanged'>
326 ;; <arg type='s'/>
327 ;; <arg type='v'/>
328 ;; </signal>
329 ;; <signal name='DeviceDisappeared'>
330 ;; <arg type='s'/>
331 ;; </signal>
332 ;; </interface>
333
334 (defcustom tramp-bluez-discover-devices-timeout 60
335 "Defines seconds since last bluetooth device discovery before rescanning.
336 A value of 0 would require an immediate discovery during hostname
337 completion, nil means to use always cached values for discovered
338 devices."
339 :group 'tramp
340 :version "23.2"
341 :type '(choice (const nil) integer))
342
343 (defvar tramp-bluez-discovery nil
344 "Indicator for a running bluetooth device discovery.
345 It keeps the timestamp of last discovery.")
346
347 (defvar tramp-bluez-devices nil
348 "Alist of detected bluetooth devices.
349 Every entry is a list (NAME ADDRESS).")
350
351 (defconst tramp-hal-service "org.freedesktop.Hal"
352 "The well known name of the HAL service.")
353
354 (defconst tramp-hal-path-manager "/org/freedesktop/Hal/Manager"
355 "The object path of the HAL daemon manager.")
356
357 (defconst tramp-hal-interface-manager "org.freedesktop.Hal.Manager"
358 "The manager interface of the HAL daemon.")
359
360 (defconst tramp-hal-interface-device "org.freedesktop.Hal.Device"
361 "The device interface of the HAL daemon.")
362
363 \f
364 ;; New handlers should be added here.
365 (defconst tramp-gvfs-file-name-handler-alist
366 '(
367 (access-file . ignore)
368 (add-name-to-file . tramp-gvfs-handle-copy-file)
369 ;; `byte-compiler-base-file-name' performed by default handler.
370 (copy-file . tramp-gvfs-handle-copy-file)
371 (delete-directory . tramp-gvfs-handle-delete-directory)
372 (delete-file . tramp-gvfs-handle-delete-file)
373 ;; `diff-latest-backup-file' performed by default handler.
374 (directory-file-name . tramp-handle-directory-file-name)
375 (directory-files . tramp-gvfs-handle-directory-files)
376 (directory-files-and-attributes
377 . tramp-gvfs-handle-directory-files-and-attributes)
378 (dired-call-process . ignore)
379 (dired-compress-file . ignore)
380 (dired-uncache . tramp-handle-dired-uncache)
381 ;; `executable-find' is not official yet. performed by default handler.
382 (expand-file-name . tramp-gvfs-handle-expand-file-name)
383 ;; `file-accessible-directory-p' performed by default handler.
384 (file-attributes . tramp-gvfs-handle-file-attributes)
385 (file-directory-p . tramp-smb-handle-file-directory-p)
386 (file-executable-p . tramp-gvfs-handle-file-executable-p)
387 (file-exists-p . tramp-gvfs-handle-file-exists-p)
388 (file-local-copy . tramp-gvfs-handle-file-local-copy)
389 (file-remote-p . tramp-handle-file-remote-p)
390 ;; `file-modes' performed by default handler.
391 (file-name-all-completions . tramp-gvfs-handle-file-name-all-completions)
392 (file-name-as-directory . tramp-handle-file-name-as-directory)
393 (file-name-completion . tramp-handle-file-name-completion)
394 (file-name-directory . tramp-handle-file-name-directory)
395 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
396 ;; `file-name-sans-versions' performed by default handler.
397 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
398 (file-ownership-preserved-p . ignore)
399 (file-readable-p . tramp-gvfs-handle-file-readable-p)
400 (file-regular-p . tramp-handle-file-regular-p)
401 (file-symlink-p . tramp-handle-file-symlink-p)
402 ;; `file-truename' performed by default handler.
403 (file-writable-p . tramp-gvfs-handle-file-writable-p)
404 (find-backup-file-name . tramp-handle-find-backup-file-name)
405 ;; `find-file-noselect' performed by default handler.
406 ;; `get-file-buffer' performed by default handler.
407 (insert-directory . tramp-gvfs-handle-insert-directory)
408 (insert-file-contents . tramp-gvfs-handle-insert-file-contents)
409 (load . tramp-handle-load)
410 (make-directory . tramp-gvfs-handle-make-directory)
411 (make-directory-internal . ignore)
412 (make-symbolic-link . ignore)
413 (process-file . tramp-gvfs-handle-process-file)
414 (rename-file . tramp-gvfs-handle-rename-file)
415 (set-file-modes . tramp-gvfs-handle-set-file-modes)
416 (set-visited-file-modtime . tramp-gvfs-handle-set-visited-file-modtime)
417 (shell-command . tramp-gvfs-handle-shell-command)
418 (start-file-process . tramp-gvfs-handle-start-file-process)
419 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
420 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
421 (vc-registered . ignore)
422 (verify-visited-file-modtime
423 . tramp-gvfs-handle-verify-visited-file-modtime)
424 (write-region . tramp-gvfs-handle-write-region)
425 )
426 "Alist of handler functions for Tramp GVFS method.
427 Operations not mentioned here will be handled by the default Emacs primitives.")
428
429 (defun tramp-gvfs-file-name-p (filename)
430 "Check if it's a filename handled by the GVFS daemon."
431 (and (tramp-tramp-file-p filename)
432 (let ((method
433 (tramp-file-name-method (tramp-dissect-file-name filename))))
434 (and (stringp method) (member method tramp-gvfs-methods)))))
435
436 (defun tramp-gvfs-file-name-handler (operation &rest args)
437 "Invoke the GVFS related OPERATION.
438 First arg specifies the OPERATION, second arg is a list of arguments to
439 pass to the OPERATION."
440 (let ((fn (assoc operation tramp-gvfs-file-name-handler-alist)))
441 (if fn
442 (save-match-data (apply (cdr fn) args))
443 (tramp-run-real-handler operation args))))
444
445 ;; This might be moved to tramp.el. It shall be the first file name
446 ;; handler.
447 (add-to-list 'tramp-foreign-file-name-handler-alist
448 (cons 'tramp-gvfs-file-name-p 'tramp-gvfs-file-name-handler))
449
450 (defmacro with-tramp-dbus-call-method
451 (vec synchronous bus service path interface method &rest args)
452 "Apply a D-Bus call on bus BUS.
453
454 If SYNCHRONOUS is non-nil, the call is synchronously. Otherwise,
455 it is an asynchronous call, with `ignore' as callback function.
456
457 The other arguments have the same meaning as with `dbus-call-method'
458 or `dbus-call-method-asynchronously'. Additionally, the call
459 will be traced by Tramp with trace level 6."
460 `(let ((func (if ,synchronous
461 'dbus-call-method 'dbus-call-method-asynchronously))
462 (args (append (list ,bus ,service ,path ,interface ,method)
463 (if ,synchronous (list ,@args) (list 'ignore ,@args))))
464 result)
465 (tramp-message ,vec 6 "%s %s" func args)
466 (setq result (apply func args))
467 (tramp-message ,vec 6 "\n%s" result)
468 result))
469
470 (put 'with-tramp-dbus-call-method 'lisp-indent-function 2)
471 (put 'with-tramp-dbus-call-method 'edebug-form-spec '(form symbolp body))
472 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-dbus-call-method\\>"))
473
474 (defmacro with-tramp-gvfs-error-message (filename handler &rest args)
475 "Apply a Tramp GVFS `handler'.
476 In case of an error, modify the error message by replacing
477 `filename' with its GVFS mounted name."
478 `(let ((fuse-file-name (regexp-quote (tramp-gvfs-fuse-file-name ,filename)))
479 elt)
480 (condition-case err
481 (apply ,handler (list ,@args))
482 (error
483 (setq elt (cdr err))
484 (while elt
485 (when (and (stringp (car elt))
486 (string-match fuse-file-name (car elt)))
487 (setcar elt (replace-match ,filename t t (car elt))))
488 (setq elt (cdr elt)))
489 (signal (car err) (cdr err))))))
490
491 (put 'with-tramp-gvfs-error-message 'lisp-indent-function 2)
492 (put 'with-tramp-gvfs-error-message 'edebug-form-spec '(form symbolp body))
493 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-gvfs-error-message\\>"))
494
495 (defvar tramp-gvfs-dbus-event-vector nil
496 "Current Tramp file name to be used, as vector.
497 It is needed when D-Bus signals or errors arrive, because there
498 is no information where to trace the message.")
499
500 (defun tramp-gvfs-dbus-event-error (event err)
501 "Called when a D-Bus error message arrives, see `dbus-event-error-hooks'."
502 (when tramp-gvfs-dbus-event-vector
503 ;(tramp-cleanup-connection tramp-gvfs-dbus-event-vector)
504 (tramp-message tramp-gvfs-dbus-event-vector 10 "%S" event)
505 (tramp-error tramp-gvfs-dbus-event-vector 'file-error "%s" (cadr err))))
506
507 (add-hook 'dbus-event-error-hooks 'tramp-gvfs-dbus-event-error)
508
509 \f
510 ;; File name primitives.
511
512 (defun tramp-gvfs-handle-copy-file
513 (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
514 "Like `copy-file' for Tramp files."
515 (copy-file
516 (if (tramp-gvfs-file-name-p filename)
517 (tramp-gvfs-fuse-file-name filename)
518 filename)
519 (if (tramp-gvfs-file-name-p newname)
520 (tramp-gvfs-fuse-file-name newname)
521 newname)
522 ok-if-already-exists keep-date preserve-uid-gid))
523
524 (defun tramp-gvfs-handle-delete-directory (directory &optional recursive)
525 "Like `delete-directory' for Tramp files."
526 (tramp-compat-delete-directory
527 (tramp-gvfs-fuse-file-name directory) recursive))
528
529 (defun tramp-gvfs-handle-delete-file (filename)
530 "Like `delete-file' for Tramp files."
531 (delete-file (tramp-gvfs-fuse-file-name filename)))
532
533 (defun tramp-gvfs-handle-directory-files
534 (directory &optional full match nosort)
535 "Like `directory-files' for Tramp files."
536 (let ((fuse-file-name (tramp-gvfs-fuse-file-name directory)))
537 (mapcar
538 (lambda (x)
539 (if (string-match fuse-file-name x)
540 (replace-match directory t t x)
541 x))
542 (directory-files fuse-file-name full match nosort))))
543
544 (defun tramp-gvfs-handle-directory-files-and-attributes
545 (directory &optional full match nosort id-format)
546 "Like `directory-files-and-attributes' for Tramp files."
547 (let ((fuse-file-name (tramp-gvfs-fuse-file-name directory)))
548 (mapcar
549 (lambda (x)
550 (when (string-match fuse-file-name (car x))
551 (setcar x (replace-match directory t t (car x))))
552 x)
553 (directory-files-and-attributes
554 fuse-file-name full match nosort id-format))))
555
556 (defun tramp-gvfs-handle-expand-file-name (name &optional dir)
557 "Like `expand-file-name' for Tramp files."
558 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
559 (setq dir (or dir default-directory "/"))
560 ;; Unless NAME is absolute, concat DIR and NAME.
561 (unless (file-name-absolute-p name)
562 (setq name (concat (file-name-as-directory dir) name)))
563 ;; If NAME is not a Tramp file, run the real handler.
564 (if (not (tramp-tramp-file-p name))
565 (tramp-run-real-handler 'expand-file-name (list name nil))
566 ;; Dissect NAME.
567 (with-parsed-tramp-file-name name nil
568 ;; Tilde expansion is not possible.
569 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
570 (tramp-error
571 v 'file-error
572 "Cannot expand tilde in file `%s'" name))
573 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
574 (setq localname (concat "/" localname)))
575 ;; We do not pass "/..".
576 (if (string-equal "smb" method)
577 (when (string-match "^/[^/]+\\(/\\.\\./?\\)" localname)
578 (setq localname (replace-match "/" t t localname 1)))
579 (when (string-match "^/\\.\\./?" localname)
580 (setq localname (replace-match "/" t t localname))))
581 ;; There might be a double slash. Remove this.
582 (while (string-match "//" localname)
583 (setq localname (replace-match "/" t t localname)))
584 ;; No tilde characters in file name, do normal
585 ;; `expand-file-name' (this does "/./" and "/../").
586 (tramp-make-tramp-file-name
587 method user host
588 (tramp-run-real-handler
589 'expand-file-name (list localname))))))
590
591 (defun tramp-gvfs-handle-file-attributes (filename &optional id-format)
592 "Like `file-attributes' for Tramp files."
593 (file-attributes (tramp-gvfs-fuse-file-name filename) id-format))
594
595 (defun tramp-gvfs-handle-file-executable-p (filename)
596 "Like `file-executable-p' for Tramp files."
597 (file-executable-p (tramp-gvfs-fuse-file-name filename)))
598
599 (defun tramp-gvfs-handle-file-exists-p (filename)
600 "Like `file-exists-p' for Tramp files."
601 (file-exists-p (tramp-gvfs-fuse-file-name filename)))
602
603 (defun tramp-gvfs-handle-file-local-copy (filename)
604 "Like `file-local-copy' for Tramp files."
605 (with-parsed-tramp-file-name filename nil
606 (let ((tmpfile (tramp-compat-make-temp-file filename)))
607 (unless (file-exists-p filename)
608 (tramp-error
609 v 'file-error
610 "Cannot make local copy of non-existing file `%s'" filename))
611 (copy-file filename tmpfile t t)
612 tmpfile)))
613
614 (defun tramp-gvfs-handle-file-name-all-completions (filename directory)
615 "Like `file-name-all-completions' for Tramp files."
616 (unless (save-match-data (string-match "/" filename))
617 (file-name-all-completions filename (tramp-gvfs-fuse-file-name directory))))
618
619 (defun tramp-gvfs-handle-file-readable-p (filename)
620 "Like `file-readable-p' for Tramp files."
621 (file-readable-p (tramp-gvfs-fuse-file-name filename)))
622
623 (defun tramp-gvfs-handle-file-writable-p (filename)
624 "Like `file-writable-p' for Tramp files."
625 (file-writable-p (tramp-gvfs-fuse-file-name filename)))
626
627 (defun tramp-gvfs-handle-insert-directory
628 (filename switches &optional wildcard full-directory-p)
629 "Like `insert-directory' for Tramp files."
630 (insert-directory
631 (tramp-gvfs-fuse-file-name filename) switches wildcard full-directory-p))
632
633 (defun tramp-gvfs-handle-insert-file-contents
634 (filename &optional visit beg end replace)
635 "Like `insert-file-contents' for Tramp files."
636 (unwind-protect
637 (let ((fuse-file-name (tramp-gvfs-fuse-file-name filename))
638 (result
639 (insert-file-contents
640 (tramp-gvfs-fuse-file-name filename) visit beg end replace)))
641 (when (string-match fuse-file-name (car result))
642 (setcar result (replace-match filename t t (car result))))
643 result)
644 (setq buffer-file-name filename)))
645
646 (defun tramp-gvfs-handle-make-directory (dir &optional parents)
647 "Like `make-directory' for Tramp files."
648 (condition-case err
649 (with-tramp-gvfs-error-message dir 'make-directory
650 (tramp-gvfs-fuse-file-name dir) parents)
651 ;; Error case. Let's try it with the GVFS utilities.
652 (error
653 (with-parsed-tramp-file-name dir nil
654 (tramp-message v 4 "`make-directory' failed, trying `gvfs-mkdir'")
655 (unless
656 (zerop
657 (tramp-local-call-process
658 "gvfs-mkdir" nil (tramp-get-buffer v) nil
659 (tramp-gvfs-url-file-name dir)))
660 (signal (car err) (cdr err)))))))
661
662 (defun tramp-gvfs-handle-process-file
663 (program &optional infile destination display &rest args)
664 "Like `process-file' for Tramp files."
665 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
666 (apply 'call-process program infile destination display args)))
667
668 (defun tramp-gvfs-handle-rename-file
669 (filename newname &optional ok-if-already-exists)
670 "Like `rename-file' for Tramp files."
671 (rename-file
672 (if (tramp-gvfs-file-name-p filename)
673 (tramp-gvfs-fuse-file-name filename)
674 filename)
675 (if (tramp-gvfs-file-name-p newname)
676 (tramp-gvfs-fuse-file-name newname)
677 newname)
678 ok-if-already-exists))
679
680 (defun tramp-gvfs-handle-set-file-modes (filename mode)
681 "Like `set-file-modes' for Tramp files."
682 (with-tramp-gvfs-error-message filename 'set-file-modes
683 (tramp-gvfs-fuse-file-name filename) mode))
684
685 (defun tramp-gvfs-handle-set-visited-file-modtime (&optional time-list)
686 "Like `set-visited-file-modtime' for Tramp files."
687 (let ((buffer-file-name (tramp-gvfs-fuse-file-name (buffer-file-name))))
688 (set-visited-file-modtime time-list)))
689
690 (defun tramp-gvfs-handle-shell-command
691 (command &optional output-buffer error-buffer)
692 "Like `shell-command' for Tramp files."
693 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
694 (shell-command command output-buffer error-buffer)))
695
696 (defun tramp-gvfs-handle-start-file-process (name buffer program &rest args)
697 "Like `start-file-process' for Tramp files."
698 (let ((default-directory (tramp-gvfs-fuse-file-name default-directory)))
699 (apply 'start-process name buffer program args)))
700
701 (defun tramp-gvfs-handle-verify-visited-file-modtime (buf)
702 "Like `verify-visited-file-modtime' for Tramp files."
703 (with-current-buffer buf
704 (let ((buffer-file-name (tramp-gvfs-fuse-file-name (buffer-file-name))))
705 (verify-visited-file-modtime buf))))
706
707 (defun tramp-gvfs-handle-write-region
708 (start end filename &optional append visit lockname confirm)
709 "Like `write-region' for Tramp files."
710 (with-parsed-tramp-file-name filename nil
711 (condition-case err
712 (with-tramp-gvfs-error-message filename 'write-region
713 start end (tramp-gvfs-fuse-file-name filename)
714 append visit lockname confirm)
715
716 ;; Error case. Let's try it with the GVFS utilities.
717 (error
718 (let ((tmpfile (tramp-compat-make-temp-file filename)))
719 (tramp-message v 4 "`write-region' failed, trying `gvfs-save'")
720 (write-region start end tmpfile)
721 (unwind-protect
722 (unless
723 (zerop
724 (tramp-local-call-process
725 "gvfs-save" tmpfile (tramp-get-buffer v) nil
726 (tramp-gvfs-url-file-name filename)))
727 (signal (car err) (cdr err)))
728 (delete-file tmpfile)))))
729
730 ;; Set file modification time.
731 (when (or (eq visit t) (stringp visit))
732 (set-visited-file-modtime (nth 5 (file-attributes filename))))
733
734 ;; The end.
735 (when (or (eq visit t) (null visit) (stringp visit))
736 (tramp-message v 0 "Wrote %s" filename))
737 (run-hooks 'tramp-handle-write-region-hook)))
738
739 \f
740 ;; File name conversions.
741
742 (defun tramp-gvfs-url-file-name (filename)
743 "Return FILENAME in URL syntax."
744 (url-recreate-url
745 (if (tramp-tramp-file-p filename)
746 (with-parsed-tramp-file-name (file-truename filename) nil
747 (when (string-match tramp-user-with-domain-regexp user)
748 (setq user
749 (concat (match-string 2 user) ";" (match-string 2 user))))
750 (url-parse-make-urlobj
751 method user nil
752 (tramp-file-name-real-host v) (tramp-file-name-port v) localname))
753 (url-parse-make-urlobj "file" nil nil nil nil (file-truename filename)))))
754
755 (defun tramp-gvfs-object-path (filename)
756 "Create a D-Bus object path from FILENAME."
757 (expand-file-name (dbus-escape-as-identifier filename) tramp-gvfs-path-tramp))
758
759 (defun tramp-gvfs-file-name (object-path)
760 "Retrieve file name from D-Bus OBJECT-PATH."
761 (dbus-unescape-from-identifier
762 (replace-regexp-in-string "^.*/\\([^/]+\\)$" "\\1" object-path)))
763
764 (defun tramp-gvfs-fuse-file-name (filename)
765 "Return FUSE file name, which is directly accessible."
766 (with-parsed-tramp-file-name (expand-file-name filename) nil
767 (tramp-gvfs-maybe-open-connection v)
768 (let ((fuse-mountpoint
769 (tramp-get-file-property v "/" "fuse-mountpoint" nil)))
770 (unless fuse-mountpoint
771 (tramp-error
772 v 'file-error "There is no FUSE mount point for `%s'" filename))
773 ;; We must remove the share from the local name.
774 (when (and (string-equal "smb" method) (string-match "/[^/]+" localname))
775 (setq localname (replace-match "" t t localname)))
776 (concat tramp-gvfs-mount-point fuse-mountpoint localname))))
777
778 (defun tramp-bluez-address (device)
779 "Return bluetooth device address from a given bluetooth DEVICE name."
780 (when (stringp device)
781 (if (string-match tramp-ipv6-regexp device)
782 (match-string 0 device)
783 (cadr (assoc device (tramp-bluez-list-devices))))))
784
785 (defun tramp-bluez-device (address)
786 "Return bluetooth device name from a given bluetooth device ADDRESS.
787 ADDRESS can have the form \"xx:xx:xx:xx:xx:xx\" or \"[xx:xx:xx:xx:xx:xx]\"."
788 (when (stringp address)
789 (while (string-match "[][]" address)
790 (setq address (replace-match "" t t address)))
791 (let (result)
792 (dolist (item (tramp-bluez-list-devices) result)
793 (when (string-match address (cadr item))
794 (setq result (car item)))))))
795
796 \f
797 ;; D-Bus GVFS functions.
798
799 (defun tramp-gvfs-handler-askpassword (message user domain flags)
800 "Implementation for the \"org.gtk.vfs.MountOperation.askPassword\" method."
801 (let* ((filename
802 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)))
803 (pw-prompt
804 (format
805 "%s for %s "
806 (if (string-match "\\([pP]assword\\|[pP]assphrase\\)" message)
807 (capitalize (match-string 1 message))
808 "Password")
809 filename))
810 password)
811
812 (condition-case nil
813 (with-parsed-tramp-file-name filename l
814 (when (and (zerop (length user))
815 (not
816 (zerop (logand flags tramp-gvfs-password-need-username))))
817 (setq user (read-string "User name: ")))
818 (when (and (zerop (length domain))
819 (not (zerop (logand flags tramp-gvfs-password-need-domain))))
820 (setq domain (read-string "Domain name: ")))
821
822 (tramp-message l 6 "%S %S %S %d" message user domain flags)
823 (setq tramp-current-method l-method
824 tramp-current-user user
825 tramp-current-host l-host
826 password (tramp-read-passwd
827 (tramp-get-connection-process l) pw-prompt))
828
829 ;; Return result.
830 (if (stringp password)
831 (list
832 t ;; password handled.
833 nil ;; no abort of D-Bus.
834 password
835 (tramp-file-name-real-user l)
836 domain
837 nil ;; not anonymous.
838 0) ;; no password save.
839 ;; No password provided.
840 (list nil t "" (tramp-file-name-real-user l) domain nil 0)))
841
842 ;; When QUIT is raised, we shall return this information to D-Bus.
843 (quit (list nil t "" "" "" nil 0)))))
844
845 (defun tramp-gvfs-handler-askquestion (message choices)
846 "Implementation for the \"org.gtk.vfs.MountOperation.askQuestion\" method."
847 (save-window-excursion
848 (let ((enable-recursive-minibuffers t)
849 choice)
850
851 (condition-case nil
852 (with-parsed-tramp-file-name
853 (tramp-gvfs-file-name (dbus-event-path-name last-input-event)) nil
854 (tramp-message v 6 "%S %S" message choices)
855
856 ;; In theory, there can be several choices. Until now,
857 ;; there is only the question whether to accept an unknown
858 ;; host signature.
859 (with-temp-buffer
860 (insert message)
861 (pop-to-buffer (current-buffer))
862 (setq choice (if (yes-or-no-p (concat (car choices) " ")) 0 1))
863 (tramp-message v 6 "%d" choice))
864
865 ;; When the choice is "no", we set an empty
866 ;; fuse-mountpoint in order to leave the timeout.
867 (unless (zerop choice)
868 (tramp-set-file-property v "/" "fuse-mountpoint" ""))
869
870 (list
871 t ;; handled.
872 nil ;; no abort of D-Bus.
873 choice))
874
875 ;; When QUIT is raised, we shall return this information to D-Bus.
876 (quit (list nil t 0))))))
877
878 (defun tramp-gvfs-handler-mounted-unmounted (mount-info)
879 "Signal handler for the \"org.gtk.vfs.MountTracker.mounted\" and
880 \"org.gtk.vfs.MountTracker.unmounted\" signals."
881 (ignore-errors
882 (let* ((signal-name (dbus-event-member-name last-input-event))
883 (mount-spec (cadar (last mount-info)))
884 (method (dbus-byte-array-to-string (cadr (assoc "type" mount-spec))))
885 (user (dbus-byte-array-to-string (cadr (assoc "user" mount-spec))))
886 (domain (dbus-byte-array-to-string
887 (cadr (assoc "domain" mount-spec))))
888 (host (dbus-byte-array-to-string
889 (cadr (or (assoc "host" mount-spec)
890 (assoc "server" mount-spec)))))
891 (port (dbus-byte-array-to-string (cadr (assoc "port" mount-spec))))
892 (ssl (dbus-byte-array-to-string (cadr (assoc "ssl" mount-spec)))))
893 (when (string-match "^smb" method)
894 (setq method "smb"))
895 (when (string-equal "obex" method)
896 (setq host (tramp-bluez-device host)))
897 (when (and (string-equal "dav" method) (string-equal "true" ssl))
898 (setq method "davs"))
899 (unless (zerop (length domain))
900 (setq user (concat user tramp-prefix-domain-format domain)))
901 (unless (zerop (length port))
902 (setq host (concat host tramp-prefix-port-format port)))
903 (with-parsed-tramp-file-name
904 (tramp-make-tramp-file-name method user host "") nil
905 (tramp-message v 6 "%s %s" signal-name mount-info)
906 (tramp-set-file-property v "/" "list-mounts" 'undef)
907 (if (string-equal signal-name "unmounted")
908 (tramp-set-file-property v "/" "fuse-mountpoint" nil)
909 (tramp-set-file-property
910 v "/" "fuse-mountpoint"
911 (file-name-nondirectory
912 (dbus-byte-array-to-string (car (last mount-info 2))))))))))
913
914 (dbus-register-signal
915 :session nil tramp-gvfs-path-mounttracker
916 tramp-gvfs-interface-mounttracker "mounted"
917 'tramp-gvfs-handler-mounted-unmounted)
918
919 (dbus-register-signal
920 :session nil tramp-gvfs-path-mounttracker
921 tramp-gvfs-interface-mounttracker "unmounted"
922 'tramp-gvfs-handler-mounted-unmounted)
923
924 (defun tramp-gvfs-connection-mounted-p (vec)
925 "Check, whether the location is already mounted."
926 (catch 'mounted
927 (dolist
928 (elt
929 (with-file-property vec "/" "list-mounts"
930 (with-tramp-dbus-call-method vec t
931 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
932 tramp-gvfs-interface-mounttracker "listMounts"))
933 nil)
934 (let* ((mount-spec (cadar (last elt)))
935 (method (dbus-byte-array-to-string
936 (cadr (assoc "type" mount-spec))))
937 (user (dbus-byte-array-to-string
938 (cadr (assoc "user" mount-spec))))
939 (domain (dbus-byte-array-to-string
940 (cadr (assoc "domain" mount-spec))))
941 (host (dbus-byte-array-to-string
942 (cadr (or (assoc "host" mount-spec)
943 (assoc "server" mount-spec)))))
944 (port (dbus-byte-array-to-string (cadr (assoc "port" mount-spec))))
945 (ssl (dbus-byte-array-to-string (cadr (assoc "ssl" mount-spec)))))
946 (when (string-match "^smb" method)
947 (setq method "smb"))
948 (when (string-equal "obex" method)
949 (setq host (tramp-bluez-device host)))
950 (when (and (string-equal "dav" method) (string-equal "true" ssl))
951 (setq method "davs"))
952 (when (and (string-equal "synce" method) (zerop (length user)))
953 (setq user (or (tramp-file-name-user vec) "")))
954 (unless (zerop (length domain))
955 (setq user (concat user tramp-prefix-domain-format domain)))
956 (unless (zerop (length port))
957 (setq host (concat host tramp-prefix-port-format port)))
958 (when (and
959 (string-equal method (tramp-file-name-method vec))
960 (string-equal user (or (tramp-file-name-user vec) ""))
961 (string-equal host (tramp-file-name-host vec)))
962 (tramp-set-file-property
963 vec "/" "fuse-mountpoint"
964 (file-name-nondirectory
965 (dbus-byte-array-to-string (car (last elt 2)))))
966 (throw 'mounted t))))))
967
968 (defun tramp-gvfs-mount-spec (vec)
969 "Return a mount-spec for \"org.gtk.vfs.MountTracker.mountLocation\"."
970 (let* ((method (tramp-file-name-method vec))
971 (user (tramp-file-name-real-user vec))
972 (domain (tramp-file-name-domain vec))
973 (host (tramp-file-name-real-host vec))
974 (port (tramp-file-name-port vec))
975 (localname (tramp-file-name-localname vec))
976 (ssl (if (string-match "^davs" method) "true" "false"))
977 (mount-spec `(:array)))
978
979 (setq
980 mount-spec
981 (append
982 mount-spec
983 (cond
984 ((string-equal "smb" method)
985 (string-match "^/?\\([^/]+\\)" localname)
986 `((:struct "type" ,(dbus-string-to-byte-array "smb-share"))
987 (:struct "server" ,(dbus-string-to-byte-array host))
988 (:struct "share" ,(dbus-string-to-byte-array
989 (match-string 1 localname)))))
990 ((string-equal "obex" method)
991 `((:struct "type" ,(dbus-string-to-byte-array method))
992 (:struct "host" ,(dbus-string-to-byte-array
993 (concat "[" (tramp-bluez-address host) "]")))))
994 ((string-match "^dav" method)
995 `((:struct "type" ,(dbus-string-to-byte-array "dav"))
996 (:struct "host" ,(dbus-string-to-byte-array host))
997 (:struct "ssl" ,(dbus-string-to-byte-array ssl))))
998 (t
999 `((:struct "type" ,(dbus-string-to-byte-array method))
1000 (:struct "host" ,(dbus-string-to-byte-array host)))))))
1001
1002 (when user
1003 (add-to-list
1004 'mount-spec
1005 `(:struct "user" ,(dbus-string-to-byte-array user))
1006 'append))
1007
1008 (when domain
1009 (add-to-list
1010 'mount-spec
1011 `(:struct "domain" ,(dbus-string-to-byte-array domain))
1012 'append))
1013
1014 (when port
1015 (add-to-list
1016 'mount-spec
1017 `(:struct "port" ,(dbus-string-to-byte-array (number-to-string port)))
1018 'append))
1019
1020 ;; Return.
1021 mount-spec))
1022
1023 \f
1024 ;; Connection functions
1025
1026 (defun tramp-gvfs-maybe-open-connection (vec)
1027 "Maybe open a connection VEC.
1028 Does not do anything if a connection is already open, but re-opens the
1029 connection if a previous connection has died for some reason."
1030
1031 ;; We set the file name, in case there are incoming D-Bus signals or
1032 ;; D-Bus errors.
1033 (setq tramp-gvfs-dbus-event-vector vec)
1034
1035 ;; For password handling, we need a process bound to the connection
1036 ;; buffer. Therefore, we create a dummy process. Maybe there is a
1037 ;; better solution?
1038 (unless (get-buffer-process (tramp-get-buffer vec))
1039 (let ((p (make-network-process
1040 :name (tramp-buffer-name vec)
1041 :buffer (tramp-get-buffer vec)
1042 :server t :host 'local :service t)))
1043 (tramp-set-process-query-on-exit-flag p nil)))
1044
1045 (unless (tramp-gvfs-connection-mounted-p vec)
1046 (let* ((method (tramp-file-name-method vec))
1047 (user (tramp-file-name-user vec))
1048 (host (tramp-file-name-host vec))
1049 (object-path
1050 (tramp-gvfs-object-path
1051 (tramp-make-tramp-file-name method user host ""))))
1052
1053 (if (zerop (length (tramp-file-name-user vec)))
1054 (tramp-message
1055 vec 3 "Opening connection for %s using %s..." host method)
1056 (tramp-message
1057 vec 3 "Opening connection for %s@%s using %s..." user host method))
1058
1059 ;; Enable auth-sorce and password-cache.
1060 (tramp-set-connection-property
1061 (tramp-get-connection-process vec) "first-password-request" t)
1062
1063 ;; There will be a callback of "askPassword", when a password is
1064 ;; needed.
1065 (dbus-register-method
1066 :session dbus-service-emacs object-path
1067 tramp-gvfs-interface-mountoperation "askPassword"
1068 'tramp-gvfs-handler-askpassword)
1069
1070 ;; There could be a callback of "askQuestion", when adding fingerprint.
1071 (dbus-register-method
1072 :session dbus-service-emacs object-path
1073 tramp-gvfs-interface-mountoperation "askQuestion"
1074 'tramp-gvfs-handler-askquestion)
1075
1076 ;; The call must be asynchronously, because of the "askPassword"
1077 ;; or "askQuestion"callbacks.
1078 (with-tramp-dbus-call-method vec nil
1079 :session tramp-gvfs-service-daemon tramp-gvfs-path-mounttracker
1080 tramp-gvfs-interface-mounttracker "mountLocation"
1081 `(:struct
1082 ,(dbus-string-to-byte-array "/")
1083 ,(tramp-gvfs-mount-spec vec))
1084 (dbus-get-unique-name :session)
1085 :object-path object-path)
1086
1087 ;; We must wait, until the mount is applied. This will be
1088 ;; indicated by the "mounted" signal, i.e. the "fuse-mountpoint"
1089 ;; file property.
1090 (with-timeout
1091 (60
1092 (if (zerop (length (tramp-file-name-user vec)))
1093 (tramp-error
1094 vec 'file-error
1095 "Timeout reached mounting %s using %s" host method)
1096 (tramp-error
1097 vec 'file-error
1098 "Timeout reached mounting %s@%s using %s" user host method)))
1099 (while (not (tramp-get-file-property vec "/" "fuse-mountpoint" nil))
1100 (read-event nil nil 0.1)))
1101
1102 ;; We set the connection property "started" in order to put the
1103 ;; remote location into the cache, which is helpful for further
1104 ;; completion.
1105 (tramp-set-connection-property vec "started" t)
1106
1107 (if (zerop (length (tramp-file-name-user vec)))
1108 (tramp-message
1109 vec 3 "Opening connection for %s using %s...done" host method)
1110 (tramp-message
1111 vec 3
1112 "Opening connection for %s@%s using %s...done" user host method)))))
1113
1114 \f
1115 ;; D-Bus BLUEZ functions.
1116
1117 (defun tramp-bluez-list-devices ()
1118 "Return all discovered bluetooth devices as list.
1119 Every entry is a list (NAME ADDRESS).
1120
1121 If `tramp-bluez-discover-devices-timeout' is an integer, and the last
1122 discovery happened more time before indicated there, a rescan will be
1123 started, which lasts some ten seconds. Otherwise, cached results will
1124 be used."
1125 ;; Reset the scanned devices list if time has passed.
1126 (and (integerp tramp-bluez-discover-devices-timeout)
1127 (integerp tramp-bluez-discovery)
1128 (> (tramp-time-diff (current-time) tramp-bluez-discovery)
1129 tramp-bluez-discover-devices-timeout)
1130 (setq tramp-bluez-devices nil))
1131
1132 ;; Rescan if needed.
1133 (unless tramp-bluez-devices
1134 (let ((object-path
1135 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1136 :system tramp-bluez-service "/"
1137 tramp-bluez-interface-manager "DefaultAdapter")))
1138 (setq tramp-bluez-devices nil
1139 tramp-bluez-discovery t)
1140 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector nil
1141 :system tramp-bluez-service object-path
1142 tramp-bluez-interface-adapter "StartDiscovery")
1143 (while tramp-bluez-discovery
1144 (read-event nil nil 0.1))))
1145 (setq tramp-bluez-discovery (current-time))
1146 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-bluez-devices)
1147 tramp-bluez-devices)
1148
1149 (defun tramp-bluez-property-changed (property value)
1150 "Signal handler for the \"org.bluez.Adapter.PropertyChanged\" signal."
1151 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" property value)
1152 (cond
1153 ((string-equal property "Discovering")
1154 (unless (car value)
1155 ;; "Discovering" FALSE means discovery run has been completed.
1156 ;; We stop it, because we don't need another run.
1157 (setq tramp-bluez-discovery nil)
1158 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1159 :system tramp-bluez-service (dbus-event-path-name last-input-event)
1160 tramp-bluez-interface-adapter "StopDiscovery")))))
1161
1162 (dbus-register-signal
1163 :system nil nil tramp-bluez-interface-adapter "PropertyChanged"
1164 'tramp-bluez-property-changed)
1165
1166 (defun tramp-bluez-device-found (device args)
1167 "Signal handler for the \"org.bluez.Adapter.DeviceFound\" signal."
1168 (tramp-message tramp-gvfs-dbus-event-vector 6 "%s %s" device args)
1169 (let ((alias (car (cadr (assoc "Alias" args))))
1170 (address (car (cadr (assoc "Address" args)))))
1171 ;; Maybe we shall check the device class for being a proper
1172 ;; device, and call also SDP in order to find the obex service.
1173 (add-to-list 'tramp-bluez-devices (list alias address))))
1174
1175 (dbus-register-signal
1176 :system nil nil tramp-bluez-interface-adapter "DeviceFound"
1177 'tramp-bluez-device-found)
1178
1179 (defun tramp-bluez-parse-device-names (ignore)
1180 "Return a list of (nil host) tuples allowed to access."
1181 (mapcar
1182 (lambda (x) (list nil (car x)))
1183 (tramp-bluez-list-devices)))
1184
1185 ;; Add completion function for OBEX method.
1186 (when (member tramp-bluez-service (dbus-list-known-names :system))
1187 (tramp-set-completion-function
1188 "obex" '((tramp-bluez-parse-device-names ""))))
1189
1190 \f
1191 ;; D-Bus zeroconf functions.
1192
1193 (defun tramp-zeroconf-parse-workstation-device-names (ignore)
1194 "Return a list of (user host) tuples allowed to access."
1195 (mapcar
1196 (lambda (x)
1197 (list nil (zeroconf-service-host x)))
1198 (zeroconf-list-services "_workstation._tcp")))
1199
1200 (defun tramp-zeroconf-parse-webdav-device-names (ignore)
1201 "Return a list of (user host) tuples allowed to access."
1202 (mapcar
1203 (lambda (x)
1204 (let ((host (zeroconf-service-host x))
1205 (port (zeroconf-service-port x))
1206 (text (zeroconf-service-txt x))
1207 user)
1208 (when port
1209 (setq host (format "%s%s%d" host tramp-prefix-port-regexp port)))
1210 ;; A user is marked in a TXT field like "u=guest".
1211 (while text
1212 (when (string-match "u=\\(.+\\)$" (car text))
1213 (setq user (match-string 1 (car text))))
1214 (setq text (cdr text)))
1215 (list user host)))
1216 (zeroconf-list-services "_webdav._tcp")))
1217
1218 ;; Add completion function for DAV and DAVS methods.
1219 (when (member zeroconf-service-avahi (dbus-list-known-names :system))
1220 (zeroconf-init tramp-gvfs-zeroconf-domain)
1221 (tramp-set-completion-function
1222 "sftp" '((tramp-zeroconf-parse-workstation-device-names "")))
1223 (tramp-set-completion-function
1224 "dav" '((tramp-zeroconf-parse-webdav-device-names "")))
1225 (tramp-set-completion-function
1226 "davs" '((tramp-zeroconf-parse-webdav-device-names ""))))
1227
1228 \f
1229 ;; D-Bus SYNCE functions.
1230
1231 (defun tramp-synce-list-devices ()
1232 "Return all discovered synce devices as list.
1233 They are retrieved from the hal daemon."
1234 (let (tramp-synce-devices)
1235 (dolist (device
1236 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1237 :system tramp-hal-service tramp-hal-path-manager
1238 tramp-hal-interface-manager "GetAllDevices"))
1239 (when (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1240 :system tramp-hal-service device tramp-hal-interface-device
1241 "PropertyExists" "sync.plugin")
1242 (add-to-list
1243 'tramp-synce-devices
1244 (with-tramp-dbus-call-method tramp-gvfs-dbus-event-vector t
1245 :system tramp-hal-service device tramp-hal-interface-device
1246 "GetPropertyString" "pda.pocketpc.name"))))
1247 (tramp-message tramp-gvfs-dbus-event-vector 10 "%s" tramp-synce-devices)
1248 tramp-synce-devices))
1249
1250 (defun tramp-synce-parse-device-names (ignore)
1251 "Return a list of (nil host) tuples allowed to access."
1252 (mapcar
1253 (lambda (x) (list nil x))
1254 (tramp-synce-list-devices)))
1255
1256 ;; Add completion function for SYNCE method.
1257 (tramp-set-completion-function
1258 "synce" '((tramp-synce-parse-device-names "")))
1259
1260 (provide 'tramp-gvfs)
1261
1262 ;;; TODO:
1263
1264 ;; * Host name completion via smb-server or smb-network.
1265 ;; * Check, how two shares of the same SMB server can be mounted in
1266 ;; parallel.
1267 ;; * Apply SDP on bluetooth devices, in order to filter out obex
1268 ;; capability.
1269 ;; * Implement obex for other serial communication but bluetooth.
1270
1271 ;; arch-tag: f7f660ce-77f4-4132-9663-f5c25a47f7ed
1272 ;;; tramp-gvfs.el ends here