Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/chat.py
blob: d498fcde48f24790982cfc4d17fde2d7a91d8ca6 (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
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
# Copyright (C) 2009, Aleksey Lim
#
# 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

from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import Pango

import logging
from gettext import gettext as _

import sugar3.graphics.style as style
from sugar3.graphics.roundbox import CanvasRoundBox
from sugar3.graphics.toggletoolbutton import ToggleToolButton

import eye
import glasses
import mouth
import face
import messenger
from chatbox import ChatBox

logger = logging.getLogger('speak')

BUDDY_SIZE = min(100, min(Gdk.Screen.width(),
	Gdk.Screen.height() - style.LARGE_ICON_SIZE) / 5)
BUDDY_PAD = 5

BUDDIES_WIDTH = int(BUDDY_SIZE * 2.5)
BUDDIES_COLOR = style.COLOR_SELECTION_GREY

ENTRY_COLOR = style.COLOR_PANEL_GREY
ENTRY_XPAD = 0
ENTRY_YPAD = 7

class View(Gtk.EventBox):
    def __init__(self):
        GObject.GObject.__init__(self)

        self.messenger = None
        self.me = None
        self.quiet = False
        self._buddies = {}

        # buddies box

        self._buddies_list = Gtk.VBox()
        self._buddies_list.override_background_color(Gtk.StateType.Normal,Gdk.RGBA(*BUDDIES_COLOR.get_rgba()))
        self._buddies_box = Gtk.ScrolledWindow()
        self._buddies_box.set_vexpand(True)
        self._buddies_box.add_with_viewport(self._buddies_list)

        # chat entry
"""
        self._chat = ChatBox()
        self.me, my_face_widget = self._new_face(self._chat.owner, ENTRY_COLOR)

        chat_post = Gtk.TextView()
        chat_post.modify_bg(Gtk.StateType.INSENSITIVE, style.COLOR_WHITE.get_gdk_color())
        chat_post.modify_base(Gtk.StateType.INSENSITIVE, style.COLOR_WHITE.get_gdk_color())
        chat_post.connect('key-press-event', self._key_press_cb)
        chat_post.props.wrap_mode = Gtk.WrapMode.WORD_CHAR
        chat_post.set_size_request(-1, BUDDY_SIZE - ENTRY_YPAD * 2)

        chat_post_box = Gtk.VBox()
        chat_post_box.override_background_color(Gtk.StateType.NORMAL,Gdk.RGBA(*COLOR_WHITE.get_rgba()))

        chat_post_box.pack_start(chat_post, True, True, ENTRY_XPAD)

        chat_entry = Gtk.HBox()
        chat_entry.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(*COLOR_WHITE.get_rgba()))
        chat_entry.add(my_face_widget)
        chat_entry.pack_start(chat_post_box, True, True, ENTRY_YPAD)

        chat_box = Gtk.VBox()
        chat_box.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(*COLOR_WHITE.get_rgba()))
        chat_box.add(self._chat)
        chat_box.add(chat_entry)
        # desk

        self._desk = Gtk.HBox()
        self._desk.add(chat_box)

        self.set_root(self._desk)
"""
def update(self, status):
    self.me.update(status)
    if self.messenger:
        self.messenger.post(None)

def post(self, buddy, status, text):
    i = self._buddies.get(buddy)
    if not i:
        self._add_buddy(buddy)
        i = self._buddies[buddy]

    face = i['face']
    lang_box = i['lang']

    if status:
        face.update(status)
        if lang_box:
                lang_box.props.text = status.voice.friendlyname
    if text:
            self._chat.add_text(buddy, text)
            if not self.quiet:
                # and self.props.window \
                #    and self.props.window.is_visible():
                face.say(text)

def farewell(self, buddy):
    i = self._buddies.get(buddy)
    if not i:
        logger.debug('farewell: cannot find buddy %s' % buddy.props.nick)
        return

    self._buddies_list.remove(i['box'])
    del self._buddies[buddy]

    if len(self._buddies) == 0:
        self._desk.remove(self._buddies_box)

def shut_up(self):
    for i in self._buddies.values():
        i['face'].shut_up();
    self.me.shut_up();

def _add_buddy(self, buddy):
    box = Gtk.HBox()
    box.override_background_color(Gtk.StateType.NORMAL,Gdk.RGBA(*BUDDIES_COLOR.get_rgba()))

    buddy_face, buddy_widget = self._new_face(buddy, BUDDIES_COLOR)

    char_box = Gtk.VBox()

    nick = Gtk.TextView()
    text_buffer = char_box.get_buffer()
    text_buffer.set_text(buddy.props.nick)
    nick.set_justification(Gtk.Justification.LEFT)

    lang = Gtk.TextView()
    text_buffer = msg.get_buffer()
    text_buffer.set_text('')
    lang.set_justification(Gtk.Justification.LEFT)

    char_box.add(nick)
    char_box.add(lang)

    box.add(buddy_widget)
    box.pack_start(char_box, True, True, ENTRY_YPAD)

    self._buddies[buddy] = {
                    'box': box,
                    'face': buddy_face,
                    'lang': lang
                    }
    self._buddies_list.add(box)

    if len(self._buddies) == 1:
        self._desk.add(self._buddies_box)

def _key_press_cb(self, widget, event):
    if event.keyval == Gdk.KEY_Return:
        if not (event.get_state() & Gdk.ModifierType.CONTROL_MASK):
            text = widget.get_buffer().props.text

            if text:
                self._chat.add_text(None, text)
                widget.get_buffer().props.text = ''
                if not self.quiet:
                    self.me.say(text)
                if self.messenger:
                    self.messenger.post(text)

            return True
    return False

def _new_face(self, buddy, color):
    stroke_color, fill_color = buddy.props.color.split(',')
    stroke_color = style.Color(stroke_color)
    fill_color = style.Color(fill_color)

    buddy_face = face.View(fill_color)
    buddy_face.show_all()

    eb = Gtk.EventBox()
    eb.override_background_color(Gtk.StateType.NORMAL, fill_color)

    inner = Gtk.VBox()
    inner.override_background_color(Gtk.StateType.NORMAL,fill_color)

    inner.set_border_width(BUDDY_PAD)
    #Adding border to inner
    eb.add(inner)
    inner.add(buddy_face)

    outer.props.border = BUDDY_PAD

    outer = Gtk.VBox()
    outer.set_border_width(BUDDY_SIZE)
    outer.override_background_color(Gtk.StateType.NORMAL, stroke_color)
    outer.pack_start(inner, True, True, BUDDY_SIZE)

    return (buddy_face, outer)

def look_at(self):
    self.me.look_at()
    for i in self._buddies.values():
            i['face'].look_at()