|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.ObjectModel; |
|
|
using System.Linq; |
|
|
using System.Threading.Tasks; |
|
|
using Mdict.Mdx.Readers; |
|
|
using Mdx.Storage; |
|
|
|
|
|
namespace mdict.Services |
|
|
{ |
|
|
public class DictionaryItem |
|
|
{ |
|
|
public string FilePath { get; set; } |
|
|
public string Name { get; set; } |
|
|
public MdxReader Reader { get; set; } |
|
|
} |
|
|
|
|
|
public class LibraryManager : IDisposable |
|
|
{ |
|
|
private static LibraryManager _instance; |
|
|
public static LibraryManager Instance => _instance ??= new LibraryManager(); |
|
|
|
|
|
public ObservableCollection<DictionaryItem> LoadedDictionaries { get; } = new ObservableCollection<DictionaryItem>(); |
|
|
|
|
|
public void AddDictionary(string path) |
|
|
{ |
|
|
if (LoadedDictionaries.Any(d => d.FilePath.Equals(path, StringComparison.OrdinalIgnoreCase))) |
|
|
return; |
|
|
|
|
|
try |
|
|
{ |
|
|
var reader = new MdxReader(path); |
|
|
var info = reader.ContentDb.Meta.DbInfo; |
|
|
var name = !string.IsNullOrEmpty(info.Title) ? info.Title : System.IO.Path.GetFileNameWithoutExtension(path); |
|
|
|
|
|
LoadedDictionaries.Add(new DictionaryItem |
|
|
{ |
|
|
FilePath = path, |
|
|
Name = name, |
|
|
Reader = reader |
|
|
}); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
|
|
|
Console.WriteLine($"Failed to load dictionary {path}: {ex.Message}"); |
|
|
throw; |
|
|
} |
|
|
} |
|
|
|
|
|
public void RemoveDictionary(DictionaryItem item) |
|
|
{ |
|
|
if (LoadedDictionaries.Contains(item)) |
|
|
{ |
|
|
item.Reader?.Dispose(); |
|
|
LoadedDictionaries.Remove(item); |
|
|
} |
|
|
} |
|
|
|
|
|
public List<(DictionaryItem Dictionary, KeyIndex Index)> Search(string query) |
|
|
{ |
|
|
var results = new List<(DictionaryItem, KeyIndex)>(); |
|
|
|
|
|
foreach (var dict in LoadedDictionaries) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
var index = dict.Reader.Lookup(query); |
|
|
if (index != null) |
|
|
{ |
|
|
results.Add((dict, index)); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
Console.WriteLine($"Error searching in {dict.Name}: {ex.Message}"); |
|
|
} |
|
|
} |
|
|
|
|
|
return results; |
|
|
} |
|
|
|
|
|
public List<string> GetSuggestions(string prefix, int maxCount = 20) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new List<string>(); |
|
|
} |
|
|
|
|
|
public string? GetHtml(string key) |
|
|
{ |
|
|
foreach (var dict in LoadedDictionaries) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
var index = dict.Reader.Lookup(key); |
|
|
if (index != null) |
|
|
{ |
|
|
return dict.Reader.GetHtml(index); |
|
|
} |
|
|
} |
|
|
catch { } |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
public byte[]? GetResource(string key) |
|
|
{ |
|
|
foreach (var dict in LoadedDictionaries) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
var data = dict.Reader.GetResource(key); |
|
|
if (data != null) |
|
|
{ |
|
|
return data; |
|
|
} |
|
|
} |
|
|
catch { } |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
public void Dispose() |
|
|
{ |
|
|
foreach (var dict in LoadedDictionaries) |
|
|
{ |
|
|
dict.Reader?.Dispose(); |
|
|
} |
|
|
LoadedDictionaries.Clear(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|