plugin_framework, handle plugin list

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-05-09 23:30:15 +08:00
parent 4eb6bd82a4
commit b06fad0e43
4 changed files with 57 additions and 33 deletions

View File

@ -231,7 +231,6 @@ class FfiModel with ChangeNotifier {
} else if (name == 'fingerprint') { } else if (name == 'fingerprint') {
FingerprintState.find(peerId).value = evt['fingerprint'] ?? ''; FingerprintState.find(peerId).value = evt['fingerprint'] ?? '';
} else if (name == 'plugin_manager') { } else if (name == 'plugin_manager') {
debugPrint('REMOVE ME ==================== plugin_manager $evt');
pluginManager.handleEvent(evt); pluginManager.handleEvent(evt);
} else if (name == 'plugin_event') { } else if (name == 'plugin_event') {
handlePluginEvent( handlePluginEvent(

View File

@ -223,7 +223,7 @@ class PluginManager with ChangeNotifier {
_plugins.add(plugin); _plugins.add(plugin);
} }
} catch (e) { } catch (e) {
debugPrint('Failed to decode plugin list \'$pluginList\''); debugPrint('Failed to decode $e, plugin list \'$pluginList\'');
} }
notifyListeners(); notifyListeners();
} }
@ -245,6 +245,22 @@ class PluginManager with ChangeNotifier {
if (m == null) { if (m == null) {
return null; return null;
} }
late DateTime lastReleased;
late DateTime published;
try {
lastReleased = DateTime.parse(
m['publish_info']?['last_released'] ?? '1970-01-01T00+00:00');
} catch (e) {
lastReleased = DateTime.utc(1970);
}
try {
published = DateTime.parse(
m['publish_info']?['published'] ?? '1970-01-01T00+00:00');
} catch (e) {
published = DateTime.utc(1970);
}
final meta = Meta( final meta = Meta(
id: m['id'], id: m['id'],
name: m['name'], name: m['name'],
@ -254,17 +270,22 @@ class PluginManager with ChangeNotifier {
home: m['home'] ?? '', home: m['home'] ?? '',
license: m['license'] ?? '', license: m['license'] ?? '',
source: m['source'] ?? '', source: m['source'] ?? '',
publishInfo: PublishInfo( publishInfo:
lastReleased: DateTime.parse( PublishInfo(lastReleased: lastReleased, published: published),
m['publish_info']?['lastReleased'] ?? '1970-01-01T00+00:00'),
published: DateTime.parse(
m['publish_info']?['published'] ?? '1970-01-01T00+00:00')),
); );
late DateTime installTime;
try {
installTime =
DateTime.parse(evt['install_time'] ?? '1970-01-01T00+00:00');
} catch (e) {
installTime = DateTime.utc(1970);
}
return PluginInfo( return PluginInfo(
sourceInfo: source, sourceInfo: source,
meta: meta, meta: meta,
installedVersion: evt['installed_version'], installedVersion: evt['installed_version'],
installTime: evt['install_time'], installTime: installTime,
invalidReason: evt['invalid_reason'] ?? '', invalidReason: evt['invalid_reason'] ?? '',
); );
} }

View File

