summaryrefslogtreecommitdiff
path: root/src/tab.py
blob: dadd2f7f476a33b17b6d8dae2a97c3bd7fb831ee (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
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
# Copyright 2010 Le Coz Florent <louizatakk@fedoraproject.org>
#
# This file is part of Poezio.
#
# Poezio 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, version 3 of the License.
#
# Poezio 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 Poezio.  If not, see <http://www.gnu.org/licenses/>.

"""
a Tab object is a way to organize various Window (see window.py)
around the screen at once.
A tab is then composed of multiple Window.
Each Tab object has different refresh() and resize() methods, defining of its
Window are displayed, etc
"""

MIN_WIDTH = 50
MIN_HEIGHT = 16

import window
import theme
from roster import RosterGroup

from common import debug

class Tab(object):
    """
    """
    number = 0

    def __init__(self, stdscr):
        self.nb = Tab.number
        Tab.number += 1
        self.size = (self.height, self.width) = stdscr.getmaxyx()
        if self.height < MIN_HEIGHT or self.width < MIN_WIDTH:
            self.visible = False
        else:
            self.visible = True

    def refresh(self, tabs, informations, roster):
        """
        Called on each screen refresh (when something has changed)
        """
        raise NotImplementedError

    def resize(self, stdscr):
        self.size = (self.height, self.width) = stdscr.getmaxyx()
        if self.height < MIN_HEIGHT or self.width < MIN_WIDTH:
            self.visible = False
        else:
            self.visible = True

    def get_color_state(self):
        """
        returns the color that should be used in the GlobalInfoBar
        """
        raise NotImplementedError

    def set_color_state(self, color):
        """
        set the color state
        """
        raise NotImplementedError

    def get_name(self):
        """
        get the name of the tab
        """
        raise NotImplementedError

    def on_input(self, key):
        raise NotImplementedError

    def on_lose_focus(self):
        """
        called when this tab loses the focus.
        """
        raise NotImplementedError

    def on_gain_focus(self):
        """
        called when this tab gains the focus.
        """
        raise NotImplementedError

    def add_message(self):
        """
        Adds a message in the tab.
        If the tab cannot add a message in itself (for example
        FormTab, where text is not intented to be appened), it returns False.
        If the tab can, it returns True
        """
        raise NotImplementedError

    def on_scroll_down(self):
        """
        Defines what happens when we scrol down
        """
        raise NotImplementedError

    def on_scroll_up(self):
        """
        Defines what happens when we scrol down
        """
        raise NotImplementedError

    def on_info_win_size_changed(self, size, stdscr):
        """
        Called when the window with the informations is resized
        """
        raise NotImplementedError

class InfoTab(Tab):
    """
    The information tab, used to display global informations
    when using a anonymous account
    """
    def __init__(self, stdscr, name):
        Tab.__init__(self, stdscr)
        self.tab_win = window.GlobalInfoBar(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.text_win = window.TextWin(self.height-2, self.width, 0, 0, stdscr, self.visible)
        self.input = window.Input(1, self.width, self.height-1, 0, stdscr, self.visible)
        self.name = name
        self.color_state = theme.COLOR_TAB_NORMAL

    def resize(self, stdscr):
        Tab.resize(self, stdscr)
        self.tab_win.resize(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.text_win.resize(self.height-2, self.width, 0, 0, stdscr, self.visible)
        self.input.resize(1, self.width, self.height-1, 0, stdscr, self.visible)

    def refresh(self, tabs, informations, _):
        self.text_win.refresh(informations)
        self.tab_win.refresh(tabs, tabs[0])
        self.input.refresh()

    def get_name(self):
        return self.name

    def get_color_state(self):
        return self.color_state

    def set_color_state(self, color):
        return

    def on_input(self, key):
        return self.input.do_command(key)

    def on_lose_focus(self):
        self.color_state = theme.COLOR_TAB_NORMAL

    def on_gain_focus(self):
        self.color_state = theme.COLOR_TAB_CURRENT

    def on_scroll_up(self):
        pass

    def on_scroll_down(self):
        pass

    def on_info_win_size_changed(self, size, stdscr):
        return

class MucTab(Tab):
    """
    The tab containing a multi-user-chat room.
    It contains an userlist, an input, a topic, an information and a chat zone
    """
    def __init__(self, stdscr, room, info_win_size):
        """
        room is a Room object
        The stdscr is passed to know the size of the
        terminal
        """
        Tab.__init__(self, stdscr)
        self._room = room
        self.info_win_size = info_win_size
        self.topic_win = window.Topic(1, self.width, 0, 0, stdscr, self.visible)
        self.text_win = window.TextWin(self.height-4-info_win_size, (self.width//10)*9, 1, 0, stdscr, self.visible)
        self.v_separator = window.VerticalSeparator(self.height-3, 1, 1, 9*(self.width//10), stdscr, self.visible)
        self.user_win = window.UserList(self.height-3, (self.width//10), 1, 9*(self.width//10)+1, stdscr, self.visible)
        self.info_header = window.MucInfoWin(1, (self.width//10)*9, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win = window.TextWin(info_win_size, (self.width//10)*9, self.height-2-self.info_win_size, 0, stdscr, self.visible)
        self.tab_win = window.GlobalInfoBar(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.input = window.Input(1, self.width, self.height-1, 0, stdscr, self.visible)

    def resize(self, stdscr):
        """
        Resize the whole window. i.e. all its sub-windows
        """
        Tab.resize(self, stdscr)
        text_width = (self.width//10)*9
        self.topic_win.resize(1, self.width, 0, 0, stdscr, self.visible)
        self.text_win.resize(self.height-4-self.info_win_size, text_width, 1, 0, stdscr, self.visible)
        self.v_separator.resize(self.height-3, 1, 1, 9*(self.width//10), stdscr, self.visible)
        self.user_win.resize(self.height-3, self.width-text_width-1, 1, text_width+1, stdscr, self.visible)
        self.info_header.resize(1, (self.width//10)*9, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win.resize(self.info_win_size, (self.width//10)*9, self.height-2-self.info_win_size, 0, stdscr, self.visible)
        self.tab_win.resize(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.input.resize(1, self.width, self.height-1, 0, stdscr, self.visible)

    def refresh(self, tabs, informations, _):
        self.topic_win.refresh(self._room.topic)
        self.text_win.refresh(self._room)
        self.v_separator.refresh()
        self.user_win.refresh(self._room.users)
        self.info_header.refresh(self._room)
        self.info_win.refresh(informations)
        self.tab_win.refresh(tabs, tabs[0])
        self.input.refresh()

    def on_input(self, key):
        self.key_func = {
            "\t": self.completion,
            "^I": self.completion,
            "KEY_BTAB": self.last_words_completion,
            }
        if key in self.key_func:
            return self.key_func[key]()
        return self.input.do_command(key)

    def completion(self):
        """
        Called when Tab is pressed, complete the nickname in the input
        """
        compare_users = lambda x: x.last_talked
        self.input.auto_completion([user.nick for user in sorted(self._room.users, key=compare_users, reverse=True)])

    def last_words_completion(self):
        """
        Complete the input with words recently said
        """
        # build the list of the recent words
        char_we_dont_want = [',', '(', ')', '.']
        words = list()
        for msg in self._room.messages[:-40:-1]:
            if not msg:
                continue
            for char in char_we_dont_want:
                msg.txt.replace(char, ' ')
            for word in msg.txt.split():
                if len(word) > 5:
                    words.append(word)
        self.input.auto_completion(words, False)

    def get_color_state(self):
        """
        """
        return self._room.color_state

    def set_color_state(self, color):
        """
        """
        self._room.set_color_state(color)

    def get_name(self):
        """
        """
        return self._room.name

    def get_room(self):
        return self._room

    def on_lose_focus(self):
        self._room.set_color_state(theme.COLOR_TAB_NORMAL)
        self._room.remove_line_separator()
        self._room.add_line_separator()

    def on_gain_focus(self):
        self._room.set_color_state(theme.COLOR_TAB_CURRENT)

    def on_scroll_up(self):
        self._room.scroll_up(self.text_win.height-1)

    def on_scroll_down(self):
        self._room.scroll_down(self.text_win.height-1)

    def on_info_win_size_changed(self, size, stdscr):
        self.info_win_size = size
        text_width = (self.width//10)*9
        self.text_win.resize(self.height-4-self.info_win_size, text_width, 1, 0, stdscr, self.visible)
        self.info_header.resize(1, (self.width//10)*9, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win.resize(self.info_win_size, (self.width//10)*9, self.height-2-self.info_win_size, 0, stdscr, self.visible)

class PrivateTab(Tab):
    """
    The tab containg a private conversation (someone from a MUC)
    """
    def __init__(self, stdscr, room, info_win_size):
        Tab.__init__(self, stdscr)
        self.info_win_size = info_win_size
        self._room = room
        self.text_win = window.TextWin(self.height-3-self.info_win_size, self.width, 0, 0, stdscr, self.visible)
        self.info_header = window.PrivateInfoWin(1, self.width, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win = window.TextWin(self.info_win_size, self.width, self.height-2-self.info_win_size, 0, stdscr, self.visible)
        self.tab_win = window.GlobalInfoBar(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.input = window.Input(1, self.width, self.height-1, 0, stdscr, self.visible)

    def resize(self, stdscr):
        Tab.resize(self, stdscr)
        self.text_win.resize(self.height-3-self.info_win_size, self.width, 0, 0, stdscr, self.visible)
        self.info_header.resize(1, self.width, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win.resize(self.info_win_size, self.width, self.height-2-self.info_win_size, 0, stdscr, self.visible)
        self.tab_win.resize(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.input.resize(1, self.width, self.height-1, 0, stdscr, self.visible)

    def refresh(self, tabs, informations, _):
        self.text_win.refresh(self._room)
        self.info_header.refresh(self._room)
        self.info_win.refresh(informations)
        self.tab_win.refresh(tabs, tabs[0])
        self.input.refresh()

    def get_color_state(self):
        if self._room.color_state == theme.COLOR_TAB_NORMAL or\
                self._room.color_state == theme.COLOR_TAB_CURRENT:
            return self._room.color_state
        return theme.COLOR_TAB_PRIVATE

    def set_color_state(self, color):
        self._room.color_state = color

    def get_name(self):
        return self._room.name

    def on_input(self, key):
        return self.input.do_command(key)

    def on_lose_focus(self):
        self._room.set_color_state(theme.COLOR_TAB_NORMAL)
        self._room.remove_line_separator()
        self._room.add_line_separator()

    def on_gain_focus(self):
        self._room.set_color_state(theme.COLOR_TAB_CURRENT)

    def on_scroll_up(self):
        self._room.scroll_up(self.text_win.height-1)

    def on_scroll_down(self):
        self._room.scroll_down(self.text_win.height-1)

    def on_info_win_size_changed(self, size, stdscr):
        self.info_win_size = size
        self.text_win.resize(self.height-3-self.info_win_size, self.width, 0, 0, stdscr, self.visible)
        self.info_header.resize(1, self.width, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win.resize(self.info_win_size, self.width, self.height-2-self.info_win_size, 0, stdscr, self.visible)

    def get_room(self):
        return self._room

class RosterInfoTab(Tab):
    """
    A tab, splitted in two, containing the roster and infos
    """
    def __init__(self, stdscr):
        Tab.__init__(self, stdscr)
        self.name = "Roster"
        roster_width = self.width//2
        info_width = self.width-roster_width-1
        self.v_separator = window.VerticalSeparator(self.height-2, 1, 0, roster_width, stdscr, self.visible)
        self.tab_win = window.GlobalInfoBar(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.info_win = window.TextWin(self.height-2, info_width, 0, roster_width+1, stdscr, self.visible)
        self.roster_win = window.RosterWin(self.height-2, roster_width, 0, 0, stdscr, self.visible)
        self.input = window.Input(1, self.width, self.height-1, 0, stdscr, self.visible)
        self.set_color_state(theme.COLOR_TAB_NORMAL)

    def resize(self, stdscr):
        Tab.resize(self, stdscr)
        roster_width = self.width//2
        info_width = self.width-roster_width-1
        self.v_separator.resize(self.height-2, 1, 0, roster_width, stdscr, self.visible)
        self.tab_win.resize(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.info_win.resize(self.height-2, info_width, 0, roster_width+1, stdscr, self.visible)
        self.roster_win.resize(self.height-2, roster_width, 0, 0, stdscr, self.visible)
        self.input.resize(1, self.width, self.height-1, 0, stdscr, self.visible)

    def refresh(self, tabs, informations, roster):
        self.roster_win.refresh(roster)
        self.v_separator.refresh()
        self.info_win.refresh(informations)
        self.tab_win.refresh(tabs, tabs[0])
        self.input.refresh()

    def get_name(self):
        return self.name

    def get_color_state(self):
        return self._color_state

    def set_color_state(self, color):
        self._color_state = color

    def on_input(self, key):
        if key in ('\n', '^J', '^M') and self.input.is_empty():
            return self.on_enter()
        return self.input.do_command(key)

    def on_lose_focus(self):
        self._color_state = theme.COLOR_TAB_NORMAL

    def on_gain_focus(self):
        self._color_state = theme.COLOR_TAB_CURRENT

    def add_message(self):
        return False

    def on_scroll_down(self):
        self.roster_win.move_cursor_down()

    def on_scroll_up(self):
        self.roster_win.move_cursor_up()

    def on_info_win_size_changed(self, _, __):
        pass

    def on_enter(self):
        selected_row = self.roster_win.get_selected_row()
        return selected_row

class ConversationTab(Tab):
    """
    The tab containg a normal conversation (someone from our roster)
    """
    def __init__(self, stdscr, room, info_win_size):
        Tab.__init__(self, stdscr)
        self.info_win_size = info_win_size
        self._room = room
        self.text_win = window.TextWin(self.height-3-self.info_win_size, self.width, 0, 0, stdscr, self.visible)
        self.info_header = window.ConversationInfoWin(1, self.width, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win = window.TextWin(self.info_win_size, self.width, self.height-2-self.info_win_size, 0, stdscr, self.visible)
        self.tab_win = window.GlobalInfoBar(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.input = window.Input(1, self.width, self.height-1, 0, stdscr, self.visible)

    def resize(self, stdscr):
        Tab.resize(self, stdscr)
        self.text_win.resize(self.height-3-self.info_win_size, self.width, 0, 0, stdscr, self.visible)
        self.info_header.resize(1, self.width, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win.resize(self.info_win_size, self.width, self.height-2-self.info_win_size, 0, stdscr, self.visible)
        self.tab_win.resize(1, self.width, self.height-2, 0, stdscr, self.visible)
        self.input.resize(1, self.width, self.height-1, 0, stdscr, self.visible)

    def refresh(self, tabs, informations, roster):
        self.text_win.refresh(self._room)
        self.info_header.refresh(self._room, roster.get_contact_by_jid(self._room.name))
        self.info_win.refresh(informations)
        self.tab_win.refresh(tabs, tabs[0])
        self.input.refresh()

    def get_color_state(self):
        if self._room.color_state == theme.COLOR_TAB_NORMAL or\
                self._room.color_state == theme.COLOR_TAB_CURRENT:
            return self._room.color_state
        return theme.COLOR_TAB_PRIVATE

    def set_color_state(self, color):
        self._room.color_state = color

    def get_name(self):
        return self._room.name

    def on_input(self, key):
        return self.input.do_command(key)

    def on_lose_focus(self):
        self._room.set_color_state(theme.COLOR_TAB_NORMAL)
        self._room.remove_line_separator()
        self._room.add_line_separator()

    def on_gain_focus(self):
        self._room.set_color_state(theme.COLOR_TAB_CURRENT)

    def on_scroll_up(self):
        self._room.scroll_up(self.text_win.height-1)

    def on_scroll_down(self):
        self._room.scroll_down(self.text_win.height-1)

    def on_info_win_size_changed(self, size, stdscr):
        self.info_win_size = size
        self.text_win.resize(self.height-3-self.info_win_size, self.width, 0, 0, stdscr, self.visible)
        self.info_header.resize(1, self.width, self.height-3-self.info_win_size, 0, stdscr, self.visible)
        self.info_win.resize(self.info_win_size, self.width, self.height-2-self.info_win_size, 0, stdscr, self.visible)

    def get_room(self):
        return self._room