comparison sat/stdui/ui_contact_list.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
29 29
30 def __init__(self, host): 30 def __init__(self, host):
31 self.host = host 31 self.host = host
32 self.__add_id = host.registerCallback(self._addContact, with_data=True) 32 self.__add_id = host.registerCallback(self._addContact, with_data=True)
33 self.__update_id = host.registerCallback(self._updateContact, with_data=True) 33 self.__update_id = host.registerCallback(self._updateContact, with_data=True)
34 self.__confirm_delete_id = host.registerCallback(self._getConfirmRemoveXMLUI, with_data=True) 34 self.__confirm_delete_id = host.registerCallback(
35 35 self._getConfirmRemoveXMLUI, with_data=True
36 host.importMenu((D_("Contacts"), D_("Add contact")), self._getAddDialogXMLUI, security_limit=2, help_string=D_("Add contact")) 36 )
37 host.importMenu((D_("Contacts"), D_("Update contact")), self._getUpdateDialogXMLUI, security_limit=2, help_string=D_("Update contact")) 37
38 host.importMenu((D_("Contacts"), D_("Remove contact")), self._getRemoveDialogXMLUI, security_limit=2, help_string=D_("Remove contact")) 38 host.importMenu(
39 (D_("Contacts"), D_("Add contact")),
40 self._getAddDialogXMLUI,
41 security_limit=2,
42 help_string=D_("Add contact"),
43 )
44 host.importMenu(
45 (D_("Contacts"), D_("Update contact")),
46 self._getUpdateDialogXMLUI,
47 security_limit=2,
48 help_string=D_("Update contact"),
49 )
50 host.importMenu(
51 (D_("Contacts"), D_("Remove contact")),
52 self._getRemoveDialogXMLUI,
53 security_limit=2,
54 help_string=D_("Remove contact"),
55 )
39 56
40 # FIXME: a plugin should not be used here, and current profile's jid host would be better than installation wise host 57 # FIXME: a plugin should not be used here, and current profile's jid host would be better than installation wise host
41 if 'MISC-ACCOUNT' in self.host.plugins: 58 if "MISC-ACCOUNT" in self.host.plugins:
42 self.default_host = self.host.plugins['MISC-ACCOUNT'].getNewAccountDomain() 59 self.default_host = self.host.plugins["MISC-ACCOUNT"].getNewAccountDomain()
43 else: 60 else:
44 self.default_host = 'example.net' 61 self.default_host = "example.net"
45 62
46 def getContacts(self, profile): 63 def getContacts(self, profile):
47 """Return a sorted list of the contacts for that profile 64 """Return a sorted list of the contacts for that profile
48 65
49 @param profile: %(doc_profile)s 66 @param profile: %(doc_profile)s
94 @param data (dict) 111 @param data (dict)
95 @return list[Element] 112 @return list[Element]
96 """ 113 """
97 elts = [] 114 elts = []
98 for key in data: 115 for key in data:
99 key_elt = Element('jid') 116 key_elt = Element("jid")
100 key_elt.setAttribute('name', key) 117 key_elt.setAttribute("name", key)
101 for value in data[key]: 118 for value in data[key]:
102 value_elt = Element('group') 119 value_elt = Element("group")
103 value_elt.setAttribute('name', value) 120 value_elt.setAttribute("name", value)
104 key_elt.childNodes.append(value_elt) 121 key_elt.childNodes.append(value_elt)
105 elts.append(key_elt) 122 elts.append(key_elt)
106 return elts 123 return elts
107 124
108 def getDialogXMLUI(self, options, data, profile): 125 def getDialogXMLUI(self, options, data, profile):
114 - 'contact_text': deferred localized string 131 - 'contact_text': deferred localized string
115 @param data (dict) 132 @param data (dict)
116 @param profile: %(doc_profile)s 133 @param profile: %(doc_profile)s
117 @return dict 134 @return dict
118 """ 135 """
119 form_ui = xml_tools.XMLUI("form", title=options['title'], submit_id=options['id']) 136 form_ui = xml_tools.XMLUI("form", title=options["title"], submit_id=options["id"])
120 if 'message' in data: 137 if "message" in data:
121 form_ui.addText(data['message']) 138 form_ui.addText(data["message"])
122 form_ui.addDivider('dash') 139 form_ui.addDivider("dash")
123 140
124 form_ui.addText(options['contact_text']) 141 form_ui.addText(options["contact_text"])
125 if options['id'] == self.__add_id: 142 if options["id"] == self.__add_id:
126 contact = data.get(xml_tools.formEscape('contact_jid'), '@%s' % self.default_host) 143 contact = data.get(
127 form_ui.addString('contact_jid', value=contact) 144 xml_tools.formEscape("contact_jid"), "@%s" % self.default_host
128 elif options['id'] == self.__update_id: 145 )
146 form_ui.addString("contact_jid", value=contact)
147 elif options["id"] == self.__update_id:
129 contacts = self.getContacts(profile) 148 contacts = self.getContacts(profile)
130 list_ = form_ui.addList('contact_jid', options=contacts, selected=contacts[0]) 149 list_ = form_ui.addList("contact_jid", options=contacts, selected=contacts[0])
131 elts = self._data2elts(self.getGroupsOfAllContacts(profile)) 150 elts = self._data2elts(self.getGroupsOfAllContacts(profile))
132 list_.setInternalCallback('groups_of_contact', fields=['contact_jid', 'groups_list'], data_elts=elts) 151 list_.setInternalCallback(
133 152 "groups_of_contact", fields=["contact_jid", "groups_list"], data_elts=elts
134 form_ui.addDivider('blank') 153 )
154
155 form_ui.addDivider("blank")
135 156
136 form_ui.addText(_("Select in which groups your contact is:")) 157 form_ui.addText(_("Select in which groups your contact is:"))
137 selected_groups = [] 158 selected_groups = []
138 if 'selected_groups' in data: 159 if "selected_groups" in data:
139 selected_groups = data['selected_groups'] 160 selected_groups = data["selected_groups"]
140 elif options['id'] == self.__update_id: 161 elif options["id"] == self.__update_id:
141 try: 162 try:
142 selected_groups = self.getGroupsOfContact(contacts[0], profile) 163 selected_groups = self.getGroupsOfContact(contacts[0], profile)
143 except IndexError: 164 except IndexError:
144 pass 165 pass
145 groups = self.getGroups(selected_groups, profile) 166 groups = self.getGroups(selected_groups, profile)
146 form_ui.addList('groups_list', options=groups, selected=selected_groups, styles=['multi']) 167 form_ui.addList(
147 168 "groups_list", options=groups, selected=selected_groups, styles=["multi"]
148 adv_list = form_ui.changeContainer("advanced_list", columns=3, selectable='no') 169 )
170
171 adv_list = form_ui.changeContainer("advanced_list", columns=3, selectable="no")
149 form_ui.addLabel(D_("Add group")) 172 form_ui.addLabel(D_("Add group"))
150 form_ui.addString("add_group") 173 form_ui.addString("add_group")
151 button = form_ui.addButton('', value=D_('Add')) 174 button = form_ui.addButton("", value=D_("Add"))
152 button.setInternalCallback('move', fields=['add_group', 'groups_list']) 175 button.setInternalCallback("move", fields=["add_group", "groups_list"])
153 adv_list.end() 176 adv_list.end()
154 177
155 form_ui.addDivider('blank') 178 form_ui.addDivider("blank")
156 return {'xmlui': form_ui.toXml()} 179 return {"xmlui": form_ui.toXml()}
157 180
158 def _getAddDialogXMLUI(self, data, profile): 181 def _getAddDialogXMLUI(self, data, profile):
159 """Get the dialog for adding contact 182 """Get the dialog for adding contact
160 183
161 @param data (dict) 184 @param data (dict)
162 @param profile: %(doc_profile)s 185 @param profile: %(doc_profile)s
163 @return dict 186 @return dict
164 """ 187 """
165 options = {'id': self.__add_id, 188 options = {
166 'title': D_('Add contact'), 189 "id": self.__add_id,
167 'contact_text': D_("New contact identifier (JID):"), 190 "title": D_("Add contact"),
168 } 191 "contact_text": D_("New contact identifier (JID):"),
192 }
169 return self.getDialogXMLUI(options, {}, profile) 193 return self.getDialogXMLUI(options, {}, profile)
170 194
171 def _getUpdateDialogXMLUI(self, data, profile): 195 def _getUpdateDialogXMLUI(self, data, profile):
172 """Get the dialog for updating contact 196 """Get the dialog for updating contact
173 197
174 @param data (dict) 198 @param data (dict)
175 @param profile: %(doc_profile)s 199 @param profile: %(doc_profile)s
176 @return dict 200 @return dict
177 """ 201 """
178 if not self.getContacts(profile): 202 if not self.getContacts(profile):
179 _dialog = xml_tools.XMLUI('popup', title=D_('Nothing to update')) 203 _dialog = xml_tools.XMLUI("popup", title=D_("Nothing to update"))
180 _dialog.addText(_('Your contact list is empty.')) 204 _dialog.addText(_("Your contact list is empty."))
181 return {'xmlui': _dialog.toXml()} 205 return {"xmlui": _dialog.toXml()}
182 206
183 options = {'id': self.__update_id, 207 options = {
184 'title': D_('Update contact'), 208 "id": self.__update_id,
185 'contact_text': D_("Which contact do you want to update?"), 209 "title": D_("Update contact"),
186 } 210 "contact_text": D_("Which contact do you want to update?"),
211 }
187 return self.getDialogXMLUI(options, {}, profile) 212 return self.getDialogXMLUI(options, {}, profile)
188 213
189 def _getRemoveDialogXMLUI(self, data, profile): 214 def _getRemoveDialogXMLUI(self, data, profile):
190 """Get the dialog for removing contact 215 """Get the dialog for removing contact
191 216
192 @param data (dict) 217 @param data (dict)
193 @param profile: %(doc_profile)s 218 @param profile: %(doc_profile)s
194 @return dict 219 @return dict
195 """ 220 """
196 if not self.getContacts(profile): 221 if not self.getContacts(profile):
197 _dialog = xml_tools.XMLUI('popup', title=D_('Nothing to delete')) 222 _dialog = xml_tools.XMLUI("popup", title=D_("Nothing to delete"))
198 _dialog.addText(_('Your contact list is empty.')) 223 _dialog.addText(_("Your contact list is empty."))
199 return {'xmlui': _dialog.toXml()} 224 return {"xmlui": _dialog.toXml()}
200 225
201 form_ui = xml_tools.XMLUI("form", title=D_('Who do you want to remove from your contacts?'), submit_id=self.__confirm_delete_id) 226 form_ui = xml_tools.XMLUI(
202 form_ui.addList('contact_jid', options=self.getContacts(profile)) 227 "form",
203 return {'xmlui': form_ui.toXml()} 228 title=D_("Who do you want to remove from your contacts?"),
229 submit_id=self.__confirm_delete_id,
230 )
231 form_ui.addList("contact_jid", options=self.getContacts(profile))
232 return {"xmlui": form_ui.toXml()}
204 233
205 def _getConfirmRemoveXMLUI(self, data, profile): 234 def _getConfirmRemoveXMLUI(self, data, profile):
206 """Get the confirmation dialog for removing contact 235 """Get the confirmation dialog for removing contact
207 236
208 @param data (dict) 237 @param data (dict)
209 @param profile: %(doc_profile)s 238 @param profile: %(doc_profile)s
210 @return dict 239 @return dict
211 """ 240 """
212 if C.bool(data.get('cancelled', 'false')): 241 if C.bool(data.get("cancelled", "false")):
213 return {} 242 return {}
214 contact = data[xml_tools.formEscape('contact_jid')] 243 contact = data[xml_tools.formEscape("contact_jid")]
244
215 def delete_cb(data, profile): 245 def delete_cb(data, profile):
216 if not C.bool(data.get('cancelled', 'false')): 246 if not C.bool(data.get("cancelled", "false")):
217 self._deleteContact(jid.JID(contact), profile) 247 self._deleteContact(jid.JID(contact), profile)
218 return {} 248 return {}
249
219 delete_id = self.host.registerCallback(delete_cb, with_data=True, one_shot=True) 250 delete_id = self.host.registerCallback(delete_cb, with_data=True, one_shot=True)
220 form_ui = xml_tools.XMLUI("form", title=D_("Delete contact"), submit_id=delete_id) 251 form_ui = xml_tools.XMLUI("form", title=D_("Delete contact"), submit_id=delete_id)
221 form_ui.addText(D_("Are you sure you want to remove %s from your contact list?") % contact) 252 form_ui.addText(
222 return {'xmlui': form_ui.toXml()} 253 D_("Are you sure you want to remove %s from your contact list?") % contact
254 )
255 return {"xmlui": form_ui.toXml()}
223 256
224 def _addContact(self, data, profile): 257 def _addContact(self, data, profile):
225 """Add the selected contact 258 """Add the selected contact
226 259
227 @param data (dict) 260 @param data (dict)
228 @param profile: %(doc_profile)s 261 @param profile: %(doc_profile)s
229 @return dict 262 @return dict
230 """ 263 """
231 if C.bool(data.get('cancelled', 'false')): 264 if C.bool(data.get("cancelled", "false")):
232 return {} 265 return {}
233 contact_jid_s = data[xml_tools.formEscape('contact_jid')] 266 contact_jid_s = data[xml_tools.formEscape("contact_jid")]
234 try: 267 try:
235 contact_jid = jid.JID(contact_jid_s) 268 contact_jid = jid.JID(contact_jid_s)
236 except (RuntimeError, jid.InvalidFormat, AttributeError): 269 except (RuntimeError, jid.InvalidFormat, AttributeError):
237 # TODO: replace '\t' by a constant (see tools.xmlui.XMLUI.onFormSubmitted) 270 # TODO: replace '\t' by a constant (see tools.xmlui.XMLUI.onFormSubmitted)
238 data['selected_groups'] = data[xml_tools.formEscape('groups_list')].split('\t') 271 data["selected_groups"] = data[xml_tools.formEscape("groups_list")].split(
239 options = {'id': self.__add_id, 272 "\t"
240 'title': D_('Add contact'), 273 )
241 'contact_text': D_('Please enter a valid JID (like "contact@%s"):') % self.default_host, 274 options = {
242 } 275 "id": self.__add_id,
276 "title": D_("Add contact"),
277 "contact_text": D_('Please enter a valid JID (like "contact@%s"):')
278 % self.default_host,
279 }
243 return self.getDialogXMLUI(options, data, profile) 280 return self.getDialogXMLUI(options, data, profile)
244 self.host.addContact(contact_jid, profile_key=profile) 281 self.host.addContact(contact_jid, profile_key=profile)
245 return self._updateContact(data, profile) # after adding, updating 282 return self._updateContact(data, profile) # after adding, updating
246 283
247 def _updateContact(self, data, profile): 284 def _updateContact(self, data, profile):
249 286
250 @param data (dict) 287 @param data (dict)
251 @param profile: %(doc_profile)s 288 @param profile: %(doc_profile)s
252 @return dict 289 @return dict
253 """ 290 """
254 if C.bool(data.get('cancelled', 'false')): 291 if C.bool(data.get("cancelled", "false")):
255 return {} 292 return {}
256 contact_jid = jid.JID(data[xml_tools.formEscape('contact_jid')]) 293 contact_jid = jid.JID(data[xml_tools.formEscape("contact_jid")])
257 # TODO: replace '\t' by a constant (see tools.xmlui.XMLUI.onFormSubmitted) 294 # TODO: replace '\t' by a constant (see tools.xmlui.XMLUI.onFormSubmitted)
258 groups = data[xml_tools.formEscape('groups_list')].split('\t') 295 groups = data[xml_tools.formEscape("groups_list")].split("\t")
259 self.host.updateContact(contact_jid, name='', groups=groups, profile_key=profile) 296 self.host.updateContact(contact_jid, name="", groups=groups, profile_key=profile)
260 return {} 297 return {}
261 298
262 def _deleteContact(self, contact_jid, profile): 299 def _deleteContact(self, contact_jid, profile):
263 """Delete the selected contact 300 """Delete the selected contact
264 301