Add an emoticons menu

This commit is contained in:
Marco Pesenti Gritti 2006-05-21 22:20:37 -04:00
parent 3841ac9983
commit 325fb8ff25
7 changed files with 102 additions and 133 deletions

View File

@ -2,7 +2,6 @@
# -*- tab-width: 4; indent-tabs-mode: t -*-
import sys
import base64
import dbus
import dbus.service
@ -37,6 +36,8 @@ class Chat(activity.Activity):
self._controller = controller
activity.Activity.__init__(self)
self._stream_writer = None
self._emt_popup = None
def activity_on_connected_to_shell(self):
self.activity_set_tab_text(self._act_name)
@ -49,7 +50,6 @@ class Chat(activity.Activity):
toolbox = Toolbox()
toolbox.connect('tool-selected', self._tool_selected)
toolbox.connect('color-selected', self._color_selected)
vbox.pack_start(toolbox, False)
toolbox.show()
@ -66,9 +66,6 @@ class Chat(activity.Activity):
def __send_button_clicked_cb(self, button):
self.send_sketch(self._sketchpad.to_svg())
def _color_selected(self, toolbox, color):
self._sketchpad.set_color(color)
def _tool_selected(self, toolbox, tool_id):
if tool_id == 'text':
@ -158,9 +155,64 @@ class Chat(activity.Activity):
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)
@ -168,6 +220,26 @@ class Chat(activity.Activity):
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()
@ -296,7 +368,7 @@ class BuddyChat(Chat):
self.activity_set_tab_icon_name("im")
self.activity_show_icon(True)
self._stream_writer = self._controller.new_buddy_writer(self._buddy.get_service_name())
def recv_message(self, sender, msg):
Chat.recv_message(self, self._buddy, msg)
self._controller.notify_new_message(self, self._buddy)
@ -304,21 +376,7 @@ class BuddyChat(Chat):
def activity_on_close_from_user(self):
Chat.activity_on_close_from_user(self)
del self._chats[self._buddy]
class BuddyIconRequestHandler(object):
def __init__(self, group, stream):
self._group = group
self._stream = stream
self._stream.register_handler(self._handle_buddy_icon_request, "get_buddy_icon")
def _handle_buddy_icon_request(self):
"""XMLRPC method, return the owner's icon encoded with base64."""
icon = self._group.get_owner().get_icon()
if icon:
return base64.b64encode(icon)
return ''
class GroupChat(Chat):
@ -345,8 +403,7 @@ class GroupChat(Chat):
def _start(self):
self._group = LocalGroup()
self._group.add_presence_listener(self._on_group_presence_event)
self._group.add_service_listener(self._on_group_service_event)
self._group.add_presence_listener(self._on_group_event)
self._group.join()
name = self._group.get_owner().get_service_name()
@ -355,7 +412,6 @@ class GroupChat(Chat):
# 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_icon_handler = BuddyIconRequestHandler(self._group, self._buddy_stream)
self._buddy_stream.set_data_listener(self._buddy_recv_message)
buddy_service.register(self._group)
@ -363,8 +419,8 @@ class GroupChat(Chat):
group_service = Service(name, GROUP_CHAT_SERVICE_TYPE,
GROUP_CHAT_SERVICE_PORT,
GROUP_CHAT_SERVICE_ADDRESS)
self._group.add_service(group_service)
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()
@ -452,6 +508,7 @@ class GroupChat(Chat):
def _on_buddyList_buddy_selected(self, widget, *args):
(model, aniter) = widget.get_selection().get_selected()
name = self._buddy_list_model.get(aniter, self._MODEL_COL_NICK)
print "Selected %s" % name
def _on_buddyList_buddy_double_clicked(self, widget, *args):
""" Select the chat for this buddy or group """
@ -463,28 +520,7 @@ class GroupChat(Chat):
self._chats[buddy] = chat
chat.activity_connect_to_shell()
def _request_buddy_icon(self, buddy):
writer = self.new_buddy_writer(buddy.get_service_name())
icon = writer.custom_request("get_buddy_icon")
if icon and len(icon):
icon = base64.b64decode(icon)
print "Setting buddy icon for '%s' to %s" % (buddy.get_nick_name(), icon)
buddy.set_icon(icon)
def _on_group_service_event(self, action, service):
if action == Group.SERVICE_ADDED:
# Look for the olpc chat service
if service.get_type() == CHAT_SERVICE_TYPE:
# Find the buddy this service belongs to
buddy = self._group.get_buddy(service.get_name())
if buddy and buddy.get_address() == service.get_address():
# Try to get the buddy's icon
if buddy.get_nick_name() != self._group.get_owner().get_nick_name():
self._request_buddy_icon(buddy)
elif action == Group.SERVICE_REMOVED:
pass
def _on_group_presence_event(self, action, buddy):
def _on_group_event(self, action, buddy):
if buddy.get_nick_name() == self._group.get_owner().get_nick_name():
# Do not show ourself in the buddy list
pass

