X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserter.java;h=d4f53c1615c9aed37ba18c4128050fdef260e430;hb=f083ad3957357d2f861e87128ee355e842c70762;hp=d24b5649393d365e5f2d9236742c22a9ff8ed4da;hpb=584485247a06f2b2b9d87bc76c46f94968deb4af;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java
index d24b564..d4f53c1 100644
--- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java
+++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java
@@ -33,6 +33,8 @@ import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.freenet.StringBucket;
import net.pterodactylus.sone.main.SonePlugin;
+import net.pterodactylus.util.collection.ListBuilder;
+import net.pterodactylus.util.collection.ReverseComparator;
import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.service.AbstractService;
@@ -81,6 +83,9 @@ public class SoneInserter extends AbstractService {
/** The Sone to insert. */
private final Sone sone;
+ /** The insert listener manager. */
+ private SoneInsertListenerManager soneInsertListenerManager;
+
/** Whether a modification has been detected. */
private volatile boolean modified = false;
@@ -102,6 +107,31 @@ public class SoneInserter extends AbstractService {
this.core = core;
this.freenetInterface = freenetInterface;
this.sone = sone;
+ this.soneInsertListenerManager = new SoneInsertListenerManager(sone);
+ }
+
+ //
+ // LISTENER MANAGEMENT
+ //
+
+ /**
+ * Adds a listener for Sone insert events.
+ *
+ * @param soneInsertListener
+ * The Sone insert listener
+ */
+ public void addSoneInsertListener(SoneInsertListener soneInsertListener) {
+ soneInsertListenerManager.addListener(soneInsertListener);
+ }
+
+ /**
+ * Removes a listener for Sone insert events.
+ *
+ * @param soneInsertListener
+ * The Sone insert listener
+ */
+ public void removeSoneInsertListener(SoneInsertListener soneInsertListener) {
+ soneInsertListenerManager.removeListener(soneInsertListener);
}
//
@@ -204,7 +234,9 @@ public class SoneInserter extends AbstractService {
core.setSoneStatus(sone, SoneStatus.inserting);
long insertTime = System.currentTimeMillis();
insertInformation.setTime(insertTime);
+ soneInsertListenerManager.fireInsertStarted();
FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
+ soneInsertListenerManager.fireInsertFinished(System.currentTimeMillis() - insertTime);
/* at this point we might already be stopped. */
if (shouldStop()) {
/* if so, bail out, donât change anything. */
@@ -216,6 +248,7 @@ public class SoneInserter extends AbstractService {
success = true;
logger.log(Level.INFO, "Inserted Sone â%sâ at %s.", new Object[] { sone.getName(), finalUri });
} catch (SoneException se1) {
+ soneInsertListenerManager.fireInsertAborted(se1);
logger.log(Level.WARNING, "Could not insert Sone â" + sone.getName() + "â!", se1);
} finally {
core.setSoneStatus(sone, SoneStatus.idle);
@@ -247,7 +280,7 @@ public class SoneInserter extends AbstractService {
*
* @author David âBombeâ Roden
*/
- private static class InsertInformation {
+ private class InsertInformation {
/** All properties of the Sone, copied for thread safety. */
private final Map soneProperties = new HashMap();
@@ -265,8 +298,8 @@ public class SoneInserter extends AbstractService {
soneProperties.put("requestUri", sone.getRequestUri());
soneProperties.put("insertUri", sone.getInsertUri());
soneProperties.put("profile", sone.getProfile());
- soneProperties.put("posts", new ArrayList(sone.getPosts()));
- soneProperties.put("replies", new HashSet(sone.getReplies()));
+ soneProperties.put("posts", new ListBuilder(new ArrayList(sone.getPosts())).sort(Post.TIME_COMPARATOR).get());
+ soneProperties.put("replies", new ListBuilder(new ArrayList(sone.getReplies())).sort(new ReverseComparator(Reply.TIME_COMPARATOR)).get());
soneProperties.put("likedPostIds", new HashSet(sone.getLikedPostIds()));
soneProperties.put("likedReplyIds", new HashSet(sone.getLikedReplyIds()));
}
@@ -345,7 +378,9 @@ public class SoneInserter extends AbstractService {
}
TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.set("core", core);
templateContext.set("currentSone", soneProperties);
+ templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition());
templateContext.set("version", SonePlugin.VERSION);
StringWriter writer = new StringWriter();
StringBucket bucket = null;