From f1bbb065ec63053b1e0dd0c78ba11376cbab451d Mon Sep 17 00:00:00 2001 From: Marvin W Date: Mon, 6 Oct 2025 20:12:19 +0200 Subject: Files: Use GFile instead of uri/path strings In some situations (like VFS or other abstractions), the file reference can have an invalid or unusabe path. Passing the GFile instead and opening it directly increases probability we can actually access it. --- .../conversation_content_view/file_image_widget.vala | 18 ++++++++++++++---- main/src/ui/conversation_content_view/file_widget.vala | 9 +++++++-- main/src/ui/conversation_view_controller.vala | 7 +++++-- main/src/view_model/preferences_dialog.vala | 4 ++-- .../account_preferences_subpage.vala | 3 +-- 5 files changed, 29 insertions(+), 12 deletions(-) (limited to 'main') diff --git a/main/src/ui/conversation_content_view/file_image_widget.vala b/main/src/ui/conversation_content_view/file_image_widget.vala index 6efe2d58..f39dd30a 100644 --- a/main/src/ui/conversation_content_view/file_image_widget.vala +++ b/main/src/ui/conversation_content_view/file_image_widget.vala @@ -154,10 +154,20 @@ public class FileImageWidget : Widget { public async void load_from_file(File file, string file_name) throws GLib.Error { FixedRatioPicture image = new FixedRatioPicture() { min_width=100, min_height=100, max_width=600, max_height=300 }; - // Work-around because Gtk.Picture does not apply the orientation itself - Gdk.Pixbuf? pixbuf = new Pixbuf.from_file(file.get_path()); - pixbuf = pixbuf.apply_embedded_orientation(); - image.paintable = Texture.for_pixbuf(pixbuf); + FileInputStream file_stream = null; + try { + file_stream = file.read(); + // Work-around because Gtk.Picture does not apply the orientation itself + Gdk.Pixbuf? pixbuf = new Pixbuf.from_stream(file_stream); + pixbuf = pixbuf.apply_embedded_orientation(); + image.paintable = Texture.for_pixbuf(pixbuf); + } finally { + try { + if (file_stream != null) file_stream.close(); + } catch (Error e) { + // Ignore + } + } stack.add_child(image); stack.set_visible_child(image); diff --git a/main/src/ui/conversation_content_view/file_widget.vala b/main/src/ui/conversation_content_view/file_widget.vala index f031db21..0e3cee03 100644 --- a/main/src/ui/conversation_content_view/file_widget.vala +++ b/main/src/ui/conversation_content_view/file_widget.vala @@ -169,8 +169,13 @@ public class FileWidgetController : Object { save_dialog.set_current_name(file_transfer.file_name); save_dialog.response.connect(() => { - try{ - GLib.File.new_for_uri(file_transfer.get_file().get_uri()).copy(save_dialog.get_file(), GLib.FileCopyFlags.OVERWRITE, null); + File? target_file = save_dialog.get_file(); + if (target_file == null) { + warning("No file returned from save dialog."); + return; + } + try { + file_transfer.get_file().copy(save_dialog.get_file(), GLib.FileCopyFlags.OVERWRITE, null); } catch (Error err) { warning("Failed copy file %s - %s", file_transfer.get_file().get_uri(), err.message); } diff --git a/main/src/ui/conversation_view_controller.vala b/main/src/ui/conversation_view_controller.vala index 1662ae56..2e11e1e9 100644 --- a/main/src/ui/conversation_view_controller.vala +++ b/main/src/ui/conversation_view_controller.vala @@ -203,7 +203,7 @@ public class ConversationViewController : Object { FileChooserNative chooser = new FileChooserNative(_("Select file"), view.get_root() as Gtk.Window, FileChooserAction.OPEN, _("Select"), _("Cancel")); chooser.response.connect((response) => { if (response == ResponseType.ACCEPT) { - open_send_file_overlay(File.new_for_path(chooser.get_file().get_path())); + open_send_file_overlay(chooser.get_file()); } }); chooser.show(); @@ -213,7 +213,10 @@ public class ConversationViewController : Object { FileInfo file_info; try { file_info = file.query_info("*", FileQueryInfoFlags.NONE); - } catch (Error e) { return; } + } catch (Error e) { + warning("Failed querying info for file %s", file.get_path()); + return; + } FileSendOverlay overlay = new FileSendOverlay(file, file_info); overlay.send_file.connect(() => send_file(file)); diff --git a/main/src/view_model/preferences_dialog.vala b/main/src/view_model/preferences_dialog.vala index c09c650a..af00494d 100644 --- a/main/src/view_model/preferences_dialog.vala +++ b/main/src/view_model/preferences_dialog.vala @@ -66,8 +66,8 @@ public class Dino.Ui.ViewModel.PreferencesDialog : Object { update(); } - public void set_avatar_uri(Account account, string uri) { - stream_interactor.get_module(AvatarManager.IDENTITY).publish(account, uri); + public void set_avatar_file(Account account, File file) { + stream_interactor.get_module(AvatarManager.IDENTITY).publish(account, file); } public void remove_avatar(Account account) { diff --git a/main/src/windows/preferences_window/account_preferences_subpage.vala b/main/src/windows/preferences_window/account_preferences_subpage.vala index 7a17fbda..660a9aeb 100644 --- a/main/src/windows/preferences_window/account_preferences_subpage.vala +++ b/main/src/windows/preferences_window/account_preferences_subpage.vala @@ -144,8 +144,7 @@ public class Dino.Ui.AccountPreferencesSubpage : Adw.NavigationPage { chooser.add_filter(filter); chooser.response.connect(() => { - string uri = chooser.get_file().get_path(); - model.set_avatar_uri(account, uri); + model.set_avatar_file(account, chooser.get_file()); }); chooser.show(); -- cgit v1.3