Headerbar: Do not allow insertion of duplicate urls.

This commit is contained in:
Jordan Petridis 2018-01-27 14:40:51 +02:00
parent 77a52bdc8c
commit a4660a0700
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 21 additions and 8 deletions

View File

@ -91,6 +91,7 @@ Tobias Bernard
<object class="GtkButton" id="add_button">
<property name="label" translatable="yes">Add</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<style>

View File

@ -2,6 +2,7 @@ use gtk;
use gtk::prelude::*;
use hammond_data::Source;
use hammond_data::dbqueries;
use url::Url;
use std::sync::Arc;
@ -135,6 +136,7 @@ fn on_add_bttn_clicked(entry: &gtk::Entry, sender: Sender<Action>) {
let source = Source::from_url(&url);
if source.is_ok() {
entry.set_text("");
sender.send(Action::UpdateSources(source.ok())).unwrap();
} else {
error!("Something went wrong.");
@ -147,18 +149,28 @@ fn on_url_change(entry: &gtk::Entry, result: &gtk::Label, add_button: &gtk::Butt
debug!("Url: {}", uri);
let url = Url::parse(&uri);
// TODO: refactor to avoid duplication
match url {
// TODO: Check if the url exists
Ok(_u) => {
add_button.set_sensitive(true);
result.hide();
Ok(u) => {
if !dbqueries::source_exists(u.as_str()).unwrap() {
add_button.set_sensitive(true);
result.hide();
result.set_label("");
} else {
add_button.set_sensitive(false);
result.set_label("Show already exists.");
result.show();
}
}
// TODO: refactor to avoid duplication
Err(err) => {
add_button.set_sensitive(false);
result.set_label("Invalid url.");
result.show();
error!("Error: {}", err);
if !uri.is_empty() {
result.set_label("Invalid url.");
result.show();
error!("Error: {}", err);
} else {
result.hide();
}
}
}
}