Merge branch 'master' of git+ssh://dcbw@crank.laptop.org/git/sugar

This commit is contained in:
Dan Williams 2006-06-22 22:42:35 -04:00
commit 4511d13fb5
12 changed files with 245 additions and 587 deletions

View File

@ -0,0 +1,114 @@
import dbus
import random
import logging
import pygtk
pygtk.require('2.0')
import gtk
import gobject
from sugar.activity.Activity import Activity
from sugar.LogWriter import LogWriter
from sugar.presence import Service
from sugar.chat.Chat import Chat
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):
Activity.__init__(self, _CHAT_ACTIVITY_TYPE)
self._service = service
self._chat = BuddyChat(self._service)
def on_connected_to_shell(self):
self.set_tab_text(self._service.get_name())
self.set_can_close(True)
self.set_tab_icon(name = "im")
self.set_show_tab_icon(True)
plug = self.gtk_plug()
plug.add(self._chat)
self._chat.show()
plug.show()
def recv_message(self, 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')
def open_chat(self, serialized_service):
self._parent.open_chat(Service.deserialize(serialized_service))
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):
ChatShellDbusService(self)
self._chats = {}
self._pservice = PresenceService.get_instance()
self._pservice.start()
self._pservice.track_service_type(BuddyChat.SERVICE_TYPE)
def start(self):
port = random.randint(5000, 65535)
service = Service.Service(sugar.env.get_nick_name(), BuddyChat.SERVICE_TYPE,
'local', '', port)
self._buddy_stream = Stream.new_from_service(service)
self._buddy_stream.set_data_listener(self._recv_message)
self._pservice.register_service(service)
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
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
log_writer = LogWriter("Chat")
log_writer.start()
chat_shell = ChatShell.get_instance()
chat_shell.start()
gtk.main()

View File

@ -0,0 +1,2 @@
[Activity]
python_class = ChatActivity

View File

@ -0,0 +1,2 @@
[Activity]
python_class = terminal

View File

@ -0,0 +1,70 @@
import os
import gtk
import vte
from sugar.activity.Activity import Activity
_TERMINAL_ACTIVITY_TYPE = "_terminal._tcp"
class Terminal(gtk.HBox):
def __init__(self):
gtk.HBox.__init__(self, False, 4)
self._vte = vte.Terminal()
self._configure_vte()
self._vte.set_size(30, 5)
self._vte.set_size_request(200, 50)
self._vte.show()
self.pack_start(self._vte)
self._scrollbar = gtk.VScrollbar(self._vte.get_adjustment())
self._scrollbar.show()
self.pack_start(self._scrollbar, False, False, 0)
self._vte.connect("child-exited", lambda term: term.fork_command())
self._vte.fork_command()
def _configure_vte(self):
self._vte.set_font(pango.FontDescription('Monospace 10'))
self._vte.set_colors(gtk.gdk.color_parse ('#AAAAAA'),
gtk.gdk.color_parse ('#000000'),
[])
self._vte.set_cursor_blinks(False)
self._vte.set_audible_bell(False)
self._vte.set_scrollback_lines(100)
self._vte.set_allow_bold(True)
self._vte.set_scroll_on_keystroke(False)
self._vte.set_scroll_on_output(False)
self._vte.set_emulation('xterm')
self._vte.set_visible_bell(False)
def on_gconf_notification(self, client, cnxn_id, entry, what):
self.reconfigure_vte()
def on_vte_button_press(self, term, event):
if event.button == 3:
self.do_popup(event)
return True
def on_vte_popup_menu(self, term):
class TerminalActivity(Activity):
def __init__(self):
Activity.__init__(self, _TERMINAL_ACTIVITY_TYPE)
def on_connected_to_shell(self):
self.set_tab_text("Terminal")
plug = self.gtk_plug()
terminal = Terminal()
plug.add(terminal)
terminal.show()
plug.show()
activity = TerminalActivity()
activity.connect_to_shell()
gtk.main()

View File

