Refactor part of the Home model. Should be much
cleaner and reliable. If you find bugs with it please let me know.
This commit is contained in:
parent
2f6790105d
commit
b1a62c2fc0
@ -40,7 +40,7 @@ class HomeActivity(gobject.GObject):
|
|||||||
gobject.PARAM_READWRITE),
|
gobject.PARAM_READWRITE),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, bundle, activity_id):
|
def __init__(self, bundle=None, activity_id=None):
|
||||||
"""Initialise the HomeActivity
|
"""Initialise the HomeActivity
|
||||||
|
|
||||||
bundle -- sugar.activity.bundle.Bundle instance,
|
bundle -- sugar.activity.bundle.Bundle instance,
|
||||||
@ -60,13 +60,8 @@ class HomeActivity(gobject.GObject):
|
|||||||
self._launch_time = time.time()
|
self._launch_time = time.time()
|
||||||
self._launching = False
|
self._launching = False
|
||||||
|
|
||||||
logging.debug("Activity %s (%s) launching..." %
|
|
||||||
(self._activity_id, self.get_type()))
|
|
||||||
|
|
||||||
def set_window(self, window):
|
def set_window(self, window):
|
||||||
"""An activity is 'launched' once we get its window."""
|
"""An activity is 'launched' once we get its window."""
|
||||||
logging.debug("Activity %s (%s) finished launching" %
|
|
||||||
(self._activity_id, self.get_type()))
|
|
||||||
if self._window or self._xid:
|
if self._window or self._xid:
|
||||||
raise RuntimeError("Activity is already launched!")
|
raise RuntimeError("Activity is already launched!")
|
||||||
if not window:
|
if not window:
|
||||||
@ -93,7 +88,10 @@ class HomeActivity(gobject.GObject):
|
|||||||
|
|
||||||
def get_icon_name(self):
|
def get_icon_name(self):
|
||||||
"""Retrieve the bundle's icon (file) name"""
|
"""Retrieve the bundle's icon (file) name"""
|
||||||
return self._bundle.get_icon()
|
if self._bundle:
|
||||||
|
return self._bundle.get_icon()
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def get_icon_color(self):
|
def get_icon_color(self):
|
||||||
"""Retrieve the appropriate icon colour for this activity
|
"""Retrieve the appropriate icon colour for this activity
|
||||||
|
@ -21,7 +21,6 @@ import wnck
|
|||||||
import dbus
|
import dbus
|
||||||
|
|
||||||
from model.homeactivity import HomeActivity
|
from model.homeactivity import HomeActivity
|
||||||
from model.homerawwindow import HomeRawWindow
|
|
||||||
from model import bundleregistry
|
from model import bundleregistry
|
||||||
|
|
||||||
_SERVICE_NAME = "org.laptop.Activity"
|
_SERVICE_NAME = "org.laptop.Activity"
|
||||||
@ -60,7 +59,7 @@ class HomeModel(gobject.GObject):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
gobject.GObject.__init__(self)
|
gobject.GObject.__init__(self)
|
||||||
|
|
||||||
self._activities = {}
|
self._activities = []
|
||||||
self._bundle_registry = bundleregistry.get_registry()
|
self._bundle_registry = bundleregistry.get_registry()
|
||||||
self._current_activity = None
|
self._current_activity = None
|
||||||
|
|
||||||
@ -69,73 +68,83 @@ class HomeModel(gobject.GObject):
|
|||||||
screen.connect('window-closed', self._window_closed_cb)
|
screen.connect('window-closed', self._window_closed_cb)
|
||||||
screen.connect('active-window-changed',
|
screen.connect('active-window-changed',
|
||||||
self._active_window_changed_cb)
|
self._active_window_changed_cb)
|
||||||
|
|
||||||
bus = dbus.SessionBus()
|
bus = dbus.SessionBus()
|
||||||
bus.add_signal_receiver(
|
bus.add_signal_receiver(self._dbus_name_owner_changed_cb,
|
||||||
self._dbus_name_owner_changed_cb,
|
'NameOwnerChanged',
|
||||||
'NameOwnerChanged',
|
'org.freedesktop.DBus',
|
||||||
'org.freedesktop.DBus',
|
'org.freedesktop.DBus')
|
||||||
'org.freedesktop.DBus')
|
|
||||||
|
|
||||||
def get_current_activity(self):
|
def get_current_activity(self):
|
||||||
return self._current_activity
|
return self._current_activity
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
ordered_acts = self._get_ordered_activities()
|
return iter(self._activities)
|
||||||
return iter(ordered_acts)
|
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self._activities)
|
return len(self._activities)
|
||||||
|
|
||||||
def __getitem__(self, i):
|
def __getitem__(self, i):
|
||||||
ordered_acts = self._get_ordered_activities()
|
return self._activities[i]
|
||||||
return ordered_acts[i]
|
|
||||||
|
|
||||||
def index(self, obj):
|
def index(self, obj):
|
||||||
ordered_acts = self._get_ordered_activities()
|
return self._activities.index(obj)
|
||||||
return ordered_acts.index(obj)
|
|
||||||
|
|
||||||
def _get_ordered_activities(self):
|
|
||||||
ordered_acts = self._activities.values()
|
|
||||||
ordered_acts.sort(key=lambda a: a.get_launch_time())
|
|
||||||
return ordered_acts
|
|
||||||
|
|
||||||
def _window_opened_cb(self, screen, window):
|
def _window_opened_cb(self, screen, window):
|
||||||
if window.get_window_type() == wnck.WINDOW_NORMAL:
|
if window.get_window_type() == wnck.WINDOW_NORMAL:
|
||||||
self._add_activity(window)
|
activity = None
|
||||||
|
|
||||||
|
service = self._get_activity_service(window.get_xid())
|
||||||
|
if service:
|
||||||
|
activity_id = service.get_id()
|
||||||
|
activity = self._get_activity_by_id(activity_id)
|
||||||
|
|
||||||
|
if activity:
|
||||||
|
activity.set_service(service)
|
||||||
|
else:
|
||||||
|
activity = self._get_activity_by_xid(window.get_xid())
|
||||||
|
|
||||||
|
if activity:
|
||||||
|
activity.set_window(window)
|
||||||
|
else:
|
||||||
|
activity = HomeActivity()
|
||||||
|
activity.set_window(window)
|
||||||
|
self._add_activity(activity)
|
||||||
|
|
||||||
|
activity.props.launching = False
|
||||||
|
self.emit('activity-started', activity)
|
||||||
|
|
||||||
def _window_closed_cb(self, screen, window):
|
def _window_closed_cb(self, screen, window):
|
||||||
if window.get_window_type() == wnck.WINDOW_NORMAL:
|
if window.get_window_type() == wnck.WINDOW_NORMAL:
|
||||||
self._remove_activity(window.get_xid())
|
self._remove_activity_by_xid(window.get_xid())
|
||||||
if not self._activities:
|
if not self._activities:
|
||||||
self.emit('active-activity-changed', None)
|
self.emit('active-activity-changed', None)
|
||||||
self._notify_activity_activation(self._current_activity, None)
|
self._notify_activity_activation(self._current_activity, None)
|
||||||
|
|
||||||
def _dbus_name_owner_changed_cb(self, name, old, new):
|
def _dbus_name_owner_changed_cb(self, name, old, new):
|
||||||
"""Detect new activity instances on the DBus
|
"""Detect new activity instances on the DBus"""
|
||||||
|
|
||||||
Normally, new activities are detected by
|
|
||||||
the _window_opened_cb callback. However, if the
|
|
||||||
window is opened before the dbus service is up,
|
|
||||||
a RawHomeWindow is created. In here we create
|
|
||||||
a proper HomeActivity replacing the RawHomeWindow.
|
|
||||||
"""
|
|
||||||
if name.startswith(_SERVICE_NAME) and new and not old:
|
if name.startswith(_SERVICE_NAME) and new and not old:
|
||||||
xid = name[len(_SERVICE_NAME):]
|
try:
|
||||||
if not xid.isdigit():
|
xid = int(name[len(_SERVICE_NAME):])
|
||||||
return
|
activity = self._get_activity_by_xid(xid)
|
||||||
logging.debug("Activity instance launch detected: %s" % name)
|
if activity:
|
||||||
xid = int(xid)
|
service = self._get_activity_service(xid)
|
||||||
act = self._get_activity_by_xid(xid)
|
if service:
|
||||||
if isinstance(act, HomeRawWindow):
|
activity.set_service()
|
||||||
logging.debug("Removing bogus raw activity %s for window %i"
|
except ValueError:
|
||||||
% (act.get_activity_id(), xid))
|
logging.error('Invalid activity service name, '
|
||||||
self._internal_remove_activity(act)
|
'cannot extract the xid')
|
||||||
self._add_activity(act.get_window())
|
|
||||||
|
|
||||||
def _get_activity_by_xid(self, xid):
|
def _get_activity_by_xid(self, xid):
|
||||||
for act in self._activities.values():
|
for activity in self._activities:
|
||||||
if act.get_xid() == xid:
|
if activity.get_xid() == xid:
|
||||||
return act
|
return activity
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _get_activity_by_id(self, activity_id):
|
||||||
|
for activity in self._activities:
|
||||||
|
if activity.get_activity_id() == activity_id:
|
||||||
|
return activity
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _notify_activity_activation(self, old_activity, new_activity):
|
def _notify_activity_activation(self, old_activity, new_activity):
|
||||||
@ -173,71 +182,33 @@ class HomeModel(gobject.GObject):
|
|||||||
|
|
||||||
self.emit('active-activity-changed', self._current_activity)
|
self.emit('active-activity-changed', self._current_activity)
|
||||||
|
|
||||||
def _add_window(self, window):
|
def _get_activity_service(self, xid):
|
||||||
home_window = HomeRawWindow(window)
|
|
||||||
self._activities[home_window.get_activity_id()] = home_window
|
|
||||||
self.emit('activity-added', home_window)
|
|
||||||
|
|
||||||
def _add_activity(self, window):
|
|
||||||
"""Add the window to the set of windows we track
|
|
||||||
|
|
||||||
At the moment this requires that something somewhere
|
|
||||||
have registered a dbus service with the XID of the
|
|
||||||
new window that is used to bind the requested activity
|
|
||||||
to the window.
|
|
||||||
|
|
||||||
window -- gtk.Window instance representing a new
|
|
||||||
normal, top-level window
|
|
||||||
"""
|
|
||||||
bus = dbus.SessionBus()
|
bus = dbus.SessionBus()
|
||||||
xid = window.get_xid()
|
|
||||||
try:
|
try:
|
||||||
service = dbus.Interface(
|
service = dbus.Interface(
|
||||||
bus.get_object(_SERVICE_NAME + '%d' % xid,
|
bus.get_object(_SERVICE_NAME + '%d' % xid,
|
||||||
_SERVICE_PATH + "/%s" % xid),
|
_SERVICE_PATH + "/%s" % xid),
|
||||||
_SERVICE_INTERFACE)
|
_SERVICE_INTERFACE)
|
||||||
|
|
||||||
except dbus.DBusException:
|
except dbus.DBusException:
|
||||||
service = None
|
service = None
|
||||||
|
|
||||||
if not service:
|
return service
|
||||||
self._add_window(window)
|
|
||||||
return
|
|
||||||
|
|
||||||
activity = None
|
def _add_activity(self, activity):
|
||||||
act_id = service.get_id()
|
self._activities.append(activity)
|
||||||
act_type = service.get_service_name()
|
self.emit('activity-added', activity)
|
||||||
if self._activities.has_key(act_id):
|
|
||||||
activity = self._activities[act_id]
|
|
||||||
else:
|
|
||||||
# activity got lost, took longer to launch than we allow,
|
|
||||||
# or it was launched by something other than the shell
|
|
||||||
act_type = service.get_service_name()
|
|
||||||
bundle = self._bundle_registry.get_bundle(act_type)
|
|
||||||
if not bundle:
|
|
||||||
raise RuntimeError("No bundle for activity type '%s'." % act_type)
|
|
||||||
return
|
|
||||||
activity = HomeActivity(bundle, act_id)
|
|
||||||
self._activities[act_id] = activity
|
|
||||||
self.emit('activity-added', activity)
|
|
||||||
|
|
||||||
activity.set_service(service)
|
def _remove_activity(self, activity):
|
||||||
activity.set_window(window)
|
|
||||||
|
|
||||||
self.emit('activity-started', activity)
|
|
||||||
|
|
||||||
def _internal_remove_activity(self, activity):
|
|
||||||
if activity == self._current_activity:
|
if activity == self._current_activity:
|
||||||
self._current_activity = None
|
self._current_activity = None
|
||||||
|
|
||||||
self.emit('activity-removed', activity)
|
self.emit('activity-removed', activity)
|
||||||
act_id = activity.get_activity_id()
|
self._activities.remove(activity)
|
||||||
del self._activities[act_id]
|
|
||||||
|
|
||||||
def _remove_activity(self, xid):
|
def _remove_activity_by_xid(self, xid):
|
||||||
activity = self._get_activity_by_xid(xid)
|
activity = self._get_activity_by_xid(xid)
|
||||||
if activity:
|
if activity:
|
||||||
self._internal_remove_activity(activity)
|
self._remove_activity(activity)
|
||||||
else:
|
else:
|
||||||
logging.error('Model for window %d does not exist.' % xid)
|
logging.error('Model for window %d does not exist.' % xid)
|
||||||
|
|
||||||
@ -247,13 +218,12 @@ class HomeModel(gobject.GObject):
|
|||||||
raise ValueError("Activity service name '%s' was not found in the bundle registry." % service_name)
|
raise ValueError("Activity service name '%s' was not found in the bundle registry." % service_name)
|
||||||
activity = HomeActivity(bundle, activity_id)
|
activity = HomeActivity(bundle, activity_id)
|
||||||
activity.props.launching = True
|
activity.props.launching = True
|
||||||
self._activities[activity_id] = activity
|
self._add_activity(activity)
|
||||||
self.emit('activity-added', activity)
|
|
||||||
|
|
||||||
def notify_activity_launch_failed(self, activity_id):
|
def notify_activity_launch_failed(self, activity_id):
|
||||||
if self._activities.has_key(activity_id):
|
if self._activities.has_key(activity_id):
|
||||||
activity = self._activities[activity_id]
|
activity = self._activities[activity_id]
|
||||||
logging.debug("Activity %s (%s) launch failed" % (activity_id, activity.get_type()))
|
logging.debug("Activity %s (%s) launch failed" % (activity_id, activity.get_type()))
|
||||||
self._internal_remove_activity(activity)
|
self._remove_activity(activity)
|
||||||
else:
|
else:
|
||||||
logging.error('Model for activity id %s does not exist.' % activity_id)
|
logging.error('Model for activity id %s does not exist.' % activity_id)
|
||||||
|
@ -66,7 +66,6 @@ class ActivityIcon(CanvasIcon):
|
|||||||
self._start_pulsing()
|
self._start_pulsing()
|
||||||
|
|
||||||
def _launching_changed_cb(self, activity, pspec):
|
def _launching_changed_cb(self, activity, pspec):
|
||||||
print activity.props.launching
|
|
||||||
if activity.props.launching:
|
if activity.props.launching:
|
||||||
self._start_pulsing()
|
self._start_pulsing()
|
||||||
else:
|
else:
|
||||||
@ -163,7 +162,7 @@ class ActivitiesDonut(hippo.CanvasBox, hippo.CanvasItem):
|
|||||||
return
|
return
|
||||||
icon = self._activities[act_id]
|
icon = self._activities[act_id]
|
||||||
self.remove(icon)
|
self.remove(icon)
|
||||||
icon.cleanup()
|
icon._cleanup()
|
||||||
del self._activities[act_id]
|
del self._activities[act_id]
|
||||||
|
|
||||||
def _add_activity(self, activity):
|
def _add_activity(self, activity):
|
||||||
|
Loading…
Reference in New Issue
Block a user