Skip to content

Commit

Permalink
Added stop button
Browse files Browse the repository at this point in the history
  • Loading branch information
4pr0n committed Apr 18, 2014
1 parent aa1dca2 commit f4f8ce7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ public abstract class AbstractRipper
public abstract String getHost();
public abstract String getGID(URL url) throws MalformedURLException;

private boolean shouldStop = false;

public void stop() {
shouldStop = true;
}
public boolean isStopped() {
return shouldStop;
}
protected void stopCheck() throws IOException {
if (shouldStop) {
threadPool.waitForThreads();
throw new IOException("Ripping interrupted");
}
}

/**
* Ensures inheriting ripper can rip this URL, raises exception if not.
* Otherwise initializes working directory and thread pool.
Expand Down Expand Up @@ -117,6 +132,11 @@ public void addURLToDownload(URL url, File saveAs) {
* Sub-directory of the working directory to save the images to.
*/
public void addURLToDownload(URL url, String prefix, String subdirectory) {
try {
stopCheck();
} catch (IOException e) {
return;
}
String saveAs = url.toExternalForm();
saveAs = saveAs.substring(saveAs.lastIndexOf('/')+1);
if (saveAs.indexOf('?') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf('?')); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public DownloadFileThread(URL url, File saveAs, AbstractRipper observer) {
* Notifies observers upon completion/error/warn.
*/
public void run() {
try {
observer.stopCheck();
} catch (IOException e) {
observer.downloadErrored(url, "Download interrupted");
return;
}
if (saveAs.exists()) {
if (Utils.getConfigBoolean("file.overwrite", false)) {
logger.info("[!] Deleting existing file" + prettySaveAs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private void ripAlbum(URL url, String subdirectory) throws IOException {
index = 0;
ImgurAlbum album = getImgurAlbum(url);
for (ImgurImage imgurImage : album.images) {
stopCheck();
String saveAs = workingDir.getCanonicalPath();
if (!saveAs.endsWith(File.separator)) {
saveAs += File.separator;
Expand Down Expand Up @@ -217,6 +218,7 @@ private void ripUserAccount(URL url) throws IOException {
logger.info("[ ] Retrieving " + url.toExternalForm());
Document doc = Jsoup.connect(url.toExternalForm()).get();
for (Element album : doc.select("div.cover a")) {
stopCheck();
if (!album.hasAttr("href")
|| !album.attr("href").contains("imgur.com/a/")) {
continue;
Expand All @@ -236,6 +238,7 @@ private void ripUserAccount(URL url) throws IOException {
private void ripSubreddit(URL url) throws IOException {
int page = 0;
while (true) {
stopCheck();
String pageURL = url.toExternalForm();
if (!pageURL.endsWith("/")) {
pageURL += "/";
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/com/rarchives/ripme/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
Expand Down Expand Up @@ -67,7 +65,8 @@ public class MainWindow implements Runnable, RipStatusHandler {

private static JFrame mainFrame;
private static JTextField ripTextfield;
private static JButton ripButton;
private static JButton ripButton,
stopButton;

private static JLabel statusLabel;
private static JButton openButton;
Expand Down Expand Up @@ -198,14 +197,21 @@ private void createUI(Container pane) {
}

ripTextfield = new JTextField("", 20);
ImageIcon ripIcon = new ImageIcon(mainIcon.getScaledInstance(20, 20, Image.SCALE_SMOOTH));
ImageIcon ripIcon = new ImageIcon(mainIcon);
ripButton = new JButton("<html><font size=\"5\"><b>Rip</b></font></html>", ripIcon);
stopButton = new JButton("<html><font size=\"5\"><b>Stop</b></font></html>");
stopButton.setVisible(false);
try {
Image stopIcon = ImageIO.read(getClass().getClassLoader().getResource("stop.png"));
stopButton.setIcon(new ImageIcon(stopIcon));
} catch (Exception e) { }
JPanel ripPanel = new JPanel(new GridBagLayout());
ripPanel.setBorder(emptyBorder);

gbc.gridx = 0; ripPanel.add(new JLabel("URL:", JLabel.RIGHT), gbc);
gbc.gridx = 1; ripPanel.add(ripTextfield, gbc);
gbc.gridx = 2; ripPanel.add(ripButton, gbc);
ripPanel.add(stopButton, gbc);

statusLabel = new JLabel("Inactive");
statusLabel.setHorizontalAlignment(JLabel.CENTER);
Expand Down Expand Up @@ -329,6 +335,23 @@ private void createUI(Container pane) {
private void setupHandlers() {
ripButton.addActionListener(new RipButtonHandler());
ripTextfield.addActionListener(new RipButtonHandler());
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (ripper != null) {
ripper.stop();
ripButton.setVisible(true);
stopButton.setVisible(false);
ripTextfield.setEnabled(true);
statusProgress.setValue(0);
statusProgress.setVisible(false);
mainFrame.pack();
statusProgress.setValue(0);
status("Ripping interrupted");
appendLog("Ripper interrupted", Color.RED);
}
}
});
optionLog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
Expand Down Expand Up @@ -574,7 +597,8 @@ private Thread ripAlbum(String urlString) {
error("Given URL is not valid, expecting http://website.com/page/...");
return null;
}
ripButton.setEnabled(false);
ripButton.setVisible(false);
stopButton.setVisible(true);
ripTextfield.setEnabled(false);
statusProgress.setValue(100);
openButton.setVisible(false);
Expand Down Expand Up @@ -612,7 +636,8 @@ private Thread ripAlbum(String urlString) {
error("Unable to rip this URL: " + e.getMessage());
}
}
ripButton.setEnabled(true);
ripButton.setVisible(true);
stopButton.setVisible(false);
ripTextfield.setEnabled(true);
statusProgress.setValue(0);
mainFrame.pack();
Expand Down Expand Up @@ -640,6 +665,9 @@ public void run() {
}

private void handleEvent(StatusEvent evt) {
if (ripper.isStopped()) {
return;
}
RipStatusMessage msg = evt.msg;

int completedPercent = evt.ripper.getCompletionPercentage();
Expand Down Expand Up @@ -667,7 +695,8 @@ private void handleEvent(StatusEvent evt) {
historyListModel.addElement(ripTextfield.getText());
}
saveHistory();
ripButton.setEnabled(true);
ripButton.setVisible(true);
stopButton.setVisible(false);
ripTextfield.setEnabled(true);
statusProgress.setValue(0);
statusProgress.setVisible(false);
Expand All @@ -678,9 +707,7 @@ private void handleEvent(StatusEvent evt) {
try {
Image folderIcon = ImageIO.read(getClass().getClassLoader().getResource("folder.png"));
openButton.setIcon(new ImageIcon(folderIcon));
} catch (Exception e) {
logger.error("Error while setting folder icon", e);
}
} catch (Exception e) { }
appendLog( "Rip complete, saved to " + prettyFile, Color.GREEN);
openButton.setActionCommand(f.toString());
openButton.addActionListener(new ActionListener() {
Expand Down
Binary file modified src/main/resources/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/stop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f4f8ce7

Please sign in to comment.