View File

@ -1,27 +1,25 @@
from SVGdraw import path
class Sketch:
def __init__(self, rgb):
def __init__(self):
self._points = []
self._rgb = (float(rgb[0]), float(rgb[1]), float(rgb[2]))
def add_point(self, x, y):
self._points.append((x, y))
self._points.append([x, y])
def draw(self, ctx):
start = True
for (x, y) in self._points:
for [x, y] in self._points:
if start:
ctx.move_to(x, y)
start = False
else:
ctx.line_to(x, y)
ctx.set_source_rgb(self._rgb[0], self._rgb[1], self._rgb[2])
ctx.stroke()
def draw_to_svg(self):
i = 0
for (x, y) in self._points:
for [x, y] in self._points:
coords = str(x) + ' ' + str(y) + ' '
if i == 0:
path_data = 'M ' + coords
@ -30,5 +28,4 @@ class Sketch:
else:
path_data += coords
i += 1
color = "#%02X%02X%02X" % (255 * self._rgb[0], 255 * self._rgb[1], 255 * self._rgb[2])
return path(path_data, fill = 'none', stroke = color)
return path(path_data, fill = 'none', stroke = '#000000')

View File

@ -13,7 +13,6 @@ class SketchPad(gtk.DrawingArea):
gtk.DrawingArea.__init__(self)
self._active_sketch = None
self._rgb = (0.0, 0.0, 0.0)
self._sketches = []
self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
@ -24,7 +23,6 @@ class SketchPad(gtk.DrawingArea):
self.connect('expose_event', self.expose)
def expose(self, widget, event):
"""Draw the background of the sketchpad."""
rect = self.get_allocation()
ctx = widget.window.cairo_create()
@ -40,11 +38,6 @@ class SketchPad(gtk.DrawingArea):
return False
def set_color(self, color):
"""Sets the current drawing color of the sketchpad.
color agument should be 3-item tuple of rgb values between 0 and 1."""
self._rgb = color
def add_sketch(self, sketch):
self._sketches.append(sketch)
@ -54,7 +47,7 @@ class SketchPad(gtk.DrawingArea):
self.window.invalidate_rect(None, False)
def __button_press_cb(self, widget, event):
self._active_sketch = Sketch(self._rgb)
self._active_sketch = Sketch()
self.add_sketch(self._active_sketch)
self.add_point(event)
@ -66,7 +59,6 @@ class SketchPad(gtk.DrawingArea):
self.add_point(event)
def to_svg(self):
"""Return a string containing an SVG representation of this sketch."""
d = drawing()
s = svg()
for sketch in self._sketches:

View File

@ -18,9 +18,6 @@ class ColorButton(gtk.RadioButton):
self.add(drawing_area)
drawing_area.show()
def color(self):
return self._rgb
def expose(self, widget, event):
rect = widget.get_allocation()
ctx = widget.window.cairo_create()
@ -34,9 +31,7 @@ class ColorButton(gtk.RadioButton):
class Toolbox(gtk.VBox):
__gsignals__ = {
'tool-selected': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_STRING])),
'color-selected': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
([gobject.TYPE_STRING]))
}
def __init__(self):
@ -101,5 +96,4 @@ class Toolbox(gtk.VBox):
self.emit("tool-selected", tool_id)
def __color_clicked_cb(self, button, rgb):
self.emit("color-selected", button.color())
pass

View File

