f-spot r4300 - trunk/extensions/MergeDb



Author: sdelcroix
Date: Tue Aug 26 14:51:15 2008
New Revision: 4300
URL: http://svn.gnome.org/viewvc/f-spot?rev=4300&view=rev

Log:
Do real import over gvfs


Added:
   trunk/extensions/MergeDb/MergeDbDialog.cs
   trunk/extensions/MergeDb/PickFolderDialog.cs
Modified:
   trunk/extensions/MergeDb/Makefile.am
   trunk/extensions/MergeDb/MergeDb.addin.xml
   trunk/extensions/MergeDb/MergeDb.cs
   trunk/extensions/MergeDb/MergeDb.glade

Modified: trunk/extensions/MergeDb/Makefile.am
==============================================================================
--- trunk/extensions/MergeDb/Makefile.am	(original)
+++ trunk/extensions/MergeDb/Makefile.am	Tue Aug 26 14:51:15 2008
@@ -7,7 +7,9 @@
 PLUGIN_ASSEMBLY = $(PLUGIN_NAME).dll
 
 PLUGIN_SOURCES =			\
-	$(srcdir)/MergeDb.cs
+	$(srcdir)/MergeDb.cs		\
+	$(srcdir)/MergeDbDialog.cs	\
+	$(srcdir)/PickFolderDialog.cs
 
 REFS =					\
 	-r:Mono.Posix			\
@@ -29,7 +31,7 @@
 mpack: $(PLUGIN_ASSEMBLY)
 	mautil p $(PLUGIN_ASSEMBLY)
 
-$(PLUGIN_ASSEMBLY): $(PLUGIN_SOURCES) $(PLUGIN_MANIFEST)
+$(PLUGIN_ASSEMBLY): $(PLUGIN_SOURCES) $(PLUGIN_MANIFEST) $(PLUGIN_NAME).glade
 	$(CSC_LIB) -out:$@ $(PLUGIN_SOURCES) $(REFS) $(PKGS) $(ASSEMBLIES) $(RESOURCES)
 
 plugindir = $(pkglibdir)/extensions

Modified: trunk/extensions/MergeDb/MergeDb.addin.xml
==============================================================================
--- trunk/extensions/MergeDb/MergeDb.addin.xml	(original)
+++ trunk/extensions/MergeDb/MergeDb.addin.xml	Tue Aug 26 14:51:15 2008
@@ -1,12 +1,12 @@
 <Addin namespace="FSpot"
 	id="MergeDb"
 	name="MergeDb"
-	version="0.4.4.100"
+	version="0.4.4.103"
 	description="Merge another db back to the main one"
 	author="Stephane Delcroix"
 	url="http://f-spot.org/Extensions";
 	category="Tools"
-	defaultEnabled="false">
+	defaultEnabled="true">
 	<Dependencies>
 		<Addin id="Core" version="0.4.4.103"/>
 	</Dependencies>

Modified: trunk/extensions/MergeDb/MergeDb.cs
==============================================================================
--- trunk/extensions/MergeDb/MergeDb.cs	(original)
+++ trunk/extensions/MergeDb/MergeDb.cs	Tue Aug 26 14:51:15 2008
@@ -1,4 +1,5 @@
-/* FSpot.MergeDb.cs
+/* 
+ * FSpot.MergeDb.cs
  *
  * Author(s):
  *	Stephane Delcroix  <stephane delcroix org>
@@ -7,6 +8,7 @@
  */
 
 using System;
+using System.IO;
 using System.Collections.Generic;
 
 using Gtk;
@@ -22,20 +24,11 @@
 {
 	public class MergeDb : ICommand
 	{
-		[Glade.Widget] Gtk.Dialog mergedb_dialog;
-		[Glade.Widget] Gtk.Button apply_button;
-		[Glade.Widget] Gtk.Button cancel_button;
-		[Glade.Widget] Gtk.FileChooserButton db_filechooser;
-		[Glade.Widget] Gtk.RadioButton newrolls_radio;
-		[Glade.Widget] Gtk.RadioButton allrolls_radio;
-		[Glade.Widget] Gtk.RadioButton singleroll_radio;
-		[Glade.Widget] Gtk.ComboBox rolls_combo;
-
 
 		Db from_db;
 		Db to_db;
-		PhotoQuery query;
 		Roll [] new_rolls;
+		MergeDbDialog mdd;
 
 		Dictionary<uint, Tag> tag_map; //Key is a TagId from from_db, Value is a Tag from to_db
 		Dictionary<uint, uint> roll_map; 
@@ -46,7 +39,11 @@
 			from_db.ExceptionThrown += HandleDbException;
 			to_db = Core.Database;
 
-			ShowDialog ();
+			//ShowDialog ();
+			mdd = new MergeDbDialog (this);
+			mdd.FileChooser.FileSet += HandleFileSet;
+			mdd.Dialog.Response += HandleResponse;
+			mdd.ShowAll ();
 		}
 
 		void HandleDbException (Exception e)
@@ -54,47 +51,29 @@
 			Log.Exception (e);
 		}
 
