<commit_before>#!/usr/bin/python -t
# -*- tab-width: 4; indent-tabs-mode: t -*- 

import dbus
import dbus.service
import dbus.glib

import pygtk
pygtk.require('2.0')
import gtk, gobject

import sys

try:
	import activity
	from Group import *
	from StreamReader import *
	from StreamWriter import *
	from sugar_globals import *
except ImportError:
	from sugar import activity
	from sugar.Group import *
	from sugar.sugar_globals import *

import richtext

CHAT_SERVICE_TYPE = "_olpc_chat._tcp"
CHAT_SERVICE_PORT = 6100

GROUP_CHAT_SERVICE_TYPE = "_olpc_group_chat._udp"
GROUP_CHAT_SERVICE_ADDRESS = "192.168.3.11"
GROUP_CHAT_SERVICE_PORT = 6200

class Chat(activity.Activity):
	def __init__(self, controller):
		self._controller = controller
		activity.Activity.__init__(self)

	def activity_on_connected_to_shell(self):
		self.activity_set_tab_text(self._act_name)
		self._plug = self.activity_get_gtk_plug()
		self._ui_setup(self._plug)
		self._plug.show_all()
		
	def _create_chat(self):
		chat_vbox = gtk.VBox()
		chat_vbox.set_spacing(6)

		sw = gtk.ScrolledWindow()
		sw.set_shadow_type(gtk.SHADOW_IN)
		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)
		sw.add(self._chat_view)
		self._chat_view.show()
		chat_vbox.pack_start(sw)
		sw.show()

		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)
		self._editor.set_size_request(-1, 50)
		chat_view_sw.add(self._editor)
		self._editor.show()

		chat_vbox.pack_start(chat_view_sw, False)
		chat_view_sw.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, buffer] = self._create_chat()
		self._hbox.pack_start(chat_vbox)
		chat_vbox.show()
		
		vbox.pack_start(self._hbox)
		self._hbox.show()

		toolbar = self._create_toolbar(buffer)
		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()
			
			serializer = richtext.RichTextSerializer()
			text = serializer.serialize(buf)
			self.send_message(text)

			buf.set_text("")
			buf.place_cursor(buf.get_start_iter())

			return True

	def _create_toolbar(self, rich_buf):
		toolbar = richtext.RichTextToolbar(rich_buf)

		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 __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 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()
		self._controller.notify_activate(self)

	def recv_message(self, buddy, msg):
		self._insert_rich_message(buddy.get_nick_name(), msg)
		self._controller.notify_new_message(self, buddy)

	def _insert_rich_message(self, nick, msg):
		buffer = self._chat_view.get_buffer()
		aniter = buffer.get_end_iter()
		buffer.insert(aniter, nick + ": ")
		
		serializer = richtext.RichTextSerializer()
		serializer.deserialize(msg, buffer)

		aniter = buffer.get_end_iter()
		buffer.insert(aniter, "\n")

	def _local_message(self, success, text):
		if not success:
			message = "Error: %s\n" % text
			buffer = self._chat_view.get_buffer()
			aniter = buffer.get_end_iter()
			buffer.insert(aniter, message)
		else:
			nick = self._group.get_owner().get_nick_name()
			self._insert_rich_message(nick, 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 _start(self):
		service_name = self._buddy.get_service_name()
		group = self._controller.get_group()
		self._stream_writer = StreamWriter(group, service_name)

	def activity_on_connected_to_shell(self):
		Chat.activity_on_connected_to_shell(self)
		self.activity_set_can_close(True)
		self.activity_set_tab_icon_name("im")
		self.activity_show_icon(True)
		self._start()
		
	def recv_message(self, sender, msg):
		Chat.recv_message(self, self._buddy, msg)

	def send_message(self, text):
		if len(text) > 0:
			success = self._stream_writer.write(text)
			self._local_message(success, text)

	def activity_on_close_from_user(self):
		Chat.activity_on_close_from_user(self)
		del self._chats[self._buddy]
			
class GroupChat(Chat):<commit_msg>Use the chat service, get to it by the nick name<commit_after>#!/usr/bin/python -t
# -*- tab-width: 4; indent-tabs-mode: t -*- 

import dbus
import dbus.service
import dbus.glib

import pygtk
pygtk.require('2.0')
import gtk, gobject

import sys

try:
	import activity
	from Group import *
	from StreamReader import *
	from StreamWriter import *
	from sugar_globals import *
except ImportError:
	from sugar import activity
	from sugar.Group import *
	from sugar.sugar_globals import *

import richtext

CHAT_SERVICE_TYPE = "_olpc_chat._tcp"
CHAT_SERVICE_PORT = 6100

GROUP_CHAT_SERVICE_TYPE = "_olpc_group_chat._udp"
GROUP_CHAT_SERVICE_ADDRESS = "192.168.3.11"
GROUP_CHAT_SERVICE_PORT = 6200

class Chat(activity.Activity):
	def __init__(self, controller):
		self._controller = controller
		activity.Activity.__init__(self)

	def activity_on_connected_to_shell(self):
		self.activity_set_tab_text(self._act_name)
		self._plug = self.activity_get_gtk_plug()
		self._ui_setup(self._plug)
		self._plug.show_all()
		
	def _create_chat(self):
		chat_vbox = gtk.VBox()
		chat_vbox.set_spacing(6)

		sw = gtk.ScrolledWindow()
		sw.set_shadow_type(gtk.SHADOW_IN)
		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)
		sw.add(self._chat_view)
		self._chat_view.show()
		chat_vbox.pack_start(sw)
		sw.show()

		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)
		self._editor.set_size_request(-1, 50)
		chat_view_sw.add(self._editor)
		self._editor.show()

		chat_vbox.pack_start(chat_view_sw, False)
		chat_view_sw.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, buffer] = self._create_chat()
		self._hbox.pack_start(chat_vbox)
		chat_vbox.show()
		
		vbox.pack_start(self._hbox)
		self._hbox.show()

		toolbar = self._create_toolbar(buffer)
		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()
			
			serializer = richtext.RichTextSerializer()
			text = serializer.serialize(buf)
			self.send_message(text)

			buf.set_text("")
			buf.place_cursor(buf.get_start_iter())

			return True

	def _create_toolbar(self, rich_buf):
		toolbar = richtext.RichTextToolbar(rich_buf)

		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 __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 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()
		self._controller.notify_activate(self)

	def recv_message(self, buddy, msg):
		self._insert_rich_message(buddy.get_nick_name(), msg)
		self._controller.notify_new_message(self, buddy)

	def _insert_rich_message(self, nick, msg):
		buffer = self._chat_view.get_buffer()
		aniter = buffer.get_end_iter()
		buffer.insert(aniter, nick + ": ")
		
		serializer = richtext.RichTextSerializer()
		serializer.deserialize(msg, buffer)

		aniter = buffer.get_end_iter()
		buffer.insert(aniter, "\n")

	def _local_message(self, success, text):
		if not success:
			message = "Error: %s\n" % text
			buffer = self._chat_view.get_buffer()
			aniter = buffer.get_end_iter()
			buffer.insert(aniter, message)
		else:
			nick = self._group.get_owner().get_nick_name()
			self._insert_rich_message(nick, 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 _start(self):
		group = self._controller.get_group()
		buddy_name = self._buddy.get_service_name()
		service = group.get_service(buddy_name, CHAT_SERVICE_TYPE)
		self._stream_writer = StreamWriter(group, service)

	def activity_on_connected_to_shell(self):
		Chat.activity_on_connected_to_shell(self)
		self.activity_set_can_close(True)
		self.activity_set_tab_icon_name("im")
		self.activity_show_icon(True)
		self._start()
		
	def recv_message(self, sender, msg):
		Chat.recv_message(self, self._buddy, msg)

	def send_message(self, text):
		if len(text) > 0:
			success = self._stream_writer.write(text)
			self._local_message(success, text)

	def activity_on_close_from_user(self):
		Chat.activity_on_close_from_user(self)
		del self._chats[self._buddy]
			
class GroupChat(Chat):
{'fraction_non_alphanumeric': 0.11787037873994395, 'fraction_numerical': 0.005604266473831691, 'mean_word_length': 5.0591456736035045, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3891855', 'n_tokens_mistral': 4518, 'n_tokens_neox': 4095, 'n_words': 670}