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;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
public class History {
|
|
|
|
|
2014-08-01 19:20:42 +02:00
|
|
|
private List<HistoryEntry> list = new ArrayList<HistoryEntry>();
|
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);
|
|
|
|
}
|
|
|
|
public void clear() {
|
|
|
|
list.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|