Better history

For #83, replaces existing system but doesn't use new features such as
date, image count, etc.
This commit is contained in:
4pr0n 2014-08-01 10:20:42 -07:00
parent 433aa1e46e
commit 72fe3303e3
3 changed files with 68 additions and 71 deletions

View File

@ -6,7 +6,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -15,12 +14,12 @@ import org.json.JSONObject;
public class History { public class History {
private List<Entry> list = new ArrayList<Entry>(); private List<HistoryEntry> list = new ArrayList<HistoryEntry>();
public void add(Entry entry) { public void add(HistoryEntry entry) {
list.add(entry); list.add(entry);
} }
public void remove(Entry entry) { public void remove(HistoryEntry entry) {
list.remove(entry); list.remove(entry);
} }
public void clear() { public void clear() {
@ -31,7 +30,7 @@ public class History {
JSONObject json; JSONObject json;
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
json = jsonArray.getJSONObject(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<String> stringList) { public void fromList(List<String> stringList) {
for (String item : stringList) { for (String item : stringList) {
Entry entry = new Entry(); HistoryEntry entry = new HistoryEntry();
entry.url = item; entry.url = item;
list.add(entry); list.add(entry);
} }
@ -56,13 +55,13 @@ public class History {
public JSONArray toJSON() { public JSONArray toJSON() {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
for (Entry entry : list) { for (HistoryEntry entry : list) {
jsonArray.put(entry.toJSON()); jsonArray.put(entry.toJSON());
} }
return jsonArray; return jsonArray;
} }
public List<Entry> toList() { public List<HistoryEntry> toList() {
return list; 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;
}
}
} }

View File

@ -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;
}
}

View File

@ -539,33 +539,10 @@ public class MainWindow implements Runnable, RipStatusHandler {
historyButtonRerip.addActionListener(new ActionListener() { historyButtonRerip.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
Runnable ripAllThread = new Runnable() { for (int i = 0; i < historyListModel.size(); i++) {
@Override HistoryEntry entry = (HistoryEntry) historyListModel.get(i);
public void run() { queueListModel.addElement(entry.url);
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();
} }
}); });
configUpdateButton.addActionListener(new ActionListener() { configUpdateButton.addActionListener(new ActionListener() {
@ -811,7 +788,7 @@ public class MainWindow implements Runnable, RipStatusHandler {
logger.info("Loading history from configuration"); logger.info("Loading history from configuration");
history.fromList(Utils.getConfigList("download.history")); history.fromList(Utils.getConfigList("download.history"));
} }
for (History.Entry entry : history.toList()) { for (HistoryEntry entry : history.toList()) {
historyListModel.addElement(entry); historyListModel.addElement(entry);
} }
} }
@ -976,8 +953,22 @@ public class MainWindow implements Runnable, RipStatusHandler {
break; break;
case RIP_COMPLETE: case RIP_COMPLETE:
if (!historyListModel.contains(ripTextfield.getText())) { boolean alreadyInHistory = false;
historyListModel.addElement(ripTextfield.getText()); 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()) { if (configPlaySound.isSelected()) {
Utils.playSound("camera.wav"); Utils.playSound("camera.wav");