2 * Sone - Core.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.core.Options.DefaultOption;
32 import net.pterodactylus.sone.core.Options.Option;
33 import net.pterodactylus.sone.core.Options.OptionWatcher;
34 import net.pterodactylus.sone.data.Client;
35 import net.pterodactylus.sone.data.Post;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Profile.Field;
38 import net.pterodactylus.sone.data.Reply;
39 import net.pterodactylus.sone.data.Sone;
40 import net.pterodactylus.sone.freenet.wot.Identity;
41 import net.pterodactylus.sone.freenet.wot.IdentityListener;
42 import net.pterodactylus.sone.freenet.wot.IdentityManager;
43 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
44 import net.pterodactylus.sone.freenet.wot.Trust;
45 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
46 import net.pterodactylus.sone.main.SonePlugin;
47 import net.pterodactylus.util.config.Configuration;
48 import net.pterodactylus.util.config.ConfigurationException;
49 import net.pterodactylus.util.logging.Logging;
50 import net.pterodactylus.util.number.Numbers;
51 import net.pterodactylus.util.validation.Validation;
52 import net.pterodactylus.util.version.Version;
53 import freenet.keys.FreenetURI;
58 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60 public class Core implements IdentityListener, UpdateListener {
63 * Enumeration for the possible states of a {@link Sone}.
65 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
67 public enum SoneStatus {
69 /** The Sone is unknown, i.e. not yet downloaded. */
72 /** The Sone is idle, i.e. not being downloaded or inserted. */
75 /** The Sone is currently being inserted. */
78 /** The Sone is currently being downloaded. */
83 private static final Logger logger = Logging.getLogger(Core.class);
86 private final Options options = new Options();
88 /** The preferences. */
89 private final Preferences preferences = new Preferences(options);
91 /** The core listener manager. */
92 private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
94 /** The configuration. */
95 private Configuration configuration;
97 /** Whether we’re currently saving the configuration. */
98 private boolean storingConfiguration = false;
100 /** The identity manager. */
101 private final IdentityManager identityManager;
103 /** Interface to freenet. */
104 private final FreenetInterface freenetInterface;
106 /** The Sone downloader. */
107 private final SoneDownloader soneDownloader;
109 /** The update checker. */
110 private final UpdateChecker updateChecker;
112 /** Whether the core has been stopped. */
113 private volatile boolean stopped;
115 /** The Sones’ statuses. */
116 /* synchronize access on itself. */
117 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
119 /** Locked local Sones. */
120 /* synchronize on itself. */
121 private final Set<Sone> lockedSones = new HashSet<Sone>();
123 /** Sone inserters. */
124 /* synchronize access on this on localSones. */
125 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
127 /** All local Sones. */
128 /* synchronize access on this on itself. */
129 private Map<String, Sone> localSones = new HashMap<String, Sone>();
131 /** All remote Sones. */
132 /* synchronize access on this on itself. */
133 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
135 /** All new Sones. */
136 private Set<String> newSones = new HashSet<String>();
138 /** All known Sones. */
139 /* synchronize access on {@link #newSones}. */
140 private Set<String> knownSones = new HashSet<String>();
143 private Map<String, Post> posts = new HashMap<String, Post>();
145 /** All new posts. */
146 private Set<String> newPosts = new HashSet<String>();
148 /** All known posts. */
149 /* synchronize access on {@link #newPosts}. */
150 private Set<String> knownPosts = new HashSet<String>();
153 private Map<String, Reply> replies = new HashMap<String, Reply>();
155 /** All new replies. */
156 private Set<String> newReplies = new HashSet<String>();
158 /** All known replies. */
159 private Set<String> knownReplies = new HashSet<String>();
161 /** Trusted identities, sorted by own identities. */
162 private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
165 * Creates a new core.
167 * @param configuration
168 * The configuration of the core
169 * @param freenetInterface
170 * The freenet interface
171 * @param identityManager
172 * The identity manager
174 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
175 this.configuration = configuration;
176 this.freenetInterface = freenetInterface;
177 this.identityManager = identityManager;
178 this.soneDownloader = new SoneDownloader(this, freenetInterface);
179 this.updateChecker = new UpdateChecker(freenetInterface);
183 // LISTENER MANAGEMENT
187 * Adds a new core listener.
189 * @param coreListener
190 * The listener to add
192 public void addCoreListener(CoreListener coreListener) {
193 coreListenerManager.addListener(coreListener);
197 * Removes a core listener.
199 * @param coreListener
200 * The listener to remove
202 public void removeCoreListener(CoreListener coreListener) {
203 coreListenerManager.removeListener(coreListener);
211 * Sets the configuration to use. This will automatically save the current
212 * configuration to the given configuration.
214 * @param configuration
215 * The new configuration to use
217 public void setConfiguration(Configuration configuration) {
218 this.configuration = configuration;
223 * Returns the options used by the core.
225 * @return The options of the core
227 public Preferences getPreferences() {
232 * Returns the identity manager used by the core.
234 * @return The identity manager
236 public IdentityManager getIdentityManager() {
237 return identityManager;
241 * Returns the update checker.
243 * @return The update checker
245 public UpdateChecker getUpdateChecker() {
246 return updateChecker;
250 * Returns the status of the given Sone.
253 * The Sone to get the status for
254 * @return The status of the Sone
256 public SoneStatus getSoneStatus(Sone sone) {
257 synchronized (soneStatuses) {
258 return soneStatuses.get(sone);
263 * Sets the status of the given Sone.
266 * The Sone to set the status of
270 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
271 synchronized (soneStatuses) {
272 soneStatuses.put(sone, soneStatus);
277 * Returns whether the given Sone is currently locked.
281 * @return {@code true} if the Sone is locked, {@code false} if it is not
283 public boolean isLocked(Sone sone) {
284 synchronized (lockedSones) {
285 return lockedSones.contains(sone);
290 * Returns all Sones, remote and local.
294 public Set<Sone> getSones() {
295 Set<Sone> allSones = new HashSet<Sone>();
296 allSones.addAll(getLocalSones());
297 allSones.addAll(getRemoteSones());
302 * Returns the Sone with the given ID, regardless whether it’s local or
306 * The ID of the Sone to get
307 * @return The Sone with the given ID, or {@code null} if there is no such
310 public Sone getSone(String id) {
311 return getSone(id, true);
315 * Returns the Sone with the given ID, regardless whether it’s local or
319 * The ID of the Sone to get
321 * {@code true} to create a new Sone if none exists,
322 * {@code false} to return {@code null} if a Sone with the given
324 * @return The Sone with the given ID, or {@code null} if there is no such
327 public Sone getSone(String id, boolean create) {
328 if (isLocalSone(id)) {
329 return getLocalSone(id);
331 return getRemoteSone(id, create);
335 * Checks whether the core knows a Sone with the given ID.
339 * @return {@code true} if there is a Sone with the given ID, {@code false}
342 public boolean hasSone(String id) {
343 return isLocalSone(id) || isRemoteSone(id);
347 * Returns whether the given Sone is a local Sone.
350 * The Sone to check for its locality
351 * @return {@code true} if the given Sone is local, {@code false} otherwise
353 public boolean isLocalSone(Sone sone) {
354 synchronized (localSones) {
355 return localSones.containsKey(sone.getId());
360 * Returns whether the given ID is the ID of a local Sone.
363 * The Sone ID to check for its locality
364 * @return {@code true} if the given ID is a local Sone, {@code false}
367 public boolean isLocalSone(String id) {
368 synchronized (localSones) {
369 return localSones.containsKey(id);
374 * Returns all local Sones.
376 * @return All local Sones
378 public Set<Sone> getLocalSones() {
379 synchronized (localSones) {
380 return new HashSet<Sone>(localSones.values());
385 * Returns the local Sone with the given ID.
388 * The ID of the Sone to get
389 * @return The Sone with the given ID
391 public Sone getLocalSone(String id) {
392 return getLocalSone(id, true);
396 * Returns the local Sone with the given ID, optionally creating a new Sone.
401 * {@code true} to create a new Sone if none exists,
402 * {@code false} to return null if none exists
403 * @return The Sone with the given ID, or {@code null}
405 public Sone getLocalSone(String id, boolean create) {
406 synchronized (localSones) {
407 Sone sone = localSones.get(id);
408 if ((sone == null) && create) {
410 localSones.put(id, sone);
411 setSoneStatus(sone, SoneStatus.unknown);
418 * Returns all remote Sones.
420 * @return All remote Sones
422 public Set<Sone> getRemoteSones() {
423 synchronized (remoteSones) {
424 return new HashSet<Sone>(remoteSones.values());
429 * Returns the remote Sone with the given ID.
432 * The ID of the remote Sone to get
433 * @return The Sone with the given ID
435 public Sone getRemoteSone(String id) {
436 return getRemoteSone(id, true);
440 * Returns the remote Sone with the given ID.
443 * The ID of the remote Sone to get
445 * {@code true} to always create a Sone, {@code false} to return
446 * {@code null} if no Sone with the given ID exists
447 * @return The Sone with the given ID
449 public Sone getRemoteSone(String id, boolean create) {
450 synchronized (remoteSones) {
451 Sone sone = remoteSones.get(id);
452 if ((sone == null) && create) {
454 remoteSones.put(id, sone);
455 setSoneStatus(sone, SoneStatus.unknown);
462 * Returns whether the given Sone is a remote Sone.
466 * @return {@code true} if the given Sone is a remote Sone, {@code false}
469 public boolean isRemoteSone(Sone sone) {
470 synchronized (remoteSones) {
471 return remoteSones.containsKey(sone.getId());
476 * Returns whether the Sone with the given ID is a remote Sone.
479 * The ID of the Sone to check
480 * @return {@code true} if the Sone with the given ID is a remote Sone,
481 * {@code false} otherwise
483 public boolean isRemoteSone(String id) {
484 synchronized (remoteSones) {
485 return remoteSones.containsKey(id);
490 * Returns whether the Sone with the given ID is a new Sone.
493 * The ID of the sone to check for
494 * @return {@code true} if the given Sone is new, false otherwise
496 public boolean isNewSone(String soneId) {
497 synchronized (newSones) {
498 return !knownSones.contains(soneId) && newSones.contains(soneId);
503 * Returns whether the given Sone has been modified.
506 * The Sone to check for modifications
507 * @return {@code true} if a modification has been detected in the Sone,
508 * {@code false} otherwise
510 public boolean isModifiedSone(Sone sone) {
511 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
515 * Returns whether the target Sone is trusted by the origin Sone.
521 * @return {@code true} if the target Sone is trusted by the origin Sone
523 public boolean isSoneTrusted(Sone origin, Sone target) {
524 return trustedIdentities.containsKey(origin) && trustedIdentities.get(origin.getIdentity()).contains(target);
528 * Returns the post with the given ID.
531 * The ID of the post to get
532 * @return The post, or {@code null} if there is no such post
534 public Post getPost(String postId) {
535 return getPost(postId, true);
539 * Returns the post with the given ID, optionally creating a new post.
542 * The ID of the post to get
544 * {@code true} it create a new post if no post with the given ID
545 * exists, {@code false} to return {@code null}
546 * @return The post, or {@code null} if there is no such post
548 public Post getPost(String postId, boolean create) {
549 synchronized (posts) {
550 Post post = posts.get(postId);
551 if ((post == null) && create) {
552 post = new Post(postId);
553 posts.put(postId, post);
560 * Returns whether the given post ID is new.
564 * @return {@code true} if the post is considered to be new, {@code false}
567 public boolean isNewPost(String postId) {
568 synchronized (newPosts) {
569 return !knownPosts.contains(postId) && newPosts.contains(postId);
574 * Returns the reply with the given ID. If there is no reply with the given
575 * ID yet, a new one is created.
578 * The ID of the reply to get
581 public Reply getReply(String replyId) {
582 return getReply(replyId, true);
586 * Returns the reply with the given ID. If there is no reply with the given
587 * ID yet, a new one is created, unless {@code create} is false in which
588 * case {@code null} is returned.
591 * The ID of the reply to get
593 * {@code true} to always return a {@link Reply}, {@code false}
594 * to return {@code null} if no reply can be found
595 * @return The reply, or {@code null} if there is no such reply
597 public Reply getReply(String replyId, boolean create) {
598 synchronized (replies) {
599 Reply reply = replies.get(replyId);
600 if (create && (reply == null)) {
601 reply = new Reply(replyId);
602 replies.put(replyId, reply);
609 * Returns all replies for the given post, order ascending by time.
612 * The post to get all replies for
613 * @return All replies for the given post
615 public List<Reply> getReplies(Post post) {
616 Set<Sone> sones = getSones();
617 List<Reply> replies = new ArrayList<Reply>();
618 for (Sone sone : sones) {
619 for (Reply reply : sone.getReplies()) {
620 if (reply.getPost().equals(post)) {
625 Collections.sort(replies, Reply.TIME_COMPARATOR);
630 * Returns whether the reply with the given ID is new.
633 * The ID of the reply to check
634 * @return {@code true} if the reply is considered to be new, {@code false}
637 public boolean isNewReply(String replyId) {
638 synchronized (newReplies) {
639 return !knownReplies.contains(replyId) && newReplies.contains(replyId);
644 * Returns all Sones that have liked the given post.
647 * The post to get the liking Sones for
648 * @return The Sones that like the given post
650 public Set<Sone> getLikes(Post post) {
651 Set<Sone> sones = new HashSet<Sone>();
652 for (Sone sone : getSones()) {
653 if (sone.getLikedPostIds().contains(post.getId())) {
661 * Returns all Sones that have liked the given reply.
664 * The reply to get the liking Sones for
665 * @return The Sones that like the given reply
667 public Set<Sone> getLikes(Reply reply) {
668 Set<Sone> sones = new HashSet<Sone>();
669 for (Sone sone : getSones()) {
670 if (sone.getLikedReplyIds().contains(reply.getId())) {
682 * Locks the given Sone. A locked Sone will not be inserted by
683 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
689 public void lockSone(Sone sone) {
690 synchronized (lockedSones) {
691 if (lockedSones.add(sone)) {
692 coreListenerManager.fireSoneLocked(sone);
698 * Unlocks the given Sone.
700 * @see #lockSone(Sone)
704 public void unlockSone(Sone sone) {
705 synchronized (lockedSones) {
706 if (lockedSones.remove(sone)) {
707 coreListenerManager.fireSoneUnlocked(sone);
713 * Adds a local Sone from the given ID which has to be the ID of an own
717 * The ID of an own identity to add a Sone for
718 * @return The added (or already existing) Sone
720 public Sone addLocalSone(String id) {
721 synchronized (localSones) {
722 if (localSones.containsKey(id)) {
723 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
724 return localSones.get(id);
726 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
727 if (ownIdentity == null) {
728 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
731 return addLocalSone(ownIdentity);
736 * Adds a local Sone from the given own identity.
739 * The own identity to create a Sone from
740 * @return The added (or already existing) Sone
742 public Sone addLocalSone(OwnIdentity ownIdentity) {
743 if (ownIdentity == null) {
744 logger.log(Level.WARNING, "Given OwnIdentity is null!");
747 synchronized (localSones) {
750 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
751 } catch (MalformedURLException mue1) {
752 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
755 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
756 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
757 /* TODO - load posts ’n stuff */
758 localSones.put(ownIdentity.getId(), sone);
759 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
760 soneInserters.put(sone, soneInserter);
761 setSoneStatus(sone, SoneStatus.idle);
763 if (!preferences.isSoneRescueMode()) {
764 soneInserter.start();
766 new Thread(new Runnable() {
769 @SuppressWarnings("synthetic-access")
771 if (!preferences.isSoneRescueMode()) {
772 soneDownloader.fetchSone(sone);
775 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
776 coreListenerManager.fireRescuingSone(sone);
778 long edition = sone.getLatestEdition();
779 while (!stopped && (edition >= 0) && preferences.isSoneRescueMode()) {
780 logger.log(Level.FINE, "Downloading edition " + edition + "…");
781 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
784 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
786 coreListenerManager.fireRescuedSone(sone);
787 soneInserter.start();
790 }, "Sone Downloader").start();
796 * Creates a new Sone for the given own identity.
799 * The own identity to create a Sone for
800 * @return The created Sone
802 public Sone createSone(OwnIdentity ownIdentity) {
804 ownIdentity.addContext("Sone");
805 } catch (WebOfTrustException wote1) {
806 logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
809 Sone sone = addLocalSone(ownIdentity);
814 * Adds the Sone of the given identity.
817 * The identity whose Sone to add
818 * @return The added or already existing Sone
820 public Sone addRemoteSone(Identity identity) {
821 if (identity == null) {
822 logger.log(Level.WARNING, "Given Identity is null!");
825 synchronized (remoteSones) {
826 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
827 boolean newSone = sone.getRequestUri() == null;
828 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
829 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
831 synchronized (newSones) {
832 newSone = !knownSones.contains(sone.getId());
834 newSones.add(sone.getId());
838 coreListenerManager.fireNewSoneFound(sone);
841 remoteSones.put(identity.getId(), sone);
842 soneDownloader.addSone(sone);
843 setSoneStatus(sone, SoneStatus.unknown);
844 new Thread(new Runnable() {
847 @SuppressWarnings("synthetic-access")
849 soneDownloader.fetchSone(sone);
852 }, "Sone Downloader").start();
858 * Retrieves the trust relationship from the origin to the target. If the
859 * trust relationship can not be retrieved, {@code null} is returned.
861 * @see Identity#getTrust(OwnIdentity)
863 * The origin of the trust tree
865 * The target of the trust
866 * @return The trust relationship
868 public Trust getTrust(Sone origin, Sone target) {
869 if (!isLocalSone(origin)) {
870 logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
873 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
877 * Sets the trust value of the given origin Sone for the target Sone.
884 * The trust value (from {@code -100} to {@code 100})
886 public void setTrust(Sone origin, Sone target, int trustValue) {
887 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();
889 ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
890 } catch (WebOfTrustException wote1) {
891 logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
896 * Removes any trust assignment for the given target Sone.
903 public void removeTrust(Sone origin, Sone target) {
904 Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
906 ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
907 } catch (WebOfTrustException wote1) {
908 logger.log(Level.WARNING, "Could not remove trust for Sone: " + target, wote1);
913 * Assigns the configured positive trust value for the given target.
920 public void trustSone(Sone origin, Sone target) {
921 setTrust(origin, target, preferences.getPositiveTrust());
925 * Assigns the configured negative trust value for the given target.
932 public void distrustSone(Sone origin, Sone target) {
933 setTrust(origin, target, preferences.getNegativeTrust());
937 * Removes the trust assignment for the given target.
944 public void untrustSone(Sone origin, Sone target) {
945 removeTrust(origin, target);
949 * Updates the stores Sone with the given Sone.
954 public void updateSone(Sone sone) {
955 if (hasSone(sone.getId())) {
956 boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode();
957 Sone storedSone = getSone(sone.getId());
958 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
959 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
962 synchronized (posts) {
963 if (!soneRescueMode) {
964 for (Post post : storedSone.getPosts()) {
965 posts.remove(post.getId());
966 if (!sone.getPosts().contains(post)) {
967 coreListenerManager.firePostRemoved(post);
971 List<Post> storedPosts = storedSone.getPosts();
972 synchronized (newPosts) {
973 for (Post post : sone.getPosts()) {
974 post.setSone(storedSone);
975 if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
976 newPosts.add(post.getId());
977 coreListenerManager.fireNewPostFound(post);
979 posts.put(post.getId(), post);
983 synchronized (replies) {
984 if (!soneRescueMode) {
985 for (Reply reply : storedSone.getReplies()) {
986 replies.remove(reply.getId());
987 if (!sone.getReplies().contains(reply)) {
988 coreListenerManager.fireReplyRemoved(reply);
992 Set<Reply> storedReplies = storedSone.getReplies();
993 synchronized (newReplies) {
994 for (Reply reply : sone.getReplies()) {
995 reply.setSone(storedSone);
996 if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
997 newReplies.add(reply.getId());
998 coreListenerManager.fireNewReplyFound(reply);
1000 replies.put(reply.getId(), reply);
1004 synchronized (storedSone) {
1005 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
1006 storedSone.setTime(sone.getTime());
1008 storedSone.setClient(sone.getClient());
1009 storedSone.setProfile(sone.getProfile());
1010 if (soneRescueMode) {
1011 for (Post post : sone.getPosts()) {
1012 storedSone.addPost(post);
1014 for (Reply reply : sone.getReplies()) {
1015 storedSone.addReply(reply);
1017 for (String likedPostId : sone.getLikedPostIds()) {
1018 storedSone.addLikedPostId(likedPostId);
1020 for (String likedReplyId : sone.getLikedReplyIds()) {
1021 storedSone.addLikedReplyId(likedReplyId);
1024 storedSone.setPosts(sone.getPosts());
1025 storedSone.setReplies(sone.getReplies());
1026 storedSone.setLikePostIds(sone.getLikedPostIds());
1027 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
1029 storedSone.setLatestEdition(sone.getLatestEdition());
1035 * Deletes the given Sone. This will remove the Sone from the
1036 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1037 * and remove the context from its identity.
1040 * The Sone to delete
1042 public void deleteSone(Sone sone) {
1043 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1044 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1047 synchronized (localSones) {
1048 if (!localSones.containsKey(sone.getId())) {
1049 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1052 localSones.remove(sone.getId());
1053 soneInserters.remove(sone).stop();
1056 ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1057 ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1058 } catch (WebOfTrustException wote1) {
1059 logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1062 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1063 } catch (ConfigurationException ce1) {
1064 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1069 * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
1070 * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
1073 * The Sone to mark as known
1075 public void markSoneKnown(Sone sone) {
1076 synchronized (newSones) {
1077 if (newSones.remove(sone.getId())) {
1078 knownSones.add(sone.getId());
1079 coreListenerManager.fireMarkSoneKnown(sone);
1080 saveConfiguration();
1086 * Loads and updates the given Sone from the configuration. If any error is
1087 * encountered, loading is aborted and the given Sone is not changed.
1090 * The Sone to load and update
1092 public void loadSone(Sone sone) {
1093 if (!isLocalSone(sone)) {
1094 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1099 String sonePrefix = "Sone/" + sone.getId();
1100 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1101 if (soneTime == null) {
1102 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1105 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1108 Profile profile = new Profile();
1109 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1110 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1111 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1112 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1113 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1114 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1116 /* load profile fields. */
1118 String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
1119 String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
1120 if (fieldName == null) {
1123 String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
1124 profile.addField(fieldName).setValue(fieldValue);
1128 Set<Post> posts = new HashSet<Post>();
1130 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1131 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1132 if (postId == null) {
1135 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1136 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1137 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1138 if ((postTime == 0) || (postText == null)) {
1139 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1142 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1143 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1144 post.setRecipient(getSone(postRecipientId));
1150 Set<Reply> replies = new HashSet<Reply>();
1152 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1153 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1154 if (replyId == null) {
1157 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1158 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1159 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1160 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1161 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1164 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1167 /* load post likes. */
1168 Set<String> likedPostIds = new HashSet<String>();
1170 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1171 if (likedPostId == null) {
1174 likedPostIds.add(likedPostId);
1177 /* load reply likes. */
1178 Set<String> likedReplyIds = new HashSet<String>();
1180 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1181 if (likedReplyId == null) {
1184 likedReplyIds.add(likedReplyId);
1188 Set<String> friends = new HashSet<String>();
1190 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1191 if (friendId == null) {
1194 friends.add(friendId);
1197 /* if we’re still here, Sone was loaded successfully. */
1198 synchronized (sone) {
1199 sone.setTime(soneTime);
1200 sone.setProfile(profile);
1201 sone.setPosts(posts);
1202 sone.setReplies(replies);
1203 sone.setLikePostIds(likedPostIds);
1204 sone.setLikeReplyIds(likedReplyIds);
1205 sone.setFriends(friends);
1206 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1208 synchronized (newSones) {
1209 for (String friend : friends) {
1210 knownSones.add(friend);
1213 synchronized (newPosts) {
1214 for (Post post : posts) {
1215 knownPosts.add(post.getId());
1218 synchronized (newReplies) {
1219 for (Reply reply : replies) {
1220 knownReplies.add(reply.getId());
1226 * Saves the given Sone. This will persist all local settings for the given
1227 * Sone, such as the friends list and similar, private options.
1232 public synchronized void saveSone(Sone sone) {
1233 if (!isLocalSone(sone)) {
1234 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1237 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1238 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1242 logger.log(Level.INFO, "Saving Sone: %s", sone);
1244 ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1246 /* save Sone into configuration. */
1247 String sonePrefix = "Sone/" + sone.getId();
1248 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1249 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1252 Profile profile = sone.getProfile();
1253 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1254 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1255 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1256 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1257 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1258 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1260 /* save profile fields. */
1261 int fieldCounter = 0;
1262 for (Field profileField : profile.getFields()) {
1263 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1264 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1265 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1267 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1270 int postCounter = 0;
1271 for (Post post : sone.getPosts()) {
1272 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1273 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1274 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1275 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1276 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1278 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1281 int replyCounter = 0;
1282 for (Reply reply : sone.getReplies()) {
1283 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1284 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1285 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1286 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1287 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1289 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1291 /* save post likes. */
1292 int postLikeCounter = 0;
1293 for (String postId : sone.getLikedPostIds()) {
1294 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1296 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1298 /* save reply likes. */
1299 int replyLikeCounter = 0;
1300 for (String replyId : sone.getLikedReplyIds()) {
1301 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1303 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1306 int friendCounter = 0;
1307 for (String friendId : sone.getFriends()) {
1308 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1310 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1312 configuration.save();
1313 logger.log(Level.INFO, "Sone %s saved.", sone);
1314 } catch (ConfigurationException ce1) {
1315 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1316 } catch (WebOfTrustException wote1) {
1317 logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1322 * Creates a new post.
1325 * The Sone that creates the post
1327 * The text of the post
1328 * @return The created post
1330 public Post createPost(Sone sone, String text) {
1331 return createPost(sone, System.currentTimeMillis(), text);
1335 * Creates a new post.
1338 * The Sone that creates the post
1340 * The time of the post
1342 * The text of the post
1343 * @return The created post
1345 public Post createPost(Sone sone, long time, String text) {
1346 return createPost(sone, null, time, text);
1350 * Creates a new post.
1353 * The Sone that creates the post
1355 * The recipient Sone, or {@code null} if this post does not have
1358 * The text of the post
1359 * @return The created post
1361 public Post createPost(Sone sone, Sone recipient, String text) {
1362 return createPost(sone, recipient, System.currentTimeMillis(), text);
1366 * Creates a new post.
1369 * The Sone that creates the post
1371 * The recipient Sone, or {@code null} if this post does not have
1374 * The time of the post
1376 * The text of the post
1377 * @return The created post
1379 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1380 if (!isLocalSone(sone)) {
1381 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1384 Post post = new Post(sone, time, text);
1385 if (recipient != null) {
1386 post.setRecipient(recipient);
1388 synchronized (posts) {
1389 posts.put(post.getId(), post);
1391 synchronized (newPosts) {
1392 knownPosts.add(post.getId());
1400 * Deletes the given post.
1403 * The post to delete
1405 public void deletePost(Post post) {
1406 if (!isLocalSone(post.getSone())) {
1407 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1410 post.getSone().removePost(post);
1411 synchronized (posts) {
1412 posts.remove(post.getId());
1414 saveSone(post.getSone());
1418 * Marks the given post as known, if it is currently a new post (according
1419 * to {@link #isNewPost(String)}).
1422 * The post to mark as known
1424 public void markPostKnown(Post post) {
1425 synchronized (newPosts) {
1426 if (newPosts.remove(post.getId())) {
1427 knownPosts.add(post.getId());
1428 coreListenerManager.fireMarkPostKnown(post);
1429 saveConfiguration();
1435 * Creates a new reply.
1438 * The Sone that creates the reply
1440 * The post that this reply refers to
1442 * The text of the reply
1443 * @return The created reply
1445 public Reply createReply(Sone sone, Post post, String text) {
1446 return createReply(sone, post, System.currentTimeMillis(), text);
1450 * Creates a new reply.
1453 * The Sone that creates the reply
1455 * The post that this reply refers to
1457 * The time of the reply
1459 * The text of the reply
1460 * @return The created reply
1462 public Reply createReply(Sone sone, Post post, long time, String text) {
1463 if (!isLocalSone(sone)) {
1464 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1467 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1468 synchronized (replies) {
1469 replies.put(reply.getId(), reply);
1471 synchronized (newReplies) {
1472 knownReplies.add(reply.getId());
1474 sone.addReply(reply);
1480 * Deletes the given reply.
1483 * The reply to delete
1485 public void deleteReply(Reply reply) {
1486 Sone sone = reply.getSone();
1487 if (!isLocalSone(sone)) {
1488 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1491 synchronized (replies) {
1492 replies.remove(reply.getId());
1494 sone.removeReply(reply);
1499 * Marks the given reply as known, if it is currently a new reply (according
1500 * to {@link #isNewReply(String)}).
1503 * The reply to mark as known
1505 public void markReplyKnown(Reply reply) {
1506 synchronized (newReplies) {
1507 if (newReplies.remove(reply.getId())) {
1508 knownReplies.add(reply.getId());
1509 coreListenerManager.fireMarkReplyKnown(reply);
1510 saveConfiguration();
1518 public void start() {
1519 loadConfiguration();
1520 updateChecker.addUpdateListener(this);
1521 updateChecker.start();
1527 public void stop() {
1528 synchronized (localSones) {
1529 for (SoneInserter soneInserter : soneInserters.values()) {
1530 soneInserter.stop();
1533 updateChecker.stop();
1534 updateChecker.removeUpdateListener(this);
1535 soneDownloader.stop();
1536 saveConfiguration();
1541 * Saves the current options.
1543 public void saveConfiguration() {
1544 synchronized (configuration) {
1545 if (storingConfiguration) {
1546 logger.log(Level.FINE, "Already storing configuration…");
1549 storingConfiguration = true;
1552 /* store the options first. */
1554 configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
1555 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1556 configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
1557 configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
1558 configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
1559 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1560 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1561 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1563 /* save known Sones. */
1564 int soneCounter = 0;
1565 synchronized (newSones) {
1566 for (String knownSoneId : knownSones) {
1567 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1569 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1572 /* save known posts. */
1573 int postCounter = 0;
1574 synchronized (newPosts) {
1575 for (String knownPostId : knownPosts) {
1576 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1578 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1581 /* save known replies. */
1582 int replyCounter = 0;
1583 synchronized (newReplies) {
1584 for (String knownReplyId : knownReplies) {
1585 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1587 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1591 configuration.save();
1593 } catch (ConfigurationException ce1) {
1594 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1596 synchronized (configuration) {
1597 storingConfiguration = false;
1607 * Loads the configuration.
1609 @SuppressWarnings("unchecked")
1610 private void loadConfiguration() {
1611 /* create options. */
1612 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1615 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1616 SoneInserter.setInsertionDelay(newValue);
1620 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75));
1621 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-100));
1622 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
1623 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1624 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1625 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1627 /* read options from configuration. */
1628 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1629 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1630 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1631 options.getBooleanOption("ClearOnNextRestart").set(null);
1632 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1633 if (clearConfiguration) {
1634 /* stop loading the configuration. */
1638 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1639 options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
1640 options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
1641 options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
1642 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1644 /* load known Sones. */
1645 int soneCounter = 0;
1647 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1648 if (knownSoneId == null) {
1651 synchronized (newSones) {
1652 knownSones.add(knownSoneId);
1656 /* load known posts. */
1657 int postCounter = 0;
1659 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1660 if (knownPostId == null) {
1663 synchronized (newPosts) {
1664 knownPosts.add(knownPostId);
1668 /* load known replies. */
1669 int replyCounter = 0;
1671 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1672 if (knownReplyId == null) {
1675 synchronized (newReplies) {
1676 knownReplies.add(knownReplyId);
1683 * Generate a Sone URI from the given URI and latest edition.
1686 * The URI to derive the Sone URI from
1687 * @return The derived URI
1689 private FreenetURI getSoneUri(String uriString) {
1691 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1693 } catch (MalformedURLException mue1) {
1694 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1700 // INTERFACE IdentityListener
1707 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1708 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1709 if (ownIdentity.hasContext("Sone")) {
1710 trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1711 addLocalSone(ownIdentity);
1719 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1720 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1721 trustedIdentities.remove(ownIdentity);
1728 public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1729 logger.log(Level.FINEST, "Adding Identity: " + identity);
1730 trustedIdentities.get(ownIdentity).add(identity);
1731 addRemoteSone(identity);
1738 public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1739 new Thread(new Runnable() {
1742 @SuppressWarnings("synthetic-access")
1744 Sone sone = getRemoteSone(identity.getId());
1745 sone.setIdentity(identity);
1746 soneDownloader.addSone(sone);
1747 soneDownloader.fetchSone(sone);
1756 public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1757 trustedIdentities.get(ownIdentity).remove(identity);
1761 // INTERFACE UpdateListener
1768 public void updateFound(Version version, long releaseTime, long latestEdition) {
1769 coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition);
1773 * Convenience interface for external classes that want to access the core’s
1776 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1778 public static class Preferences {
1780 /** The wrapped options. */
1781 private final Options options;
1784 * Creates a new preferences object wrapped around the given options.
1787 * The options to wrap
1789 public Preferences(Options options) {
1790 this.options = options;
1794 * Returns the insertion delay.
1796 * @return The insertion delay
1798 public int getInsertionDelay() {
1799 return options.getIntegerOption("InsertionDelay").get();
1803 * Sets the insertion delay
1805 * @param insertionDelay
1806 * The new insertion delay, or {@code null} to restore it to
1808 * @return This preferences
1810 public Preferences setInsertionDelay(Integer insertionDelay) {
1811 options.getIntegerOption("InsertionDelay").set(insertionDelay);
1816 * Returns the positive trust.
1818 * @return The positive trust
1820 public int getPositiveTrust() {
1821 return options.getIntegerOption("PositiveTrust").get();
1825 * Sets the positive trust.
1827 * @param positiveTrust
1828 * The new positive trust, or {@code null} to restore it to
1830 * @return This preferences
1832 public Preferences setPositiveTrust(Integer positiveTrust) {
1833 options.getIntegerOption("PositiveTrust").set(positiveTrust);
1838 * Returns the negative trust.
1840 * @return The negative trust
1842 public int getNegativeTrust() {
1843 return options.getIntegerOption("NegativeTrust").get();
1847 * Sets the negative trust.
1849 * @param negativeTrust
1850 * The negative trust, or {@code null} to restore it to the
1852 * @return The preferences
1854 public Preferences setNegativeTrust(Integer negativeTrust) {
1855 options.getIntegerOption("NegativeTrust").set(negativeTrust);
1860 * Returns the trust comment. This is the comment that is set in the web
1861 * of trust when a trust value is assigned to an identity.
1863 * @return The trust comment
1865 public String getTrustComment() {
1866 return options.getStringOption("TrustComment").get();
1870 * Sets the trust comment.
1872 * @param trustComment
1873 * The trust comment, or {@code null} to restore it to the
1875 * @return This preferences
1877 public Preferences setTrustComment(String trustComment) {
1878 options.getStringOption("TrustComment").set(trustComment);
1883 * Returns whether the rescue mode is active.
1885 * @return {@code true} if the rescue mode is active, {@code false}
1888 public boolean isSoneRescueMode() {
1889 return options.getBooleanOption("SoneRescueMode").get();
1893 * Sets whether the rescue mode is active.
1895 * @param soneRescueMode
1896 * {@code true} if the rescue mode is active, {@code false}
1898 * @return This preferences
1900 public Preferences setSoneRescueMode(Boolean soneRescueMode) {
1901 options.getBooleanOption("SoneRescueMode").set(soneRescueMode);
1906 * Returns whether Sone should clear its settings on the next restart.
1907 * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
1908 * to return {@code true} as well!
1910 * @return {@code true} if Sone should clear its settings on the next
1911 * restart, {@code false} otherwise
1913 public boolean isClearOnNextRestart() {
1914 return options.getBooleanOption("ClearOnNextRestart").get();
1918 * Sets whether Sone will clear its settings on the next restart.
1920 * @param clearOnNextRestart
1921 * {@code true} if Sone should clear its settings on the next
1922 * restart, {@code false} otherwise
1923 * @return This preferences
1925 public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
1926 options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
1931 * Returns whether Sone should really clear its settings on next
1932 * restart. This is a confirmation option that needs to be set in
1933 * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
1934 * settings on the next restart.
1936 * @return {@code true} if Sone should really clear its settings on the
1937 * next restart, {@code false} otherwise
1939 public boolean isReallyClearOnNextRestart() {
1940 return options.getBooleanOption("ReallyClearOnNextRestart").get();
1944 * Sets whether Sone should really clear its settings on the next
1947 * @param reallyClearOnNextRestart
1948 * {@code true} if Sone should really clear its settings on
1949 * the next restart, {@code false} otherwise
1950 * @return This preferences
1952 public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
1953 options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);