gui.py 7.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/python
# -*- coding:utf-8 -*-
#
# 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/>.

from handler import Handler
import curses
from curses import textpad

24 25
import sys

26 27
from connection import *

28 29
class Win(object):
    def __init__(self, height, width, y, x, parent_win):
30 31 32
        self._resize(height, width, y, x, parent_win)

    def _resize(self, height, width, y, x, parent_win):
33
        self.height, self.width, self.x, self.y = height, width, x, y
34 35 36 37
        try:
            self.win = parent_win.subwin(height, width, y, x)
        except:
            pass
38 39 40 41 42 43 44 45 46 47 48 49

class UserList(Win):
    def __init__(self, height, width, y, x, parent_win):
        Win.__init__(self, height, width, y, x, parent_win)
        self.list = []

    def add_user(self, name):
        """
        add an user to the list
        """
        self.list.append(name)

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    def refresh(self):
        self.win.clear()
        self.win.attron(curses.color_pair(2))
        self.win.vline(0, 0, curses.ACS_VLINE, self.height)
        self.win.attroff(curses.color_pair(2))
        y = 0
        for name in self.list:
            self.win.addstr(y, 1, name)
            y += 1
        self.win.refresh()

    def resize(self, height, width, y, x, stdscr):
        self._resize(height, width, y, x, stdscr)
        self.refresh()

65 66 67
class Info(Win):
    def __init__(self, height, width, y, x, parent_win):
        Win.__init__(self, height, width, y, x, parent_win)
68
        self.txt = ""
69 70 71
#        self.win.bkgd(ord('p'), curses.COLOR_BLUE)

    def set_info(self, text):
72 73 74 75 76 77 78 79 80 81
        self.txt = text
        self.refresh()

    def resize(self, height, width, y, x, stdscr):
        self._resize(height, width, y, x, stdscr)
        self.refresh()

    def refresh(self):
        try:
            self.win.addstr(0, 0, self.txt + " "*(self.width-len(self.txt)-1)
82
                        , curses.color_pair(1))
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
        except:
            pass

class TextWin(Win):
    def __init__(self, height, width, y, x, parent_win):
        Win.__init__(self, height, width, y, x, parent_win)
        self.lines = []

    def add_line(self, time, nick, text):
        self.lines.append((time, nick, text))
        self.refresh()

    def refresh(self):
        self.win.clear()
        y = 0
        for line in self.lines[-self.height:]:
            self.win.addstr(y, 0, line[0] + " : " + line[1] + ": " + line[2])
            y += 1
        self.win.refresh()

    def resize(self, height, width, y, x, stdscr):
        self._resize(height, width, y, x, stdscr)
        self.refresh()

class Input(Win):
    """
    """
    def __init__(self, height, width, y, x, stdscr):
        Win.__init__(self, height, width, y, x, stdscr)
        self.input = curses.textpad.Textbox(self.win)
        self.input.tripspaces = False

    def resize(self, height, width, y, x, stdscr):
        self._resize(height, width, y, x, stdscr)
        txt = self.input.gather()
        self.input = curses.textpad.Textbox(self.win)
        self.input.tripspaces = False
        self.win.clear()
#        self.win.addstr(txt)

    def do_command(self, key):
        self.input.do_command(key)
        self.win.refresh()

    def gettext(self):
        return self.input.gather()
129

130
class Tab(object):
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    """
    The whole "screen" that can be seen at once in the terminal.
    It contains an userlist, an input zone and a chat zone, all
    related to one single chat room.
    """
    def __init__(self, stdscr, name=None):
        """
        name is the name of the Tab, and it's also
        the JID of the chatroom.
        A particular tab is the "Info" tab which has no
        name (None). This info tab should be unique.
        The stdscr should be passed to know the size of the
        terminal
        """
        self.name = name
        self.size = (self.height, self.width) = stdscr.getmaxyx()
147 148

        self.user_win = UserList(self.height-3, self.width/7, 1, 6*(self.width/7), stdscr)
