Skip to content

Commit ac35bf1

Browse files
committed
refactor: set file early
1 parent 8a4a415 commit ac35bf1

File tree

1 file changed

+30
-48
lines changed

1 file changed

+30
-48
lines changed

src/recording.rs

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{anyhow, ensure, Context, Error, Result};
1+
use anyhow::{ensure, Context, Error, Result};
22
use gettextrs::gettext;
33
use gst::prelude::*;
44
use gtk::{
@@ -7,7 +7,7 @@ use gtk::{
77
};
88

99
use std::{
10-
cell::{Cell, RefCell},
10+
cell::{Cell, OnceCell, RefCell},
1111
error, fmt,
1212
os::unix::prelude::RawFd,
1313
rc::Rc,
@@ -60,10 +60,7 @@ struct BoxedResult(Rc<Result<gio::File>>);
6060

6161
mod imp {
6262
use super::*;
63-
use glib::{
64-
once_cell::{sync::Lazy, unsync::OnceCell},
65-
subclass::Signal,
66-
};
63+
use glib::{once_cell::sync::Lazy, subclass::Signal};
6764
use gst::bus::BusWatchGuard;
6865

6966
#[derive(Debug, Default, glib::Properties)]
@@ -235,6 +232,11 @@ impl Recording {
235232
|| gettext("Failed to start recording"),
236233
)?;
237234
imp.pipeline.set(pipeline.clone()).unwrap();
235+
let location = pipeline
236+
.by_name("filesink")
237+
.context("Element filesink not found on pipeline")?
238+
.property::<String>("location");
239+
imp.file.set(gio::File::for_path(location)).unwrap();
238240
let bus_watch_guard = pipeline
239241
.bus()
240242
.unwrap()
@@ -352,20 +354,6 @@ impl Recording {
352354
)
353355
}
354356

355-
fn file(&self) -> Result<&gio::File> {
356-
let imp = self.imp();
357-
self.imp().file.get_or_try_init(|| {
358-
let location = imp
359-
.pipeline
360-
.get()
361-
.ok_or_else(|| anyhow!("Pipeline not set"))?
362-
.by_name("filesink")
363-
.ok_or_else(|| anyhow!("Element filesink not found on pipeline"))?
364-
.property::<String>("location");
365-
Ok(gio::File::for_path(location))
366-
})
367-
}
368-
369357
fn set_state(&self, state: State) {
370358
if state == self.state() {
371359
return;
@@ -375,6 +363,13 @@ impl Recording {
375363
self.notify_state();
376364
}
377365

366+
fn file(&self) -> &gio::File {
367+
self.imp()
368+
.file
369+
.get()
370+
.expect("file not set, make sure to start recording first")
371+
}
372+
378373
fn pipeline(&self) -> &gst::Pipeline {
379374
self.imp()
380375
.pipeline
@@ -402,19 +397,15 @@ impl Recording {
402397

403398
/// Deletes recording file on background
404399
fn delete_file(&self) {
405-
if let Ok(file) = self.file() {
406-
file.delete_async(
407-
glib::Priority::DEFAULT_IDLE,
408-
gio::Cancellable::NONE,
409-
|res| {
410-
if let Err(err) = res {
411-
tracing::warn!("Failed to delete recording file: {:?}", err);
412-
}
413-
},
414-
);
415-
} else {
416-
tracing::error!("Failed to delete recording file: Failed to get file");
417-
}
400+
self.file().delete_async(
401+
glib::Priority::DEFAULT_IDLE,
402+
gio::Cancellable::NONE,
403+
|res| {
404+
if let Err(err) = res {
405+
tracing::warn!("Failed to delete recording file: {:?}", err);
406+
}
407+
},
408+
);
418409
}
419410

420411
fn update_duration(&self) {
@@ -461,20 +452,11 @@ impl Recording {
461452
let error = if e.error().matches(gst::ResourceError::OpenWrite) {
462453
error.help(
463454
gettext("Make sure that the saving location exists and is accessible."),
464-
if let Some(ref path) = self
465-
.file()
466-
.ok()
467-
.and_then(|f| f.path())
468-
.and_then(|path| path.parent().map(|p| p.to_owned()))
469-
{
470-
gettext_f(
471-
// Translators: Do NOT translate the contents between '{' and '}', this is a variable name.
472-
"Failed to open “{path}” for writing",
473-
&[("path", &path.display().to_string())],
474-
)
475-
} else {
476-
gettext("Failed to open file for writing")
477-
},
455+
gettext_f(
456+
// Translators: Do NOT translate the contents between '{' and '}', this is a variable name.
457+
"Failed to open “{path}” for writing",
458+
&[("path", &self.file().uri())],
459+
),
478460
)
479461
} else {
480462
error
@@ -502,7 +484,7 @@ impl Recording {
502484
source_id.remove();
503485
}
504486

505-
self.set_finished(Ok(self.file().unwrap().clone()));
487+
self.set_finished(Ok(self.file().clone()));
506488

507489
glib::ControlFlow::Break
508490
}

0 commit comments

Comments
 (0)