# HG changeset patch # User souliane # Date 1384719367 -3600 # Node ID 9eb9c7d41bdc95c02e13d69248a374901d5ccdf3 # Parent 79970bf6af9330c802f583084cbbb036df3f7c9e browser_side: added generic method "send" in SatWebFrontend diff -r 79970bf6af93 -r 9eb9c7d41bdc browser_side/panels.py --- a/browser_side/panels.py Sun Nov 17 17:57:14 2013 +0100 +++ b/browser_side/panels.py Sun Nov 17 21:16:07 2013 +0100 @@ -230,17 +230,10 @@ # if keycode == KEY_ENTER and not self.visible: if keycode == KEY_ENTER: if _txt: - target_hook, _type, msg = target + target_hook, type_, msg = target if target_hook: parsed_txt, data = target_hook - if _type == "PUBLIC": - self.host.bridge.call("sendMblog", None, "PUBLIC", None, parsed_txt, {}) - elif _type == "GROUP": - self.host.bridge.call("sendMblog", None, "GROUP", data, parsed_txt, {}) - elif _type == "STATUS": - self.host.bridge.call('setStatus', None, parsed_txt) - else: - print "ERROR: Unknown target hook type" + self.host.send([(type_, data)], parsed_txt) else: # we send the message to the selected target self._selected_cache.onTextEntered(_txt) self.host._updateInputHistory(_txt) @@ -440,14 +433,15 @@ comments_node = self.selected_entry.comments if not comments_node: raise Exception("ERROR: comments node is empty") - self.host.bridge.call("sendMblogComment", None, comments_node, text, {}) + target = ("COMMENT", comments_node) elif not self._accepted_groups: # we are entering a public microblog - self.host.bridge.call("sendMblog", None, "PUBLIC", None, text, {}) + target = ("PUBLIC", None) else: # we are entering a microblog restricted to a group # FIXME: manage several groups - self.host.bridge.call("sendMblog", None, "GROUP", self._accepted_groups[0], text, {}) + target = ("GROUP", self._accepted_groups[0]) + self.host.send([target], text) def accept_all(self): return not self._accepted_groups # we accept every microblog only if we are not filtering by groups @@ -753,8 +747,7 @@ return ("ONE2ONE" if self.type == "one2one" else "GROUP", msg) def onTextEntered(self, text): - mess_type = "groupchat" if self.type == 'group' else "chat" - self.host.bridge.call('sendMessage', None, str(self.target), text, '', mess_type, {}) + self.host.send([("groupchat" if self.type == 'group' else "chat", str(self.target))], text) self.state_machine._onEvent("active") def onQuit(self): diff -r 79970bf6af93 -r 9eb9c7d41bdc libervia.py --- a/libervia.py Sun Nov 17 17:57:14 2013 +0100 +++ b/libervia.py Sun Nov 17 21:16:07 2013 +0100 @@ -708,6 +708,29 @@ def _newAlert(self, message, title, alert_type): dialog.InfoDialog(title, message).show() + def send(self, targets, text, rich=False): + """Send a message to any target type. + @param targets: list of couple (type, entities) with: + - type in ("PUBLIC", "GROUP", "COMMENT", "STATUS" , "groupchat" , "chat") + - entities could be a JID, a list groups, a node hash... depending the target + @param text: the message content + @param rich: set to True if the message is sent as a rich text message. + """ + # FIXME: too many magic strings, we should use constants instead + extra = {"rich": text} if rich else {} + for type_, entities in targets: + if type_ in ("PUBLIC", "GROUP"): + self.bridge.call("sendMblog", None, type_, entities if type_ == "GROUP" else None, text, extra) + elif type_ == "COMMENT": + self.bridge.call("sendMblogComment", None, entities, text, extra) + elif type_ == "STATUS": + self.bridge.call('setStatus', None, text) + elif type_ in ("groupchat", "chat"): + self.bridge.call('sendMessage', None, entities, text, '', type_, extra) + else: + print "ERROR: Unknown target type" + + if __name__ == '__main__': pyjd.setup("http://localhost:8080/libervia.html") app = SatWebFrontend()