From 9e63783abc6dddd4537f662e179112dac580d850 Mon Sep 17 00:00:00 2001 From: 4pr0n Date: Sun, 11 Jan 2015 00:23:43 -0800 Subject: [PATCH] Storing number of files in history --- .../ripme/ripper/AbstractRipper.java | 14 +++++++-- .../rarchives/ripme/ripper/AlbumRipper.java | 5 ++++ .../java/com/rarchives/ripme/ui/History.java | 2 +- .../com/rarchives/ripme/ui/HistoryEntry.java | 23 +++++++++++---- .../com/rarchives/ripme/ui/MainWindow.java | 7 ++++- .../rarchives/ripme/ui/RipStatusComplete.java | 29 +++++++++++++++++++ 6 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/rarchives/ripme/ui/RipStatusComplete.java diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index 33b5fd45..d84adb4c 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -15,6 +15,7 @@ import org.apache.log4j.FileAppender; import org.apache.log4j.Logger; import org.jsoup.HttpStatusException; +import com.rarchives.ripme.ui.RipStatusComplete; import com.rarchives.ripme.ui.RipStatusHandler; import com.rarchives.ripme.ui.RipStatusMessage; import com.rarchives.ripme.ui.RipStatusMessage.STATUS; @@ -199,6 +200,13 @@ public abstract class AbstractRipper */ public abstract void downloadProblem(URL url, String message); + /** + * @return Number of files downloaded. + */ + public int getCount() { + return 1; + } + /** * Notifies observers and updates state if all files have been ripped. */ @@ -206,13 +214,15 @@ public abstract class AbstractRipper if (observer == null) { return; } - + if (!completed) { completed = true; logger.info(" Rip completed!"); - RipStatusMessage msg = new RipStatusMessage(STATUS.RIP_COMPLETE, workingDir); + RipStatusComplete rsc = new RipStatusComplete(workingDir, getCount()); + RipStatusMessage msg = new RipStatusMessage(STATUS.RIP_COMPLETE, rsc); observer.update(this, msg); + Logger rootLogger = Logger.getRootLogger(); FileAppender fa = (FileAppender) rootLogger.getAppender("FILE"); if (fa != null) { diff --git a/src/main/java/com/rarchives/ripme/ripper/AlbumRipper.java b/src/main/java/com/rarchives/ripme/ripper/AlbumRipper.java index b4135b22..8d40086d 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AlbumRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AlbumRipper.java @@ -33,6 +33,11 @@ public abstract class AlbumRipper extends AbstractRipper { return false; } + @Override + public int getCount() { + return itemsCompleted.size(); + } + public boolean addURLToDownload(URL url, File saveAs, String referrer, Map cookies) { if (!allowDuplicates() && ( itemsPending.containsKey(url) diff --git a/src/main/java/com/rarchives/ripme/ui/History.java b/src/main/java/com/rarchives/ripme/ui/History.java index 42532a82..e0159be2 100644 --- a/src/main/java/com/rarchives/ripme/ui/History.java +++ b/src/main/java/com/rarchives/ripme/ui/History.java @@ -68,7 +68,7 @@ public class History { public void toFile(String filename) throws IOException { OutputStream os = new FileOutputStream(filename); try { - IOUtils.write(toJSON().toString(), os); + IOUtils.write(toJSON().toString(2), os); } finally { os.close(); } diff --git a/src/main/java/com/rarchives/ripme/ui/HistoryEntry.java b/src/main/java/com/rarchives/ripme/ui/HistoryEntry.java index c9b8afb0..f2b653eb 100644 --- a/src/main/java/com/rarchives/ripme/ui/HistoryEntry.java +++ b/src/main/java/com/rarchives/ripme/ui/HistoryEntry.java @@ -7,30 +7,43 @@ import org.json.JSONObject; public class HistoryEntry { public String url = "", - title = ""; + title = "", + dir = ""; public int count = 0; public Date startDate = new Date(), modifiedDate = new Date(); + boolean selected = false; public HistoryEntry() { } public HistoryEntry fromJSON(JSONObject json) { this.url = json.getString("url"); - this.title = json.getString("title"); - this.count = json.getInt("count"); this.startDate = new Date(json.getLong("startDate")); this.modifiedDate = new Date(json.getLong("modifiedDate")); + if (json.has("title")) { + this.title = json.getString("title"); + } + if (json.has("count")) { + this.count = json.getInt("count"); + } + if (json.has("dir")) { + this.dir = json.getString("dir"); + } + if (json.has("selected")) { + this.selected = json.getBoolean("selected"); + } return this; } public JSONObject toJSON() { JSONObject json = new JSONObject(); json.put("url", this.url); - json.put("title", this.title); - json.put("count", this.count); json.put("startDate", this.startDate.getTime()); json.put("modifiedDate", this.modifiedDate.getTime()); + json.put("title", this.title); + json.put("count", this.count); + json.put("selected", this.selected); return json; } diff --git a/src/main/java/com/rarchives/ripme/ui/MainWindow.java b/src/main/java/com/rarchives/ripme/ui/MainWindow.java index c84002ba..fb9a0863 100644 --- a/src/main/java/com/rarchives/ripme/ui/MainWindow.java +++ b/src/main/java/com/rarchives/ripme/ui/MainWindow.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; +import java.util.Date; import java.util.List; import javax.imageio.ImageIO; @@ -977,17 +978,21 @@ public class MainWindow implements Runnable, RipStatusHandler { case RIP_COMPLETE: boolean alreadyInHistory = false; + RipStatusComplete rsc = (RipStatusComplete) msg.getObject(); String url = ripper.getURL().toExternalForm(); for (int i = 0; i < historyListModel.size(); i++) { HistoryEntry entry = (HistoryEntry) historyListModel.get(i); if (entry.url.equals(url)) { alreadyInHistory = true; + entry.modifiedDate = new Date(); break; } } if (!alreadyInHistory) { HistoryEntry entry = new HistoryEntry(); entry.url = url; + entry.dir = rsc.getDir(); + entry.count = rsc.count; try { entry.title = ripper.getAlbumTitle(ripper.getURL()); } catch (MalformedURLException e) { } @@ -1001,7 +1006,7 @@ public class MainWindow implements Runnable, RipStatusHandler { statusProgress.setValue(0); statusProgress.setVisible(false); openButton.setVisible(true); - File f = (File) msg.getObject(); + File f = rsc.dir; String prettyFile = Utils.shortenPath(f); openButton.setText("Open " + prettyFile); mainFrame.setTitle("RipMe v" + UpdateUtils.getThisJarVersion()); diff --git a/src/main/java/com/rarchives/ripme/ui/RipStatusComplete.java b/src/main/java/com/rarchives/ripme/ui/RipStatusComplete.java new file mode 100644 index 00000000..b6f89471 --- /dev/null +++ b/src/main/java/com/rarchives/ripme/ui/RipStatusComplete.java @@ -0,0 +1,29 @@ +package com.rarchives.ripme.ui; + +import java.io.File; +import java.io.IOException; + +public class RipStatusComplete { + File dir = null; + int count = 0; + + public RipStatusComplete(File dir) { + this.dir = dir; + this.count = 1; + } + + public RipStatusComplete(File dir, int count) { + this.dir = dir; + this.count = count; + } + + public String getDir() { + String result; + try { + result = this.dir.getCanonicalPath(); + } catch (IOException e) { + result = this.dir.toString(); + } + return result; + } +}