comparison src/browser/sat_browser/richtext.py @ 737:398b54bd97f0

browser_side: remove RichMessageEditor and RecipientManager which are not used since the removal of the unibox
author souliane <souliane@mailoo.org>
date Thu, 19 Nov 2015 12:08:58 +0100
parents fe3c2357a8c9
children caad07bdb659
comparison
equal deleted inserted replaced
736:fe3c2357a8c9 737:398b54bd97f0
340 _dialog = dialog.ConfirmDialog(confirm_cb, text="Do you really want to %s?" % ("cancel your changes" if self.update_msg else "cancel this message")) 340 _dialog = dialog.ConfirmDialog(confirm_cb, text="Do you really want to %s?" % ("cancel your changes" if self.update_msg else "cancel this message"))
341 _dialog.cancel_button.setText(_("No")) 341 _dialog.cancel_button.setText(_("No"))
342 _dialog.show() 342 _dialog.show()
343 343
344 344
345 class RichMessageEditor(RichTextEditor):
346 """Use the rich text editor for sending messages with extended addressing.
347 Recipient panels are on top and data may be synchronized from/to the unibox."""
348
349 @classmethod
350 def getOrCreate(cls, host, parent=None, callback=None):
351 """Get or create the message editor associated to that host.
352 Add it to parent if parent is not None, otherwise display it
353 in a popup dialog.
354 @param host: the host
355 @param parent: parent panel (or None to display in a popup).
356 @return: the RichTextEditor instance if parent is not None,
357 otherwise a popup DialogBox containing the RichTextEditor.
358 """
359 if not hasattr(host, 'richtext'):
360 modifiedCb = lambda content: True
361
362 def afterEditCb(content):
363 if hasattr(host.richtext, 'popup'):
364 host.richtext.popup.hide()
365 else:
366 host.richtext.setVisible(False)
367 callback()
368 options = ['no_title']
369 style = {'main': 'richMessageEditor', 'textarea': 'richMessageArea'}
370 host.richtext = RichMessageEditor(host, None, modifiedCb, afterEditCb, options, style)
371
372 def add(widget, parent):
373 if widget.getParent() is not None:
374 if widget.getParent() != parent:
375 widget.removeFromParent()
376 parent.add(widget)
377 else:
378 parent.add(widget)
379 widget.setVisible(True)
380 widget.initialized = False # fake a new creation
381 widget.edit(True)
382
383 if parent is None:
384 if not hasattr(host.richtext, 'popup'):
385 host.richtext.popup = DialogBox(autoHide=False, centered=True)
386 host.richtext.popup.setHTML("Compose your message")
387 host.richtext.popup.add(host.richtext)
388 add(host.richtext, host.richtext.popup)
389 host.richtext.popup.center()
390 else:
391 add(host.richtext, parent)
392 return host.richtext.popup if parent is None else host.richtext
393
394 def _prepareUI(self, y_offset=0):
395 """Prepare the UI to host recipients panel, toolbar, text area...
396 @param y_offset: Y offset to start from (extra rows on top)"""
397 self.recipient_offset = y_offset
398 self.recipient_spacer_offset = self.recipient_offset + len(composition.RECIPIENT_TYPES)
399 RichTextEditor._prepareUI(self, self.recipient_spacer_offset + 1)
400
401 def refresh(self, edit=None):
402 """Refresh the UI between edition/display mode
403 @param edit: set to True to display the edition mode"""
404 if edit is None:
405 edit = hasattr(self, 'textarea') and self.textarea.getVisible()
406 RichTextEditor.refresh(self, edit)
407
408 for widget in ['recipient', 'recipient_spacer']:
409 if hasattr(self, widget):
410 getattr(self, widget).setVisible(edit)
411
412 if not edit:
413 return
414
415 if not hasattr(self, 'recipient'):
416 # recipient types sub-panels are automatically added by the manager
417 self.recipient = RecipientManager(self, self.recipient_offset)
418 self.recipient_spacer = HTML('')
419 self.recipient_spacer.setStyleName('recipientSpacer')
420 self.getFlexCellFormatter().setColSpan(self.recipient_spacer_offset, 0, 2)
421 self.setWidget(self.recipient_spacer_offset, 0, self.recipient_spacer)
422
423 if not hasattr(self, 'sync_button'):
424 self.sync_button = Button("Back to quick box", lambda: self.edit(True, sync=True))
425 self.command.insert(self.sync_button, 1)
426
427 def syncToEditor(self):
428 """Synchronize from unibox."""
429 def setContent(target, data):
430 if hasattr(self, 'recipient'):
431 self.recipient.setContacts({"To": [target]} if target else {})
432 self.setContent({'text': data if data else '', 'syntax': ''})
433 self.textarea.setText(data if data else '')
434 data, target = self.host.uni_box.getTargetAndData() if self.host.uni_box else (None, None)
435 setContent(target, data)
436
437 def __syncToUniBox(self, recipients=None, emptyText=False):
438 """Synchronize to unibox if a maximum of one recipient is set.
439 @return True if the sync could be done, False otherwise"""
440 # FIXME: To be removed (unibox doesn't exist anymore)
441 if not self.host.uni_box:
442 return
443 setText = lambda: self.host.uni_box.setText("" if emptyText else self.getContent()['text'])
444 if not hasattr(self, 'recipient'):
445 setText()
446 return True
447 if recipients is None:
448 recipients = self.recipient.getItemsByKey()
449 target = ""
450 # we could eventually allow more in the future
451 allowed = 1
452 for key in recipients:
453 count = len(recipients[key])
454 if count == 0:
455 continue
456 allowed -= count
457 if allowed < 0:
458 return False
459 # TODO: change this if later more then one recipients are allowed
460 target = recipients[key][0]
461 setText()
462 if target == "":
463 return True
464 if target.startswith("@"):
465 _class = blog.MicroblogPanel
466 target = None if target == "@@" else target[1:]
467 else:
468 _class = chat.Chat
469 self.host.displayWidget(_class, target)
470 return True
471
472 def syncFromEditor(self, content):
473 """Synchronize to unibox and close the dialog afterward. Display
474 a message and leave the dialog open if the sync was not possible."""
475 if self.__syncToUniBox():
476 self._afterEditCb(content)
477 return
478 dialog.InfoDialog("Too many recipients",
479 "A message with more than one direct recipient (To)," +
480 " or with any special recipient (Cc or Bcc), could not be" +
481 " stored in the quick box.\n\nPlease finish your composing" +
482 " in the rich text editor, and send your message directly" +
483 " from here.", Width="400px").center()
484
485 def edit(self, edit=True, abort=False, sync=False):
486 if not edit and not abort and not sync: # force sending message even when the text has not been modified
487 if not self.__sendMessage(): # message has not been sent (missing information), do nothing
488 return
489 RichTextEditor.edit(self, edit, abort, sync)
490
491 def __sendMessage(self):
492 """Send the message."""
493 recipients = self.recipient.getItemsByKey()
494 targets = []
495 for addr in recipients:
496 for recipient in recipients[addr]:
497 if recipient.startswith("@"):
498 targets.append(("PUBLIC", None, addr) if recipient == "@@" else ("GROUP", recipient[1:], addr))
499 else:
500 targets.append(("chat", recipient, addr))
501 # check that we actually have a message target and data
502 content = self.getContent()
503 if content['text'] == "" or len(targets) == 0:
504 dialog.InfoDialog("Missing information",
505 "Some information are missing and the message hasn't been sent.", Width="400px").center()
506 return None
507 self.__syncToUniBox(recipients, emptyText=True)
508 extra = {'content_rich': content['text']}
509 if hasattr(self, 'title_panel'):
510 extra.update({'title': content['title']})
511 self.host.send(targets, content['text'], extra=extra)
512 return True
513
514
515 class RecipientManager(list_manager.ListManager):
516 """A manager for sub-panels to set the recipients for each recipient type."""
517
518 def __init__(self, parent, y_offset=0):
519 # TODO: be sure we also display empty groups and disconnected contacts + their groups
520 # store the full list of potential recipients (groups and contacts)
521 list_ = []
522 list_.append("@@")
523 list_.extend("@%s" % group for group in parent.host.contact_panel.getGroups())
524 list_.extend(contact for contact in parent.host.contact_list.roster_entities)
525 list_manager.ListManager.__init__(self, parent, composition.RECIPIENT_TYPES, list_, {'y': y_offset})
526
527 self.registerPopupMenuPanel(entries=composition.RECIPIENT_TYPES,
528 hide=lambda sender, key: self.__children[key]["panel"].isVisible(),
529 callback=self.setContactPanelVisible)
530
531
532 class EditTextArea(TextArea, KeyboardHandler): 345 class EditTextArea(TextArea, KeyboardHandler):
533 def __init__(self, _parent): 346 def __init__(self, _parent):
534 TextArea.__init__(self) 347 TextArea.__init__(self)
535 self._parent = _parent 348 self._parent = _parent
536 KeyboardHandler.__init__(self) 349 KeyboardHandler.__init__(self)