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