+# Generate the "MusicBrainz:" entry in the top menu.
+sub top_track_musicbrainz {
+ my ($entry) = @_;
+ my ($track_mbid, $recording_mbid, $release_mbid);
+ my @artist_mbids;
+ my $label = "MB:";
+ my %idmap;
+
+ ($track_mbid) = get_track_metadata($entry, "MUSICBRAINZ_RELEASETRACKID");
+ ($recording_mbid) = get_track_metadata($entry, "MUSICBRAINZ_TRACKID");
+ ($release_mbid) = get_track_metadata($entry, "MUSICBRAINZ_ALBUMID");
+ @artist_mbids = get_track_metadata($entry, "MUSICBRAINZ_ARTISTID");
+ return unless $track_mbid // $recording_mbid
+ // $release_mbid // @artist_mbids;
+
+ foreach (get_track_metadata($entry, "Comment")) {
+ $idmap{$1} = $2 if /^([^=]*)=(.*) \(idmap\)$/;
+ }
+
+ fvwm_cmd("AddToMenu", $menu, "", "Nop");
+ if ($track_mbid) {
+ fvwm_cmd("AddToMenu", $menu, "$label\tShow track",
+ "Exec", "exec", "xdg-open",
+ "https://musicbrainz.org/track/$track_mbid");
+ $label = "";
+ } elsif ($recording_mbid) {
+ fvwm_cmd("AddToMenu", $menu, "$label\tShow recording",
+ "Exec", "exec", "xdg-open",
+ "https://musicbrainz.org/recording/$recording_mbid");
+ $label = "";
+ } elsif ($release_mbid) {
+ fvwm_cmd("AddToMenu", $menu, "$label\tShow",
+ "Exec", "exec", "xdg-open",
+ "https://musicbrainz.org/release/$release_mbid");
+ $label = "";
+ }
+
+ foreach my $mbid (@artist_mbids) {
+ my $name = " $idmap{$mbid}" if $idmap{$mbid};
+
+ fvwm_cmd("AddToMenu", $menu, "$label\tShow artist$name",
+ "Exec", "exec", "xdg-open",
+ "https://musicbrainz.org/artist/$mbid");
+ $label = "";
+ }
+}
+
+# Given a work MBID, return a hash reference containing all tracks
+# linked to that work. The hash keys are filenames.
+sub get_tracks_by_work_mbid {
+ my %matches;
+ my $entry;
+
+ foreach my $mbid (@_) {
+ MPD::exec("search", "(MUSICBRAINZ_WORKID == \"$mbid\")");
+ while (<$sock>) {
+ last if (/^OK/);
+ die($_) if (/^ACK/);
+
+ if (/^(\w+): (.*)$/) {
+ if ($1 eq "file") {
+ if (exists($matches{$2})) {
+ $entry = $matches{$2};
+ } else {
+ $entry = {};
+ $matches{$2} = $entry;
+ }
+ }
+
+ add_track_metadata($entry, $1, $2);
+ }
+ }
+ }
+
+ return \%matches;
+}
+
+# Given a track MBID, return a hash reference containing all "related"
+# tracks in the MPD database. The hash keys are filenames.
+#
+# Currently tracks are considered "related" if their associated recordings
+# have at least one work in common.
+sub get_tracks_by_track_mbid {
+ my ($mbid, $tagname) = (@_, "MUSICBRAINZ_RELEASETRACKID");
+ my %source;
+ my %matches;
+ my $entry;
+
+ return \%matches unless ($mbid);
+ MPD::exec("search", "($tagname == \"$mbid\")");
+ while (<$sock>) {
+ last if (/^OK/);
+ die($_) if (/^ACK/);
+
+ if (/^(\w+): (.*)$/) {
+ add_track_metadata(\%source, $1, $2);
+ }
+ }
+
+ # Always include the current track
+ $matches{$source{file}} = \%source;
+
+ # Find all tracks related by work
+ foreach my $mbid (get_track_metadata(\%source, "MUSICBRAINZ_WORKID")) {
+ my $related = get_tracks_by_work_mbid($mbid);
+ foreach (keys %$related) {
+ $matches{$_} //= $related->{$_};
+ }
+ }
+
+ return \%matches;
+}
+
+sub get_tracks_by_recording_mbid {
+ return get_tracks_by_track_mbid($_[0], "MUSICBRAINZ_TRACKID");
+}
+