@ -6,35 +6,18 @@ from Service import Service
PRESENCE_SERVICE_TYPE = "_olpc_presence._tcp"
PRESENCE_SERVICE_PORT = 6000
class BuddyBase:
class Buddy:
def __init__(self, service, nick_name):
self._service = service
self._nick_name = nick_name
def get_icon(self):
"""Return the buddies icon, if any."""
return self._icon
def get_address(self):
return self._service.get_address()
def get_service_name(self):
return self._service.get_name()
def get_nick_name(self):
return self._nick_name
class Buddy(BuddyBase):
"""Normal buddy class."""
def set_icon(self, icon):
"""Can only set icon for other buddies. The Owner
takes care of setting it's own icon."""
self._icon = icon
class Owner(BuddyBase):
"""Class representing the owner of this machine/instance."""
class Owner(Buddy):
def __init__(self, group):
self._group = group
@ -43,18 +26,8 @@ class Owner(BuddyBase):
nick = "n00b"
service = Service(nick, PRESENCE_SERVICE_TYPE, PRESENCE_SERVICE_PORT)
BuddyBase.__init__(self, service, nick)
sugar_dir = os.path.abspath(os.path.expanduser("~/.sugar"))
icon = None
for fname in os.listdir(sugar_dir):
if not fname.startswith("buddy-icon."):
continue
fd = open(os.path.join(sugar_dir, fname), "r")
self._icon = fd.read()
fd.close()
break
Buddy.__init__(self, service, nick)
def register(self):
self._service.register(self._group)

View File

@ -7,8 +7,6 @@ from Service import Service
from sugar.p2p.model.Store import Store
import presence
_OLPC_SERVICE_TYPE_PREFIX = "_olpc"
class Group:
SERVICE_ADDED = "service_added"
SERVICE_REMOVED = "service_removed"
@ -80,12 +78,11 @@ class LocalGroup(Group):
def get_service(self, name, stype):
if self._services.has_key((name, stype)):
return self._services[(name, stype)]
return None
else:
return None
def get_buddy(self, name):
if self._buddies.has_key(name):
return self._buddies[name]
return None
return self._buddies[name]
def _add_buddy(self, buddy):
bid = buddy.get_nick_name()
@ -107,7 +104,7 @@ class LocalGroup(Group):
elif action == presence.ACTION_SERVICE_REMOVED:
if stype == PRESENCE_SERVICE_TYPE:
self._remove_buddy(name)
elif stype.startswith(_OLPC_SERVICE_TYPE_PREFIX):
elif stype.startswith("_olpc"):
self.remove_service((name, stype))
def _on_service_resolved(self, interface, protocol, name, stype, domain,
@ -122,5 +119,5 @@ class LocalGroup(Group):
if stype == PRESENCE_SERVICE_TYPE:
self._add_buddy(Buddy(service, name))
elif stype.startswith(_OLPC_SERVICE_TYPE_PREFIX):
elif stype.startswith("_olpc"):
self.add_service(service)

View File

@ -1,14 +1,11 @@
import xmlrpclib
import socket
import traceback
import network
from MostlyReliablePipe import MostlyReliablePipe
class Stream(object):
def __init__(self, service, group):
if not service:
raise ValueError("service must be valid")
self._service = service
self._group = group
self._owner_nick_name = self._group.get_owner().get_nick_name()
@ -35,8 +32,6 @@ class Stream(object):
class UnicastStreamWriter(object):
def __init__(self, stream, service, owner_nick_name):
# set up the writer
if not service:
raise ValueError("service must be valid")
self._service = service
self._owner_nick_name = owner_nick_name
self._address = self._service.get_address()
@ -45,7 +40,6 @@ class UnicastStreamWriter(object):
self._writer = xmlrpclib.ServerProxy(self._xmlrpc_addr)
def write(self, data):
"""Write some data to the default endpoint of this pipe on the remote server."""
try:
self._writer.message(self._owner_nick_name, data)
return True
@ -53,15 +47,6 @@ class UnicastStreamWriter(object):
traceback.print_exc()
return False
def custom_request(self, method_name, *args):
"""Call a custom XML-RPC method on the remote server."""
try:
method = getattr(self._writer, method_name)
return method(*args)
except (socket.error, xmlrpclib.Fault, xmlrpclib.ProtocolError):
traceback.print_exc()
return None
class UnicastStream(Stream):
def __init__(self, service, group):
@ -77,23 +62,18 @@ class UnicastStream(Stream):
while not started and tries > 0:
try:
self._reader = network.GlibXMLRPCServer(("", port))
self._reader.register_function(self._message, "message")
self._reader.register_instance(self)
started = True
except(socket.error):
port = port + 1
tries = tries - 1
self._service.set_port(port)
def _message(self, nick_name, message):
def message(self, nick_name, message):
"""Called by the XMLRPC server when network data arrives."""
self.recv(nick_name, message)
return True
def register_handler(self, handler, name):
if name == "message":
raise ValueError("Handler name 'message' is a reserved handler.")
self._reader.register_function(handler, name)
def new_writer(self, service):
return UnicastStreamWriter(self, service, self._owner_nick_name)