Added ability for server to notify connected clients of file changes
[jlew:xo-file-distro.git] / FileShare.activity / TubeSpeak.py
1 import logging
2 from dbus.service import method, signal
3 from dbus.gobject_service import ExportedGObject
4
5 SERVICE = "org.laptop.FileShare"
6 IFACE = SERVICE
7 PATH = "/org/laptop/FileShare"
8
9 class TubeSpeak(ExportedGObject):
10     def __init__(self, tube, is_initiator, text_received_cb, alert, get_fileList):
11         super(TubeSpeak, self).__init__(tube, PATH)
12         self._logger = logging.getLogger('fileshare-activity.TubeSpeak')
13         self.tube = tube
14         self.is_initiator = is_initiator
15         self.text_received_cb = text_received_cb
16         self._alert = alert
17         self.entered = False  # Have we set up the tube?
18         self.getFileList = get_fileList
19         self.tube.watch_participants(self.participant_change_cb)
20
21     def participant_change_cb(self, added, removed):
22         if not self.entered:
23             if self.is_initiator:
24                 self._logger.debug("I'm initiating the tube.")
25                 self.add_join_handler()
26             else:
27                 self._logger.debug('Requesting file data')
28                 self.add_file_change_handler()
29                 self.announceJoin()
30         self.entered = True
31
32     #Signals
33     @signal(dbus_interface=IFACE, signature='')
34     def announceJoin(self):
35         self._logger.debug('Announced join.')
36
37     @signal(dbus_interface=IFACE, signature='s')
38     def FileAdd(self, addFile):
39         self._logger.debug('Announced addFile.')
40         self.addFile = addFile
41
42     @signal(dbus_interface=IFACE, signature='s')
43     def FileRem(self, remFile):
44         self._logger.debug('Announced remFile.')
45         self.remFile = remFile
46
47     # Methods
48     @method(dbus_interface=IFACE, in_signature='s', out_signature='')
49     def FileList(self, fileList):
50         """To be called on the incoming XO after they Hello."""
51         self._logger.debug('Somebody called FileList and sent me %s', fileList)
52         self.text_received_cb('filelist',fileList)
53
54     # Handelers
55     def add_join_handler(self):
56         self._logger.debug('Adding join handler.')
57         # Watch for announceJoin
58         self.tube.add_signal_receiver(self.announceJoin_cb, 'announceJoin', IFACE,
59             path=PATH, sender_keyword='sender')
60
61     def add_file_change_handler(self):
62         self._logger.debug('Adding file change handlers.')
63
64         self.tube.add_signal_receiver(self.file_add_cb, 'FileAdd', IFACE,
65             path=PATH, sender_keyword='sender')
66
67         self.tube.add_signal_receiver(self.file_rem_cb, 'FileRem', IFACE,
68             path=PATH, sender_keyword='sender')
69
70     # Callbacks
71     def announceJoin_cb(self, sender=None):
72         """Somebody joined."""
73         if sender == self.tube.get_unique_name():
74             # sender is my bus name, so ignore my own signal
75             return
76         self._logger.debug('Welcoming %s and sending them data', sender)
77
78         self.tube.get_object(sender, PATH).FileList(self.getFileList(), dbus_interface=IFACE)
79
80     def file_add_cb(self, addFile, sender=None):
81         if sender == self.tube.get_unique_name():
82             # sender is my bus name, so ignore my own signal
83             return
84         self._logger.debug('File Add Noticed')
85         self.text_received_cb('fileadd',addFile)
86
87     def file_rem_cb(self, remFile, sender=None):
88         if sender == self.tube.get_unique_name():
89             # sender is my bus name, so ignore my own signal
90             return
91         self._logger.debug('File Rem Noticed')
92         self.text_received_cb('filerem',remFile)