download db only once

This commit is contained in:
Ward from fusion-voyager-3 2024-06-18 21:34:53 +03:00
parent 373a34434a
commit 3f01d9263a
2 changed files with 117 additions and 35 deletions

View File

@ -1,6 +1,7 @@
use crate::{content, kernel_pkg, sched_ext, KernelBranch, PRETTY_NAME}; use crate::{content, kernel_pkg, sched_ext, KernelBranch, PRETTY_NAME};
use adw::prelude::*; use adw::prelude::*;
use adw::*; use adw::*;
use glib::property::PropertyGet;
use glib::{clone, MainContext}; use glib::{clone, MainContext};
use gtk::prelude::*; use gtk::prelude::*;
use gtk::*; use gtk::*;
@ -8,11 +9,14 @@ use std::cell::RefCell;
use std::process::Command; use std::process::Command;
use std::rc::Rc; use std::rc::Rc;
use std::{thread, time}; use std::{thread, time};
use glib::property::PropertyGet;
pub fn build_ui(app: &adw::Application) { pub fn build_ui(app: &adw::Application) {
let internet_connected = Rc::new(RefCell::new(false)); let internet_connected = Rc::new(RefCell::new(false));
let selected_kernel_branch: Rc<RefCell<KernelBranch>> = Rc::new(RefCell::new(KernelBranch{name: "?".to_owned(), db_url:"?".to_owned() , db:"?".to_owned()})); let selected_kernel_branch: Rc<RefCell<KernelBranch>> = Rc::new(RefCell::new(KernelBranch {
name: "?".to_owned(),
db_url: "?".to_owned(),
db: "?".to_owned(),
}));
let (internet_loop_sender, internet_loop_receiver) = async_channel::unbounded(); let (internet_loop_sender, internet_loop_receiver) = async_channel::unbounded();
let internet_loop_sender = internet_loop_sender.clone(); let internet_loop_sender = internet_loop_sender.clone();
@ -66,7 +70,8 @@ pub fn build_ui(app: &adw::Application) {
content_stack.add_named( content_stack.add_named(
&content::content(&content_stack, &selected_kernel_branch2), &content::content(&content_stack, &selected_kernel_branch2),
Some("content_page")); Some("content_page"),
);
content_stack.add_named( content_stack.add_named(
&sched_ext::sched_ext_page(&content_stack), &sched_ext::sched_ext_page(&content_stack),
Some("sched_ext_page"), Some("sched_ext_page"),

View File

@ -1,23 +1,36 @@
use std::cell::RefCell;
use crate::{KernelBranch, RunningKernelInfo}; use crate::{KernelBranch, RunningKernelInfo};
use adw::prelude::*; use adw::prelude::*;
use adw::ExpanderRow; use adw::ExpanderRow;
use async_channel::Receiver;
use duct::cmd; use duct::cmd;
use glib::property::PropertyGet;
use glib::*; use glib::*;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::*; use gtk::*;
use homedir::get_my_home;
use std::cell::RefCell;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::rc::Rc; use std::rc::Rc;
use version_compare::Version; use version_compare::Version;
use homedir::get_my_home;
use Vec; use Vec;
use glib::property::PropertyGet;
pub fn content(content_stack: &gtk::Stack, selected_kernel_branch: &Rc<RefCell<KernelBranch>>) -> gtk::Box { pub fn content(
content_stack: &gtk::Stack,
selected_kernel_branch: &Rc<RefCell<KernelBranch>>,
) -> gtk::Box {
let running_kernel_info = get_running_kernel_info(); let running_kernel_info = get_running_kernel_info();
let (get_kernel_branches_sender, get_kernel_branches_receiver) = async_channel::unbounded();
let get_kernel_branches_sender = get_kernel_branches_sender.clone();
std::thread::spawn(move || {
get_kernel_branches_sender
.send_blocking(get_kernel_branches())
.expect("channel needs to be open.");
});
let content_box = gtk::Box::builder() let content_box = gtk::Box::builder()
.hexpand(true) .hexpand(true)
.vexpand(true) .vexpand(true)
@ -48,7 +61,11 @@ pub fn content(content_stack: &gtk::Stack, selected_kernel_branch: &Rc<RefCell<K
.subtitle("Kernel Branch") .subtitle("Kernel Branch")
.build(); .build();
kernel_branch_expander_row.add_row(&kernel_branch_expandable(&kernel_branch_expander_row, selected_kernel_branch)); kernel_branch_expander_row.add_row(&kernel_branch_expandable(
&kernel_branch_expander_row,
selected_kernel_branch,
get_kernel_branches_receiver.clone(),
));
let kernel_branch_expander_row_boxedlist = gtk::ListBox::builder() let kernel_branch_expander_row_boxedlist = gtk::ListBox::builder()
.selection_mode(SelectionMode::None) .selection_mode(SelectionMode::None)
@ -116,27 +133,55 @@ pub fn content(content_stack: &gtk::Stack, selected_kernel_branch: &Rc<RefCell<K
content_box.append(&kernel_branch_expander_row_boxedlist); content_box.append(&kernel_branch_expander_row_boxedlist);
content_box.append(&button_box); content_box.append(&button_box);
let get_kernel_branches_loop_context = MainContext::default();
// The main loop executes the asynchronous block
get_kernel_branches_loop_context.spawn_local(
clone!(@strong selected_kernel_branch => async move {
while let Ok(data) = get_kernel_branches_receiver.recv().await {
let selected_kernel_branch_clone0 = selected_kernel_branch.clone();
let selected_kernel_branch_clone1 = selected_kernel_branch.clone();
let selected_kernel_branch_clone0 = selected_kernel_branch.clone(); match get_my_home()
let selected_kernel_branch_clone1 = selected_kernel_branch.clone(); .unwrap()
.unwrap()
match get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager/branch").exists() { .join(".config/fedora-kernel-manager/branch")
true => { .exists()
let narrow_branch: Vec<KernelBranch> = get_kernel_branches().clone().into_iter() {
.filter(|a| a.name == fs::read_to_string(get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager/branch")).unwrap()) true => {
.collect(); let narrow_branch: Vec<KernelBranch> = data
*selected_kernel_branch_clone0.borrow_mut()=narrow_branch.get(0).unwrap().clone() .clone()
} .into_iter()
_ => { .filter(|a| {
let normal_branch = get_kernel_branches().clone().get(0).unwrap().clone(); a.name
*selected_kernel_branch_clone0.borrow_mut()=normal_branch == fs::read_to_string(
} get_my_home()
}; .unwrap()
.unwrap()
.join(".config/fedora-kernel-manager/branch"),
)
.unwrap()
})
.collect();
*selected_kernel_branch_clone0.borrow_mut() =
narrow_branch.get(0).unwrap().clone()
}
_ => {
let normal_branch = data.clone().get(0).unwrap().clone();
*selected_kernel_branch_clone0.borrow_mut() = normal_branch
}
};
}
}),
);
content_box content_box
} }
fn kernel_branch_expandable(expander_row: &adw::ExpanderRow, selected_kernel_branch: &Rc<RefCell<KernelBranch>>) -> gtk::ListBox { fn kernel_branch_expandable(
expander_row: &adw::ExpanderRow,
selected_kernel_branch: &Rc<RefCell<KernelBranch>>,
get_kernel_branches_receiver: Receiver<Vec<KernelBranch>>,
) -> gtk::ListBox {
let searchbar = gtk::SearchEntry::builder().search_delay(500).build(); let searchbar = gtk::SearchEntry::builder().search_delay(500).build();
searchbar.add_css_class("round-border-only-top"); searchbar.add_css_class("round-border-only-top");
@ -155,7 +200,11 @@ fn kernel_branch_expandable(expander_row: &adw::ExpanderRow, selected_kernel_bra
.label("No branch selected") .label("No branch selected")
.build(); .build();
for branch in get_kernel_branches() { let get_kernel_branches_loop_context = MainContext::default();
// The main loop executes the asynchronous block
get_kernel_branches_loop_context.spawn_local(clone!(@weak expander_row, @weak branch_container, @strong selected_kernel_branch => async move {
while let Ok(data) = get_kernel_branches_receiver.recv().await {
for branch in data {
let branch_clone0 = branch.clone(); let branch_clone0 = branch.clone();
let branch_clone1 = branch.clone(); let branch_clone1 = branch.clone();
let branch_checkbutton = gtk::CheckButton::builder() let branch_checkbutton = gtk::CheckButton::builder()
@ -180,11 +229,28 @@ fn kernel_branch_expandable(expander_row: &adw::ExpanderRow, selected_kernel_bra
}), }),
); );
match get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager/branch").exists() { match get_my_home()
true if fs::read_to_string(get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager/branch")).unwrap() == branch_clone1.name => branch_checkbutton.set_active(true), .unwrap()
_ => {} .unwrap()
.join(".config/fedora-kernel-manager/branch")
.exists()
{
true if fs::read_to_string(
get_my_home()
.unwrap()
.unwrap()
.join(".config/fedora-kernel-manager/branch"),
)
.unwrap()
== branch_clone1.name =>
{
branch_checkbutton.set_active(true)
}
_ => {}
} }
} }
}
}));
let branch_container_viewport = gtk::ScrolledWindow::builder() let branch_container_viewport = gtk::ScrolledWindow::builder()
.child(&branch_container) .child(&branch_container)
@ -280,19 +346,27 @@ fn get_kernel_branches() -> Vec<KernelBranch> {
let data = fs::read_to_string( let data = fs::read_to_string(
"/home/ward/RustroverProjects/fedora-kernel-manager/data/kernel_branches.json", "/home/ward/RustroverProjects/fedora-kernel-manager/data/kernel_branches.json",
) )
.expect("Unable to read file"); .expect("Unable to read file");
let res: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse"); let res: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse");
if let serde_json::Value::Array(branches) = &res["branches"] { if let serde_json::Value::Array(branches) = &res["branches"] {
for branch in branches { for branch in branches {
println!(
println!("Downloading & Parsing package DB for {}.", branch.name); "Downloading & Parsing package DB for {}.",
branch["name"].as_str().to_owned().unwrap().to_string()
);
let branch = KernelBranch { let branch = KernelBranch {
name: branch["name"].as_str().to_owned().unwrap().to_string(), name: branch["name"].as_str().to_owned().unwrap().to_string(),
db_url: branch["db_url"].as_str().to_owned().unwrap().to_string(), db_url: branch["db_url"].as_str().to_owned().unwrap().to_string(),
db: reqwest::blocking::get(branch["db_url"].as_str().to_owned().unwrap().to_string(),).unwrap().text().unwrap() db: reqwest::blocking::get(
branch["db_url"].as_str().to_owned().unwrap().to_string(),
)
.unwrap()
.text()
.unwrap(),
}; };
kernel_branches_array.push(branch) kernel_branches_array.push(branch)
}}; }
};
kernel_branches_array kernel_branches_array
} }
@ -418,7 +492,10 @@ fn create_kernel_badges(badge_box: &gtk::Box, running_kernel_info: &RunningKerne
} }
fn save_branch_config(branch: &str) { fn save_branch_config(branch: &str) {
let config_path = get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager"); let config_path = get_my_home()
.unwrap()
.unwrap()
.join(".config/fedora-kernel-manager");
match &config_path.exists() { match &config_path.exists() {
true => fs::write(config_path.join("branch"), branch).unwrap(), true => fs::write(config_path.join("branch"), branch).unwrap(),
_ => { _ => {
@ -426,4 +503,4 @@ fn save_branch_config(branch: &str) {
fs::write(config_path.join("branch"), branch).unwrap(); fs::write(config_path.join("branch"), branch).unwrap();
} }
} }
} }