X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=c8d500ecdddec30863fe4c07ad3fa02dac7c9742;hb=977406b376374680eb2dae63dc17d8aea1a780dd;hp=053e04280b21a4a63039203a519a3b1a743a0603;hpb=1f70b96444f309fbbf338d474af395ff615d244b;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java
index 053e042..c8d500e 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -34,6 +34,7 @@ import net.pterodactylus.sone.core.Options.OptionWatcher;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.freenet.wot.Identity;
@@ -48,6 +49,7 @@ import net.pterodactylus.util.config.ConfigurationException;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.validation.Validation;
+import net.pterodactylus.util.version.Version;
import freenet.keys.FreenetURI;
/**
@@ -55,7 +57,7 @@ import freenet.keys.FreenetURI;
*
* @author David âBombeâ Roden
*/
-public class Core implements IdentityListener {
+public class Core implements IdentityListener, UpdateListener {
/**
* Enumeration for the possible states of a {@link Sone}.
@@ -101,6 +103,9 @@ public class Core implements IdentityListener {
/** The Sone downloader. */
private final SoneDownloader soneDownloader;
+ /** The update checker. */
+ private final UpdateChecker updateChecker;
+
/** Whether the core has been stopped. */
private volatile boolean stopped;
@@ -168,6 +173,7 @@ public class Core implements IdentityListener {
this.freenetInterface = freenetInterface;
this.identityManager = identityManager;
this.soneDownloader = new SoneDownloader(this, freenetInterface);
+ this.updateChecker = new UpdateChecker(freenetInterface);
}
//
@@ -239,6 +245,15 @@ public class Core implements IdentityListener {
}
/**
+ * Returns the update checker.
+ *
+ * @return The update checker
+ */
+ public UpdateChecker getUpdateChecker() {
+ return updateChecker;
+ }
+
+ /**
* Returns the status of the given Sone.
*
* @param sone
@@ -400,6 +415,7 @@ public class Core implements IdentityListener {
if ((sone == null) && create) {
sone = new Sone(id);
localSones.put(id, sone);
+ setSoneStatus(sone, SoneStatus.unknown);
}
return sone;
}
@@ -443,6 +459,7 @@ public class Core implements IdentityListener {
if ((sone == null) && create) {
sone = new Sone(id);
remoteSones.put(id, sone);
+ setSoneStatus(sone, SoneStatus.unknown);
}
return sone;
}
@@ -477,20 +494,38 @@ public class Core implements IdentityListener {
}
/**
- * Returns whether the given Sone is a new Sone. After this check, the Sone
- * is marked as known, i.e. a second call with the same parameters will
- * always yield {@code false}.
+ * Returns whether the Sone with the given ID is a new Sone. After this
+ * check, the Sone is marked as known, i.e. a second call with the same
+ * parameters will always yield {@code false}.
*
- * @param sone
- * The sone to check for
+ * @param soneId
+ * The ID of the sone to check for
+ * @return {@code true} if the given Sone is new, false otherwise
+ */
+ public boolean isNewSone(String soneId) {
+ return isNewSone(soneId, true);
+ }
+
+ /**
+ * Returns whether the Sone with the given ID is a new Sone. The Sone will
+ * be marked as known if {@code markAsKnown} is {@code true}, otherwise the
+ * Sone will keep its current ânewâ state.
+ *
+ * @param soneId
+ * The ID of the sone to check for
+ * @param markAsKnown
+ * {@code true} to mark the Sone as known in any case,
+ * {@code false} to not mark it as known
* @return {@code true} if the given Sone is new, false otherwise
*/
- public boolean isNewSone(Sone sone) {
+ public boolean isNewSone(String soneId, boolean markAsKnown) {
synchronized (newSones) {
- boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
- knownSones.add(sone.getId());
- if (isNew) {
- coreListenerManager.fireMarkSoneKnown(sone);
+ boolean isNew = !knownSones.contains(soneId) && newSones.contains(soneId);
+ if (markAsKnown) {
+ Sone sone = getSone(soneId, false);
+ if (sone != null) {
+ markSoneKnown(sone);
+ }
}
return isNew;
}
@@ -930,7 +965,7 @@ public class Core implements IdentityListener {
public void setTrust(Sone origin, Sone target, int trustValue) {
Validation.begin().isNotNull("Trust Origin", origin).check().isInstanceOf("Trust Origin", origin.getIdentity(), OwnIdentity.class).isNotNull("Trust Target", target).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check();
try {
- ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, "Set from Sone Web Interface");
+ ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, options.getStringOption("TrustComment").get());
} catch (WebOfTrustException wote1) {
logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
}
@@ -1108,6 +1143,23 @@ public class Core implements IdentityListener {
}
/**
+ * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
+ * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
+ *
+ * @param sone
+ * The Sone to mark as known
+ */
+ public void markSoneKnown(Sone sone) {
+ synchronized (newSones) {
+ if (newSones.remove(sone.getId())) {
+ knownSones.add(sone.getId());
+ coreListenerManager.fireMarkSoneKnown(sone);
+ saveConfiguration();
+ }
+ }
+ }
+
+ /**
* Loads and updates the given Sone from the configuration. If any error is
* encountered, loading is aborted and the given Sone is not changed.
*
@@ -1138,6 +1190,17 @@ public class Core implements IdentityListener {
profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
+ /* load profile fields. */
+ while (true) {
+ String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
+ String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
+ if (fieldName == null) {
+ break;
+ }
+ String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
+ profile.addField(fieldName).setValue(fieldValue);
+ }
+
/* load posts. */
Set posts = new HashSet();
while (true) {
@@ -1271,6 +1334,15 @@ public class Core implements IdentityListener {
configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
+ /* save profile fields. */
+ int fieldCounter = 0;
+ for (Field profileField : profile.getFields()) {
+ String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
+ configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
+ configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
+ }
+ configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
+
/* save posts. */
int postCounter = 0;
for (Post post : sone.getPosts()) {
@@ -1522,6 +1594,8 @@ public class Core implements IdentityListener {
*/
public void start() {
loadConfiguration();
+ updateChecker.addUpdateListener(this);
+ updateChecker.start();
}
/**
@@ -1533,6 +1607,8 @@ public class Core implements IdentityListener {
soneInserter.stop();
}
}
+ updateChecker.stop();
+ updateChecker.removeUpdateListener(this);
soneDownloader.stop();
saveConfiguration();
stopped = true;
@@ -1552,9 +1628,11 @@ public class Core implements IdentityListener {
/* store the options first. */
try {
+ configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
+ configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
@@ -1618,6 +1696,7 @@ public class Core implements IdentityListener {
}));
options.addIntegerOption("PositiveTrust", new DefaultOption(75));
options.addIntegerOption("NegativeTrust", new DefaultOption(-100));
+ options.addStringOption("TrustComment", new DefaultOption("Set from Sone Web Interface"));
options.addBooleanOption("SoneRescueMode", new DefaultOption(false));
options.addBooleanOption("ClearOnNextRestart", new DefaultOption(false));
options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption(false));
@@ -1636,6 +1715,7 @@ public class Core implements IdentityListener {
options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
+ options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
/* load known Sones. */
@@ -1753,4 +1833,16 @@ public class Core implements IdentityListener {
trustedIdentities.get(ownIdentity).remove(identity);
}
+ //
+ // INTERFACE UpdateListener
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void updateFound(Version version, long releaseTime) {
+ coreListenerManager.fireUpdateFound(version, releaseTime);
+ }
+
}