# Copyright 2012 S. Daniel Francis # Copyright 2013 Agustin Zubiaga # # 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 3 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 Street, Fifth Floor, Boston, # MA 02110-1301, USA. import logging logger = logging.getLogger('canvas') from gi.repository import Gtk from gettext import gettext as _ from chat import ChatClient class Canvas(Gtk.Box): def __init__(self, toolbar_box, activity): Gtk.Box.__init__(self) self.activity = activity self._client = ChatClient(*open('../pass.txt').read().split(',')) self._client.connect('message-received', self._message_received) self._rooms = {} self._buddy_list = BuddyList() self._client.connect('presence-changed', self._buddy_list.presence_change()) self._rooms_notebook = Gtk.Notebook() paned = Gtk.HPaned() paned.add1(self._buddy_list) paned.add2(self._rooms_notebook) self.pack_start(paned, True, True, 0) self.show_all() def _message_received(self, client, user, message): if user in self._rooms.keys(): name = self._buddy_list.get_user_name(user) new_room = Room(self._client, user, name, msg=message) label = Gtk.Label(name) self._rooms_notebook.append_page(label, new_room) def new(self): pass class Room(Gtk.Box): def __init__(self, client, user, name, msg=''): Gtk.Box.__init__(self) self.set_orientation(Gtk.Orientation.VERTICAL) messages = Gtk.TextView() self._messages_buffer = messages.get_buffer() self.pack_start(messages, True, True, 5) input_box = Gtk.Box() msg_input = Gtk.Entry() send_btn = Gtk.Button(_('Send')) input_box.pack_start(msg_input, True, False, 5) input_box.pack_start(send_btn, False, True, 5) self.pack_start(input_box, False, True, 5) self._add_message(name, msg) self._user = user self._name = name self._client = client self.show_all() def _message_received(self, user, message): self._add_message(self._name, message) def _add_message(self, user, message): self._messages_buffer.props.text += '%s: %s\n' % (user, message) def _send_new_message(self, message): self._add_message(_('Me'), message) self._client.send_message(self._user, message) class BuddyList(Gtk.Box): def __init__(self): Gtk.Box.__init__(self) self.set_orientation(Gtk.Orientation.VERTICAL) scroll = Gtk.ScrolledWindow() scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) model = Gtk.ListStore(str, str) tree_view = Gtk.TreeView(model) column = Gtk.TreeViewColumn() # TODO: Use a pixbuf to show the state status = Gtk.CellRendererText() column.pack_start(status, True) column.add_attribute(status, 'text', 0) tree_view.append_column(column) column = Gtk.TreeViewColumn() buddy = Gtk.CellRendererText() column.pack_start(buddy, True) column.add_attribute(buddy, 'text', 1) tree_view.append_column(column) scroll.add(tree_view) self.pack_start(scroll, True, True, 0) self._list = {} self.show_all() def presence_change(self, client, user, status): self._list[user] = status self._model.clear() for b in self._list.keys(): self.model.append([self._list(b), b]) def get_user(self, user_address): # TODO: Implement this return user_address