Search in sources :

Example 1 with MethodResult

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);
}
Also used : BeforeSoundAction(com.github.mob41.osumer.queue.actions.BeforeSoundAction) MalformedURLException(java.net.MalformedURLException) AfterSoundAction(com.github.mob41.osumer.queue.actions.AfterSoundAction) WithDumpException(com.github.mob41.osumer.debug.WithDumpException) OsuSong(com.github.mob41.osums.beatmap.OsuSong) QueueAction(com.github.mob41.osumer.queue.QueueAction) URL(java.net.URL) Observable(java.util.Observable) CustomImportAction(com.github.mob41.osumer.queue.actions.CustomImportAction) Observer(java.util.Observer) OsuDownloader(com.github.mob41.osumer.io.OsuDownloader) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) BeatmapImportAction(com.github.mob41.osumer.queue.actions.BeatmapImportAction) Queue(com.github.mob41.osumer.queue.Queue) MethodResult(com.github.mob41.osumer.method.MethodResult)

Example 2 with MethodResult

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;
    }
}
Also used : AlertType(javafx.scene.control.Alert.AlertType) Alert(javafx.scene.control.Alert) RemoteException(java.rmi.RemoteException) DebugDump(com.github.mob41.osumer.debug.DebugDump)

Aggregations

DebugDump (com.github.mob41.osumer.debug.DebugDump)1 WithDumpException (com.github.mob41.osumer.debug.WithDumpException)1 OsuDownloader (com.github.mob41.osumer.io.OsuDownloader)1 MethodResult (com.github.mob41.osumer.method.MethodResult)1 Queue (com.github.mob41.osumer.queue.Queue)1 QueueAction (com.github.mob41.osumer.queue.QueueAction)1 AfterSoundAction (com.github.mob41.osumer.queue.actions.AfterSoundAction)1 BeatmapImportAction (com.github.mob41.osumer.queue.actions.BeatmapImportAction)1 BeforeSoundAction (com.github.mob41.osumer.queue.actions.BeforeSoundAction)1 CustomImportAction (com.github.mob41.osumer.queue.actions.CustomImportAction)1 OsuSong (com.github.mob41.osums.beatmap.OsuSong)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 RemoteException (java.rmi.RemoteException)1 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)1 Observable (java.util.Observable)1 Observer (java.util.Observer)1 Alert (javafx.scene.control.Alert)1 AlertType (javafx.scene.control.Alert.AlertType)1