use of com.github.mob41.osumer.method.MethodResult in project osumer by mob41.
the class Daemon method addQueue.
@Override
public MethodResult<Integer> addQueue(String url, int downloadAction, String targetFileOrFolder) throws RemoteException {
DumpManager.reportEvent("event", "queueAdd");
String user = config.getUser();
String pass = config.getPass();
if (user == null || user.isEmpty() || pass == null || pass.isEmpty()) {
return new MethodResult<Integer>(ErrorCode.RESULT_NO_CREDENTIALS);
}
try {
osums.login(user, pass);
} catch (WithDumpException e) {
e.printStackTrace();
return new MethodResult<Integer>(ErrorCode.RESULT_LOGIN_FAILED, e.getDump());
}
OsuSong map;
try {
if (url.contains("b/")) {
map = osums.getBeatmapInfo(url);
} else {
map = osums.getSongInfo(url);
}
} catch (WithDumpException e) {
e.printStackTrace();
return new MethodResult<Integer>(ErrorCode.RESULT_GET_BEATMAP_INFO_FAILED, e.getDump());
}
String thumbUrl = "http:" + map.getThumbUrl();
String dwnUrlStr = map.getDwnUrl();
if (dwnUrlStr.length() <= 3) {
return new MethodResult<Integer>(ErrorCode.RESULT_DOWNLOAD_URL_TOO_SHORT);
}
URL downloadUrl = null;
try {
downloadUrl = new URL("https://osu.ppy.sh" + dwnUrlStr);
} catch (MalformedURLException e) {
e.printStackTrace();
return new MethodResult<Integer>(ErrorCode.RESULT_VALIDATE_DOWNLOAD_URL_FAILED);
}
String tmpdir = System.getProperty("java.io.tmpdir");
final String mapName = map.getName();
String fileName = dwnUrlStr.substring(3, map.getDwnUrl().length()) + " " + mapName;
fileName = fileName.replaceAll("[\\/:*?\"<>|]", " ");
OsuDownloader dwn = new OsuDownloader(tmpdir, fileName, osums, downloadUrl);
QueueAction importAction;
if (downloadAction == -1) {
importAction = new BeatmapImportAction(config);
} else {
importAction = new CustomImportAction(downloadAction, targetFileOrFolder);
}
QueueAction[] beforeActions = new QueueAction[] { new BeforeSoundAction(config) };
QueueAction[] afterActions = new QueueAction[] { new AfterSoundAction(config), new QueueAction() {
@Override
public void run(Queue queue) {
trayIcon.displayMessage("Download completed for \"" + mapName + "\"", "This osumer queue has completed downloading.", TrayIcon.MessageType.INFO);
}
}, importAction };
boolean added = queueManager.addQueue(new Queue(map.getName(), dwn, thumbUrl, beforeActions, afterActions));
if (added) {
trayIcon.displayMessage("Downloading \"" + mapName + "\"", "osumerExpress is downloading the requested beatmap!", TrayIcon.MessageType.INFO);
dwn.addObserver(new Observer() {
@Override
public void update(Observable o, Object arg) {
requestAllUiUpdateQueues();
}
});
DumpManager.reportEvent("event", "queueAdded");
if (!url.endsWith("/")) {
String beatmapNum = url.replaceAll("\\D+", "");
;
DumpManager.reportEvent("beatmap", "queueFrequency", beatmapNum);
}
} else {
trayIcon.displayMessage("Could not add \"" + mapName + "\" to queue", "It has already in queue/downloading or completed.", TrayIcon.MessageType.INFO);
}
requestAllUiUpdateQueues();
return new MethodResult<Integer>(ErrorCode.RESULT_OK);
}
use of com.github.mob41.osumer.method.MethodResult in project osumer by mob41.
the class MainController method requestQueue.
private boolean requestQueue(String url) {
int downloadAction = -1;
String targetFileOrFolder = null;
if (rdBtnUseDefault.isSelected()) {
downloadAction = -1;
} else if (rdBtnDwnImport.isSelected()) {
downloadAction = 0;
} else if (rdBtnDwnOsuSong.isSelected()) {
downloadAction = 1;
} else if (rdBtnDwnFile.isSelected()) {
downloadAction = 2;
targetFileOrFolder = "";
} else if (rdBtnDwnFolder.isSelected()) {
downloadAction = 3;
targetFileOrFolder = "";
}
MethodResult<Integer> result = null;
try {
result = d.addQueue(url, downloadAction, targetFileOrFolder);
} catch (RemoteException e) {
e.printStackTrace();
DumpManager.addDump(new DebugDump(null, "Process download action", "Request daemon to add queue", "(Method End)", "Unable to request daemon to add queue", false, e));
Alert alert = new Alert(AlertType.ERROR, e.getMessage(), ButtonType.OK);
alert.setHeaderText("Unable to request daemon to add queue");
alert.showAndWait();
DumpManager.forceMetricsReport();
return false;
}
if (result == null) {
Alert alert = new Alert(AlertType.ERROR, "No result was returned from daemon.", ButtonType.OK);
alert.setHeaderText("Unable to request daemon to add queue");
alert.showAndWait();
return false;
} else if (result.getResult() != ErrorCode.RESULT_OK) {
String header = null;
String msg = null;
AlertType type = null;
switch(result.getResult()) {
case ErrorCode.RESULT_NO_CREDENTIALS:
header = "No credentials available";
msg = "Please enter your credentials in the Preferences in order to download beatmaps.";
type = AlertType.WARNING;
break;
case ErrorCode.RESULT_LOGIN_FAILED:
header = "Login Failed";
msg = "Please validate your credentials entered in the Preferences.";
type = AlertType.ERROR;
break;
case ErrorCode.RESULT_GET_BEATMAP_INFO_FAILED:
header = "Could not obtain beatmap info";
msg = "Is your beatmap link/ID correct?";
type = AlertType.ERROR;
break;
case ErrorCode.RESULT_VALIDATE_DOWNLOAD_URL_FAILED:
header = "Invalid download link received";
msg = "Is your beatmap link/ID correct?";
type = AlertType.ERROR;
break;
default:
header = "Unable to request daemon to add queue";
msg = "Unknown result code.";
type = AlertType.ERROR;
}
Alert alert = new Alert(type, msg, ButtonType.OK);
alert.setHeaderText(header);
alert.showAndWait();
return false;
} else {
return true;
}
}
Aggregations