using MediaBrowser.Controller.Configuration; using System; using System.Collections.Generic; using System.IO; namespace MediaBrowser.Controller.Localization { /// /// Class RatingsDefinition /// public class RatingsDefinition { /// /// Initializes a new instance of the class. /// /// The file. /// The configuration manager. public RatingsDefinition(string file, IServerConfigurationManager configurationManager) { this.file = file; if (!Load()) { Init(configurationManager.Configuration.MetadataCountryCode.ToUpper()); } } /// /// Inits the specified country. /// /// The country. protected void Init(string country) { //intitialze based on country switch (country) { case "US": RatingsDict = new USRatingsDictionary(); break; case "GB": RatingsDict = new GBRatingsDictionary(); break; case "NL": RatingsDict = new NLRatingsDictionary(); break; case "AU": RatingsDict = new AURatingsDictionary(); break; default: RatingsDict = new USRatingsDictionary(); break; } Save(); } /// /// The file /// readonly string file; /// /// Save to file /// public void Save() { // Use simple text serialization - no need for xml using (var fs = new StreamWriter(file)) { foreach (var pair in RatingsDict) { fs.WriteLine(pair.Key + "," + pair.Value); } } } /// /// Load from file /// /// true if XXXX, false otherwise protected bool Load() { // Read back in our simple serialized format RatingsDict = new Dictionary(StringComparer.OrdinalIgnoreCase); try { using (var fs = new StreamReader(file)) { while (!fs.EndOfStream) { var line = fs.ReadLine() ?? ""; var values = line.Split(','); if (values.Length == 2) { int value; if (int.TryParse(values[1], out value)) { RatingsDict[values[0].Trim()] = value; } else { //Logger.Error("Invalid line in ratings file " + file + "(" + line + ")"); } } } } } catch { // Couldn't load - probably just not there yet return false; } return true; } /// /// The ratings dict /// public Dictionary RatingsDict = new Dictionary(StringComparer.OrdinalIgnoreCase); } }