Skip to content

Commit c24f473

Browse files
committed
Editors API improvements / cleanup
1 parent af4a043 commit c24f473

File tree

1 file changed

+86
-88
lines changed

1 file changed

+86
-88
lines changed

rust/src/android_editor.rs

+86-88
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::mpsc::{self, Receiver};
2+
13
// Copyright 2017 astonbitecode
24
// This file is part of rust-keylock password manager.
35
//
@@ -15,17 +17,12 @@
1517
// along with rust-keylock. If not, see <http://www.gnu.org/licenses/>.
1618
use j4rs::{Instance, InvocationArg, Jvm};
1719
use log::*;
18-
use rust_keylock::{
19-
AsyncEditor,
20-
Menu,
21-
MessageSeverity,
22-
RklConfiguration,
23-
Safe, UserOption,
24-
UserSelection,
25-
};
26-
use std::sync::mpsc::{self, Receiver};
27-
use super::japi;
20+
21+
use rust_keylock::{AsyncEditor, Entry, EntryPresentationType, Menu, MessageSeverity, UserOption, UserSelection};
2822
use rust_keylock::dropbox::DropboxConfiguration;
23+
use rust_keylock::nextcloud::NextcloudConfiguration;
24+
25+
use super::japi;
2926

3027
pub struct AndroidImpl {
3128
jvm: Jvm,
@@ -74,14 +71,8 @@ impl AsyncEditor for AndroidImpl {
7471
japi::handle_instance_receiver_result(instance_receiver)
7572
}
7673

77-
fn show_menu(&self,
78-
menu: &Menu,
79-
safe: &Safe,
80-
configuration: &RklConfiguration)
81-
-> Receiver<UserSelection> {
82-
debug!("Opening menu '{:?}' with entries size {}",
83-
menu,
84-
safe.get_entries().len());
74+
fn show_menu(&self, menu: &Menu) -> Receiver<UserSelection> {
75+
debug!("Opening menu '{:?}'", menu);
8576

8677
let instance_receiver_res = match menu {
8778
&Menu::Main => {
@@ -90,50 +81,6 @@ impl AsyncEditor for AndroidImpl {
9081
"apply",
9182
&[InvocationArg::from("Main")])
9283
}
93-
&Menu::EntriesList(_) => {
94-
let java_entries: Vec<japi::JavaEntry> = safe.get_entries().iter()
95-
.map(|entry| japi::JavaEntry::new(entry))
96-
.collect();
97-
let filter = if safe.get_filter().is_empty() {
98-
"null".to_string()
99-
} else {
100-
safe.get_filter().clone()
101-
};
102-
self.jvm.invoke_to_channel(
103-
&self.show_entries_set_cb,
104-
"apply",
105-
&[
106-
InvocationArg::from((
107-
java_entries.as_slice(),
108-
"org.astonbitecode.rustkeylock.api.JavaEntry",
109-
&self.jvm)),
110-
InvocationArg::from(filter)])
111-
}
112-
&Menu::ShowEntry(index) => {
113-
let entry = safe.get_entry_decrypted(index);
114-
self.jvm.invoke_to_channel(
115-
&self.show_entry_cb,
116-
"apply",
117-
&[
118-
InvocationArg::new(&japi::JavaEntry::new(&entry), "org.astonbitecode.rustkeylock.api.JavaEntry"),
119-
InvocationArg::from(index as i32),
120-
InvocationArg::from(false),
121-
InvocationArg::from(false)
122-
])
123-
}
124-
&Menu::DeleteEntry(index) => {
125-
let entry = japi::JavaEntry::new(safe.get_entry(index));
126-
127-
self.jvm.invoke_to_channel(
128-
&self.show_entry_cb,
129-
"apply",
130-
&[
131-
InvocationArg::new(&entry, "org.astonbitecode.rustkeylock.api.JavaEntry"),
132-
InvocationArg::from(index as i32),
133-
InvocationArg::from(false),
134-
InvocationArg::from(true)
135-
])
136-
}
13784
&Menu::NewEntry => {
13885
let empty_entry = japi::JavaEntry::empty();
13986
// In order to denote that this is a new entry, put -1 as index
@@ -147,18 +94,6 @@ impl AsyncEditor for AndroidImpl {
14794
InvocationArg::from(false)
14895
])
14996
}
150-
&Menu::EditEntry(index) => {
151-
let selected_entry = safe.get_entry_decrypted(index);
152-
self.jvm.invoke_to_channel(
153-
&self.show_entry_cb,
154-
"apply",
155-
&[
156-
InvocationArg::new(&japi::JavaEntry::new(&selected_entry), "org.astonbitecode.rustkeylock.api.JavaEntry"),
157-
InvocationArg::from(index as i32),
158-
InvocationArg::from(true),
159-
InvocationArg::from(false)
160-
])
161-
}
16297
&Menu::ExportEntries => {
16398
self.jvm.invoke_to_channel(
16499
&self.show_menu_cb,
@@ -171,20 +106,6 @@ impl AsyncEditor for AndroidImpl {
171106
"apply",
172107
&[InvocationArg::from("ImportEntries")])
173108
}
174-
&Menu::ShowConfiguration => {
175-
let conf_strings = vec![
176-
configuration.nextcloud.server_url.clone(),
177-
configuration.nextcloud.username.clone(),
178-
configuration.nextcloud.decrypted_password().unwrap(),
179-
configuration.nextcloud.use_self_signed_certificate.to_string(),
180-
DropboxConfiguration::dropbox_url(),
181-
configuration.dropbox.decrypted_token().unwrap(),
182-
];
183-
self.jvm.invoke_to_channel(
184-
&self.edit_configuration_cb,
185-
"apply",
186-
&[InvocationArg::from((conf_strings.as_slice(), &self.jvm))])
187-
}
188109
&Menu::Current => {
189110
self.jvm.invoke_to_channel(
190111
&self.show_menu_cb,
@@ -201,6 +122,83 @@ impl AsyncEditor for AndroidImpl {
201122
japi::handle_instance_receiver_result(instance_receiver_res)
202123
}
203124

125+
fn show_entries(&self, entries: Vec<Entry>, filter: String) -> Receiver<UserSelection> {
126+
let java_entries: Vec<japi::JavaEntry> = entries.iter()
127+
.map(|entry| japi::JavaEntry::new(entry))
128+
.collect();
129+
let filter = if filter.is_empty() {
130+
"null".to_string()
131+
} else {
132+
filter
133+
};
134+
135+
let instance_receiver_res = self.jvm.invoke_to_channel(
136+
&self.show_entries_set_cb,
137+
"apply",
138+
&[
139+
InvocationArg::from((
140+
java_entries.as_slice(),
141+
"org.astonbitecode.rustkeylock.api.JavaEntry",
142+
&self.jvm)),
143+
InvocationArg::from(filter)]);
144+
japi::handle_instance_receiver_result(instance_receiver_res)
145+
}
146+
147+
fn show_entry(&self, entry: Entry, index: usize, presentation_type: EntryPresentationType) -> Receiver<UserSelection> {
148+
let instance_receiver_res = match presentation_type {
149+
EntryPresentationType::View => {
150+
self.jvm.invoke_to_channel(
151+
&self.show_entry_cb,
152+
"apply",
153+
&[
154+
InvocationArg::new(&japi::JavaEntry::new(&entry), "org.astonbitecode.rustkeylock.api.JavaEntry"),
155+
InvocationArg::from(index as i32),
156+
InvocationArg::from(false),
157+
InvocationArg::from(false)
158+
])
159+
}
160+
EntryPresentationType::Delete => {
161+
self.jvm.invoke_to_channel(
162+
&self.show_entry_cb,
163+
"apply",
164+
&[
165+
InvocationArg::new(&japi::JavaEntry::new(&entry), "org.astonbitecode.rustkeylock.api.JavaEntry"),
166+
InvocationArg::from(index as i32),
167+
InvocationArg::from(false),
168+
InvocationArg::from(true)
169+
])
170+
}
171+
EntryPresentationType::Edit => {
172+
self.jvm.invoke_to_channel(
173+
&self.show_entry_cb,
174+
"apply",
175+
&[
176+
InvocationArg::new(&japi::JavaEntry::new(&entry), "org.astonbitecode.rustkeylock.api.JavaEntry"),
177+
InvocationArg::from(index as i32),
178+
InvocationArg::from(true),
179+
InvocationArg::from(false)
180+
])
181+
}
182+
};
183+
184+
japi::handle_instance_receiver_result(instance_receiver_res)
185+
}
186+
187+
fn show_configuration(&self, nextcloud: NextcloudConfiguration, dropbox: DropboxConfiguration) -> Receiver<UserSelection> {
188+
let conf_strings = vec![
189+
nextcloud.server_url.clone(),
190+
nextcloud.username.clone(),
191+
nextcloud.decrypted_password().unwrap(),
192+
nextcloud.use_self_signed_certificate.to_string(),
193+
DropboxConfiguration::dropbox_url(),
194+
dropbox.decrypted_token().unwrap()];
195+
let instance_receiver_res = self.jvm.invoke_to_channel(
196+
&self.edit_configuration_cb,
197+
"apply",
198+
&[InvocationArg::from((conf_strings.as_slice(), &self.jvm))]);
199+
japi::handle_instance_receiver_result(instance_receiver_res)
200+
}
201+
204202
fn exit(&self, contents_changed: bool) -> Receiver<UserSelection> {
205203
debug!("Exiting rust-keylock...");
206204
if contents_changed {

0 commit comments

Comments
 (0)