Tiny misc code improvements

This commit is contained in:
Skye 2023-07-01 13:46:13 -04:00 committed by Scott Lamb
parent c2d226d58e
commit 1fde947f36
5 changed files with 31 additions and 39 deletions

View File

@ -198,7 +198,7 @@ pub struct NewLimit {
/// This is expected to be performed from `moonfire-nvr config` when no syncer is running.
/// It potentially flushes the database twice (before and after the actual deletion).
pub fn lower_retention(
db: Arc<db::Database>,
db: &Arc<db::Database>,
dir_id: i32,
limits: &[NewLimit],
) -> Result<(), Error> {

View File

@ -77,28 +77,28 @@ fn get_camera(siv: &mut Cursive) -> Camera {
};
for &t in &db::ALL_STREAM_TYPES {
let url = siv
.find_name::<views::EditView>(&format!("{}_url", t.as_str()))
.find_name::<views::EditView>(&format!("{}_url", t))
.unwrap()
.get_content()
.as_str()
.to_owned();
let record = siv
.find_name::<views::Checkbox>(&format!("{}_record", t.as_str()))
.find_name::<views::Checkbox>(&format!("{}_record", t))
.unwrap()
.is_checked();
let rtsp_transport = *siv
.find_name::<views::SelectView<&'static str>>(&format!("{}_rtsp_transport", t.as_str()))
.find_name::<views::SelectView<&'static str>>(&format!("{}_rtsp_transport", t))
.unwrap()
.selection()
.unwrap();
let flush_if_sec = siv
.find_name::<views::EditView>(&format!("{}_flush_if_sec", t.as_str()))
.find_name::<views::EditView>(&format!("{}_flush_if_sec", t))
.unwrap()
.get_content()
.as_str()
.to_owned();
let sample_file_dir_id = *siv
.find_name::<views::SelectView<Option<i32>>>(&format!("{}_sample_file_dir", t.as_str()))
.find_name::<views::SelectView<Option<i32>>>(&format!("{}_sample_file_dir", t))
.unwrap()
.selection()
.unwrap();
@ -367,9 +367,8 @@ fn confirm_deletion(siv: &mut Cursive, db: &Arc<db::Database>, id: i32, to_delet
let l = db.lock();
for (&stream_id, stream) in l.streams_by_id() {
if stream.camera_id == id {
let dir_id = match stream.sample_file_dir_id {
Some(d) => d,
None => continue,
let Some(dir_id) = stream.sample_file_dir_id else {
continue
};
let l = zero_limits
.entry(dir_id)
@ -406,7 +405,7 @@ fn lower_retention(
let dirs_to_open: Vec<_> = zero_limits.keys().copied().collect();
db.lock().open_sample_file_dirs(&dirs_to_open[..])?;
for (&dir_id, l) in &zero_limits {
writer::lower_retention(db.clone(), dir_id, l)?;
writer::lower_retention(db, dir_id, l)?;
}
Ok(())
}
@ -441,10 +440,7 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
let camera_list = views::ListView::new()
.child(
"id",
views::TextView::new(match *item {
None => "<new>".to_string(),
Some(id) => id.to_string(),
}),
views::TextView::new(item.map_or_else(|| "<new>".to_string(), |id| id.to_string())),
)
.child("uuid", views::TextView::new("<new>").with_name("uuid"))
.child("short name", views::EditView::new().with_name("short_name"))
@ -481,18 +477,18 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
views::EditView::new()
.on_edit(move |siv, content, _pos| {
let test_button = siv
.find_name::<views::Button>(&format!("{}_test", type_.as_str()))
.find_name::<views::Button>(&format!("{}_test", type_))
.unwrap();
edit_stream_url(type_, content, test_button);
})
.with_name(format!("{}_url", type_.as_str()))
.with_name(format!("{}_url", type_))
.full_width(),
)
.child(views::DummyView)
.child(
views::Button::new("Test", move |siv| press_test(siv, type_))
.disabled()
.with_name(format!("{}_test", type_.as_str())),
.with_name(format!("{}_test", type_)),
),
)
.child(
@ -500,30 +496,30 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
views::SelectView::<Option<i32>>::new()
.with_all(dirs.iter().map(|(p, id)| (p.display().to_string(), *id)))
.popup()
.with_name(format!("{}_sample_file_dir", type_.as_str())),
.with_name(format!("{}_sample_file_dir", type_)),
)
.child(
"record",
views::Checkbox::new().with_name(format!("{}_record", type_.as_str())),
views::Checkbox::new().with_name(format!("{}_record", type_)),
)
.child(
"rtsp_transport",
views::SelectView::<&str>::new()
.with_all([("(default)", ""), ("tcp", "tcp"), ("udp", "udp")])
.popup()
.with_name(format!("{}_rtsp_transport", type_.as_str())),
.with_name(format!("{}_rtsp_transport", type_)),
)
.child(
"flush_if_sec",
views::EditView::new().with_name(format!("{}_flush_if_sec", type_.as_str())),
views::EditView::new().with_name(format!("{}_flush_if_sec", type_)),
)
.child(
"usage/capacity",
views::TextView::new("").with_name(format!("{}_usage_cap", type_.as_str())),
views::TextView::new("").with_name(format!("{}_usage_cap", type_)),
)
.min_height(5);
layout.add_child(views::DummyView);
layout.add_child(views::TextView::new(format!("{} stream", type_.as_str())));
layout.add_child(views::TextView::new(format!("{} stream", type_)));
layout.add_child(list);
}
@ -596,14 +592,13 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
})
},
);
dialog.call_on_name(
&format!("{}_flush_if_sec", t.as_str()),
|v: &mut views::EditView| v.set_content(s.config.flush_if_sec.to_string()),
);
dialog.call_on_name(&format!("{}_flush_if_sec", t), |v: &mut views::EditView| {
v.set_content(s.config.flush_if_sec.to_string())
});
}
log::debug!("setting {} dir to {}", t.as_str(), selected_dir);
dialog.call_on_name(
&format!("{}_sample_file_dir", t.as_str()),
&format!("{}_sample_file_dir", t),
|v: &mut views::SelectView<Option<i32>>| v.set_selection(selected_dir),
);
}
@ -616,8 +611,7 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
.config
.onvif_base_url
.as_ref()
.map(Url::as_str)
.unwrap_or(""),
.map_or("", Url::as_str),
),
("username", &camera.config.username),
("password", &camera.config.password),
@ -666,7 +660,7 @@ pub fn top_dialog(db: &Arc<db::Database>, siv: &mut Cursive) {
let db = db.clone();
move |siv, item| edit_camera_dialog(&db, siv, item)
})
.item("<new camera>".to_string(), None)
.item("<new camera>", None)
.with_all(
db.lock()
.cameras_by_id()

View File

@ -140,7 +140,7 @@ fn actually_delete(model: &RefCell<Model>, siv: &mut Cursive) {
let mut l = model.db.lock();
l.open_sample_file_dirs(&[model.dir_id]).unwrap(); // TODO: don't unwrap.
}
if let Err(e) = writer::lower_retention(model.db.clone(), model.dir_id, &new_limits[..]) {
if let Err(e) = writer::lower_retention(&model.db, model.dir_id, &new_limits[..]) {
siv.add_layer(
views::Dialog::text(format!("Unable to delete excess video: {e}"))
.title("Error")

View File

@ -47,9 +47,9 @@ pub fn run(args: Args) -> Result<i32, Error> {
views::Dialog::around(
views::SelectView::<fn(&Arc<db::Database>, &mut Cursive)>::new()
.on_submit(move |siv, item| item(&db, siv))
.item("Cameras and streams".to_string(), cameras::top_dialog)
.item("Directories and retention".to_string(), dirs::top_dialog)
.item("Users".to_string(), users::top_dialog),
.item("Cameras and streams", cameras::top_dialog)
.item("Directories and retention", dirs::top_dialog)
.item("Users", users::top_dialog),
)
.button("Quit", |siv| siv.quit())
.title("Main menu"),

View File

@ -127,9 +127,7 @@ fn edit_user_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: Option<i32>
let l = db.lock();
let u = item.map(|id| l.users_by_id().get(&id).unwrap());
username = u.map(|u| u.username.clone()).unwrap_or_default();
id_str = item
.map(|id| id.to_string())
.unwrap_or_else(|| "<new>".to_string());
id_str = item.map_or_else(|| "<new>".to_string(), |id| id.to_string());
has_password = u.map(|u| u.has_password()).unwrap_or(false);
permissions = u.map(|u| u.permissions.clone()).unwrap_or_default();
}
@ -138,7 +136,7 @@ fn edit_user_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: Option<i32>
.child(
"username",
views::EditView::new()
.content(username.clone())
.content(&username)
.with_name("username"),
);
let mut layout = views::LinearLayout::vertical()