Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/model/BuddyModel.py
blob: 651e911eb6ed157f58bf80d113ec23c47efb38ec (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
# Copyright (C) 2006, Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from sugar.presence import PresenceService
from sugar.graphics.iconcolor import IconColor
import gobject

_NOT_PRESENT_COLOR = "#888888,#BBBBBB"

class BuddyModel(gobject.GObject):
	__gsignals__ = {
		'appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
		'disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
		'color-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([gobject.TYPE_PYOBJECT])),
		'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
						 ([])),
		'current-activity-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
									([gobject.TYPE_PYOBJECT]))
	}

	def __init__(self, name=None, buddy=None):
		if name and buddy:
			raise RuntimeError("Must specify only _one_ of name or buddy.")

		gobject.GObject.__init__(self)

		self._ba_handler = None
		self._pc_handler = None
		self._dis_handler = None
		self._bic_handler = None
		self._cac_handler = None

		self._pservice = PresenceService.get_instance()

		self._buddy = None

		# If given just a name, try to get the buddy from the PS first
		if not buddy:
			self._name = name
			# FIXME: use public key, not name
			buddy = self._pservice.get_buddy_by_name(self._name)

		# If successful, copy properties from the PS buddy object
		if buddy:
			self.__update_buddy(buddy)
		else:
			# Otherwise, connect to the PS's buddy-appeared signal and
			# wait for the buddy to appear
			self._ba_handler = self._pservice.connect('buddy-appeared',
					self.__buddy_appeared_cb)
			self._name = name
			# Set color to 'inactive'/'disconnected'
			self.__set_color_from_string(_NOT_PRESENT_COLOR)

	def __set_color_from_string(self, color_string):
		self._color = IconColor(color_string)

	def get_name(self):
		return self._name

	def get_color(self):
		return self._color

	def get_buddy(self):
		return self._buddy

	def is_present(self):
		if self._buddy:
			return True
		return False

	def get_current_activity(self):
		if self._buddy:
			return self._buddy.get_current_activity()
		return None

	def __update_buddy(self, buddy):
		if not buddy:
			raise ValueError("Buddy cannot be None.")

		self._buddy = buddy
		self._name = self._buddy.get_name()
		self.__set_color_from_string(self._buddy.get_color())

		self._pc_handler = self._buddy.connect('property-changed', self.__buddy_property_changed_cb)
		self._dis_handler = self._buddy.connect('disappeared', self.__buddy_disappeared_cb)
		self._bic_handler = self._buddy.connect('icon-changed', self.__buddy_icon_changed_cb)
		self._cac_handler = self._buddy.connect('current-activity-changed', self.__buddy_current_activity_changed_cb)

	def __buddy_appeared_cb(self, pservice, buddy):
		# FIXME: use public key rather than buddy name
		if self._buddy or buddy.get_name() != self._name:
			return

		if self._ba_handler:
			# Once we have the buddy, we no longer need to
			# monitor buddy-appeared events
			self._pservice.disconnect(self._ba_handler)
			self._ba_handler = None

		self.__update_buddy(buddy)
		self.emit('appeared')

	def __buddy_property_changed_cb(self, buddy, keys):
		if not self._buddy:
			return
		if 'color' in keys:
			self.__set_color_from_string(self._buddy.get_color())
			self.emit('color-changed', self.get_color())

	def __buddy_disappeared_cb(self, buddy):
		if buddy != self._buddy:
			return
		self._buddy.disconnect(self._pc_handler)
		self._buddy.disconnect(self._dis_handler)
		self._buddy.disconnect(self._bic_handler)
		self._buddy.disconnect(self._cac_handler)
		self.__set_color_from_string(_NOT_PRESENT_COLOR)
		self.emit('disappeared')
		self._buddy = None

	def __buddy_icon_changed_cb(self, buddy):
		self.emit('icon-changed')

	def __buddy_current_activity_changed_cb(self, buddy, activity=None):
		if not self._buddy:
			return
		self.emit('current-activity-changed', activity)