@ -2,8 +2,11 @@ import pygtk
pygtk.require('2.0') pygtk.require('2.0')
import gtk import gtk
import gobject import gobject
import dbus
from sugar.presence.PresenceService import PresenceService from sugar.presence.PresenceService import PresenceService
from sugar.presence.Service import Service
from sugar.chat.BuddyChat import BuddyChat
class PresenceWindow(gtk.Window): class PresenceWindow(gtk.Window):
_MODEL_COL_NICK = 0 _MODEL_COL_NICK = 0
@ -20,9 +23,10 @@ class PresenceWindow(gtk.Window):
self._pservice = PresenceService.get_instance() self._pservice = PresenceService.get_instance()
self._pservice.connect("buddy-appeared", self._on_buddy_appeared_cb) self._pservice.connect("buddy-appeared", self._on_buddy_appeared_cb)
self._pservice.connect("buddy-disappeared", self._on_buddy_disappeared_cb) self._pservice.connect("buddy-disappeared", self._on_buddy_disappeared_cb)
self._pservice.set_debug(True)
self._pservice.start() self._pservice.start()
self._pservice.track_service_type(BuddyChat.SERVICE_TYPE)
self._setup_ui() self._setup_ui()
def _is_buddy_visible(self, buddy): def _is_buddy_visible(self, buddy):
@ -116,11 +120,16 @@ class PresenceWindow(gtk.Window):
(model, aniter) = view.get_selection().get_selected() (model, aniter) = view.get_selection().get_selected()
chat = None chat = None
buddy = view.get_model().get_value(aniter, self._MODEL_COL_BUDDY) buddy = view.get_model().get_value(aniter, self._MODEL_COL_BUDDY)
if buddy and not self._chats.has_key(buddy): if buddy:
#chat = BuddyChat(self, buddy) chat_service = buddy.get_service_of_type(BuddyChat.SERVICE_TYPE)
#self._chats[buddy] = chat if chat_service:
#chat.connect_to_shell() bus = dbus.SessionBus()
pass proxy_obj = bus.get_object('com.redhat.Sugar.Chat', '/com/redhat/Sugar/Chat')
chat_shell = dbus.Interface(proxy_obj, 'com.redhat.Sugar.ChatShell')
serialized_service = Service.serialize(chat_service)
chat_shell.open_chat(serialized_service)
else:
print 'Could not find buddy chat'
def __buddy_icon_changed_cb(self, buddy): def __buddy_icon_changed_cb(self, buddy):
it = self._get_iter_for_buddy(buddy) it = self._get_iter_for_buddy(buddy)

View File

@ -342,7 +342,7 @@ class ActivityContainer(dbus.service.Object):
wm = WindowManager(self._presence_window) wm = WindowManager(self._presence_window)
wm.set_width(0.15, WindowManager.SCREEN_RELATIVE) wm.set_width(0.18, WindowManager.SCREEN_RELATIVE)
wm.set_height(1.0, WindowManager.SCREEN_RELATIVE) wm.set_height(1.0, WindowManager.SCREEN_RELATIVE)
wm.set_position(WindowManager.LEFT) wm.set_position(WindowManager.LEFT)
wm.manage() wm.manage()

14
sugar/chat/BuddyChat.py Normal file
View File

@ -0,0 +1,14 @@
from sugar.chat.Chat import Chat
from sugar.p2p.Stream import Stream
class BuddyChat(Chat):
SERVICE_TYPE = "_olpc_buddy_chat._tcp"
def __init__(self, service):
Chat.__init__(self)
self._stream = Stream.new_from_service(service, False)
self._stream_writer = self._stream.new_writer(service)
def _recv_message_cb(self, address, msg):
self.recv_message(msg)

View File

@ -28,6 +28,9 @@ class Chat(gtk.VBox):
def __init__(self): def __init__(self):
gtk.VBox.__init__(self, False, 6) gtk.VBox.__init__(self, False, 6)
self._pservice = PresenceService.get_instance()
self._pservice.start()
self._stream_writer = None self._stream_writer = None
self.set_border_width(12) self.set_border_width(12)
@ -193,13 +196,16 @@ class Chat(gtk.VBox):
return msg[desc_start:svg_last + len(tag_svg_end)] return msg[desc_start:svg_last + len(tag_svg_end)]
return None return None
def recv_message(self, buddy, msg): def recv_message(self, message):
"""Insert a remote chat message into the chat buffer.""" """Insert a remote chat message into the chat buffer."""
[nick, msg] = Chat.deserialize_message(message)
buddy = self._pservice.get_buddy_by_nick_name(nick)
if not buddy: if not buddy:
logging.error('The buddy %s is not present.' % (nick))
return return
# FIXME a better way to compare buddies? # FIXME a better way to compare buddies?
owner = PresenceService.get_instance().get_owner() owner = self._pservice.get_owner()
if buddy.get_nick_name() == owner.get_nick_name(): if buddy.get_nick_name() == owner.get_nick_name():
return return
@ -236,5 +242,7 @@ class Chat(gtk.VBox):
owner = PresenceService.get_instance().get_owner() owner = PresenceService.get_instance().get_owner()
return owner.get_nick_name() + '||' + message return owner.get_nick_name() + '||' + message
def deserialize_message(self, message): def deserialize_message(message):
return message.split('||', 1) return message.split('||', 1)
deserialize_message = staticmethod(deserialize_message)

