Skip to content

Commit

Permalink
Wrong account edit (#67)
Browse files Browse the repository at this point in the history
Wrong account edit
  • Loading branch information
gr211 authored Dec 21, 2020
1 parent cb2ee4e commit 6bd6330
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 104 deletions.
10 changes: 5 additions & 5 deletions src/helpers/config_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl ConfigManager {
}
}

pub fn load_account_groups(connection: &Connection, filter: Option<&str>) -> Result<Vec<AccountGroup>, LoadError> {
pub fn load_account_groups(connection: &Connection, filter: Option<String>) -> Result<Vec<AccountGroup>, LoadError> {
let mut stmt = connection.prepare("SELECT id, name, icon, url FROM groups ORDER BY LOWER(name)").unwrap();

stmt.query_map(params![], |row| {
Expand All @@ -66,15 +66,15 @@ impl ConfigManager {
name.as_str(),
icon.as_deref(),
url.as_deref(),
Self::get_accounts(&connection, id, filter)?,
Self::get_accounts(&connection, id, filter.clone())?,
))
})
.map(|rows| {
rows.map(|each| each.unwrap())
.collect::<Vec<AccountGroup>>()
.into_iter()
//filter out empty groups
.filter(|account_group| !account_group.entries.is_empty())
//filter out empty groups - unless no filter is applied then display everything
.filter(|account_group| !account_group.entries.is_empty() || filter.is_none())
.collect()
})
.map_err(|e| LoadError::DbError(format!("{:?}", e)))
Expand Down Expand Up @@ -276,7 +276,7 @@ impl ConfigManager {
stmt.execute(params![account_id]).map_err(|e| LoadError::DbError(format!("{:?}", e)))
}

fn get_accounts(connection: &Connection, group_id: u32, filter: Option<&str>) -> Result<Vec<Account>, rusqlite::Error> {
fn get_accounts(connection: &Connection, group_id: u32, filter: Option<String>) -> Result<Vec<Account>, rusqlite::Error> {
let mut stmt = connection.prepare("SELECT id, label, secret FROM accounts WHERE group_id = ?1 AND label LIKE ?2 ORDER BY LOWER(label)")?;

let label_filter = filter.map(|f| format!("%{}%", f)).unwrap_or_else(|| "%".to_owned());
Expand Down
37 changes: 13 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
mod main_window;

use main_window::MainWindow;

extern crate gio;
extern crate glib;
extern crate gtk;

use crate::helpers::runner;
use crate::helpers::ConfigManager;
use crate::model::AccountGroup;
use crate::ui::{AccountsWindow, AddGroupWindow, EditAccountWindow};
use std::sync::{Arc, Mutex};

use gettextrs::*;
use gio::prelude::*;
use gtk::prelude::*;
use log::info;
use log4rs::config::Config;
use log4rs::file::{Deserializers, RawConfig};
use rusqlite::Connection;
use std::cell::RefCell;
use std::sync::{Arc, Mutex};

use main_window::MainWindow;

use crate::helpers::runner;
use crate::helpers::ConfigManager;
use crate::ui::{AccountsWindow, AddGroupWindow, EditAccountWindow};

mod main_window;

mod helpers;
mod model;
mod ui;

use log::info;

const NAMESPACE: &str = "uk.co.grumlimited.authenticator-rs";
const NAMESPACE_PREFIX: &str = "/uk/co/grumlimited/authenticator-rs";

Expand Down Expand Up @@ -74,25 +73,15 @@ fn main() {
// SQL migrations
runner::run(&mut connection).unwrap();

let groups = ConfigManager::load_account_groups(&connection, None).unwrap();

let connection: Arc<Mutex<Connection>> = Arc::new(Mutex::new(connection));

let groups: Arc<Mutex<RefCell<Vec<AccountGroup>>>> = Arc::new(Mutex::new(RefCell::new(groups)));

gui.display(groups);

gui.set_application(&app, connection.clone());

AccountsWindow::edit_buttons_actions(&gui, connection.clone());

AccountsWindow::group_edit_buttons_actions(&gui, connection.clone());
AccountsWindow::replace_accounts_and_widgets(&gui, connection.clone());

EditAccountWindow::edit_account_buttons_actions(&gui, connection.clone());

AddGroupWindow::edit_account_buttons_actions(&gui, connection.clone());

AccountsWindow::delete_buttons_actions(&gui, connection);
AddGroupWindow::edit_account_buttons_actions(&gui, connection);

info!("Authenticator RS initialised");
});
Expand Down
19 changes: 0 additions & 19 deletions src/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use gtk::prelude::*;
use rusqlite::Connection;

use crate::helpers::ConfigManager;
use crate::model::{AccountGroup, AccountGroupWidgets};
use crate::ui::{AccountsWindow, AddGroupWindow, EditAccountWindow};
use crate::{ui, NAMESPACE_PREFIX};

Expand Down Expand Up @@ -164,7 +163,6 @@ impl MainWindow {
pub fn bind_account_filter_events(&mut self, connection: Arc<Mutex<Connection>>) {
{
//First bind user input event to refreshing account list

let (tx, rx) = glib::MainContext::channel::<bool>(glib::PRIORITY_DEFAULT);

let gui = self.clone();
Expand All @@ -184,7 +182,6 @@ impl MainWindow {

{
//then bind "x" icon to emptying the filter input.

let (tx, rx) = glib::MainContext::channel::<bool>(glib::PRIORITY_DEFAULT);

let gui = self.clone();
Expand All @@ -207,21 +204,6 @@ impl MainWindow {
}
}

pub fn display(&mut self, groups: Arc<Mutex<RefCell<Vec<AccountGroup>>>>) {
let mut guard = groups.lock().unwrap();
let groups = guard.get_mut();

let widgets: Vec<AccountGroupWidgets> = groups.iter_mut().map(|account_group| account_group.widget(self.state.clone())).collect();

widgets.iter().for_each(|widget| self.accounts_window.accounts_container.add(&widget.container));

let m_widgets = self.accounts_window.widgets.clone();
let mut m_widgets = m_widgets.lock().unwrap();
*m_widgets = widgets;

self.accounts_window.accounts_container.show_all();
}

pub fn start_progress_bar(&mut self) {
let (tx, rx) = glib::MainContext::channel::<u8>(glib::PRIORITY_DEFAULT);
self.pool.spawn_ok(progress_bar_interval(tx));
Expand Down Expand Up @@ -306,7 +288,6 @@ impl MainWindow {
let connection = connection.clone();
dark_mode_slider.connect_state_set(move |_, state| {
let g_settings = gio::Settings::new("uk.co.grumlimited.authenticator-rs");

g_settings.set_boolean("dark-theme", state).expect("Could not find setting dark-theme");

// switch first then redraw - to take into account state change
Expand Down
33 changes: 15 additions & 18 deletions src/ui/accounts_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ impl AccountsWindow {

let groups = {
let connection = connection.lock().unwrap();
let mut filter_text = gui.accounts_window.filter.lock().unwrap();
let filter_text = filter_text.get_mut();
let filter_text = filter_text.get_text();
ConfigManager::load_account_groups(&connection, Some(&filter_text)).unwrap()
ConfigManager::load_account_groups(&connection, gui.accounts_window.get_filter_value()).unwrap()
};

{
Expand Down Expand Up @@ -204,11 +201,7 @@ impl AccountsWindow {

account_widgets.edit_button.connect_clicked(move |_| {
let connection = connection.lock().unwrap();
let mut filter_text = gui.accounts_window.filter.lock().unwrap();
let filter_text = filter_text.get_mut();
let filter_text = filter_text.get_text();

let groups = ConfigManager::load_account_groups(&connection, Some(&filter_text)).unwrap();
let groups = ConfigManager::load_account_groups(&connection, gui.accounts_window.get_filter_value()).unwrap();
let account = ConfigManager::get_account(&connection, id).unwrap();

input_group.remove_all(); //re-added and refreshed just below
Expand Down Expand Up @@ -296,28 +289,32 @@ impl AccountsWindow {
debug!("Loading for group_id {:?}", group_id);
let groups = {
let connection = connection.lock().unwrap();
let mut filter_text = main_window.accounts_window.filter.lock().unwrap();
let filter_text = filter_text.get_mut();
let filter_text = filter_text.get_text();
ConfigManager::load_account_groups(&connection, Some(&filter_text)).unwrap()
ConfigManager::load_account_groups(&connection, main_window.accounts_window.get_filter_value()).unwrap()
};

edit_account_window.reset();
edit_account_window.set_group_dropdown(group_id, groups.as_slice());

edit_account_window.input_name.set_text("");

edit_account_window.add_accounts_container_edit.set_visible(false);
edit_account_window.add_accounts_container_add.set_visible(true);

let buffer = edit_account_window.input_secret.get_buffer().unwrap();
buffer.set_text("");

popover.hide();
MainWindow::switch_to(&main_window, Display::DisplayAddAccount);
}
})
}

pub fn get_filter_value(&self) -> Option<String> {
let mut filter_text = self.filter.lock().unwrap();
let filter_text = filter_text.get_mut();
let filter_text = filter_text.get_text();

if filter_text.is_empty() {
None
} else {
Some(filter_text.to_owned())
}
}
}

/**
Expand Down
47 changes: 18 additions & 29 deletions src/ui/add_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,24 @@ impl AddGroupWindow {
}

pub fn reset(&self) {
let icon_filename = self.icon_filename.clone();
let input_group = self.input_group.clone();
let icon_error = self.icon_error.clone();
let icon_reload = self.icon_reload.clone();
let icon_delete = self.icon_delete.clone();
let image_input = self.image_input.clone();
let save_button = self.save_button.clone();
let url_input = self.url_input.clone();
let group_id = self.group_id.clone();

Self::remove_tmp_file(icon_filename.clone());

input_group.set_text("");
url_input.set_text("");

icon_filename.set_label("");
group_id.set_label("");

icon_error.set_label("");
icon_error.set_visible(false);

save_button.set_sensitive(true);
icon_reload.set_sensitive(true);
icon_delete.set_sensitive(true);
image_input.set_from_icon_name(Some("content-loading-symbolic"), IconSize::Button);

input_group.set_property_primary_icon_name(None);
let style_context = input_group.get_style_context();
Self::remove_tmp_file(self.icon_filename.clone());

self.input_group.set_text("");
self.url_input.set_text("");

self.icon_filename.set_label("");
self.group_id.set_label("");

self.icon_error.set_label("");
self.icon_error.set_visible(false);

self.save_button.set_sensitive(true);
self.icon_reload.set_sensitive(true);
self.icon_delete.set_sensitive(true);
self.image_input.set_from_icon_name(Some("content-loading-symbolic"), IconSize::Button);

self.input_group.set_property_primary_icon_name(None);
let style_context = self.input_group.get_style_context();
style_context.remove_class("error");
}

Expand Down Expand Up @@ -230,7 +220,6 @@ impl AddGroupWindow {
let gui = gui.clone();
Box::new(move |_| {
gui.add_group.reset();
gui.add_group.input_group.set_text("");

MainWindow::switch_to(&gui, Display::DisplayAccounts);
})
Expand Down
27 changes: 18 additions & 9 deletions src/ui/edit_account_window.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::sync::{Arc, Mutex};

use gtk::prelude::*;
use gtk::Builder;
use rusqlite::Connection;

use crate::helpers::ConfigManager;
use crate::main_window::{Display, MainWindow};
use crate::model::{Account, AccountGroup};
use crate::ui::{AccountsWindow, ValidationError};
use gtk::prelude::*;
use gtk::Builder;
use rusqlite::Connection;
use std::sync::{Arc, Mutex};

#[derive(Clone, Debug)]
pub struct EditAccountWindow {
Expand Down Expand Up @@ -74,7 +76,7 @@ impl EditAccountWindow {
result
}

pub fn reset(&self) {
pub fn reset_errors(&self) {
let name = self.input_name.clone();
let secret = self.input_secret.clone();
let group = self.input_group.clone();
Expand All @@ -90,6 +92,16 @@ impl EditAccountWindow {
style_context.remove_class("error");
}

pub fn reset(&self) {
self.input_name.set_text("");
self.input_account_id.set_text("");

let buffer = self.input_secret.get_buffer().unwrap();
buffer.set_text("");

self.reset_errors();
}

pub fn set_group_dropdown(&self, group_id: Option<u32>, groups: &[AccountGroup]) {
self.input_group.remove_all();

Expand Down Expand Up @@ -125,7 +137,6 @@ impl EditAccountWindow {
Box::new(move |_| {
let edit_account_window = gui.edit_account_window.clone();
edit_account_window.reset();
edit_account_window.input_name.set_text("");

let buffer = edit_account_window.input_secret.get_buffer().unwrap();
buffer.set_text("");
Expand All @@ -138,7 +149,7 @@ impl EditAccountWindow {
with_action(&gui, connection, gui.edit_account_window.save_button.clone(), |connection, gui| {
let gui = gui.clone();
Box::new(move |_| {
gui.edit_account_window.reset();
gui.edit_account_window.reset_errors();

if let Ok(()) = gui.edit_account_window.validate() {
let edit_account_window = gui.edit_account_window.clone();
Expand All @@ -165,12 +176,10 @@ impl EditAccountWindow {
match account_id.get_buffer().get_text().parse() {
Ok(account_id) => {
let mut account = Account::new(account_id, group_id, name.as_str(), secret.as_str());

ConfigManager::update_account(&connection, &mut account).unwrap();
}
Err(_) => {
let mut account = Account::new(0, group_id, name.as_str(), secret.as_str());

ConfigManager::save_account(&connection, &mut account).unwrap();
}
};
Expand Down

0 comments on commit 6bd6330

Please sign in to comment.