Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/canvas.py
blob: 5a9cf541d6935969bd5cef371f36d57d81417003 (plain)
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
# Copyright 2012 S. Daniel Francis <francis@sugarlabs.org>
# Copyright 2013 Agustin Zubiaga <aguz@sugarlabs.org>
#
# 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