Skip to content

Commit

Permalink
improve no disc found handling
Browse files Browse the repository at this point in the history
  • Loading branch information
joske committed Apr 4, 2024
1 parent caaec09 commit 12c594e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
25 changes: 25 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ pub struct Disc {
pub tracks: Vec<Track>,
}

impl Disc {
pub(crate) fn with_tracks(num: i32) -> Disc {
let mut d = Disc {
title: "Unknown".to_string(),
artist: "Unknown".to_string(),
year: None,
genre: None,
tracks: Vec::with_capacity(num as usize),
};
for i in 1..=num {
d.tracks.push(Track {
number: i as u32,
title: "Unknown".to_string(),
artist: "Unknown".to_string(),
duration: 0,
composer: None,
rip: false,
});
}
d
}
}

#[derive(Default, Debug)]
pub struct Track {
pub number: u32,
Expand Down Expand Up @@ -44,6 +67,7 @@ pub struct Config {
pub encode_path: String,
pub encoder: Encoder,
pub quality: Quality,
pub fake_cdrom: bool,
}

impl Default for Config {
Expand All @@ -54,6 +78,7 @@ impl Default for Config {
encode_path: path,
encoder: Encoder::MP3,
quality: Quality::Medium,
fake_cdrom: false,
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod data;
mod musicbrainz;
mod ripper;
mod ui;
mod util;

pub fn main() {
simplelog::TermLogger::init(
Expand Down
30 changes: 8 additions & 22 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
data::{Config, Data, Encoder, Quality},
ripper::extract,
util::{lookup_disc, scan_disc},
};
use discid::DiscId;
use glib::Type;
use gtk::{
prelude::*, Align, Application, ApplicationWindow, Box, Builder, Button, ButtonsType, Dialog,
Expand Down Expand Up @@ -292,23 +292,10 @@ fn handle_scan(data: Arc<RwLock<Data>>, builder: &Builder, window: &ApplicationW
let scan_button: Button = builder.object("scan_button").expect("Failed to get widget");
scan_button.connect_clicked(move |_| {
debug!("Scan");
let result = DiscId::read(Some(&DiscId::default_device()));
let discid = if let Ok(d) = result {
d
} else {
// show_message("Disc not found!", MessageType::Error, &window);
// return;
// for testing on machine without CDROM drive: hardcode offsets of a dire straits disc
let offsets = [
298_948, 183, 26155, 44233, 64778, 80595, 117_410, 144_120, 159_913, 178_520,
204_803, 258_763, 277_218,
];
DiscId::put(1, &offsets).unwrap() // this is for testing only so this unwrap is ok
};

debug!("Scanned: {discid:?}");
debug!("id={}", discid.id());
if let Ok(disc) = crate::musicbrainz::lookup(&discid.id()) {
if let Ok(discid) = scan_disc() {
debug!("Scanned: {discid:?}");
debug!("id={}", discid.id());
let disc = lookup_disc(discid);
debug!("disc:{}", disc.title);
// store.clear();
title_text.buffer().set_text(&disc.title);
Expand All @@ -319,13 +306,12 @@ fn handle_scan(data: Arc<RwLock<Data>>, builder: &Builder, window: &ApplicationW
if let Some(genre) = &disc.genre {
genre_text.buffer().set_text(&genre.clone());
}
let tracks = disc.tracks.len();
// panic if we can't get a write lock
data.write()
.expect("Failed to aquire write lock on data")
.disc = Some(disc);
// here we know how many tracks there are
let tracks = usize::try_from(discid.last_track_num() - discid.first_track_num() + 1)
.expect("Failed to convert track number");
for i in 0..tracks {
let iter = store.append();
if let Ok(r) = data.read() {
Expand All @@ -341,10 +327,10 @@ fn handle_scan(data: Arc<RwLock<Data>>, builder: &Builder, window: &ApplicationW
}
}
}
go_button.set_sensitive(true);
} else {
show_message("Disc not found!", MessageType::Error, &window);
show_message("Failed to scan disc", MessageType::Error, &window);
}
go_button.set_sensitive(true);
});
}

Expand Down
65 changes: 65 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use discid::{DiscError, DiscId};
use log::debug;

use crate::data::{Config, Disc};

pub fn scan_disc() -> Result<DiscId, DiscError> {
let config: Config = confy::load("ripperx4", None).expect("failed to load config");
debug!("fake={}", config.fake_cdrom);
match DiscId::read(Some(&DiscId::default_device())) {
Ok(discid) => Ok(discid),
Err(e) => {
if config.fake_cdrom {
debug!("fake_cdrom is set, using hardcoded offsets");
// for testing on machine without CDROM drive: hardcode offsets of a dire straits disc
Ok(fake_discid())
} else {
Err(e)
}
}
}
}

pub fn lookup_disc(discid: DiscId) -> Disc {
debug!("id={}", discid.id());
if let Ok(disc) = crate::musicbrainz::lookup(&discid.id()) {
disc
} else {
Disc::with_tracks(discid.last_track_num() - discid.first_track_num() + 1)
}
}

fn fake_discid() -> DiscId {
let offsets = [
298_948, 183, 26155, 44233, 64778, 80595, 117_410, 144_120, 159_913, 178_520, 204_803,
258_763, 277_218,
];
DiscId::put(1, &offsets).unwrap() // this is for testing only so this unwrap is ok
}

#[cfg(test)]
mod test {
use super::*;

fn bad_discid() -> DiscId {
let offsets = [450, 150, 300];
DiscId::put(1, &offsets).unwrap() // this is for testing only so this unwrap is ok
}

#[test]
fn test_lookup_disc_dire_straits() {
let disc = lookup_disc(fake_discid());
assert_eq!(disc.tracks.len(), 12);
assert_eq!(disc.title, "Money for Nothing");
}

#[test]
fn test_lookup_disc_bad_discid() {
let disc = lookup_disc(bad_discid());
assert_eq!(disc.tracks.len(), 2);
assert_eq!(disc.title, "Unknown");
assert_eq!(disc.artist, "Unknown");
assert_eq!(disc.tracks[0].title, "Unknown");
assert_eq!(disc.tracks[0].artist, "Unknown");
}
}

0 comments on commit 12c594e

Please sign in to comment.