コード例 #1
0
ファイル: Configuration.cs プロジェクト: FileMeta/FMSlideShow
        public static SlideShow.Settings Load()
        {
            string settingsPath = Path.Combine(s_configPath, c_settingsFilename);

            // If the configuration file doesn't exist, copy in default (if it exists)
            if (!File.Exists(settingsPath))
            {
                string defaultSettingsPath = Path.Combine(
                    Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]),
                    c_defaultSettingsFilename);
                if (File.Exists(defaultSettingsPath))
                {
                    File.Copy(defaultSettingsPath, settingsPath);
                }
            }

            // Create the result
            var list = new List <KeyValuePair <string, object> >();

            var settings = new SlideShow.Settings();

            try
            {
                XElement doc = null;
                using (var stream = new FileStream(settingsPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (var jsonReader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(stream, Encoding.UTF8, new System.Xml.XmlDictionaryReaderQuotas(), null))
                    {
                        doc = XElement.Load(jsonReader);
                    }
                }

                foreach (var ele in doc.Elements())
                {
                    switch (ele.Name.LocalName)
                    {
                    case c_keyRootPath:
                        settings.RootPath = ele.Value;
                        break;

                    case c_keyPaths:
                        foreach (var item in ele.Elements("item"))
                        {
                            settings.Paths.Add(item.Value);
                        }
                        break;

                    case c_keyAdvanceTime:
                    {
                        int intValue;
                        if (int.TryParse(ele.Value, out intValue))
                        {
                            settings.AdvanceTime = intValue;
                        }
                    }
                    break;

                    case c_keyFadeTime:
                    {
                        int intValue;
                        if (int.TryParse(ele.Value, out intValue))
                        {
                            settings.FadeTime = intValue;
                        }
                    }
                    break;

                    case c_keyShowMetadata:
                    {
                        int intValue;
                        if (ele.Value.Equals(c_true, StringComparison.OrdinalIgnoreCase))
                        {
                            settings.ShowMetadata = true;
                        }
                        else if (ele.Value.Equals(c_false, StringComparison.OrdinalIgnoreCase))
                        {
                            settings.ShowMetadata = false;
                        }
                        else if (int.TryParse(ele.Value, out intValue))
                        {
                            settings.ShowMetadata = (intValue != 0);
                        }
                    }
                    break;
                    }
                }
            }
            catch (Exception err)
            {
                // Suppress any exception and just return blank configuration.
                // The only common form is FileNotFound.
                System.Diagnostics.Debug.Assert(err is FileNotFoundException);
            }

            return(settings);
        }
コード例 #2
0
ファイル: Configuration.cs プロジェクト: FileMeta/FMSlideShow
        public static void Save(SlideShow.Settings settings)
        {
            // Merge existing settings
            {
                var existing = Load();
                if (string.IsNullOrEmpty(settings.RootPath))
                {
                    settings.RootPath = existing.RootPath;
                }
                if (settings.Paths.Count == 0)
                {
                    foreach (var p in existing.Paths)
                    {
                        settings.Paths.Add(p);
                    }
                }
                if (!settings.AdvanceTime.HasValue)
                {
                    settings.AdvanceTime = existing.AdvanceTime;
                }
                if (!settings.FadeTime.HasValue)
                {
                    settings.FadeTime = existing.FadeTime;
                }
                if (!settings.ShowMetadata.HasValue)
                {
                    settings.ShowMetadata = existing.ShowMetadata;
                }
            }

            string path = Path.Combine(s_configPath, c_settingsFilename);

            using (var writer = new StreamWriter(path, false, s_utf8))
            {
                writer.WriteLine("{");

                bool first = true;

                if (!string.IsNullOrEmpty(settings.RootPath))
                {
                    first = false;
                    writer.Write($"  \"{c_keyRootPath}\": \"{JsonEncode(settings.RootPath)}\"");
                }

                if (settings.Paths.Count > 0)
                {
                    if (!first)
                    {
                        writer.WriteLine(",");
                    }
                    first = false;
                    writer.WriteLine($"  \"{c_keyPaths}\": [");
                    bool first2 = true;
                    foreach (var val in settings.Paths)
                    {
                        if (!first2)
                        {
                            writer.WriteLine(",");
                        }
                        first2 = false;
                        writer.Write($"    \"{JsonEncode(val)}\"");
                    }
                    writer.WriteLine();
                    writer.Write("  ]");
                }

                if (settings.AdvanceTime.HasValue)
                {
                    if (!first)
                    {
                        writer.WriteLine(",");
                    }
                    first = false;
                    writer.Write($"  \"{c_keyAdvanceTime}\": \"{JsonEncode(settings.AdvanceTime.ToString())}\"");
                }

                if (settings.FadeTime.HasValue)
                {
                    if (!first)
                    {
                        writer.WriteLine(",");
                    }
                    first = false;
                    writer.Write($"  \"{c_keyFadeTime}\": \"{JsonEncode(settings.FadeTime.ToString())}\"");
                }

                if (settings.ShowMetadata.HasValue)
                {
                    if (!first)
                    {
                        writer.WriteLine(",");
                    }
                    first = false;
                    writer.Write($"  \"{c_keyShowMetadata}\": \"{JsonEncode(settings.ShowMetadata.Value ? c_true : c_false)}\"");
                }

                writer.WriteLine();
                writer.WriteLine("}");
            }
        }