annotate frontends/sortilege/chat.py @ 0:c4bc297b82f0

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