2014-07-30 16:48:52 +02:00
|
|
|
package com.rarchives.ripme.ui;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2015-01-11 13:31:30 +01:00
|
|
|
import java.text.SimpleDateFormat;
|
2014-07-30 16:48:52 +02:00
|
|
|
import java.util.ArrayList;
|
2015-01-11 13:31:30 +01:00
|
|
|
import java.util.Date;
|
2014-07-30 16:48:52 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.json.JSONArray;
|
2015-02-08 08:39:27 +01:00
|
|
|
import org.json.JSONException;
|
2014-07-30 16:48:52 +02:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
public class History {
|
|
|
|
|
2014-08-01 19:20:42 +02:00
|
|
|
private List<HistoryEntry> list = new ArrayList<HistoryEntry>();
|
2015-01-11 13:31:30 +01:00
|
|
|
private static final String[] COLUMNS = new String[] {
|
|
|
|
"URL",
|
|
|
|
"created",
|
|
|
|
"modified",
|
|
|
|
"#",
|
|
|
|
""
|
|
|
|
};
|
2014-07-30 16:48:52 +02:00
|
|
|
|
2014-08-01 19:20:42 +02:00
|
|
|
public void add(HistoryEntry entry) {
|
2014-07-30 16:48:52 +02:00
|
|
|
list.add(entry);
|
|
|
|
}
|
2014-08-01 19:20:42 +02:00
|
|
|
public void remove(HistoryEntry entry) {
|
2014-07-30 16:48:52 +02:00
|
|
|
list.remove(entry);
|
|
|
|
}
|
2015-01-11 13:31:30 +01:00
|
|
|
public void remove(int index) {
|
|
|
|
list.remove(index);
|
|
|
|
}
|
2014-07-30 16:48:52 +02:00
|
|
|
public void clear() {
|
|
|
|
list.clear();
|
|
|
|
}
|
2015-01-11 13:31:30 +01:00
|
|
|
public HistoryEntry get(int index) {
|
|
|
|
return list.get(index);
|
|
|
|
}
|
|
|
|
public String getColumnName(int index) {
|
|
|
|
return COLUMNS[index];
|
|
|
|
}
|
|
|
|
public int getColumnCount() {
|
|
|
|
return COLUMNS.length;
|
|
|
|
}
|
|
|
|
public Object getValueAt(int row, int col) {
|
|
|
|
HistoryEntry entry = this.list.get(row);
|
|
|
|
switch (col) {
|
|
|
|
case 0:
|
|
|
|
return entry.url;
|
|
|
|
case 1:
|
|
|
|
return dateToHumanReadable(entry.startDate);
|
|
|
|
case 2:
|
|
|
|
return dateToHumanReadable(entry.modifiedDate);
|
|
|
|
case 3:
|
|
|
|
return entry.count;
|
|
|
|
case 4:
|
|
|
|
return entry.selected;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private String dateToHumanReadable(Date date) {
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
|
return sdf.format(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean containsURL(String url) {
|
|
|
|
for (HistoryEntry entry : this.list) {
|
|
|
|
if (entry.url.equals(url)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HistoryEntry getEntryByURL(String url) {
|
|
|
|
for (HistoryEntry entry : this.list) {
|
|
|
|
if (entry.url.equals(url)) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new RuntimeException("Could not find URL " + url + " in History");
|
|
|
|
}
|
2014-07-30 16:48:52 +02:00
|
|
|
|
|
|
|
public void fromJSON(JSONArray jsonArray) {
|
|
|
|
JSONObject json;
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
|
json = jsonArray.getJSONObject(i);
|
2014-08-01 19:20:42 +02:00
|
|
|
list.add(new HistoryEntry().fromJSON(json));
|
2014-07-30 16:48:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void fromFile(String filename) throws IOException {
|
|
|
|
InputStream is = new FileInputStream(filename);
|
|
|
|
try {
|
|
|
|
String jsonString = IOUtils.toString(is);
|
|
|
|
JSONArray jsonArray = new JSONArray(jsonString);
|
|
|
|
fromJSON(jsonArray);
|
2015-02-08 08:39:27 +01:00
|
|
|
} catch (JSONException e) {
|
|
|
|
throw new IOException("Failed to load JSON file " + filename + ": " + e.getMessage(), e);
|
2014-07-30 16:48:52 +02:00
|
|
|
} finally {
|
|
|
|
is.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void fromList(List<String> stringList) {
|
|
|
|
for (String item : stringList) {
|
2014-08-01 19:20:42 +02:00
|
|
|
HistoryEntry entry = new HistoryEntry();
|
2014-07-30 16:48:52 +02:00
|
|
|
entry.url = item;
|
|
|
|
list.add(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public JSONArray toJSON() {
|
|
|
|
JSONArray jsonArray = new JSONArray();
|
2014-08-01 19:20:42 +02:00
|
|
|
for (HistoryEntry entry : list) {
|
2014-07-30 16:48:52 +02:00
|
|
|
jsonArray.put(entry.toJSON());
|
|
|
|
}
|
|
|
|
return jsonArray;
|
|
|
|
}
|
|
|
|
|
2014-08-01 19:20:42 +02:00
|
|
|
public List<HistoryEntry> toList() {
|
2014-07-30 16:48:52 +02:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void toFile(String filename) throws IOException {
|
|
|
|
OutputStream os = new FileOutputStream(filename);
|
|
|
|
try {
|
2015-01-11 09:23:43 +01:00
|
|
|
IOUtils.write(toJSON().toString(2), os);
|
2014-07-30 16:48:52 +02:00
|
|
|
} finally {
|
|
|
|
os.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|