From 72fe3303e3f86f183a8b57c60e4a299b7eae2ba3 Mon Sep 17 00:00:00 2001 From: 4pr0n Date: Fri, 1 Aug 2014 10:20:42 -0700 Subject: [PATCH] Better history For #83, replaces existing system but doesn't use new features such as date, image count, etc. --- .../java/com/rarchives/ripme/ui/History.java | 48 +++-------------- .../com/rarchives/ripme/ui/HistoryEntry.java | 40 +++++++++++++++ .../com/rarchives/ripme/ui/MainWindow.java | 51 ++++++++----------- 3 files changed, 68 insertions(+), 71 deletions(-) create mode 100644 src/main/java/com/rarchives/ripme/ui/HistoryEntry.java diff --git a/src/main/java/com/rarchives/ripme/ui/History.java b/src/main/java/com/rarchives/ripme/ui/History.java index 6d4fd1cc..42532a82 100644 --- a/src/main/java/com/rarchives/ripme/ui/History.java +++ b/src/main/java/com/rarchives/ripme/ui/History.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; -import java.util.Date; import java.util.List; import org.apache.commons.io.IOUtils; @@ -15,12 +14,12 @@ import org.json.JSONObject; public class History { - private List list = new ArrayList(); + private List list = new ArrayList(); - public void add(Entry entry) { + public void add(HistoryEntry entry) { list.add(entry); } - public void remove(Entry entry) { + public void remove(HistoryEntry entry) { list.remove(entry); } public void clear() { @@ -31,7 +30,7 @@ public class History { JSONObject json; for (int i = 0; i < jsonArray.length(); i++) { json = jsonArray.getJSONObject(i); - list.add(new Entry().fromJSON(json)); + list.add(new HistoryEntry().fromJSON(json)); } } @@ -48,7 +47,7 @@ public class History { public void fromList(List stringList) { for (String item : stringList) { - Entry entry = new Entry(); + HistoryEntry entry = new HistoryEntry(); entry.url = item; list.add(entry); } @@ -56,13 +55,13 @@ public class History { public JSONArray toJSON() { JSONArray jsonArray = new JSONArray(); - for (Entry entry : list) { + for (HistoryEntry entry : list) { jsonArray.put(entry.toJSON()); } return jsonArray; } - public List toList() { + public List toList() { return list; } @@ -75,37 +74,4 @@ public class History { } } - class Entry { - public String url = "", - title = ""; - public int count = 0; - public Date startDate = new Date(), - modifiedDate = new Date(); - - public Entry() { - } - - public Entry 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")); - 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()); - return json; - } - - public String toString() { - return this.url; - } - } } diff --git a/src/main/java/com/rarchives/ripme/ui/HistoryEntry.java b/src/main/java/com/rarchives/ripme/ui/HistoryEntry.java new file mode 100644 index 00000000..c9b8afb0 --- /dev/null +++ b/src/main/java/com/rarchives/ripme/ui/HistoryEntry.java @@ -0,0 +1,40 @@ +package com.rarchives.ripme.ui; + +import java.util.Date; + +import org.json.JSONObject; + +public class HistoryEntry { + + public String url = "", + title = ""; + public int count = 0; + public Date startDate = new Date(), + modifiedDate = new Date(); + + 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")); + 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()); + return json; + } + + public String toString() { + return this.url; + } +} diff --git a/src/main/java/com/rarchives/ripme/ui/MainWindow.java b/src/main/java/com/rarchives/ripme/ui/MainWindow.java index df221e41..d46c8f48 100644 --- a/src/main/java/com/rarchives/ripme/ui/MainWindow.java +++ b/src/main/java/com/rarchives/ripme/ui/MainWindow.java @@ -539,33 +539,10 @@ public class MainWindow implements Runnable, RipStatusHandler { historyButtonRerip.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { - Runnable ripAllThread = new Runnable() { - @Override - public void run() { - historyButtonPanel.setEnabled(false); - historyList.setEnabled(false); - for (int i = 0; i < historyListModel.size(); i++) { - historyList.clearSelection(); - historyList.setSelectedIndex(i); - Thread t = ripAlbum( (String) historyListModel.get(i) ); - if (t == null) { - continue; - } - try { - synchronized (t) { - t.wait(); - } - t.join(); - } catch (InterruptedException e) { - logger.error("[!] Exception while waiting for ripper to finish:", e); - } - } - historyList.setEnabled(true); - historyButtonPanel.setEnabled(true); - } - - }; - new Thread(ripAllThread).start(); + for (int i = 0; i < historyListModel.size(); i++) { + HistoryEntry entry = (HistoryEntry) historyListModel.get(i); + queueListModel.addElement(entry.url); + } } }); configUpdateButton.addActionListener(new ActionListener() { @@ -811,7 +788,7 @@ public class MainWindow implements Runnable, RipStatusHandler { logger.info("Loading history from configuration"); history.fromList(Utils.getConfigList("download.history")); } - for (History.Entry entry : history.toList()) { + for (HistoryEntry entry : history.toList()) { historyListModel.addElement(entry); } } @@ -976,8 +953,22 @@ public class MainWindow implements Runnable, RipStatusHandler { break; case RIP_COMPLETE: - if (!historyListModel.contains(ripTextfield.getText())) { - historyListModel.addElement(ripTextfield.getText()); + boolean alreadyInHistory = false; + 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; + break; + } + } + if (!alreadyInHistory) { + HistoryEntry entry = new HistoryEntry(); + entry.url = url; + try { + entry.title = ripper.getAlbumTitle(ripper.getURL()); + } catch (MalformedURLException e) { } + historyListModel.addElement(entry); } if (configPlaySound.isSelected()) { Utils.playSound("camera.wav");