-		public void ShowDialog () {
-			Glade.XML xml = new Glade.XML (null, "MergeDb.glade", "mergedb_dialog", "f-spot");
-			xml.Autoconnect (this);
-			mergedb_dialog.Modal = false;
-			mergedb_dialog.TransientFor = null;
-
-			db_filechooser.FileSet += HandleFileSet;
-
-			newrolls_radio.Toggled += HandleRollsChanged;
-			allrolls_radio.Toggled += HandleRollsChanged;
-			singleroll_radio.Toggled += HandleRollsChanged;
-
-			rolls_combo.Changed += HandleRollsChanged;
 
-			mergedb_dialog.Response += HandleResponse;
-			mergedb_dialog.ShowAll ();
+		internal Db FromDb {
+			get { return from_db; }
 		}
 
 		void HandleFileSet (object o, EventArgs e)
 		{
 			try {
-				from_db.Init (db_filechooser.Filename, true);
-				Log.Debug ("HE");
-				query = new PhotoQuery (from_db.Photos);
-			
-				CheckRolls ();
-				rolls_combo.Active = 0;
+				string tempfilename = System.IO.Path.GetTempFileName ();
+				System.IO.File.Copy (mdd.FileChooser.Filename, tempfilename, true);
 
-				newrolls_radio.Sensitive = true;
-				allrolls_radio.Sensitive = true;
-				singleroll_radio.Sensitive = true;
+				from_db.Init (tempfilename, true);
+			
+				FillRolls ();
+				mdd.Rolls = new_rolls;
 
-				apply_button.Sensitive = true;
+				mdd.SetSensitive ();
 
-				newrolls_radio.Active = true;
-				HandleRollsChanged (null, null);
 			} catch (Exception ex) {
 				string msg = Catalog.GetString ("Error opening the selected file");
 				string desc = String.Format (Catalog.GetString ("The file you selected is not a valid or supported database.\n\nReceived exception \"{0}\"."), ex.Message);
 				
-				HigMessageDialog md = new HigMessageDialog (mergedb_dialog, DialogFlags.DestroyWithParent, 
+				HigMessageDialog md = new HigMessageDialog (mdd.Dialog, DialogFlags.DestroyWithParent, 
 									    Gtk.MessageType.Error,
 									    ButtonsType.Ok, 
 									    msg,
@@ -106,7 +85,7 @@
 			}
 		}
 
-		void CheckRolls ()
+		void FillRolls ()
 		{
 			List<Roll> from_rolls = new List<Roll> (from_db.Rolls.GetRolls ());
 			Roll [] to_rolls = to_db.Rolls.GetRolls ();
@@ -116,35 +95,15 @@
 						from_rolls.Remove (fr);
 			new_rolls = from_rolls.ToArray ();
 
-			foreach (Roll r in from_rolls) {
-				uint numphotos = from_db.Rolls.PhotosInRoll (r);
-				DateTime date = r.Time.ToLocalTime ();
-				rolls_combo.AppendText (String.Format ("{0} ({1})", date.ToString("%dd %MMM, %HH:%mm"), numphotos));
-			}
-		}
-
-		void HandleRollsChanged (object o, EventArgs e)
-		{
-			rolls_combo.Sensitive = singleroll_radio.Active;
-
-			if (allrolls_radio.Active)
-				query.RollSet = null;
-
-			if (newrolls_radio.Active)
-				query.RollSet = new RollSet (new_rolls);
-
-			if (singleroll_radio.Active) {
-				Console.WriteLine (rolls_combo.Active);
-				query.RollSet = new RollSet (new_rolls [rolls_combo.Active]);
-			}
 		}
 
 		void HandleResponse (object obj, ResponseArgs args) {
 			if (args.ResponseId == ResponseType.Accept) {
-				Roll [] mergerolls = singleroll_radio.Active ? new Roll [] {new_rolls [rolls_combo.Active]} : new_rolls;
-				DoMerge (query, mergerolls, false);
+				PhotoQuery query = new PhotoQuery (from_db.Photos);
+				query.RollSet = mdd.ActiveRolls == null ? null : new RollSet (mdd.ActiveRolls);
+				DoMerge (query, mdd.ActiveRolls, mdd.Copy);
 			}
-			mergedb_dialog.Destroy ();
+			mdd.Dialog.Destroy ();
 		}
 
 
@@ -199,6 +158,8 @@
 
 		void CreateRolls (Roll [] rolls)
 		{
+			if (rolls == null)
+				rolls = from_db.Rolls.GetRolls ();
 			RollStore from_store = from_db.Rolls;
 			RollStore to_store = to_db.Rolls;
 
@@ -215,15 +176,87 @@
 				ImportPhoto (p, copy);
 		}
 
+		Dictionary<string, string> path_map = null;
+		Dictionary<string, string> PathMap {
+			get {
+				if (path_map == null)
+					path_map = new Dictionary<string, string> ();
+				return path_map;
+			}
+		}
+
 		void ImportPhoto (Photo photo, bool copy)
 		{
 			Log.WarningFormat ("Importing {0}", photo.Name);
 			PhotoStore from_store = from_db.Photos;
 			PhotoStore to_store = to_db.Photos;
 
+			string photo_path = photo.VersionUri (Photo.OriginalVersionId).AbsolutePath;
+
+			while (!System.IO.File.Exists (photo_path)) {
+				Log.Debug ("Not found, trying the mappings...");
+				foreach (string key in PathMap.Keys) {
+					string path = photo_path;
+					path = path.Replace (key, PathMap [key]);
+					Log.DebugFormat ("Replaced path {0}", path);
+					if (System.IO.File.Exists (path)) {
+						photo_path = path;
+						break;;
+					}
+				}
+
+				if (System.IO.File.Exists (photo_path)) {
+					Log.Debug ("Exists!!!");					
+					continue;
+				}
+
+				string [] parts = photo_path.Split (new char[] {'/'});
+				if (parts.Length > 6) {
+					string folder = String.Join ("/", parts, 0, parts.Length - 4);
+					PickFolderDialog pfd = new PickFolderDialog (mdd.Dialog, folder);
+					string new_folder = pfd.Run ();
+					pfd.Dialog.Destroy ();
+					if (new_folder == null) //Skip
+						return;
+					Log.DebugFormat ("{0} maps to {1}", folder, new_folder);
+					
+					PathMap[folder] = new_folder;
+					
+				} else
+					Console.WriteLine ("point me to the file");
+				Console.WriteLine ("FNF: {0}", photo_path);
+
+			}
+
+			string destination;
 			Gdk.Pixbuf pixbuf;
-			Photo newp = to_store.Create (photo.VersionUri (Photo.OriginalVersionId), roll_map [photo.RollId], out pixbuf);
+			Photo newp;
 
+			if (copy)
+				destination = FileImportBackend.ChooseLocation (photo_path, null);
+			else
+				destination = photo_path;
+
+			// Don't copy if we are already home
+			if (photo_path == destination) 
+				newp = to_store.Create (destination, roll_map [photo.RollId], out pixbuf);
+			else {
+				System.IO.File.Copy (photo_path, destination);
+
+				newp = to_store.Create (destination, photo_path, roll_map [photo.RollId], out pixbuf);
+				try {
+					File.SetAttributes (destination, File.GetAttributes (destination) & ~FileAttributes.ReadOnly);
+					DateTime create = File.GetCreationTime (photo_path);
+					File.SetCreationTime (destination, create);
+					DateTime mod = File.GetLastWriteTime (photo_path);
+					File.SetLastWriteTime (destination, mod);
+				} catch (IOException) {
+					// we don't want an exception here to be fatal.
+				}
+			} 
+
+			if (newp == null)
+				return;
 
 			foreach (Tag t in photo.Tags) {
 				Log.WarningFormat ("Tagging with {0}", t.Name);

Modified: trunk/extensions/MergeDb/MergeDb.glade
==============================================================================
--- trunk/extensions/MergeDb/MergeDb.glade	(original)
+++ trunk/extensions/MergeDb/MergeDb.glade	Tue Aug 26 14:51:15 2008
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
-<!--Generated with glade3 3.4.3 on Mon Aug 25 17:35:47 2008 -->
+<!--Generated with glade3 3.4.3 on Tue Aug 26 16:41:20 2008 -->
 <glade-interface>
   <widget class="GtkDialog" id="mergedb_dialog">
     <property name="border_width">5</property>
@@ -266,4 +266,87 @@
       </widget>
     </child>
   </widget>
+  <widget class="GtkDialog" id="pickfolder_dialog">
+    <property name="border_width">5</property>
+    <property name="window_position">GTK_WIN_POS_CENTER_ON_PARENT</property>
+    <property name="default_width">736</property>
+    <property name="default_height">575</property>
+    <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
+    <property name="has_separator">False</property>
+    <child internal-child="vbox">
+      <widget class="GtkVBox" id="pickfolder_vbox">
+        <property name="visible">True</property>
+        <property name="spacing">2</property>
+        <child>
+          <widget class="GtkVBox" id="vbox1">
+            <property name="visible">True</property>
+            <child>
+              <placeholder/>
+            </child>
+            <child>
+              <widget class="GtkLabel" id="pickfolder_label">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="ypad">12</property>
+                <property name="use_markup">True</property>
+              </widget>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+            <child>
+              <widget class="GtkFileChooserWidget" id="pickfolder_chooser">
+                <property name="visible">True</property>
+                <property name="use_preview_label">False</property>
+                <property name="show_hidden">True</property>
+                <property name="preview_widget_active">False</property>
+                <property name="action">GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER</property>
+                <property name="local_only">False</property>
+              </widget>
+              <packing>
+                <property name="position">2</property>
+              </packing>
+            </child>
+          </widget>
+          <packing>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child internal-child="action_area">
+          <widget class="GtkHButtonBox" id="dialog-action_area2">
+            <property name="visible">True</property>
+            <property name="layout_style">GTK_BUTTONBOX_END</property>
+            <child>
+              <widget class="GtkButton" id="button2">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="label" translatable="yes">Skip</property>
+                <property name="response_id">-1</property>
+              </widget>
+            </child>
+            <child>
+              <widget class="GtkButton" id="button1">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="label" translatable="yes">gtk-ok</property>
+                <property name="use_stock">True</property>
+                <property name="response_id">-6</property>
+              </widget>
+              <packing>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </widget>
+          <packing>
+            <property name="expand">False</property>
+            <property name="pack_type">GTK_PACK_END</property>
+          </packing>
+        </child>
+      </widget>
+    </child>
+  </widget>
 </glade-interface>

Added: trunk/extensions/MergeDb/MergeDbDialog.cs
==============================================================================
--- (empty file)
+++ trunk/extensions/MergeDb/MergeDbDialog.cs	Tue Aug 26 14:51:15 2008
@@ -0,0 +1,114 @@
+/* 
+ * FSpot.MergeDbDialog.cs
+ *
+ * Author(s):
+ *	Stephane Delcroix  <stephane delcroix org>
+ *
+ * This is free software. See COPYING for details
+ */
+
+using System;
+using FSpot;
+using FSpot.Query;
+
+namespace MergeDbExtension
+{
+	internal class MergeDbDialog
+	{
+		[Glade.Widget] Gtk.Dialog mergedb_dialog;
+		[Glade.Widget] Gtk.Button apply_button;
+		[Glade.Widget] Gtk.Button cancel_button;
+		[Glade.Widget] Gtk.FileChooserButton db_filechooser;
+		[Glade.Widget] Gtk.RadioButton newrolls_radio;
+		[Glade.Widget] Gtk.RadioButton allrolls_radio;
+		[Glade.Widget] Gtk.RadioButton singleroll_radio;
+		[Glade.Widget] Gtk.ComboBox rolls_combo;
+		[Glade.Widget] Gtk.RadioButton copy_radio;
+		[Glade.Widget] Gtk.RadioButton keep_radio;
+
+		MergeDb parent;
+
+		public event EventHandler FileSet;
+
+		public MergeDbDialog (MergeDb parent) {
+			this.parent = parent;
+
+			Glade.XML xml = new Glade.XML (null, "MergeDb.glade", "mergedb_dialog", "f-spot");
+			xml.Autoconnect (this);
+			mergedb_dialog.Modal = false;
+			mergedb_dialog.TransientFor = null;
+
+			db_filechooser.LocalOnly = false;
+			db_filechooser.FileSet += OnFileSet;
+
+			newrolls_radio.Toggled += HandleRollsChanged;
+			allrolls_radio.Toggled += HandleRollsChanged;
+			singleroll_radio.Toggled += HandleRollsChanged;
+		}
+
+		void HandleRollsChanged (object o, EventArgs e)
+		{
+			rolls_combo.Sensitive = singleroll_radio.Active;
+		}
+
+		public Gtk.FileChooserButton FileChooser {
+			get { return db_filechooser; }
+		}
+
+		Roll [] rolls;
+		public Roll [] Rolls {
+			get { return rolls; }
+			set { 
+				rolls = value; 
+				foreach (Roll r in rolls) {
+					uint numphotos = parent.FromDb.Rolls.PhotosInRoll (r);
+					DateTime date = r.Time.ToLocalTime ();
+					rolls_combo.AppendText (String.Format ("{0} ({1})", date.ToString("%dd %MMM, %HH:%mm"), numphotos));
+					rolls_combo.Active = 0;
+				}
+			}
+		}
+
+		public Roll [] ActiveRolls {
+			get {
+				if (allrolls_radio.Active)
+					return null;
+				if (newrolls_radio.Active)
+					return rolls;
+				else
+					return new Roll [] {rolls [rolls_combo.Active]};
+			}
+		}
+
+		public bool Copy {
+			get { return copy_radio.Active; }
+		}
+
+		public void OnFileSet (object o, EventArgs e)
+		{
+			if (FileSet != null)
+				FileSet (o, e);
+		}
+
+		public void SetSensitive ()
+		{
+			newrolls_radio.Sensitive = true;
+			allrolls_radio.Sensitive = true;
+			singleroll_radio.Sensitive = true;
+			apply_button.Sensitive = true;
+			copy_radio.Sensitive = true;	
+			keep_radio.Sensitive = true;	
+		}
+
+		public Gtk.Dialog Dialog {
+			get { return mergedb_dialog; }
+		}
+
+		public void ShowAll ()
+		{
+			mergedb_dialog.ShowAll ();
+		}
+	}
+}
+
+

Added: trunk/extensions/MergeDb/PickFolderDialog.cs
==============================================================================
--- (empty file)
+++ trunk/extensions/MergeDb/PickFolderDialog.cs	Tue Aug 26 14:51:15 2008
@@ -0,0 +1,53 @@
+/* 
+ * FSpot.PickFolderDialog.cs
+ *
+ * Author(s):
+ *	Stephane Delcroix  <stephane delcroix org>
+ *
+ * This is free software. See COPYING for details
+ */
+
+using System;
+using FSpot;
+using FSpot.Query;
+using Mono.Unix;
+
+namespace MergeDbExtension
+{
+	internal class PickFolderDialog
+	{
+		[Glade.Widget] Gtk.Dialog pickfolder_dialog;
+		[Glade.Widget] Gtk.FileChooserWidget pickfolder_chooser;
+		[Glade.Widget] Gtk.Label pickfolder_label;
+
+		public PickFolderDialog (Gtk.Dialog parent, string folder) 
+		{
+			Glade.XML xml = new Glade.XML (null, "MergeDb.glade", "pickfolder_dialog", "f-spot");
+			xml.Autoconnect (this);
+			Console.WriteLine ("new pickfolder");
+			pickfolder_dialog.Modal = false;
+			pickfolder_dialog.TransientFor = parent;
+
+			pickfolder_chooser.LocalOnly = false;
+
+			pickfolder_label.Text = String.Format (Catalog.GetString ("<big>The database refers to files contained in the <b>{0}</b> folder.\n Please select that folder so I can do the mapping.</big>"), folder);
+			pickfolder_label.UseMarkup = true;
+		}
+
+		public string Run ()
+		{
+			pickfolder_dialog.ShowAll ();
+			if (pickfolder_dialog.Run () == -6)
+				return pickfolder_chooser.Filename;
+			else
+				return null;
+		}
+
+		public Gtk.Dialog Dialog {
+			get { return pickfolder_dialog; }
+		}
+
+	}
+}
+
+



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