From ac51bbf822da409b353364165e42e6b8e54578e3 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Sun, 22 Mar 2020 22:15:47 +0800 Subject: [PATCH] Try rename pdf file with web page title after download finish. --- core/browser.py | 11 +++++++++-- core/buffer.py | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/browser.py b/core/browser.py index d29a466..4319b24 100644 --- a/core/browser.py +++ b/core/browser.py @@ -459,7 +459,14 @@ class BrowserBuffer(Buffer): def notify_print_message(self, file_path, success): if success: - self.message_to_emacs.emit("Successfully saved current webpage as '{}'.".format(file_path)) + # Try to rename pdf file with title. + # Use host name if title include invalid file char. + title_path = os.path.join(os.path.expanduser(self.emacs_var_dict["eaf-browser-download-path"]), "{}.pdf".format(self.title)) + try: + os.rename(file_path, title_path) + self.message_to_emacs.emit("Successfully saved current webpage as '{}'.".format(title_path)) + except Exception: + self.message_to_emacs.emit("Successfully saved current webpage as '{}'.".format(file_path)) else: self.message_to_emacs.emit("Failed to save current webpage as '{}'.".format(file_path)) @@ -621,7 +628,7 @@ class BrowserBuffer(Buffer): parsed = urlparse(self.url) qd = parse_qs(parsed.query, keep_blank_values=True) pdf_path = os.path.join(os.path.expanduser(self.emacs_var_dict["eaf-browser-download-path"]), "{}.pdf".format(parsed.netloc)) - self.message_to_emacs.emit("Saving to " + pdf_path + "...") + self.message_to_emacs.emit("Saving as pdf...") self.buffer_widget.web_page.printToPdf(pdf_path) def cancel_input_message(self, result_tag): diff --git a/core/buffer.py b/core/buffer.py index d2fc16b..56f81d7 100755 --- a/core/buffer.py +++ b/core/buffer.py @@ -174,6 +174,7 @@ class Buffer(QGraphicsScene): self.background_color = background_color self.setBackgroundBrush(QBrush(self.background_color)) self.is_dark_mode = is_dark_mode + self.title = "" self.buffer_widget = None @@ -221,6 +222,7 @@ class Buffer(QGraphicsScene): self.buffer_widget.deleteLater() def change_title(self, title): + self.title = title self.update_details.emit(self.buffer_id, title, self.url) def request_close_buffer(self):