[f-spot] Convert XmpTagsImporter to TagLib#.



commit a633a4ed98929dd2b1daf86e4677bf6bece33485
Author: Ruben Vermeersch <ruben savanne be>
Date:   Sat Jun 26 22:35:33 2010 +0200

    Convert XmpTagsImporter to TagLib#.
    
    Now named MetadataImporter.

 src/Imaging/JpegFile.cs        |    1 -
 src/Import/ImportController.cs |    9 +-
 src/Import/MetadataImporter.cs |  136 ++++++++++++++++++
 src/Makefile.am                |    2 +-
 src/MetadataStore.cs           |    1 -
 src/PhotoStore.cs              |    4 +-
 src/PhotoView.cs               |    1 -
 src/XmpTagsImporter.cs         |  304 ----------------------------------------
 8 files changed, 143 insertions(+), 315 deletions(-)
---
diff --git a/src/Imaging/JpegFile.cs b/src/Imaging/JpegFile.cs
index d93576f..097a4e4 100644
--- a/src/Imaging/JpegFile.cs
+++ b/src/Imaging/JpegFile.cs
@@ -1,6 +1,5 @@
 using System;
 using System.IO;
-using FSpot.Xmp;
 using FSpot.Utils;
 using Hyena;
 using TagLib;
diff --git a/src/Import/ImportController.cs b/src/Import/ImportController.cs
index 9faceab..f5f344a 100644
--- a/src/Import/ImportController.cs
+++ b/src/Import/ImportController.cs
@@ -1,6 +1,5 @@
 using Hyena;
 using FSpot.Utils;
-using FSpot.Xmp;
 using System;
 using System.Collections.Generic;
 using System.Threading;
@@ -204,7 +203,7 @@ namespace FSpot.Import
         PhotoStore store = App.Instance.Database.Photos;
         RollStore rolls = App.Instance.Database.Rolls;
         volatile bool photo_scan_running;
-        XmpTagsImporter xmp_importer;
+        MetadataImporter metadata_importer;
         volatile bool import_cancelled = false;
 
         void DoImport ()
@@ -218,7 +217,7 @@ namespace FSpot.Import
             created_directories = new Stack<SafeUri> ();
             imported_photos = new List<uint> ();
             copied_files = new List<SafeUri> ();
-            xmp_importer = new XmpTagsImporter (store, App.Instance.Database.Tags);
+            metadata_importer = new MetadataImporter ();
             CreatedRoll = rolls.Create ();
 
             EnsureDirectory (Global.PhotoUri);
@@ -288,7 +287,7 @@ namespace FSpot.Import
             }
 
             // Clean created tags
-            xmp_importer.Cancel();
+            metadata_importer.Cancel();
 
             // Remove created roll
 		    rolls.Remove (CreatedRoll);
@@ -324,7 +323,7 @@ namespace FSpot.Import
             }
 
             // Import XMP metadata
