mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-09 17:39:35 +00:00
yomitan init on UI
This commit is contained in:
1 parent
3b5afbc34e
commit
ff64c035e6
8 files changed
+245
-46
No files matched your search
+111
-5
@@ -1,6 +1,11 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs::read_dir,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use rusqlite::Connection;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
db::{
|
||||
@@ -10,6 +15,12 @@ use crate::{
|
||||
error::CJDicError,
|
||||
};
|
||||
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct LoadYomitanZipDirResult {
|
||||
pub new_dicts: Vec<String>,
|
||||
pub to_be_removed_dicts: Vec<String>,
|
||||
}
|
||||
|
||||
pub struct AppService {
|
||||
db: Database,
|
||||
}
|
||||
@@ -20,10 +31,6 @@ impl AppService {
|
||||
Ok(Self { db })
|
||||
}
|
||||
|
||||
pub fn is_yomitan_setup_yet(&self) -> Result<bool, CJDicError> {
|
||||
Ok(self.db.is_yomitan_dbfile_init()?)
|
||||
}
|
||||
|
||||
pub fn search_yomitan(
|
||||
&self,
|
||||
q_term: &str,
|
||||
@@ -44,6 +51,105 @@ impl AppService {
|
||||
Ok(writer)
|
||||
}
|
||||
|
||||
pub fn load_yomitan_zip_dir<LoadCallback, ImportCallback>(
|
||||
&self,
|
||||
zip_dir: PathBuf,
|
||||
lang: &str,
|
||||
load_callback: LoadCallback,
|
||||
import_callback: ImportCallback,
|
||||
) -> Result<(), CJDicError>
|
||||
where
|
||||
LoadCallback: Fn(LoadYomitanZipDirResult),
|
||||
ImportCallback: Fn(YomitanZipImportProgress),
|
||||
{
|
||||
let mut dir_zip_list: HashSet<String> = HashSet::new();
|
||||
|
||||
for entry in read_dir(&zip_dir)? {
|
||||
let e = entry?;
|
||||
let p = e.path();
|
||||
|
||||
if p.extension().and_then(|s| s.to_str()) == Some("zip")
|
||||
&& let Some(f) = e.file_name().to_str()
|
||||
{
|
||||
dir_zip_list.insert(f.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let conn = Connection::open(self.db.dir.join(YOMITAN_DBFILE))?;
|
||||
let mut writer = YomitanWriter::new(conn)?;
|
||||
writer.create_schema()?;
|
||||
}
|
||||
|
||||
let mut new_dicts: Vec<String> = vec![];
|
||||
let mut to_be_removed_dicts: Vec<String> = vec![];
|
||||
|
||||
{
|
||||
let conn = Connection::open(self.db.dir.join(YOMITAN_DBFILE))?;
|
||||
let mut stmt =
|
||||
conn.prepare("SELECT DISTINCT bundle_name FROM dictionaries WHERE lang = ?1")?;
|
||||
let mut rows = stmt.query([lang])?;
|
||||
|
||||
let mut db_zip_list: HashSet<String> = HashSet::new();
|
||||
while let Some(row) = rows.next()? {
|
||||
let b_name: String = row.get(0)?;
|
||||
db_zip_list.insert(b_name);
|
||||
}
|
||||
|
||||
for z in dir_zip_list.iter() {
|
||||
if !db_zip_list.contains(z) {
|
||||
new_dicts.push(z.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
for z in db_zip_list.iter() {
|
||||
if !dir_zip_list.contains(z) {
|
||||
to_be_removed_dicts.push(z.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Import ordering
|
||||
new_dicts.sort_by_key(|s| {
|
||||
if s.starts_with("[")
|
||||
&& let Some(end_i) = s.find(']')
|
||||
{
|
||||
s[(end_i + 1)..].trim_start().to_string()
|
||||
} else {
|
||||
s.to_string()
|
||||
}
|
||||
});
|
||||
to_be_removed_dicts.sort();
|
||||
|
||||
load_callback(LoadYomitanZipDirResult {
|
||||
new_dicts: new_dicts.clone(),
|
||||
to_be_removed_dicts: to_be_removed_dicts.clone(),
|
||||
});
|
||||
|
||||
{
|
||||
let conn = Connection::open(self.db.dir.join(YOMITAN_DBFILE))?;
|
||||
for z in to_be_removed_dicts.iter() {
|
||||
conn.execute("DELETE FROM dictionaries WHERE bundle_name = ?1", [z])?;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let conn = Connection::open(self.db.dir.join(YOMITAN_DBFILE))?;
|
||||
let mut writer = YomitanWriter::new(conn)?;
|
||||
|
||||
for z in new_dicts.iter() {
|
||||
Self::import_yomitan_zip_file(
|
||||
&mut writer,
|
||||
zip_dir.join(z),
|
||||
lang,
|
||||
&import_callback,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn import_yomitan_zip_file<Callback>(
|
||||
writer: &mut YomitanWriter,
|
||||
zip_file: PathBuf,
|
||||
|
||||
Reference in new issue
Block a user