149 150
        self.topic_win = Info(1, self.width, 0, 0, stdscr)
        self.info_win = Info(1, self.width, self.height-2, 0, stdscr)
151 152 153 154 155 156
        self.text_win = TextWin(self.height-3, (self.width/7)*6, 1, 0, stdscr)
        self.input = Input(1, self.width, self.height-1, 0, stdscr)
#        self.text_win.refresh()

        # debug
        self.topic_win.set_info("Salon machin - Blablablbla, le topic blablabla")
157
        self.info_win.set_info("FION")
158 159 160
        for name in ["pipi", "caca", "louiz", "mRk", "restrict", "jacko"]:
            self.user_win.add_user(name)
        self.refresh()
161

162
    def resize(self, stdscr):
163 164 165
        """
        Resize the whole tabe. i.e. all its sub-windows
        """
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        self.size = (self.height, self.width) = stdscr.getmaxyx()
        self.user_win.resize(self.height-3, self.width/7, 1, 6*(self.width/7), stdscr)
        self.topic_win.resize(1, self.width, 0, 0, stdscr)
        self.info_win.resize(1, self.width, self.height-2, 0, stdscr)
        self.text_win.resize(self.height-3, (self.width/7)*6, 1, 0, stdscr)
        self.input.resize(1, self.width, self.height-1, 0, stdscr)
        self.refresh()

    def refresh(self):
        self.text_win.refresh()
        self.user_win.refresh()
        self.topic_win.refresh()
        self.info_win.refresh()

    def do_command(self, key):
        self.input.do_command(key)
#        self.input_win.refresh()

    def send_message(self):
        self.text_win.add_line("NOW", "louiz'", self.input.gettext())
186

187 188 189 190
class Gui(object):
    """
    Graphical user interface using ncurses
    """
191
    def __init__(self, stdscr):
192 193 194
        self.handler = Handler()

        self.handler.connect('on-muc-message-received', self.on_message)
195
        self.handler.connect('gui-join-room', self.on_join_room)
196 197
        self.handler.connect('on-muc-presence-changed', self.on_presence)

198
        self.init_curses(stdscr)
199

200 201 202 203
    def init_curses(self, stdscr):
#        self.stdscr=       curses.initscr()
        # curses.noecho()
        # curses.cbreak()
204
        stdscr.leaveok(True)
205 206
        curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
        curses.init_pair(2, curses.COLOR_BLUE, 0)
207
        self.current_tab = Tab(stdscr)
208

209 210
    def main_loop(self, stdscr):
        while 1:
211
            stdscr.refresh()
212 213
            key = stdscr.getch()
            if key == curses.KEY_RESIZE:
214 215 216 217 218 219 220
                self.current_tab.resize(stdscr)
            elif key == 10:
                self.current_tab.send_message()
            self.current_tab.do_command(key)
            # else:
            #     sys.exit()
#            self.current_tab.input.do_command(key)
221

222 223 224 225 226 227 228 229 230 231 232 233
    def on_message(self, jid, msg, subject, typ, stanza):
        print "on_message", jid, msg, subject, typ

    def on_join_room(self, room, nick):
        print "on_join_room", room, nick

    def on_presence(self, jid, priority, show, status, stanza):
        print "on presence", jid, priority, show, status

    def get_input(self):
        return self.stdscr.getch()

234
def main(stdscr):
235
    gui = Gui(stdscr)
236
    gui.main_loop(stdscr)
237

238
if __name__ == '__main__':
239 240 241 242 243 244 245 246 247 248 249
    resource = config.get('resource')
    server = config.get('server')
    connection = Connection(server, resource)
    connection.start()
    rooms = config.get('rooms').split(':')
    from time import sleep
    print connection.online
    sleep(2)
    print connection.online
    for room in rooms:
        connection.send_join_room(room.split('/')[0], room.split('/')[1])
250
    curses.wrapper(main)