[longomatch/redesign2: 40/140] Use a base class for serialization



commit b66970269600996d4eeb688a13ef8cd50a70ae52
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Wed Dec 22 01:31:53 2010 +0100

    Use a base class for serialization

 LongoMatch/Common/SerializableObject.cs           |   42 +++++++++
 LongoMatch/Gui/Component/ProjectDetailsWidget.cs  |    6 +-
 LongoMatch/Gui/Component/ProjectTemplateWidget.cs |    9 +-
 LongoMatch/Gui/Dialog/TemplatesEditor.cs          |   14 ++--
 LongoMatch/IO/SectionsReader.cs                   |  103 ---------------------
 LongoMatch/IO/SectionsWriter.cs                   |  101 --------------------
 LongoMatch/LongoMatch.mdp                         |    3 +-
 LongoMatch/Main.cs                                |    3 +-
 LongoMatch/Makefile.am                            |    4 +-
 LongoMatch/Playlist/IPlayList.cs                  |    1 -
 LongoMatch/Playlist/PlayList.cs                   |   40 ++------
 LongoMatch/Store/Templates/CategoriesTemplate.cs  |    2 +-
 LongoMatch/Store/Templates/TagsTemplate.cs        |    3 +-
 LongoMatch/Store/Templates/TeamTemplate.cs        |    3 +-
 14 files changed, 74 insertions(+), 260 deletions(-)
---
diff --git a/LongoMatch/Common/SerializableObject.cs b/LongoMatch/Common/SerializableObject.cs
new file mode 100644
index 0000000..fa3acbf
--- /dev/null
+++ b/LongoMatch/Common/SerializableObject.cs
@@ -0,0 +1,42 @@
+// 
+//  Copyright (C) 2010 Andoni Morales Alastruey
+// 
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+// 
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//  
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+// 
+using System;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
+
+namespace LongoMatch.Common
+{
+	public class SerializableObject
+	{
+		protected void Save<T>(T obj, string filepath) {
+			BinaryFormatter formatter = new  BinaryFormatter();
+			Stream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None);
+			formatter.Serialize(stream, obj);
+			stream.Close();
+		}
+
+		protected static T Load<T>(string filepath) {
+			BinaryFormatter formatter = new BinaryFormatter();
+			Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
+			var obj = formatter.Deserialize(stream);
+			stream.Close();
+			return (T)obj;
+		}
+	}
+}
+
diff --git a/LongoMatch/Gui/Component/ProjectDetailsWidget.cs b/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
index ad86aa1..81258c6 100644
--- a/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
+++ b/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
@@ -414,8 +414,7 @@ namespace LongoMatch.Gui.Component
 				i++;
 			}
 			tagscombobox.Active = index;
-			var reader = new CategoriesReader(System.IO.Path.Combine(MainClass.TemplatesDir(),SectionsFile));
-			Categories = reader.GetCategories();
+			Categories = Categories.Load(System.IO.Path.Combine(MainClass.TemplatesDir(),SectionsFile));
 		}
 
 		private void FillTeamsTemplate() {
@@ -537,8 +536,7 @@ namespace LongoMatch.Gui.Component
 
 		protected virtual void OnCombobox1Changed(object sender, System.EventArgs e)
 		{
-			var reader = new CategoriesReader(System.IO.Path.Combine(MainClass.TemplatesDir(),SectionsFile));
-			Categories = reader.GetCategories();
+			Categories = Categories.Load(System.IO.Path.Combine(MainClass.TemplatesDir(),SectionsFile));
 		}
 
 		protected virtual void OnVisitorcomboboxChanged(object sender, System.EventArgs e)
diff --git a/LongoMatch/Gui/Component/ProjectTemplateWidget.cs b/LongoMatch/Gui/Component/ProjectTemplateWidget.cs
index 6e01435..1df0aaa 100644
--- a/LongoMatch/Gui/Component/ProjectTemplateWidget.cs
+++ b/LongoMatch/Gui/Component/ProjectTemplateWidget.cs
@@ -167,10 +167,6 @@ namespace LongoMatch.Gui.Component
 			Edited = true;
 		}
 		
