Skip to content

Commit

Permalink
QR code - file dialog performance issue fix (#72)
Browse files Browse the repository at this point in the history
QR code - file dialog performance
  • Loading branch information
gr211 authored Dec 25, 2020
1 parent 55a8972 commit 86b71de
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
1 change: 0 additions & 1 deletion data/resources/gtk/ui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,6 @@ See the <a href="https://www.gnu.org/licenses/gpl-3.0.html">GNU General Pu
<property name="modal">True</property>
<property name="window-position">center</property>
<property name="type-hint">dialog</property>
<property name="attached-to">add_group_image_button</property>
<property name="create-folders">False</property>
<property name="filter">icon_filter</property>
<child internal-child="vbox">
Expand Down
3 changes: 3 additions & 0 deletions po/en_GB.po
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@ msgstr "filter accounts"

msgid "Invalid QR code"
msgstr "Invalid QR code"

msgid "Processing QR code"
msgstr "Processing QR code..."
3 changes: 3 additions & 0 deletions po/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@ msgstr "filtre de comptes"

msgid "Invalid QR code"
msgstr "QR code invalide"

msgid "Processing QR code"
msgstr "QR code en cours de traitement..."
85 changes: 52 additions & 33 deletions src/ui/edit_account_window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::{Arc, Mutex};

use gettextrs::*;
use glib::Sender;
use gtk::prelude::*;
use gtk::Builder;
use log::{debug, warn};
Expand Down Expand Up @@ -127,58 +128,76 @@ impl EditAccountWindow {
}
}

async fn process_qr_code(path: String, tx: Sender<(bool, String)>) {
let _ = match image::open(&path).map(|v| v.to_luma8()) {
Ok(img) => {
let mut luma = PreparedImage::prepare(img);
let grids = luma.detect_grids();

if grids.len() != 1 {
warn!("No grids found in {}", path);
tx.send((false, "Invalid QR code".to_owned()))
} else {
match grids[0].decode() {
Ok((_, content)) => tx.send((true, content)),
Err(e) => {
warn!("{}", e);
tx.send((false, "Invalid QR code".to_owned()))
}
}
}
}
Err(e) => {
warn!("{}", e);
tx.send((false, "Invalid QR code".to_owned()))
}
};
}

fn qrcode_action(gui: &MainWindow) {
let qr_button = gui.edit_account_window.qr_button.clone();
let dialog = gui.add_group.image_dialog.clone();

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

{
let input_secret = gui.edit_account_window.input_secret.clone();
rx.attach(None, move |path| {
let gui = gui.clone();
rx.attach(None, move |(ok, qr_code)| {
let buffer = input_secret.get_buffer().unwrap();

match image::open(&path).map(|v| v.to_luma8()) {
Ok(img) => {
let mut luma = PreparedImage::prepare(img);
let grids = luma.detect_grids();

if grids.len() != 1 {
buffer.set_text(&gettext("Invalid QR code"));
warn!("Could not detect grids in {}", path);
} else {
match grids[0].decode() {
Ok((_, content)) => buffer.set_text(content.as_str()),
Err(e) => {
buffer.set_text(&gettext("Invalid QR code"));
warn!("{:?}", e)
}
}
}
}
Err(e) => {
buffer.set_text(&gettext("Invalid QR code"));
warn!("{:?}", e)
}
gui.edit_account_window.reset_errors();

if ok {
buffer.set_text(qr_code.as_str());
let _ = gui.edit_account_window.validate();
} else {
buffer.set_text(&gettext(qr_code));
let _ = gui.edit_account_window.validate();
}

glib::Continue(true)
});
}

let input_secret = gui.edit_account_window.input_secret.clone();
qr_button.connect_clicked(move |_| match dialog.run() {
gtk::ResponseType::Accept => {
let path = dialog.get_filename().unwrap();
debug!("path: {}", path.display());
let pool = gui.pool.clone();

let buffer = input_secret.get_buffer().unwrap();
buffer.set_text(&gettext("Processing QR code"));
qr_button.connect_clicked(move |_| {
let tx = tx.clone();
match dialog.run() {
gtk::ResponseType::Accept => {
let path = dialog.get_filename().unwrap();
debug!("path: {}", path.display());

dialog.hide();
tx.send(format!("{}", path.display())).unwrap();
let buffer = input_secret.get_buffer().unwrap();
buffer.set_text(&gettext("Processing QR code"));

dialog.hide();
pool.spawn_ok(Self::process_qr_code(path.to_str().unwrap().to_owned(), tx));
}
_ => dialog.hide(),
}
_ => dialog.hide(),
});
}

Expand Down

0 comments on commit 86b71de

Please sign in to comment.