[shotwell] Unify mount point detection



commit 086cf121133a5ac1b0c74d662fc52a34e149dcff
Author: Jens Georg <mail jensge org>
Date:   Sat Jan 4 13:30:08 2020 +0100

    Unify mount point detection

 src/camera/CameraBranch.vala   |  2 +-
 src/camera/CameraTable.vala    | 28 +++++++++++++++++++++++++++-
 src/camera/ImportPage.vala     | 24 ++++--------------------
 src/library/LibraryWindow.vala |  8 +-------
 4 files changed, 33 insertions(+), 29 deletions(-)
---
diff --git a/src/camera/CameraBranch.vala b/src/camera/CameraBranch.vala
index 052f0933..0cb2c321 100644
--- a/src/camera/CameraBranch.vala
+++ b/src/camera/CameraBranch.vala
@@ -104,7 +104,7 @@ public class Camera.SidebarEntry : Sidebar.SimplePageEntry {
     }
     
     protected override Page create_page() {
-        return new ImportPage(camera.gcamera, uri, get_sidebar_name(), get_sidebar_icon());
+        return new ImportPage(camera, camera.gcamera, uri, get_sidebar_name(), get_sidebar_icon());
     }
     
     public string get_uri() {
diff --git a/src/camera/CameraTable.vala b/src/camera/CameraTable.vala
index 6f4427d3..46e5ccdc 100644
--- a/src/camera/CameraTable.vala
+++ b/src/camera/CameraTable.vala
@@ -12,12 +12,17 @@ public class DiscoveredCamera {
 
     private string port;
     private string camera_name;
+    private string[] mount_uris;
 
-    public DiscoveredCamera(string name, string port, GPhoto.PortInfo port_info, GPhoto.CameraAbilities 
camera_abilities) throws Error {
+    public DiscoveredCamera(string name, string port, GPhoto.PortInfo port_info, GPhoto.CameraAbilities 
camera_abilities) throws GPhotoError {
         this.port = port;
         this.camera_name = name;
         this.uri = "gphoto2://[%s]".printf(port);
 
+        this.mount_uris = new string[0];
+        this.mount_uris += this.uri;
+        this.mount_uris += "mtp://[%s]".printf(port);
+
         var res = GPhoto.Camera.create(out this.gcamera);
 
         if (res != GPhoto.Result.OK) {
@@ -50,6 +55,14 @@ public class DiscoveredCamera {
 #if HAVE_UDEV
             var client = new GUdev.Client(null);
             var device = client.query_by_device_file(path);
+
+
+            // Create alternative uris (used for unmount)
+            var serial = device.get_property("ID_SERIAL");
+            this.mount_uris += "gphoto2://%s".printf(serial);
+            this.mount_uris += "mtp://%s".printf(serial);
+
+            // Look-up alternative display names
             if (display_name == null) {
                 display_name = device.get_sysfs_attr("product");
             }
@@ -81,6 +94,19 @@ public class DiscoveredCamera {
             this.display_name = camera_name;
         }
     }
+
+    public Mount? get_mount() {
+        foreach (var uri in this.mount_uris) {
+            var f = File.new_for_uri(uri);
+            try {
+                var mount = f.find_enclosing_mount(null);
+                if (mount != null)
+                    return mount;
+            } catch (Error error) {}
+        }
+
+        return null;
+    }
 }
 
 public class CameraTable {
diff --git a/src/camera/ImportPage.vala b/src/camera/ImportPage.vala
index d6f5e604..50fd6ebc 100644
--- a/src/camera/ImportPage.vala
+++ b/src/camera/ImportPage.vala
@@ -683,6 +683,7 @@ public class ImportPage : CheckerboardPage {
     private Gtk.CheckButton hide_imported;
     private Gtk.ProgressBar progress_bar = new Gtk.ProgressBar();
     private GPhoto.Camera camera;
+    private DiscoveredCamera dcamera;
     private string uri;
     private bool busy = false;
     private bool refreshed = false;
@@ -707,8 +708,9 @@ public class ImportPage : CheckerboardPage {
         LIBRARY_ERROR
     }
     
-    public ImportPage(GPhoto.Camera camera, string uri, string? display_name = null, string? icon = null) {
+    public ImportPage(DiscoveredCamera dcamera, GPhoto.Camera camera, string uri, string? display_name = 
null, string? icon = null) {
         base(_("Camera"));
+        this.dcamera = dcamera;
         this.camera = camera;
         this.uri = uri;
         this.import_sources = new ImportSourceCollection("ImportSources for %s".printf(uri));
@@ -994,25 +996,7 @@ public class ImportPage : CheckerboardPage {
                 // if locked because it's mounted, offer to unmount
                 debug("Checking if %s is mounted…", uri);
 
-                File uri = File.new_for_uri(uri);
-
-                Mount mount = null;
-                try {
-                    mount = uri.find_enclosing_mount(null);
-                } catch (Error err) {
-                    // error means not mounted
-                }
-
-                // Could not find mount for gphoto2://, re-try with mtp://
-                // It seems some devices are mounted using MTP and not gphoto2 daemon
-                if (mount == null && this.uri.has_prefix("gphoto2")) {
-                    uri = File.new_for_uri("mtp" + this.uri.substring(7));
-                    try {
-                        mount = uri.find_enclosing_mount(null);
-                    } catch (Error err) {
-                        // error means not mounted
-                    }
-                }
+                var mount = dcamera.get_mount();
                 
                 if (mount != null) {
                     // it's mounted, offer to unmount for the user
diff --git a/src/library/LibraryWindow.vala b/src/library/LibraryWindow.vala
index 4ced992c..1ec6cbeb 100644
--- a/src/library/LibraryWindow.vala
+++ b/src/library/LibraryWindow.vala
@@ -978,15 +978,9 @@ public class LibraryWindow : AppWindow {
             return;
         
         ImportPage page = (ImportPage) entry.get_page();
-        File uri_file = File.new_for_uri(camera.uri);
         
         // find the VFS mount point
-        Mount mount = null;
-        try {
-            mount = uri_file.find_enclosing_mount(null);
-        } catch (Error err) {
-            // error means not mounted
-        }
+        var mount = camera.get_mount();
         
         // don't unmount mass storage cameras, as they are then unavailable to gPhoto
         if (mount != null && !camera.uri.has_prefix("file://")) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]