[f-spot] Fixed bug 402849 - prune empty directories when deleting photos



commit 1bd991667ecf7cf3c2c78db70fd6f32c42299403
Author: Mike Wallick <mike wallick net>
Date:   Thu Jul 22 09:03:01 2010 -0500

    Fixed bug 402849 - prune empty directories when deleting photos

 src/Core/Photo.cs |   50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 47 insertions(+), 3 deletions(-)
---
diff --git a/src/Core/Photo.cs b/src/Core/Photo.cs
index 7b240b7..1ff8d83 100644
--- a/src/Core/Photo.cs
+++ b/src/Core/Photo.cs
@@ -271,22 +271,32 @@ namespace FSpot
 			if (version_id == OriginalVersionId && !remove_original)
 				throw new Exception ("Cannot delete original version");
 	
-			SafeUri uri =  VersionUri (version_id);
+			SafeUri uri = VersionUri (version_id);
 	
 			if (!keep_file) {
 				GLib.File file = GLib.FileFactory.NewForUri (uri);
-				if (file.Exists) 
+				if (file.Exists)
 					try {
 						file.Trash (null);
 					} catch (GLib.GException) {
 						Log.Debug ("Unable to Trash, trying to Delete");
 						file.Delete ();
-					}	
+					}
 				try {
 					XdgThumbnailSpec.RemoveThumbnail (uri);
 				} catch {
 					//ignore an error here we don't really care.
 				}
+
+				// do we really need to check if the parent is a directory?
+				// i.e. is file.Parent always a directory if the file instance is
+				// an actual file?
+				GLib.File directory = file.Parent;
+				GLib.FileType file_type = directory.QueryFileType (GLib.FileQueryInfoFlags.None, null);
+
+				if (directory.Exists && file_type == GLib.FileType.Directory)
+					DeleteEmptyDirectory (directory);
+
 			}
 			versions.Remove (version_id);
 
@@ -300,6 +310,40 @@ namespace FSpot
 			}
 		}
 
+		private void DeleteEmptyDirectory (GLib.File directory)
+		{
+			// if the directory we're dealing with is not in the
+			// F-Spot photos directory, don't delete anything,
+			// even if it is empty
+			string photo_uri = SafeUri.UriToFilename (FSpot.Global.PhotoUri.ToString ());
+			bool path_matched = directory.Path.IndexOf (photo_uri) > -1;
+
+			if (directory.Path.Equals (photo_uri) || !path_matched)
+				return;
+
+			if (DirectoryIsEmpty (directory)) {
+				try {
+					Log.DebugFormat ("Removing empty directory: {0}", directory.Path);
+					directory.Trash (null);
+				} catch (GLib.GException) {
+					Log.DebugFormat ("Unable to Trash, trying to Delete: {0}", directory.Path);
+					directory.Delete();
+				}
+				// check to see if the parent is empty
+				DeleteEmptyDirectory (directory.Parent);
+			}
+		}
+
+		private bool DirectoryIsEmpty (GLib.File directory)
+		{
+			uint count = 0;
+			GLib.FileEnumerator list = directory.EnumerateChildren ("standard::name",
+				GLib.FileQueryInfoFlags.None, null);
+			foreach (var item in list)
+				count++;
+			return count == 0;
+		}
+
 		public uint CreateVersion (string name, uint base_version_id, bool create)
 		{
 			return CreateVersion (name, null, base_version_id, create, false);



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