2006-10-15 01:24:45 +02:00
|
|
|
# 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
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
from sugar.presence import presenceservice
|
2007-02-23 13:09:33 +01:00
|
|
|
from sugar.graphics.xocolor import XoColor
|
2006-09-22 23:24:00 +02:00
|
|
|
import gobject
|
|
|
|
|
|
|
|
_NOT_PRESENT_COLOR = "#888888,#BBBBBB"
|
|
|
|
|
|
|
|
class BuddyModel(gobject.GObject):
|
2006-12-04 20:12:24 +01:00
|
|
|
__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]))
|
|
|
|
}
|
|
|
|
|
2007-04-10 21:55:55 +02:00
|
|
|
def __init__(self, key=None, buddy=None):
|
|
|
|
if (key and buddy) or (not key and not buddy):
|
|
|
|
raise RuntimeError("Must specify only _one_ of key or buddy.")
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
|
|
|
self._ba_handler = None
|
|
|
|
self._pc_handler = None
|
|
|
|
self._dis_handler = None
|
|
|
|
self._bic_handler = None
|
|
|
|
self._cac_handler = None
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
self._pservice = presenceservice.get_instance()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
self._buddy = None
|
|
|
|
|
2007-04-10 21:55:55 +02:00
|
|
|
# If given just a key, try to get the buddy from the PS first
|
2006-12-04 20:12:24 +01:00
|
|
|
if not buddy:
|
2007-04-10 21:55:55 +02:00
|
|
|
self._key = key
|
2007-04-09 22:05:32 +02:00
|
|
|
for iter_buddy in self._pservice.get_buddies():
|
2007-04-10 21:55:55 +02:00
|
|
|
if iter_buddy.props.key == key:
|
2007-04-09 22:05:32 +02:00
|
|
|
buddy = iter_buddy
|
|
|
|
break
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
# If successful, copy properties from the PS buddy object
|
|
|
|
if buddy:
|
2007-04-09 22:30:30 +02:00
|
|
|
self._update_buddy(buddy)
|
2006-12-04 20:12:24 +01:00
|
|
|
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',
|
2007-04-09 22:30:30 +02:00
|
|
|
self._buddy_appeared_cb)
|
2007-04-10 21:55:55 +02:00
|
|
|
self._key = key
|
2006-12-04 20:12:24 +01:00
|
|
|
# Set color to 'inactive'/'disconnected'
|
2007-04-09 22:30:30 +02:00
|
|
|
self._set_color_from_string(_NOT_PRESENT_COLOR)
|
2007-04-10 21:55:55 +02:00
|
|
|
self._name = "Unknown buddy"
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
def _set_color_from_string(self, color_string):
|
2007-02-23 13:09:33 +01:00
|
|
|
self._color = XoColor(color_string)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-04-10 21:55:55 +02:00
|
|
|
def get_key(self):
|
|
|
|
return self._key
|
|
|
|
|
|
|
|
def get_nick(self):
|
|
|
|
return self._nick
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def get_color(self):
|
|
|
|
return self._color
|
|
|
|
|
|
|
|
def get_buddy(self):
|
|
|
|
return self._buddy
|
|
|
|
|
2007-04-16 10:36:15 +02:00
|
|
|
def is_owner(self):
|
|
|
|
if not self._buddy:
|
|
|
|
return False
|
|
|
|
return self._buddy.props.owner
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def is_present(self):
|
|
|
|
if self._buddy:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_current_activity(self):
|
|
|
|
if self._buddy:
|
2007-04-09 22:30:30 +02:00
|
|
|
return self._buddy.props.current_activity
|
2006-12-04 20:12:24 +01:00
|
|
|
return None
|
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
def _update_buddy(self, buddy):
|
2006-12-04 20:12:24 +01:00
|
|
|
if not buddy:
|
|
|
|
raise ValueError("Buddy cannot be None.")
|
|
|
|
|
|
|
|
self._buddy = buddy
|
2007-04-10 21:55:55 +02:00
|
|
|
self._key = self._buddy.props.key
|
|
|
|
self._nick = self._buddy.props.nick
|
2007-04-09 22:30:30 +02:00
|
|
|
self._set_color_from_string(self._buddy.props.color)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
self._pc_handler = self._buddy.connect('property-changed', self._buddy_property_changed_cb)
|
|
|
|
self._bic_handler = self._buddy.connect('icon-changed', self._buddy_icon_changed_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
def _buddy_appeared_cb(self, pservice, buddy):
|
2007-04-10 21:55:55 +02:00
|
|
|
if self._buddy or buddy.props.key != self._key:
|
2006-12-04 20:12:24 +01:00
|
|
|
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
|
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
self._update_buddy(buddy)
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('appeared')
|
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
def _buddy_property_changed_cb(self, buddy, keys):
|
2006-12-04 20:12:24 +01:00
|
|
|
if not self._buddy:
|
|
|
|
return
|
|
|
|
if 'color' in keys:
|
2007-04-09 22:30:30 +02:00
|
|
|
self._set_color_from_string(self._buddy.props.color)
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('color-changed', self.get_color())
|
2007-04-20 18:38:04 +02:00
|
|
|
if 'current-activity' in keys:
|
|
|
|
self.emit('current-activity-changed', buddy.props.current_activity)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
def _buddy_disappeared_cb(self, buddy):
|
2006-12-04 20:12:24 +01:00
|
|
|
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)
|
2007-04-09 22:30:30 +02:00
|
|
|
self._set_color_from_string(_NOT_PRESENT_COLOR)
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('disappeared')
|
|
|
|
self._buddy = None
|
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
def _buddy_icon_changed_cb(self, buddy):
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('icon-changed')
|