🧹 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:
Vladislav Nepogodin 2023-02-18 01:58:47 +04:00
parent c40aee852a
commit 0d9c30545b
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
1 changed files with 56 additions and 75 deletions

View File

@ -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: &gtk::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,9 +370,7 @@ 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: &gtk::Widget = &builder_ref.object(elt_value).unwrap();
let item: gtk::Widget =
G_HELLO_WINDOW.as_ref().unwrap().builder.object(elt_value).unwrap();
if default_texts[method.0].get(elt_value).is_none() { if default_texts[method.0].get(elt_value).is_none() {
let item_buf = item.property::<String>(method.0.as_str()); let item_buf = item.property::<String>(method.0.as_str());
default_texts[method.0][elt_value] = json!(item_buf); default_texts[method.0][elt_value] = json!(item_buf);
@ -386,21 +383,15 @@ fn set_locale(use_locale: &str) {
} }
} }
} }
}
unsafe {
let preferences = &G_HELLO_WINDOW.as_ref().unwrap().preferences;
let save = &*G_SAVE_JSON.lock().unwrap(); let save = &*G_SAVE_JSON.lock().unwrap();
let preferences = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().preferences };
// 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,
preferences["default_locale"].as_str().unwrap()
);
for page in fs::read_dir(pages).unwrap() { for page in fs::read_dir(pages).unwrap() {
let stack: gtk::Stack = let stack: &gtk::Stack = &builder_ref.object("stack").unwrap();
G_HELLO_WINDOW.as_ref().unwrap().builder.object("stack").unwrap();
let child = stack.child_by_name(&format!( let child = stack.child_by_name(&format!(
"{}page", "{}page",
page.as_ref().unwrap().path().file_name().unwrap().to_str().unwrap() page.as_ref().unwrap().path().file_name().unwrap().to_str().unwrap()
@ -410,10 +401,8 @@ fn set_locale(use_locale: &str) {
continue; continue;
} }
let first_child = &child.unwrap().downcast::<gtk::Container>().unwrap().children(); let first_child = &child.unwrap().downcast::<gtk::Container>().unwrap().children();
let second_child = let second_child = &first_child[0].clone().downcast::<gtk::Container>().unwrap().children();
&first_child[0].clone().downcast::<gtk::Container>().unwrap().children(); let third_child = &second_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(); let label = &third_child[0].clone().downcast::<gtk::Label>().unwrap();
label.set_markup( label.set_markup(
@ -425,7 +414,6 @@ fn set_locale(use_locale: &str) {
.as_str(), .as_str(),
); );
} }
}
} }
fn set_autostart(autostart: bool) { fn set_autostart(autostart: bool) {
@ -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: &gtk::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 preferences = &G_HELLO_WINDOW.as_ref().unwrap().preferences["save_path"];
let save = &*G_SAVE_JSON.lock().unwrap(); let save = &*G_SAVE_JSON.lock().unwrap();
let preferences = unsafe { &G_HELLO_WINDOW.as_ref().unwrap().preferences["save_path"] };
write_json(preferences.as_str().unwrap(), save); write_json(preferences.as_str().unwrap(), save);
}
Some(false.to_value()) Some(false.to_value())
} }