Skip to content

Commit

Permalink
feat(conversation_tab): add support for checking attachment formats
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AAClause committed Jan 28, 2025
1 parent 95d0597 commit 7bc3e46
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion basilisk/gui/conversation_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7bc3e46

Please sign in to comment.