2006-12-24 12:19:24 +01:00
|
|
|
# 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
|
|
|
|
|
2006-12-24 15:39:00 +01:00
|
|
|
import logging
|
|
|
|
|
2006-12-24 12:19:24 +01:00
|
|
|
import gobject
|
2006-12-24 14:35:02 +01:00
|
|
|
import wnck
|
2007-02-21 17:28:49 +01:00
|
|
|
import dbus
|
2006-12-24 12:19:24 +01:00
|
|
|
|
2007-06-01 21:21:30 +02:00
|
|
|
from sugar import wm
|
|
|
|
|
2006-12-24 12:19:24 +01:00
|
|
|
from model.homeactivity import HomeActivity
|
2007-05-27 20:24:10 +02:00
|
|
|
from model import bundleregistry
|
2006-12-24 12:19:24 +01:00
|
|
|
|
2007-04-08 19:20:59 +02:00
|
|
|
_SERVICE_NAME = "org.laptop.Activity"
|
|
|
|
_SERVICE_PATH = "/org/laptop/Activity"
|
2007-05-23 18:38:22 +02:00
|
|
|
_SERVICE_INTERFACE = "org.laptop.Activity"
|
2007-02-21 17:28:49 +01:00
|
|
|
|
2006-12-24 12:19:24 +01:00
|
|
|
class HomeModel(gobject.GObject):
|
2007-04-10 04:47:37 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2006-12-24 12:19:24 +01:00
|
|
|
__gsignals__ = {
|
2007-06-01 11:03:18 +02:00
|
|
|
'activity-added': (gobject.SIGNAL_RUN_FIRST,
|
2007-01-07 01:31:19 +01:00
|
|
|
gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2007-06-01 11:03:18 +02:00
|
|
|
'activity-started': (gobject.SIGNAL_RUN_FIRST,
|
2006-12-24 15:39:00 +01:00
|
|
|
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]))
|
2006-12-24 12:19:24 +01:00
|
|
|
}
|
|
|
|
|
2007-02-21 17:53:44 +01:00
|
|
|
def __init__(self):
|
2006-12-24 12:19:24 +01:00
|
|
|
gobject.GObject.__init__(self)
|
2006-12-24 14:35:02 +01:00
|
|
|
|
2007-06-01 13:38:34 +02:00
|
|
|
self._activities = []
|
2007-02-21 17:53:44 +01:00
|
|
|
self._bundle_registry = bundleregistry.get_registry()
|
2006-12-24 15:39:00 +01:00
|
|
|
self._current_activity = None
|
2006-12-24 14:35:02 +01:00
|
|
|
|
|
|
|
screen = wnck.screen_get_default()
|
|
|
|
screen.connect('window-opened', self._window_opened_cb)
|
|
|
|
screen.connect('window-closed', self._window_closed_cb)
|
2006-12-24 15:39:00 +01:00
|
|
|
screen.connect('active-window-changed',
|
|
|
|
self._active_window_changed_cb)
|
2007-06-01 13:38:34 +02:00
|
|
|
|
2006-12-24 15:39:00 +01:00
|
|
|
def get_current_activity(self):
|
|
|
|
return self._current_activity
|
|
|
|
|
2006-12-24 12:19:24 +01:00
|
|
|
def __iter__(self):
|
2007-06-01 13:38:34 +02:00
|
|
|
return iter(self._activities)
|
2006-12-24 12:19:24 +01:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._activities)
|
|
|
|
|
|
|
|
def __getitem__(self, i):
|
2007-06-01 13:38:34 +02:00
|
|
|
return self._activities[i]
|
2007-01-26 03:30:37 +01:00
|
|
|
|
|
|
|
def index(self, obj):
|
2007-06-01 13:38:34 +02:00
|
|
|
return self._activities.index(obj)
|
2007-01-26 03:30:37 +01:00
|
|
|
|
2006-12-24 14:35:02 +01:00
|
|
|
def _window_opened_cb(self, screen, window):
|
|
|
|
if window.get_window_type() == wnck.WINDOW_NORMAL:
|
2007-06-01 13:38:34 +02:00
|
|
|
activity = None
|
|
|
|
|
2007-06-01 21:21:30 +02:00
|
|
|
activity_id = wm.get_activity_id(window)
|
2007-06-01 13:38:34 +02:00
|
|
|
|
2007-06-01 21:21:30 +02:00
|
|
|
bundle_id = wm.get_bundle_id(window)
|
|
|
|
if bundle_id:
|
|
|
|
bundle = self._bundle_registry.get_bundle(bundle_id)
|
2007-06-01 13:38:34 +02:00
|
|
|
else:
|
2007-06-01 21:21:30 +02:00
|
|
|
bundle = None
|
2007-06-01 13:38:34 +02:00
|
|
|
|
2007-06-01 21:21:30 +02:00
|
|
|
if activity_id:
|
|
|
|
activity = self._get_activity_by_id(activity_id)
|
|
|
|
|
|
|
|
if not activity:
|
|
|
|
activity = HomeActivity(bundle, activity_id)
|
2007-06-01 13:38:34 +02:00
|
|
|
self._add_activity(activity)
|
|
|
|
|
2007-06-20 11:52:29 +02:00
|
|
|
service = self._get_activity_service(activity_id)
|
2007-06-01 21:21:30 +02:00
|
|
|
activity.set_service(service)
|
|
|
|
activity.set_window(window)
|
|
|
|
|
2007-06-01 13:38:34 +02:00
|
|
|
activity.props.launching = False
|
|
|
|
self.emit('activity-started', activity)
|
2006-12-24 14:35:02 +01:00
|
|
|
|
|
|
|
def _window_closed_cb(self, screen, window):
|
|
|
|
if window.get_window_type() == wnck.WINDOW_NORMAL:
|
2007-06-01 13:38:34 +02:00
|
|
|
self._remove_activity_by_xid(window.get_xid())
|
2007-01-09 17:55:35 +01:00
|
|
|
if not self._activities:
|
|
|
|
self.emit('active-activity-changed', None)
|
2007-05-16 21:30:49 +02:00
|
|
|
self._notify_activity_activation(self._current_activity, None)
|
2006-12-24 15:39:00 +01:00
|
|
|
|
2007-01-07 01:31:19 +01:00
|
|
|
def _get_activity_by_xid(self, xid):
|
2007-06-01 13:38:34 +02:00
|
|
|
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
|
2007-01-07 01:31:19 +01:00
|
|
|
return None
|
|
|
|
|
2007-05-16 21:30:49 +02:00
|
|
|
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)
|
|
|
|
|
2006-12-24 15:39:00 +01:00
|
|
|
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)
|
2007-05-16 21:30:49 +02:00
|
|
|
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:
|
2006-12-24 15:39:00 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
xid = window.get_xid()
|
2007-01-07 01:31:19 +01:00
|
|
|
act = self._get_activity_by_xid(window.get_xid())
|
|
|
|
if act:
|
2007-06-01 10:24:13 +02:00
|
|
|
self._notify_activity_activation(self._current_activity, act)
|
|
|
|
self._current_activity = act
|
2006-12-24 15:39:00 +01:00
|
|
|
else:
|
2007-05-16 21:30:49 +02:00
|
|
|
self._notify_activity_activation(self._current_activity, None)
|
2006-12-24 15:39:00 +01:00
|
|
|
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
|
|
|
|
2007-06-20 11:52:29 +02:00
|
|
|
def _get_activity_service(self, activity_id):
|
2007-02-21 17:28:49 +01:00
|
|
|
bus = dbus.SessionBus()
|
2007-04-08 19:20:59 +02:00
|
|
|
try:
|
2007-05-23 18:38:22 +02:00
|
|
|
service = dbus.Interface(
|
2007-06-20 11:52:29 +02:00
|
|
|
bus.get_object(_SERVICE_NAME + activity_id,
|
|
|
|
_SERVICE_PATH + "/" + activity_id),
|
2007-06-01 13:38:34 +02:00
|
|
|
_SERVICE_INTERFACE)
|
2007-04-08 19:20:59 +02:00
|
|
|
except dbus.DBusException:
|
|
|
|
service = None
|
|
|
|
|
2007-06-01 13:38:34 +02:00
|
|
|
return service
|
2007-06-01 11:03:18 +02:00
|
|
|
|
2007-06-01 13:38:34 +02:00
|
|
|
def _add_activity(self, activity):
|
|
|
|
self._activities.append(activity)
|
|
|
|
self.emit('activity-added', activity)
|
2006-12-24 12:19:24 +01:00
|
|
|
|
2007-06-01 13:38:34 +02:00
|
|
|
def _remove_activity(self, activity):
|
2007-01-31 17:16:33 +01:00
|
|
|
if activity == self._current_activity:
|
|
|
|
self._current_activity = None
|
|
|
|
|
2007-01-07 01:31:19 +01:00
|
|
|
self.emit('activity-removed', activity)
|
2007-06-01 13:38:34 +02:00
|
|
|
self._activities.remove(activity)
|
2007-01-07 01:31:19 +01:00
|
|
|
|
2007-06-01 13:38:34 +02:00
|
|
|
def _remove_activity_by_xid(self, xid):
|
2007-01-07 06:04:30 +01:00
|
|
|
activity = self._get_activity_by_xid(xid)
|
2007-01-07 01:31:19 +01:00
|
|
|
if activity:
|
2007-06-01 13:38:34 +02:00
|
|
|
self._remove_activity(activity)
|
2006-12-24 15:39:00 +01:00
|
|
|
else:
|
|
|
|
logging.error('Model for window %d does not exist.' % xid)
|
2007-01-07 01:31:19 +01:00
|
|
|
|
|
|
|
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)
|
2007-06-01 11:03:18 +02:00
|
|
|
activity.props.launching = True
|
2007-06-01 13:38:34 +02:00
|
|
|
self._add_activity(activity)
|
2007-01-07 01:31:19 +01:00
|
|
|
|
|
|
|
def notify_activity_launch_failed(self, activity_id):
|
2007-06-01 21:21:30 +02:00
|
|
|
activity = self._get_activity_by_id(activity_id)
|
|
|
|
if activity:
|
2007-01-07 01:31:19 +01:00
|
|
|
logging.debug("Activity %s (%s) launch failed" % (activity_id, activity.get_type()))
|
2007-06-01 13:38:34 +02:00
|
|
|
self._remove_activity(activity)
|
2007-01-07 01:31:19 +01:00
|
|
|
else:
|
|
|
|
logging.error('Model for activity id %s does not exist.' % activity_id)
|