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') {
FingerprintState.find(peerId).value = evt['fingerprint'] ?? '';
} else if (name == 'plugin_manager') {
debugPrint('REMOVE ME ==================== plugin_manager $evt');
pluginManager.handleEvent(evt);
} else if (name == 'plugin_event') {
handlePluginEvent(

View File

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

View File

@ -2,8 +2,10 @@
// 2. Install or uninstall.
use super::{desc::Meta as PluginMeta, ipc::InstallStatus, *};
use crate::{common::is_server, flutter};
use hbb_common::{allow_err, bail, config::load_path, log, tokio, toml};
use crate::flutter;
#[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_json;
use std::{
@ -38,7 +40,7 @@ pub struct PluginSource {
#[derive(Debug, Serialize)]
pub struct PluginInfo {
pub source: PluginSource,
pub plugin: PluginMeta,
pub meta: PluginMeta,
pub installed_version: String,
pub install_time: String,
pub invalid_reason: String,
@ -60,18 +62,18 @@ fn get_plugin_source_list() -> Vec<PluginSource> {
fn get_source_plugins() -> HashMap<String, PluginInfo> {
let meta_file = super::get_plugins_dir().unwrap().join("meta.toml");
let mut plugins = HashMap::new();
let meta = load_path::<ManagerMeta>(meta_file);
let manager_meta = load_path::<ManagerMeta>(meta_file);
let source = PluginSource {
name: "rustdesk".to_string(),
url: "https://github.com/fufesou/rustdesk-plugins".to_string(),
description: "".to_string(),
};
for plugin in meta.plugins.iter() {
for meta in manager_meta.plugins.iter() {
plugins.insert(
plugin.id.clone(),
meta.id.clone(),
PluginInfo {
source: source.clone(),
plugin: plugin.clone(),
meta: meta.clone(),
installed_version: "".to_string(),
install_time: "".to_string(),
invalid_reason: "".to_string(),
@ -95,22 +97,24 @@ fn get_source_plugins() -> HashMap<String, PluginInfo> {
resp.status()
);
}
match resp.json::<ManagerMeta>() {
Ok(meta) => {
for plugin in meta.plugins.iter() {
plugins.insert(
plugin.id.clone(),
PluginInfo {
source: source.clone(),
plugin: plugin.clone(),
installed_version: "".to_string(),
install_time: "".to_string(),
invalid_reason: "".to_string(),
},
);
if let Ok(text) = resp.text() {
match toml::from_str::<ManagerMeta>(&text) {
Ok(manager_meta) => {
for meta in manager_meta.plugins.iter() {
plugins.insert(
meta.id.clone(),
PluginInfo {
source: source.clone(),
meta: meta.clone(),
installed_version: "".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),
@ -121,7 +125,7 @@ fn get_source_plugins() -> HashMap<String, PluginInfo> {
fn send_plugin_list_event(plugins: &HashMap<String, PluginInfo>) {
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) {
let mut m = HashMap::new();
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(),
description: "".to_string(),
},
plugin: info.desc.meta().clone(),
meta: info.desc.meta().clone(),
installed_version: info.desc.meta().version.clone(),
install_time: info.install_time.clone(),
invalid_reason: "".to_string(),
@ -165,7 +169,7 @@ pub fn install_plugin(id: &str) -> ResultType<()> {
Some(plugin) => {
let _plugin_url = format!(
"{}/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)]
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::{
env,
ffi::{c_char, c_int, c_void, CStr},