annotate frontends/sortilege_old/chat.py @ 673:903c4749de15

moved sat.tools.frontend to sat.tools.frontends
author Goffi <goffi@goffi.org>
date Fri, 08 Nov 2013 16:35:51 +0100
parents 84a6e83157c2
children f7878ad3c846
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
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
19
goffi@necton2
parents:
diff changeset
20
goffi@necton2
parents:
diff changeset
21
goffi@necton2
parents:
diff changeset
22 import os.path
goffi@necton2
parents:
diff changeset
23 import pdb
goffi@necton2
parents:
diff changeset
24 from logging import debug, info, error
goffi@necton2
parents:
diff changeset
25 from window import Window
goffi@necton2
parents:
diff changeset
26 import os
goffi@necton2
parents:
diff changeset
27 from time import time, localtime, strftime
goffi@necton2
parents:
diff changeset
28 import curses
goffi@necton2
parents:
diff changeset
29 import curses.ascii as ascii
goffi@necton2
parents:
diff changeset
30 from tools.jid import JID
goffi@necton2
parents:
diff changeset
31 from quick_frontend.quick_chat import QuickChat
goffi@necton2
parents:
diff changeset
32
goffi@necton2
parents:
diff changeset
33
goffi@necton2
parents:
diff changeset
34 def C(k):
goffi@necton2
parents:
diff changeset
35 """return the value of Ctrl+key"""
goffi@necton2
parents:
diff changeset
36 return ord(ascii.ctrl(k))
goffi@necton2
parents:
diff changeset
37
goffi@necton2
parents:
diff changeset
38 class Chat(Window, QuickChat):
goffi@necton2
parents:
diff changeset
39
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
42 self.__parent=host.stdscr
goffi@necton2
parents:
diff changeset
43 self.to_id=JID(to_id)
goffi@necton2
parents:
diff changeset
44 self.content=[]
goffi@necton2
parents:
diff changeset
45 self.__scollIdx=0
goffi@necton2
parents:
diff changeset
46 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
47
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
50
goffi@necton2
parents:
diff changeset
51 self.hide()
goffi@necton2
parents:
diff changeset
52
goffi@necton2
parents:
diff changeset
53 def resize(self, height, width, y, x):
goffi@necton2
parents:
diff changeset
54 Window.resize(self, height, width, y, x)
goffi@necton2
parents:
diff changeset
55 self.update()
goffi@necton2
parents:
diff changeset
56
goffi@necton2
parents:
diff changeset
57 def resizeAdapt(self):
goffi@necton2
parents:
diff changeset
58 """Adapt window size to self.__parent size.
goffi@necton2
parents:
diff changeset
59 Must be called when self.__parent is resized."""
goffi@necton2
parents:
diff changeset
60 self.resize(self.__parent.getmaxyx()[0]-2, self.__parent.getmaxyx()[1]-30, 0,30)
goffi@necton2
parents:
diff changeset
61 self.update()
goffi@necton2
parents:
diff changeset
62
goffi@necton2
parents:
diff changeset
63 def __getHeader(self, line):
goffi@necton2
parents:
diff changeset
64 """Return the header of a line (eg: "[12:34] <toto> ")."""
goffi@necton2
parents:
diff changeset
65 header=''
goffi@necton2
parents:
diff changeset
66 if self.host.chatParams["timestamp"]:
goffi@necton2
parents:
diff changeset
67 header = header + '[%s] ' % strftime("%H:%M", localtime(float(line[0])))
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
70 else:
goffi@necton2
parents:
diff changeset
71 header = header + '<%s> ' % line[1]
goffi@necton2
parents:
diff changeset
72 return header
goffi@necton2
parents:
diff changeset
73
goffi@necton2
parents:
diff changeset
74 def update(self):
goffi@necton2
parents:
diff changeset
75 if self.isHidden():
goffi@necton2
parents:
diff changeset
76 return
goffi@necton2
parents:
diff changeset
77 Window.update(self)
goffi@necton2
parents:
diff changeset
78 content=[] #what is really printed
goffi@necton2
parents:
diff changeset
79 irange=range(len(self.content))
goffi@necton2
parents:
diff changeset
80 irange.reverse() #we print the text upward
goffi@necton2
parents:
diff changeset
81 for idx in irange:
goffi@necton2
parents:
diff changeset
82 header=self.__getHeader(self.content[idx])
goffi@necton2
parents:
diff changeset
83 msg=self.content[idx][2]
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
86 att_header=curses.color_pair(1)
goffi@necton2
parents:
diff changeset
87 else:
goffi@necton2
parents:
diff changeset
88 att_header=curses.color_pair(2)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 480
diff changeset
89
0
goffi@necton2
parents:
diff changeset
90 while (msg):
goffi@necton2
parents:
diff changeset
91 if part==0:
goffi@necton2
parents:
diff changeset
92 hd=header
goffi@necton2
parents:
diff changeset
93 att=att_header
goffi@necton2
parents:
diff changeset
94 max=self.rWidth-len(header)
goffi@necton2
parents:
diff changeset
95 else:
goffi@necton2
parents:
diff changeset
96 hd=""
goffi@necton2
parents:
diff changeset
97 att=0
goffi@necton2
parents:
diff changeset
98 max=self.rWidth
goffi@necton2
parents:
diff changeset
99
goffi@necton2
parents:
diff changeset
100 LF = msg.find('\n') #we look for Line Feed
goffi@necton2
parents:
diff changeset
101 if LF != -1 and LF < max:
goffi@necton2
parents:
diff changeset
102 max = LF
goffi@necton2
parents:
diff changeset
103 next = max + 1 #we skip the LF
goffi@necton2
parents:
diff changeset
104 else:
goffi@necton2
parents:
diff changeset
105 next = max
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 480
diff changeset
106
0
goffi@necton2
parents:
diff changeset
107 content.insert(part,[att,hd, msg[:max]])
goffi@necton2
parents:
diff changeset
108 msg=msg[next:] #we erase treated part
goffi@necton2
parents:
diff changeset
109 part=part+1
goffi@necton2
parents:
diff changeset
110
goffi@necton2
parents:
diff changeset
111 if len(content)>=self.rHeight+self.__scollIdx:
goffi@necton2
parents:
diff changeset
112 #all the screen is filled, we can continue
goffi@necton2
parents:
diff changeset
113 break
goffi@necton2
parents:
diff changeset
114
goffi@necton2
parents:
diff changeset
115 if self.__scollIdx>0 and len(content)<self.rHeight+self.__scollIdx:
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
118 idx=0
goffi@necton2
parents:
diff changeset
119 for line in content[-self.rHeight-self.__scollIdx : -self.__scollIdx or None]:
goffi@necton2
parents:
diff changeset
120 self.addYXStr(idx, 0, line[1], line[0])
goffi@necton2
parents:
diff changeset
121 self.addYXStr(idx, len(line[1]), line[2])
goffi@necton2
parents:
diff changeset
122 idx=idx+1
goffi@necton2
parents:
diff changeset
123
goffi@necton2
parents:
diff changeset
124 self.noutrefresh()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 480
diff changeset
125
0
goffi@necton2
parents:
diff changeset
126 def scrollIdxUp(self):
goffi@necton2
parents:
diff changeset
127 """increment scroll index"""
goffi@necton2
parents:
diff changeset
128 self.__scollIdx = self.__scollIdx + 1
goffi@necton2
parents:
diff changeset
129 self.update()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 480
diff changeset
130
0
goffi@necton2
parents:
diff changeset
131 def scrollIdxDown(self):
goffi@necton2
parents:
diff changeset
132 """decrement scroll index"""
goffi@necton2
parents:
diff changeset
133 if self.__scollIdx > 0:
goffi@necton2
parents:
diff changeset
134 self.__scollIdx = self.__scollIdx - 1
goffi@necton2
parents:
diff changeset
135 self.update()
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
138 if timestamp=="":
goffi@necton2
parents:
diff changeset
139 current_time=time()
goffi@necton2
parents:
diff changeset
140 timestamp=str(current_time)
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
142 return
goffi@necton2
parents:
diff changeset
143 self.content.append([timestamp,jid.short,msg])
goffi@necton2
parents:
diff changeset
144 self.update()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 480
diff changeset
145
0
goffi@necton2
parents:
diff changeset
146 def handleKey(self, k):
goffi@necton2
parents:
diff changeset
147 if k == C('p'):
goffi@necton2
parents:
diff changeset
148 self.scrollIdxUp()
goffi@necton2
parents:
diff changeset
149 elif k == C('n'):
goffi@necton2
parents:
diff changeset
150 self.scrollIdxDown()
goffi@necton2
parents:
diff changeset
151