diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-20 20:33:05 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-20 20:33:05 -0500 |
| commit | 767cdc1f6f6a63ce997fc9476911e2c361f9d402 (patch) | |
| tree | 49add55976f895441167c66cfa95e5c7688d18ce /MediaBrowser.Controller/Localization/RatingsDefinition.cs | |
| parent | 845554722efaed872948a9e0f7202e3ef52f1b6e (diff) | |
Pushing missing changes
Diffstat (limited to 'MediaBrowser.Controller/Localization/RatingsDefinition.cs')
| -rw-r--r-- | MediaBrowser.Controller/Localization/RatingsDefinition.cs | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/MediaBrowser.Controller/Localization/RatingsDefinition.cs b/MediaBrowser.Controller/Localization/RatingsDefinition.cs new file mode 100644 index 0000000000..65bd3bcbb4 --- /dev/null +++ b/MediaBrowser.Controller/Localization/RatingsDefinition.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.IO; +using MediaBrowser.Common.Logging; + +namespace MediaBrowser.Controller.Localization +{ + /// <summary> + /// Class RatingsDefinition + /// </summary> + public class RatingsDefinition + { + /// <summary> + /// Initializes a new instance of the <see cref="RatingsDefinition" /> class. + /// </summary> + /// <param name="file">The file.</param> + public RatingsDefinition(string file) + { + Logger.LogInfo("Loading Certification Ratings from file " + file); + this.file = file; + if (!Load()) + { + Init(Kernel.Instance.Configuration.MetadataCountryCode.ToUpper()); + } + } + + /// <summary> + /// Inits the specified country. + /// </summary> + /// <param name="country">The country.</param> + 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(); + } + + /// <summary> + /// The file + /// </summary> + readonly string file; + + /// <summary> + /// Save to file + /// </summary> + 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); + } + } + } + + /// <summary> + /// Load from file + /// </summary> + /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> + protected bool Load() + { + // Read back in our simple serialized format + RatingsDict = new Dictionary<string, int>(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.LogError("Invalid line in ratings file " + file + "(" + line + ")"); + } + } + } + } + } + catch + { + // Couldn't load - probably just not there yet + return false; + } + return true; + } + + /// <summary> + /// The ratings dict + /// </summary> + public Dictionary<string, int> RatingsDict = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); + + } +} |
