Lots of gui and procedural changes
[jlew:xo-file-distro.git] / FileShare.activity / TubeSpeak.py
1 # Copyright (C) 2009, Justin Lewis  (jtl1728@rit.edu)
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16
17 import logging
18 from dbus.service import method, signal
19 from dbus.gobject_service import ExportedGObject
20
21 SERVICE = "org.laptop.FileShare"
22 IFACE = SERVICE
23 PATH = "/org/laptop/FileShare"
24
25 class TubeSpeak(ExportedGObject):
26     def __init__(self, tube, is_initiator, text_received_cb, get_fileList):
27         super(TubeSpeak, self).__init__(tube, PATH)
28         self._logger = logging.getLogger('fileshare-activity.TubeSpeak')
29         self.tube = tube
30         self.is_initiator = is_initiator
31         self.text_received_cb = text_received_cb
32         self.entered = False  # Have we set up the tube?
33         self.getFileList = get_fileList
34         self.still_serving = True
35         self.tube.watch_participants(self.participant_change_cb)
36
37     def switch_to_server_mode(self):
38         self.still_serving = False
39
40     def participant_change_cb(self, added, removed):
41         if not self.entered:
42             if self.is_initiator:
43                 self._logger.debug("I'm initiating the tube.")
44                 self.add_join_handler()
45             else:
46                 self._logger.debug('Requesting file data')
47                 self.add_file_change_handler()
48                 self.announceJoin()
49         self.entered = True
50
51     #Signals
52     @signal(dbus_interface=IFACE, signature='')
53     def announceJoin(self):
54         self._logger.debug('Announced join.')
55
56     @signal(dbus_interface=IFACE, signature='s')
57     def FileAdd(self, addFile):
58         self._logger.debug('Announced addFile.')
59         self.addFile = addFile
60
61     @signal(dbus_interface=IFACE, signature='s')
62     def FileRem(self, remFile):
63         self._logger.debug('Announced remFile.')
64         self.remFile = remFile
65
66     # Methods
67     @method(dbus_interface=IFACE, in_signature='s', out_signature='')
68     def FileList(self, fileList):
69         """To be called on the incoming XO after they Hello."""
70         self._logger.debug('Somebody called FileList and sent me %s', fileList)
71         self.text_received_cb('filelist',fileList)
72
73     # Handelers
74     def add_join_handler(self):
75         self._logger.debug('Adding join handler.')
76         # Watch for announceJoin
77         self.tube.add_signal_receiver(self.announceJoin_cb, 'announceJoin', IFACE,
78             path=PATH, sender_keyword='sender')
79
80     def add_file_change_handler(self):
81         self._logger.debug('Adding file change handlers.')
82
83         self.tube.add_signal_receiver(self.file_add_cb, 'FileAdd', IFACE,
84             path=PATH, sender_keyword='sender')
85
86         self.tube.add_signal_receiver(self.file_rem_cb, 'FileRem', IFACE,
87             path=PATH, sender_keyword='sender')
88
89     # Callbacks
90     def announceJoin_cb(self, sender=None):
91         """Somebody joined."""
92         if sender == self.tube.get_unique_name() or not self.still_serving:
93             # sender is my bus name, so ignore my own signal
94             return
95         self._logger.debug('Welcoming %s and sending them data', sender)
96
97         self.tube.get_object(sender, PATH).FileList(self.getFileList(), dbus_interface=IFACE)
98
99     def file_add_cb(self, addFile, sender=None):
100         if sender == self.tube.get_unique_name():
101             # sender is my bus name, so ignore my own signal
102             return
103         self._logger.debug('File Add Noticed')
104         self.text_received_cb('fileadd',addFile)
105
106     def file_rem_cb(self, remFile, sender=None):
107         if sender == self.tube.get_unique_name():
108             # sender is my bus name, so ignore my own signal
109             return
110         self._logger.debug('File Rem Noticed')
111         self.text_received_cb('filerem',remFile)