diff --git a/nomadnet/Conversation.py b/nomadnet/Conversation.py index 35b8cf5..086da2e 100644 --- a/nomadnet/Conversation.py +++ b/nomadnet/Conversation.py @@ -227,7 +227,7 @@ class Conversation: RNS.log("Destination is not known, cannot create LXMF Message.", RNS.LOG_VERBOSE) return False - def paper_output(self, content="", title=""): + def paper_output(self, content="", title="", mode="print_qr"): if self.send_destination: try: dest = self.send_destination @@ -235,18 +235,45 @@ class Conversation: desired_method = LXMF.LXMessage.PAPER lxm = LXMF.LXMessage(dest, source, content, title=title, desired_method=desired_method) - qr_code = lxm.as_qr() - qr_tmp_path = self.app.tmpfilespath+"/"+str(RNS.hexrep(lxm.hash, delimit=False)) - qr_code.save(qr_tmp_path) - print_result = self.app.print_file(qr_tmp_path) - os.unlink(qr_tmp_path) - - if print_result: + if mode == "print_qr": + qr_code = lxm.as_qr() + qr_tmp_path = self.app.tmpfilespath+"/"+str(RNS.hexrep(lxm.hash, delimit=False)) + qr_code.save(qr_tmp_path) + + print_result = self.app.print_file(qr_tmp_path) + os.unlink(qr_tmp_path) + + if print_result: + message_path = Conversation.ingest(lxm, self.app, originator=True) + self.messages.append(ConversationMessage(message_path)) + + return print_result + + elif mode == "save_qr": + qr_code = lxm.as_qr() + qr_save_path = self.app.downloads_path+"/LXM_"+str(RNS.hexrep(lxm.hash, delimit=False)+".png") + qr_code.save(qr_save_path) message_path = Conversation.ingest(lxm, self.app, originator=True) self.messages.append(ConversationMessage(message_path)) + return qr_save_path - return print_result + elif mode == "save_uri": + lxm_uri = lxm.as_uri() + uri_save_path = self.app.downloads_path+"/LXM_"+str(RNS.hexrep(lxm.hash, delimit=False)+".txt") + with open(uri_save_path, "wb") as f: + f.write(lxm_uri.encode("utf-8")) + + # TODO: Remove after LXMF 0.3.5 #### + lxm.determine_transport_encryption() + #################################### + + message_path = Conversation.ingest(lxm, self.app, originator=True) + self.messages.append(ConversationMessage(message_path)) + return uri_save_path + + elif mode == "return_uri": + return lxm.as_uri() except Exception as e: RNS.log("An error occurred while generating paper message, the contained exception was: "+str(e), RNS.LOG_ERROR) diff --git a/nomadnet/ui/textui/Conversations.py b/nomadnet/ui/textui/Conversations.py index 3929bd1..ea52568 100644 --- a/nomadnet/ui/textui/Conversations.py +++ b/nomadnet/ui/textui/Conversations.py @@ -974,7 +974,27 @@ class ConversationWidget(urwid.WidgetWrap): else: pass - def paper_message(self): + def paper_message_saved(self, path): + g = self.app.ui.glyphs + def dismiss_dialog(sender): + self.dialog_open = False + self.conversation_changed(None) + + dialog = DialogLineBox( + urwid.Pile([ + urwid.Text("The paper message was saved to:\n\n"+str(path)+"\n", align="center"), + urwid.Columns([("weight", 0.6, urwid.Text("")), ("weight", 0.4, urwid.Button("OK", on_press=dismiss_dialog))]) + ]), title=g["page"] + ) + dialog.delegate = self + bottom = self.messagelist + + overlay = urwid.Overlay(dialog, bottom, align="center", width=60, valign="middle", height="pack", left=2, right=2) + + self.frame.contents["body"] = (overlay, self.frame.options()) + self.frame.set_focus("body") + + def print_paper_message_qr(self): content = self.content_editor.get_edit_text() title = self.title_editor.get_edit_text() if not content == "": @@ -983,6 +1003,67 @@ class ConversationWidget(urwid.WidgetWrap): else: self.paper_message_failed() + def save_paper_message_qr(self): + content = self.content_editor.get_edit_text() + title = self.title_editor.get_edit_text() + if not content == "": + output_result = self.conversation.paper_output(content, title, mode="save_qr") + if output_result != False: + self.clear_editor() + self.paper_message_saved(output_result) + else: + self.paper_message_failed() + + def save_paper_message_uri(self): + content = self.content_editor.get_edit_text() + title = self.title_editor.get_edit_text() + if not content == "": + output_result = self.conversation.paper_output(content, title, mode="save_uri") + if output_result != False: + self.clear_editor() + self.paper_message_saved(output_result) + else: + self.paper_message_failed() + + def paper_message(self): + def dismiss_dialog(sender): + self.dialog_open = False + self.conversation_changed(None) + + def print_qr(sender): + dismiss_dialog(self) + self.print_paper_message_qr() + + def save_qr(sender): + dismiss_dialog(self) + self.save_paper_message_qr() + + def save_uri(sender): + dismiss_dialog(self) + self.save_paper_message_uri() + + dialog = DialogLineBox( + urwid.Pile([ + urwid.Text("Select the desired paper message output method.\nSaved files will be written to:\n\n"+str(self.app.downloads_path)+"\n", align="center"), + urwid.Columns([ + ("weight", 0.5, urwid.Button("Print QR", on_press=print_qr)), + ("weight", 0.1, urwid.Text("")), + ("weight", 0.5, urwid.Button("Save QR", on_press=save_qr)), + ("weight", 0.1, urwid.Text("")), + ("weight", 0.5, urwid.Button("Save URI", on_press=save_uri)), + ("weight", 0.1, urwid.Text("")), + ("weight", 0.5, urwid.Button("Cancel", on_press=dismiss_dialog)) + ]) + ]), title="Create Paper Message" + ) + dialog.delegate = self + bottom = self.messagelist + + overlay = urwid.Overlay(dialog, bottom, align="center", width=60, valign="middle", height="pack", left=2, right=2) + + self.frame.contents["body"] = (overlay, self.frame.options()) + self.frame.set_focus("body") + def paper_message_failed(self): def dismiss_dialog(sender): self.dialog_open = False