From 7bc3e46cc32b6f6e7dfdd5121beabd5af1186c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9-Abush=20Clause?= Date: Sun, 26 Jan 2025 16:40:29 +0100 Subject: [PATCH] feat(conversation_tab): add support for checking attachment formats Enhanced the 'add_attachments' function to check if the attachment format is supported by the current provider. If the format is not supported, a message box is displayed to inform the user about the unsupported format. This prevents adding unsupported attachment formats and enhances user feedback. --- basilisk/gui/conversation_tab.py | 47 +++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/basilisk/gui/conversation_tab.py b/basilisk/gui/conversation_tab.py index 4e868459..e9340c61 100644 --- a/basilisk/gui/conversation_tab.py +++ b/basilisk/gui/conversation_tab.py @@ -542,12 +542,26 @@ def refresh_attachments_list(self): self.attachments_list.EnsureVisible(i) def add_attachments(self, paths: list[str | AttachmentFile | ImageFile]): - log.debug(f"Adding images: {paths}") + log.debug(f"Adding attachments: {paths}") for path in paths: if isinstance(path, (AttachmentFile, ImageFile)): self.attachment_files.append(path) else: mime_type = get_mime_type(path) + supported_attachment_formats = ( + self.current_engine.supported_attachment_formats + ) + if mime_type not in supported_attachment_formats: + wx.MessageBox( + # Translators: This message is displayed when there are no supported attachment formats. + _( + "This attachment format is not supported by the current provider. Source:" + ) + + f"\n{path}", + _("Error"), + wx.OK | wx.ICON_ERROR, + ) + return if mime_type.startswith("image/"): file = ImageFile(location=path) else: @@ -1174,10 +1188,41 @@ def get_completion_args(self) -> dict[str, Any] | None: "stream": new_block.stream, } + def _check_attachments_valid(self) -> bool: + supported_attachment_formats = ( + self.current_engine.supported_attachment_formats + ) + for attachment in self.attachment_files: + if attachment.mime_type not in supported_attachment_formats: + self.attachment_files.remove(attachment) + wx.MessageBox( + # Translators: This message is displayed when an attachment format is not supported. + _( + "This attachment format is not supported by the current provider. Source:" + ) + + f"\n{attachment.location}", + _("Error"), + wx.OK | wx.ICON_ERROR, + ) + return False + if not attachment.location.exists(): + self.attachment_files.remove(attachment) + wx.MessageBox( + # Translators: This message is displayed when an attachment file does not exist. + _("The attachment file does not exist: %s") + % attachment.location, + _("Error"), + wx.OK | wx.ICON_ERROR, + ) + return False + return True + @ensure_no_task_running def on_submit(self, event: wx.CommandEvent): if not self.submit_btn.IsEnabled(): return + if not self._check_attachments_valid(): + return if not self.prompt.GetValue() and not self.attachment_files: self.prompt.SetFocus() return