Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/model/MeshModel.py
blob: 374e5fa11f43e23c2aceba7b9f81816215d1d9b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (C) 2006, Red Hat, Inc.
#
# 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 gobject

import conf
from sugar.graphics.iconcolor import IconColor
from sugar.presence import PresenceService
from model.BuddyModel import BuddyModel

class ActivityModel:
	def __init__(self, activity, service):
		self._service = service
		self._activity = activity

	def get_id(self):
		return self._activity.get_id()
		
	def get_icon_name(self):
		registry = conf.get_activity_registry()
		info = registry.get_activity_from_type(self._service.get_type())

		return info.get_icon()
	
	def get_color(self):
		return IconColor(self._activity.get_color())

	def get_service(self):
		return self._service

class MeshModel(gobject.GObject):
	__gsignals__ = {
		'activity-added':   (gobject.SIGNAL_RUN_FIRST,
							 gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
		'activity-removed': (gobject.SIGNAL_RUN_FIRST,
							 gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
		'buddy-added':      (gobject.SIGNAL_RUN_FIRST,
							 gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
		'buddy-moved':      (gobject.SIGNAL_RUN_FIRST,
							 gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT,
												  gobject.TYPE_PYOBJECT])),
		'buddy-removed':    (gobject.SIGNAL_RUN_FIRST,
							 gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT]))
	}

	def __init__(self):
		gobject.GObject.__init__(self)

		self._activities = {}
		self._buddies = {}

		self._pservice = PresenceService.get_instance()
		self._pservice.connect("service-appeared",
							   self._service_appeared_cb)
		self._pservice.connect('activity-disappeared',
							   self._activity_disappeared_cb)
		self._pservice.connect("buddy-appeared",
							   self._buddy_appeared_cb)
		self._pservice.connect("buddy-disappeared",
							   self._buddy_disappeared_cb)

		for service in self._pservice.get_services():
			self._check_service(service)

	def get_activities(self):
		return self._activities

	def get_buddies(self):
		return self._buddies

	def _buddy_activity_changed_cb(self, buddy, cur_activity):
		buddy_model = self._buddies[buddy.get_name()]

		if cur_activity == None:
			self.emit('buddy-moved', buddy_model, None)
		else:
			activity_model = self._activities[cur_activity.get_id()]
			self.emit('buddy-moved', buddy_model, activity_model)

	def _buddy_appeared_cb(self, pservice, buddy):
		model = BuddyModel(buddy=buddy)
		model.connect('current-activity-changed',
					  self._buddy_activity_changed_cb)
		self._buddies[model.get_name()] = model
		self.emit('buddy-added', model)

	def _buddy_disappeared_cb(self, pservice, buddy):
		self.emit('buddy-removed', buddy)
		del self._buddies[buddy.get_name()]

	def _service_appeared_cb(self, pservice, service):
		self._check_service(service)

	def _check_service(self, service):
		registry = conf.get_activity_registry()
		if registry.get_activity_from_type(service.get_type()) != None:
			activity_id = service.get_activity_id()
			if not self.has_activity(activity_id):
				activity = self._pservice.get_activity(activity_id)
				if activity != None:
					self.add_activity(activity, service)

	def has_activity(self, activity_id):
		return self._activities.has_key(activity_id)

	def add_activity(self, activity, service):
		model = ActivityModel(activity, service)
		self._activities[model.get_id()] = model
		self.emit('activity-added', model)

	def _activity_disappeared_cb(self, pservice, activity):
		if self._activities.has_key(activity.get_id()):
			activity_model = self._activities[activity.get_id()]
			self.emit('activity-removed', activity_model)
			del self._activities[activity.get_id()]