comparison src/browser/sat_browser/dialog.py @ 626:ba2555c29c84 frontends_multi_profiles

browser_side: fixes RoomAndContactsChooser dialog
author souliane <souliane@mailoo.org>
date Sun, 22 Feb 2015 21:56:18 +0100
parents ed9cd20260ff
children 63697f082e8a
comparison
equal deleted inserted replaced
625:ed9cd20260ff 626:ba2555c29c84
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.log import getLogger 20 from sat.core.log import getLogger
21 log = getLogger(__name__) 21 log = getLogger(__name__)
22
23 from constants import Const as C
22 from sat_frontends.tools.misc import DEFAULT_MUC 24 from sat_frontends.tools.misc import DEFAULT_MUC
25 from sat_frontends.tools import jid
23 26
24 from pyjamas.ui.VerticalPanel import VerticalPanel 27 from pyjamas.ui.VerticalPanel import VerticalPanel
25 from pyjamas.ui.Grid import Grid 28 from pyjamas.ui.Grid import Grid
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel 29 from pyjamas.ui.HorizontalPanel import HorizontalPanel
27 from pyjamas.ui.PopupPanel import PopupPanel 30 from pyjamas.ui.PopupPanel import PopupPanel
42 45
43 # List here the patterns that are not allowed in contact group names 46 # List here the patterns that are not allowed in contact group names
44 FORBIDDEN_PATTERNS_IN_GROUP = () 47 FORBIDDEN_PATTERNS_IN_GROUP = ()
45 48
46 49
50 unicode = str # XXX: pyjama doesn't manage unicode
51
52
47 class RoomChooser(Grid): 53 class RoomChooser(Grid):
48 """Select a room from the rooms you already joined, or create a new one""" 54 """Select a room from the rooms you already joined, or create a new one"""
49 55
50 GENERATE_MUC = "<use random name>" 56 GENERATE_MUC = "<use random name>"
51 57
52 def __init__(self, host, default_room=DEFAULT_MUC): 58 def __init__(self, host, default_room=DEFAULT_MUC):
59 """
60
61 @param host (SatWebFrontend)
62 """
53 Grid.__init__(self, 2, 2, Width='100%') 63 Grid.__init__(self, 2, 2, Width='100%')
54 self.host = host 64 self.host = host
55 65
56 self.new_radio = RadioButton("room", "Discussion room:") 66 self.new_radio = RadioButton("room", "Discussion room:")
57 self.new_radio.setChecked(True) 67 self.new_radio.setChecked(True)
68 self.box.addFocusListener(self) 78 self.box.addFocusListener(self)
69 self.rooms_list.addFocusListener(self) 79 self.rooms_list.addFocusListener(self)
70 80
71 self.exist_radio.setVisible(False) 81 self.exist_radio.setVisible(False)
72 self.rooms_list.setVisible(False) 82 self.rooms_list.setVisible(False)
73 self.setRooms() 83 self.refreshOptions()
84
85 @property
86 def room(self):
87 """Get the room that has been selected or entered by the user
88
89 @return: jid.JID or None
90 """
91 if self.exist_radio.getChecked():
92 values = self.rooms_list.getSelectedValues()
93 return jid.JID(values[0]) if values else None
94 value = self.box.getText()
95 return None if value == self.GENERATE_MUC else jid.JID(value)
74 96
75 def onFocus(self, sender): 97 def onFocus(self, sender):
76 if sender == self.rooms_list: 98 if sender == self.rooms_list:
77 self.exist_radio.setChecked(True) 99 self.exist_radio.setChecked(True)
78 elif sender == self.box: 100 elif sender == self.box:
83 def onLostFocus(self, sender): 105 def onLostFocus(self, sender):
84 if sender == self.box: 106 if sender == self.box:
85 if self.box.getText() == "": 107 if self.box.getText() == "":
86 self.box.setText(self.GENERATE_MUC) 108 self.box.setText(self.GENERATE_MUC)
87 109
88 def setRooms(self): 110 def refreshOptions(self):
89 for room in self.host.room_list: 111 """Refresh the already joined room list"""
112 contact_list = self.host.contact_list
113 muc_rooms = contact_list.getSpecials(C.CONTACT_SPECIAL_GROUP)
114 for room in muc_rooms:
90 self.rooms_list.addItem(room.bare) 115 self.rooms_list.addItem(room.bare)
91 if len(self.host.room_list) > 0: 116 if len(muc_rooms) > 0:
92 self.exist_radio.setVisible(True) 117 self.exist_radio.setVisible(True)
93 self.rooms_list.setVisible(True) 118 self.rooms_list.setVisible(True)
94 self.exist_radio.setChecked(True) 119 self.exist_radio.setChecked(True)
95
96 def getRoom(self):
97 if self.exist_radio.getChecked():
98 values = self.rooms_list.getSelectedValues()
99 return "" if values == [] else values[0]
100 value = self.box.getText()
101 return "" if value == self.GENERATE_MUC else value
102 120
103 121
104 class ContactsChooser(VerticalPanel): 122 class ContactsChooser(VerticalPanel):
105 """Select one or several connected contacts""" 123 """Select one or several connected contacts"""
106 124
130 self.contacts_list.setMultipleSelect(True) 148 self.contacts_list.setMultipleSelect(True)
131 self.contacts_list.setWidth("95%") 149 self.contacts_list.setWidth("95%")
132 self.contacts_list.addStyleName('contactsChooser') 150 self.contacts_list.addStyleName('contactsChooser')
133 self.contacts_list.addChangeListener(self.onChange) 151 self.contacts_list.addChangeListener(self.onChange)
134 self.add(self.contacts_list) 152 self.add(self.contacts_list)
135 self.setContacts() 153 self.refreshOptions()
136 self.onChange() 154 self.onChange()
155
156 @property
157 def contacts(self):
158 """Return the selected contacts.
159
160 @return: list[jid.JID]
161 """
162 return [jid.JID(contact) for contact in self.contacts_list.getSelectedValues(True)]
137 163
138 def onChange(self, sender=None): 164 def onChange(self, sender=None):
139 if self.ok_button is None: 165 if self.ok_button is None:
140 return 166 return
141 if self.nb_contact: 167 if self.nb_contact:
142 selected = len(self.contacts_list.getSelectedValues(True)) 168 selected = len(self.contacts_list.getSelectedValues(True))
143 if selected >= self.nb_contact[0] and selected <= self.nb_contact[1]: 169 if selected >= self.nb_contact[0] and selected <= self.nb_contact[1]:
144 self.ok_button.setEnabled(True) 170 self.ok_button.setEnabled(True)
145 else: 171 else:
146 self.ok_button.setEnabled(False) 172 self.ok_button.setEnabled(False)
147 173
148 def setContacts(self, selected=[]): 174 def refreshOptions(self, keep_selected=False):
149 """Fill the list with the connected contacts 175 """Fill the list with the connected contacts.
150 @param select: list of the contacts to select by default 176
151 """ 177 @param keep_selected (boolean): if True, keep the current selection
178 """
179 selection = self.contacts if keep_selected else []
152 self.contacts_list.clear() 180 self.contacts_list.clear()
153 contacts = self.host.contact_panel.getConnected(filter_muc=True) 181 contacts = self.host.contact_list.roster_entities_connected
154 self.contacts_list.setVisibleItemCount(10 if len(contacts) > 5 else 5) 182 self.contacts_list.setVisibleItemCount(10 if len(contacts) > 5 else 5)
155 self.contacts_list.addItem("") 183 self.contacts_list.addItem("")
156 for contact in contacts: 184 for contact in contacts:
157 if contact not in [room.bare for room in self.host.room_list]: 185 self.contacts_list.addItem(contact)
158 self.contacts_list.addItem(contact) 186 if selection:
159 self.contacts_list.setItemTextSelection(selected) 187 self.contacts_list.setItemTextSelection([unicode(contact) for contact in selection])
160
161 def getContacts(self):
162 return self.contacts_list.getSelectedValues(True)
163 188
164 189
165 class RoomAndContactsChooser(DialogBox): 190 class RoomAndContactsChooser(DialogBox):
166 """Select a room and some users to invite in""" 191 """Select a room and some users to invite in"""
167 192
196 221
197 self.setWidget(main_panel) 222 self.setWidget(main_panel)
198 self.setHTML(title) 223 self.setHTML(title)
199 self.show() 224 self.show()
200 225
201 # needed to update the contacts list when someone logged in/out 226 # FIXME: workaround for a pyjamas issue: calling hash on a class method always return a different value if that method is defined directly within the class (with the "def" keyword)
202 self.host.room_contacts_chooser = self 227 self.presenceListener = self.refreshContactList
203 228 # update the contacts list when someone logged in/out
204 def getRoom(self, asSuffix=False): 229 self.host.addListener('presence', self.presenceListener, [C.PROF_KEY_NONE])
205 room = self.room_panel.getRoom() 230
206 if asSuffix: 231 @property
207 return room if room == "" else ": %s" % room 232 def room(self):
208 else: 233 """Get the room that has been selected or entered by the user
209 return room 234
210 235 @return: jid.JID or None
211 def getContacts(self, asSuffix=False): 236 """
212 contacts = self.contact_panel.getContacts() 237 return self.room_panel.room
213 if asSuffix: 238
214 return "" if contacts == [] else ": %s" % ", ".join(contacts) 239 @property
215 else: 240 def contacts(self):
216 return contacts 241 """Return the selected contacts.
242
243 @return: list[jid.JID]
244 """
245 return self.contact_panel.contacts
217 246
218 def onStackChanged(self, sender, index, visible=None): 247 def onStackChanged(self, sender, index, visible=None):
219 if visible is None: 248 if visible is None:
220 visible = sender.getWidget(index).getVisible() 249 visible = sender.getWidget(index).getVisible()
221 if index == 0: 250 if index == 0:
222 sender.setStackText(0, self.title_room + ("" if visible else self.getRoom(True))) 251 suffix = "" if (visible or not self.room) else ": %s" % self.room
252 sender.setStackText(0, self.title_room + suffix)
223 elif index == 1: 253 elif index == 1:
224 sender.setStackText(1, self.title_invite + ("" if visible else self.getContacts(True))) 254 suffix = "" if (visible or not self.contacts) else ": %s" % ", ".join([unicode(contact) for contact in self.contacts])
225 255 sender.setStackText(1, self.title_invite + suffix)
226 def resetContacts(self): 256
227 """Called when someone log in/out to update the list""" 257 def refreshContactList(self, *args):
228 self.contact_panel.setContacts(self.getContacts()) 258 """Called when someone log in/out to update the list.
259
260 @param args: set by the event call but not used here
261 """
262 self.contact_panel.refreshOptions(keep_selected=True)
229 263
230 def onOK(self, sender): 264 def onOK(self, sender):
231 room_jid = self.getRoom() 265 room = self.room # pyjamas issue: you need to use an intermediate variable to access a property's method
232 if room_jid != "" and "@" not in room_jid: 266 if room and not room.is_valid():
233 Window.alert('You must enter a room jid in the form room@chat.%s' % self.host._defaultDomain) 267 Window.alert('You must enter a room jid in the form room@chat.%s' % self.host._defaultDomain)
234 return 268 return
235 self.hide() 269 self.hide()
236 self.callback(room_jid, self.getContacts()) 270 self.callback(room, self.contacts)
237 271
238 def onCancel(self, sender): 272 def onCancel(self, sender):
239 self.hide() 273 self.hide()
240 274
241 def hide(self): 275 def hide(self):
242 self.host.room_contacts_chooser = None 276 self.host.removeListener('presence', self.presenceListener)
243 DialogBox.hide(self, autoClosed=True) 277 DialogBox.hide(self, autoClosed=True)
244 278
245 279
246 class GenericConfirmDialog(DialogBox): 280 class GenericConfirmDialog(DialogBox):
247 281