2006-06-22 21:59:38 +02:00
|
|
|
import dbus
|
|
|
|
import random
|
2006-06-23 00:07:54 +02:00
|
|
|
import logging
|
2006-06-22 21:59:38 +02:00
|
|
|
|
|
|
|
import pygtk
|
|
|
|
pygtk.require('2.0')
|
|
|
|
import gtk
|
2006-06-23 00:07:54 +02:00
|
|
|
import gobject
|
2006-06-22 21:59:38 +02:00
|
|
|
|
|
|
|
from sugar.activity.Activity import Activity
|
|
|
|
from sugar.LogWriter import LogWriter
|
2006-06-23 00:07:54 +02:00
|
|
|
from sugar.presence import Service
|
|
|
|
from sugar.chat.Chat import Chat
|
2006-06-22 21:59:38 +02:00
|
|
|
from sugar.chat.BuddyChat import BuddyChat
|
|
|
|
from sugar.p2p.Stream import Stream
|
|
|
|
from sugar.presence.PresenceService import PresenceService
|
|
|
|
import sugar.env
|
|
|
|
|
|
|
|
_CHAT_ACTIVITY_TYPE = "_chat_activity_type._tcp"
|
|
|
|
|
|
|
|
class ChatActivity(Activity):
|
|
|
|
def __init__(self, service):
|
2006-06-23 00:07:54 +02:00
|
|
|
Activity.__init__(self, _CHAT_ACTIVITY_TYPE)
|
2006-06-22 21:59:38 +02:00
|
|
|
self._service = service
|
2006-06-23 00:07:54 +02:00
|
|
|
self._chat = BuddyChat(self._service)
|
2006-06-22 21:59:38 +02:00
|
|
|
|
|
|
|
def on_connected_to_shell(self):
|
2006-06-23 00:07:54 +02:00
|
|
|
self.set_tab_text(self._service.get_name())
|
2006-06-22 21:59:38 +02:00
|
|
|
self.set_can_close(True)
|
2006-06-23 00:07:54 +02:00
|
|
|
self.set_tab_icon(name = "im")
|
2006-06-22 21:59:38 +02:00
|
|
|
self.set_show_tab_icon(True)
|
|
|
|
|
|
|
|
plug = self.gtk_plug()
|
2006-06-23 00:07:54 +02:00
|
|
|
plug.add(self._chat)
|
|
|
|
self._chat.show()
|
2006-06-22 21:59:38 +02:00
|
|
|
|
|
|
|
plug.show()
|
2006-06-23 00:07:54 +02:00
|
|
|
|
|
|
|
def recv_message(self, message):
|
|
|
|
self._chat.recv_message(message)
|
2006-06-22 21:59:38 +02:00
|
|
|
|
|
|
|
class ChatShellDbusService(dbus.service.Object):
|
|
|
|
def __init__(self, parent):
|
|
|
|
self._parent = parent
|
|
|
|
session_bus = dbus.SessionBus()
|
|
|
|
bus_name = dbus.service.BusName('com.redhat.Sugar.Chat', bus=session_bus)
|
|
|
|
object_path = '/com/redhat/Sugar/Chat'
|
|
|
|
dbus.service.Object.__init__(self, bus_name, object_path)
|
|
|
|
|
|
|
|
@dbus.service.method('com.redhat.Sugar.ChatShell')
|
2006-06-23 00:07:54 +02:00
|
|
|
def open_chat(self, serialized_service):
|
|
|
|
self._parent.open_chat(Service.deserialize(serialized_service))
|
2006-06-22 21:59:38 +02:00
|
|
|
|
|
|
|
class ChatShell:
|
|
|
|
instance = None
|
|
|
|
|
|
|
|
def get_instance():
|
|
|
|
if not ChatShell.instance:
|
|
|
|
ChatShell.instance = ChatShell()
|
|
|
|
return ChatShell.instance
|
|
|
|
get_instance = staticmethod(get_instance)
|
|
|
|
|
|
|
|
def __init__(self):
|
2006-06-23 00:07:54 +02:00
|
|
|
ChatShellDbusService(self)
|
|
|
|
|
|
|
|
self._chats = {}
|
|
|
|
|
2006-06-22 21:59:38 +02:00
|
|
|
self._pservice = PresenceService.get_instance()
|
|
|
|
self._pservice.start()
|
|
|
|
self._pservice.track_service_type(BuddyChat.SERVICE_TYPE)
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
port = random.randint(5000, 65535)
|
2006-06-23 00:07:54 +02:00
|
|
|
service = Service.Service(sugar.env.get_nick_name(), BuddyChat.SERVICE_TYPE,
|
|
|
|
'local', '', port)
|
2006-06-22 21:59:38 +02:00
|
|
|
self._buddy_stream = Stream.new_from_service(service)
|
2006-06-23 00:07:54 +02:00
|
|
|
self._buddy_stream.set_data_listener(self._recv_message)
|
2006-06-22 21:59:38 +02:00
|
|
|
self._pservice.register_service(service)
|
|
|
|
|
2006-06-23 00:07:54 +02:00
|
|
|
def _recv_message(self, address, message):
|
|
|
|
[nick, msg] = Chat.deserialize_message(message)
|
|
|
|
buddy = self._pservice.get_buddy_by_nick_name(nick)
|
|
|
|
if buddy:
|
|
|
|
if buddy == self._pservice.get_owner():
|
|
|
|
return
|
|
|
|
service = buddy.get_service_of_type(BuddyChat.SERVICE_TYPE)
|
|
|
|
name = service.get_name()
|
|
|
|
if service:
|
|
|
|
if not self._chats.has_key(name):
|
|
|
|
self.open_chat(service)
|
|
|
|
self._chats[name].recv_message(message)
|
|
|
|
else:
|
|
|
|
logging.error('The buddy %s does not have a chat service.' % (nick))
|
|
|
|
else:
|
|
|
|
logging.error('The buddy %s is not present.' % (nick))
|
|
|
|
return
|
2006-06-22 21:59:38 +02:00
|
|
|
|
2006-06-23 00:07:54 +02:00
|
|
|
def open_chat(self, service):
|
|
|
|
chat = ChatActivity(service)
|
|
|
|
self._chats[service.get_name()] = chat
|
|
|
|
gobject.idle_add(self._connect_chat, chat)
|
|
|
|
return chat
|
|
|
|
|
|
|
|
def _connect_chat(self, chat):
|
|
|
|
chat.connect_to_shell()
|
|
|
|
return False
|
2006-06-22 21:59:38 +02:00
|
|
|
|
|
|
|
log_writer = LogWriter("Chat")
|
|
|
|
log_writer.start()
|
|
|
|
|
|
|
|
chat_shell = ChatShell.get_instance()
|
|
|
|
chat_shell.start()
|
|
|
|
|
|
|
|
gtk.main()
|