@ -2,8 +2,10 @@
// 2. Install or uninstall. // 2. Install or uninstall.
use super::{desc::Meta as PluginMeta, ipc::InstallStatus, *}; use super::{desc::Meta as PluginMeta, ipc::InstallStatus, *};
use crate::{common::is_server, flutter}; use crate::flutter;
use hbb_common::{allow_err, bail, config::load_path, log, tokio, toml}; #[cfg(not(debug_assertions))]
use hbb_common::toml;
use hbb_common::{allow_err, bail, config::load_path, log, tokio};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use serde_json; use serde_json;
use std::{ use std::{
@ -38,7 +40,7 @@ pub struct PluginSource {
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct PluginInfo { pub struct PluginInfo {
pub source: PluginSource, pub source: PluginSource,
pub plugin: PluginMeta, pub meta: PluginMeta,
pub installed_version: String, pub installed_version: String,
pub install_time: String, pub install_time: String,
pub invalid_reason: String, pub invalid_reason: String,
@ -60,18 +62,18 @@ fn get_plugin_source_list() -> Vec<PluginSource> {
fn get_source_plugins() -> HashMap<String, PluginInfo> { fn get_source_plugins() -> HashMap<String, PluginInfo> {
let meta_file = super::get_plugins_dir().unwrap().join("meta.toml"); let meta_file = super::get_plugins_dir().unwrap().join("meta.toml");
let mut plugins = HashMap::new(); let mut plugins = HashMap::new();
let meta = load_path::<ManagerMeta>(meta_file); let manager_meta = load_path::<ManagerMeta>(meta_file);
let source = PluginSource { let source = PluginSource {
name: "rustdesk".to_string(), name: "rustdesk".to_string(),
url: "https://github.com/fufesou/rustdesk-plugins".to_string(), url: "https://github.com/fufesou/rustdesk-plugins".to_string(),
description: "".to_string(), description: "".to_string(),
}; };
for plugin in meta.plugins.iter() { for meta in manager_meta.plugins.iter() {
plugins.insert( plugins.insert(
plugin.id.clone(), meta.id.clone(),
PluginInfo { PluginInfo {
source: source.clone(), source: source.clone(),
plugin: plugin.clone(), meta: meta.clone(),
installed_version: "".to_string(), installed_version: "".to_string(),
install_time: "".to_string(), install_time: "".to_string(),
invalid_reason: "".to_string(), invalid_reason: "".to_string(),
@ -95,22 +97,24 @@ fn get_source_plugins() -> HashMap<String, PluginInfo> {
resp.status() resp.status()
); );
} }
match resp.json::<ManagerMeta>() { if let Ok(text) = resp.text() {
Ok(meta) => { match toml::from_str::<ManagerMeta>(&text) {
for plugin in meta.plugins.iter() { Ok(manager_meta) => {
plugins.insert( for meta in manager_meta.plugins.iter() {
plugin.id.clone(), plugins.insert(
PluginInfo { meta.id.clone(),
source: source.clone(), PluginInfo {
plugin: plugin.clone(), source: source.clone(),
installed_version: "".to_string(), meta: meta.clone(),
install_time: "".to_string(), installed_version: "".to_string(),
invalid_reason: "".to_string(), install_time: "".to_string(),
}, invalid_reason: "".to_string(),
); },
);
}
} }
Err(e) => log::error!("Failed to parse plugin list from '{}', {}", url, e),
} }
Err(e) => log::error!("Failed to parse plugin list from '{}', {}", url, e),
} }
} }
Err(e) => log::error!("Failed to get plugin list from '{}', {}", url, e), Err(e) => log::error!("Failed to get plugin list from '{}', {}", url, e),
@ -121,7 +125,7 @@ fn get_source_plugins() -> HashMap<String, PluginInfo> {
fn send_plugin_list_event(plugins: &HashMap<String, PluginInfo>) { fn send_plugin_list_event(plugins: &HashMap<String, PluginInfo>) {
let mut plugin_list = plugins.values().collect::<Vec<_>>(); let mut plugin_list = plugins.values().collect::<Vec<_>>();
plugin_list.sort_by(|a, b| a.plugin.name.cmp(&b.plugin.name)); plugin_list.sort_by(|a, b| a.meta.name.cmp(&b.meta.name));
if let Ok(plugin_list) = serde_json::to_string(&plugin_list) { if let Ok(plugin_list) = serde_json::to_string(&plugin_list) {
let mut m = HashMap::new(); let mut m = HashMap::new();
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_MANAGER); m.insert("name", MSG_TO_UI_TYPE_PLUGIN_MANAGER);
@ -148,7 +152,7 @@ pub fn load_plugin_list() {
url: PLUGIN_SOURCE_LOCAL_DIR.to_string(), url: PLUGIN_SOURCE_LOCAL_DIR.to_string(),
description: "".to_string(), description: "".to_string(),
}, },
plugin: info.desc.meta().clone(), meta: info.desc.meta().clone(),
installed_version: info.desc.meta().version.clone(), installed_version: info.desc.meta().version.clone(),
install_time: info.install_time.clone(), install_time: info.install_time.clone(),
invalid_reason: "".to_string(), invalid_reason: "".to_string(),
@ -165,7 +169,7 @@ pub fn install_plugin(id: &str) -> ResultType<()> {
Some(plugin) => { Some(plugin) => {
let _plugin_url = format!( let _plugin_url = format!(
"{}/plugins/{}/{}_{}.zip", "{}/plugins/{}/{}_{}.zip",
plugin.source.url, plugin.plugin.id, plugin.plugin.id, plugin.plugin.version plugin.source.url, plugin.meta.id, plugin.meta.id, plugin.meta.version
); );
#[cfg(windows)] #[cfg(windows)]
let _res = let _res =

View File

@ -1,4 +1,4 @@
use hbb_common::{libc, tokio, ResultType, allow_err, log}; use hbb_common::{allow_err, libc, log, ResultType};
use std::{ use std::{
env, env,
ffi::{c_char, c_int, c_void, CStr}, ffi::{c_char, c_int, c_void, CStr},