diff --git a/JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanLoader.cs b/JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanLoader.cs index b071e353..e50e1e26 100644 --- a/JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanLoader.cs +++ b/JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanLoader.cs @@ -14,10 +14,7 @@ public static async Task Load(Dict dict) return; } - List jsonFiles = Directory.EnumerateFiles(fullPath, "*_bank_*.json", SearchOption.TopDirectoryOnly) - .Where(static s => s.Contains("term", StringComparison.Ordinal) || s.Contains("kanji", StringComparison.Ordinal)) - .ToList(); - + IEnumerable jsonFiles = Directory.EnumerateFiles(fullPath, "*_bank_*.json", SearchOption.TopDirectoryOnly); foreach (string jsonFile in jsonFiles) { List>? jsonElementLists; diff --git a/JL.Core/Dicts/KanjiDict/YomichanKanjiLoader.cs b/JL.Core/Dicts/KanjiDict/YomichanKanjiLoader.cs index 75f3aa0b..5486fdf1 100644 --- a/JL.Core/Dicts/KanjiDict/YomichanKanjiLoader.cs +++ b/JL.Core/Dicts/KanjiDict/YomichanKanjiLoader.cs @@ -14,8 +14,7 @@ public static async Task Load(Dict dict) return; } - List jsonFiles = Directory.EnumerateFiles(fullPath, "kanji_bank_*.json", SearchOption.TopDirectoryOnly).ToList(); - + IEnumerable jsonFiles = Directory.EnumerateFiles(fullPath, "kanji_bank_*.json", SearchOption.TopDirectoryOnly); foreach (string jsonFile in jsonFiles) { List>? jsonObjects; diff --git a/JL.Core/Dicts/PitchAccent/YomichanPitchAccentLoader.cs b/JL.Core/Dicts/PitchAccent/YomichanPitchAccentLoader.cs index b4e2e685..c71b8f2b 100644 --- a/JL.Core/Dicts/PitchAccent/YomichanPitchAccentLoader.cs +++ b/JL.Core/Dicts/PitchAccent/YomichanPitchAccentLoader.cs @@ -16,8 +16,7 @@ public static async Task Load(Dict dict) IDictionary> pitchDict = dict.Contents; - string[] jsonFiles = Directory.GetFiles(fullPath, "term*bank_*.json"); - + IEnumerable jsonFiles = Directory.EnumerateFiles(fullPath, "term*bank_*.json", SearchOption.TopDirectoryOnly); foreach (string jsonFile in jsonFiles) { List>? jsonObjects; diff --git a/JL.Core/Freqs/FrequencyYomichan/FrequencyYomichanLoader.cs b/JL.Core/Freqs/FrequencyYomichan/FrequencyYomichanLoader.cs index a5c2e7eb..92d63c21 100644 --- a/JL.Core/Freqs/FrequencyYomichan/FrequencyYomichanLoader.cs +++ b/JL.Core/Freqs/FrequencyYomichan/FrequencyYomichanLoader.cs @@ -15,10 +15,7 @@ public static async Task Load(Freq freq) return; } - List jsonFiles = Directory.EnumerateFiles(fullPath, "*_bank_*.json", SearchOption.TopDirectoryOnly) - .Where(static s => s.Contains("term", StringComparison.Ordinal) || s.Contains("kanji", StringComparison.Ordinal)) - .ToList(); - + IEnumerable jsonFiles = Directory.EnumerateFiles(fullPath, "*_bank_*.json", SearchOption.TopDirectoryOnly); foreach (string jsonFile in jsonFiles) { List>? frequencyJson; diff --git a/JL.Windows/GUI/AddDictionaryWindow.xaml.cs b/JL.Windows/GUI/AddDictionaryWindow.xaml.cs index 712d962f..046d858e 100644 --- a/JL.Windows/GUI/AddDictionaryWindow.xaml.cs +++ b/JL.Windows/GUI/AddDictionaryWindow.xaml.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -32,17 +33,15 @@ private void CancelButton_Click(object sender, RoutedEventArgs e) private void SaveButton_Click(object sender, RoutedEventArgs e) { - bool isValid = true; + ComboBoxDictType.ClearValue(BorderBrushProperty); + TextBlockPath.ClearValue(BorderBrushProperty); + NameTextBox.ClearValue(BorderBrushProperty); string? typeString = ComboBoxDictType.SelectionBoxItem.ToString(); if (string.IsNullOrEmpty(typeString)) { ComboBoxDictType.BorderBrush = Brushes.Red; - isValid = false; - } - else if (ComboBoxDictType.BorderBrush == Brushes.Red) - { - ComboBoxDictType.ClearValue(BorderBrushProperty); + return; } string path = TextBlockPath.Text; @@ -53,11 +52,7 @@ private void SaveButton_Click(object sender, RoutedEventArgs e) || DictUtils.Dicts.Values.Any(dict => dict.Path == path)) { TextBlockPath.BorderBrush = Brushes.Red; - isValid = false; - } - else if (TextBlockPath.BorderBrush == Brushes.Red) - { - TextBlockPath.ClearValue(BorderBrushProperty); + return; } string name = NameTextBox.Text; @@ -67,28 +62,42 @@ private void SaveButton_Click(object sender, RoutedEventArgs e) || DictUtils.Dicts.ContainsKey(name)) { NameTextBox.BorderBrush = Brushes.Red; - isValid = false; - } - else if (NameTextBox.BorderBrush == Brushes.Red) - { - NameTextBox.ClearValue(BorderBrushProperty); + return; } - if (isValid) + DictType type = typeString!.GetEnum(); + if (DictUtils.YomichanDictTypes.Contains(type)) { - DictType type = typeString!.GetEnum(); - - DictOptions options = _dictOptionsControl.GetDictOptions(type); - Dict dict = new(type, name, path, true, DictUtils.Dicts.Count + 1, 0, false, options); - DictUtils.Dicts.Add(name, dict); - - if (dict.Type is DictType.PitchAccentYomichan) + if (type is DictType.NonspecificKanjiYomichan) { - DictUtils.SingleDictTypeDicts[DictType.PitchAccentYomichan] = dict; + bool validPath = Directory.EnumerateFiles(fullPath, "kanji_bank_*.json", SearchOption.TopDirectoryOnly).Any(); + if (!validPath) + { + TextBlockPath.BorderBrush = Brushes.Red; + return; + } } + else + { + bool validPath = Directory.EnumerateFiles(fullPath, "*_bank_*.json", SearchOption.TopDirectoryOnly).Any(); + if (!validPath) + { + TextBlockPath.BorderBrush = Brushes.Red; + return; + } + } + } + + DictOptions options = _dictOptionsControl.GetDictOptions(type); + Dict dict = new(type, name, path, true, DictUtils.Dicts.Count + 1, 0, false, options); + DictUtils.Dicts.Add(name, dict); - Close(); + if (dict.Type is DictType.PitchAccentYomichan) + { + DictUtils.SingleDictTypeDicts[DictType.PitchAccentYomichan] = dict; } + + Close(); } private void BrowseForDictionaryFile(string filter) diff --git a/JL.Windows/GUI/AddFrequencyWindow.xaml.cs b/JL.Windows/GUI/AddFrequencyWindow.xaml.cs index f13617e6..06a45ef3 100644 --- a/JL.Windows/GUI/AddFrequencyWindow.xaml.cs +++ b/JL.Windows/GUI/AddFrequencyWindow.xaml.cs @@ -30,17 +30,15 @@ private void CancelButton_Click(object sender, RoutedEventArgs e) private void SaveButton_Click(object sender, RoutedEventArgs e) { - bool isValid = true; + FreqTypeComboBox.ClearValue(BorderBrushProperty); + TextBlockPath.ClearValue(BorderBrushProperty); + NameTextBox.ClearValue(BorderBrushProperty); string? typeString = FreqTypeComboBox.SelectionBoxItem.ToString(); if (string.IsNullOrEmpty(typeString)) { FreqTypeComboBox.BorderBrush = Brushes.Red; - isValid = false; - } - else if (FreqTypeComboBox.BorderBrush == Brushes.Red) - { - FreqTypeComboBox.ClearValue(BorderBrushProperty); + return; } string path = TextBlockPath.Text; @@ -51,11 +49,7 @@ private void SaveButton_Click(object sender, RoutedEventArgs e) || FreqUtils.FreqDicts.Values.Any(freq => freq.Path == path)) { TextBlockPath.BorderBrush = Brushes.Red; - isValid = false; - } - else if (TextBlockPath.BorderBrush == Brushes.Red) - { - TextBlockPath.ClearValue(BorderBrushProperty); + return; } string name = NameTextBox.Text; @@ -65,24 +59,26 @@ private void SaveButton_Click(object sender, RoutedEventArgs e) || FreqUtils.FreqDicts.ContainsKey(name)) { NameTextBox.BorderBrush = Brushes.Red; - isValid = false; - } - else if (NameTextBox.BorderBrush == Brushes.Red) - { - NameTextBox.ClearValue(BorderBrushProperty); + return; } - if (isValid) + FreqType type = typeString!.GetEnum(); + if (type is FreqType.Yomichan or FreqType.YomichanKanji) { - FreqType type = typeString!.GetEnum(); + bool validPath = Directory.EnumerateFiles(fullPath, "*_bank_*.json", SearchOption.TopDirectoryOnly).Any(); + if (!validPath) + { + TextBlockPath.BorderBrush = Brushes.Red; + return; + } + } - FreqOptions options = _freqOptionsControl.GetFreqOptions(type); + FreqOptions options = _freqOptionsControl.GetFreqOptions(type); - FreqUtils.FreqDicts.Add(name, - new Freq(type, name, path, true, FreqUtils.FreqDicts.Count + 1, 0, 0, false, options)); + FreqUtils.FreqDicts.Add(name, + new Freq(type, name, path, true, FreqUtils.FreqDicts.Count + 1, 0, 0, false, options)); - Close(); - } + Close(); } private void BrowseForFrequencyFile(string filter) diff --git a/JL.Windows/GUI/EditDictionaryWindow.xaml.cs b/JL.Windows/GUI/EditDictionaryWindow.xaml.cs index e8b40ba7..ba38d986 100644 --- a/JL.Windows/GUI/EditDictionaryWindow.xaml.cs +++ b/JL.Windows/GUI/EditDictionaryWindow.xaml.cs @@ -37,7 +37,8 @@ private void CancelButton_Click(object sender, RoutedEventArgs e) private void SaveButton_Click(object sender, RoutedEventArgs e) { - bool isValid = true; + TextBlockPath.ClearValue(BorderBrushProperty); + NameTextBox.ClearValue(BorderBrushProperty); string path = TextBlockPath.Text; string fullPath = Path.GetFullPath(path, Utils.ApplicationPath); @@ -47,11 +48,7 @@ private void SaveButton_Click(object sender, RoutedEventArgs e) && (!Path.Exists(fullPath) || DictUtils.Dicts.Values.Any(dict => dict.Path == path)))) { TextBlockPath.BorderBrush = Brushes.Red; - isValid = false; - } - else if (TextBlockPath.BorderBrush == Brushes.Red) - { - TextBlockPath.ClearValue(BorderBrushProperty); + return; } string name = NameTextBox.Text; @@ -61,89 +58,103 @@ private void SaveButton_Click(object sender, RoutedEventArgs e) || (!_dict.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && DictUtils.Dicts.ContainsKey(name))) { NameTextBox.BorderBrush = Brushes.Red; - isValid = false; - } - else if (NameTextBox.BorderBrush == Brushes.Red) - { - NameTextBox.ClearValue(BorderBrushProperty); + return; } - if (isValid) - { - string dbPath = DBUtils.GetDictDBPath(_dict.Name); - bool dbExists = File.Exists(dbPath); + string dbPath = DBUtils.GetDictDBPath(_dict.Name); + bool dbExists = File.Exists(dbPath); - if (_dict.Path != path) + if (_dict.Path != path) + { + if (DictUtils.YomichanDictTypes.Contains(_dict.Type)) { - _dict.Path = path; - _dict.Contents = FrozenDictionary>.Empty; - _dict.Ready = false; - - if (dbExists) + if (_dict.Type is DictType.NonspecificKanjiYomichan) + { + bool validPath = Directory.EnumerateFiles(fullPath, "kanji_bank_*.json", SearchOption.TopDirectoryOnly).Any(); + if (!validPath) + { + TextBlockPath.BorderBrush = Brushes.Red; + return; + } + } + else { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); - dbExists = false; + bool validPath = Directory.EnumerateFiles(fullPath, "*_bank_*.json", SearchOption.TopDirectoryOnly).Any(); + if (!validPath) + { + TextBlockPath.BorderBrush = Brushes.Red; + return; + } } } - DictOptions options = _dictOptionsControl.GetDictOptions(_dict.Type); + _dict.Path = path; + _dict.Contents = FrozenDictionary>.Empty; + _dict.Ready = false; - if (_dict.Type is DictType.PitchAccentYomichan) + if (dbExists) { - bool oldDottedLinesOption = _dict.Options.ShowPitchAccentWithDottedLines!.Value; - bool newDottedLinesOption = options.ShowPitchAccentWithDottedLines!.Value; - - if (oldDottedLinesOption != newDottedLinesOption) - { - PopupWindowUtils.SetStrokeDashArray(newDottedLinesOption); - } + DBUtils.SendOptimizePragmaToAllDBs(); + SqliteConnection.ClearAllPools(); + File.Delete(dbPath); + dbExists = false; } + } - if (_dict.Options.Examples?.Value != options.Examples?.Value) - { - _dict.Contents = FrozenDictionary>.Empty; + DictOptions options = _dictOptionsControl.GetDictOptions(_dict.Type); + if (_dict.Type is DictType.PitchAccentYomichan) + { + bool oldDottedLinesOption = _dict.Options.ShowPitchAccentWithDottedLines!.Value; + bool newDottedLinesOption = options.ShowPitchAccentWithDottedLines!.Value; - if (dbExists) - { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); - dbExists = false; - } + if (oldDottedLinesOption != newDottedLinesOption) + { + PopupWindowUtils.SetStrokeDashArray(newDottedLinesOption); } + } + + if (_dict.Options.Examples?.Value != options.Examples?.Value) + { + _dict.Contents = FrozenDictionary>.Empty; - if (_dict.Options.UseDB.Value != options.UseDB.Value) + if (dbExists) { - _dict.Ready = false; - //if (dbExists && !(options.UseDB?.Value ?? false)) - //{ - // DBUtils.SendOptimizePragmaToAllDBs(); - // SqliteConnection.ClearAllPools(); - // File.Delete(dbPath); - // dbExists = false; - //} + DBUtils.SendOptimizePragmaToAllDBs(); + SqliteConnection.ClearAllPools(); + File.Delete(dbPath); + dbExists = false; } + } - if (_dict.Name != name) - { - if (dbExists) - { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Move(dbPath, DBUtils.GetDictDBPath(name)); - } + if (_dict.Options.UseDB.Value != options.UseDB.Value) + { + _dict.Ready = false; + //if (dbExists && !(options.UseDB?.Value ?? false)) + //{ + // DBUtils.SendOptimizePragmaToAllDBs(); + // SqliteConnection.ClearAllPools(); + // File.Delete(dbPath); + // dbExists = false; + //} + } - _ = DictUtils.Dicts.Remove(_dict.Name); - _dict.Name = name; - DictUtils.Dicts.Add(name, _dict); + if (_dict.Name != name) + { + if (dbExists) + { + DBUtils.SendOptimizePragmaToAllDBs(); + SqliteConnection.ClearAllPools(); + File.Move(dbPath, DBUtils.GetDictDBPath(name)); } - _dict.Options = options; - - Close(); + _ = DictUtils.Dicts.Remove(_dict.Name); + _dict.Name = name; + DictUtils.Dicts.Add(name, _dict); } + + _dict.Options = options; + + Close(); } private void BrowseForDictionaryFile(string filter) diff --git a/JL.Windows/GUI/EditFrequencyWindow.xaml.cs b/JL.Windows/GUI/EditFrequencyWindow.xaml.cs index f0e50835..5a7459b8 100644 --- a/JL.Windows/GUI/EditFrequencyWindow.xaml.cs +++ b/JL.Windows/GUI/EditFrequencyWindow.xaml.cs @@ -35,7 +35,8 @@ private void CancelButton_Click(object sender, RoutedEventArgs e) private void SaveButton_Click(object sender, RoutedEventArgs e) { - bool isValid = true; + TextBlockPath.ClearValue(BorderBrushProperty); + NameTextBox.ClearValue(BorderBrushProperty); string path = TextBlockPath.Text; string fullPath = Path.GetFullPath(path, Utils.ApplicationPath); @@ -45,11 +46,7 @@ private void SaveButton_Click(object sender, RoutedEventArgs e) && (!Path.Exists(fullPath) || FreqUtils.FreqDicts.Values.Any(dict => dict.Path == path)))) { TextBlockPath.BorderBrush = Brushes.Red; - isValid = false; - } - else if (TextBlockPath.BorderBrush == Brushes.Red) - { - TextBlockPath.ClearValue(BorderBrushProperty); + return; } string name = NameTextBox.Text; @@ -59,65 +56,68 @@ private void SaveButton_Click(object sender, RoutedEventArgs e) || (!_freq.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && FreqUtils.FreqDicts.ContainsKey(name))) { NameTextBox.BorderBrush = Brushes.Red; - isValid = false; - } - else if (NameTextBox.BorderBrush == Brushes.Red) - { - NameTextBox.ClearValue(BorderBrushProperty); + return; } - if (isValid) - { - string dbPath = DBUtils.GetFreqDBPath(_freq.Name); - bool dbExists = File.Exists(dbPath); + string dbPath = DBUtils.GetFreqDBPath(_freq.Name); + bool dbExists = File.Exists(dbPath); - if (_freq.Path != path) + if (_freq.Path != path) + { + if (_freq.Type is FreqType.Yomichan or FreqType.YomichanKanji) { - _freq.Path = path; - _freq.Contents = FrozenDictionary>.Empty; - _freq.Ready = false; - - if (dbExists) + bool hasValidFiles = Directory.EnumerateFiles(fullPath, "*_bank_*.json", SearchOption.TopDirectoryOnly).Any(); + if (!hasValidFiles) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); - dbExists = false; + TextBlockPath.BorderBrush = Brushes.Red; + return; } } - FreqOptions options = _freqOptionsControl.GetFreqOptions(_freq.Type); + _freq.Path = path; + _freq.Contents = FrozenDictionary>.Empty; + _freq.Ready = false; - if (_freq.Options.UseDB.Value != options.UseDB.Value) + if (dbExists) { - _freq.Ready = false; - //if (dbExists && !(options.UseDB?.Value ?? false)) - //{ - // DBUtils.SendOptimizePragmaToAllDBs(); - // SqliteConnection.ClearAllPools(); - // File.Delete(dbPath); - // dbExists = false; - //} + DBUtils.SendOptimizePragmaToAllDBs(); + SqliteConnection.ClearAllPools(); + File.Delete(dbPath); + dbExists = false; } + } - if (_freq.Name != name) - { - if (dbExists) - { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Move(dbPath, DBUtils.GetFreqDBPath(name)); - } + FreqOptions options = _freqOptionsControl.GetFreqOptions(_freq.Type); - _ = FreqUtils.FreqDicts.Remove(_freq.Name); - _freq.Name = name; - FreqUtils.FreqDicts.Add(name, _freq); - } + if (_freq.Options.UseDB.Value != options.UseDB.Value) + { + _freq.Ready = false; + //if (dbExists && !(options.UseDB?.Value ?? false)) + //{ + // DBUtils.SendOptimizePragmaToAllDBs(); + // SqliteConnection.ClearAllPools(); + // File.Delete(dbPath); + // dbExists = false; + //} + } - _freq.Options = options; + if (_freq.Name != name) + { + if (dbExists) + { + DBUtils.SendOptimizePragmaToAllDBs(); + SqliteConnection.ClearAllPools(); + File.Move(dbPath, DBUtils.GetFreqDBPath(name)); + } - Close(); + _ = FreqUtils.FreqDicts.Remove(_freq.Name); + _freq.Name = name; + FreqUtils.FreqDicts.Add(name, _freq); } + + _freq.Options = options; + + Close(); } private void BrowseForFrequencyFile(string filter)