Primitivus: better popups handling:

- widget to remove can now be specified in removePopUp: if it is not the current one, it will be removed from queue
    - Primitivus.Alert method is a shortcut to display an Alert which will be removed on "OK" press
    - showDialog is now obsolete and removed, if generic dialog creation is needed, XMLUI can be used
This commit is contained in:
Goffi 2015-11-11 18:32:59 +01:00
commit 73139e59ba
4 changed files with 59 additions and 44 deletions

View file

@ -22,7 +22,7 @@ from sat.core import log as logging
log = logging.getLogger(__name__)
import urwid
from urwid_satext import sat_widgets
from urwid_satext.files_management import FileDialog
# from urwid_satext.files_management import FileDialog # FIXME
from sat_frontends.quick_frontend import quick_widgets
from sat_frontends.quick_frontend.quick_chat import QuickChat
from sat_frontends.quick_frontend import quick_games
@ -378,27 +378,29 @@ class Chat(PrimitivusWidget, QuickChat):
def onSendFileRequest(self, menu):
# TODO: move this to core with dynamic menus
dialog = FileDialog(ok_cb=self.onFileSelected, cancel_cb=self.host.removePopUp)
self.host.showPopUp(dialog, 80, 80)
pass
# dialog = FileDialog(ok_cb=self.onFileSelected, cancel_cb=self.host.removePopUp) # FIXME: to be removed
# self.host.showPopUp(dialog, 80, 80)
# MISC EVENTS #
def onFileSelected(self, filepath):
self.host.removePopUp()
try:
filepath = filepath.decode('utf-8') # FIXME: correctly manage unicode
except UnicodeError:
log.error("FIXME: filepath with unicode error are not managed yet")
self.host.showDialog(_(u"File has a unicode error in its name, it's not yet managed by SàT"), title=_("Can't send file"), type_="error")
return
# FIXME: check last_resource: what if self.target.resource exists ?
last_resource = self.host.bridge.getMainResource(unicode(self.target.bare), self.profile)
if last_resource:
full_jid = jid.JID("%s/%s" % (self.target.bare, last_resource))
else:
full_jid = self.target
progress_id = self.host.bridge.sendFile(full_jid, filepath, {}, self.profile)
self.host.addProgress(progress_id, filepath)
self.host.showDialog(_(u"You file request has been sent, we are waiting for your contact answer"), title=_("File request sent"))
# FIXME: to be removed
# def onFileSelected(self, filepath):
# self.host.removePopUp()
# try:
# filepath = filepath.decode('utf-8') # FIXME: correctly manage unicode
# except UnicodeError:
# log.error("FIXME: filepath with unicode error are not managed yet")
# self.host.showDialog(_(u"File has a unicode error in its name, it's not yet managed by SàT"), title=_("Can't send file"), type_="error")
# return
# # FIXME: check last_resource: what if self.target.resource exists ?
# last_resource = self.host.bridge.getMainResource(unicode(self.target.bare), self.profile)
# if last_resource:
# full_jid = jid.JID("%s/%s" % (self.target.bare, last_resource))
# else:
# full_jid = self.target
# progress_id = self.host.bridge.sendFile(full_jid, filepath, {}, self.profile)
# self.host.addProgress(progress_id, filepath)
# self.host.showDialog(_(u"You file request has been sent, we are waiting for your contact answer"), title=_("File request sent"))
def onDelete(self):
QuickChat.onDelete(self)

View file

@ -350,7 +350,7 @@ class PrimitivusApp(QuickApp, InputHistory):
config.applyConfig(self)
except Exception as e:
log.error(u"configuration error: {}".format(e))
popup = sat_widgets.Alert(_("Configuration Error"), _("Something went wrong while reading the configuration, please check :messages"), ok_cb=self.removePopUp)
popup = self.Alert(_(u"Configuration Error"), _(u"Something went wrong while reading the configuration, please check :messages"))
if self.options.profile:
self._early_popup = popup
else:
@ -479,13 +479,38 @@ class PrimitivusApp(QuickApp, InputHistory):
else:
del self._early_popup
def Alert(self, title, message):
"""Shortcut method to create an alert message
Alert will have an "OK" button, which remove it if pressed
@param title(unicode): title of the dialog
@param message(unicode): body of the dialog
@return (urwid_satext.Alert): the create Alert instance
"""
popup = sat_widgets.Alert(title, message)
popup.setCallback('ok', lambda dummy: self.removePopUp(popup))
self.showPopUp(popup)
return popup
def removePopUp(self, widget=None):
"Remove current pop-up, and if there is other in queue, show it"
"""Remove current pop-up, and if there is other in queue, show it
@param widget(None, urwid.Widget): if not None remove this popup from front or queue
"""
# TODO: refactor popup management in a cleaner way
if widget is not None:
if isinstance(self.loop.widget, urwid.Overlay):
current_popup = self.loop.widget.top_w
if not current_popup == widget:
self.notif_bar.removePopUp(widget)
return
self.loop.widget = self.main_widget
next_popup = self.notif_bar.getNextPopup()
if next_popup:
#we still have popup to show, we display it
self.showPopUp(next_popup)
else:
self.redraw()
def showPopUp(self, pop_up_widget, perc_width=40, perc_height=40, align='center', valign='middle'):
"Show a pop-up window if possible, else put it in queue"
@ -569,26 +594,12 @@ class PrimitivusApp(QuickApp, InputHistory):
answer_data = [data[1]] if data[1] else []
answer_cb(False, *answer_data)
def showDialog(self, message, title="", type_="info", answer_cb = None, answer_data = None):
if type_ == 'info':
popup = sat_widgets.Alert(unicode(title), unicode(message), ok_cb=answer_cb or self.removePopUp) #FIXME: remove unicode here when DBus Bridge will no return dbus.String anymore
elif type_ == 'error':
popup = sat_widgets.Alert(unicode(title), unicode(message), ok_cb=answer_cb or self.removePopUp) #FIXME: remove unicode here when DBus Bridge will no return dbus.String anymore
elif type_ == 'yes/no':
popup = sat_widgets.ConfirmDialog(unicode(message),
yes_cb=self._dialogOkCb, yes_value = (answer_cb, answer_data),
no_cb=self._dialogCancelCb, no_value = (answer_cb, answer_data))
else:
popup = sat_widgets.Alert(unicode(title), unicode(message), ok_cb=answer_cb or self.removePopUp) #FIXME: remove unicode here when DBus Bridge will no return dbus.String anymore
log.error(_('unmanaged dialog type: %s'), type_)
self.showPopUp(popup)
def dialogFailure(self, failure):
"""Show a failure that has been returned by an asynchronous bridge method.
@param failure (defer.Failure): Failure instance
"""
self.showPopUp(sat_widgets.Alert(failure.classname, failure.message, ok_cb=self.removePopUp))
self.Alert(failure.classname, failure.message)
def onNotification(self, notif_bar):
"""Called when a new notification has been received"""
@ -602,7 +613,7 @@ class PrimitivusApp(QuickApp, InputHistory):
self.main_widget.show('notif_bar')
def _actionManagerUnknownError(self):
self.showPopUp(sat_widgets.Alert(_("Error"), _(u"Unmanaged action"), ok_cb=self.removePopUp))
self.Alert(_("Error"), _(u"Unmanaged action"))
def askConfirmationHandler(self, confirmation_id, confirmation_type, data, profile):
answer_data = {}
@ -659,7 +670,7 @@ class PrimitivusApp(QuickApp, InputHistory):
ui.show('window')
elif type_ == "ERROR":
self.current_action_ids.remove(id)
self.showPopUp(sat_widgets.Alert(_("Error"), unicode(data["message"]), ok_cb=self.removePopUp)) #FIXME: remove unicode here when DBus Bridge will no return dbus.String anymore
self.Alert(_("Error"), unicode(data["message"])) #FIXME: remove unicode here when DBus Bridge will no return dbus.String anymore
elif type_ == "RESULT":
self.current_action_ids.remove(id)
if self.current_action_ids_cb.has_key(id):
@ -702,7 +713,7 @@ class PrimitivusApp(QuickApp, InputHistory):
ui.show()
def failure(error):
self.showPopUp(sat_widgets.Alert(_("Error"), _("Can't get parameters (%s)") % error, ok_cb=self.removePopUp))
self.Alert(_("Error"), _("Can't get parameters (%s)") % error)
self.bridge.getParamsUI(app=C.APP_NAME, profile_key=self.current_profile, callback=success, errback=failure)
def onExitRequest(self, menu):
@ -711,11 +722,12 @@ class PrimitivusApp(QuickApp, InputHistory):
def onJoinRoomRequest(self, menu):
"""User wants to join a MUC room"""
pop_up_widget = sat_widgets.InputDialog(_("Entering a MUC room"), _("Please enter MUC's JID"), default_txt=self.bridge.getDefaultMUC(), cancel_cb=self.removePopUp, ok_cb=self.onJoinRoom)
pop_up_widget = sat_widgets.InputDialog(_("Entering a MUC room"), _("Please enter MUC's JID"), default_txt=self.bridge.getDefaultMUC(), ok_cb=self.onJoinRoom)
pop_up_widget.setCallback('cancel', lambda dummy: self.removePopUp(pop_up_widget))
self.showPopUp(pop_up_widget)
def onAboutRequest(self, menu):
self.showPopUp(sat_widgets.Alert(_("About"), C.APP_NAME + " v" + self.bridge.getVersion(), ok_cb=self.removePopUp))
self.Alert(_("About"), C.APP_NAME + " v" + self.bridge.getVersion())
#MISC CALLBACKS#

View file

@ -328,7 +328,7 @@ class PrimitivusDialog(object):
self.host.showPopUp(self)
def _xmluiClose(self):
self.host.removePopUp()
self.host.removePopUp(self)
class PrimitivusMessageDialog(PrimitivusDialog, xmlui.MessageDialog, sat_widgets.Alert):
@ -455,7 +455,7 @@ class XMLUIPanel(xmlui.XMLUIPanel, PrimitivusWidget):
if self._dest == 'window':
self.host.removeWindow()
elif self._dest == 'popup':
self.host.removePopUp()
self.host.removePopUp(self)
else:
raise exceptions.InternalError("self._dest unknown, are you sure you have called XMLUI.show ?")

View file

@ -742,6 +742,7 @@ class QuickApp(object):
if data is None:
data = dict()
def action_cb(data):
if callback is None:
self.actionManager(data, profile=profile)