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