2006-04-19 20:43:40 +02:00
|
|
|
#!/usr/bin/python -t
|
2006-04-21 21:30:49 +02:00
|
|
|
# -*- tab-width: 4; indent-tabs-mode: t -*-
|
2006-04-19 20:43:40 +02:00
|
|
|
|
2006-04-21 22:16:47 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
import dbus.glib
|
|
|
|
|
|
|
|
import pygtk
|
|
|
|
pygtk.require('2.0')
|
2006-04-19 20:43:40 +02:00
|
|
|
import gtk, gobject
|
|
|
|
|
2006-04-21 22:16:47 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import pwd
|
|
|
|
import gc
|
2006-04-24 19:08:18 +02:00
|
|
|
import socket
|
2006-04-19 20:43:40 +02:00
|
|
|
|
2006-04-21 22:16:47 +02:00
|
|
|
sys.path.append(os.getcwd())
|
|
|
|
sys.path.append('../shell/example-activity/')
|
|
|
|
import activity
|
2006-04-19 20:43:40 +02:00
|
|
|
|
2006-04-21 22:16:47 +02:00
|
|
|
import presence
|
|
|
|
import BuddyList
|
|
|
|
import network
|
2006-04-23 07:00:49 +02:00
|
|
|
import richtext
|
2006-04-24 19:08:18 +02:00
|
|
|
import xmlrpclib
|
2006-04-21 21:30:49 +02:00
|
|
|
|
|
|
|
class Chat(object):
|
2006-04-24 19:08:18 +02:00
|
|
|
def __init__(self, parent, view, label):
|
|
|
|
self._parent = parent
|
2006-04-23 07:00:49 +02:00
|
|
|
self._buffer = richtext.RichTextBuffer()
|
2006-04-21 21:30:49 +02:00
|
|
|
self._view = view
|
|
|
|
self._label = label
|
|
|
|
|
2006-04-24 19:08:18 +02:00
|
|
|
def error_message(self, msg):
|
|
|
|
aniter = self._buffer.get_end_iter()
|
|
|
|
self._buffer.insert(aniter, "Error: %s\n" % msg)
|
|
|
|
|
2006-04-21 21:30:49 +02:00
|
|
|
def activate(self, label):
|
|
|
|
self._view.set_buffer(self._buffer)
|
|
|
|
self._label.set_text(label)
|
|
|
|
|
|
|
|
def recv_message(self, buddy, msg):
|
|
|
|
aniter = self._buffer.get_end_iter()
|
2006-04-23 07:00:49 +02:00
|
|
|
self._buffer.insert(aniter, buddy.nick() + ": ")
|
|
|
|
|
|
|
|
serializer = richtext.RichTextSerializer()
|
|
|
|
serializer.deserialize(msg, self._buffer)
|
2006-04-21 21:30:49 +02:00
|
|
|
|
2006-04-23 07:00:49 +02:00
|
|
|
aniter = self._buffer.get_end_iter()
|
|
|
|
self._buffer.insert(aniter, "\n")
|
2006-04-21 21:30:49 +02:00
|
|
|
|
2006-04-24 19:08:18 +02:00
|
|
|
class BuddyChat(Chat):
|
|
|
|
def __init__(self, parent, buddy, view, label):
|
|
|
|
self._buddy = buddy
|
|
|
|
Chat.__init__(self, parent, view, label)
|
|
|
|
|
|
|
|
def activate(self):
|
|
|
|
Chat.activate(self, self._buddy.nick())
|
|
|
|
|
|
|
|
def recv_message(self, msg):
|
|
|
|
Chat.recv_message(self, self._buddy, msg)
|
|
|
|
|
|
|
|
def send_message(self, text):
|
|
|
|
if len(text) <= 0:
|
|
|
|
return
|
|
|
|
addr = "http://%s:%d" % (self._buddy.address(), self._buddy.port())
|
|
|
|
peer = xmlrpclib.ServerProxy(addr)
|
|
|
|
msg = None
|
|
|
|
success = True
|
|
|
|
try:
|
|
|
|
peer.message(text)
|
|
|
|
except socket.error, e:
|
|
|
|
msg = str(e)
|
|
|
|
success = False
|
|
|
|
return (success, msg)
|
|
|
|
|
2006-04-21 21:30:49 +02:00
|
|
|
class GroupChat(Chat):
|
|
|
|
def __init__(self, parent, view, label):
|
2006-04-24 19:08:18 +02:00
|
|
|
Chat.__init__(self, parent, view, label)
|
2006-04-21 21:30:49 +02:00
|
|
|
self._gc_controller = network.GroupChatController('224.0.0.221', 6666, self._recv_group_message)
|
|
|
|
self._gc_controller.start()
|
|
|
|
|
|
|
|
def activate(self):
|
|
|
|
Chat.activate(self, "Group Chat")
|
|
|
|
|
|
|
|
def send_message(self, text):
|
|
|
|
if len(text) > 0:
|
|
|
|
self._gc_controller.send_msg(text)
|
2006-04-24 19:08:18 +02:00
|
|
|
return (True, None)
|
2006-04-21 21:30:49 +02:00
|
|
|
|
|
|
|
def _recv_group_message(self, msg):
|
|
|
|
buddy = self._parent.find_buddy_by_address(msg['addr'])
|
|
|
|
if buddy:
|
|
|
|
self.recv_message(buddy, msg['data'])
|
|
|
|
|
2006-04-24 19:08:18 +02:00
|
|
|
|
|
|
|
class ChatRequestHandler(object):
|
2006-04-24 19:19:10 +02:00
|
|
|
def __init__(self, parent, chat_view, chat_label):
|
2006-04-24 19:08:18 +02:00
|
|
|
self._parent = parent
|
2006-04-24 19:19:10 +02:00
|
|
|
self._chat_view = chat_view
|
|
|
|
self._chat_label = chat_label
|
2006-04-24 19:08:18 +02:00
|
|
|
|
|
|
|
def message(self, message):
|
|
|
|
client_address = network.get_authinfo()
|
|
|
|
buddy = self._parent.find_buddy_by_address(client_address[0])
|
|
|
|
if buddy:
|
2006-04-24 19:19:10 +02:00
|
|
|
chat = buddy.chat()
|
|
|
|
if not chat:
|
|
|
|
chat = BuddyChat(self, buddy, self._chat_view, self._chat_label)
|
|
|
|
buddy.set_chat(chat)
|
|
|
|
chat.recv_message(message)
|
|
|
|
return True
|
2006-04-24 19:08:18 +02:00
|
|
|
|
2006-04-21 22:16:47 +02:00
|
|
|
class ChatActivity(activity.Activity):
|
|
|
|
def __init__(self):
|
|
|
|
activity.Activity.__init__(self)
|
|
|
|
self._act_name = "Chat"
|
2006-04-19 20:43:40 +02:00
|
|
|
self._pannounce = presence.PresenceAnnounce()
|
2006-04-20 21:33:30 +02:00
|
|
|
self._buddy_list = BuddyList.BuddyList()
|
|
|
|
self._buddy_list.add_buddy_listener(self._on_buddy_presence_event)
|
2006-04-19 20:43:40 +02:00
|
|
|
|
|
|
|
(self._nick, self._realname) = self._get_name()
|
|
|
|
|
2006-04-21 22:16:47 +02:00
|
|
|
def _ui_setup(self, plug):
|
2006-04-23 07:00:49 +02:00
|
|
|
hbox = gtk.HBox(False, 6)
|
|
|
|
|
|
|
|
chat_vbox = gtk.VBox()
|
|
|
|
hbox.pack_start(chat_vbox)
|
|
|
|
chat_vbox.show()
|
|
|
|
|
|
|
|
self._chat_label = gtk.Label()
|
|
|
|
chat_vbox.pack_start(self._chat_label, False)
|
|
|
|
self._chat_label.show()
|
|
|
|
|
|
|
|
sw = gtk.ScrolledWindow()
|
|
|
|
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_ALWAYS)
|
|
|
|
self._chat_view = gtk.TextView()
|
|
|
|
sw.add(self._chat_view)
|
|
|
|
self._chat_view.show()
|
|
|
|
chat_vbox.pack_start(sw)
|
|
|
|
sw.show()
|
|
|
|
|
|
|
|
rich_buf = richtext.RichTextBuffer()
|
|
|
|
chat_view_sw = gtk.ScrolledWindow()
|
|
|
|
chat_view_sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
|
|
|
|
self._editor = gtk.TextView(rich_buf)
|
|
|
|
self._editor.connect("key-press-event", self.__key_press_event_cb)
|
|
|
|
self._editor.set_size_request(-1, 100)
|
|
|
|
chat_view_sw.add(self._editor)
|
|
|
|
self._editor.show()
|
|
|
|
|
|
|
|
toolbar = richtext.RichTextToolbar(rich_buf)
|
|
|
|
chat_vbox.pack_start(toolbar, False);
|
|
|
|
toolbar.show()
|
|
|
|
|
|
|
|
chat_vbox.pack_start(chat_view_sw, False);
|
|
|
|
chat_view_sw.show()
|
|
|
|
|
2006-04-21 22:16:47 +02:00
|
|
|
self._buddy_list_model = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
|
2006-04-23 07:00:49 +02:00
|
|
|
sw = gtk.ScrolledWindow()
|
|
|
|
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
|
|
|
|
self._buddy_list_view = gtk.TreeView(self._buddy_list_model)
|
2006-04-21 22:16:47 +02:00
|
|
|
self._buddy_list_view.connect("cursor-changed", self._on_buddyList_buddy_selected)
|
|
|
|
self._buddy_list_view.connect("row-activated", self._on_buddyList_buddy_double_clicked)
|
2006-04-23 07:00:49 +02:00
|
|
|
sw.set_size_request(150, -1)
|
|
|
|
sw.add(self._buddy_list_view)
|
|
|
|
self._buddy_list_view.show()
|
|
|
|
hbox.pack_start(sw, False)
|
|
|
|
sw.show()
|
2006-04-21 22:16:47 +02:00
|
|
|
|
|
|
|
renderer = gtk.CellRendererText()
|
|
|
|
column = gtk.TreeViewColumn("", renderer, text=0)
|
|
|
|
column.set_resizable(True)
|
|
|
|
column.set_sizing("GTK_TREE_VIEW_COLUMN_GROW_ONLY");
|
|
|
|
column.set_expand(True);
|
|
|
|
self._buddy_list_view.append_column(column)
|
|
|
|
|
|
|
|
self._group_chat = GroupChat(self, self._chat_view, self._chat_label)
|
|
|
|
aniter = self._buddy_list_model.append(None)
|
|
|
|
self._buddy_list_model.set(aniter, 0, "Group", 1, None)
|
|
|
|
self._group_chat.activate()
|
2006-04-23 07:00:49 +02:00
|
|
|
plug.add(hbox)
|
|
|
|
|
|
|
|
hbox.show()
|
|
|
|
|
|
|
|
def __key_press_event_cb(self, text_view, event):
|
|
|
|
if event.keyval == gtk.keysyms.Return:
|
|
|
|
buf = text_view.get_buffer()
|
|
|
|
chat = self._get_current_chat()
|
|
|
|
|
|
|
|
serializer = richtext.RichTextSerializer()
|
|
|
|
text = serializer.serialize(buf)
|
2006-04-24 19:08:18 +02:00
|
|
|
(success, msg) = chat.send_message(text)
|
|
|
|
if not success:
|
|
|
|
chat.error_message(msg)
|
|
|
|
|
2006-04-23 07:00:49 +02:00
|
|
|
buf.set_text("")
|
|
|
|
buf.place_cursor(buf.get_start_iter())
|
|
|
|
|
|
|
|
return True
|
2006-04-21 22:16:47 +02:00
|
|
|
|
|
|
|
def _start(self):
|
|
|
|
self._buddy_list.start()
|
2006-04-24 19:08:18 +02:00
|
|
|
print "Starting announce."
|
2006-04-21 22:16:47 +02:00
|
|
|
self._pannounce.register_service(self._realname, 6666, presence.OLPC_CHAT_SERVICE,
|
|
|
|
name = self._nick, realname = self._realname)
|
2006-04-24 19:08:18 +02:00
|
|
|
print "Done announce."
|
|
|
|
|
|
|
|
# Create the P2P chat XMLRPC server
|
2006-04-24 19:19:10 +02:00
|
|
|
self._p2p_req_handler = ChatRequestHandler(self, self._chat_view, self._chat_label)
|
2006-04-24 19:08:18 +02:00
|
|
|
self._p2p_server = network.GlibXMLRPCServer(("", 6666))
|
|
|
|
self._p2p_server.register_instance(self._p2p_req_handler)
|
2006-04-21 22:16:47 +02:00
|
|
|
|
|
|
|
def activity_on_connected_to_shell(self):
|
|
|
|
print "act %d: in activity_on_connected_to_shell" % self.activity_get_id()
|
|
|
|
self.activity_set_tab_text(self._act_name)
|
|
|
|
self._plug = self.activity_get_gtk_plug()
|
|
|
|
self._ui_setup(self._plug)
|
|
|
|
self._plug.show_all()
|
|
|
|
self._start()
|
|
|
|
|
|
|
|
def activity_on_disconnected_from_shell(self):
|
|
|
|
print "act %d: in activity_on_disconnected_from_shell"%self.activity_get_id()
|
|
|
|
print "act %d: Shell disappeared..."%self.activity_get_id()
|
|
|
|
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
def activity_on_close_from_user(self):
|
|
|
|
print "act %d: in activity_on_close_from_user"%self.activity_get_id()
|
|
|
|
self.activity_shutdown()
|
|
|
|
|
|
|
|
def activity_on_lost_focus(self):
|
|
|
|
print "act %d: in activity_on_lost_focus"%self.activity_get_id()
|
|
|
|
|
|
|
|
def activity_on_got_focus(self):
|
|
|
|
print "act %d: in activity_on_got_focus"%self.activity_get_id()
|
2006-04-19 20:43:40 +02:00
|
|
|
|
|
|
|
def _get_name(self):
|
|
|
|
ent = pwd.getpwuid(os.getuid())
|
|
|
|
nick = ent[0]
|
|
|
|
if not nick or not len(nick):
|
|
|
|
nick = "n00b"
|
|
|
|
realname = ent[4]
|
|
|
|
if not realname or not len(realname):
|
|
|
|
realname = "Some Clueless User"
|
|
|
|
return (nick, realname)
|
|
|
|
|
2006-04-20 21:33:30 +02:00
|
|
|
def _on_buddyList_buddy_selected(self, widget, *args):
|
2006-04-19 20:43:40 +02:00
|
|
|
(model, aniter) = widget.get_selection().get_selected()
|
2006-04-21 22:16:47 +02:00
|
|
|
name = self._buddy_list_model.get(aniter,0)
|
2006-04-19 20:43:40 +02:00
|
|
|
print "Selected %s" % name
|
|
|
|
|
2006-04-20 21:33:30 +02:00
|
|
|
def _on_buddyList_buddy_double_clicked(self, widget, *args):
|
2006-04-21 21:30:49 +02:00
|
|
|
""" Select the chat for this buddy or group """
|
2006-04-19 20:43:40 +02:00
|
|
|
(model, aniter) = widget.get_selection().get_selected()
|
2006-04-21 21:30:49 +02:00
|
|
|
chat = None
|
2006-04-21 22:16:47 +02:00
|
|
|
buddy = self._buddy_list_model.get_value(aniter, 1)
|
2006-04-21 21:30:49 +02:00
|
|
|
if not buddy:
|
|
|
|
chat = self._group_chat
|
|
|
|
else:
|
|
|
|
chat = buddy.chat()
|
2006-04-24 19:08:18 +02:00
|
|
|
if not chat:
|
|
|
|
chat = BuddyChat(self, buddy, self._chat_view, self._chat_label)
|
|
|
|
buddy.set_chat(chat)
|
2006-04-21 21:30:49 +02:00
|
|
|
|
2006-04-24 19:08:18 +02:00
|
|
|
chat.activate()
|
2006-04-19 20:43:40 +02:00
|
|
|
|
2006-04-20 21:33:30 +02:00
|
|
|
def _on_buddy_presence_event(self, action, buddy):
|
|
|
|
if action == BuddyList.ACTION_BUDDY_ADDED:
|
2006-04-21 22:16:47 +02:00
|
|
|
aniter = self._buddy_list_model.append(None)
|
|
|
|
self._buddy_list_model.set(aniter, 0, buddy.nick(), 1, buddy)
|
2006-04-24 19:42:01 +02:00
|
|
|
elif action == BuddyList.ACTION_BUDDY_REMOVED:
|
|
|
|
aniter = self._buddy_list_model.get_iter_first()
|
|
|
|
while aniter:
|
|
|
|
list_buddy = self._buddy_list_model.get_value(aniter, 1)
|
|
|
|
if buddy == list_buddy:
|
|
|
|
self._buddy_list_model.remove(aniter)
|
|
|
|
break
|
|
|
|
aniter = self._buddy_list_model.iter_next(aniter)
|
2006-04-20 21:33:30 +02:00
|
|
|
|
2006-04-21 21:30:49 +02:00
|
|
|
def find_buddy_by_address(self, address):
|
|
|
|
return self._buddy_list.find_buddy_by_address(address)
|
|
|
|
|
2006-04-20 21:33:30 +02:00
|
|
|
def _on_main_window_delete(self, widget, *args):
|
2006-04-19 20:43:40 +02:00
|
|
|
self.quit()
|
|
|
|
|
2006-04-21 21:30:49 +02:00
|
|
|
def _get_current_chat(self):
|
2006-04-21 22:16:47 +02:00
|
|
|
selection = self._buddy_list_view.get_selection()
|
2006-04-21 21:30:49 +02:00
|
|
|
(model, aniter) = selection.get_selected()
|
2006-04-21 22:24:46 +02:00
|
|
|
buddy = None
|
|
|
|
if aniter:
|
|
|
|
buddy = model.get_value(aniter, 1)
|
2006-04-21 21:30:49 +02:00
|
|
|
if not buddy:
|
|
|
|
return self._group_chat
|
|
|
|
return buddy.chat()
|
2006-04-19 20:43:40 +02:00
|
|
|
|
2006-04-21 22:16:47 +02:00
|
|
|
def run(self):
|
2006-04-24 16:43:45 +02:00
|
|
|
try:
|
|
|
|
gtk.main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2006-04-19 20:43:40 +02:00
|
|
|
|
|
|
|
def main():
|
2006-04-21 22:16:47 +02:00
|
|
|
app = ChatActivity()
|
|
|
|
app.activity_connect_to_shell()
|
2006-04-19 20:43:40 +02:00
|
|
|
app.run()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|