/** This sample is provided "as is" without warranty of any kind. Microsoft disclaims all warranties either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Microsoft Corporation or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if Microsoft corporation or its suppliers have been advised or the possibility of such damages. Some states do nto allow the exclusion or limitation of liability for consequential or inidental damages so the foregoing limitation may not apply. **/ using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Drawing; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Diagnostics; using System.ComponentModel; using System.Net; using System.Collections; namespace Uberdemo { public class Food : INotifyPropertyChanged { //IMPORTANT: In order to make the ECB file contents searchable from Windows you will have to go // to the control panel and make sure that the food file type contents are searchable (which is not the default) // the default is just to search the title and properties. // public Food() { } public Food(string thumbnailimage, string guid, string title) { this.ThumbnailImage = thumbnailimage; this._title = title; this._guid = guid; } private string thumbnailImage = null; [XmlElement("ThumbnailImage", IsNullable = true)] public string ThumbnailImage { get { return this.thumbnailImage; } set { this.thumbnailImage = value; } } private string _title = null; public string Title { get { return this._title; } set { this._title = value; } } private string _note; public string Note { get { return _note; } set { _note = value; } } private string _guid = null; [XmlElement("Guid", IsNullable = true)] public string Guid { get { return this._guid; } set { this._guid = value; } } private string _version; public string Version { get { return _version; } set { _version = value; } } private string _tags; public string Tags { get { return _tags; } set { _tags = value; } } private string _source; public string Source { get { return _source; } set { _source = value; } } private string _shelflife; public string Shelflife { get { return _shelflife; } set { _shelflife = value; } } private string _rating; public string Rating { get { return _rating; } set { _rating = value; } } private string _difficulty; public string Difficulty { get { return _difficulty; } set { _difficulty = value; } } private string _servings; public string Servings { get { return _servings; } set { _servings = value; } } private string _prepTime; public string PrepTime { get { return _prepTime; } set { _prepTime = value; } } private string _totalTime; public string TotalTime { get { return _totalTime; } set { _totalTime = value; } } private PartCollection _parts = null; public PartCollection Parts { get { return this._parts; } set { this._parts = value; } } private CommentCollection _comments = null; public CommentCollection Comments { get { return this._comments; } set { this._comments = value; } } public void Draw(Graphics g, int squareLength) { Debug.Assert(image != null); g.DrawImage(image, new Rectangle(0,0,image.Width,image.Height)); } public Rectangle GetImage() { try { Debug.WriteLine(">>>>>" + this.Parts[0].Ingredients[0].Item); //just show first ingredient to show that debugger is working. } catch { } try { // TODO: When an image is not available, supply a standard image. // //TODO: Look in file for image, if it is not in file, look remotely. // if (this.thumbnailImage != null && this.thumbnailImage != "") { if (!this.thumbnailImage.ToLower().Contains("http:")) { //if it is a full local path if (this.thumbnailImage.Contains(":")) { this.image = Bitmap.FromFile(this.thumbnailImage); } else { // if it is relative path string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal).Replace("Documents", ""), @"recipes\"); this.image = Bitmap.FromFile(path + this.thumbnailImage); } } else { // if is is a web path. HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(this.thumbnailImage); this.image = Bitmap.FromStream(Request.GetResponse().GetResponseStream()); } } else { Image i = null; //i = new Bitmap(GetType(), "images.chef_hat.jpg"); i = new Bitmap(GetType(), "images.ecb.png"); Debug.WriteLine(">>>>>~~~~" + i); this.image = i; } } catch (Exception e) { Debug.WriteLine(">>>>>" + e.Message + ":" + e.StackTrace); } return new Rectangle(0, 0, image.Width, image.Height); } /// /// Creates an instance of the Food class from a byte array. /// This is useful for cases where a stream is available such as in shell. /// /// /// public static Food FoodFromByteArray(byte[] bits) { Food c = null; using (MemoryStream ms = new MemoryStream(bits)) { c = serializer.Deserialize(ms) as Food; } return (c); } /// /// Reads a ECB file from disk and marshals it into the Food class /// Returns null if file is not found or valid /// /// /// public static Food ReadFromFile(string filepath) { Food food = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.IgnoreWhitespace = true; try { using (XmlReader reader = XmlReader.Create(filepath, settings)) { food = serializer.Deserialize(reader) as Food; } } catch(Exception e) { Debug.WriteLine("> > > > >" + e.Message + ":" + e.StackTrace); } return food; } public void WriteToFile(string filepath) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); using (XmlWriter writer = XmlWriter.Create(filepath, settings)) { serializer.Serialize(writer, this); } } static Food() { serializer = new XmlSerializer(typeof(Food)); } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(String info) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(info)); } } private static XmlSerializer serializer; private Image image = null; } public class Ingredient { public Ingredient() { } public Ingredient(float quantity, string item, string unit) { this._item = item; this._quantity = quantity; this._unit = unit; } public Ingredient(float quantity, string item, string unit, string style) { this._item = item; this._quantity = quantity; this._unit = unit; this._style = style; } private float _quantity; [XmlElement(ElementName="Quantity", Type = typeof(float))] public float Quantity { get { return this._quantity; } set { this._quantity = value; } } string _unit = null; public string Unit { get { return this._unit; } set { this._unit = value; } } string _item = null; public string Item { get { return this._item; } set { this._item = value; } } string _style = null; public string Style { get { return this._style;} set { this._style = value;} } private bool _optional; [XmlElement(ElementName = "Optional", Type = typeof(bool))] public bool Optional { get { return this._optional; } set { this._optional = value; } } private string _ref; public string Ref { get { return this._ref; } set { this._ref = value; } } private IngredientCollection _substitutes; public IngredientCollection Substitutes { get { return this._substitutes; } set { this._substitutes = value; } } private string _shelflife; public string Shelflife { get { return this._shelflife; } set { this._shelflife = value; } } } public class Comment { public Comment() { } public Comment(string author, string note, string rating, string avatarimagepath) { this._author = author; this._note = note; this._rating = rating; this._avatarImagePath = avatarimagepath; } private string _avatarImagePath; public string AvatarImagePath { get { return this._avatarImagePath; } set { this._avatarImagePath = value; } } private string _author; public string Author { get { return this._author; } set { this._author = value; } } private string _note; public string Note { get { return this._note; } set { this._note = value; } } private string _rating; public string Rating { get { return this._rating; } set { this._rating = value; } } private string _ref; public string Ref { get { return this._ref; } set { this._ref = value; } } private IngredientCollection _ingredients; public IngredientCollection Ingredients { get { return this._ingredients; } set { this._ingredients = value; } } } public class Part { public Part() { } public Part(string name) { this._name = name; } public Part(string name, IngredientCollection ingredients, StepCollection directions) { this._name = name; this._directions = directions; this._ingredients = ingredients; } private string _name; public string Name { get { return this._name; } set { this._name = value; } } private IngredientCollection _ingredients; public IngredientCollection Ingredients { get { return this._ingredients; } set { this._ingredients = value; } } private StepCollection _directions; public StepCollection Directions { get { return this._directions; } set { this._directions = value; } } } public class Step { public Step() { } public Step(string text) { this.valueField = text; } public override string ToString() { return this.valueField; } private string valueField; /// // this allows having text in the root of the element. Normally you would have to add a property to the class and put the content in that property, // but that is very verbose, and I wanted to keep the XML simple [System.Xml.Serialization.XmlTextAttribute] // important. must add this attribute to have it work public string Value { get { return this.valueField; } set { this.valueField = value; } } } public class StepCollection : ICollection { public string CollectionName; private ArrayList array = new ArrayList(); public Step this[int index] { get { return (Step)array[index]; } } public void CopyTo(Array a, int index) { array.CopyTo(a, index); } public int Count { get { return array.Count; } } public object SyncRoot { get { return this; } } public bool IsSynchronized { get { return false; } } public IEnumerator GetEnumerator() { return array.GetEnumerator(); } public void Add(Step o) { array.Add(o); } } public class IngredientCollection : ICollection { public string CollectionName; private ArrayList array = new ArrayList(); public Ingredient this[int index] { get { return (Ingredient)array[index]; } } public void CopyTo(Array a, int index) { array.CopyTo(a, index); } public int Count { get { return array.Count; } } public object SyncRoot { get { return this; } } public bool IsSynchronized { get { return false; } } public IEnumerator GetEnumerator() { return array.GetEnumerator(); } public void Add(Ingredient o) { array.Add(o); } } public class PartCollection : ICollection { public string CollectionName; private ArrayList array = new ArrayList(); public Part this[int index] { get { return (Part)array[index]; } } public void CopyTo(Array a, int index) { array.CopyTo(a, index); } public int Count { get { return array.Count; } } public object SyncRoot { get { return this; } } public bool IsSynchronized { get { return false; } } public IEnumerator GetEnumerator() { return array.GetEnumerator(); } public void Add(Part o) { array.Add(o); } } public class CommentCollection : ICollection { public string CollectionName; private ArrayList array = new ArrayList(); public Comment this[int index] { get { return (Comment)array[index]; } } public void CopyTo(Array a, int index) { array.CopyTo(a, index); } public int Count { get { return array.Count; } } public object SyncRoot { get { return this; } } public bool IsSynchronized { get { return false; } } public IEnumerator GetEnumerator() { return array.GetEnumerator(); } public void Add(Comment o) { array.Add(o); } } }