View File

@ -8,8 +8,6 @@ import sugar.env
class GroupChat(Chat): class GroupChat(Chat):
def __init__(self): def __init__(self):
Chat.__init__(self) Chat.__init__(self)
self._pservice = PresenceService.get_instance()
self._pservice.start()
self._group_stream = None self._group_stream = None
def _setup_stream(self, service): def _setup_stream(self, service):
@ -18,10 +16,4 @@ class GroupChat(Chat):
self._stream_writer = self._group_stream.new_writer() self._stream_writer = self._group_stream.new_writer()
def _group_recv_message(self, address, msg): def _group_recv_message(self, address, msg):
pservice = PresenceService.get_instance() self.recv_message(msg)
[nick, msg] = self.deserialize_message(msg)
buddy = pservice.get_buddy_by_nick_name(nick)
if buddy:
self.recv_message(buddy, msg)
else:
logging.error('The buddy %s is not present.' % (nick))

View File

@ -1,557 +0,0 @@
#!/usr/bin/env python
import base64
import sha
import dbus
import dbus.service
import dbus.glib
import threading
import pygtk
pygtk.require('2.0')
import gtk, gobject, pango
from sugar.shell import activity
from sugar.presence.Group import Group
from sugar.presence import Buddy
from sugar.presence.Service import Service
from sugar.p2p.Stream import Stream
from sugar.p2p import network
from sugar.session.LogWriter import LogWriter
from sugar.chat.sketchpad.Toolbox import Toolbox
from sugar.chat.sketchpad.SketchPad import SketchPad
from sugar.chat.Emoticons import Emoticons
import sugar.env
import richtext
PANGO_SCALE = 1024 # Where is this defined?
CHAT_SERVICE_TYPE = "_olpc_chat._tcp"
CHAT_SERVICE_PORT = 6100
GROUP_CHAT_SERVICE_TYPE = "_olpc_group_chat._udp"
GROUP_CHAT_SERVICE_ADDRESS = "224.0.0.221"
GROUP_CHAT_SERVICE_PORT = 6200
class Chat(activity.Activity):
def __init__(self, controller):
Buddy.recognize_buddy_service_type(CHAT_SERVICE_TYPE)
self._controller = controller
activity.Activity.__init__(self)
self._stream_writer = None
self._emt_popup = None
bus = dbus.SessionBus()
proxy_obj = bus.get_object('com.redhat.Sugar.Browser', '/com/redhat/Sugar/Browser')
self._browser_shell = dbus.Interface(proxy_obj, 'com.redhat.Sugar.BrowserShell')
def on_connected_to_shell(self):
self.set_tab_text(self._act_name)
self._ui_setup(self._plug)
self._plug.show_all()
def _create_toolbox(self):
vbox = gtk.VBox(False, 12)
toolbox = Toolbox()
toolbox.connect('tool-selected', self._tool_selected)
toolbox.connect('color-selected', self._color_selected)
vbox.pack_start(toolbox, False)
toolbox.show()
button_box = gtk.HButtonBox()
send_button = gtk.Button('Send')
button_box.pack_start(send_button, False)
send_button.connect('clicked', self.__send_button_clicked_cb)
vbox.pack_start(button_box, False)
button_box.show()
return vbox
def __send_button_clicked_cb(self, button):
self.send_sketch(self._sketchpad.to_svg())
self._sketchpad.clear()
def _color_selected(self, toolbox, color):
self._sketchpad.set_color(color)
def _tool_selected(self, toolbox, tool_id):
if tool_id == 'text':
self._editor_nb.set_current_page(0)
else:
self._editor_nb.set_current_page(1)
def _create_chat_editor(self):
nb = gtk.Notebook()
nb.set_show_tabs(False)
nb.set_show_border(False)
nb.set_size_request(-1, 70)
chat_view_sw = gtk.ScrolledWindow()
chat_view_sw.set_shadow_type(gtk.SHADOW_IN)
chat_view_sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self._editor = richtext.RichTextView()
self._editor.connect("key-press-event", self.__key_press_event_cb)
chat_view_sw.add(self._editor)
self._editor.show()
nb.append_page(chat_view_sw)
chat_view_sw.show()
self._sketchpad = SketchPad()
nb.append_page(self._sketchpad)
self._sketchpad.show()
nb.set_current_page(0)
return nb
def _create_chat(self):
chat_vbox = gtk.VBox()
chat_vbox.set_spacing(6)
self._chat_sw = gtk.ScrolledWindow()
self._chat_sw.set_shadow_type(gtk.SHADOW_IN)
self._chat_sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_ALWAYS)
self._chat_view = richtext.RichTextView()
self._chat_view.connect("link-clicked", self.__link_clicked_cb)
self._chat_view.set_editable(False)
self._chat_view.set_cursor_visible(False)
self._chat_view.set_pixels_above_lines(7)
self._chat_view.set_left_margin(5)
self._chat_sw.add(self._chat_view)
self._chat_view.show()
chat_vbox.pack_start(self._chat_sw)
self._chat_sw.show()
self._editor_nb = self._create_chat_editor()
chat_vbox.pack_start(self._editor_nb, False)
self._editor_nb.show()
return chat_vbox, self._editor.get_buffer()
def _ui_setup(self, base):
vbox = gtk.VBox(False, 6)
self._hbox = gtk.HBox(False, 12)
self._hbox.set_border_width(12)
[chat_vbox, buf] = self._create_chat()
self._hbox.pack_start(chat_vbox)
chat_vbox.show()
vbox.pack_start(self._hbox)
self._hbox.show()
toolbar = self._create_toolbar(buf)
vbox.pack_start(toolbar, False)
toolbar.show()
base.add(vbox)
vbox.show()
def __link_clicked_cb(self, view, address):
self._browser_shell.open_browser(address)
def __key_press_event_cb(self, text_view, event):
if event.keyval == gtk.keysyms.Return:
buf = text_view.get_buffer()
text = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
if len(text.strip()) > 0:
serializer = richtext.RichTextSerializer()
text = serializer.serialize(buf)
self.send_text_message(text)
buf.set_text("")
buf.place_cursor(buf.get_start_iter())
return True
def _create_emoticons_popup(self):
model = gtk.ListStore(gtk.gdk.Pixbuf, str)
for name in Emoticons.get_instance().get_all():
icon_theme = gtk.icon_theme_get_default()
pixbuf = icon_theme.load_icon(name, 16, 0)
model.append([pixbuf, name])
icon_view = gtk.IconView(model)
icon_view.connect('selection-changed', self.__emoticon_selection_changed_cb)
icon_view.set_pixbuf_column(0)
icon_view.set_selection_mode(gtk.SELECTION_SINGLE)
frame = gtk.Frame()
frame.set_shadow_type(gtk.SHADOW_ETCHED_IN)
frame.add(icon_view)
icon_view.show()
window = gtk.Window(gtk.WINDOW_POPUP)
window.add(frame)
frame.show()
return window
def __emoticon_selection_changed_cb(self, icon_view):
items = icon_view.get_selected_items()
if items:
model = icon_view.get_model()
icon_name = model[items[0]][1]
self._editor.get_buffer().append_icon(icon_name)
self._emt_popup.hide()
def _create_toolbar(self, rich_buf):
toolbar = richtext.RichTextToolbar(rich_buf)
item = gtk.ToolButton()
hbox = gtk.HBox(False, 6)
e_image = gtk.Image()
e_image.set_from_icon_name('stock_smiley-1', gtk.ICON_SIZE_SMALL_TOOLBAR)
hbox.pack_start(e_image)
e_image.show()
arrow = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_NONE)
hbox.pack_start(arrow)
arrow.show()
item.set_icon_widget(hbox)
item.set_homogeneous(False)
item.connect("clicked", self.__emoticons_button_clicked_cb)
toolbar.insert(item, -1)
item.show()
separator = gtk.SeparatorToolItem()
toolbar.insert(separator, -1)
separator.show()
item = gtk.MenuToolButton(None, "Links")
item.set_menu(gtk.Menu())
item.connect("show-menu", self.__show_link_menu_cb)
toolbar.insert(item, -1)
item.show()
return toolbar
def __emoticons_button_clicked_cb(self, button):
# FIXME grabs...
if not self._emt_popup:
self._emt_popup = self._create_emoticons_popup()
if self._emt_popup.get_property('visible'):
self._emt_popup.hide()
else:
width = 180
height = 130
self._emt_popup.set_default_size(width, height)
[x, y] = button.window.get_origin()
x += button.allocation.x
y += button.allocation.y - height
self._emt_popup.move(x, y)
self._emt_popup.show()
def __link_activate_cb(self, item, link):
buf = self._editor.get_buffer()
buf.append_link(link['title'], link['address'])
def __show_link_menu_cb(self, button):
menu = gtk.Menu()
links = self._browser_shell.get_links()
for link in links:
item = gtk.MenuItem(link['title'], False)
item.connect("activate", self.__link_activate_cb, link)
menu.append(item)
item.show()
button.set_menu(menu)
def _scroll_chat_view_to_bottom(self):
# Only scroll to bottom if the view is already close to the bottom
vadj = self._chat_sw.get_vadjustment()
if vadj.value + vadj.page_size > vadj.upper * 0.8:
vadj.value = vadj.upper - vadj.page_size
self._chat_sw.set_vadjustment(vadj)
def _message_inserted(self):
gobject.idle_add(self._scroll_chat_view_to_bottom)
self.set_has_changes(True)
def _insert_buddy(self, buf, nick):
# Stuff in the buddy icon, if we have one for this buddy
buddy = self._controller.get_group().get_buddy(nick)
icon = buddy.get_icon_pixbuf()
if icon:
rise = int(icon.get_height() / 4) * -1
chat_service = buddy.get_service(CHAT_SERVICE_TYPE)
hash_string = "%s-%s" % (nick, chat_service.get_address())
sha_hash = sha.new()
sha_hash.update(hash_string)
tagname = "buddyicon-%s" % sha_hash.hexdigest()
if not buf.get_tag_table().lookup(tagname):
buf.create_tag(tagname, rise=(rise * PANGO_SCALE))
aniter = buf.get_end_iter()
buf.insert_pixbuf(aniter, icon)
aniter.backward_char()
enditer = buf.get_end_iter()
buf.apply_tag_by_name(tagname, aniter, enditer)
# Stick in the buddy's nickname
if not buf.get_tag_table().lookup("nickname"):
buf.create_tag("nickname", weight=pango.WEIGHT_BOLD)
aniter = buf.get_end_iter()
offset = aniter.get_offset()
buf.insert(aniter, " " + nick + ": ")
enditer = buf.get_iter_at_offset(offset)
buf.apply_tag_by_name("nickname", aniter, enditer)
def _insert_rich_message(self, nick, msg):
msg = Emoticons.get_instance().replace(msg)
buf = self._chat_view.get_buffer()
self._insert_buddy(buf, nick)
serializer = richtext.RichTextSerializer()
serializer.deserialize(msg, buf)
aniter = buf.get_end_iter()
buf.insert(aniter, "\n")
self._message_inserted()
def _insert_sketch(self, nick, svgdata):
"""Insert a sketch object into the chat buffer."""
pbl = gtk.gdk.PixbufLoader("svg")
pbl.write(svgdata)
pbl.close()
pbuf = pbl.get_pixbuf()
buf = self._chat_view.get_buffer()
self._insert_buddy(buf, nick)
rise = int(pbuf.get_height() / 3) * -1
sha_hash = sha.new()
sha_hash.update(svgdata)
tagname = "sketch-%s" % sha_hash.hexdigest()
if not buf.get_tag_table().lookup(tagname):
buf.create_tag(tagname, rise=(rise * PANGO_SCALE))
aniter = buf.get_end_iter()
buf.insert_pixbuf(aniter, pbuf)
aniter.backward_char()
enditer = buf.get_end_iter()
buf.apply_tag_by_name(tagname, aniter, enditer)
aniter = buf.get_end_iter()
buf.insert(aniter, "\n")
self._message_inserted()
def _get_first_richtext_chunk(self, msg):
"""Scan the message for the first richtext-tagged chunk and return it."""
rt_last = -1
tag_rt_start = "<richtext>"
tag_rt_end = "</richtext>"
rt_first = msg.find(tag_rt_start)
length = -1
if rt_first >= 0:
length = len(msg)
rt_last = msg.find(tag_rt_end, rt_first)
if rt_first >= 0 and rt_last >= (rt_first + len(tag_rt_start)) and length > 0:
return msg[rt_first:rt_last + len(tag_rt_end)]
return None
def _get_first_sketch_chunk(self, msg):
"""Scan the message for the first SVG-tagged chunk and return it."""
svg_last = -1
tag_svg_start = "<svg"
tag_svg_end = "</svg>"
desc_start = msg.find("<?xml version='1.0' encoding='UTF-8'?>")
if desc_start < 0:
return None
ignore = msg.find("<!DOCTYPE svg")
if ignore < 0:
return None
svg_first = msg.find(tag_svg_start)
length = -1
if svg_first >= 0:
length = len(msg)
svg_last = msg.find(tag_svg_end, svg_first)
if svg_first >= 0 and svg_last >= (svg_first + len(tag_svg_start)) and length > 0:
return msg[desc_start:svg_last + len(tag_svg_end)]
return None
def recv_message(self, buddy, msg):
"""Insert a remote chat message into the chat buffer."""
if not buddy:
return
chunk = self._get_first_richtext_chunk(msg)
if chunk:
self._insert_rich_message(buddy.get_nick_name(), chunk)
return
chunk = self._get_first_sketch_chunk(msg)
if chunk:
self._insert_sketch(buddy.get_nick_name(), chunk)
return
def send_sketch(self, svgdata):
if not svgdata or not len(svgdata):
return
self._stream_writer.write(svgdata)
owner = self._controller.get_group().get_owner()
self._insert_sketch(owner.get_nick_name(), svgdata)
def send_text_message(self, text):
"""Send a chat message and insert it into the local buffer."""
if len(text) <= 0:
return
self._stream_writer.write(text)
owner = self._controller.get_group().get_owner()
self._insert_rich_message(owner.get_nick_name(), text)
class BuddyChat(Chat):
def __init__(self, controller, buddy):
self._buddy = buddy
self._act_name = "Chat: %s" % buddy.get_nick_name()
Chat.__init__(self, controller)
def on_connected_to_shell(self):
Chat.on_connected_to_shell(self)
self.set_can_close(True)
self.set_tab_icon(icon_name="im")
self.set_show_tab_icon(True)
self._stream_writer = self._controller.new_buddy_writer(self._buddy)
def recv_message(self, sender, msg):
Chat.recv_message(self, self._buddy, msg)
def on_close_from_user(self):
Chat.on_close_from_user(self)
del self._chats[self._buddy]
class GroupChat(Chat):
def __init__(self):
self._group = Group.get_from_id('local')
self._act_name = "Chat"
self._chats = {}
Chat.__init__(self, self)
def get_group(self):
return self._group
def new_buddy_writer(self, buddy):
service = buddy.get_service(CHAT_SERVICE_TYPE)
return self._buddy_stream.new_writer(service)
def _start(self):
name = self._group.get_owner().get_nick_name()
# Group controls the Stream for incoming messages for
# specific buddy chats
buddy_service = Service(name, CHAT_SERVICE_TYPE, CHAT_SERVICE_PORT)
self._buddy_stream = Stream.new_from_service(buddy_service, self._group)
self._buddy_stream.set_data_listener(getattr(self, "_buddy_recv_message"))
buddy_service.register(self._group)
# Group chat Stream
group_service = Service(name, GROUP_CHAT_SERVICE_TYPE,
GROUP_CHAT_SERVICE_PORT,
GROUP_CHAT_SERVICE_ADDRESS)
self._group.add_service(group_service)
self._group_stream = Stream.new_from_service(group_service, self._group)
self._group_stream.set_data_listener(self._group_recv_message)
self._stream_writer = self._group_stream.new_writer()
def _ui_setup(self, base):
Chat._ui_setup(self, base)
vbox = gtk.VBox(False, 12)
toolbox = self._create_toolbox()
vbox.pack_start(toolbox, False)
toolbox.show()
self._hbox.pack_start(vbox, False)
vbox.show()
self._plug.show_all()
def on_connected_to_shell(self):
Chat.on_connected_to_shell(self)
self.set_tab_icon(name="stock_help-chat")
self.set_show_tab_icon(True)
self._start()
def on_disconnected_from_shell(self):
Chat.on_disconnected_from_shell(self)
gtk.main_quit()
def _group_recv_message(self, buddy, msg):
self.recv_message(buddy, msg)
def _buddy_recv_message(self, buddy, msg):
if not self._chats.has_key(buddy):
chat = BuddyChat(self, buddy)
self._chats[buddy] = chat
chat.connect_to_shell()
else:
chat = self._chats[buddy]
chat.recv_message(buddy, msg)
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')
def send_text_message(self, message):
self._parent.send_text_message(message)
class ChatShell(object):
instance = None
_lock = threading.Lock()
def get_instance():
ChatShell._lock.acquire()
if not ChatShell.instance:
ChatShell.instance = ChatShell()
ChatShell._lock.release()
return ChatShell.instance
get_instance = staticmethod(get_instance)
def open_group_chat(self):
self._group_chat = GroupChat()
self._group_chat.connect_to_shell()
def send_text_message(self, message):
self._group_chat.send_text_message(message)
log_writer = LogWriter("Chat")
log_writer.start()
ChatShell.get_instance().open_group_chat()
gtk.main()