-		private void SaveTemplate(string templateName){
-			CategoriesWriter.UpdateTemplate(templateName+".sct", Categories);
-		}
-
 		protected virtual void OnNewAfter(object sender, EventArgs args) {
 			AddCategory(categories.CategoriesList.IndexOf(selectedCategories[0])+1);
 		}
@@ -233,10 +229,11 @@ namespace LongoMatch.Gui.Component
 					                                                       "Do you want to overwrite it ?")
 					                                   );
 					if (md.Run() == (int)ResponseType.Yes)
-						SaveTemplate(dialog.Text);
+						Categories.Save(dialog.Text);
 					md.Destroy();
 				}					
-				else SaveTemplate(dialog.Text);
+				else
+					Categories.Save(dialog.Text);
 			}	
 			dialog.Destroy();
 		}
diff --git a/LongoMatch/Gui/Dialog/TemplatesEditor.cs b/LongoMatch/Gui/Dialog/TemplatesEditor.cs
index ba747fd..468a9d0 100644
--- a/LongoMatch/Gui/Dialog/TemplatesEditor.cs
+++ b/LongoMatch/Gui/Dialog/TemplatesEditor.cs
@@ -21,7 +21,6 @@
 using System;
 using System.Collections.Generic;
 using Gtk;
-using LongoMatch.IO;
 using LongoMatch.Store.Templates;
 using Mono.Unix;
 
@@ -107,14 +106,14 @@ namespace LongoMatch.Gui.Dialog
 		}
 
 		private void UpdateCategories() {
-			CategoriesReader sr = new CategoriesReader(templateName);
-			selectedCategoriesTemplate = sr.GetCategories();
-			SetCategoriesTemplate(sr.GetCategories());
+			selectedCategoriesTemplate = Categories.Load(templateName);
+			SetCategoriesTemplate(selectedCategoriesTemplate);
 			SetSensitive(true);
 		}
 
 		private void UpdateTeamTemplate() {
-			SetTeamTemplate(TeamTemplate.Load(templateName));
+			selectedTeamTemplate = TeamTemplate.Load(templateName);
+			SetTeamTemplate(selectedTeamTemplate);
 			SetSensitive(true);
 		}
 
