annotate frontends/sortilege_old/chat.py @ 265:b5f1f3dc9ac6

bridge: automatic bridge generator, first draft
author Goffi <goffi@goffi.org>
date Mon, 24 Jan 2011 01:22:00 +0100
parents b1794cbb88e5
children 2a072735e459
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
228
b1794cbb88e5 2011 copyright upgrade
Goffi <goffi@goffi.org>
parents: 112
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
0
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
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
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']:
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
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 return
goffi@necton2
parents:
diff changeset
79 Window.update(self)
goffi@necton2
parents:
diff changeset
80 content=[] #what is really printed
goffi@necton2
parents:
diff changeset
81 irange=range(len(self.content))
goffi@necton2
parents:
diff changeset
82 irange.reverse() #we print the text upward
goffi@necton2
parents:
diff changeset
83 for idx in irange:
goffi@necton2
parents:
diff changeset
84 header=self.__getHeader(self.content[idx])
goffi@necton2
parents:
diff changeset
85 msg=self.content[idx][2]
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
88 att_header=curses.color_pair(1)
goffi@necton2
parents:
diff changeset
89 else:
goffi@necton2
parents:
diff changeset
90 att_header=curses.color_pair(2)
goffi@necton2
parents:
diff changeset
91
goffi@necton2
parents:
diff changeset
92 while (msg):
goffi@necton2
parents:
diff changeset
93 if part==0:
goffi@necton2
parents:
diff changeset
94 hd=header
goffi@necton2
parents:
diff changeset
95 att=att_header
goffi@necton2
parents:
diff changeset
96 max=self.rWidth-len(header)
goffi@necton2
parents:
diff changeset
97 else:
goffi@necton2
parents:
diff changeset
98 hd=""
goffi@necton2
parents:
diff changeset
99 att=0
goffi@necton2
parents:
diff changeset
100 max=self.rWidth
goffi@necton2
parents:
diff changeset
101
goffi@necton2
parents:
diff changeset
102 LF = msg.find('\n') #we look for Line Feed
goffi@necton2
parents:
diff changeset
103 if LF != -1 and LF < max:
goffi@necton2
parents:
diff changeset
104 max = LF
goffi@necton2
parents:
diff changeset
105 next = max + 1 #we skip the LF
goffi@necton2
parents:
diff changeset
106 else:
goffi@necton2
parents:
diff changeset
107 next = max
goffi@necton2
parents:
diff changeset
108
goffi@necton2
parents:
diff changeset
109 content.insert(part,[att,hd, msg[:max]])
goffi@necton2
parents:
diff changeset
110 msg=msg[next:] #we erase treated part
goffi@necton2
parents:
diff changeset
111 part=part+1
goffi@necton2
parents:
diff changeset
112
goffi@necton2
parents:
diff changeset
113 if len(content)>=self.rHeight+self.__scollIdx:
goffi@necton2
parents:
diff changeset
114 #all the screen is filled, we can continue
goffi@necton2
parents:
diff changeset
115 break
goffi@necton2
parents:
diff changeset
116
goffi@necton2
parents:
diff changeset
117 if self.__scollIdx>0 and len(content)<self.rHeight+self.__scollIdx:
goffi@necton2
parents:
diff changeset
118 self.__scollIdx=abs(len(content)-self.rHeight) #all the log fit on the screen, we must stop here
goffi@necton2
parents:
diff changeset
119
goffi@necton2
parents:
diff changeset
120 idx=0
goffi@necton2
parents:
diff changeset
121 for line in content[-self.rHeight-self.__scollIdx : -self.__scollIdx or None]:
goffi@necton2
parents:
diff changeset
122 self.addYXStr(idx, 0, line[1], line[0])
goffi@necton2
parents:
diff changeset
123 self.addYXStr(idx, len(line[1]), line[2])
goffi@necton2
parents:
diff changeset
124 idx=idx+1
goffi@necton2
parents:
diff changeset
125
goffi@necton2
parents:
diff changeset
126 self.noutrefresh()
goffi@necton2
parents:
diff changeset
127
goffi@necton2
parents:
diff changeset
128 def scrollIdxUp(self):
goffi@necton2
parents:
diff changeset
129 """increment scroll index"""
goffi@necton2
parents:
diff changeset
130 self.__scollIdx = self.__scollIdx + 1
goffi@necton2
parents:
diff changeset
131 self.update()
goffi@necton2
parents:
diff changeset
132
goffi@necton2
parents:
diff changeset
133 def scrollIdxDown(self):
goffi@necton2
parents:
diff changeset
134 """decrement scroll index"""
goffi@necton2
parents:
diff changeset
135 if self.__scollIdx > 0:
goffi@necton2
parents:
diff changeset
136 self.__scollIdx = self.__scollIdx - 1
goffi@necton2
parents:
diff changeset
137 self.update()
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
140 if timestamp=="":
goffi@necton2
parents:
diff changeset
141 current_time=time()
goffi@necton2
parents:
diff changeset
142 timestamp=str(current_time)
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
144 return
goffi@necton2
parents:
diff changeset
145 self.content.append([timestamp,jid.short,msg])
goffi@necton2
parents:
diff changeset
146 self.update()
goffi@necton2
parents:
diff changeset
147
goffi@necton2
parents:
diff changeset
148 def handleKey(self, k):
goffi@necton2
parents:
diff changeset
149 if k == C('p'):
goffi@necton2
parents:
diff changeset
150 self.scrollIdxUp()
goffi@necton2
parents:
diff changeset
151 elif k == C('n'):
goffi@necton2
parents:
diff changeset
152 self.scrollIdxDown()
goffi@necton2
parents:
diff changeset
153