🧹 drop unsafe at main.rs for save json
drop unsafe at main.rs for save json Cleanup the unsafe blocks
This commit is contained in:
parent
c40aee852a
commit
0d9c30545b
131
src/main.rs
131
src/main.rs
|
@ -28,7 +28,7 @@ use serde_json::json;
|
||||||
use std::{fs, str};
|
use std::{fs, str};
|
||||||
use subprocess::Exec;
|
use subprocess::Exec;
|
||||||
|
|
||||||
static mut G_SAVE_JSON: Lazy<Mutex<serde_json::Value>> = Lazy::new(|| Mutex::new(json!(null)));
|
static G_SAVE_JSON: Lazy<Mutex<serde_json::Value>> = Lazy::new(|| Mutex::new(json!(null)));
|
||||||
|
|
||||||
static mut G_HELLO_WINDOW: Option<Arc<HelloWindow>> = None;
|
static mut G_HELLO_WINDOW: Option<Arc<HelloWindow>> = None;
|
||||||
|
|
||||||
|
@ -180,14 +180,13 @@ fn build_ui(application: >k::Application) {
|
||||||
let main_window: Window = builder.object("window").expect("Could not get the object window");
|
let main_window: Window = builder.object("window").expect("Could not get the object window");
|
||||||
main_window.set_application(Some(application));
|
main_window.set_application(Some(application));
|
||||||
|
|
||||||
|
*G_SAVE_JSON.lock().unwrap() = save;
|
||||||
unsafe {
|
unsafe {
|
||||||
G_HELLO_WINDOW = Some(Arc::new(HelloWindow {
|
G_HELLO_WINDOW = Some(Arc::new(HelloWindow {
|
||||||
window: main_window.clone(),
|
window: main_window.clone(),
|
||||||
builder: builder.clone(),
|
builder: builder.clone(),
|
||||||
preferences: preferences.clone(),
|
preferences: preferences.clone(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
*G_SAVE_JSON.lock().unwrap() = save;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Subtitle of headerbar
|
// Subtitle of headerbar
|
||||||
|
@ -353,9 +352,7 @@ fn set_locale(use_locale: &str) {
|
||||||
gettextrs::textdomain(GETTEXT_PACKAGE).expect("Unable to switch to the text domain.");
|
gettextrs::textdomain(GETTEXT_PACKAGE).expect("Unable to switch to the text domain.");
|
||||||
glib::setenv("LANGUAGE", use_locale, true).expect("Unable to change env variable.");
|
glib::setenv("LANGUAGE", use_locale, true).expect("Unable to change env variable.");
|
||||||
|
|
||||||
unsafe {
|
G_SAVE_JSON.lock().unwrap()["locale"] = json!(use_locale);
|
||||||
G_SAVE_JSON.lock().unwrap()["locale"] = json!(use_locale);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Real-time locale changing
|
// Real-time locale changing
|
||||||
let elts: HashMap<String, serde_json::Value> = serde_json::from_str(&serde_json::to_string(&json!({
|
let elts: HashMap<String, serde_json::Value> = serde_json::from_str(&serde_json::to_string(&json!({
|
||||||
|
@ -363,6 +360,8 @@ fn set_locale(use_locale: &str) {
|
||||||
"tooltip_text": ["about", "development", "software", "donate", "forum", "mailling", "wiki"],
|
"tooltip_text": ["about", "development", "software", "donate", "forum", "mailling", "wiki"],
|
||||||
})).unwrap()).unwrap();
|
})).unwrap()).unwrap();
|
||||||
|
|
||||||
|
let builder_ref = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().builder };
|
||||||
|
|
||||||
let mut default_texts = json!(null);
|
let mut default_texts = json!(null);
|
||||||
for method in elts.iter() {
|
for method in elts.iter() {
|
||||||
if default_texts.get(method.0).is_none() {
|
if default_texts.get(method.0).is_none() {
|
||||||
|
@ -371,60 +370,49 @@ fn set_locale(use_locale: &str) {
|
||||||
|
|
||||||
for elt in elts[method.0].as_array().unwrap() {
|
for elt in elts[method.0].as_array().unwrap() {
|
||||||
let elt_value = elt.as_str().unwrap();
|
let elt_value = elt.as_str().unwrap();
|
||||||
unsafe {
|
let item: >k::Widget = &builder_ref.object(elt_value).unwrap();
|
||||||
let item: gtk::Widget =
|
if default_texts[method.0].get(elt_value).is_none() {
|
||||||
G_HELLO_WINDOW.as_ref().unwrap().builder.object(elt_value).unwrap();
|
let item_buf = item.property::<String>(method.0.as_str());
|
||||||
if default_texts[method.0].get(elt_value).is_none() {
|
default_texts[method.0][elt_value] = json!(item_buf);
|
||||||
let item_buf = item.property::<String>(method.0.as_str());
|
}
|
||||||
default_texts[method.0][elt_value] = json!(item_buf);
|
if method.0 == "tooltip_text" {
|
||||||
}
|
item.set_property(
|
||||||
if method.0 == "tooltip_text" {
|
method.0,
|
||||||
item.set_property(
|
&gettextrs::gettext(default_texts[method.0][elt_value].as_str().unwrap()),
|
||||||
method.0,
|
);
|
||||||
&gettextrs::gettext(default_texts[method.0][elt_value].as_str().unwrap()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
let save = &*G_SAVE_JSON.lock().unwrap();
|
||||||
let preferences = &G_HELLO_WINDOW.as_ref().unwrap().preferences;
|
let preferences = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().preferences };
|
||||||
let save = &*G_SAVE_JSON.lock().unwrap();
|
|
||||||
|
|
||||||
// Change content of pages
|
// Change content of pages
|
||||||
let pages = format!(
|
let pages =
|
||||||
"{}/data/pages/{}",
|
format!("{}/data/pages/{}", PKGDATADIR, preferences["default_locale"].as_str().unwrap());
|
||||||
PKGDATADIR,
|
for page in fs::read_dir(pages).unwrap() {
|
||||||
preferences["default_locale"].as_str().unwrap()
|
let stack: >k::Stack = &builder_ref.object("stack").unwrap();
|
||||||
);
|
let child = stack.child_by_name(&format!(
|
||||||
for page in fs::read_dir(pages).unwrap() {
|
"{}page",
|
||||||
let stack: gtk::Stack =
|
page.as_ref().unwrap().path().file_name().unwrap().to_str().unwrap()
|
||||||
G_HELLO_WINDOW.as_ref().unwrap().builder.object("stack").unwrap();
|
));
|
||||||
let child = stack.child_by_name(&format!(
|
if child.is_none() {
|
||||||
"{}page",
|
eprintln!("child not found");
|
||||||
page.as_ref().unwrap().path().file_name().unwrap().to_str().unwrap()
|
continue;
|
||||||
));
|
|
||||||
if child.is_none() {
|
|
||||||
eprintln!("child not found");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let first_child = &child.unwrap().downcast::<gtk::Container>().unwrap().children();
|
|
||||||
let second_child =
|
|
||||||
&first_child[0].clone().downcast::<gtk::Container>().unwrap().children();
|
|
||||||
let third_child =
|
|
||||||
&second_child[0].clone().downcast::<gtk::Container>().unwrap().children();
|
|
||||||
|
|
||||||
let label = &third_child[0].clone().downcast::<gtk::Label>().unwrap();
|
|
||||||
label.set_markup(
|
|
||||||
get_page(
|
|
||||||
page.unwrap().path().file_name().unwrap().to_str().unwrap(),
|
|
||||||
preferences,
|
|
||||||
save,
|
|
||||||
)
|
|
||||||
.as_str(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
let first_child = &child.unwrap().downcast::<gtk::Container>().unwrap().children();
|
||||||
|
let second_child = &first_child[0].clone().downcast::<gtk::Container>().unwrap().children();
|
||||||
|
let third_child = &second_child[0].clone().downcast::<gtk::Container>().unwrap().children();
|
||||||
|
|
||||||
|
let label = &third_child[0].clone().downcast::<gtk::Label>().unwrap();
|
||||||
|
label.set_markup(
|
||||||
|
get_page(
|
||||||
|
page.unwrap().path().file_name().unwrap().to_str().unwrap(),
|
||||||
|
preferences,
|
||||||
|
save,
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -495,10 +483,9 @@ fn on_btn_clicked(param: &[glib::Value]) -> Option<glib::Value> {
|
||||||
let widget = param[0].get::<gtk::Button>().unwrap();
|
let widget = param[0].get::<gtk::Button>().unwrap();
|
||||||
let name = widget.widget_name();
|
let name = widget.widget_name();
|
||||||
|
|
||||||
unsafe {
|
let builder_ref = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().builder };
|
||||||
let stack: gtk::Stack = G_HELLO_WINDOW.as_ref().unwrap().builder.object("stack").unwrap();
|
let stack: >k::Stack = &builder_ref.object("stack").unwrap();
|
||||||
stack.set_visible_child_name(&format!("{name}page"));
|
stack.set_visible_child_name(&format!("{name}page"));
|
||||||
};
|
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -507,13 +494,11 @@ fn on_link_clicked(param: &[glib::Value]) -> Option<glib::Value> {
|
||||||
let widget = param[0].get::<gtk::Widget>().unwrap();
|
let widget = param[0].get::<gtk::Widget>().unwrap();
|
||||||
let name = widget.widget_name();
|
let name = widget.widget_name();
|
||||||
|
|
||||||
unsafe {
|
let window_ref = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().window };
|
||||||
let window_ref = &G_HELLO_WINDOW.as_ref().unwrap().window;
|
let preferences = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().preferences["urls"] };
|
||||||
let preferences = &G_HELLO_WINDOW.as_ref().unwrap().preferences["urls"];
|
|
||||||
|
|
||||||
let uri = preferences[name.as_str()].as_str().unwrap();
|
let uri = preferences[name.as_str()].as_str().unwrap();
|
||||||
let _ = gtk::show_uri_on_window(Some(window_ref), uri, 0);
|
let _ = gtk::show_uri_on_window(Some(window_ref), uri, 0);
|
||||||
}
|
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -522,23 +507,19 @@ fn on_link1_clicked(param: &[glib::Value]) -> Option<glib::Value> {
|
||||||
let widget = param[0].get::<gtk::Widget>().unwrap();
|
let widget = param[0].get::<gtk::Widget>().unwrap();
|
||||||
let name = widget.widget_name();
|
let name = widget.widget_name();
|
||||||
|
|
||||||
unsafe {
|
let window_ref = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().window };
|
||||||
let window_ref = &G_HELLO_WINDOW.as_ref().unwrap().window;
|
let preferences = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().preferences["urls"] };
|
||||||
let preferences = &G_HELLO_WINDOW.as_ref().unwrap().preferences["urls"];
|
|
||||||
|
|
||||||
let uri = preferences[name.as_str()].as_str().unwrap();
|
let uri = preferences[name.as_str()].as_str().unwrap();
|
||||||
let _ = gtk::show_uri_on_window(Some(window_ref), uri, 0);
|
let _ = gtk::show_uri_on_window(Some(window_ref), uri, 0);
|
||||||
}
|
|
||||||
|
|
||||||
Some(false.to_value())
|
Some(false.to_value())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_delete_window(_param: &[glib::Value]) -> Option<glib::Value> {
|
fn on_delete_window(_param: &[glib::Value]) -> Option<glib::Value> {
|
||||||
unsafe {
|
let save = &*G_SAVE_JSON.lock().unwrap();
|
||||||
let preferences = &G_HELLO_WINDOW.as_ref().unwrap().preferences["save_path"];
|
let preferences = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().preferences["save_path"] };
|
||||||
let save = &*G_SAVE_JSON.lock().unwrap();
|
write_json(preferences.as_str().unwrap(), save);
|
||||||
write_json(preferences.as_str().unwrap(), save);
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(false.to_value())
|
Some(false.to_value())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue