/usr/share/sugar/activities/Chat.activity/activity.py is in sugar-chat-activity 84-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 | # -*- coding: utf-8 -*-
# Copyright 2007-2008 One Laptop Per Child
# Copyright 2009-14 Sugar Labs
# Copyright 2014, Walter Bender
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from gi.repository import GObject
try:
gi.require_version('Gst', '1.0')
from gi.repository import Gst
_HAS_SOUND = True
except:
_HAS_SOUND = False
OSK_HEIGHT = [400, 300]
SLASH = '-x-SLASH-x-' # slash safe encoding
import logging
import json
import os
import subprocess
import time
from gettext import gettext as _
from telepathy.interfaces import CHANNEL_INTERFACE
from telepathy.interfaces import CHANNEL_INTERFACE_GROUP
from telepathy.interfaces import CHANNEL_TYPE_TEXT
from telepathy.interfaces import CONN_INTERFACE_ALIASING
from telepathy.constants import CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES
from telepathy.constants import CHANNEL_TEXT_MESSAGE_TYPE_NORMAL
from telepathy.client import Connection
from telepathy.client import Channel
from sugar3.graphics import style
from sugar3.graphics.icon import EventIcon, Icon
from sugar3.graphics.alert import NotifyAlert
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.toolbutton import ToolButton
from sugar3.activity import activity
from sugar3.activity.activity import get_bundle_path
from sugar3.presence import presenceservice
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.activity.activity import get_activity_root
from sugar3.activity.activity import show_object_in_journal
from sugar3.datastore import datastore
from sugar3 import profile
from chat import smilies
from chat.box import ChatBox
from utils import EbookModeDetector
logger = logging.getLogger('chat-activity')
if _HAS_SOUND:
Gst.init([])
# pylint: disable-msg=W0223
class Chat(activity.Activity):
def __init__(self, handle):
smilies.init()
pservice = presenceservice.get_instance()
self.owner = pservice.get_owner()
self._ebook_mode_detector = EbookModeDetector()
self.chatbox = ChatBox(
self.owner, self._ebook_mode_detector.get_ebook_mode())
self.chatbox.connect('open-on-journal', self.__open_on_journal)
super(Chat, self).__init__(handle)
self._entry = None
self._has_alert = False
self._has_osk = False
self._setup_canvas()
GObject.idle_add(self._create_smiley_window)
self._entry.grab_focus()
toolbar_box = ToolbarBox()
self.set_toolbar_box(toolbar_box)
self._activity_toolbar_button = ActivityToolbarButton(self)
self._activity_toolbar_button.connect('clicked', self._fixed_resize_cb)
toolbar_box.toolbar.insert(self._activity_toolbar_button, 0)
self._activity_toolbar_button.show()
separator = Gtk.SeparatorToolItem()
separator.props.draw = False
separator.set_expand(True)
toolbar_box.toolbar.insert(separator, -1)
toolbar_box.toolbar.insert(StopButton(self), -1)
toolbar_box.show_all()
# Chat is room or one to one:
self._chat_is_room = False
self.text_channel = None
if _HAS_SOUND:
self.element = Gst.ElementFactory.make('playbin', 'Player')
if self.shared_activity:
# we are joining the activity
self._entry.props.placeholder_text = \
_('Please wait for a connection before starting to chat.')
self.connect('joined', self._joined_cb)
if self.get_shared():
# we have already joined
self._joined_cb(self)
elif handle.uri:
# XMPP non-sugar3 incoming chat, not sharable
self._activity_toolbar_button.props.page.share.props.visible = \
False
self._one_to_one_connection(handle.uri)
else:
# we are creating the activity
if not self.metadata or self.metadata.get(
'share-scope', activity.SCOPE_PRIVATE) == \
activity.SCOPE_PRIVATE:
# if we are in private session
self._alert(_('Off-line'), _('Share, or invite someone.'))
else:
self._entry.props.placeholder_text = \
_('Please wait for a connection before starting to chat.')
self.connect('shared', self._shared_cb)
def _fixed_resize_cb(self, widget=None, rect=None):
''' If a toolbar opens or closes, we need to resize the vbox
holding out scrolling window. '''
if self._has_alert:
dy = style.GRID_CELL_SIZE
else:
dy = 0
if self._has_osk:
if Gdk.Screen.width() > Gdk.Screen.height():
dy += OSK_HEIGHT[0]
else:
dy += OSK_HEIGHT[1]
if self._toolbar_expanded():
self.chatbox.set_size_request(
self._chat_width,
self._chat_height - style.GRID_CELL_SIZE - dy)
self._fixed.move(self._entry_grid, style.GRID_CELL_SIZE,
self._chat_height - style.GRID_CELL_SIZE - dy)
else:
self.chatbox.set_size_request(self._chat_width,
self._chat_height - dy)
self._fixed.move(self._entry_grid, style.GRID_CELL_SIZE,
self._chat_height - dy)
self.chatbox.resize_conversation(dy)
def _setup_canvas(self):
''' Create a canvas '''
self._fixed = Gtk.Fixed()
self._fixed.set_size_request(
Gdk.Screen.width(), Gdk.Screen.height() - style.GRID_CELL_SIZE)
self._fixed.connect('size-allocate', self._fixed_resize_cb)
self.set_canvas(self._fixed)
self._fixed.show()
self._entry_widgets = self._make_entry_widgets()
self._fixed.put(self.chatbox, 0, 0)
self.chatbox.show()
self._fixed.put(self._entry_grid, style.GRID_CELL_SIZE,
self._chat_height)
self._entry_grid.show()
Gdk.Screen.get_default().connect('size-changed', self._configure_cb)
def _configure_cb(self, event):
self._fixed.set_size_request(
Gdk.Screen.width(), Gdk.Screen.height() - style.GRID_CELL_SIZE)
if self._ebook_mode_detector.get_ebook_mode():
self._entry_height = int(style.GRID_CELL_SIZE * 1.5)
else:
self._entry_height = style.GRID_CELL_SIZE
entry_width = Gdk.Screen.width() - \
2 * (self._entry_height + style.GRID_CELL_SIZE)
self._entry.set_size_request(entry_width, self._entry_height)
self._entry_grid.set_size_request(
Gdk.Screen.width() - 2 * style.GRID_CELL_SIZE,
self._entry_height)
self._chat_height = Gdk.Screen.height() - self._entry_height - \
style.GRID_CELL_SIZE
self._chat_width = Gdk.Screen.width()
self.chatbox.set_size_request(self._chat_width, self._chat_height)
self.chatbox.resize_all()
width = int(Gdk.Screen.width() - 2 * style.GRID_CELL_SIZE)
if self._ebook_mode_detector.get_ebook_mode():
height = int(Gdk.Screen.height() - 8 * style.GRID_CELL_SIZE)
else:
height = int(Gdk.Screen.height() - 5 * style.GRID_CELL_SIZE)
self._smiley_table.set_size_request(width, height)
self._smiley_toolbar.set_size_request(width, -1)
self._smiley_window.set_size_request(width, -1)
self._fixed_resize_cb()
def _create_smiley_table(self, width):
button_size = style.STANDARD_ICON_SIZE + style.DEFAULT_PADDING
width = width - 2 * style.DEFAULT_SPACING
smilies_columns = int(width / button_size) - 2
table = Gtk.Grid()
table.set_row_spacing(0)
table.set_column_spacing(0)
table.set_border_width(style.DEFAULT_SPACING)
x = 0
y = 0
for i in range(len(smilies.THEME)):
path, hint, codes = smilies.THEME[i]
image = Gtk.Image()
image.set_from_file(path)
button = Gtk.ToolButton()
button.set_icon_widget(image)
button.connect('clicked', self._add_smiley_to_entry, codes[0])
table.attach(button, x, y, 1, 1)
button.show()
x += 1
if x == smilies_columns:
y += 1
x = 0
return table
def _add_smiley_to_entry(self, button, text):
pos = self._entry.props.cursor_position
self._entry.insert_text(text, pos)
self._entry.grab_focus()
self._entry.set_position(pos + len(text))
self._hide_smiley_window()
def _shared_cb(self, sender):
self._setup()
def _one_to_one_connection(self, tp_channel):
'''Handle a private invite from a non-sugar3 XMPP client.'''
if self.shared_activity or self.text_channel:
return
bus_name, connection, channel = json.loads(tp_channel)
logger.debug('GOT XMPP: %s %s %s', bus_name, connection, channel)
Connection(bus_name, connection, ready_handler=lambda conn:
self._one_to_one_connection_ready_cb(
bus_name, channel, conn))
def _one_to_one_connection_ready_cb(self, bus_name, channel, conn):
'''Callback for Connection for one to one connection'''
text_channel = Channel(bus_name, channel)
self.text_channel = TextChannelWrapper(text_channel, conn)
self.text_channel.set_received_callback(self._received_cb)
self.text_channel.handle_pending_messages()
self.text_channel.set_closed_callback(
self._one_to_one_connection_closed_cb)
self._chat_is_room = False
self._alert(_('On-line'), _('Private Chat'))
# XXX How do we detect the sender going offline?
self._entry.set_sensitive(True)
self._entry.props.placeholder_text = None
self._entry.grab_focus()
def _one_to_one_connection_closed_cb(self):
'''Callback for when the text channel closes.'''
self._alert(_('Off-line'), _('left the chat'))
def _setup(self):
self.text_channel = TextChannelWrapper(
self.shared_activity.telepathy_text_chan,
self.shared_activity.telepathy_conn)
self.text_channel.set_received_callback(self._received_cb)
self._alert(_('On-line'), _('Connected'))
self.shared_activity.connect('buddy-joined', self._buddy_joined_cb)
self.shared_activity.connect('buddy-left', self._buddy_left_cb)
self._chat_is_room = True
self._entry.set_sensitive(True)
self._entry.props.placeholder_text = None
self._entry.grab_focus()
def _joined_cb(self, sender):
'''Joined a shared activity.'''
if not self.shared_activity:
return
logger.debug('Joined a shared chat')
for buddy in self.shared_activity.get_joined_buddies():
self._buddy_already_exists(buddy)
self._setup()
def _received_cb(self, buddy, text):
'''Show message that was received.'''
if buddy:
if type(buddy) is dict:
nick = buddy['nick']
else:
nick = buddy.props.nick
else:
nick = '???'
logger.debug('Received message from %s: %s', nick, text)
self.chatbox.add_text(buddy, text)
if self.owner.props.nick in text:
self.play_sound('said_nick')
'''
vscroll = self.chatbox.get_vadjustment()
if vscroll.get_property('value') != vscroll.get_property('upper'):
self._alert(_('New message'), _('New message from %s' % nick))
'''
if not self.has_focus:
self.notify_user(_('Message from %s') % buddy, text)
def _toolbar_expanded(self):
if self._activity_toolbar_button.is_expanded():
return True
return False
def _alert(self, title, text=None):
alert = NotifyAlert(timeout=5)
alert.props.title = title
alert.props.msg = text
self.add_alert(alert)
alert.connect('response', self._alert_cancel_cb)
alert.show()
self._has_alert = True
self._fixed_resize_cb()
def _alert_cancel_cb(self, alert, response_id):
self.remove_alert(alert)
self._has_alert = False
self._fixed_resize_cb()
def __open_on_journal(self, widget, url):
'''Ask the journal to display a URL'''
logger.debug('Create journal entry for URL: %s', url)
jobject = datastore.create()
metadata = {
'title': '%s: %s' % (_('URL from Chat'), url),
'title_set_by_user': '1',
'icon-color': profile.get_color().to_string(),
'mime_type': 'text/uri-list',
}
for k, v in metadata.items():
jobject.metadata[k] = v
file_path = os.path.join(get_activity_root(), 'instance',
'%i_' % time.time())
open(file_path, 'w').write(url + '\r\n')
os.chmod(file_path, 0755)
jobject.set_file_path(file_path)
datastore.write(jobject)
show_object_in_journal(jobject.object_id)
jobject.destroy()
os.unlink(file_path)
def _buddy_joined_cb(self, sender, buddy):
'''Show a buddy who joined'''
if buddy == self.owner:
return
self.chatbox.add_text(
buddy, _('%s joined the chat') % buddy.props.nick,
status_message=True)
self.play_sound('login')
def _buddy_left_cb(self, sender, buddy):
'''Show a buddy who joined'''
if buddy == self.owner:
return
self.chatbox.add_text(
buddy, _('%s left the chat') % buddy.props.nick,
status_message=True)
self.play_sound('logout')
def _buddy_already_exists(self, buddy):
'''Show a buddy already in the chat.'''
if buddy == self.owner:
return
self.chatbox.add_text(
buddy, _('%s is here') % buddy.props.nick,
status_message=True)
def can_close(self):
'''Perform cleanup before closing.
Close text channel of a one to one XMPP chat.
'''
if self._chat_is_room is False:
if self.text_channel is not None:
self.text_channel.close()
return True
def _make_entry_widgets(self):
'''We need to create a button for the smiley, a text entry, and a
send button.
All of this, along with the chatbox, goes into a grid.
---------------------------------------
| chat box |
| smiley button | entry | send button |
---------------------------------------
'''
if self._ebook_mode_detector.get_ebook_mode():
self._entry_height = int(style.GRID_CELL_SIZE * 1.5)
else:
self._entry_height = style.GRID_CELL_SIZE
entry_width = Gdk.Screen.width() - \
2 * (self._entry_height + style.GRID_CELL_SIZE)
self._chat_height = Gdk.Screen.height() - self._entry_height - \
style.GRID_CELL_SIZE
self._chat_width = Gdk.Screen.width()
self.chatbox.set_size_request(self._chat_width, self._chat_height)
self._entry_grid = Gtk.Grid()
self._entry_grid.set_size_request(
Gdk.Screen.width() - 2 * style.GRID_CELL_SIZE,
self._entry_height)
smiley_button = EventIcon(icon_name='smilies',
pixel_size=self._entry_height)
smiley_button.connect('button-press-event', self._smiley_button_cb)
self._entry_grid.attach(smiley_button, 0, 0, 1, 1)
smiley_button.show()
self._entry = Gtk.Entry()
self._entry.set_size_request(entry_width, self._entry_height)
self._entry.modify_bg(Gtk.StateType.INSENSITIVE,
style.COLOR_WHITE.get_gdk_color())
self._entry.modify_base(Gtk.StateType.INSENSITIVE,
style.COLOR_WHITE.get_gdk_color())
self._entry.set_sensitive(False)
self._entry.props.placeholder_text = \
_('You must be connected to a friend before starting to chat.')
self._entry.connect('focus-in-event', self._entry_focus_in_cb)
self._entry.connect('focus-out-event', self._entry_focus_out_cb)
self._entry.connect('activate', self._entry_activate_cb)
self._entry.connect('key-press-event', self._entry_key_press_cb)
self._entry_grid.attach(self._entry, 1, 0, 1, 1)
self._entry.show()
send_button = EventIcon(icon_name='send',
pixel_size=self._entry_height)
send_button.connect('button-press-event', self._send_button_cb)
self._entry_grid.attach(send_button, 2, 0, 1, 1)
send_button.show()
def _get_icon_pixbuf(self, name):
icon_theme = Gtk.IconTheme.get_default()
icon_info = icon_theme.lookup_icon(
name, style.LARGE_ICON_SIZE, 0)
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
icon_info.get_filename(), style.LARGE_ICON_SIZE,
style.LARGE_ICON_SIZE)
del icon_info
return pixbuf
def _entry_focus_in_cb(self, entry, event):
self._hide_smiley_window()
if self._ebook_mode_detector.get_ebook_mode():
self._has_osk = True
self._fixed_resize_cb()
def _entry_focus_out_cb(self, entry, event):
if self._ebook_mode_detector.get_ebook_mode():
self._has_osk = False
self._fixed_resize_cb()
def _entry_key_press_cb(self, widget, event):
'''Check for scrolling keys.
Check if the user pressed Page Up, Page Down, Home or End and
scroll the window according the pressed key.
'''
vadj = self.chatbox.get_vadjustment()
if event.keyval == Gdk.KEY_Page_Down:
value = vadj.get_value() + vadj.page_size
if value > vadj.upper - vadj.page_size:
value = vadj.upper - vadj.page_size
vadj.set_value(value)
elif event.keyval == Gdk.KEY_Page_Up:
vadj.set_value(vadj.get_value() - vadj.page_size)
elif event.keyval == Gdk.KEY_Home and \
event.get_state() & Gdk.ModifierType.CONTROL_MASK:
vadj.set_value(vadj.lower)
elif event.keyval == Gdk.KEY_End and \
event.get_state() & Gdk.ModifierType.CONTROL_MASK:
vadj.set_value(vadj.upper - vadj.page_size)
def _smiley_button_cb(self, widget, event):
self._show_smiley_window()
def _send_button_cb(self, widget, event):
self._entry_activate_cb(self._entry)
def _entry_activate_cb(self, entry):
self.chatbox._scroll_auto = True
text = entry.props.text
if text:
logger.debug('Adding text to chatbox: %s: %s' % (self.owner, text))
self.chatbox.add_text(self.owner, text)
entry.props.text = ''
if self.text_channel:
logger.debug('sending to text_channel: %s' % (text))
self.text_channel.send(text)
else:
logger.debug('Tried to send message but text channel '
'not connected.')
def write_file(self, file_path):
'''Store chat log in Journal.
Handling the Journal is provided by Activity - we only need
to define this method.
'''
logger.debug('write_file: writing %s' % file_path)
self.chatbox.add_log_timestamp()
f = open(file_path, 'w')
try:
f.write(self.chatbox.get_log())
finally:
f.close()
self.metadata['mime_type'] = 'text/plain'
def read_file(self, file_path):
'''Load a chat log from the Journal.
Handling the Journal is provided by Activity - we only need
to define this method.
'''
logger.debug('read_file: reading %s' % file_path)
log = open(file_path).readlines()
last_line_was_timestamp = False
for line in log:
if line.endswith('\t\t\n'):
if last_line_was_timestamp is False:
timestamp = line.strip().split('\t')[0]
self.chatbox.add_separator(timestamp)
last_line_was_timestamp = True
else:
timestamp, nick, color, status, text = line.strip().split('\t')
status_message = bool(int(status))
self.chatbox.add_text({'nick': nick, 'color': color},
text, status_message)
last_line_was_timestamp = False
def play_sound(self, event):
if _HAS_SOUND:
SOUNDS_PATH = os.path.join(get_bundle_path(), 'sounds')
SOUNDS = {'said_nick': os.path.join(SOUNDS_PATH, 'alert.wav'),
'login': os.path.join(SOUNDS_PATH, 'login.wav'),
'logout': os.path.join(SOUNDS_PATH, 'logout.wav')}
self.element.set_state(Gst.State.NULL)
self.element.set_property('uri', 'file://%s' % SOUNDS[event])
self.element.set_state(Gst.State.PLAYING)
def _create_smiley_window(self):
grid = Gtk.Grid()
width = int(Gdk.Screen.width() - 2 * style.GRID_CELL_SIZE)
self._smiley_toolbar = SmileyToolbar(self)
height = style.GRID_CELL_SIZE
self._smiley_toolbar.set_size_request(width, height)
grid.attach(self._smiley_toolbar, 0, 0, 1, 1)
self._smiley_toolbar.show()
self._smiley_table = Gtk.ScrolledWindow()
self._smiley_table.set_policy(Gtk.PolicyType.AUTOMATIC,
Gtk.PolicyType.AUTOMATIC)
self._smiley_table.modify_bg(
Gtk.StateType.NORMAL, style.COLOR_BLACK.get_gdk_color())
if self._ebook_mode_detector.get_ebook_mode():
height = int(Gdk.Screen.height() - 8 * style.GRID_CELL_SIZE)
else:
height = int(Gdk.Screen.height() - 5 * style.GRID_CELL_SIZE)
self._smiley_table.set_size_request(width, height)
table = self._create_smiley_table(width)
self._smiley_table.add_with_viewport(table)
table.show_all()
grid.attach(self._smiley_table, 0, 1, 1, 1)
self._smiley_table.show()
self._smiley_window = Gtk.ScrolledWindow()
self._smiley_window.set_policy(Gtk.PolicyType.NEVER,
Gtk.PolicyType.NEVER)
self._smiley_window.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
self._smiley_window.set_size_request(width, -1)
self._smiley_window.add_with_viewport(grid)
grid.show()
self._fixed.put(self._smiley_window, style.GRID_CELL_SIZE, 0)
def _show_smiley_window(self):
self._smiley_window.show()
def _hide_smiley_window(self):
self._smiley_window.hide()
class TextChannelWrapper(object):
'''Wrap a telepathy Text Channfel to make usage simpler.'''
def __init__(self, text_chan, conn):
'''Connect to the text channel'''
self._activity_cb = None
self._activity_close_cb = None
self._text_chan = text_chan
self._conn = conn
self._logger = logging.getLogger(
'chat-activity.TextChannelWrapper')
self._signal_matches = []
m = self._text_chan[CHANNEL_INTERFACE].connect_to_signal(
'Closed', self._closed_cb)
self._signal_matches.append(m)
def send(self, text):
'''Send text over the Telepathy text channel.'''
# XXX Implement CHANNEL_TEXT_MESSAGE_TYPE_ACTION
logging.debug('sending %s' % text)
text = text.replace('/', SLASH)
if self._text_chan is not None:
self._text_chan[CHANNEL_TYPE_TEXT].Send(
CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, text)
def close(self):
'''Close the text channel.'''
self._logger.debug('Closing text channel')
try:
self._text_chan[CHANNEL_INTERFACE].Close()
except Exception:
self._logger.debug('Channel disappeared!')
self._closed_cb()
def _closed_cb(self):
'''Clean up text channel.'''
self._logger.debug('Text channel closed.')
for match in self._signal_matches:
match.remove()
self._signal_matches = []
self._text_chan = None
if self._activity_close_cb is not None:
self._activity_close_cb()
def set_received_callback(self, callback):
'''Connect the function callback to the signal.
callback -- callback function taking buddy and text args
'''
if self._text_chan is None:
return
self._activity_cb = callback
m = self._text_chan[CHANNEL_TYPE_TEXT].connect_to_signal(
'Received', self._received_cb)
self._signal_matches.append(m)
def handle_pending_messages(self):
'''Get pending messages and show them as received.'''
for identity, timestamp, sender, type_, flags, text in \
self._text_chan[
CHANNEL_TYPE_TEXT].ListPendingMessages(False):
self._received_cb(identity, timestamp, sender, type_, flags, text)
def _received_cb(self, identity, timestamp, sender, type_, flags, text):
'''Handle received text from the text channel.
Converts sender to a Buddy.
Calls self._activity_cb which is a callback to the activity.
'''
logging.debug('received_cb %r %s' % (type_, text))
if type_ != 0:
# Exclude any auxiliary messages
return
text = text.replace(SLASH, '/')
if self._activity_cb:
try:
self._text_chan[CHANNEL_INTERFACE_GROUP]
except Exception:
# One to one XMPP chat
nick = self._conn[
CONN_INTERFACE_ALIASING].RequestAliases([sender])[0]
buddy = {'nick': nick, 'color': '#000000,#808080'}
else:
# Normal sugar3 MUC chat
# XXX: cache these
buddy = self._get_buddy(sender)
self._activity_cb(buddy, text)
self._text_chan[
CHANNEL_TYPE_TEXT].AcknowledgePendingMessages([identity])
else:
self._logger.debug('Throwing received message on the floor'
' since there is no callback connected. See'
' set_received_callback')
def set_closed_callback(self, callback):
'''Connect a callback for when the text channel is closed.
callback -- callback function taking no args
'''
self._activity_close_cb = callback
def _get_buddy(self, cs_handle):
'''Get a Buddy from a (possibly channel-specific) handle.'''
# XXX This will be made redundant once Presence Service
# provides buddy resolution
# Get the Presence Service
pservice = presenceservice.get_instance()
# Get the Telepathy Connection
tp_name, tp_path = pservice.get_preferred_connection()
conn = Connection(tp_name, tp_path)
group = self._text_chan[CHANNEL_INTERFACE_GROUP]
my_csh = group.GetSelfHandle()
if my_csh == cs_handle:
handle = conn.GetSelfHandle()
elif group.GetGroupFlags() & \
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES:
handle = group.GetHandleOwners([cs_handle])[0]
else:
handle = cs_handle
# XXX: deal with failure to get the handle owner
assert handle != 0
return pservice.get_buddy_by_telepathy_handle(
tp_name, tp_path, handle)
class SmileyToolbar(Gtk.Toolbar):
def __init__(self, activity):
Gtk.Toolbar.__init__(self)
self._activity = activity
self._add_separator()
self._icon = Icon(icon_name='smilies-white')
self._add_widget(self._icon)
self._add_separator()
self._title = Gtk.Label(_('Insert a smiley'))
self._add_widget(self._title)
self._add_separator(True)
self.cancel_button = ToolButton('dialog-cancel')
self.cancel_button.set_tooltip(_('Cancel'))
self.cancel_button.connect('clicked', self.__cancel_button_clicked_cb)
self.insert(self.cancel_button, -1)
self.cancel_button.show()
def _add_separator(self, expand=False):
separator = Gtk.SeparatorToolItem()
separator.props.draw = False
if expand:
separator.set_expand(True)
else:
separator.set_size_request(style.DEFAULT_SPACING, -1)
self.insert(separator, -1)
separator.show()
def _add_widget(self, widget, expand=False):
tool_item = Gtk.ToolItem()
tool_item.set_expand(expand)
tool_item.add(widget)
widget.show()
self.insert(tool_item, -1)
tool_item.show()
def __cancel_button_clicked_cb(self, widget, data=None):
self._activity._hide_smiley_window()
|