Code Refactoring. Started to separate gui and functional code.
[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.tube.watch_participants(self.participant_change_cb)
35
36     def participant_change_cb(self, added, removed):
37         if not self.entered:
38             if self.is_initiator:
39                 self._logger.debug("I'm initiating the tube.")
40                 self.add_join_handler()
41             else:
42                 self._logger.debug('Requesting file data')
43                 self.add_file_change_handler()
44                 self.announceJoin()
45         self.entered = True
46
47     #Signals
48     @signal(dbus_interface=IFACE, signature='')
49     def announceJoin(self):
50         self._logger.debug('Announced join.')
51
52     @signal(dbus_interface=IFACE, signature='s')
53     def FileAdd(self, addFile):
54         self._logger.debug('Announced addFile.')
55         self.addFile = addFile
56
57     @signal(dbus_interface=IFACE, signature='s')
58     def FileRem(self, remFile):
59         self._logger.debug('Announced remFile.')
60         self.remFile = remFile
61
62     # Methods
63     @method(dbus_interface=IFACE, in_signature='s', out_signature='')
64     def FileList(self, fileList):
65         """To be called on the incoming XO after they Hello."""
66         self._logger.debug('Somebody called FileList and sent me %s', fileList)
67         self.text_received_cb('filelist',fileList)
68
69     # Handelers
70     def add_join_handler(self):
71         self._logger.debug('Adding join handler.')
72         # Watch for announceJoin
73         self.tube.add_signal_receiver(self.announceJoin_cb, 'announceJoin', IFACE,
74             path=PATH, sender_keyword='sender')
75
76     def add_file_change_handler(self):
77         self._logger.debug('Adding file change handlers.')
78
79         self.tube.add_signal_receiver(self.file_add_cb, 'FileAdd', IFACE,
80             path=PATH, sender_keyword='sender')
81
82         self.tube.add_signal_receiver(self.file_rem_cb, 'FileRem', IFACE,
83             path=PATH, sender_keyword='sender')
84
85     # Callbacks
86     def announceJoin_cb(self, sender=None):
87         """Somebody joined."""
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('Welcoming %s and sending them data', sender)
92
93         self.tube.get_object(sender, PATH).FileList(self.getFileList(), dbus_interface=IFACE)
94
95     def file_add_cb(self, addFile, sender=None):
96         if sender == self.tube.get_unique_name():
97             # sender is my bus name, so ignore my own signal
98             return
99         self._logger.debug('File Add Noticed')
100         self.text_received_cb('fileadd',addFile)
101
102     def file_rem_cb(self, remFile, sender=None):
103         if sender == self.tube.get_unique_name():
104             # sender is my bus name, so ignore my own signal
105             return
106         self._logger.debug('File Rem Noticed')
107         self.text_received_cb('filerem',remFile)