sugar-toolkit-gtk3/shell/model/homemodel.py

230 lines
8.4 KiB
Python
Raw Normal View History

# Copyright (C) 2006, Owen Williams.
#
# 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
import logging
import gobject
import wnck
import dbus
from model.homeactivity import HomeActivity
from model import bundleregistry
2007-04-08 19:20:59 +02:00
_SERVICE_NAME = "org.laptop.Activity"
_SERVICE_PATH = "/org/laptop/Activity"
_SERVICE_INTERFACE = "org.laptop.Activity"
class HomeModel(gobject.GObject):
"""Model of the "Home" view (activity management)
The HomeModel is basically the point of registration
for all running activities within Sugar. It traps
events that tell the system there is a new activity
being created (generated by the activity factories),
or removed, as well as those which tell us that the
currently focussed activity has changed.
The HomeModel tracks a set of HomeActivity instances,
which are tracking the window to activity mappings
the activity factories have set up.
"""
__gsignals__ = {
'activity-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'activity-started': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'activity-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'active-activity-changed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
}
def __init__(self):
gobject.GObject.__init__(self)
self._activities = []
self._bundle_registry = bundleregistry.get_registry()
self._current_activity = None
screen = wnck.screen_get_default()
screen.connect('window-opened', self._window_opened_cb)
screen.connect('window-closed', self._window_closed_cb)
screen.connect('active-window-changed',
self._active_window_changed_cb)
bus = dbus.SessionBus()
bus.add_signal_receiver(self._dbus_name_owner_changed_cb,
'NameOwnerChanged',
'org.freedesktop.DBus',
'org.freedesktop.DBus')
def get_current_activity(self):
return self._current_activity
def __iter__(self):
return iter(self._activities)
def __len__(self):
return len(self._activities)
def __getitem__(self, i):
return self._activities[i]
def index(self, obj):
return self._activities.index(obj)
def _window_opened_cb(self, screen, window):
if window.get_window_type() == wnck.WINDOW_NORMAL:
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):
if window.get_window_type() == wnck.WINDOW_NORMAL:
self._remove_activity_by_xid(window.get_xid())
if not self._activities:
self.emit('active-activity-changed', None)
self._notify_activity_activation(self._current_activity, None)
def _dbus_name_owner_changed_cb(self, name, old, new):
"""Detect new activity instances on the DBus"""
if name.startswith(_SERVICE_NAME) and new and not old:
try:
xid = int(name[len(_SERVICE_NAME):])
activity = self._get_activity_by_xid(xid)
if activity:
service = self._get_activity_service(xid)
if service:
activity.set_service()
except ValueError:
logging.error('Invalid activity service name, '
'cannot extract the xid')
def _get_activity_by_xid(self, xid):
for activity in self._activities:
if activity.get_xid() == xid:
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
def _notify_activity_activation(self, old_activity, new_activity):
if old_activity == new_activity:
return
if old_activity:
service = old_activity.get_service()
if service:
service.set_active(False)
if new_activity:
service = new_activity.get_service()
if service:
service.set_active(True)
def _active_window_changed_cb(self, screen):
window = screen.get_active_window()
2006-12-24 15:58:53 +01:00
if window == None:
self.emit('active-activity-changed', None)
self._notify_activity_activation(self._current_activity, None)
2006-12-24 15:58:53 +01:00
return
if window.get_window_type() != wnck.WINDOW_NORMAL:
return
xid = window.get_xid()
act = self._get_activity_by_xid(window.get_xid())
if act:
self._notify_activity_activation(self._current_activity, act)
self._current_activity = act
else:
self._notify_activity_activation(self._current_activity, None)
self._current_activity = None
logging.error('Model for window %d does not exist.' % xid)
self.emit('active-activity-changed', self._current_activity)
2007-04-08 19:20:59 +02:00
def _get_activity_service(self, xid):
bus = dbus.SessionBus()
2007-04-08 19:20:59 +02:00
try:
service = dbus.Interface(
bus.get_object(_SERVICE_NAME + '%d' % xid,
_SERVICE_PATH + "/%s" % xid),
_SERVICE_INTERFACE)
2007-04-08 19:20:59 +02:00
except dbus.DBusException:
service = None
return service
def _add_activity(self, activity):
self._activities.append(activity)
self.emit('activity-added', activity)
def _remove_activity(self, activity):
if activity == self._current_activity:
self._current_activity = None
self.emit('activity-removed', activity)
self._activities.remove(activity)
def _remove_activity_by_xid(self, xid):
activity = self._get_activity_by_xid(xid)
if activity:
self._remove_activity(activity)
else:
logging.error('Model for window %d does not exist.' % xid)
def notify_activity_launch(self, activity_id, service_name):
bundle = self._bundle_registry.get_bundle(service_name)
if not bundle:
raise ValueError("Activity service name '%s' was not found in the bundle registry." % service_name)
activity = HomeActivity(bundle, activity_id)
activity.props.launching = True
self._add_activity(activity)
def notify_activity_launch_failed(self, activity_id):
if self._activities.has_key(activity_id):
activity = self._activities[activity_id]
logging.debug("Activity %s (%s) launch failed" % (activity_id, activity.get_type()))
self._remove_activity(activity)
else:
logging.error('Model for activity id %s does not exist.' % activity_id)