Skip to content

Commit

Permalink
NOJ - Adding duplicate account name checks (#156)
Browse files Browse the repository at this point in the history
NOJ - Adding duplicate account name checks (#156)
  • Loading branch information
gr211 authored Jul 23, 2024
1 parent f519dec commit 4ec5a84
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 38 deletions.
39 changes: 24 additions & 15 deletions data/resources/gtk/ui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,13 @@
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-right">5</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-bottom">5</property>
<property name="label-xalign">0</property>
<property name="shadow-type">in</property>
<child>
<!-- n-columns=2 n-rows=6 -->
<!-- n-columns=2 n-rows=7 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
Expand All @@ -245,7 +244,6 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="margin-left">5</property>
<property name="margin-start">5</property>
<property name="margin-top">20</property>
<property name="label" translatable="yes">Group</property>
Expand All @@ -266,7 +264,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
Expand All @@ -275,7 +273,6 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">Group</property>
<property name="margin-right">5</property>
<property name="margin-end">5</property>
<property name="margin-top">20</property>
<property name="hexpand">True</property>
Expand All @@ -291,7 +288,6 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="valign">start</property>
<property name="margin-left">5</property>
<property name="margin-start">5</property>
<property name="label" translatable="yes">Secret</property>
<property name="xalign">0</property>
Expand Down Expand Up @@ -339,9 +335,8 @@
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-left">5</property>
<property name="margin-start">5</property>
<property name="margin-bottom">20</property>
<property name="margin-bottom">10</property>
<property name="label" translatable="yes">use QR code</property>
<property name="use-underline">True</property>
</object>
Expand All @@ -363,7 +358,6 @@
<property name="height-request">140</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-right">5</property>
<property name="margin-end">5</property>
<property name="label-xalign">0</property>
<property name="shadow-type">none</property>
Expand All @@ -373,8 +367,6 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="tooltip-text" translatable="yes">Secret</property>
<property name="margin-left">10</property>
<property name="margin-right">5</property>
<property name="margin-start">10</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
Expand Down Expand Up @@ -412,9 +404,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="margin-right">5</property>
<property name="margin-end">5</property>
<property name="margin-top">30</property>
<property name="margin-bottom">20</property>
<property name="column-spacing">5</property>
<child>
Expand Down Expand Up @@ -452,9 +442,28 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">5</property>
<property name="top-attach">6</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="edit_account_icon_error">
<property name="name">edit_account_icon_error</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-end">5</property>
<property name="wrap">True</property>
<property name="max-width-chars">0</property>
<property name="track-visited-links">False</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
Expand Down Expand Up @@ -691,7 +700,7 @@
<property name="can-focus">False</property>
<property name="margin-end">5</property>
<property name="wrap">True</property>
<property name="max-width-chars">23</property>
<property name="max-width-chars">0</property>
<property name="track-visited-links">False</property>
<property name="xalign">0</property>
</object>
Expand Down
10 changes: 2 additions & 8 deletions data/resources/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
border-color: @error_color;
}

/*
* Note: apparently pseudo class :indeterminate
* works (also) with widget state StateFlags::INCONSISTENT.
*
* StateFlags::INDETERMINATE is deprecated.
*/
.edit_account_input_secret_frame:indeterminate {
.edit_account_input_secret_frame.error {
border-color: @error_color;
}

Expand Down Expand Up @@ -90,7 +84,7 @@
font-size: 80%;
}

#add_group_icon_error {
#add_group_icon_error, #edit_account_icon_error {
color: @error_color;
}

Expand Down
6 changes: 6 additions & 0 deletions po/en_GB.po
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,9 @@ msgstr "Name of group"

msgid "Name of account"
msgstr "Name of account"

msgid "Group name already exists"
msgstr "Group name already exists"

msgid "Account name already exists"
msgstr "Account name already exists"
6 changes: 6 additions & 0 deletions po/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,9 @@ msgstr "Nom du groupe"

msgid "Name of account"
msgstr "Nom du compte"

msgid "Group name already exists"
msgstr "Un groupe existe avec ce nom"

msgid "Account name already exists"
msgstr "Un compte existe avec ce nom"
33 changes: 33 additions & 0 deletions src/helpers/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,23 @@ impl Database {
.map_err(RepositoryError::SqlError)
}

pub fn account_exists(connection: &Connection, name: &str, group_id: u32) -> Result<Option<u32>> {
let mut stmt = connection.prepare("SELECT id FROM accounts WHERE label = :label AND group_id = :group_id")?;

stmt.query_row(
named_params! {
":label": name,
":group_id": group_id,
},
|row| {
let account_id: u32 = row.get_unwrap(0);
Ok(account_id)
},
)
.optional()
.map_err(RepositoryError::SqlError)
}

pub fn get_group(connection: &Connection, group_id: u32) -> Result<AccountGroup> {
let mut stmt = connection.prepare("SELECT id, name, icon, url, collapsed FROM groups WHERE id = :group_id")?;

Expand Down Expand Up @@ -509,6 +526,22 @@ mod tests {
assert!(result.is_none());
}

#[test]
fn account_exists() {
let mut connection = Connection::open_in_memory().unwrap();

runner::run(&mut connection).unwrap();

let mut account = Account::new(0, 1, "label", "secret", LOCAL);
let _ = Database::save_account(&connection, &mut account);

let result = Database::account_exists(&connection, "label", 1).unwrap();
assert!(result.is_some());

let result = Database::account_exists(&connection, "non_existent", 1).unwrap();
assert!(result.is_none());
}

#[test]
fn save_group_and_accounts() {
let mut connection = Connection::open_in_memory().unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/ui/add_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::prelude::*;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk::{Builder, IconSize};
Expand Down Expand Up @@ -77,7 +78,7 @@ impl AddGroupWindow {
let group_id = group_id.parse().map(Some).unwrap_or(None);

if existing_group.is_some() && existing_group != group_id {
self.icon_error.set_label("Group name already exists");
self.icon_error.set_label(&gettext("Group name already exists"));
self.icon_error.set_visible(true);
return Err(ValidationError::FieldError("name".to_owned()));
}
Expand Down
Loading

0 comments on commit 4ec5a84

Please sign in to comment.