[longomatch] Added Project's serialization support



commit 76ebd3e6491fb9dab1d24cd4c1f4e3b775870ebd
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Wed Dec 23 04:19:21 2009 +0100

    Added Project's serialization support

 LongoMatch/DB/Project.cs            |   25 ++++++++++++++++++++++++-
 LongoMatch/DB/Sections.cs           |    1 +
 LongoMatch/DB/TagsTemplate.cs       |    2 +-
 LongoMatch/Time/HotKey.cs           |    1 +
 LongoMatch/Time/PixbufTimeNode.cs   |    1 +
 LongoMatch/Time/SectionsTimeNode.cs |   31 +++++++++++++++++++++++++++++--
 LongoMatch/Time/Tag.cs              |    2 +-
 LongoMatch/Time/Time.cs             |    1 +
 8 files changed, 59 insertions(+), 5 deletions(-)
---
diff --git a/LongoMatch/DB/Project.cs b/LongoMatch/DB/Project.cs
index a8f6176..da47207 100644
--- a/LongoMatch/DB/Project.cs
+++ b/LongoMatch/DB/Project.cs
@@ -22,8 +22,11 @@ using System;
 using System.IO;
 using System.Collections;
 using System.Collections.Generic;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
 using Gtk;
 using Gdk;
+using Mono.Unix;
 using LongoMatch.TimeNodes;
 using LongoMatch.Video.Utils;
 
@@ -511,12 +514,32 @@ namespace LongoMatch.DB
 		public int CompareTo(object obj) {
 			if (obj is Project) {
 				Project project = (Project) obj;
-
 				return this.File.FilePath.CompareTo(project.File.FilePath);
 			}
 			else
 				throw new ArgumentException("object is not a Project and cannot be compared");
 		}
+		
+		public static void Export(Project project, string file) {
+			file = Path.ChangeExtension(file,"lpr");
+			IFormatter formatter = new BinaryFormatter();
+			using(Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)){
+				formatter.Serialize(stream, project);			
+			}
+		}
+		
+		public static Project Import(string file) {
+			using(Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))
+			{
+				try {
+					IFormatter formatter = new BinaryFormatter();
+					return (Project)formatter.Deserialize(stream);
+				}
+				catch {
+					throw new Exception(Catalog.GetString("The file you are trying to load is not a valid project"));
+				}
+			}			
+		}		
 		#endregion
 	}
 }
diff --git a/LongoMatch/DB/Sections.cs b/LongoMatch/DB/Sections.cs
index 2ead058..46d6ace 100644
--- a/LongoMatch/DB/Sections.cs
+++ b/LongoMatch/DB/Sections.cs
@@ -33,6 +33,7 @@ namespace LongoMatch.DB
 	/// respect the same index used in the plays list inside a project.
 	/// The <see cref="LongoMatch.DB.Project"/> must handle all the changes
 	/// </summary>
