Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/presence/Buddy.py
blob: 579592b1d1696c9f0b3e5f0520931f35a7bf610e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Copyright (C) 2006, Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import gobject
import gtk
import dbus

class Buddy(gobject.GObject):

	__gsignals__ = {
		'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([])),
		'disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([])),
		'service-appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([gobject.TYPE_PYOBJECT])),
		'service-disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([gobject.TYPE_PYOBJECT])),
		'joined-activity': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([gobject.TYPE_PYOBJECT])),
		'left-activity': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([gobject.TYPE_PYOBJECT])),
		'property-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([gobject.TYPE_PYOBJECT])),
		'current-activity-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([gobject.TYPE_PYOBJECT]))
	}

	_PRESENCE_SERVICE = "org.laptop.Presence"
	_BUDDY_DBUS_INTERFACE = "org.laptop.Presence.Buddy"

	def __init__(self, bus, new_obj_cb, del_obj_cb, object_path):
		gobject.GObject.__init__(self)
		self._object_path = object_path
		self._ps_new_object = new_obj_cb
		self._ps_del_object = del_obj_cb
		self._properties = {}
		bobj = bus.get_object(self._PRESENCE_SERVICE, object_path)
		self._buddy = dbus.Interface(bobj, self._BUDDY_DBUS_INTERFACE)
		self._buddy.connect_to_signal('IconChanged', self._icon_changed_cb)
		self._buddy.connect_to_signal('ServiceAppeared', self._service_appeared_cb)
		self._buddy.connect_to_signal('ServiceDisappeared', self._service_disappeared_cb)
		self._buddy.connect_to_signal('Disappeared', self._disappeared_cb)
		self._buddy.connect_to_signal('JoinedActivity', self._joined_activity_cb)
		self._buddy.connect_to_signal('LeftActivity', self._left_activity_cb)
		self._buddy.connect_to_signal('PropertyChanged', self._property_changed_cb)
		self._buddy.connect_to_signal('CurrentActivityChanged', self._current_activity_changed_cb)
		self._properties = self._get_properties_helper()

		self._current_activity = None
		try:
			self._current_activity = self._buddy.getCurrentActivity()
		except Exception, e:
			pass

	def _get_properties_helper(self):
		props = self._buddy.getProperties()
		if not props:
			return {}
		return props

	def object_path(self):
		return self._object_path

	def _emit_icon_changed_signal(self):
		self.emit('icon-changed')
		return False

	def _icon_changed_cb(self):
		gobject.idle_add(self._emit_icon_changed_signal)

	def _emit_disappeared_signal(self):
		self.emit('disappeared')

	def _disappeared_cb(self):
		gobject.idle_add(self._emit_disappeared_signal)

	def _emit_service_appeared_signal(self, object_path):
		self.emit('service-appeared', self._ps_new_object(object_path))
		return False

	def _service_appeared_cb(self, object_path):
		gobject.idle_add(self._emit_service_appeared_signal, object_path)

	def _emit_service_disappeared_signal(self, object_path):
		self.emit('service-disappeared', self._ps_new_object(object_path))
		return False

	def _service_disappeared_cb(self, object_path):
		gobject.idle_add(self._emit_service_disappeared_signal, object_path)

	def _emit_joined_activity_signal(self, object_path):
		self.emit('joined-activity', self._ps_new_object(object_path))
		return False

	def _joined_activity_cb(self, object_path):
		gobject.idle_add(self._emit_joined_activity_signal, object_path)

	def _emit_left_activity_signal(self, object_path):
		self.emit('left-activity', self._ps_new_object(object_path))
		return False

	def _left_activity_cb(self, object_path):
		gobject.idle_add(self._emit_left_activity_signal, object_path)

	def _handle_property_changed_signal(self, prop_list):
		self._properties = self._get_properties_helper()
		self.emit('property-changed', prop_list)
		return False

	def _property_changed_cb(self, prop_list):
		gobject.idle_add(self._handle_property_changed_signal, prop_list)

	def _handle_current_activity_changed_signal(self, act_list):
		if len(act_list) == 0:
			self._current_activity = None
			self.emit('current-activity-changed', None)
		else:
			self._current_activity = act_list[0]
			self.emit('current-activity-changed', self._ps_new_object(act_list[0]))
		return False

	def _current_activity_changed_cb(self, act_list):
		gobject.idle_add(self._handle_current_activity_changed_signal, act_list)

	def get_name(self):
		return self._properties['name']

	def get_ip4_address(self):
		return self._properties['ip4_address']

	def is_owner(self):
		return self._properties['owner']

	def get_color(self):
		return self._properties['color']

	def get_icon(self):
		return self._buddy.getIcon()

	def get_current_activity(self):
		if not self._current_activity:
			return None
		return self._ps_new_object(self._current_activity)

	def get_icon_pixbuf(self):
		icon = self._buddy.getIcon()
		if icon and len(icon):
			pbl = gtk.gdk.PixbufLoader()
			icon_data = ""
			for item in icon:
				if item < 0:
					item = item + 128
				icon_data = icon_data + chr(item)
			pbl.write(icon_data)
			pbl.close()
			return pbl.get_pixbuf()
		else:
			return None

	def get_service_of_type(self, stype, activity=None):
		try:
			act_op = "/"
			if activity:
				act_op = activity.object_path()
			object_path = self._buddy.getServiceOfType(stype, act_op)
		except dbus.exceptions.DBusException:
			return None
		return self._ps_new_object(object_path)

	def get_joined_activities(self):
		try:
			resp = self._buddy.getJoinedActivities()
		except dbus.exceptions.DBusException:
			return []
		acts = []
		for item in resp:
			acts.append(self._ps_new_object(item))
		return acts