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