From 230c3ae1f733e970dedd5e79bd390e5d7e953c26 Mon Sep 17 00:00:00 2001 From: ikey4u Date: Sat, 19 Feb 2022 21:31:05 +0800 Subject: [PATCH] feat: remove dictionary relay and add human-friendly dictionary name in wikit desktop app --- cli/Cargo.toml | 2 +- core/src/config.rs | 15 +++++----- core/src/router.rs | 54 +++++++++++++---------------------- core/src/wikit.rs | 1 + desktop/src-tauri/Cargo.toml | 2 +- desktop/src-tauri/src/main.rs | 12 +++----- desktop/src/App.svelte | 14 +++++++-- desktop/src/Settings.svelte | 7 ++--- desktop/src/store.js | 2 ++ 9 files changed, 51 insertions(+), 58 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index f4ca128..2ff66da 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wikit" -version = "0.3.1" +version = "0.3.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/core/src/config.rs b/core/src/config.rs index 2d7b097..8679e7e 100644 --- a/core/src/config.rs +++ b/core/src/config.rs @@ -11,26 +11,27 @@ /// [srvcfg] /// uris = [ /// "file://", -/// "https://[user@token]", -/// "http://[user@token]", /// ] /// port = 8888 /// host = "0.0.0.0" /// -/// `[cltcfg]` is used for wikit desktop client, and `[srvcfg]` is used for serving dictionary. +/// `[cltcfg]` is used for wikit desktop client, and `[srvcfg]` is used for serving dictionaries. /// /// `uris` are a list of [URI](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier) which -/// refers directory resource path, supported URI are +/// refers directory resource path, supported URIs are /// -/// - file:// +/// - `file://` /// /// This refers your local file on your system, such as `file:///home/user/Downloads/awesome.wikit` /// where `/home/user/Downloads/awesome.wikit` is the full path to your dictionary /// `awesome.wikit` on your system. /// -/// - https:// and http:// +/// - `https://` and `http://` +/// +/// Wikit allows client access remote hosted dictionary by network. However, this type URI is +/// only allowed in `[cltcfg]` section, if them does appear in `[srvcfg]`, then will be ignored. /// -/// wikit allows you access remote hosted dictionary by network. +/// Only IP address is allowed for now, domain will be added in future. /// /// Assuming the base url of the remote server is `https://example.com`, then there must exist following API /// diff --git a/core/src/router.rs b/core/src/router.rs index d0c6d6f..b44bb31 100644 --- a/core/src/router.rs +++ b/core/src/router.rs @@ -1,16 +1,14 @@ use crate::config; use crate::wikit; use crate::crypto; +use crate::util; use std::net::{IpAddr, Ipv4Addr}; use std::{sync::Mutex, collections::HashMap}; -use std::path::Path; use std::sync::Arc; -use sqlx::postgres::PgPoolOptions; -use rocket::{Build, Request, response::content, catch, get, catchers, routes}; -use regex::Regex; -use rocket::serde::{Serialize, Deserialize, json::Json}; +use rocket::{Build, Request, catch, get, catchers, routes}; +use rocket::serde::json::Json; use once_cell::sync::Lazy; pub static DICTMP: Lazy>>> = Lazy::new(|| { @@ -30,47 +28,35 @@ fn not_found(req: &Request) -> String { #[get("/list")] async fn list() -> Json> { - let mut dict = vec![]; + let mut dictlist = vec![]; if let Ok(config) = config::load_config() { for uri in config.srvcfg.uris.iter() { let dictid = crypto::md5(uri.as_bytes()); if let Some(dict) = wikit::load_dictionary_from_uri(uri) { let style_key = format!("style[{}]", dictid); let script_key = format!("script[{}]", dictid); - match dict { - wikit::WikitDictionary::Local(d) => { - if let Ok(mut dictmp) = DICTMP.lock() { - if dictmp.get(&style_key).is_none() { - dictmp.insert(style_key, d.head.style.clone()); - } - if dictmp.get(&script_key).is_none() { - dictmp.insert(script_key, d.head.script.clone()); - } + if let wikit::WikitDictionary::Local(d) = dict { + if let Ok(mut dictmp) = DICTMP.lock() { + if dictmp.get(&style_key).is_none() { + dictmp.insert(style_key, d.head.style.clone()); } - }, - wikit::WikitDictionary::Remote(d) => { - if let Ok(mut dictmp) = DICTMP.lock() { - if dictmp.get(&style_key).is_none() { - dictmp.insert(style_key, d.get_style(&dictid)); - } - if dictmp.get(&script_key).is_none() { - dictmp.insert(script_key, d.get_script(&dictid)); - } + if dictmp.get(&script_key).is_none() { + dictmp.insert(script_key, d.head.script.clone()); } - }, + } + if let Ok(mut dictmp) = DICTMP.lock() { + dictmp.insert(dictid.clone(), uri.to_string()); + } + + dictlist.push(wikit::DictList { + name: d.head.name, + id: dictid.clone(), + }); } } - if let Ok(mut dictmp) = DICTMP.lock() { - dictmp.insert(dictid.clone(), uri.to_string()); - } - dict.push(wikit::DictList { - // FIXME(2021-11-21): give a nice dictionar name - name: dictid.clone(), - id: dictid.clone(), - }); } } - Json(dict) + Json(dictlist) } #[get("/style?")] diff --git a/core/src/wikit.rs b/core/src/wikit.rs index fbb5f07..fedc4f9 100644 --- a/core/src/wikit.rs +++ b/core/src/wikit.rs @@ -21,6 +21,7 @@ const WIKIT_MAGIC: &'static str = "WIKIT516"; // the latest wikit dictionary format version const LATEST_WIKIT_FMT_VERSION: u32 = 0x00_00_00_01; +// TODO(2022-02-19): select a better type name #[derive(Debug, Serialize, Deserialize)] pub struct DictList { pub name: String, diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml index cfefeec..df67ce0 100644 --- a/desktop/src-tauri/Cargo.toml +++ b/desktop/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wikit-desktop" -version = "0.1.0-beta.2" +version = "0.1.0-beta.3" description = "Wikit Desktop - A universal dictionary for human" authors = ["ikey4u "] license = "MIT" diff --git a/desktop/src-tauri/src/main.rs b/desktop/src-tauri/src/main.rs index bea2bdb..7dbf9de 100644 --- a/desktop/src-tauri/src/main.rs +++ b/desktop/src-tauri/src/main.rs @@ -69,20 +69,22 @@ fn lookup(dictid: String, word: String) -> LookupResponse { } #[tauri::command] -fn get_dict_list() -> Vec { +fn get_dict_list() -> Vec { + let mut dictlist = vec![]; let mut dictdb = DICTDB.lock().unwrap(); - if let Ok(dicts) = wikit::load_client_dictionary() { for dict in dicts { match dict { wikit::WikitDictionary::Local(ref ld) => { let id = format!("{}", ld.path.display()); + dictlist.push(wikit::DictList{ name: ld.head.name.clone(), id: id.clone() }); dictdb.insert(id.clone(), dict); }, wikit::WikitDictionary::Remote(ref rd) => { if let Ok(ds) = rd.get_dict_list() { for d in ds { dictdb.insert(d.id.clone(), dict.clone()); + dictlist.push(d); } } else { println!("failed to get remote dictionary list"); @@ -91,12 +93,6 @@ fn get_dict_list() -> Vec { } } } - - let mut dictlist = vec![]; - for (k, _) in dictdb.iter() { - dictlist.push(k.to_string()); - } - dictlist } diff --git a/desktop/src/App.svelte b/desktop/src/App.svelte index 51fb340..1562238 100644 --- a/desktop/src/App.svelte +++ b/desktop/src/App.svelte @@ -19,7 +19,7 @@ let r = await ffi.get_dict_list(); $dictSettings.dict.all = r; if (r.length > 0) { - $dictSettings.dict.selected = [r[0]]; + $dictSettings.dict.selected = [r[0].id]; } unlisten = listen("rust-event", (e) => { @@ -48,8 +48,18 @@ return; } + let dictid = $dictSettings.dict.selected[0]; + if (!dictid) { + if ($dictSettings.dict.all.length > 0) { + dictid = $dictSettings.dict.all[0].id + } else { + console.log("no dictionary is found, stop lookup"); + return; + } + } + input = input.trim().toLowerCase(); - let resp = await ffi.lookup($dictSettings.dict.selected[0], input); + let resp = await ffi.lookup(dictid, input); let meanings = resp["words"]; let meaning = meanings[input] if (!meaning) { diff --git a/desktop/src/Settings.svelte b/desktop/src/Settings.svelte index 18efe4c..9fc5f85 100644 --- a/desktop/src/Settings.svelte +++ b/desktop/src/Settings.svelte @@ -32,9 +32,9 @@ {#each $dictSettings.dict.all as dict, i} - + - {dict} + {dict.name} {/each} @@ -56,9 +56,6 @@ async () => { let r = await ffi.get_dict_list(); $dictSettings.dict.all = r; - if (r.length > 0) { - $dictSettings.dict.selected = [r[0]]; - } open = true } }>+ diff --git a/desktop/src/store.js b/desktop/src/store.js index b61ec5c..442682d 100644 --- a/desktop/src/store.js +++ b/desktop/src/store.js @@ -2,7 +2,9 @@ import { writable } from 'svelte/store'; export const dictSettings = writable({ 'dict': { + // array of dictionary, element format is `{ "id:: "", "name": "dictionary name for human" }` 'all': [], + // selected dictionary ID 'selected': [], }, });