static AlbumDetails *
make_album_from_release (Mb4ReleaseGroup group,
                         Mb4Release release,
                         Mb4Medium medium)
{
  AlbumDetails *album;
  Mb4ArtistCredit credit;
  GList *artists;
  char *date = NULL;
  char buffer[512]; /* for the GET macro */

  g_assert (release);
  g_return_val_if_fail (medium != NULL, NULL);

  album = g_new0 (AlbumDetails, 1);

  GET (album->album_id, mb4_release_get_id, release);
  GET (album->title, mb4_medium_get_title, medium);
  if (album->title == NULL)
    GET (album->title, mb4_release_get_title, release);

  credit = mb4_release_get_artistcredit (release);

  artists = get_artist_list (credit);
  if (artists) {
    get_artist_info (artists, &album->artist,
                     &album->artist_sortname,
                     &album->artist_id);
  }
  album->artists = artists;

  GET (date, mb4_release_get_date, release);
  album->release_date = sj_metadata_helper_scan_date (date);
  g_free (date);

  GET (album->asin, mb4_release_get_asin, release);
  GET (album->country, mb4_release_get_country, release);
  if (group) {
    GET (album->type, mb4_releasegroup_get_type, group);
    if (g_str_has_suffix (album->type, "Spokenword")
        || g_str_has_suffix (album->type, "Interview")
        || g_str_has_suffix (album->type, "Audiobook")) {
      album->is_spoken_word = TRUE;
    }
    fill_relations (mb4_releasegroup_get_relationlist(group), album);
  }

  album->disc_number = mb4_medium_get_position (medium);
  fill_tracks_from_medium (medium, album);
  fill_relations (mb4_release_get_relationlist (release), album);

  sj_mb4_album_details_dump (album);
  return album;
}
static AlbumDetails *
make_album_from_release (MbRelease *release)
{
  AlbumDetails *album;
  char buffer[512];
  MbArtist artist;
  char *new_title;
  int i;

  g_assert (release);

  album = g_new0 (AlbumDetails, 1);

  GET_ID (album->album_id, mb_release_get_id, release);
  GET (album->title, mb_release_get_title, release);
  new_title = sj_metadata_helper_scan_disc_number (album->title, &album->disc_number);
  if (new_title) {
    g_free (album->title);
    album->title = new_title;
  }

  artist = mb_release_get_artist (release);
  GET_ID (album->artist_id, mb_artist_get_id, artist);
  GET (album->artist, mb_artist_get_name, artist);
  GET (album->artist_sortname, mb_artist_get_sortname, artist);

  if (mb_release_get_num_release_events (release) >= 1) {
    MbReleaseEvent event;
    char *date = NULL;

    event = mb_release_get_release_event (release, 0);
    GET (date, mb_release_event_get_date, event);
    album->release_date = sj_metadata_helper_scan_date (date);
    g_free (date);
  }

  album->number = mb_release_get_num_tracks (release);
  GET (album->asin, mb_release_get_asin, release);

  for (i = 0; i < mb_release_get_num_relations (release); i++) {
    MbRelation relation;
    char *type = NULL;

    relation = mb_release_get_relation (release, i);
    GET(type, mb_relation_get_type, relation);
    if (type && g_str_equal (type, "http://musicbrainz.org/ns/rel-1.0#Wikipedia")) {
      GET (album->wikipedia, mb_relation_get_target_id, relation);
    } else if (type && g_str_equal (type, "http://musicbrainz.org/ns/rel-1.0#Discogs")) {
      GET (album->discogs, mb_relation_get_target_id, relation);
      continue;
    }
    g_free (type);
  }

  for (i = 0; i < mb_release_get_num_types (release); i++) {
    mb_release_get_type (release, i, buffer, sizeof(buffer));

    if (g_str_has_suffix (buffer, "#Spokenword")
    	|| g_str_has_suffix (buffer, "#Interview")
    	|| g_str_has_suffix (buffer, "#Audiobook")) {
      album->is_spoken_word = TRUE;
      break;
    }
  }

  for (i = 0; i < album->number; i++) {
    MbTrack mbt;
    TrackDetails *track;

    mbt = mb_release_get_track (release, i);
    track = g_new0 (TrackDetails, 1);

    track->album = album;

    track->number = i + 1;
    GET_ID (track->track_id, mb_track_get_id, mbt);

    GET (track->title, mb_track_get_title, mbt);
    track->duration = mb_track_get_duration (mbt) / 1000;

    artist = mb_track_get_artist (mbt);
    if (artist == NULL)
      artist = mb_release_get_artist (release);
    GET_ID (track->artist_id, mb_artist_get_id, artist);
    GET (track->artist, mb_artist_get_name, artist);
    GET (track->artist_sortname, mb_artist_get_sortname, artist);

    album->tracks = g_list_append (album->tracks, track);
  }

  return album;
}