+	[Serializable]
 	public class Sections
 	{
 		private List<SectionsTimeNode> sectionsList;
diff --git a/LongoMatch/DB/TagsTemplate.cs b/LongoMatch/DB/TagsTemplate.cs
index fae3a41..16d412f 100644
--- a/LongoMatch/DB/TagsTemplate.cs
+++ b/LongoMatch/DB/TagsTemplate.cs
@@ -23,7 +23,7 @@ using LongoMatch.TimeNodes;
 namespace LongoMatch.DB
 {
 
-
+	[Serializable]
 	public class TagsTemplate
 	{
 		List<Tag> tagsList;
diff --git a/LongoMatch/Time/HotKey.cs b/LongoMatch/Time/HotKey.cs
index 81db5dd..0e6735b 100644
--- a/LongoMatch/Time/HotKey.cs
+++ b/LongoMatch/Time/HotKey.cs
@@ -36,6 +36,7 @@ namespace LongoMatch.TimeNodes
 	/// modifiers to avoid interfering with ohter shortcuts. In case I'am not associated to any
 	/// key combinatio 'key' and 'modifier' will be set to -1
 	/// </summary>
+	[Serializable]
 	public class HotKey : IEquatable<HotKey>
 	{
 		private int key;
diff --git a/LongoMatch/Time/PixbufTimeNode.cs b/LongoMatch/Time/PixbufTimeNode.cs
index 09c5fde..ab02b3d 100644
--- a/LongoMatch/Time/PixbufTimeNode.cs
+++ b/LongoMatch/Time/PixbufTimeNode.cs
@@ -28,6 +28,7 @@ namespace LongoMatch.TimeNodes
 	/// I am the base class for all the video segments.
 	/// I have a <see cref="Pixbuf"/> with a thumbnail of the video segment I represent
 	/// </summary>
+	[Serializable]
 	public class PixbufTimeNode : TimeNode
 	{
 		private byte[] thumbnailBuf;
diff --git a/LongoMatch/Time/SectionsTimeNode.cs b/LongoMatch/Time/SectionsTimeNode.cs
index 9a882f5..428b9b4 100644
--- a/LongoMatch/Time/SectionsTimeNode.cs
+++ b/LongoMatch/Time/SectionsTimeNode.cs
@@ -19,6 +19,7 @@
 //
 
 using System;
+using System.Runtime.Serialization;
 using Gdk;
 
 namespace LongoMatch.TimeNodes
@@ -28,7 +29,8 @@ namespace LongoMatch.TimeNodes
 	/// I am a tagging category for the analysis. I contain the default values to creates plays
 	/// tagged in my category
 	/// </summary>
-	public class SectionsTimeNode:TimeNode
+	[Serializable]
+	public class SectionsTimeNode:TimeNode, ISerializable
 	{
 		HotKey hotkey;
 		Gdk.Color color;
@@ -57,6 +59,19 @@ namespace LongoMatch.TimeNodes
 			this.hotkey = hotkey;
 			this.color = color;
 		}
+		
+		// this constructor is automatically called during deserialization
+		public SectionsTimeNode(SerializationInfo info, StreamingContext context) {
+			Name = info.GetString("name");
+			Start = (Time)info.GetValue("start", typeof(Time));
+			Stop = (Time)info.GetValue("stop", typeof(Time));
+			HotKey = (HotKey)info.GetValue("hotkey", typeof(HotKey));
+			// read 'red', 'blue' and 'green' values and convert it to Gdk.Color
+			Color = new Color((byte)info.GetValue("red", typeof(ushort)),
+			                  (byte)info.GetValue("green", typeof(ushort)),
+			                  (byte)info.GetValue("blue", typeof(ushort)));
+			Console.WriteLine("Deserialize");
+		}
 		#endregion
 		#region  Properties
 
@@ -83,6 +98,18 @@ namespace LongoMatch.TimeNodes
 				this.color=value;
 			}
 		}
-		#endregion
+				
+		// this method is automatically called during serialization
+		public void GetObjectData(SerializationInfo info, StreamingContext context) {
+			Console.WriteLine("Serialize");
+			info.AddValue("name", Name);
+			info.AddValue("start", Start);
+			info.AddValue("stop", Stop);
+			info.AddValue("hotkey", hotkey);
+			info.AddValue("red", color.Red);
+			info.AddValue("blue", color.Green);
+			info.AddValue("green", color.Blue);
+		}
+		#endregion		
 	}
 }
diff --git a/LongoMatch/Time/Tag.cs b/LongoMatch/Time/Tag.cs
index 99979cf..5214cf9 100644
--- a/LongoMatch/Time/Tag.cs
+++ b/LongoMatch/Time/Tag.cs
@@ -21,7 +21,7 @@ using System;
 namespace LongoMatch.TimeNodes
 {
 
-
+	[Serializable]
 	public class Tag
 	{
 		string text;
diff --git a/LongoMatch/Time/Time.cs b/LongoMatch/Time/Time.cs
index b20d05d..b7a2579 100644
--- a/LongoMatch/Time/Time.cs
+++ b/LongoMatch/Time/Time.cs
@@ -27,6 +27,7 @@ namespace LongoMatch.TimeNodes
 	/// I represent a time instant. Other objects uses me to maintain time units consitency.
 	/// I am expressed in miliseconds and I provide some helper methods for time conversion and representation
 	/// </summary>
+	[Serializable]
 	public class Time :  IComparable
 	{
 		private int time;



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