[f-spot] Fix MergeDb extension.



commit 5c7f8c15f4f25396ccc5805bcb0dd08ee82c147e
Author: Ruben Vermeersch <ruben savanne be>
Date:   Sat Jun 5 22:45:01 2010 +0200

    Fix MergeDb extension.

 extensions/Tools/MergeDb/MergeDb.cs |   47 ++++++++++++++++++++++++++++++++++-
 src/Import/ImportController.cs      |    2 -
 2 files changed, 46 insertions(+), 3 deletions(-)
---
diff --git a/extensions/Tools/MergeDb/MergeDb.cs b/extensions/Tools/MergeDb/MergeDb.cs
index 0f54f17..e76beb7 100644
--- a/extensions/Tools/MergeDb/MergeDb.cs
+++ b/extensions/Tools/MergeDb/MergeDb.cs
@@ -232,7 +232,7 @@ namespace MergeDbExtension
 			Photo newp;
 
 			if (copy)
-				destination = FileImportBackend.ChooseLocation (new SafeUri (photo_path)).AbsolutePath;
+				destination = FindImportDestination (new SafeUri (photo_path)).AbsolutePath;
 			else
 				destination = photo_path;
 
@@ -277,5 +277,50 @@ namespace MergeDbExtension
 
 			to_store.Commit (newp);
 		}
+
+        SafeUri FindImportDestination (SafeUri uri)
+        {
+            // Find a new unique location inside the photo folder
+            string name = uri.GetFilename ();
+            DateTime time;
+            using (FSpot.ImageFile img = FSpot.ImageFile.Create (uri)) {
+                time = img.Date;
+            }
+
+            var dest_uri = FSpot.Global.PhotoUri.Append (time.Year.ToString ())
+                                          .Append (String.Format ("{0:D2}", time.Month))
+                                          .Append (String.Format ("{0:D2}", time.Day));
+            EnsureDirectory (dest_uri);
+
+            // If the destination we'd like to use is the file itself return that
+            if (dest_uri.Append (name) == uri)
+                return uri;
+
+            // Find an unused name
+            int i = 1;
+            var dest = dest_uri.Append (name);
+            var file = GLib.FileFactory.NewForUri (dest);
+            while (file.Exists) {
+                var filename = uri.GetFilenameWithoutExtension ();
+                var extension = uri.GetExtension ();
+                dest = dest_uri.Append (String.Format ("{0}-{1}{2}", filename, i++, extension));
+                file = GLib.FileFactory.NewForUri (dest);
+            }
+
+            return dest;
+        }
+
+        void EnsureDirectory (SafeUri uri)
+        {
+            var parts = uri.AbsolutePath.Split('/');
+            SafeUri current = new SafeUri (uri.Scheme + ":///", true);
+            for (int i = 0; i < parts.Length; i++) {
+                current = current.Append (parts [i]);
+                var file = GLib.FileFactory.NewForUri (current);
+                if (!file.Exists) {
+                    file.MakeDirectory (null);
+                }
+            }
+        }
 	}
 }
diff --git a/src/Import/ImportController.cs b/src/Import/ImportController.cs
index 7e9a484..f36e05b 100644
--- a/src/Import/ImportController.cs
+++ b/src/Import/ImportController.cs
@@ -375,8 +375,6 @@ namespace FSpot.Import
                 current = current.Append (parts [i]);
                 var file = GLib.FileFactory.NewForUri (current);
                 if (!file.Exists) {
-                    created_directories.Push (current);
-                    Log.Debug ("Creating "+current);
                     file.MakeDirectory (null);
                 }
             }



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