2006-09-15 15:30:46 +02:00
|
|
|
from sugar.presence import PresenceService
|
2006-10-04 00:55:20 +02:00
|
|
|
from sugar.graphics.iconcolor import IconColor
|
2006-09-22 23:24:00 +02:00
|
|
|
import gobject
|
|
|
|
|
|
|
|
_NOT_PRESENT_COLOR = "#888888,#BBBBBB"
|
|
|
|
|
|
|
|
class BuddyModel(gobject.GObject):
|
|
|
|
__gsignals__ = {
|
|
|
|
'appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
|
2006-09-22 23:37:16 +02:00
|
|
|
'disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
|
|
|
|
'color-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-09-25 20:05:01 +02:00
|
|
|
'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([])),
|
2006-09-22 23:37:16 +02:00
|
|
|
'current-activity-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT]))
|
2006-09-22 23:24:00 +02:00
|
|
|
}
|
2006-09-15 15:30:46 +02:00
|
|
|
|
2006-09-22 22:35:03 +02:00
|
|
|
def __init__(self, name=None, buddy=None):
|
|
|
|
if name and buddy:
|
|
|
|
raise RuntimeError("Must specify only _one_ of name or buddy.")
|
|
|
|
|
2006-09-22 23:24:00 +02:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
|
|
|
self._ba_handler = None
|
|
|
|
self._pc_handler = None
|
|
|
|
self._dis_handler = None
|
2006-09-25 20:05:01 +02:00
|
|
|
self._bic_handler = None
|
2006-09-26 19:39:42 +02:00
|
|
|
self._cac_handler = None
|
2006-09-22 23:24:00 +02:00
|
|
|
|
2006-09-21 22:49:25 +02:00
|
|
|
self._pservice = PresenceService.get_instance()
|
2006-09-22 21:49:16 +02:00
|
|
|
|
2006-09-22 23:24:00 +02:00
|
|
|
self._buddy = None
|
2006-09-22 22:35:03 +02:00
|
|
|
|
|
|
|
# If given just a name, try to get the buddy from the PS first
|
2006-09-22 23:24:00 +02:00
|
|
|
if not buddy:
|
2006-09-22 22:35:03 +02:00
|
|
|
self._name = name
|
|
|
|
# FIXME: use public key, not name
|
2006-09-22 23:24:00 +02:00
|
|
|
buddy = self._pservice.get_buddy_by_name(self._name)
|
2006-09-22 22:35:03 +02:00
|
|
|
|
|
|
|
# If successful, copy properties from the PS buddy object
|
2006-09-22 23:24:00 +02:00
|
|
|
if buddy:
|
2006-09-22 22:35:03 +02:00
|
|
|
self.__update_buddy(buddy)
|
2006-09-22 21:49:16 +02:00
|
|
|
else:
|
2006-09-22 22:35:03 +02:00
|
|
|
# Otherwise, connect to the PS's buddy-appeared signal and
|
|
|
|
# wait for the buddy to appear
|
2006-09-22 23:24:00 +02:00
|
|
|
self._ba_handler = self._pservice.connect('buddy-appeared',
|
2006-09-22 22:35:03 +02:00
|
|
|
self.__buddy_appeared_cb)
|
|
|
|
self._name = name
|
|
|
|
# Set color to 'inactive'/'disconnected'
|
2006-09-22 23:24:00 +02:00
|
|
|
self.__set_color_from_string(_NOT_PRESENT_COLOR)
|
2006-09-15 15:30:46 +02:00
|
|
|
|
2006-09-22 22:35:03 +02:00
|
|
|
def __set_color_from_string(self, color_string):
|
2006-09-15 15:30:46 +02:00
|
|
|
self._color = IconColor(color_string)
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
def get_color(self):
|
|
|
|
return self._color
|
|
|
|
|
|
|
|
def get_buddy(self):
|
2006-09-21 22:49:25 +02:00
|
|
|
return self._buddy
|
|
|
|
|
2006-09-26 19:39:42 +02:00
|
|
|
def is_present(self):
|
|
|
|
if self._buddy:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2006-09-25 05:12:46 +02:00
|
|
|
def get_current_activity(self):
|
2006-09-26 19:39:42 +02:00
|
|
|
if self._buddy:
|
|
|
|
return self._buddy.get_current_activity()
|
|
|
|
return None
|
2006-09-25 05:12:46 +02:00
|
|
|
|
2006-09-22 22:35:03 +02:00
|
|
|
def __update_buddy(self, buddy):
|
|
|
|
if not buddy:
|
|
|
|
raise ValueError("Buddy cannot be None.")
|
2006-09-22 23:24:00 +02:00
|
|
|
|
2006-09-22 22:35:03 +02:00
|
|
|
self._buddy = buddy
|
|
|
|
self._name = self._buddy.get_name()
|
|
|
|
self.__set_color_from_string(self._buddy.get_color())
|
2006-09-22 23:24:00 +02:00
|
|
|
|
|
|
|
self._pc_handler = self._buddy.connect('property-changed', self.__buddy_property_changed_cb)
|
|
|
|
self._dis_handler = self._buddy.connect('disappeared', self.__buddy_disappeared_cb)
|
2006-09-25 20:05:01 +02:00
|
|
|
self._bic_handler = self._buddy.connect('icon-changed', self.__buddy_icon_changed_cb)
|
2006-09-26 19:39:42 +02:00
|
|
|
self._cac_handler = self._buddy.connect('current-activity-changed', self.__buddy_current_activity_changed_cb)
|
2006-09-22 22:35:03 +02:00
|
|
|
|
2006-09-21 22:49:25 +02:00
|
|
|
def __buddy_appeared_cb(self, pservice, buddy):
|
2006-09-22 21:49:16 +02:00
|
|
|
# FIXME: use public key rather than buddy name
|
2006-09-22 22:35:03 +02:00
|
|
|
if self._buddy or buddy.get_name() != self._name:
|
|
|
|
return
|
|
|
|
|
2006-09-22 23:24:00 +02:00
|
|
|
if self._ba_handler:
|
2006-09-22 22:35:03 +02:00
|
|
|
# Once we have the buddy, we no longer need to
|
|
|
|
# monitor buddy-appeared events
|
2006-09-22 23:24:00 +02:00
|
|
|
self._pservice.disconnect(self._ba_handler)
|
|
|
|
self._ba_handler = None
|
2006-09-22 22:35:03 +02:00
|
|
|
|
|
|
|
self.__update_buddy(buddy)
|
2006-09-22 23:24:00 +02:00
|
|
|
self.emit('appeared')
|
2006-09-21 22:49:25 +02:00
|
|
|
|
|
|
|
def __buddy_property_changed_cb(self, buddy, keys):
|
2006-09-22 23:37:16 +02:00
|
|
|
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())
|
2006-09-21 22:49:25 +02:00
|
|
|
|
2006-09-22 23:24:00 +02:00
|
|
|
def __buddy_disappeared_cb(self, buddy):
|
|
|
|
if buddy != self._buddy:
|
|
|
|
return
|
|
|
|
self._buddy.disconnect(self._pc_handler)
|
|
|
|
self._buddy.disconnect(self._dis_handler)
|
2006-09-25 20:05:01 +02:00
|
|
|
self._buddy.disconnect(self._bic_handler)
|
2006-09-26 19:39:42 +02:00
|
|
|
self._buddy.disconnect(self._cac_handler)
|
2006-09-22 23:24:00 +02:00
|
|
|
self.__set_color_from_string(_NOT_PRESENT_COLOR)
|
|
|
|
self.emit('disappeared')
|
|
|
|
self._buddy = None
|
2006-09-25 20:05:01 +02:00
|
|
|
|
|
|
|
def __buddy_icon_changed_cb(self, buddy):
|
|
|
|
self.emit('icon-changed')
|
2006-09-26 19:39:42 +02:00
|
|
|
|
|
|
|
def __buddy_current_activity_changed_cb(self, buddy, activity=None):
|
|
|
|
if not self._buddy:
|
|
|
|
return
|
|
|
|
if activity:
|
|
|
|
self.emit('current-activity-changed', activity)
|
|
|
|
else:
|
|
|
|
self.emit('current-activity-changed')
|