Mercurial > libervia-backend
annotate frontends/sortilege_old/chat.py @ 402:f03688bdb858
jp: use with statement to open fifo
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 07 Oct 2011 11:19:15 +0200 |
parents | b1794cbb88e5 |
children | 2a072735e459 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 sortilege: a SAT frontend | |
228 | 6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
0 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 | |
23 | |
24 import os.path | |
25 import pdb | |
26 from logging import debug, info, error | |
27 from window import Window | |
28 import os | |
29 from time import time, localtime, strftime | |
30 import curses | |
31 import curses.ascii as ascii | |
32 from tools.jid import JID | |
33 from quick_frontend.quick_chat import QuickChat | |
34 | |
35 | |
36 def C(k): | |
37 """return the value of Ctrl+key""" | |
38 return ord(ascii.ctrl(k)) | |
39 | |
40 class Chat(Window, QuickChat): | |
41 | |
42 def __init__(self, to_id, host): | |
43 QuickChat.__init__(self, to_id, host) | |
44 self.__parent=host.stdscr | |
45 self.to_id=JID(to_id) | |
46 self.content=[] | |
47 self.__scollIdx=0 | |
48 Window.__init__(self, self.__parent, self.__parent.getmaxyx()[0]-2, self.__parent.getmaxyx()[1]-30, 0,30, code=host.code) | |
49 | |
50 #history | |
67
0e50dd3a234a
message sending bug fixes + sortilege update
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
51 self.historyPrint(50, True, profile=self.host.profile) |
0 | 52 |
53 self.hide() | |
54 | |
55 def resize(self, height, width, y, x): | |
56 Window.resize(self, height, width, y, x) | |
57 self.update() | |
58 | |
59 def resizeAdapt(self): | |
60 """Adapt window size to self.__parent size. | |
61 Must be called when self.__parent is resized.""" | |
62 self.resize(self.__parent.getmaxyx()[0]-2, self.__parent.getmaxyx()[1]-30, 0,30) | |
63 self.update() | |
64 | |
65 def __getHeader(self, line): | |
66 """Return the header of a line (eg: "[12:34] <toto> ").""" | |
67 header='' | |
68 if self.host.chatParams["timestamp"]: | |
69 header = header + '[%s] ' % strftime("%H:%M", localtime(float(line[0]))) | |
70 if self.host.chatParams['short_nick']: | |
67
0e50dd3a234a
message sending bug fixes + sortilege update
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
71 header = header + ('> ' if line[1]==self.host.profiles[self.host.profile]['whoami'] else '** ') |
0 | 72 else: |
73 header = header + '<%s> ' % line[1] | |
74 return header | |
75 | |
76 def update(self): | |
77 if self.isHidden(): | |
78 return | |
79 Window.update(self) | |
80 content=[] #what is really printed | |
81 irange=range(len(self.content)) | |
82 irange.reverse() #we print the text upward | |
83 for idx in irange: | |
84 header=self.__getHeader(self.content[idx]) | |
85 msg=self.content[idx][2] | |
86 part=0 #part of the text | |
67
0e50dd3a234a
message sending bug fixes + sortilege update
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
87 if JID(self.content[idx][1]).short==self.host.profiles[self.host.profile]['whoami'].short: |
0 | 88 att_header=curses.color_pair(1) |
89 else: | |
90 att_header=curses.color_pair(2) | |
91 | |
92 while (msg): | |
93 if part==0: | |
94 hd=header | |
95 att=att_header | |
96 max=self.rWidth-len(header) | |
97 else: | |
98 hd="" | |
99 att=0 | |
100 max=self.rWidth | |
101 | |
102 LF = msg.find('\n') #we look for Line Feed | |
103 if LF != -1 and LF < max: | |
104 max = LF | |
105 next = max + 1 #we skip the LF | |
106 else: | |
107 next = max | |
108 | |
109 content.insert(part,[att,hd, msg[:max]]) | |
110 msg=msg[next:] #we erase treated part | |
111 part=part+1 | |
112 | |
113 if len(content)>=self.rHeight+self.__scollIdx: | |
114 #all the screen is filled, we can continue | |
115 break | |
116 | |
117 if self.__scollIdx>0 and len(content)<self.rHeight+self.__scollIdx: | |
118 self.__scollIdx=abs(len(content)-self.rHeight) #all the log fit on the screen, we must stop here | |
119 | |
120 idx=0 | |
121 for line in content[-self.rHeight-self.__scollIdx : -self.__scollIdx or None]: | |
122 self.addYXStr(idx, 0, line[1], line[0]) | |
123 self.addYXStr(idx, len(line[1]), line[2]) | |
124 idx=idx+1 | |
125 | |
126 self.noutrefresh() | |
127 | |
128 def scrollIdxUp(self): | |
129 """increment scroll index""" | |
130 self.__scollIdx = self.__scollIdx + 1 | |
131 self.update() | |
132 | |
133 def scrollIdxDown(self): | |
134 """decrement scroll index""" | |
135 if self.__scollIdx > 0: | |
136 self.__scollIdx = self.__scollIdx - 1 | |
137 self.update() | |
138 | |
67
0e50dd3a234a
message sending bug fixes + sortilege update
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
139 def printMessage(self, jid, msg, profile, timestamp=""): |
0 | 140 if timestamp=="": |
141 current_time=time() | |
142 timestamp=str(current_time) | |
143 if self.last_history and current_time - float(self.last_history) < 5: #FIXME: Q&D fix to avoid double print on new chat window | |
144 return | |
145 self.content.append([timestamp,jid.short,msg]) | |
146 self.update() | |
147 | |
148 def handleKey(self, k): | |
149 if k == C('p'): | |
150 self.scrollIdxUp() | |
151 elif k == C('n'): | |
152 self.scrollIdxDown() | |
153 |