1
- use anyhow:: { anyhow , ensure, Context , Error , Result } ;
1
+ use anyhow:: { ensure, Context , Error , Result } ;
2
2
use gettextrs:: gettext;
3
3
use gst:: prelude:: * ;
4
4
use gtk:: {
@@ -7,7 +7,7 @@ use gtk::{
7
7
} ;
8
8
9
9
use std:: {
10
- cell:: { Cell , RefCell } ,
10
+ cell:: { Cell , OnceCell , RefCell } ,
11
11
error, fmt,
12
12
os:: unix:: prelude:: RawFd ,
13
13
rc:: Rc ,
@@ -60,10 +60,7 @@ struct BoxedResult(Rc<Result<gio::File>>);
60
60
61
61
mod imp {
62
62
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 } ;
67
64
use gst:: bus:: BusWatchGuard ;
68
65
69
66
#[ derive( Debug , Default , glib:: Properties ) ]
@@ -235,6 +232,11 @@ impl Recording {
235
232
|| gettext ( "Failed to start recording" ) ,
236
233
) ?;
237
234
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 ( ) ;
238
240
let bus_watch_guard = pipeline
239
241
. bus ( )
240
242
. unwrap ( )
@@ -352,20 +354,6 @@ impl Recording {
352
354
)
353
355
}
354
356
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
-
369
357
fn set_state ( & self , state : State ) {
370
358
if state == self . state ( ) {
371
359
return ;
@@ -375,6 +363,13 @@ impl Recording {
375
363
self . notify_state ( ) ;
376
364
}
377
365
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
+
378
373
fn pipeline ( & self ) -> & gst:: Pipeline {
379
374
self . imp ( )
380
375
. pipeline
@@ -402,19 +397,15 @@ impl Recording {
402
397
403
398
/// Deletes recording file on background
404
399
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
+ ) ;
418
409
}
419
410
420
411
fn update_duration ( & self ) {
@@ -461,20 +452,11 @@ impl Recording {
461
452
let error = if e. error ( ) . matches ( gst:: ResourceError :: OpenWrite ) {
462
453
error. help (
463
454
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
+ ) ,
478
460
)
479
461
} else {
480
462
error
@@ -502,7 +484,7 @@ impl Recording {
502
484
source_id. remove ( ) ;
503
485
}
504
486
505
- self . set_finished ( Ok ( self . file ( ) . unwrap ( ) . clone ( ) ) ) ;
487
+ self . set_finished ( Ok ( self . file ( ) . clone ( ) ) ) ;
506
488
507
489
glib:: ControlFlow :: Break
508
490
}
0 commit comments