Start refactoring to get back buddy chat to work...
This commit is contained in:
parent
4d67407f6a
commit
e4c4e866a5
@ -1,20 +1,7 @@
|
|||||||
import dbus
|
|
||||||
import random
|
|
||||||
import logging
|
|
||||||
|
|
||||||
import gtk
|
|
||||||
import gobject
|
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
|
|
||||||
from sugar.activity.Activity import Activity
|
from sugar.activity.Activity import Activity
|
||||||
from sugar.presence import Service
|
|
||||||
from sugar.chat.Chat import Chat
|
|
||||||
from sugar.chat.BuddyChat import BuddyChat
|
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._tcp"
|
|
||||||
|
|
||||||
class ChatActivity(Activity):
|
class ChatActivity(Activity):
|
||||||
def __init__(self, service):
|
def __init__(self, service):
|
||||||
@ -28,62 +15,3 @@ class ChatActivity(Activity):
|
|||||||
|
|
||||||
def recv_message(self, message):
|
def recv_message(self, message):
|
||||||
self._chat.recv_message(message)
|
self._chat.recv_message(message)
|
||||||
|
|
||||||
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',
|
|
||||||
in_signature="o", out_signature="")
|
|
||||||
def open_chat(self, service_path):
|
|
||||||
self._parent.open_chat_from_path(service_path)
|
|
||||||
|
|
||||||
class ChatListener:
|
|
||||||
def __init__(self):
|
|
||||||
ChatShellDbusService(self)
|
|
||||||
|
|
||||||
self._chats = {}
|
|
||||||
|
|
||||||
self._pservice = PresenceService()
|
|
||||||
self._pservice.register_service_type(BuddyChat.SERVICE_TYPE)
|
|
||||||
|
|
||||||
def start(self):
|
|
||||||
self._service = self._pservice.register_service(sugar.env.get_nick_name(),
|
|
||||||
BuddyChat.SERVICE_TYPE)
|
|
||||||
self._buddy_stream = Stream.new_from_service(self._service)
|
|
||||||
self._buddy_stream.set_data_listener(self._recv_message)
|
|
||||||
|
|
||||||
def _recv_message(self, address, message):
|
|
||||||
[nick, msg] = Chat.deserialize_message(message)
|
|
||||||
buddy = self._pservice.get_buddy_by_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
|
|
||||||
|
|
||||||
def open_chat_from_path(self, service_path):
|
|
||||||
service = self._pservice.get(service_path)
|
|
||||||
self.open_chat(service)
|
|
||||||
|
|
||||||
def open_chat(self, service):
|
|
||||||
chat = ChatActivity(service)
|
|
||||||
self._chats[service.get_name()] = chat
|
|
||||||
return chat
|
|
||||||
|
|
||||||
def start():
|
|
||||||
chat_listener = ChatListener()
|
|
||||||
chat_listener.start()
|
|
||||||
|
29
shell/ChatListener.py
Normal file
29
shell/ChatListener.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from sugar import env
|
||||||
|
from sugar.chat.BuddyChat import BuddyChat
|
||||||
|
from sugar.activity import ActivityFactory
|
||||||
|
from sugar.presence.PresenceService import PresenceService
|
||||||
|
from sugar.p2p.Stream import Stream
|
||||||
|
|
||||||
|
class ChatListener:
|
||||||
|
def __init__(self):
|
||||||
|
self._chats = {}
|
||||||
|
|
||||||
|
self._pservice = PresenceService()
|
||||||
|
self._pservice.register_service_type(BuddyChat.SERVICE_TYPE)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self._service = self._pservice.register_service(env.get_nick_name(),
|
||||||
|
BuddyChat.SERVICE_TYPE)
|
||||||
|
self._buddy_stream = Stream.new_from_service(self._service)
|
||||||
|
self._buddy_stream.set_data_listener(self._recv_message)
|
||||||
|
|
||||||
|
def _recv_message(self, address, message):
|
||||||
|
[nick, msg] = Chat.deserialize_message(message)
|
||||||
|
buddy = self._pservice.get_buddy_by_name(nick)
|
||||||
|
if buddy:
|
||||||
|
service = buddy.get_service_of_type(BuddyChat.SERVICE_TYPE)
|
||||||
|
self.open_chat(service)
|
||||||
|
#chat.recv_message(message)
|
||||||
|
|
||||||
|
def open_chat(self, service):
|
||||||
|
ActivityFactory.create("com.redhat.Sugar.ChatActivity")
|
@ -14,6 +14,7 @@ from ConsoleWindow import ConsoleWindow
|
|||||||
from Owner import ShellOwner
|
from Owner import ShellOwner
|
||||||
from PresenceService import PresenceService
|
from PresenceService import PresenceService
|
||||||
from ActivityHost import ActivityHost
|
from ActivityHost import ActivityHost
|
||||||
|
from ChatListener import ChatListener
|
||||||
|
|
||||||
class ShellDbusService(dbus.service.Object):
|
class ShellDbusService(dbus.service.Object):
|
||||||
def __init__(self, shell, bus_name):
|
def __init__(self, shell, bus_name):
|
||||||
@ -57,6 +58,9 @@ class Shell:
|
|||||||
self._owner = ShellOwner()
|
self._owner = ShellOwner()
|
||||||
self._owner.announce()
|
self._owner.announce()
|
||||||
|
|
||||||
|
chat_listener = ChatListener()
|
||||||
|
chat_listener.start()
|
||||||
|
|
||||||
self._home_window = HomeWindow(self)
|
self._home_window = HomeWindow(self)
|
||||||
self._home_window.show()
|
self._home_window.show()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user