2007-06-24 14:45:05 +02:00
|
|
|
# Copyright (C) 2006-2007 Owen Williams.
|
2006-12-24 12:19:24 +01:00
|
|
|
#
|
|
|
|
# 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
|
2007-08-09 18:10:16 +02:00
|
|
|
from sugar import activity
|
2007-06-01 21:21:30 +02:00
|
|
|
|
2006-12-24 12:19:24 +01:00
|
|
|
from model.homeactivity import HomeActivity
|
|
|
|
|
|
|
|
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,
|
2007-08-08 15:30:45 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'pending-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-08-08 15:30:45 +02:00
|
|
|
self._active_activity = None
|
|
|
|
self._pending_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
|
|
|
|
2007-11-09 10:56:43 +01:00
|
|
|
def _get_activities_with_window(self):
|
|
|
|
ret = []
|
|
|
|
for i in self._activities:
|
|
|
|
if i.get_window() is not None:
|
|
|
|
ret.append(i)
|
|
|
|
return ret
|
|
|
|
|
2007-10-23 15:19:17 +02:00
|
|
|
def get_previous_activity(self):
|
2007-11-09 10:56:43 +01:00
|
|
|
activities = self._get_activities_with_window()
|
2007-11-08 21:18:38 +01:00
|
|
|
i = activities.index(self._pending_activity)
|
2007-11-09 10:56:43 +01:00
|
|
|
if len(activities) == 0:
|
|
|
|
return None
|
|
|
|
elif i - 1 >= 0:
|
2007-11-08 21:18:38 +01:00
|
|
|
return activities[i - 1]
|
2007-10-23 15:19:17 +02:00
|
|
|
else:
|
2007-11-09 10:56:43 +01:00
|
|
|
return activities[len(activities) - 1]
|
2007-10-23 15:19:17 +02:00
|
|
|
|
|
|
|
def get_next_activity(self):
|
2007-11-09 10:56:43 +01:00
|
|
|
activities = self._get_activities_with_window()
|
|
|
|
i = activities.index(self._pending_activity)
|
|
|
|
if len(activities) == 0:
|
|
|
|
return None
|
|
|
|
elif i + 1 < len(activities):
|
|
|
|
return activities[i + 1]
|
2007-10-23 15:19:17 +02:00
|
|
|
else:
|
2007-11-09 10:56:43 +01:00
|
|
|
return activities[0]
|
2007-10-23 15:19:17 +02:00
|
|
|
|
2007-08-08 15:30:45 +02:00
|
|
|
def get_pending_activity(self):
|
|
|
|
"""Returns the activity that would be seen in the Activity zoom level
|
|
|
|
|
|
|
|
In the Home (or Neighborhood or Groups) zoom level, this
|
|
|
|
indicates the activity that would become active if the user
|
|
|
|
switched to the Activity zoom level. (In the Activity zoom
|
|
|
|
level, this just returns the currently-active activity.)
|
|
|
|
Unlike get_active_activity(), this never returns None as long
|
|
|
|
as there is any activity running.
|
|
|
|
"""
|
|
|
|
return self._pending_activity
|
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def _set_pending_activity(self, home_activity):
|
|
|
|
if self._pending_activity == home_activity:
|
2007-08-08 15:30:45 +02:00
|
|
|
return
|
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
self._pending_activity = home_activity
|
2007-08-08 15:30:45 +02:00
|
|
|
self.emit('pending-activity-changed', self._pending_activity)
|
|
|
|
|
|
|
|
def get_active_activity(self):
|
|
|
|
"""Returns the activity that the user is currently working in
|
|
|
|
|
|
|
|
In the Activity zoom level, this returns the currently-active
|
|
|
|
activity. In the other zoom levels, it returns the activity
|
|
|
|
that was most-recently active in the Activity zoom level, or
|
|
|
|
None if the most-recently-active activity is no longer
|
|
|
|
running.
|
|
|
|
"""
|
|
|
|
return self._active_activity
|
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def _set_active_activity(self, home_activity):
|
|
|
|
if self._active_activity == home_activity:
|
2007-08-08 15:30:45 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if self._active_activity:
|
|
|
|
service = self._active_activity.get_service()
|
|
|
|
if service:
|
2007-08-20 15:18:46 +02:00
|
|
|
service.SetActive(False,
|
|
|
|
reply_handler=self._set_active_success,
|
|
|
|
error_handler=self._set_active_error)
|
2007-08-09 18:10:16 +02:00
|
|
|
if home_activity:
|
|
|
|
service = home_activity.get_service()
|
2007-08-08 15:30:45 +02:00
|
|
|
if service:
|
2007-08-20 15:18:46 +02:00
|
|
|
service.SetActive(True,
|
|
|
|
reply_handler=self._set_active_success,
|
|
|
|
error_handler=self._set_active_error)
|
2007-08-08 15:30:45 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
self._active_activity = home_activity
|
2007-08-08 15:30:45 +02:00
|
|
|
self.emit('active-activity-changed', self._active_activity)
|
2006-12-24 15:39:00 +01:00
|
|
|
|
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-08-09 18:10:16 +02:00
|
|
|
home_activity = None
|
2007-06-01 13:38:34 +02:00
|
|
|
|
2007-06-01 21:21:30 +02:00
|
|
|
activity_id = wm.get_activity_id(window)
|
2007-06-01 13:38:34 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
service_name = wm.get_bundle_id(window)
|
|
|
|
if service_name:
|
|
|
|
registry = activity.get_registry()
|
|
|
|
activity_info = registry.get_activity(service_name)
|
2007-06-01 13:38:34 +02:00
|
|
|
else:
|
2007-08-09 18:10:16 +02:00
|
|
|
activity_info = None
|
2007-06-01 13:38:34 +02:00
|
|
|
|
2007-06-01 21:21:30 +02:00
|
|
|
if activity_id:
|
2007-08-09 18:10:16 +02:00
|
|
|
home_activity = self._get_activity_by_id(activity_id)
|
2007-06-01 21:21:30 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
if not home_activity:
|
|
|
|
home_activity = HomeActivity(activity_info, activity_id)
|
|
|
|
self._add_activity(home_activity)
|
2007-06-01 13:38:34 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
home_activity.set_window(window)
|
2007-06-01 21:21:30 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
home_activity.props.launching = False
|
|
|
|
self.emit('activity-started', home_activity)
|
2006-12-24 14:35:02 +01:00
|
|
|
|
2007-08-08 15:30:45 +02:00
|
|
|
if self._pending_activity is None:
|
2007-08-09 18:10:16 +02:00
|
|
|
self._set_pending_activity(home_activity)
|
2007-08-08 15:30:45 +02:00
|
|
|
|
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())
|
2006-12-24 15:39:00 +01:00
|
|
|
|
2007-01-07 01:31:19 +01:00
|
|
|
def _get_activity_by_xid(self, xid):
|
2007-08-09 18:10:16 +02:00
|
|
|
for home_activity in self._activities:
|
|
|
|
if home_activity.get_xid() == xid:
|
|
|
|
return home_activity
|
2007-06-01 13:38:34 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _get_activity_by_id(self, activity_id):
|
2007-08-09 18:10:16 +02:00
|
|
|
for home_activity in self._activities:
|
|
|
|
if home_activity.get_activity_id() == activity_id:
|
|
|
|
return home_activity
|
2007-01-07 01:31:19 +01:00
|
|
|
return None
|
|
|
|
|
2007-07-17 07:34:03 +02:00
|
|
|
def _set_active_success(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _set_active_error(self, err):
|
|
|
|
logging.error("set_active() failed: %s" % err)
|
2007-05-16 21:30:49 +02:00
|
|
|
|
2007-09-11 11:02:07 +02:00
|
|
|
def _active_window_changed_cb(self, screen, previous_window=None):
|
2006-12-24 15:39:00 +01:00
|
|
|
window = screen.get_active_window()
|
2007-09-10 11:12:05 +02:00
|
|
|
if window is None:
|
2006-12-24 15:39:00 +01:00
|
|
|
return
|
|
|
|
|
2007-09-10 11:12:05 +02:00
|
|
|
if window.get_window_type() != wnck.WINDOW_DIALOG:
|
|
|
|
while window.get_transient() is not None:
|
|
|
|
window = window.get_transient()
|
|
|
|
|
|
|
|
activity = self._get_activity_by_xid(window.get_xid())
|
|
|
|
if activity is not None:
|
|
|
|
self._set_pending_activity(activity)
|
|
|
|
self._set_active_activity(activity)
|
2007-04-08 19:20:59 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def _add_activity(self, home_activity):
|
|
|
|
self._activities.append(home_activity)
|
|
|
|
self.emit('activity-added', home_activity)
|
2006-12-24 12:19:24 +01:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def _remove_activity(self, home_activity):
|
|
|
|
if home_activity == self._active_activity:
|
2007-08-08 15:30:45 +02:00
|
|
|
self._set_active_activity(None)
|
2007-09-07 17:39:42 +02:00
|
|
|
|
|
|
|
if home_activity == self._pending_activity:
|
|
|
|
# Figure out the new _pending_activity
|
2007-08-08 15:30:45 +02:00
|
|
|
windows = wnck.screen_get_default().get_windows_stacked()
|
|
|
|
windows.reverse()
|
|
|
|
for window in windows:
|
|
|
|
new_activity = self._get_activity_by_xid(window.get_xid())
|
|
|
|
if new_activity is not None:
|
|
|
|
self._set_pending_activity(new_activity)
|
|
|
|
break
|
2007-09-07 17:39:42 +02:00
|
|
|
else:
|
2007-08-08 15:30:45 +02:00
|
|
|
logging.error('No activities are running')
|
|
|
|
self._set_pending_activity(None)
|
2007-01-31 17:16:33 +01:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
self.emit('activity-removed', home_activity)
|
|
|
|
self._activities.remove(home_activity)
|
2007-09-07 17:39:42 +02:00
|
|
|
|
2007-06-01 13:38:34 +02:00
|
|
|
def _remove_activity_by_xid(self, xid):
|
2007-08-09 18:10:16 +02:00
|
|
|
home_activity = self._get_activity_by_xid(xid)
|
|
|
|
if home_activity:
|
|
|
|
self._remove_activity(home_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):
|
2007-08-09 18:10:16 +02:00
|
|
|
registry = activity.get_registry()
|
|
|
|
activity_info = registry.get_activity(service_name)
|
|
|
|
if not activity_info:
|
2007-01-07 01:31:19 +01:00
|
|
|
raise ValueError("Activity service name '%s' was not found in the bundle registry." % service_name)
|
2007-08-09 18:10:16 +02:00
|
|
|
home_activity = HomeActivity(activity_info, activity_id)
|
|
|
|
home_activity.props.launching = True
|
|
|
|
self._add_activity(home_activity)
|
2007-01-07 01:31:19 +01:00
|
|
|
|
2007-11-22 11:57:44 +01:00
|
|
|
# FIXME: better learn about finishing processes by receiving a signal.
|
|
|
|
# Now just check whether an activity has a window after ~90sec
|
|
|
|
gobject.timeout_add(90000, self._check_activity_launched, activity_id)
|
|
|
|
|
2007-01-07 01:31:19 +01:00
|
|
|
def notify_activity_launch_failed(self, activity_id):
|
2007-08-09 18:10:16 +02:00
|
|
|
home_activity = self._get_activity_by_id(activity_id)
|
|
|
|
if home_activity:
|
|
|
|
logging.debug("Activity %s (%s) launch failed" % (activity_id, home_activity.get_type()))
|
|
|
|
self._remove_activity(home_activity)
|
2007-01-07 01:31:19 +01:00
|
|
|
else:
|
|
|
|
logging.error('Model for activity id %s does not exist.' % activity_id)
|
2007-11-22 11:57:44 +01:00
|
|
|
|
|
|
|
def _check_activity_launched(self, activity_id):
|
|
|
|
home_activity = self._get_activity_by_id(activity_id)
|
|
|
|
if home_activity and home_activity.props.launching:
|
|
|
|
logging.debug('Activity %s still launching, assuming it failed...', activity_id)
|
|
|
|
self.notify_activity_launch_failed(activity_id)
|
2007-11-22 12:02:14 +01:00
|
|
|
return False
|