Search in sources :

Example 6 with Queue

use of com.github.mob41.osumer.io.queue.Queue in project osumer by mob41.

the class UIFrame method checkUpdate.

public void checkUpdate() {
    if (checkingUpdate) {
        return;
    }
    checkingUpdate = true;
    Thread thread = new Thread() {

        public void run() {
            lblUpdateStatus.setForeground(Color.BLACK);
            lblUpdateStatus.setText("Checking for updates...");
            UpdateInfo verInfo = null;
            try {
                verInfo = getUpdateInfoByConfig();
            } catch (NoBuildsForVersionException e) {
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("No builds available for the new version. See dump.");
                checkingUpdate = false;
                return;
            } catch (NoSuchVersionException e) {
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("No current version in the selected branch. See dump.");
                JOptionPane.showMessageDialog(UIFrame.this, "We don't have version " + Osumer.OSUMER_VERSION + " in the current update branch\n\nPlease try another update branch (snapshot, beta, stable).", "Version not available", JOptionPane.INFORMATION_MESSAGE);
                checkingUpdate = false;
                return;
            } catch (NoSuchBuildNumberException e) {
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("This version has a invalid build number. See dump");
                JOptionPane.showMessageDialog(UIFrame.this, "We don't have build number greater or equal to " + Osumer.OSUMER_BUILD_NUM + " in version " + Osumer.OSUMER_VERSION + ".\n" + "If you are using a modified/development osumer,\n" + " you can just ignore this message.\n" + "If not, this might be the versions.json in GitHub goes wrong,\n" + " post a new issue about this.", "Build not available", JOptionPane.WARNING_MESSAGE);
                checkingUpdate = false;
                return;
            } catch (DebuggableException e) {
                e.printStackTrace();
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("Could not connect to update server.");
                JOptionPane.showMessageDialog(UIFrame.this, "Could not connect to update server.", "Error", JOptionPane.ERROR_MESSAGE);
                checkingUpdate = false;
                return;
            }
            if (verInfo == null) {
                lblUpdateStatus.setForeground(Color.RED);
                lblUpdateStatus.setText("Could not obtain update info.");
                JOptionPane.showMessageDialog(UIFrame.this, "Could not obtain update info.", "Error", JOptionPane.ERROR_MESSAGE);
                checkingUpdate = false;
                return;
            }
            if (verInfo.isThisVersion()) {
                lblUpdateStatus.setForeground(Color.BLACK);
                lblUpdateStatus.setText("You are running the latest version of osumer" + " (" + verInfo.getVersion() + "-" + Updater.getBranchStr(verInfo.getBranch()) + "-" + verInfo.getBuildNum() + ")");
                checkingUpdate = false;
                return;
            }
            lblUpdateStatus.setForeground(new Color(0, 153, 0));
            lblUpdateStatus.setText((verInfo.isUpgradedVersion() ? "Upgrade" : "Update") + " available! New version: " + verInfo.getVersion() + "-" + Updater.getBranchStr(verInfo.getBranch()) + "-b" + verInfo.getBuildNum());
            int option;
            String desc = verInfo.getDescription();
            if (desc == null) {
                option = JOptionPane.showOptionDialog(UIFrame.this, "New " + (verInfo.isUpgradedVersion() ? "upgrade" : "update") + " available! New version:\n" + verInfo.getVersion() + "-" + Updater.getBranchStr(verInfo.getBranch()) + "-b" + verInfo.getBuildNum() + "\n\n" + "Do you want to update it now?", "Update available", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, JOptionPane.NO_OPTION);
            } else {
                option = JOptionPane.showOptionDialog(UIFrame.this, "New " + (verInfo.isUpgradedVersion() ? "upgrade" : "update") + " available! New version:\n" + verInfo.getVersion() + "-" + Updater.getBranchStr(verInfo.getBranch()) + "-b" + verInfo.getBuildNum() + "\n\n" + "Do you want to update it now?", "Update available", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Yes", "No", "Description/Changelog" }, JOptionPane.NO_OPTION);
                if (option == 2) {
                    option = JOptionPane.showOptionDialog(UIFrame.this, new TextPanel(desc), "Update description/change-log", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, 0);
                }
            }
            if (option == JOptionPane.YES_OPTION) {
                /*
                    try {
                        Desktop.getDesktop().browse(new URI(verInfo.getWebLink()));
                    } catch (IOException | URISyntaxException e) {
                        DebugDump dump = new DebugDump(
                                verInfo.getWebLink(),
                                "Show option dialog of updating osumer or not",
                                "Set checkingUpdate to false",
                                "(End of function / thread)",
                                "Error when opening the web page",
                                false,
                                e);
                        DumpManager.getInstance().addDump(dump);
                        DebugDump.showDebugDialog(dump);
                    }
                    */
                try {
                    String updaterLink = Updater.getUpdaterLink();
                    if (updaterLink == null) {
                        System.out.println("No latest updater .exe defined! Falling back to legacy updater!");
                        updaterLink = Updater.LEGACY_UPDATER_JAR;
                    }
                    URL url;
                    try {
                        url = new URL(updaterLink);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                        JOptionPane.showMessageDialog(null, "Error:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    final String folder = System.getProperty("java.io.tmpdir");
                    final String fileName = "osumer_updater_" + Calendar.getInstance().getTimeInMillis() + ".exe";
                    mgr.addQueue(new Queue("osumer Updater", new URLDownloader(folder, fileName, url), null, null, new QueueAction[] { new UpdaterRunAction(folder + fileName) }));
                    tab.setSelectedIndex(1);
                    new Thread() {

                        public void run() {
                            JOptionPane.showMessageDialog(UIFrame.this, "The web updater will be downloaded and started very soon.", "Notice", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }.start();
                } catch (DebuggableException e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Error:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
                    checkingUpdate = false;
                }
            }
            checkingUpdate = false;
        }
    };
    thread.start();
}
Also used : NoSuchVersionException(com.github.mob41.osumer.exceptions.NoSuchVersionException) DebuggableException(com.github.mob41.organdebug.exceptions.DebuggableException) MalformedURLException(java.net.MalformedURLException) UpdaterRunAction(com.github.mob41.osumer.io.queue.actions.UpdaterRunAction) Color(java.awt.Color) QueueAction(com.github.mob41.osumer.io.queue.QueueAction) NoBuildsForVersionException(com.github.mob41.osumer.exceptions.NoBuildsForVersionException) URLDownloader(com.github.mob41.osumer.io.legacy.URLDownloader) URL(java.net.URL) SockThread(com.github.mob41.osumer.sock.SockThread) NoSuchBuildNumberException(com.github.mob41.osumer.exceptions.NoSuchBuildNumberException) Queue(com.github.mob41.osumer.io.queue.Queue) EventQueue(java.awt.EventQueue) UpdateInfo(com.github.mob41.osumer.updater.UpdateInfo)

Example 7 with Queue

use of com.github.mob41.osumer.io.queue.Queue in project osumer by mob41.

the class UIFrame method addBtQueue.

public boolean addBtQueue(String url, boolean preview, boolean changeTab, QueueAction[] beforeActions, QueueAction[] afterActions) {
    if (config.getCheckUpdateFreq() == Config.CHECK_UPDATE_FREQ_EVERY_ACT) {
        checkUpdate();
    }
    map = null;
    thumb = null;
    pbd = new ProgressDialog();
    new Thread() {

        public void run() {
            pbd.getProgressBar().setIndeterminate(true);
            pbd.getLabel().setText("Status: Getting configuration...");
            String user = config.getUser();
            String pass = config.getPass();
            if (user == null || user.isEmpty() || pass == null || pass.isEmpty()) {
                pbd.getLabel().setText("Status: Prompting username and password...");
                LoginPanel loginPanel = new LoginPanel();
                int option = JOptionPane.showOptionDialog(UIFrame.this, loginPanel, "Login to osu!", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, JOptionPane.CANCEL_OPTION);
                if (option == JOptionPane.OK_OPTION) {
                    if (loginPanel.getUsername().isEmpty() || loginPanel.getPassword().isEmpty()) {
                        JOptionPane.showMessageDialog(UIFrame.this, "Username or password cannot be empty.", "Error", JOptionPane.ERROR_MESSAGE);
                        pbd.dispose();
                        return;
                    }
                    user = loginPanel.getUsername();
                    pass = loginPanel.getPassword();
                } else {
                    pbd.dispose();
                    return;
                }
            }
            pbd.getLabel().setText("Status: Logging in...");
            try {
                osu.login(user, pass);
            } catch (DebuggableException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(UIFrame.this, "Error logging in:\n" + e.getDump().getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                pbd.dispose();
                return;
            }
            String modUrl = url.replace("osu.ppy.sh", "old.ppy.sh");
            pbd.getLabel().setText("Status: Obtaining beatmap information...");
            try {
                map = osu.getBeatmapInfo(modUrl);
            } catch (DebuggableException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(UIFrame.this, "Error getting beatmap info:\n" + e.getDump().getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                pbd.dispose();
                return;
            }
            pbd.dispose();
            pbd = null;
        }
    }.start();
    pbd.setLocationRelativeTo(UIFrame.this);
    pbd.setModal(true);
    pbd.setVisible(true);
    if (map == null) {
        return false;
    }
    boolean stillDwn = true;
    if (preview) {
        BeatmapPreviewDialog bpd = new BeatmapPreviewDialog(map);
        bpd.setLocationRelativeTo(UIFrame.this);
        bpd.setModal(true);
        bpd.setVisible(true);
        stillDwn = bpd.isSelectedYes();
        thumb = bpd.getDownloadedImage();
    }
    if (thumb == null) {
        pbd = new ProgressDialog();
        new Thread() {

            public void run() {
                pbd.getProgressBar().setIndeterminate(true);
                pbd.getLabel().setText("Status: Downloading thumb image...");
                URL url = null;
                try {
                    url = new URL("http:" + map.getThumbUrl());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    pbd.dispose();
                    return;
                }
                URLConnection conn = null;
                try {
                    conn = url.openConnection();
                } catch (IOException e) {
                    e.printStackTrace();
                    pbd.dispose();
                    return;
                }
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                try {
                    thumb = ImageIO.read(conn.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                    pbd.dispose();
                    return;
                }
                pbd.dispose();
            }
        }.start();
        pbd.setLocationRelativeTo(UIFrame.this);
        pbd.setModal(true);
        pbd.setVisible(true);
    }
    if (stillDwn) {
        URL downloadUrl = null;
        try {
            downloadUrl = new URL("http://osu.ppy.sh" + map.getDwnUrl());
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(UIFrame.this, "Error validating download URL:\n" + e1, "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        String tmpdir = System.getProperty("java.io.tmpdir");
        final String mapName = map.getName();
        OsuDownloader dwn = new OsuDownloader(tmpdir, map.getDwnUrl().substring(3, map.getDwnUrl().length()) + " " + map.getName(), osu, downloadUrl);
        QueueAction importAction;
        if (rdbtnUseDefaultSettings.isSelected()) {
            importAction = new BeatmapImportAction(config);
        } else {
            int action = -1;
            String targetFileOrFolder = null;
            if (rdbtnDownloadAndImport.isSelected()) {
                action = 0;
            } else if (rdbtnDownloadToOsu.isSelected()) {
                action = 1;
            } else if (rdbtnDownloadToFile.isSelected()) {
                action = 2;
                targetFileOrFolder = targetFile;
            } else if (rdbtnDownloadToFolder.isSelected()) {
                action = 3;
                targetFileOrFolder = targetFolder;
            }
            importAction = new CustomImportAction(action, targetFileOrFolder);
        }
        if (beforeActions == null) {
            beforeActions = new QueueAction[] { new BeforeSoundAction(config) };
        }
        if (afterActions == null) {
            afterActions = new QueueAction[] { new AfterSoundAction(config), new QueueAction() {

                @Override
                public void run(Queue queue) {
                    icon.displayMessage("Download completed for \"" + mapName + "\"", "This osumer queue has completed downloading.", TrayIcon.MessageType.INFO);
                }
            }, importAction };
        }
        boolean added = mgr.addQueue(new Queue(map.getName(), dwn, thumb, beforeActions, afterActions));
        if (added) {
            icon.displayMessage("Downloading \"" + mapName + "\"", "osumerExpress is downloading the requested beatmap!", TrayIcon.MessageType.INFO);
        } else {
            icon.displayMessage("Could not add \"" + mapName + "\" to queue", "It has already in queue/downloading or completed.", TrayIcon.MessageType.INFO);
        }
        tableModel.fireTableDataChanged();
        if (changeTab) {
            tab.setSelectedIndex(1);
        }
    } else {
        icon.displayMessage("Could not download \"" + url + "\"", "Error occurred when finding beatmap. Start UI to see details.", TrayIcon.MessageType.INFO);
    }
    return stillDwn;
}
Also used : BeforeSoundAction(com.github.mob41.osumer.io.queue.actions.BeforeSoundAction) DebuggableException(com.github.mob41.organdebug.exceptions.DebuggableException) MalformedURLException(java.net.MalformedURLException) AfterSoundAction(com.github.mob41.osumer.io.queue.actions.AfterSoundAction) QueueAction(com.github.mob41.osumer.io.queue.QueueAction) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) SockThread(com.github.mob41.osumer.sock.SockThread) CustomImportAction(com.github.mob41.osumer.io.queue.actions.CustomImportAction) OsuDownloader(com.github.mob41.osums.io.beatmap.OsuDownloader) BeatmapImportAction(com.github.mob41.osumer.io.queue.actions.BeatmapImportAction) Queue(com.github.mob41.osumer.io.queue.Queue) EventQueue(java.awt.EventQueue)

Example 8 with Queue

use of com.github.mob41.osumer.io.queue.Queue in project osumer by mob41.

the class AfterSoundAction method run.

@Override
public void run(Queue queue) {
    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {
                Media m = new Media(new File(config.getToneAfterDownloadPath()).toURI().toString());
                MediaPlayer mp = new MediaPlayer(m);
                mp.play();
            } catch (Exception e) {
                e.printStackTrace();
                DumpManager.getInstance().addDump(new DebugDump(null, "---", "Play after download sound", "---", "Error occurred when trying to play sound", false, e));
            }
        }
    });
    thread.setDaemon(true);
    thread.start();
}
Also used : Media(javafx.scene.media.Media) File(java.io.File) DebugDump(com.github.mob41.organdebug.DebugDump) MediaPlayer(javafx.scene.media.MediaPlayer)

Example 9 with Queue

use of com.github.mob41.osumer.io.queue.Queue in project osumer by mob41.

the class CustomImportAction method run.

@Override
public void run(Queue queue) {
    Downloader dwn = queue.getDownloader();
    String path = dwn.getDownloadFolder() + dwn.getFileName();
    File file = new File(path + ".osz");
    if (!file.exists()) {
        System.out.println("File not exists: " + path + ".osz");
        return;
    }
    if (action == 1 || action == 2 || action == 3) {
        String loc = null;
        if (action == 1) {
            loc = System.getenv("LOCALAPPDATA") + "\\osu!\\Songs";
        } else {
            loc = targetFileOrFolder;
        }
        if (action == 3) {
            File songsFolder = new File(loc);
            if (!songsFolder.exists()) {
                songsFolder.mkdirs();
            }
        }
        String filePath;
        if (action == 2) {
            if (!loc.endsWith(".osz")) {
                loc = loc + ".osz";
            }
            filePath = loc;
        } else {
            filePath = loc + "\\" + dwn.getFileName() + ".osz";
        }
        File toFile = new File(filePath);
        if (toFile.exists()) {
            toFile.delete();
        }
        FileOutputStream toFileOut;
        try {
            toFileOut = new FileOutputStream(toFile);
            Files.copy(file.toPath(), toFileOut);
            toFileOut.close();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "osumer Song Copy failed:\n\nFrom: " + path + "\nTo: " + loc + "\n\nMake sure you have access to that folder.\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    } else {
        try {
            Desktop.getDesktop().open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Downloader(com.github.mob41.osums.io.Downloader) IOException(java.io.IOException) File(java.io.File)

Example 10 with Queue

use of com.github.mob41.osumer.io.queue.Queue in project osumer by mob41.

the class QueueCell method updateData.

public void updateData(Queue queue) {
    Downloader dwn = queue.getDownloader();
    lblFilename.setText(dwn.getFileName());
    lblTitle.setText(queue.getName());
    lblThumb.setIcon(null);
    lblThumb.setText("Loading thumb...");
    lblStatus.setForeground(Color.BLACK);
    BufferedImage image = queue.getThumb();
    if (image != null) {
        lblThumb.setText("");
        lblThumb.setIcon(new ImageIcon(image));
    } else {
        lblThumb.setText("No thumb");
    }
    int progress = (int) dwn.getProgress();
    pb.setValue(progress);
    switch(dwn.getStatus()) {
        case Downloader.DOWNLOADING:
            pb.setIndeterminate(false);
            long elapsedTime = System.nanoTime() - queue.getStartTime();
            long allTimeForDownloading = dwn.getDownloaded() != 0 ? (elapsedTime * dwn.getSize() / dwn.getDownloaded()) : -1;
            if (allTimeForDownloading == -1) {
                lblStatus.setText("Status: Starting...");
            } else {
                long eta = allTimeForDownloading - elapsedTime;
                lblElapsed.setText("Elapsed: " + nanoSecToString(elapsedTime));
                lblEta.setText("ETA: " + nanoSecToString(eta));
                lblStatus.setText("Status: Downloading...");
            }
            break;
        case Downloader.COMPLETED:
            lblStatus.setForeground(Color.GREEN);
            lblEta.setText("ETA: ---");
            lblStatus.setText("Status: Completed.");
            break;
        case Downloader.ERROR:
            lblEta.setText("ETA: ---");
            lblStatus.setForeground(Color.RED);
            lblStatus.setText("Status: Error occurred while downloading.");
            break;
        case Downloader.PAUSED:
            lblEta.setText("ETA: ---");
            lblStatus.setForeground(Color.BLUE);
            lblStatus.setText("Status: Paused.");
            break;
        case Downloader.CANCELLED:
            lblEta.setText("ETA: ---");
            lblStatus.setForeground(Color.BLACK);
            lblStatus.setText("Status: Cancelled.");
            break;
        case -1:
            lblEta.setText("ETA: ---");
            lblStatus.setForeground(Color.BLACK);
            lblStatus.setText("Status: Waiting for queuing...");
            break;
        default:
            lblEta.setText("ETA: ---");
            lblStatus.setForeground(Color.RED);
            lblStatus.setText("Status: Unknown status.");
    }
/*
         * if (this.queue != null){
         * this.queue.getDownloader().deleteObservers(); }
         * 
         * this.queue = queue;
         * 
         * Downloader downloader = queue.getDownloader();
         * downloader.addObserver(new Observer() {
         * 
         * @Override public void update(Observable o, Object arg) { switch
         * (downloader.getStatus()){ case Downloader.DOWNLOADING:
         * pb.setIndeterminate(false); int progress = (int)
         * downloader.getProgress(); pb.setValue(progress);
         * 
         * long elapsedTime = System.nanoTime() - queue.getStartTime(); long
         * allTimeForDownloading = downloader.getDownloaded() != 0 ?
         * (elapsedTime * downloader.getSize() / downloader.getDownloaded()) :
         * -1;
         * 
         * if (allTimeForDownloading == -1){ lblStatus.setText(
         * "Status: Starting..."); } else { long eta = allTimeForDownloading -
         * elapsedTime; lblElapsed.setText("Elapsed: " +
         * nanoSecToString(elapsedTime)); lblEta.setText("ETA: " +
         * nanoSecToString(eta)); lblStatus.setText("Status: Downloading..."); }
         * break; case Downloader.COMPLETED:
         * lblStatus.setForeground(Color.GREEN);
         * 
         * lblEta.setText("ETA: ---"); lblStatus.setText("Status: Completed.");
         * break; case Downloader.ERROR: lblEta.setText("ETA: ---");
         * lblStatus.setForeground(Color.RED); lblStatus.setText(
         * "Status: Error occurred while downloading."); break; case
         * Downloader.PAUSED: lblEta.setText("ETA: ---");
         * lblStatus.setForeground(Color.BLUE); lblStatus.setText(
         * "Status: Paused."); break; case Downloader.CANCELLED: lblEta.setText(
         * "ETA: ---"); lblStatus.setForeground(Color.BLACK); lblStatus.setText(
         * "Status: Cancelled."); break; default: lblEta.setText("ETA: ---");
         * lblStatus.setForeground(Color.RED); lblStatus.setText(
         * "Status: Unknown status."); } } }); queue.start();
         */
}
Also used : ImageIcon(javax.swing.ImageIcon) Downloader(com.github.mob41.osums.io.Downloader) BufferedImage(java.awt.image.BufferedImage)

Aggregations

IOException (java.io.IOException)5 Queue (com.github.mob41.osumer.io.queue.Queue)4 File (java.io.File)4 DebugDump (com.github.mob41.organdebug.DebugDump)3 DebuggableException (com.github.mob41.organdebug.exceptions.DebuggableException)3 QueueAction (com.github.mob41.osumer.io.queue.QueueAction)3 Downloader (com.github.mob41.osums.io.Downloader)3 EventQueue (java.awt.EventQueue)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 AfterSoundAction (com.github.mob41.osumer.io.queue.actions.AfterSoundAction)2 BeatmapImportAction (com.github.mob41.osumer.io.queue.actions.BeatmapImportAction)2 BeforeSoundAction (com.github.mob41.osumer.io.queue.actions.BeforeSoundAction)2 CustomImportAction (com.github.mob41.osumer.io.queue.actions.CustomImportAction)2 SockThread (com.github.mob41.osumer.sock.SockThread)2 OsuDownloader (com.github.mob41.osums.io.beatmap.OsuDownloader)2 FileOutputStream (java.io.FileOutputStream)2 URLConnection (java.net.URLConnection)2 Media (javafx.scene.media.Media)2 MediaPlayer (javafx.scene.media.MediaPlayer)2