[ease/inspector] Refactor Document from Theme code to a constructor, copy media from themes.



commit 7fdd813fbe69996017b5ce8285bfcd5058cddc4d
Author: Nate Stedman <natesm gmail com>
Date:   Mon Jun 7 20:36:59 2010 -0400

    Refactor Document from Theme code to a constructor, copy media from themes.

 src/Document.vala      |   30 +++++++++++++++++++++++++++++-
 src/SlideSet.vala      |   17 +++++++++++++++++
 src/WelcomeWindow.vala |   34 +++++++++++++++-------------------
 3 files changed, 61 insertions(+), 20 deletions(-)
---
diff --git a/src/Document.vala b/src/Document.vala
index 3f21276..f043a47 100644
--- a/src/Document.vala
+++ b/src/Document.vala
@@ -23,6 +23,8 @@
  */
 public class Ease.Document : SlideSet
 {
+	private const string DEFAULT_SLIDE = "Standard";
+
 	/**
 	 * The { link Theme} linked to this Document.
 	 */
@@ -44,7 +46,7 @@ public class Ease.Document : SlideSet
 	public float aspect { get { return (float)width / (float)height; } }
 
 	/**
-	 * Default constructor, used for new documents.
+	 * Default constructor, creates an empty Document.
 	 * 
 	 * Creates a new, empty document with no slides. Used for creating new
 	 * documents (which can then add a default slide).
@@ -52,6 +54,32 @@ public class Ease.Document : SlideSet
 	public Document() { }
 	
 	/**
+	 * Theme constructor, used for new documents.
+	 *
+	 * @param doc_theme The { link Theme} for this Document.
+	 * @param w The width of the new Document.
+	 * @param h The height of the new Document.
+	 */
+	public Document.from_theme(Theme doc_theme, int w, int h) throws GLib.Error
+	{
+		width = w;
+		height = h;
+		theme = doc_theme;
+		
+		// allocate a temp directory for the new document
+		path = Temp.request();
+		
+		// copy media to the new path
+		doc_theme.copy_media(path);
+		
+		// get the master
+		var master = theme.slide_by_title(DEFAULT_SLIDE);
+		
+		// add the first slide
+		append_slide(new Slide.from_master(master, this, width, height));
+	}
+	
+	/**
 	 * Inserts a new { link Slide} into the Document
 	 *
 	 * @param s The { link Slide} to insert.
diff --git a/src/SlideSet.vala b/src/SlideSet.vala
index 7fd4f7f..7f179cb 100644
--- a/src/SlideSet.vala
+++ b/src/SlideSet.vala
@@ -20,6 +20,8 @@
  */
 public abstract class Ease.SlideSet : Object
 {
+	private const string MEDIA_PATH = "Media";
+
 	/**
 	 * The filename of the of the SlideSet when archived. Typically, this is a
 	 * .ease or .easetheme file.
@@ -95,5 +97,20 @@ public abstract class Ease.SlideSet : Object
 		}
 		return null;
 	}
+	
+	/**
+	 * Copies all files under Media/ to a new directory.
+	 *
+	 * @param target The path to copy media files to.
+	 */
+	public void copy_media(string target) throws GLib.Error
+	{
+		var origin_path = Path.build_filename(path, MEDIA_PATH);
+		
+		var target_path = Path.build_filename(target, MEDIA_PATH);
+		
+		// TODO: non-system implementation of recursive copy
+		Posix.system("cp -r %s %s".printf(origin_path, target_path));
+	}
 }
 
diff --git a/src/WelcomeWindow.vala b/src/WelcomeWindow.vala
index fcce850..65df72a 100644
--- a/src/WelcomeWindow.vala
+++ b/src/WelcomeWindow.vala
@@ -196,28 +196,24 @@ public class Ease.WelcomeWindow : Gtk.Window
 		
 		// ui signals
 		new_button.clicked.connect(() => {
-			// create a new Document
-			var document = new Document();
-			document.width = (int)x_res.get_value();
-			document.height = (int)y_res.get_value();
-			document.theme = selected_theme;
-			
-			// allocate a temp directory for the new document
-			document.path = Temp.request();
-			
-			// get the master
-			var master = selected_theme.slide_by_title(PREVIEW_ID);
+			try
+			{
+				// create a new Document
+				var document = new Document.from_theme(selected_theme,
+					                                   (int)x_res.get_value(),
+					                                   (int)y_res.get_value());
 			
-			// add the first slide
-			document.append_slide(new Slide.from_master(master, document,
-			                                            document.width,
-			                                            document.height));
 			
-			// create an EditorWindow for the new Document
-			var editor = new EditorWindow(document);
+				// create an EditorWindow for the new Document
+				var editor = new EditorWindow(document);
 			
-			Main.add_window(editor);
-			Main.remove_welcome();
+				Main.add_window(editor);
+				Main.remove_welcome();
+			}
+			catch (Error e)
+			{
+				error_dialog(_("Error creating new document"), e.message);
+			}
 		});
 		
 		// changing resolution values



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