|
| 1 | +// **************************************************************************** |
| 2 | +// |
| 3 | +// Copyright (C) 2014-2016 TautCony (TautCony@vcb-s.com) |
| 4 | +// |
| 5 | +// This program is free software; you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation; either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program; if not, write to the Free Software |
| 17 | +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | +// |
| 19 | +// **************************************************************************** |
| 20 | + |
| 21 | +namespace ChapterTool.Util |
| 22 | +{ |
| 23 | + using System; |
| 24 | + using System.Collections.Generic; |
| 25 | + using System.IO; |
| 26 | + using System.Text.Json; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Cross-platform settings storage using JSON file |
| 30 | + /// Replaces Registry-based storage from WinForms version |
| 31 | + /// </summary> |
| 32 | + public static class RegistryStorage |
| 33 | + { |
| 34 | + private static readonly string SettingsPath = Path.Combine( |
| 35 | + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), |
| 36 | + "ChapterTool", |
| 37 | + "settings.json"); |
| 38 | + |
| 39 | + private static Dictionary<string, string> _settings = new(); |
| 40 | + private static bool _loaded = false; |
| 41 | + |
| 42 | + static RegistryStorage() |
| 43 | + { |
| 44 | + EnsureSettingsDirectory(); |
| 45 | + } |
| 46 | + |
| 47 | + private static void EnsureSettingsDirectory() |
| 48 | + { |
| 49 | + var directory = Path.GetDirectoryName(SettingsPath); |
| 50 | + if (directory != null && !Directory.Exists(directory)) |
| 51 | + { |
| 52 | + Directory.CreateDirectory(directory); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static void LoadSettings() |
| 57 | + { |
| 58 | + if (_loaded) return; |
| 59 | + |
| 60 | + try |
| 61 | + { |
| 62 | + if (File.Exists(SettingsPath)) |
| 63 | + { |
| 64 | + var json = File.ReadAllText(SettingsPath); |
| 65 | + _settings = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new(); |
| 66 | + } |
| 67 | + } |
| 68 | + catch |
| 69 | + { |
| 70 | + _settings = new Dictionary<string, string>(); |
| 71 | + } |
| 72 | + |
| 73 | + _loaded = true; |
| 74 | + } |
| 75 | + |
| 76 | + private static void SaveSettings() |
| 77 | + { |
| 78 | + try |
| 79 | + { |
| 80 | + EnsureSettingsDirectory(); |
| 81 | + var json = JsonSerializer.Serialize(_settings, new JsonSerializerOptions { WriteIndented = true }); |
| 82 | + File.WriteAllText(SettingsPath, json); |
| 83 | + } |
| 84 | + catch |
| 85 | + { |
| 86 | + // Silently fail if we can't save settings |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static string? Load(string subkey, string name) |
| 91 | + { |
| 92 | + // Legacy compatibility - combine subkey and name |
| 93 | + return Load($"{subkey}_{name}"); |
| 94 | + } |
| 95 | + |
| 96 | + public static string? Load(string name) |
| 97 | + { |
| 98 | + LoadSettings(); |
| 99 | + return _settings.TryGetValue(name, out var value) ? value : null; |
| 100 | + } |
| 101 | + |
| 102 | + public static void Save(string value, string subkey, string name) |
| 103 | + { |
| 104 | + // Legacy compatibility - combine subkey and name |
| 105 | + Save($"{subkey}_{name}", value); |
| 106 | + } |
| 107 | + |
| 108 | + public static void Save(string name, string value) |
| 109 | + { |
| 110 | + LoadSettings(); |
| 111 | + _settings[name] = value; |
| 112 | + SaveSettings(); |
| 113 | + } |
| 114 | + |
| 115 | + public static void Delete(string name) |
| 116 | + { |
| 117 | + LoadSettings(); |
| 118 | + if (_settings.ContainsKey(name)) |
| 119 | + { |
| 120 | + _settings.Remove(name); |
| 121 | + SaveSettings(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments