annotate frontends/primitivus/primitivus @ 119:ded2431cea5a

Primitivus: chat window / text sending. Primitivus has now the most basics features \o/ - core: new getVersion method - primitivus: new debug key (C-d), only work if SàT is in dev version (D in version) - quick_app: new post_init method, used for automatique task like auto-plug - primitivus: lists now use genericList (Box) or List (Flow) - primitivus: List now manage correctly its size - primitivus: new FocusFrame widget which manage focus changing with 'tab' - primitivus: advancedEdit now manage 'click' signal - primitivus: contactList now manager 'change' and 'click' signals - primitivus: Chat window now working
author Goffi <goffi@goffi.org>
date Mon, 05 Jul 2010 19:13:36 +0800
parents 76055a209ed9
children 1ca5f254ce41
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Primitivus: a SAT frontend
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from quick_frontend.quick_app import QuickApp
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from quick_frontend.quick_chat_list import QuickChatList
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from quick_frontend.quick_contact_list import QuickContactList
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from quick_frontend.quick_contact_management import QuickContactManagement
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import urwid
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from profile_manager import ProfileManager
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 113
diff changeset
29 from contact_list import ContactList
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
30 from chat import Chat
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
31 from custom_widgets import AdvancedEdit,FocusFrame
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import pdb
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 """from window import Window
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from editbox import EditBox
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from statusbar import StatusBar
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from chat import Chat
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from tools.jid import JID"""
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 import logging
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from logging import debug, info, error
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 #import locale
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 import sys, os
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 #from curses import ascii
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 #import locale
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 #from signal import signal, SIGWINCH
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 #import fcntl
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 #import struct
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 #import termios
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 #from boxsizer import BoxSizer
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 ### logging configuration FIXME: put this elsewhere ###
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 logging.basicConfig(level=logging.CRITICAL, #TODO: configure it to put messages in a log file
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 format='%(message)s')
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 ###
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 const_APP_NAME = "Primitivus"
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
57 const_PALETTE = [('title', 'black', 'light gray', 'standout,underline'),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
58 ('selected', 'default', 'dark red'),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
59 ('selected_focus', 'default,bold', 'dark red'),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
60 ('default', 'default', 'default'),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
61 ('default_focus', 'default,bold', 'default'),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
62 ]
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
64 class ChatList(QuickChatList):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
65 """This class manage the list of chat windows"""
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
66
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
67 def __init__(self, host):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
68 QuickChatList.__init__(self, host)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
69
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
70 def createChat(self, target):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
71 return Chat(target, self.host)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
72
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 class PrimitivusApp(QuickApp):
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 def __init__(self):
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 113
diff changeset
76 self.CM = QuickContactManagement() #FIXME: not the best place
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 QuickApp.__init__(self) #XXX: yes it's an unusual place for the constructor of a parent class, but the init order is important
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 ## main loop setup ##
117
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
80 self.main_widget = ProfileManager(self)
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.loop = urwid.MainLoop(self.main_widget, const_PALETTE, event_loop=urwid.GLibEventLoop(), unhandled_input=self.key_handler)
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
83 ##misc setup##
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
84 self.chat_wins=ChatList(self)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
85
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
86 def debug(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
87 """convenience method to reset screen and launch pdb"""
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
88 import os
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
89 os.system('reset')
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
90 print 'Entered debug mode'
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
91 pdb.set_trace()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
92
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
93 def redraw(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
94 """redraw the screen"""
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
95 self.loop.draw_screen()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
96
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def start(self):
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
98 self.i = 0
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
99 self.loop.set_alarm_in(0,lambda a,b: self.postInit())
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.loop.run()
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def key_handler(self, input):
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if input in ('q', 'Q') or input == 'ctrl x':
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 raise urwid.ExitMainLoop()
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
105 elif input == 'ctrl d' and 'D' in self.bridge.getVersion(): #Debug only for dev versions
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
106 self.debug()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
107
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 def __buildMainWidget(self):
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
109 self.contactList = ContactList(self, self.CM, on_click = self.contactSelected, on_change=lambda w: self.redraw())
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
110 #self.center_part = urwid.Columns([('weight',2,self.contactList),('weight',8,Chat('',self))])
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
111 self.center_part = urwid.Columns([('weight',2,self.contactList), ('weight',8,urwid.Filler(urwid.Text('')))])
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
112 editBar = AdvancedEdit('> ')
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
113 urwid.connect_signal(editBar,'click',self.onTextEntered)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
114 self.main_widget = FocusFrame(self.center_part, footer=editBar, focus_part='footer')
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
115 return self.main_widget
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 113
diff changeset
117 def plug_profile(self, profile_key='@DEFAULT@'):
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
118 self.loop.widget = self.__buildMainWidget()
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 113
diff changeset
119 QuickApp.plug_profile(self, profile_key)
117
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
120
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
121 def removePopUp(self):
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
122 self.loop.widget = self.main_widget
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
123
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
124 def showPopUp(self, pop_up_widget):
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
125 display_widget = urwid.Overlay(pop_up_widget, self.main_widget, 'center', ('relative', 40), 'middle', ('relative', 40))
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
126 self.loop.widget = display_widget
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
128 def contactSelected(self, contact_list):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
129 contact = contact_list.get_contact()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
130 if contact:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
131 assert(len(self.center_part.widget_list)==2)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
132 #self.center_part.widget_list.append(self.chat_wins[contact])
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
133 #self.center_part.column_types.append(('weight',8))
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
134 self.center_part.widget_list[1] = self.chat_wins[contact]
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
135
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
136 def onTextEntered(self, editBar):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
137 contact = self.contactList.get_contact()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
138 if contact:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
139 self.bridge.sendMessage(contact, editBar.get_edit_text(), profile_key=self.profile)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
140 editBar.set_edit_text('')
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
141
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 sat = PrimitivusApp()
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 sat.start()
117
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
144