comparison browser_side/dialog.py @ 268:79970bf6af93

browser_side: added class RoomAndContactsChooser: - unified UI for all the MUC menu items callbacks to join a room, invite people and start a game
author souliane <souliane@mailoo.org>
date Sun, 17 Nov 2013 17:57:14 +0100
parents 30c01671e338
children d868181d0649
comparison
equal deleted inserted replaced
267:a76243c02074 268:79970bf6af93
18 You should have received a copy of the GNU Affero General Public License 18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 from pyjamas.ui.VerticalPanel import VerticalPanel 22 from pyjamas.ui.VerticalPanel import VerticalPanel
23 from pyjamas.ui.Grid import Grid
23 from pyjamas.ui.HorizontalPanel import HorizontalPanel 24 from pyjamas.ui.HorizontalPanel import HorizontalPanel
24 from pyjamas.ui.PopupPanel import PopupPanel 25 from pyjamas.ui.PopupPanel import PopupPanel
25 from pyjamas.ui.DialogBox import DialogBox 26 from pyjamas.ui.DialogBox import DialogBox
26 from pyjamas.ui.ListBox import ListBox 27 from pyjamas.ui.ListBox import ListBox
27 from pyjamas.ui.Button import Button 28 from pyjamas.ui.Button import Button
28 from pyjamas.ui.TextBox import TextBox 29 from pyjamas.ui.TextBox import TextBox
29 from pyjamas.ui.Label import Label 30 from pyjamas.ui.Label import Label
30 from pyjamas.ui.HTML import HTML 31 from pyjamas.ui.HTML import HTML
31 from pyjamas.ui.Frame import Frame 32 from pyjamas.ui.RadioButton import RadioButton
32 from pyjamas.ui import HasAlignment 33 from pyjamas.ui import HasAlignment
33 from pyjamas.ui.KeyboardListener import KEY_ESCAPE, KEY_ENTER 34 from pyjamas.ui.KeyboardListener import KEY_ESCAPE, KEY_ENTER
34 from pyjamas.ui.MouseListener import MouseWheelHandler 35 from pyjamas.ui.MouseListener import MouseWheelHandler
35 from pyjamas import DOM
36 from pyjamas import Window 36 from pyjamas import Window
37 from browser_side import panels
38 from sat.tools.frontends.misc import DEFAULT_MUC
37 39
38 # List here the patterns that are not allowed in contact group names 40 # List here the patterns that are not allowed in contact group names
39 FORBIDDEN_PATTERNS_IN_GROUP = () 41 FORBIDDEN_PATTERNS_IN_GROUP = ()
40 42
41 43
42 class ContactsChooser(DialogBox): 44 class RoomChooser(Grid):
43 45 """Select a room from the rooms you already joined, or create a new one"""
44 def __init__(self, host, callback, nb_contact=None, text='Please select contacts'): 46
45 """ 47 GENERATE_MUC = "<use random name>"
46 ContactsChooser allow to select one or several connected contacts 48
49 def __init__(self, host, default_room=DEFAULT_MUC):
50 Grid.__init__(self, 2, 2, Width='100%')
51 self.host = host
52
53 self.exist_radio = RadioButton("room", "Select discussion room:")
54 self.rooms_list = ListBox(Width='100%')
55 self.setRooms()
56 self.add(self.exist_radio, 0, 0)
57 self.add(self.rooms_list, 0, 1)
58
59 self.new_radio = RadioButton("room", "New discussion room:")
60 self.new_radio.setChecked(True)
61 self.box = TextBox(Width='100%')
62 self.box.setText(self.GENERATE_MUC if default_room == "" else default_room)
63 self.add(self.new_radio, 1, 0)
64 self.add(self.box, 1, 1)
65
66 self.rooms_list.addFocusListener(self)
67 self.box.addFocusListener(self)
68
69 def onFocus(self, sender):
70 if sender == self.rooms_list:
71 self.exist_radio.setChecked(True)
72 elif sender == self.box:
73 if self.box.getText() == self.GENERATE_MUC:
74 self.box.setText("")
75 self.new_radio.setChecked(True)
76
77 def onLostFocus(self, sender):
78 if sender == self.box:
79 if self.box.getText() == "":
80 self.box.setText(self.GENERATE_MUC)
81
82 def setRooms(self, room_data):
83 for room in self.host.room_list:
84 self.rooms_list.addItem(room.bare)
85
86 def getRoom(self):
87 if self.exist_radio.getChecked():
88 values = self.rooms_list.getSelectedValues()
89 return "" if values == [] else values[0]
90 value = self.box.getText()
91 return "" if value == self.GENERATE_MUC else value
92
93
94 class ContactsChooser(VerticalPanel):
95 """Select one or several connected contacts"""
96
97 def __init__(self, host, nb_contact=None, ok_button=None):
98 """
47 @param host: SatWebFrontend instance 99 @param host: SatWebFrontend instance
48 @param callback: method to call when contacts have been choosed
49 @param nb_contact: number of contacts that have to be selected, None for no limit 100 @param nb_contact: number of contacts that have to be selected, None for no limit
50 If a tuple is given instead of an integer, nb_contact[0] is the minimal and 101 If a tuple is given instead of an integer, nb_contact[0] is the minimal and
51 nb_contact[1] is the maximal number of contacts to be chosen. 102 nb_contact[1] is the maximal number of contacts to be chosen.
52 """ 103 """
53 self.host = host 104 self.host = host
54 self.callback = callback
55 if isinstance(nb_contact, tuple): 105 if isinstance(nb_contact, tuple):
56 if len(nb_contact) == 0: 106 if len(nb_contact) == 0:
57 nb_contact = None 107 nb_contact = None
58 elif len(nb_contact) == 1: 108 elif len(nb_contact) == 1:
59 nb_contact = (nb_contact[0], nb_contact[0]) 109 nb_contact = (nb_contact[0], nb_contact[0])
62 if nb_contact is None: 112 if nb_contact is None:
63 print "Need to select as many contacts as you want" 113 print "Need to select as many contacts as you want"
64 else: 114 else:
65 print "Need to select between %d and %d contacts" % nb_contact 115 print "Need to select between %d and %d contacts" % nb_contact
66 self.nb_contact = nb_contact 116 self.nb_contact = nb_contact
117 self.ok_button = ok_button
118 VerticalPanel.__init__(self, Width='100%')
119 self.contacts_list = ListBox()
120 self.contacts_list.setMultipleSelect(True)
121 self.contacts_list.setWidth("95%")
122 self.contacts_list.addStyleName('contactsChooser')
123 self.contacts_list.addChangeListener(self.onChange)
124 self.add(self.contacts_list)
125 self.setContacts()
126 self.onChange()
127
128 def onChange(self, sender=None):
129 if self.ok_button is None:
130 return
131 if self.nb_contact:
132 selected = len(self.contacts_list.getSelectedValues(True))
133 if selected >= self.nb_contact[0] and selected <= self.nb_contact[1]:
134 self.ok_button.setEnabled(True)
135 else:
136 self.ok_button.setEnabled(False)
137
138 def setContacts(self, selected=[]):
139 """Fill the list with the connected contacts
140 @param select: list of the contacts to select by default
141 """
142 self.contacts_list.clear()
143 contacts = self.host.contact_panel.getConnected()
144 self.contacts_list.setVisibleItemCount(10 if len(contacts) > 5 else 5)
145 self.contacts_list.addItem("")
146 for contact in contacts:
147 if contact not in [room.bare for room in self.host.room_list]:
148 self.contacts_list.addItem(contact)
149 self.contacts_list.setItemTextSelection(selected)
150
151 def getContacts(self):
152 return self.contacts_list.getSelectedValues(True)
153
154
155 class RoomAndContactsChooser(DialogBox):
156 """Select a room and some users to invite in"""
157
158 def __init__(self, host, callback, nb_contact=None, ok_button="OK", title="Group discussions",
159 title_room="Join room", title_invite="Invite contacts", visible=(True, True)):
67 DialogBox.__init__(self, centered=True) 160 DialogBox.__init__(self, centered=True)
68 content = VerticalPanel() 161 self.host = host
69 content.setWidth('100%') 162 self.callback = callback
70 self.contacts_list = ListBox() 163 self.title_room = title_room
71 self.contacts_list.setVisibleItemCount(10) 164 self.title_invite = title_invite
72 self.contacts_list.setMultipleSelect(True) 165
73 self.contacts_list.setWidth("100%")
74 self.contacts_list.setStyleName('contactsChooser')
75 self.contacts_list.addChangeListener(self.onChange)
76 content.add(self.contacts_list)
77 button_panel = HorizontalPanel() 166 button_panel = HorizontalPanel()
78 button_panel.addStyleName("marginAuto") 167 button_panel.addStyleName("marginAuto")
79 self.choose_button = Button("Choose", self.onChoose) 168 ok_button = Button("OK", self.onOK)
80 if nb_contact and nb_contact[0] > 0: 169 button_panel.add(ok_button)
81 self.choose_button.setEnabled(False)
82 button_panel.add(self.choose_button)
83 button_panel.add(Button("Cancel", self.onCancel)) 170 button_panel.add(Button("Cancel", self.onCancel))
84 content.add(button_panel) 171
85 self.setHTML(text) 172 self.room_panel = RoomChooser(host, "" if visible == (False, True) else DEFAULT_MUC)
86 self.setWidget(content) 173 self.contact_panel = ContactsChooser(host, nb_contact, ok_button)
87 174
88 def onChange(self, sender): 175 self.stack_panel = panels.ToggleStackPanel(Width="100%")
89 if self.nb_contact: 176 self.stack_panel.add(self.room_panel, title_room, visible=visible[0])
90 selected = len(self.contacts_list.getSelectedValues()) 177 self.stack_panel.add(self.contact_panel, title_invite, visible=visible[1])
91 if selected >= self.nb_contact[0] and selected <= self.nb_contact[1]: 178 self.stack_panel.addStackChangeListener(self)
92 self.choose_button.setEnabled(True) 179 main_panel = VerticalPanel()
93 else: 180 main_panel.setStyleName("room-contact-chooser")
94 self.choose_button.setEnabled(False) 181 main_panel.add(self.stack_panel)
95 182 main_panel.add(button_panel)
96 def getContacts(self): 183
97 """ 184 self.setWidget(main_panel)
98 Actually ask to choose the contacts 185 self.setHTML(title)
99 """
100 self.contacts_list.clear()
101 for contact in self.host.contact_panel.getConnected():
102 if contact not in [room.bare for room in self.host.room_list]:
103 self.contacts_list.addItem(contact)
104 self.show() 186 self.show()
105 187
106 def onChoose(self, sender): 188 def getRoom(self, asSuffix=False):
107 self.hide() 189 room = self.room_panel.getRoom()
108 self.callback(self.contacts_list.getSelectedValues()) 190 if asSuffix:
191 return room if room == "" else ": %s" % room
192 else:
193 return room
194
195 def getContacts(self, asSuffix=False):
196 contacts = self.contact_panel.getContacts()
197 if asSuffix:
198 return "" if contacts == [] else ": %s" % ", ".join(contacts)
199 else:
200 return contacts
201
202 def onStackChanged(self, sender, index, visible=None):
203 if visible is None:
204 visible = sender.getWidget(index).getVisible()
205 if index == 0:
206 sender.setStackText(0, self.title_room + ("" if visible else self.getRoom(True)))
207 elif index == 1:
208 sender.setStackText(1, self.title_invite + ("" if visible else self.getContacts(True)))
209 if visible:
210 # update the contacts list in case someone recently logged in/out
211 self.contact_panel.setContacts(self.getContacts())
212
213 def onOK(self, sender):
214 room_jid = self.getRoom()
215 if room_jid != "" and "@" not in room_jid:
216 Window.alert('You must enter a room jid in the form room@chat.%s' % self.host._defaultDomain)
217 self.hide()
218 self.callback(room_jid, self.getContacts())
109 219
110 def onCancel(self, sender): 220 def onCancel(self, sender):
111 self.hide() 221 self.hide()
112 222
113 223