Files
Alfis/src/ui/mod.rs
T
2025-10-01 15:04:46 +03:00

161 lines
4.9 KiB
Rust

#[warn(unused_imports)]
use alfis::{
blockchain::{
transaction::DomainData,
types::MineResult
},
commons::*,
crypto::CryptoBox,
dns::protocol::DnsRecord,
event::Event,
eventbus::{post, register},
miner::Miner,
keystore, Block, Bytes, Context, Keystore, Transaction
};
use std::{
sync::{Arc, Mutex, MutexGuard},
thread,
time::{Duration, Instant}
};
use gtk::prelude::*;
use gtk::{
glib,
ApplicationWindow,
Button,
Label,
HeaderBar,
Box,
Orientation,
Notebook,
Entry,
FileDialog
};
// pub use run_ui;
#[warn(unused_variables)]
pub fn run_ui(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) -> glib::ExitCode {
let application = adw::Application::builder()
.application_id("ru.Revertron.Alfis")
.build();
let context = context.clone();
application.connect_activate(move |app| {
let window = ApplicationWindow::builder()
.application(app)
.title(format!("ALFIS {}", env!("CARGO_PKG_VERSION")))
.default_width(1023)
.default_height(720)
.resizable(false)
// .show_menubar(true)
.build();
let ab = HeaderBar::new();
// ab.pack_start(&Label::new(Some("Application name")));
window.set_titlebar(Some(&ab));
let nb = Notebook::new();
window.set_child(Some(&nb));
let main = Box::new(Orientation::Vertical, 5);
// main.set_hexpand(true);
menu_credentials(&window, &main, &context);
nb.append_page(&main, Some(&Label::new(Some("Credentials"))));
// window.set_child(Some(&main));
window.present();
});
application.run()
}
fn menu_credentials(window: &ApplicationWindow, parent: &gtk::Box, context: &Arc<Mutex<Context>>) {
let keys = Box::new(Orientation::Horizontal, 8);
keys.set_hexpand(true);
parent.append(&keys);
let key_hash = Entry::builder()
.placeholder_text("No key loaded")
.editable(false)
.tooltip_text("If you load or mine a keypair the public key will be displayed here")
.hexpand(true)
.focusable(false)
.build();
keys.append(&key_hash);
let load_btn = Button::with_label("Load key");
let window = window.clone();
let context = context.clone();
load_btn.connect_clicked(move |_| { open_file(&window, &context); });
keys.append(&load_btn);
let save_btn = Button::builder()
.label("Save key")
.sensitive(false)
.build();
keys.append(&save_btn);
let mine_btn = Button::with_label("Mine new key");
mine_btn.add_css_class("suggested-action");
keys.append(&mine_btn);
parent.append(&Label::new(Some("To mine domains you need a strong pair of signing keys and a pair of ecryption keys.")));
}
fn open_file(window: &ApplicationWindow, context: &Arc<Mutex<Context>>) {
let filter = gtk::FileFilter::new();
filter.add_pattern("*.key");
filter.add_pattern("*.toml");
filter.set_name(Some("Key files"));
let fc = FileDialog::builder()
.title("Select key")
.default_filter(&filter)
.build();
// let window = window.clone();
let context = context.clone();
fc.open(Some(window), None::<gtk::gio::Cancellable>.as_ref(), move |result| {
match result {
Ok(file) => {
match Keystore::from_file(file.path().unwrap().to_str().unwrap(), "") {
None => {
todo!("Setup error messages");
// error!("Error loading keystore '{}'!", &file_name);
// show_warning(web_view, "Error loading key!<br>Key cannot be loaded or its difficulty is not enough.");
// event_fail(web_view, &format!("Error loading key from \\'{}\\'!", &file_name));
}
Some(keystore) => {
// todo!("Setup info messages");
// info!("Loaded keystore with keys: {:?}, {:?}", &keystore.get_public(), &keystore.get_encryption_public());
let path = keystore.get_path().to_owned();
let public = keystore.get_public().to_string();
let hash = keystore.get_hash().to_string();
post(Event::KeyLoaded { path, public, hash });
if !context.lock().unwrap().select_key_by_public(&keystore.get_public()) {
context.lock().unwrap().add_keystore(keystore);
} else {
todo!();
// warn!("This key is already loaded!");
}
}
}
}
Err(e) => {
if !e.matches(gtk::gio::IOErrorEnum::Cancelled) {
eprintln!("Error selecting file: {}", e);
}
}
}
});
}