2006-10-15 01:08:44 +02:00
|
|
|
# Copyright (C) 2006, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library 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
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2006-06-22 05:49:37 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from sugar.chat.GroupChat import GroupChat
|
|
|
|
|
|
|
|
class ActivityChat(GroupChat):
|
|
|
|
SERVICE_TYPE = "_olpc_activity_chat._udp"
|
|
|
|
|
|
|
|
def __init__(self, activity):
|
|
|
|
GroupChat.__init__(self)
|
2006-06-22 20:37:34 +02:00
|
|
|
self._chat_service = None
|
|
|
|
|
2006-10-19 14:51:13 +02:00
|
|
|
self.connect('destroy', self._destroy_cb)
|
|
|
|
|
2006-06-22 05:49:37 +02:00
|
|
|
self._activity = activity
|
2006-07-24 17:57:02 +02:00
|
|
|
self._pservice.register_service_type(ActivityChat.SERVICE_TYPE)
|
2006-07-23 16:21:00 +02:00
|
|
|
self._pservice.connect('service-appeared', self._service_appeared_cb)
|
2006-06-22 20:37:34 +02:00
|
|
|
|
|
|
|
# Find an existing activity chat to latch onto
|
2006-07-24 18:13:07 +02:00
|
|
|
ps_activity = self._pservice.get_activity(activity.get_id())
|
|
|
|
if ps_activity is not None:
|
2006-07-26 12:57:54 +02:00
|
|
|
services = ps_activity.get_services_of_type(ActivityChat.SERVICE_TYPE)
|
|
|
|
if len(services) > 0:
|
|
|
|
self._service_appeared_cb(self._pservice, services[0])
|
2006-06-22 05:49:37 +02:00
|
|
|
|
2006-07-26 02:04:15 +02:00
|
|
|
def _service_appeared_cb(self, pservice, service):
|
2006-06-22 20:37:34 +02:00
|
|
|
if service.get_activity_id() != self._activity.get_id():
|
|
|
|
return
|
|
|
|
if service.get_type() != ActivityChat.SERVICE_TYPE:
|
|
|
|
return
|
|
|
|
if self._chat_service:
|
|
|
|
return
|
|
|
|
|
|
|
|
logging.debug('Activity chat service appeared, setup the stream.')
|
|
|
|
# Ok, there's an existing chat service that we copy
|
|
|
|
# parameters and such from
|
|
|
|
addr = service.get_address()
|
|
|
|
port = service.get_port()
|
|
|
|
self._chat_service = self._pservice.share_activity(self._activity,
|
2006-07-26 13:41:49 +02:00
|
|
|
stype=ActivityChat.SERVICE_TYPE, address=addr, port=port)
|
2006-06-22 20:37:34 +02:00
|
|
|
self._setup_stream(self._chat_service)
|
2006-06-22 05:49:37 +02:00
|
|
|
|
2006-07-26 00:17:05 +02:00
|
|
|
def share(self):
|
|
|
|
"""Only called when we share the activity this chat is tied to."""
|
2006-06-22 20:37:34 +02:00
|
|
|
self._chat_service = self._pservice.share_activity(self._activity,
|
|
|
|
stype=ActivityChat.SERVICE_TYPE)
|
2006-06-27 18:27:30 +02:00
|
|
|
self._setup_stream(self._chat_service)
|
2006-10-19 14:51:13 +02:00
|
|
|
|
|
|
|
def _destroy_cb(self, widget):
|
2006-10-19 15:22:42 +02:00
|
|
|
if self._chat_service:
|
|
|
|
self._pservice.unregister_service(self._chat_service)
|