View File

@ -90,10 +90,9 @@ class PresenceService(gobject.GObject):
return PresenceService.__instance return PresenceService.__instance
get_instance = staticmethod(get_instance) get_instance = staticmethod(get_instance)
def __init__(self, debug=True): def __init__(self):
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
self._debug = debug
self._lock = threading.Lock() self._lock = threading.Lock()
self._started = False self._started = False
@ -158,9 +157,6 @@ class PresenceService(gobject.GObject):
db = dbus.Interface(self._bus.get_object(avahi.DBUS_NAME, domain_browser), avahi.DBUS_INTERFACE_DOMAIN_BROWSER) db = dbus.Interface(self._bus.get_object(avahi.DBUS_NAME, domain_browser), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
db.connect_to_signal('ItemNew', self._new_domain_cb_glue) db.connect_to_signal('ItemNew', self._new_domain_cb_glue)
def set_debug(self, debug):
self._debug = debug
def get_owner(self): def get_owner(self):
"""Return the owner of this machine/instance, if we've recognized them yet.""" """Return the owner of this machine/instance, if we've recognized them yet."""
return self._owner return self._owner
@ -634,7 +630,6 @@ def main():
import pygtk, gtk import pygtk, gtk
global ps global ps
ps = PresenceService.get_instance() ps = PresenceService.get_instance()
ps.set_debug(True)
ps.start() ps.start()
gobject.timeout_add(4000, runTests) gobject.timeout_add(4000, runTests)
gtk.main() gtk.main()

View File

@ -69,9 +69,6 @@ def deserialize(sdict):
stype = sdict['stype'] stype = sdict['stype']
if type(stype) == type(u""): if type(stype) == type(u""):
stype = stype.encode() stype = stype.encode()
activity_id = sdict['activity_id']
if type(activity_id) == type(u""):
activity_id = activity_id.encode()
domain = sdict['domain'] domain = sdict['domain']
if type(domain) == type(u""): if type(domain) == type(u""):
domain = domain.encode() domain = domain.encode()
@ -87,7 +84,18 @@ def deserialize(sdict):
address = address.encode() address = address.encode()
except KeyError: except KeyError:
pass pass
activity_id = None
try:
activity_id = sdict['activity_id']
if type(activity_id) == type(u""):
activity_id = activity_id.encode()
except KeyError:
pass
if activity_id is not None:
name = compose_service_name(name, activity_id) name = compose_service_name(name, activity_id)
return Service(name, stype, domain, address=address, return Service(name, stype, domain, address=address,
port=port, properties=properties) port=port, properties=properties)
@ -152,6 +160,7 @@ class Service(object):
else: else:
sdict['name'] = dbus.Variant(self._name) sdict['name'] = dbus.Variant(self._name)
sdict['stype'] = dbus.Variant(self._stype) sdict['stype'] = dbus.Variant(self._stype)
if self._activity_id:
sdict['activity_id'] = dbus.Variant(self._activity_id) sdict['activity_id'] = dbus.Variant(self._activity_id)
sdict['domain'] = dbus.Variant(self._domain) sdict['domain'] = dbus.Variant(self._domain)
if self._address: if self._address: