Merge branch 'edit-wot-trust' into next
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 7277533..27e8db8 100644 (file)
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-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
@@ -1138,6 +1153,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<Post> posts = new HashSet<Post>();
                while (true) {
@@ -1271,6 +1297,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 +1557,8 @@ public class Core implements IdentityListener {
         */
        public void start() {
                loadConfiguration();
+               updateChecker.addUpdateListener(this);
+               updateChecker.start();
        }
 
        /**
@@ -1533,6 +1570,8 @@ public class Core implements IdentityListener {
                                soneInserter.stop();
                        }
                }
+               updateChecker.stop();
+               updateChecker.removeUpdateListener(this);
                soneDownloader.stop();
                saveConfiguration();
                stopped = true;
@@ -1552,6 +1591,7 @@ 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());
@@ -1756,4 +1796,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);
+       }
+
 }