-            needs_commit |= xmp_importer.Import (photo, destination, item.DefaultVersion.Uri);
+            needs_commit |= metadata_importer.Import (photo, item);
 
             if (needs_commit) {
                 store.Commit (photo);
diff --git a/src/Import/MetadataImporter.cs b/src/Import/MetadataImporter.cs
new file mode 100644
index 0000000..a974bb4
--- /dev/null
+++ b/src/Import/MetadataImporter.cs
@@ -0,0 +1,136 @@
+using System;
+using Mono.Unix;
+using System.Collections.Generic;
+using FSpot.Utils;
+
+namespace FSpot.Import {
+    internal class MetadataImporter {
+        private TagStore tag_store;
+        private Stack<Tag> tags_created;
+
+        static private string LastImportIcon = "gtk-new";
+
+        private class TagInfo {
+            // This class contains the Root tag name, and its Icon name (if any)
+            string tag_name;
+            string icon_name;
+
+            public string TagName {
+                get { return tag_name; }
+            }
+
+            public string IconName {
+                get { return icon_name; }
+            }
+
+            public bool HasIcon {
+                get { return icon_name != null; }
+            }
+
+            public TagInfo (string t_name, string i_name)
+            {
+                tag_name = t_name;
+                icon_name = i_name;
+            }
+
+            public TagInfo (string t_name)
+            {
+                tag_name = t_name;
+                icon_name = null;
+            }
+        } // TagInfo
+
+        TagInfo li_root_tag; // This is the Last Import root tag
+
+        public MetadataImporter ()
+        {
+            this.tag_store = App.Instance.Database.Tags;
+            tags_created = new Stack<Tag> ();
+
+            li_root_tag = new TagInfo (Catalog.GetString ("Imported Tags"), LastImportIcon);
+        }
+
+        private Tag EnsureTag (TagInfo info, Category parent)
+        {
+            Tag tag = tag_store.GetTagByName (info.TagName);
+
+            if (tag != null)
+                return tag;
+
+            tag = tag_store.CreateCategory (parent,
+                    info.TagName,
+                    false);
+
+            if (info.HasIcon) {
+                tag.ThemeIconName = info.IconName;
+                tag_store.Commit(tag);
+            }
+
+            tags_created.Push (tag);
+            return tag;
+        }
+
+        private void AddTagToPhoto (Photo photo, string new_tag_name)
+        {
+            if (new_tag_name == null || new_tag_name.Length == 0)
+                return;
+
+            Tag parent = EnsureTag (li_root_tag, tag_store.RootCategory);
+            Tag tag = EnsureTag (new TagInfo (new_tag_name), parent as Category);
+
+            // Now we have the tag for this place, add the photo to it
+            photo.AddTag (tag);
+        }
+
+        public bool Import (Photo photo, IBrowsableItem importing_from)
+        {
+            var res = new GIOTagLibFileAbstraction () {
+                Uri = importing_from.DefaultVersion.Uri
+            };
+
+            var sidecar_uri = importing_from.DefaultVersion.Uri.ReplaceExtension (".xmp");
+            var sidecar_res = new GIOTagLibFileAbstraction () {
+                Uri = sidecar_uri
+            };
+
+            using (var metadata = TagLib.File.Create (res) as TagLib.Image.File) {
+                var file = GLib.FileFactory.NewForUri (sidecar_uri);
+                if (file.Exists) {
+                    metadata.ParseXmpSidecar (sidecar_res);
+                }
+
+                // Copy Rating
+                var rating = metadata.ImageTag.Rating;
+                if (rating.HasValue) {
+                    var rating_val = Math.Min (metadata.ImageTag.Rating.Value, 5);
+                    photo.Rating = Math.Max (0, rating_val);
+                }
+
+                // Copy Keywords
+                foreach (var keyword in metadata.ImageTag.Keywords) {
+                    AddTagToPhoto (photo, keyword);
+                }
+
+                // XXX: We might want to copy more data.
+            }
+            return true;
+        }
+
+        public void Cancel()
+        {
+            // User have cancelled the import.
+            // Remove all created tags
+            while (tags_created.Count > 0)
+                tag_store.Remove (tags_created.Pop());
+
+            // Clear the tags_created array
+            tags_created.Clear();
+        }
+
+        public void Finish()
+        {
+            // Clear the tags_created array, since we do not need it anymore.
+            tags_created.Clear();
+        }
+    }
+} // namespace
diff --git a/src/Makefile.am b/src/Makefile.am
index 7e84b6e..38ae9d8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -80,6 +80,7 @@ SOURCES = \
 	Import/ImportController.cs \
 	Import/ImportSource.cs \
 	Import/FileImportSource.cs \
+	Import/MetadataImporter.cs \
 	InfoOverlay.cs \
 	ItemAction.cs \
 	Imaging/DCRawFile.cs \
@@ -201,7 +202,6 @@ SOURCES = \
 	Widgets/TrayView.cs \
 	Widgets/ViewContext.cs \
 	Widgets/Wipe.cs \
-	XmpTagsImporter.cs \
 	main.cs \
 	Tests/UpdaterTests.cs
 
diff --git a/src/MetadataStore.cs b/src/MetadataStore.cs
index 494ad8d..78dba8d 100644
--- a/src/MetadataStore.cs
+++ b/src/MetadataStore.cs
@@ -1,7 +1,6 @@
 using SemWeb;
 using SemWeb.Util;
 using Mono.Unix;
-using FSpot.Xmp;
 using FSpot.Utils;
 using FSpot.Imaging;
 using FSpot.Imaging.Tiff;
diff --git a/src/PhotoStore.cs b/src/PhotoStore.cs
index 45a7003..d5c9eea 100644
--- a/src/PhotoStore.cs
+++ b/src/PhotoStore.cs
@@ -146,7 +146,7 @@ public class PhotoStore : DbStore<Photo> {
 		Photo photo;
 
 		long unix_time = DateTimeUtil.FromDateTime (item.Time);
-		string description = item.Description;
+		string description = item.Description ?? String.Empty;
 
 		uint id = (uint) Database.Execute (
 			new DbCommand (
@@ -155,7 +155,7 @@ public class PhotoStore : DbStore<Photo> {
 				"time", unix_time,
 				"base_uri", item.DefaultVersion.BaseUri.ToString (),
 				"filename", item.DefaultVersion.Filename,
-				"description", description ?? String.Empty,
+				"description", description,
 				"roll_id", roll_id,
 				"default_version_id", Photo.OriginalVersionId,
 				"rating", "0"
diff --git a/src/PhotoView.cs b/src/PhotoView.cs
index 46db068..caa93b1 100644
--- a/src/PhotoView.cs
+++ b/src/PhotoView.cs
@@ -18,7 +18,6 @@ using System.Collections.Generic;
 using System.Xml.Serialization;
 using Mono.Unix;
 
-using FSpot.Xmp;
 using FSpot.Widgets;
 using FSpot.Utils;
 using Hyena;



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