[ease] [general] Don't include unused media in saved documents



commit f6b468b0babd400e16fb1b9765ab8548277043f3
Author: Nate Stedman <natesm gmail com>
Date:   Sat Aug 7 20:14:41 2010 -0400

    [general] Don't include unused media in saved documents
    
    Saving was previous a "dumb" archive - just archive
    everything in the temp directory. When media files
    are "deleted" from the Document, they need to stick
    around in the temp directory, in case the user reverts
    the deletion but has moved the original file elsewhere.
    
    However, undo isn't saved into .ease files, so there's
    no need to include those extra media files. This commit
    excludes any files that are not currently used
    somewhere in the document.

 ease-core/ease-archiver.vala      |   19 +++++++++++++++++--
 ease-core/ease-document.vala      |   36 +++++++++++++++++++++++++++++++++++-
 ease-core/ease-element.vala       |   11 +++++++++++
 ease-core/ease-media-element.vala |    8 ++++++++
 ease-core/ease-shape-element.vala |   10 ++++++++++
 5 files changed, 81 insertions(+), 3 deletions(-)
---
diff --git a/ease-core/ease-archiver.vala b/ease-core/ease-archiver.vala
index e16e4b3..dace243 100644
--- a/ease-core/ease-archiver.vala
+++ b/ease-core/ease-archiver.vala
@@ -23,6 +23,7 @@ internal class Ease.Archiver : GLib.Object
 	private unowned Thread thread;
 	private bool async = true;
 	private int total_size = 0;
+	private Gee.LinkedList<string> include_files;
 	
 	private static GLib.List<Archiver> archivers = new GLib.List<Archiver>();
 	
@@ -34,15 +35,21 @@ internal class Ease.Archiver : GLib.Object
 	 */
 	private const int ASYNC_SIZE = 1024 * 1024 * 5;
 	