@@ -148,7 +147,7 @@ namespace LongoMatch.Gui.Dialog
 		private void SaveTemplate() {
 			if (useType == UseType.CategoriesTemplate) {
 				selectedCategoriesTemplate = sectionspropertieswidget1.Categories;
-				CategoriesWriter.UpdateTemplate(templateName,selectedCategoriesTemplate);
+				selectedCategoriesTemplate.Save(templateName);
 			}
 			else {
 				selectedTeamTemplate = teamtemplatewidget1.TeamTemplate;
@@ -215,7 +214,8 @@ namespace LongoMatch.Gui.Dialog
 						System.IO.File.Copy(System.IO.Path.Combine(MainClass.TemplatesDir(),ed.SelectedTemplate),
 						                    System.IO.Path.Combine(MainClass.TemplatesDir(),name+fileExtension));
 				else if (useType == UseType.CategoriesTemplate){
-					CategoriesWriter.CreateNewTemplate(name+fileExtension);
+					Categories cat = Categories.DefaultTemplate();
+					cat.Save(name+fileExtension);
 				}
 				else {
 					TeamTemplate tt = TeamTemplate.DefaultTemplate(count);
diff --git a/LongoMatch/LongoMatch.mdp b/LongoMatch/LongoMatch.mdp
index 30d9192..9994af9 100644
--- a/LongoMatch/LongoMatch.mdp
+++ b/LongoMatch/LongoMatch.mdp
@@ -198,7 +198,8 @@
     <File subtype="Code" buildaction="Compile" name="Store/Templates/CategoriesTemplate.cs" />
     <File subtype="Code" buildaction="Compile" name="Store/Templates/TagsTemplate.cs" />
     <File subtype="Code" buildaction="Compile" name="Store/Templates/TeamTemplate.cs" />
-    <File subtype="Code" buildaction="Compile" name="Store/Templates/Template.cs" />
+    <File subtype="Code" buildaction="Compile" name="IO/XMLReader.cs" />
+    <File subtype="Code" buildaction="Compile" name="Common/SerializableObject.cs" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
diff --git a/LongoMatch/Main.cs b/LongoMatch/Main.cs
index 903fbfd..c1d2beb 100644
--- a/LongoMatch/Main.cs
+++ b/LongoMatch/Main.cs
@@ -131,7 +131,8 @@ namespace LongoMatch
 			string fConfig;
 			fConfig = System.IO.Path.Combine(TemplatesDir(),"default.sct");
 			if (!System.IO.File.Exists(fConfig)) {
-				CategoriesWriter.CreateNewTemplate("default.sct");
+				Categories cat = Categories.DefaultTemplate();
+				cat.Save("default.sct");
 			}
 
 			fConfig = System.IO.Path.Combine(TemplatesDir(),"default.tem");
diff --git a/LongoMatch/Makefile.am b/LongoMatch/Makefile.am
index 6b23ca0..ce4b677 100644
--- a/LongoMatch/Makefile.am
+++ b/LongoMatch/Makefile.am
@@ -79,6 +79,7 @@ FILES = \
 	Common/Enums.cs \
 	Common/Cairo.cs \
 	Common/Constants.cs \
+	Common/SerializableObject.cs \
 	DB/DataBase.cs \
 	gtk-gui/generated.cs \
 	gtk-gui/LongoMatch.Gui.Component.ButtonsWidget.cs \
@@ -180,8 +181,6 @@ FILES = \
 	Handlers/HotKeysManager.cs \
 	Handlers/VideoDrawingsManager.cs\
 	IO/CSVExport.cs \
-	IO/SectionsReader.cs \
-	IO/SectionsWriter.cs \
 	IO/XMLReader.cs \
 	Main.cs \
 	Playlist/IPlayList.cs \
@@ -202,7 +201,6 @@ FILES = \
 	Store/Templates/CategoriesTemplate.cs \
 	Store/Templates/TagsTemplate.cs \
 	Store/Templates/TeamTemplate.cs \
-	Store/Templates/Template.cs \
 	Updates/Updater.cs \
 	Updates/XmlUpdateParser.cs \
 	Utils/ProjectUtils.cs
diff --git a/LongoMatch/Playlist/IPlayList.cs b/LongoMatch/Playlist/IPlayList.cs
index 68d6aa3..ad41f5c 100644
--- a/LongoMatch/Playlist/IPlayList.cs
+++ b/LongoMatch/Playlist/IPlayList.cs
@@ -30,7 +30,6 @@ namespace LongoMatch.Playlist {
 		int Count {
 			get;
 		}
-		void Load(string path);
 		
 		void Save(string path);
 		
diff --git a/LongoMatch/Playlist/PlayList.cs b/LongoMatch/Playlist/PlayList.cs
index 92a31bb..6933dd7 100644
--- a/LongoMatch/Playlist/PlayList.cs
+++ b/LongoMatch/Playlist/PlayList.cs
@@ -26,31 +26,28 @@ using System.Runtime.Serialization.Formatters.Binary;
 using System.Xml.Serialization;
 using Gtk;
 using LongoMatch.Store;
+using LongoMatch.Common;
 using Mono.Unix;
 namespace LongoMatch.Playlist
 {
 
 
-	public class PlayList: IPlayList
+	public class PlayList: SerializableObject,IPlayList
 	{
 
 		private  List<PlayListPlay> list;
-		private static XmlSerializer ser;
 		private string filename = null;
 		private int indexSelection = 0;
 		private Version version;
 
 		#region Constructors
 		public PlayList() {
-			ser = new XmlSerializer(typeof(List<PlayListPlay>),new Type[] {typeof(PlayListPlay)});
 			list = new List<PlayListPlay>();
 			version = new Version(1,0);
 		}
 
 		public PlayList(string file)
 		{
-			ser = new XmlSerializer(typeof(List<PlayListPlay>),new Type[] {typeof(PlayListPlay)});
-
 			//For new Play List
 			if (!System.IO.File.Exists(file)) {
 				list = new List<PlayListPlay>();
@@ -85,35 +82,18 @@ namespace LongoMatch.Playlist
 		#endregion
 
 		#region Public methods
-
-		public void Load(string file) {
-			using(FileStream strm = new FileStream(file, FileMode.Open, FileAccess.Read))
-			{
-				try {
-					list = ser.Deserialize(strm) as List<PlayListPlay>;
-				}
-				catch {
-					throw new Exception(Catalog.GetString("The file you are trying to load is not a valid playlist"));
-				}
-			}
-			foreach (PlayListPlay plNode in list) {
-				plNode.Valid = System.IO.File.Exists(plNode.MediaFile.FilePath);
-			}
-			filename = file;
+		public void Save(){
+			Save(File);
 		}
 
-		public void Save() {
-			Save(filename);
+		public void Save(string filePath){
+			Save(this, filePath);
 		}
-
-		public void Save(string file) {
-			file = Path.ChangeExtension(file,"lgm");
-			using(FileStream strm = new FileStream(file, FileMode.Create, FileAccess.Write))
-			{
-				ser.Serialize(strm, list);
-			}
+		
+		public static PlayList Load(string filePath) {
+			return Load<PlayList>(filePath);
 		}
-
+		
 		public bool isLoaded() {
 			return filename != null;
 		}
diff --git a/LongoMatch/Store/Templates/CategoriesTemplate.cs b/LongoMatch/Store/Templates/CategoriesTemplate.cs
index 42e3af2..43554ca 100644
--- a/LongoMatch/Store/Templates/CategoriesTemplate.cs
+++ b/LongoMatch/Store/Templates/CategoriesTemplate.cs
@@ -35,7 +35,7 @@ namespace LongoMatch.Store.Templates
 	/// The <see cref="LongoMatch.DB.Project"/> must handle all the changes
 	/// </summary>
 	[Serializable]
-	public class Categories: Template
+	public class Categories: SerializableObject
 	{
 		private List<Category> categoriesList;
 
diff --git a/LongoMatch/Store/Templates/TagsTemplate.cs b/LongoMatch/Store/Templates/TagsTemplate.cs
index 583f016..e4bd3cc 100644
--- a/LongoMatch/Store/Templates/TagsTemplate.cs
+++ b/LongoMatch/Store/Templates/TagsTemplate.cs
@@ -18,12 +18,13 @@
 
 using System;
 using System.Collections.Generic;
+using LongoMatch.Common;
 
 namespace LongoMatch.Store.Templates
 {
 
 	[Serializable]
-	public class TagsTemplate: Template
+	public class TagsTemplate: SerializableObject
 	{
 		List<Tag> tagsList;
 		public TagsTemplate()
diff --git a/LongoMatch/Store/Templates/TeamTemplate.cs b/LongoMatch/Store/Templates/TeamTemplate.cs
index f3b41b7..931f759 100644
--- a/LongoMatch/Store/Templates/TeamTemplate.cs
+++ b/LongoMatch/Store/Templates/TeamTemplate.cs
@@ -19,12 +19,13 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using LongoMatch.Common;
 
 namespace LongoMatch.Store.Templates
 {
 	[Serializable]
 
-	public class TeamTemplate: Template
+	public class TeamTemplate: SerializableObject
 	{
 		private List<Player> playersList;
 



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