You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
4.3 KiB
199 lines
4.3 KiB
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
package moe.nekojimi.chords;
|
|
|
|
import com.amihaiemil.eoyaml.Yaml;
|
|
import com.amihaiemil.eoyaml.YamlMapping;
|
|
import com.amihaiemil.eoyaml.YamlSequence;
|
|
import com.amihaiemil.eoyaml.YamlSequenceBuilder;
|
|
import java.io.File;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
*
|
|
* @author jimj316
|
|
*/
|
|
public class Song
|
|
{
|
|
private String title;
|
|
private String artist;
|
|
private final URL url;
|
|
private File location = null;
|
|
private int number;
|
|
private List<Format> formats = new ArrayList<>();
|
|
|
|
private String requestedBy;
|
|
private String requestedIn;
|
|
private boolean kept = false;
|
|
|
|
public Song(URL url)
|
|
{
|
|
this.url = url;
|
|
}
|
|
|
|
public YamlMapping toYaml()
|
|
{
|
|
YamlSequenceBuilder build = Yaml.createYamlSequenceBuilder();
|
|
|
|
for (Format format : formats)
|
|
build = build.add(format.toYaml());
|
|
|
|
return Yaml.createYamlMappingBuilder()
|
|
.add("title", title)
|
|
.add("url", url.toExternalForm())
|
|
.add("location", location.getAbsolutePath())
|
|
.add("num", Integer.toString(number))
|
|
.add("formats", build.build())
|
|
.add("requestedBy", requestedBy)
|
|
.add("requestedIn", requestedIn)
|
|
.add("kept", Boolean.toString(kept))
|
|
.build();
|
|
}
|
|
|
|
public static Song fromYaml(YamlMapping map) throws MalformedURLException
|
|
{
|
|
Song song = new Song(new URL(map.string("url")));
|
|
song.setArtist(map.string("artist"));
|
|
song.setLocation(new File(map.string("location")));
|
|
song.setNumber(map.integer("num"));
|
|
song.setKept(Boolean.parseBoolean(map.string("kept")));
|
|
song.setRequestedBy(map.string("requestedBy"));
|
|
song.setRequestedIn(map.string("requestedIn"));
|
|
|
|
List<Format> formats = new ArrayList<>();
|
|
YamlSequence formatSeq = map.yamlSequence("formats");
|
|
for (int i = 0; i < formats.size(); i++)
|
|
formats.add(Format.fromYaml(formatSeq.yamlMapping(i)));
|
|
song.setFormats(formats);
|
|
|
|
return song;
|
|
}
|
|
|
|
public boolean isDownloaded()
|
|
{
|
|
return (location != null && location.exists() && location.canRead());
|
|
}
|
|
|
|
public String getTitle()
|
|
{
|
|
return title;
|
|
}
|
|
|
|
public void setTitle(String title)
|
|
{
|
|
this.title = title;
|
|
}
|
|
|
|
public int getNumber()
|
|
{
|
|
return number;
|
|
}
|
|
|
|
public void setNumber(int number)
|
|
{
|
|
this.number = number;
|
|
}
|
|
|
|
public String getArtist()
|
|
{
|
|
return artist;
|
|
}
|
|
|
|
public void setArtist(String artist)
|
|
{
|
|
this.artist = artist;
|
|
}
|
|
|
|
public URL getUrl()
|
|
{
|
|
return url;
|
|
}
|
|
|
|
public File getLocation()
|
|
{
|
|
return location;
|
|
}
|
|
|
|
public void setLocation(File location)
|
|
{
|
|
this.location = location;
|
|
}
|
|
|
|
void delete()
|
|
{
|
|
if (location != null)
|
|
location.delete();
|
|
location = null;
|
|
}
|
|
|
|
public String getRequestedBy()
|
|
{
|
|
return requestedBy;
|
|
}
|
|
|
|
public void setRequestedBy(String requestedBy)
|
|
{
|
|
this.requestedBy = requestedBy;
|
|
}
|
|
|
|
public String getRequestedIn()
|
|
{
|
|
return requestedIn;
|
|
}
|
|
|
|
public void setRequestedIn(String requestedIn)
|
|
{
|
|
this.requestedIn = requestedIn;
|
|
}
|
|
|
|
public boolean isKept()
|
|
{
|
|
return kept;
|
|
}
|
|
|
|
public void setKept(boolean kept)
|
|
{
|
|
this.kept = kept;
|
|
}
|
|
|
|
@Override
|
|
public String toString()
|
|
{
|
|
String ret = "";
|
|
if (artist != null && !artist.isEmpty())
|
|
ret += artist + " - ";
|
|
|
|
if (title != null && !title.isEmpty())
|
|
ret += title;
|
|
|
|
if (ret.isEmpty())
|
|
ret = "track " + number;
|
|
|
|
return /*"[" + number + "] " + */ ret;
|
|
// return url.toExternalForm();
|
|
}
|
|
|
|
public List<Format> getFormats()
|
|
{
|
|
return formats;
|
|
}
|
|
|
|
public void setFormats(List<Format> formats)
|
|
{
|
|
this.formats = formats;
|
|
}
|
|
|
|
public Format getBestFormat()
|
|
{
|
|
if (formats.isEmpty())
|
|
return null;
|
|
return formats.get(0);
|
|
}
|
|
|
|
}
|
|
|