Make ShellModel emit signals on activity change, and make Owner listen for them. Fix Owner's current activity update code to actually update at the correct interval
This commit is contained in:
parent
22be6cb0da
commit
906f5bbed0
@ -2,6 +2,7 @@ import os
|
||||
import random
|
||||
import base64
|
||||
import time
|
||||
import gobject
|
||||
|
||||
import conf
|
||||
from sugar import env
|
||||
@ -18,9 +19,12 @@ class ShellOwner(object):
|
||||
"""Class representing the owner of this machine/instance. This class
|
||||
runs in the shell and serves up the buddy icon and other stuff. It's the
|
||||
server portion of the Owner, paired with the client portion in Buddy.py."""
|
||||
def __init__(self):
|
||||
def __init__(self, shell_model):
|
||||
profile = conf.get_profile()
|
||||
|
||||
self._shell_model = shell_model
|
||||
self._shell_model.connect('activity-changed', self.__activity_changed_cb)
|
||||
|
||||
self._nick = profile.get_nick_name()
|
||||
user_dir = profile.get_path()
|
||||
|
||||
@ -78,29 +82,33 @@ class ShellOwner(object):
|
||||
def __update_advertised_current_activity_cb(self):
|
||||
self._last_activity_update = time.time()
|
||||
self._pending_activity_update_timer = None
|
||||
if self._pending_activity_update:
|
||||
logging.debug("*** Updating current activity to %s" % self._pending_activity_update)
|
||||
self._service.set_published_value('curact', dbus.String(self._pending_activity_update))
|
||||
actid = self._pending_activity_update
|
||||
if not actid:
|
||||
actid = ""
|
||||
self._service.set_published_value('curact', dbus.String(actid))
|
||||
return False
|
||||
|
||||
def set_current_activity(self, activity_id):
|
||||
def __activity_changed_cb(self, shell_model, activity_id):
|
||||
"""Update our presence service with the latest activity, but no
|
||||
more frequently than every 30 seconds"""
|
||||
self._pending_activity_update = activity_id
|
||||
# If there's no pending update, we must not have updated it in the
|
||||
# last 30 seconds (except for the initial update, hence we also check
|
||||
# for the last update)
|
||||
if not self._pending_activity_update_timer or time.time() - self._last_activity_update > 30:
|
||||
self.__update_advertised_current_activity_cb()
|
||||
if activity_id == self._pending_activity_update:
|
||||
return
|
||||
self._pending_activity_update = activity_id
|
||||
|
||||
# If we have a pending update already, we have nothing left to do
|
||||
if self._pending_activity_update_timer:
|
||||
return
|
||||
|
||||
# If there's no pending update, we must not have updated it in the
|
||||
# last 30 seconds (except for the initial update, hence we also check
|
||||
# for the last update)
|
||||
if time.time() - self._last_activity_update > 30:
|
||||
self.__update_advertised_current_activity_cb()
|
||||
return
|
||||
|
||||
# Otherwise, we start a timer to update the activity at the next
|
||||
# interval, which should be 30 seconds from the last update, or if that
|
||||
# is in the past already, then now
|
||||
next = 30 - max(30, time.time() - self._last_activity_update)
|
||||
next = int(30 - max(0, time.time() - self._last_activity_update))
|
||||
self._pending_activity_update_timer = gobject.timeout_add(next * 1000,
|
||||
self.__update_advertised_current_activity_cb)
|
||||
|
@ -2,15 +2,22 @@ from sugar.presence import PresenceService
|
||||
from model.Friends import Friends
|
||||
from model.MeshModel import MeshModel
|
||||
from model.Owner import ShellOwner
|
||||
import gobject
|
||||
|
||||
class ShellModel(gobject.GObject):
|
||||
__gsignals__ = {
|
||||
'activity-changed': (gobject.SIGNAL_RUN_FIRST,
|
||||
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT]))
|
||||
}
|
||||
|
||||
class ShellModel:
|
||||
def __init__(self):
|
||||
gobject.GObject.__init__(self)
|
||||
self._current_activity = None
|
||||
|
||||
PresenceService.start()
|
||||
self._pservice = PresenceService.get_instance()
|
||||
|
||||
self._owner = ShellOwner()
|
||||
self._owner = ShellOwner(self)
|
||||
self._owner.announce()
|
||||
self._friends = Friends()
|
||||
self._mesh = MeshModel()
|
||||
@ -29,7 +36,7 @@ class ShellModel:
|
||||
|
||||
def set_current_activity(self, activity_id):
|
||||
self._current_activity = activity_id
|
||||
self._owner.set_current_activity(activity_id)
|
||||
self.emit('activity-changed', activity_id)
|
||||
|
||||
def get_current_activity(self):
|
||||
return self._current_activity
|
||||
|
Loading…
Reference in New Issue
Block a user