-	internal Archiver(string temp, string fname, Dialogs.Progress dlog)
+	internal Archiver(string temp, string fname, Gee.LinkedList<string> files,
+	                  Dialogs.Progress dlog)
 	{
 		temp_path = temp;
 		filename = fname;
 		dialog = dlog;
+		include_files = files;
 		archivers.append(this);
 		
 		// this is a little redundant, probably not a huge perf hit though
 		recursive_directory(temp_path, null, (path, full_path) => {
+			// if we're not going to archive that file, don't include its size
+			if (!files.contains(path)) return;
+			
+			// add the file's size to the total
 			Posix.Stat st;
 			Posix.stat(full_path, out st);
 			total_size += (int)st.st_size;
@@ -56,8 +63,11 @@ internal class Ease.Archiver : GLib.Object
 			return;
 		}
 		
+		// show the dialog
 		dialog.set_label(LABEL_TEXT.printf(filename));
 		dialog.show();
+		
+		// archive in a thread
 		thread = Thread.create(archive_real, true);
 	}
 	
@@ -92,6 +102,9 @@ internal class Ease.Archiver : GLib.Object
 		
 		// add files
 		recursive_directory(temp_path, null, (path, full_path) => {
+			// skip files we aren't including
+			if (!include_files.contains(path)) return;
+		
 			// create an archive entry for the file
 			var entry = new Archive.Entry();
 			entry.set_pathname(path);
@@ -149,17 +162,19 @@ namespace Ease
 	 * @param temp_path The path of the temporary directory.
 	 * @param filename The filename of the archive to save to.
 	 * @param title The title of the progress dialog.
+	 * @param files The files to include in the archive.
 	 * @param win The window to display a progress dialog modal for.
 	 */
 	internal static void archive(string temp_path,
 		                         string filename,
 		                         string title,
+		                         Gee.LinkedList<string> files,
 		                         Gtk.Window? win) throws Error
 	{
 		// create a progress dialog
 		var dialog = new Dialogs.Progress(title, false, 1, win);
 	
 		// archive away!
-		var arc = new Archiver(temp_path, filename, dialog);
+		var arc = new Archiver(temp_path, filename, files, dialog);
 	}
 }
diff --git a/ease-core/ease-document.vala b/ease-core/ease-document.vala
index 12d9306..04483c8 100644
--- a/ease-core/ease-document.vala
+++ b/ease-core/ease-document.vala
@@ -196,6 +196,7 @@ public class Ease.Document : GLib.Object, UndoSource
 	
 	public void to_json(Gtk.Window? window) throws GLib.Error
 	{
+		// create the json base
 		var root = new Json.Node(Json.NodeType.OBJECT);
 		var obj = new Json.Object();
 		
@@ -222,8 +223,41 @@ public class Ease.Document : GLib.Object, UndoSource
 		generator.pretty = true;
 		generator.to_file(Path.build_filename(path, JSON_FILE));
 		
+		// find the files that we're going to include
+		var files = new Gee.LinkedList<string>();
+		
+		// include the document file
+		files.add(JSON_FILE);
+		
+		// include all theme files
+		recursive_directory(Path.build_filename(path, THEME_PATH), null,
+		                    (path, full_path) => {
+			files.add(Path.build_filename(THEME_PATH, path));
+		});
+		
+		// find the files used by slides
+		Slide slide;
+		string[] claimed;
+		foreach (var itr in slides)
+		{
+			slides.get(itr, COL_SLIDE, out slide);
+			
+			// add the slide's background image if needed
+			if (slide.background.image.filename != null)
+			{
+				files.add(slide.background.image.filename);
+			}
+			
+			// add media claimed by each Element
+			foreach (var element in slide)
+			{
+				claimed = element.claim_media();
+				foreach (var str in claimed) files.add(str);
+			}
+		}
+		
 		// archive
-		archive(path, filename, _("Saving Document"), window);
+		archive(path, filename, _("Saving Document"), files, window);
 	}
 	
 	/**
diff --git a/ease-core/ease-element.vala b/ease-core/ease-element.vala
index eed4bd3..f22a633 100644
--- a/ease-core/ease-element.vala
+++ b/ease-core/ease-element.vala
@@ -142,6 +142,17 @@ public abstract class Ease.Element : GLib.Object, UndoSource
 	}
 	
 	/**
+	 * Allows a subclass of Element to "claim" media files. Media files that
+	 * are not claimed will not be included when the { link Document} is saved.
+	 * Subclasses with media should override this method and return an
+	 * appropriate array. The "short" (not absolute) path must be used.
+	 */
+	public virtual string[] claim_media()
+	{
+		return {};
+	}
+	
+	/**
 	 * Requests that the presentation be advanced a slide.
 	 */
 	internal void request_advance()
diff --git a/ease-core/ease-media-element.vala b/ease-core/ease-media-element.vala
index d13b21e..9e765be 100644
--- a/ease-core/ease-media-element.vala
+++ b/ease-core/ease-media-element.vala
@@ -47,6 +47,14 @@ public abstract class Ease.MediaElement : Element
 	}
 	
 	/**
+	 * Claims this MediaElement's media file.
+	 */
+	public override string[] claim_media()
+	{
+		return { filename };
+	}
+	
+	/**
 	 * The path to a media file.
 	 */
 	public string filename { get; set; }
diff --git a/ease-core/ease-shape-element.vala b/ease-core/ease-shape-element.vala
index 152d711..d6c7c82 100644
--- a/ease-core/ease-shape-element.vala
+++ b/ease-core/ease-shape-element.vala
@@ -70,6 +70,16 @@ public class Ease.ShapeElement : CairoElement
 	}
 	
 	/**
+	 * Claims this ShapeElement's background image, if needed.
+	 */
+	public override string[] claim_media()
+	{
+		if (background.image.filename != null)
+			return { background.image.filename };
+		return {};
+	}
+	
+	/**
 	 * Renders (or doesn't, it isn't supported yet) this ShapeElement as HTML.
 	 * When implemented, this should be done in CairoElement probably, so it
 	 * can be generic to anything else Cairo-based.



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