mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-08 17:35:18 +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
@@ -46,19 +46,6 @@ impl Database {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_yomitan_dbfile_init(&self) -> Result<bool> {
|
||||
let yomitan_db_path = self.dir.join(YOMITAN_DBFILE);
|
||||
|
||||
if yomitan_db_path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let conn = Connection::open(yomitan_db_path)?;
|
||||
let mut stmt = conn.prepare("SELECT 1 FROM dictionaries")?;
|
||||
|
||||
Ok(stmt.exists([])?)
|
||||
}
|
||||
|
||||
pub fn yomitan(&self) -> Result<yomitan::YomitanDatabase> {
|
||||
self.ensure_yomitan_attached()?;
|
||||
Ok(yomitan::YomitanDatabase::new(self.clone()))
|
||||
|
||||
@@ -19,7 +19,7 @@ pub(super) fn normalize_term(s: &str) -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct YomitanZipImportProgress {
|
||||
pub message: String,
|
||||
pub current: u32,
|
||||
@@ -69,6 +69,7 @@ impl YomitanWriter {
|
||||
description TEXT,
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
installed_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
bundle_name TEXT, -- custom
|
||||
lang TEXT NOT NULL, -- custom
|
||||
UNIQUE (title, revision)
|
||||
);
|
||||
@@ -126,7 +127,7 @@ impl YomitanWriter {
|
||||
|
||||
self.conn.execute_batch(
|
||||
"
|
||||
CREATE INDEX IF NOT EXISTS dictionaries_sort_order ON dctionaries (sort_order DESC);
|
||||
CREATE INDEX IF NOT EXISTS dictionaries_sort_order ON dictionaries (sort_order DESC);
|
||||
CREATE INDEX IF NOT EXISTS dictionaries_lang ON dictionaries (lang);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS terms_dict_id ON terms (dict_id);
|
||||
@@ -246,7 +247,13 @@ impl YomitanWriter {
|
||||
where
|
||||
Callback: Fn(YomitanZipImportProgress),
|
||||
{
|
||||
self.create_schema()?; // < 1 ms
|
||||
let bundle_name = if let Some(fo) = zip_file.file_name()
|
||||
&& let Some(f) = fo.to_str()
|
||||
{
|
||||
f
|
||||
} else {
|
||||
return Err(CJDicError::FileNameNotFound);
|
||||
};
|
||||
|
||||
let start_time = Instant::now();
|
||||
self.conn.execute_batch("PRAGMA foreign_keys = off")?;
|
||||
@@ -274,7 +281,7 @@ impl YomitanWriter {
|
||||
|
||||
let mut steps: u64 = 0;
|
||||
progress_callback(YomitanZipImportProgress {
|
||||
message: format!("Extracted {}", zip_file.display()),
|
||||
message: format!("Opening {}", bundle_name),
|
||||
current: 0,
|
||||
total: 0,
|
||||
steps,
|
||||
@@ -327,7 +334,9 @@ impl YomitanWriter {
|
||||
|
||||
let tx = self.conn.transaction()?;
|
||||
tx.execute(
|
||||
"INSERT INTO dictionaries (title, revision, author, url, description, lang) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||
"INSERT INTO dictionaries
|
||||
(title, revision, author, url, description, lang, bundle_name) VALUES
|
||||
( ?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
||||
params![
|
||||
title,
|
||||
revision,
|
||||
@@ -335,6 +344,7 @@ impl YomitanWriter {
|
||||
index_file.get("url").and_then(Value::as_str),
|
||||
index_file.get("description").and_then(Value::as_str),
|
||||
lang,
|
||||
bundle_name,
|
||||
],
|
||||
)?;
|
||||
|
||||
@@ -391,7 +401,7 @@ impl YomitanWriter {
|
||||
|
||||
steps += entries.len() as u64;
|
||||
progress_callback(YomitanZipImportProgress {
|
||||
message: format!("Reading term bank {} of {}", bank_i, n_bank),
|
||||
message: format!("Reading term banks"),
|
||||
current: bank_i,
|
||||
total: n_bank,
|
||||
steps,
|
||||
@@ -492,7 +502,7 @@ impl YomitanWriter {
|
||||
|
||||
steps += 1;
|
||||
progress_callback(YomitanZipImportProgress {
|
||||
message: format!("Reading term meta bank"),
|
||||
message: format!("Reading term meta banks"),
|
||||
current: 0,
|
||||
total: 0,
|
||||
steps,
|
||||
@@ -529,7 +539,7 @@ impl YomitanWriter {
|
||||
|
||||
steps += 1;
|
||||
progress_callback(YomitanZipImportProgress {
|
||||
message: format!("Reading tag bank"),
|
||||
message: format!("Reading tag banks"),
|
||||
current: 0,
|
||||
total: 0,
|
||||
steps,
|
||||
|
||||
@@ -15,6 +15,9 @@ pub enum CJDicError {
|
||||
#[error("IO error: {0}")]
|
||||
IOError(#[from] std::io::Error),
|
||||
|
||||
#[error("File name not found")]
|
||||
FileNameNotFound,
|
||||
|
||||
#[error("Zip error: {0}")]
|
||||
ZipError(#[from] ZipError),
|
||||
|
||||
|
||||
+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