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 /** All bookmarked posts. */
162 /* synchronize access on itself. */
163 private Set<String> bookmarkedPosts = new HashSet<String>();
165 /** Trusted identities, sorted by own identities. */
166 private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
169 * Creates a new core.
171 * @param configuration
172 * The configuration of the core
173 * @param freenetInterface
174 * The freenet interface
175 * @param identityManager
176 * The identity manager
178 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
179 this.configuration = configuration;
180 this.freenetInterface = freenetInterface;
181 this.identityManager = identityManager;
182 this.soneDownloader = new SoneDownloader(this, freenetInterface);
183 this.updateChecker = new UpdateChecker(freenetInterface);
187 // LISTENER MANAGEMENT
191 * Adds a new core listener.
193 * @param coreListener
194 * The listener to add
196 public void addCoreListener(CoreListener coreListener) {
197 coreListenerManager.addListener(coreListener);
201 * Removes a core listener.
203 * @param coreListener
204 * The listener to remove
206 public void removeCoreListener(CoreListener coreListener) {
207 coreListenerManager.removeListener(coreListener);
215 * Sets the configuration to use. This will automatically save the current
216 * configuration to the given configuration.
218 * @param configuration
219 * The new configuration to use
221 public void setConfiguration(Configuration configuration) {
222 this.configuration = configuration;
227 * Returns the options used by the core.
229 * @return The options of the core
231 public Preferences getPreferences() {
236 * Returns the identity manager used by the core.
238 * @return The identity manager
240 public IdentityManager getIdentityManager() {
241 return identityManager;
245 * Returns the update checker.
247 * @return The update checker
249 public UpdateChecker getUpdateChecker() {
250 return updateChecker;
254 * Returns the status of the given Sone.
257 * The Sone to get the status for
258 * @return The status of the Sone
260 public SoneStatus getSoneStatus(Sone sone) {
261 synchronized (soneStatuses) {
262 return soneStatuses.get(sone);
267 * Sets the status of the given Sone.
270 * The Sone to set the status of
274 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
275 synchronized (soneStatuses) {
276 soneStatuses.put(sone, soneStatus);
281 * Returns whether the given Sone is currently locked.
285 * @return {@code true} if the Sone is locked, {@code false} if it is not
287 public boolean isLocked(Sone sone) {
288 synchronized (lockedSones) {
289 return lockedSones.contains(sone);
294 * Returns all Sones, remote and local.
298 public Set<Sone> getSones() {
299 Set<Sone> allSones = new HashSet<Sone>();
300 allSones.addAll(getLocalSones());
301 allSones.addAll(getRemoteSones());
306 * Returns the Sone with the given ID, regardless whether it’s local or
310 * The ID of the Sone to get
311 * @return The Sone with the given ID, or {@code null} if there is no such
314 public Sone getSone(String id) {
315 return getSone(id, true);
319 * Returns the Sone with the given ID, regardless whether it’s local or
323 * The ID of the Sone to get
325 * {@code true} to create a new Sone if none exists,
326 * {@code false} to return {@code null} if a Sone with the given
328 * @return The Sone with the given ID, or {@code null} if there is no such
331 public Sone getSone(String id, boolean create) {
332 if (isLocalSone(id)) {
333 return getLocalSone(id);
335 return getRemoteSone(id, create);
339 * Checks whether the core knows a Sone with the given ID.
343 * @return {@code true} if there is a Sone with the given ID, {@code false}
346 public boolean hasSone(String id) {
347 return isLocalSone(id) || isRemoteSone(id);
351 * Returns whether the given Sone is a local Sone.
354 * The Sone to check for its locality
355 * @return {@code true} if the given Sone is local, {@code false} otherwise
357 public boolean isLocalSone(Sone sone) {
358 synchronized (localSones) {
359 return localSones.containsKey(sone.getId());
364 * Returns whether the given ID is the ID of a local Sone.
367 * The Sone ID to check for its locality
368 * @return {@code true} if the given ID is a local Sone, {@code false}
371 public boolean isLocalSone(String id) {
372 synchronized (localSones) {
373 return localSones.containsKey(id);
378 * Returns all local Sones.
380 * @return All local Sones
382 public Set<Sone> getLocalSones() {
383 synchronized (localSones) {
384 return new HashSet<Sone>(localSones.values());
389 * Returns the local Sone with the given ID.
392 * The ID of the Sone to get
393 * @return The Sone with the given ID
395 public Sone getLocalSone(String id) {
396 return getLocalSone(id, true);
400 * Returns the local Sone with the given ID, optionally creating a new Sone.
405 * {@code true} to create a new Sone if none exists,
406 * {@code false} to return null if none exists
407 * @return The Sone with the given ID, or {@code null}
409 public Sone getLocalSone(String id, boolean create) {
410 synchronized (localSones) {
411 Sone sone = localSones.get(id);
412 if ((sone == null) && create) {
414 localSones.put(id, sone);
415 setSoneStatus(sone, SoneStatus.unknown);
422 * Returns all remote Sones.
424 * @return All remote Sones
426 public Set<Sone> getRemoteSones() {
427 synchronized (remoteSones) {
428 return new HashSet<Sone>(remoteSones.values());
433 * Returns the remote Sone with the given ID.
436 * The ID of the remote Sone to get
437 * @return The Sone with the given ID
439 public Sone getRemoteSone(String id) {
440 return getRemoteSone(id, true);
444 * Returns the remote Sone with the given ID.
447 * The ID of the remote Sone to get
449 * {@code true} to always create a Sone, {@code false} to return
450 * {@code null} if no Sone with the given ID exists
451 * @return The Sone with the given ID
453 public Sone getRemoteSone(String id, boolean create) {
454 synchronized (remoteSones) {
455 Sone sone = remoteSones.get(id);
456 if ((sone == null) && create) {
458 remoteSones.put(id, sone);
459 setSoneStatus(sone, SoneStatus.unknown);
466 * Returns whether the given Sone is a remote Sone.
470 * @return {@code true} if the given Sone is a remote Sone, {@code false}
473 public boolean isRemoteSone(Sone sone) {
474 synchronized (remoteSones) {
475 return remoteSones.containsKey(sone.getId());
480 * Returns whether the Sone with the given ID is a remote Sone.
483 * The ID of the Sone to check
484 * @return {@code true} if the Sone with the given ID is a remote Sone,
485 * {@code false} otherwise
487 public boolean isRemoteSone(String id) {
488 synchronized (remoteSones) {
489 return remoteSones.containsKey(id);
494 * Returns whether the Sone with the given ID is a new Sone.
497 * The ID of the sone to check for
498 * @return {@code true} if the given Sone is new, false otherwise
500 public boolean isNewSone(String soneId) {
501 synchronized (newSones) {
502 return !knownSones.contains(soneId) && newSones.contains(soneId);
507 * Returns whether the given Sone has been modified.
510 * The Sone to check for modifications
511 * @return {@code true} if a modification has been detected in the Sone,
512 * {@code false} otherwise
514 public boolean isModifiedSone(Sone sone) {
515 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
519 * Returns whether the target Sone is trusted by the origin Sone.
525 * @return {@code true} if the target Sone is trusted by the origin Sone
527 public boolean isSoneTrusted(Sone origin, Sone target) {
528 Validation.begin().isNotNull("Origin", origin).isNotNull("Target", target).check().isInstanceOf("Origin’s OwnIdentity", origin.getIdentity(), OwnIdentity.class).check();
529 return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity());
533 * Returns the post with the given ID.
536 * The ID of the post to get
537 * @return The post with the given ID, or a new post with the given ID
539 public Post getPost(String postId) {
540 return getPost(postId, true);
544 * Returns the post with the given ID, optionally creating a new post.
547 * The ID of the post to get
549 * {@code true} it create a new post if no post with the given ID
550 * exists, {@code false} to return {@code null}
551 * @return The post, or {@code null} if there is no such post
553 public Post getPost(String postId, boolean create) {
554 synchronized (posts) {
555 Post post = posts.get(postId);
556 if ((post == null) && create) {
557 post = new Post(postId);
558 posts.put(postId, post);
565 * Returns whether the given post ID is new.
569 * @return {@code true} if the post is considered to be new, {@code false}
572 public boolean isNewPost(String postId) {
573 synchronized (newPosts) {
574 return !knownPosts.contains(postId) && newPosts.contains(postId);
579 * Returns all posts that have the given Sone as recipient.
581 * @see Post#getRecipient()
583 * The recipient of the posts
584 * @return All posts that have the given Sone as recipient
586 public Set<Post> getDirectedPosts(Sone recipient) {
587 Validation.begin().isNotNull("Recipient", recipient).check();
588 Set<Post> directedPosts = new HashSet<Post>();
589 synchronized (posts) {
590 for (Post post : posts.values()) {
591 if (recipient.equals(post.getRecipient())) {
592 directedPosts.add(post);
596 return directedPosts;
600 * Returns the reply with the given ID. If there is no reply with the given
601 * ID yet, a new one is created.
604 * The ID of the reply to get
607 public Reply getReply(String replyId) {
608 return getReply(replyId, true);
612 * Returns the reply with the given ID. If there is no reply with the given
613 * ID yet, a new one is created, unless {@code create} is false in which
614 * case {@code null} is returned.
617 * The ID of the reply to get
619 * {@code true} to always return a {@link Reply}, {@code false}
620 * to return {@code null} if no reply can be found
621 * @return The reply, or {@code null} if there is no such reply
623 public Reply getReply(String replyId, boolean create) {
624 synchronized (replies) {
625 Reply reply = replies.get(replyId);
626 if (create && (reply == null)) {
627 reply = new Reply(replyId);
628 replies.put(replyId, reply);
635 * Returns all replies for the given post, order ascending by time.
638 * The post to get all replies for
639 * @return All replies for the given post
641 public List<Reply> getReplies(Post post) {
642 Set<Sone> sones = getSones();
643 List<Reply> replies = new ArrayList<Reply>();
644 for (Sone sone : sones) {
645 for (Reply reply : sone.getReplies()) {
646 if (reply.getPost().equals(post)) {
651 Collections.sort(replies, Reply.TIME_COMPARATOR);
656 * Returns whether the reply with the given ID is new.
659 * The ID of the reply to check
660 * @return {@code true} if the reply is considered to be new, {@code false}
663 public boolean isNewReply(String replyId) {
664 synchronized (newReplies) {
665 return !knownReplies.contains(replyId) && newReplies.contains(replyId);
670 * Returns all Sones that have liked the given post.
673 * The post to get the liking Sones for
674 * @return The Sones that like the given post
676 public Set<Sone> getLikes(Post post) {
677 Set<Sone> sones = new HashSet<Sone>();
678 for (Sone sone : getSones()) {
679 if (sone.getLikedPostIds().contains(post.getId())) {
687 * Returns all Sones that have liked the given reply.
690 * The reply to get the liking Sones for
691 * @return The Sones that like the given reply
693 public Set<Sone> getLikes(Reply reply) {
694 Set<Sone> sones = new HashSet<Sone>();
695 for (Sone sone : getSones()) {
696 if (sone.getLikedReplyIds().contains(reply.getId())) {
704 * Returns whether the given post is bookmarked.
708 * @return {@code true} if the given post is bookmarked, {@code false}
711 public boolean isBookmarked(Post post) {
712 return isPostBookmarked(post.getId());
716 * Returns whether the post with the given ID is bookmarked.
719 * The ID of the post to check
720 * @return {@code true} if the post with the given ID is bookmarked,
721 * {@code false} otherwise
723 public boolean isPostBookmarked(String id) {
724 synchronized (bookmarkedPosts) {
725 return bookmarkedPosts.contains(id);
730 * Returns all currently known bookmarked posts.
732 * @return All bookmarked posts
734 public Set<Post> getBookmarkedPosts() {
735 Set<Post> posts = new HashSet<Post>();
736 synchronized (bookmarkedPosts) {
737 for (String bookmarkedPostId : bookmarkedPosts) {
738 Post post = getPost(bookmarkedPostId, false);
752 * Locks the given Sone. A locked Sone will not be inserted by
753 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
759 public void lockSone(Sone sone) {
760 synchronized (lockedSones) {
761 if (lockedSones.add(sone)) {
762 coreListenerManager.fireSoneLocked(sone);
768 * Unlocks the given Sone.
770 * @see #lockSone(Sone)
774 public void unlockSone(Sone sone) {
775 synchronized (lockedSones) {
776 if (lockedSones.remove(sone)) {
777 coreListenerManager.fireSoneUnlocked(sone);
783 * Adds a local Sone from the given ID which has to be the ID of an own
787 * The ID of an own identity to add a Sone for
788 * @return The added (or already existing) Sone
790 public Sone addLocalSone(String id) {
791 synchronized (localSones) {
792 if (localSones.containsKey(id)) {
793 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
794 return localSones.get(id);
796 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
797 if (ownIdentity == null) {
798 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
801 return addLocalSone(ownIdentity);
806 * Adds a local Sone from the given own identity.
809 * The own identity to create a Sone from
810 * @return The added (or already existing) Sone
812 public Sone addLocalSone(OwnIdentity ownIdentity) {
813 if (ownIdentity == null) {
814 logger.log(Level.WARNING, "Given OwnIdentity is null!");
817 synchronized (localSones) {
820 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
821 } catch (MalformedURLException mue1) {
822 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
825 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
826 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
827 /* TODO - load posts ’n stuff */
828 localSones.put(ownIdentity.getId(), sone);
829 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
830 soneInserters.put(sone, soneInserter);
831 setSoneStatus(sone, SoneStatus.idle);
833 if (!preferences.isSoneRescueMode()) {
834 soneInserter.start();
836 new Thread(new Runnable() {
839 @SuppressWarnings("synthetic-access")
841 if (!preferences.isSoneRescueMode()) {
844 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
845 coreListenerManager.fireRescuingSone(sone);
847 long edition = sone.getLatestEdition();
848 while (!stopped && (edition >= 0) && preferences.isSoneRescueMode()) {
849 logger.log(Level.FINE, "Downloading edition " + edition + "…");
850 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
853 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
855 coreListenerManager.fireRescuedSone(sone);
856 soneInserter.start();
859 }, "Sone Downloader").start();
865 * Creates a new Sone for the given own identity.
868 * The own identity to create a Sone for
869 * @return The created Sone
871 public Sone createSone(OwnIdentity ownIdentity) {
873 ownIdentity.addContext("Sone");
874 } catch (WebOfTrustException wote1) {
875 logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
878 Sone sone = addLocalSone(ownIdentity);
883 * Adds the Sone of the given identity.
886 * The identity whose Sone to add
887 * @return The added or already existing Sone
889 public Sone addRemoteSone(Identity identity) {
890 if (identity == null) {
891 logger.log(Level.WARNING, "Given Identity is null!");
894 synchronized (remoteSones) {
895 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
896 boolean newSone = sone.getRequestUri() == null;
897 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
898 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
900 synchronized (newSones) {
901 newSone = !knownSones.contains(sone.getId());
903 newSones.add(sone.getId());
907 coreListenerManager.fireNewSoneFound(sone);
910 remoteSones.put(identity.getId(), sone);
911 soneDownloader.addSone(sone);
912 setSoneStatus(sone, SoneStatus.unknown);
913 new Thread(new Runnable() {
916 @SuppressWarnings("synthetic-access")
918 soneDownloader.fetchSone(sone);
921 }, "Sone Downloader").start();
927 * Retrieves the trust relationship from the origin to the target. If the
928 * trust relationship can not be retrieved, {@code null} is returned.
930 * @see Identity#getTrust(OwnIdentity)
932 * The origin of the trust tree
934 * The target of the trust
935 * @return The trust relationship
937 public Trust getTrust(Sone origin, Sone target) {
938 if (!isLocalSone(origin)) {
939 logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
942 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
946 * Sets the trust value of the given origin Sone for the target Sone.
953 * The trust value (from {@code -100} to {@code 100})
955 public void setTrust(Sone origin, Sone target, int trustValue) {
956 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();
958 ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
959 } catch (WebOfTrustException wote1) {
960 logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
965 * Removes any trust assignment for the given target Sone.
972 public void removeTrust(Sone origin, Sone target) {
973 Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
975 ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
976 } catch (WebOfTrustException wote1) {
977 logger.log(Level.WARNING, "Could not remove trust for Sone: " + target, wote1);
982 * Assigns the configured positive trust value for the given target.
989 public void trustSone(Sone origin, Sone target) {
990 setTrust(origin, target, preferences.getPositiveTrust());
994 * Assigns the configured negative trust value for the given target.
1001 public void distrustSone(Sone origin, Sone target) {
1002 setTrust(origin, target, preferences.getNegativeTrust());
1006 * Removes the trust assignment for the given target.
1013 public void untrustSone(Sone origin, Sone target) {
1014 removeTrust(origin, target);
1018 * Updates the stores Sone with the given Sone.
1023 public void updateSone(Sone sone) {
1024 if (hasSone(sone.getId())) {
1025 boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode();
1026 Sone storedSone = getSone(sone.getId());
1027 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
1028 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
1031 synchronized (posts) {
1032 if (!soneRescueMode) {
1033 for (Post post : storedSone.getPosts()) {
1034 posts.remove(post.getId());
1035 if (!sone.getPosts().contains(post)) {
1036 coreListenerManager.firePostRemoved(post);
1040 List<Post> storedPosts = storedSone.getPosts();
1041 synchronized (newPosts) {
1042 for (Post post : sone.getPosts()) {
1043 post.setSone(storedSone);
1044 if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
1045 newPosts.add(post.getId());
1046 coreListenerManager.fireNewPostFound(post);
1048 posts.put(post.getId(), post);
1052 synchronized (replies) {
1053 if (!soneRescueMode) {
1054 for (Reply reply : storedSone.getReplies()) {
1055 replies.remove(reply.getId());
1056 if (!sone.getReplies().contains(reply)) {
1057 coreListenerManager.fireReplyRemoved(reply);
1061 Set<Reply> storedReplies = storedSone.getReplies();
1062 synchronized (newReplies) {
1063 for (Reply reply : sone.getReplies()) {
1064 reply.setSone(storedSone);
1065 if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
1066 newReplies.add(reply.getId());
1067 coreListenerManager.fireNewReplyFound(reply);
1069 replies.put(reply.getId(), reply);
1073 synchronized (storedSone) {
1074 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
1075 storedSone.setTime(sone.getTime());
1077 storedSone.setClient(sone.getClient());
1078 storedSone.setProfile(sone.getProfile());
1079 if (soneRescueMode) {
1080 for (Post post : sone.getPosts()) {
1081 storedSone.addPost(post);
1083 for (Reply reply : sone.getReplies()) {
1084 storedSone.addReply(reply);
1086 for (String likedPostId : sone.getLikedPostIds()) {
1087 storedSone.addLikedPostId(likedPostId);
1089 for (String likedReplyId : sone.getLikedReplyIds()) {
1090 storedSone.addLikedReplyId(likedReplyId);
1093 storedSone.setPosts(sone.getPosts());
1094 storedSone.setReplies(sone.getReplies());
1095 storedSone.setLikePostIds(sone.getLikedPostIds());
1096 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
1098 storedSone.setLatestEdition(sone.getLatestEdition());
1104 * Deletes the given Sone. This will remove the Sone from the
1105 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1106 * and remove the context from its identity.
1109 * The Sone to delete
1111 public void deleteSone(Sone sone) {
1112 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1113 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1116 synchronized (localSones) {
1117 if (!localSones.containsKey(sone.getId())) {
1118 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1121 localSones.remove(sone.getId());
1122 soneInserters.remove(sone).stop();
1125 ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1126 ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1127 } catch (WebOfTrustException wote1) {
1128 logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1131 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1132 } catch (ConfigurationException ce1) {
1133 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1138 * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
1139 * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
1142 * The Sone to mark as known
1144 public void markSoneKnown(Sone sone) {
1145 synchronized (newSones) {
1146 if (newSones.remove(sone.getId())) {
1147 knownSones.add(sone.getId());
1148 coreListenerManager.fireMarkSoneKnown(sone);
1149 saveConfiguration();
1155 * Loads and updates the given Sone from the configuration. If any error is
1156 * encountered, loading is aborted and the given Sone is not changed.
1159 * The Sone to load and update
1161 public void loadSone(Sone sone) {
1162 if (!isLocalSone(sone)) {
1163 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1168 String sonePrefix = "Sone/" + sone.getId();
1169 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1170 if (soneTime == null) {
1171 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1174 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1177 Profile profile = new Profile();
1178 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1179 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1180 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1181 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1182 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1183 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1185 /* load profile fields. */
1187 String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
1188 String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
1189 if (fieldName == null) {
1192 String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
1193 profile.addField(fieldName).setValue(fieldValue);
1197 Set<Post> posts = new HashSet<Post>();
1199 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1200 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1201 if (postId == null) {
1204 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1205 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1206 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1207 if ((postTime == 0) || (postText == null)) {
1208 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1211 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1212 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1213 post.setRecipient(getSone(postRecipientId));
1219 Set<Reply> replies = new HashSet<Reply>();
1221 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1222 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1223 if (replyId == null) {
1226 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1227 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1228 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1229 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1230 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1233 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1236 /* load post likes. */
1237 Set<String> likedPostIds = new HashSet<String>();
1239 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1240 if (likedPostId == null) {
1243 likedPostIds.add(likedPostId);
1246 /* load reply likes. */
1247 Set<String> likedReplyIds = new HashSet<String>();
1249 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1250 if (likedReplyId == null) {
1253 likedReplyIds.add(likedReplyId);
1257 Set<String> friends = new HashSet<String>();
1259 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1260 if (friendId == null) {
1263 friends.add(friendId);
1266 /* if we’re still here, Sone was loaded successfully. */
1267 synchronized (sone) {
1268 sone.setTime(soneTime);
1269 sone.setProfile(profile);
1270 sone.setPosts(posts);
1271 sone.setReplies(replies);
1272 sone.setLikePostIds(likedPostIds);
1273 sone.setLikeReplyIds(likedReplyIds);
1274 sone.setFriends(friends);
1275 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1277 synchronized (newSones) {
1278 for (String friend : friends) {
1279 knownSones.add(friend);
1282 synchronized (newPosts) {
1283 for (Post post : posts) {
1284 knownPosts.add(post.getId());
1287 synchronized (newReplies) {
1288 for (Reply reply : replies) {
1289 knownReplies.add(reply.getId());
1295 * Saves the given Sone. This will persist all local settings for the given
1296 * Sone, such as the friends list and similar, private options.
1301 public synchronized void saveSone(Sone sone) {
1302 if (!isLocalSone(sone)) {
1303 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1306 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1307 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1311 logger.log(Level.INFO, "Saving Sone: %s", sone);
1313 ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1315 /* save Sone into configuration. */
1316 String sonePrefix = "Sone/" + sone.getId();
1317 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1318 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1321 Profile profile = sone.getProfile();
1322 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1323 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1324 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1325 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1326 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1327 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1329 /* save profile fields. */
1330 int fieldCounter = 0;
1331 for (Field profileField : profile.getFields()) {
1332 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1333 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1334 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1336 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1339 int postCounter = 0;
1340 for (Post post : sone.getPosts()) {
1341 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1342 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1343 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1344 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1345 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1347 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1350 int replyCounter = 0;
1351 for (Reply reply : sone.getReplies()) {
1352 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1353 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1354 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1355 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1356 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1358 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1360 /* save post likes. */
1361 int postLikeCounter = 0;
1362 for (String postId : sone.getLikedPostIds()) {
1363 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1365 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1367 /* save reply likes. */
1368 int replyLikeCounter = 0;
1369 for (String replyId : sone.getLikedReplyIds()) {
1370 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1372 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1375 int friendCounter = 0;
1376 for (String friendId : sone.getFriends()) {
1377 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1379 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1381 configuration.save();
1382 logger.log(Level.INFO, "Sone %s saved.", sone);
1383 } catch (ConfigurationException ce1) {
1384 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1385 } catch (WebOfTrustException wote1) {
1386 logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1391 * Creates a new post.
1394 * The Sone that creates the post
1396 * The text of the post
1397 * @return The created post
1399 public Post createPost(Sone sone, String text) {
1400 return createPost(sone, System.currentTimeMillis(), text);
1404 * Creates a new post.
1407 * The Sone that creates the post
1409 * The time of the post
1411 * The text of the post
1412 * @return The created post
1414 public Post createPost(Sone sone, long time, String text) {
1415 return createPost(sone, null, time, text);
1419 * Creates a new post.
1422 * The Sone that creates the post
1424 * The recipient Sone, or {@code null} if this post does not have
1427 * The text of the post
1428 * @return The created post
1430 public Post createPost(Sone sone, Sone recipient, String text) {
1431 return createPost(sone, recipient, System.currentTimeMillis(), text);
1435 * Creates a new post.
1438 * The Sone that creates the post
1440 * The recipient Sone, or {@code null} if this post does not have
1443 * The time of the post
1445 * The text of the post
1446 * @return The created post
1448 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1449 if (!isLocalSone(sone)) {
1450 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1453 Post post = new Post(sone, time, text);
1454 if (recipient != null) {
1455 post.setRecipient(recipient);
1457 synchronized (posts) {
1458 posts.put(post.getId(), post);
1460 synchronized (newPosts) {
1461 knownPosts.add(post.getId());
1469 * Deletes the given post.
1472 * The post to delete
1474 public void deletePost(Post post) {
1475 if (!isLocalSone(post.getSone())) {
1476 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1479 post.getSone().removePost(post);
1480 synchronized (posts) {
1481 posts.remove(post.getId());
1483 saveSone(post.getSone());
1487 * Marks the given post as known, if it is currently a new post (according
1488 * to {@link #isNewPost(String)}).
1491 * The post to mark as known
1493 public void markPostKnown(Post post) {
1494 synchronized (newPosts) {
1495 if (newPosts.remove(post.getId())) {
1496 knownPosts.add(post.getId());
1497 coreListenerManager.fireMarkPostKnown(post);
1498 saveConfiguration();
1504 * Bookmarks the given post.
1507 * The post to bookmark
1509 public void bookmark(Post post) {
1510 bookmarkPost(post.getId());
1514 * Bookmarks the post with the given ID.
1517 * The ID of the post to bookmark
1519 public void bookmarkPost(String id) {
1520 synchronized (bookmarkedPosts) {
1521 bookmarkedPosts.add(id);
1526 * Removes the given post from the bookmarks.
1529 * The post to unbookmark
1531 public void unbookmark(Post post) {
1532 unbookmarkPost(post.getId());
1536 * Removes the post with the given ID from the bookmarks.
1539 * The ID of the post to unbookmark
1541 public void unbookmarkPost(String id) {
1542 synchronized (bookmarkedPosts) {
1543 bookmarkedPosts.remove(id);
1548 * Creates a new reply.
1551 * The Sone that creates the reply
1553 * The post that this reply refers to
1555 * The text of the reply
1556 * @return The created reply
1558 public Reply createReply(Sone sone, Post post, String text) {
1559 return createReply(sone, post, System.currentTimeMillis(), text);
1563 * Creates a new reply.
1566 * The Sone that creates the reply
1568 * The post that this reply refers to
1570 * The time of the reply
1572 * The text of the reply
1573 * @return The created reply
1575 public Reply createReply(Sone sone, Post post, long time, String text) {
1576 if (!isLocalSone(sone)) {
1577 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1580 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1581 synchronized (replies) {
1582 replies.put(reply.getId(), reply);
1584 synchronized (newReplies) {
1585 knownReplies.add(reply.getId());
1587 sone.addReply(reply);
1593 * Deletes the given reply.
1596 * The reply to delete
1598 public void deleteReply(Reply reply) {
1599 Sone sone = reply.getSone();
1600 if (!isLocalSone(sone)) {
1601 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1604 synchronized (replies) {
1605 replies.remove(reply.getId());
1607 sone.removeReply(reply);
1612 * Marks the given reply as known, if it is currently a new reply (according
1613 * to {@link #isNewReply(String)}).
1616 * The reply to mark as known
1618 public void markReplyKnown(Reply reply) {
1619 synchronized (newReplies) {
1620 if (newReplies.remove(reply.getId())) {
1621 knownReplies.add(reply.getId());
1622 coreListenerManager.fireMarkReplyKnown(reply);
1623 saveConfiguration();
1631 public void start() {
1632 loadConfiguration();
1633 updateChecker.addUpdateListener(this);
1634 updateChecker.start();
1640 public void stop() {
1641 synchronized (localSones) {
1642 for (SoneInserter soneInserter : soneInserters.values()) {
1643 soneInserter.stop();
1646 updateChecker.stop();
1647 updateChecker.removeUpdateListener(this);
1648 soneDownloader.stop();
1649 saveConfiguration();
1654 * Saves the current options.
1656 public void saveConfiguration() {
1657 synchronized (configuration) {
1658 if (storingConfiguration) {
1659 logger.log(Level.FINE, "Already storing configuration…");
1662 storingConfiguration = true;
1665 /* store the options first. */
1667 configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
1668 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1669 configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
1670 configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
1671 configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
1672 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1673 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1674 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1676 /* save known Sones. */
1677 int soneCounter = 0;
1678 synchronized (newSones) {
1679 for (String knownSoneId : knownSones) {
1680 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1682 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1685 /* save known posts. */
1686 int postCounter = 0;
1687 synchronized (newPosts) {
1688 for (String knownPostId : knownPosts) {
1689 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1691 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1694 /* save known replies. */
1695 int replyCounter = 0;
1696 synchronized (newReplies) {
1697 for (String knownReplyId : knownReplies) {
1698 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1700 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1703 /* save bookmarked posts. */
1704 int bookmarkedPostCounter = 0;
1705 synchronized (bookmarkedPosts) {
1706 for (String bookmarkedPostId : bookmarkedPosts) {
1707 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
1710 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
1713 configuration.save();
1715 } catch (ConfigurationException ce1) {
1716 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1718 synchronized (configuration) {
1719 storingConfiguration = false;
1729 * Loads the configuration.
1731 @SuppressWarnings("unchecked")
1732 private void loadConfiguration() {
1733 /* create options. */
1734 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1737 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1738 SoneInserter.setInsertionDelay(newValue);
1742 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75));
1743 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25));
1744 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
1745 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1746 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1747 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1749 /* read options from configuration. */
1750 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1751 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1752 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1753 options.getBooleanOption("ClearOnNextRestart").set(null);
1754 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1755 if (clearConfiguration) {
1756 /* stop loading the configuration. */
1760 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1761 options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
1762 options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
1763 options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
1764 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1766 /* load known Sones. */
1767 int soneCounter = 0;
1769 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1770 if (knownSoneId == null) {
1773 synchronized (newSones) {
1774 knownSones.add(knownSoneId);
1778 /* load known posts. */
1779 int postCounter = 0;
1781 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1782 if (knownPostId == null) {
1785 synchronized (newPosts) {
1786 knownPosts.add(knownPostId);
1790 /* load known replies. */
1791 int replyCounter = 0;
1793 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1794 if (knownReplyId == null) {
1797 synchronized (newReplies) {
1798 knownReplies.add(knownReplyId);
1802 /* load bookmarked posts. */
1803 int bookmarkedPostCounter = 0;
1805 String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null);
1806 if (bookmarkedPostId == null) {
1809 synchronized (bookmarkedPosts) {
1810 bookmarkedPosts.add(bookmarkedPostId);
1817 * Generate a Sone URI from the given URI and latest edition.
1820 * The URI to derive the Sone URI from
1821 * @return The derived URI
1823 private FreenetURI getSoneUri(String uriString) {
1825 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1827 } catch (MalformedURLException mue1) {
1828 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1834 // INTERFACE IdentityListener
1841 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1842 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1843 if (ownIdentity.hasContext("Sone")) {
1844 trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1845 addLocalSone(ownIdentity);
1853 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1854 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1855 trustedIdentities.remove(ownIdentity);
1862 public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1863 logger.log(Level.FINEST, "Adding Identity: " + identity);
1864 trustedIdentities.get(ownIdentity).add(identity);
1865 addRemoteSone(identity);
1872 public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1873 new Thread(new Runnable() {
1876 @SuppressWarnings("synthetic-access")
1878 Sone sone = getRemoteSone(identity.getId());
1879 sone.setIdentity(identity);
1880 soneDownloader.addSone(sone);
1881 soneDownloader.fetchSone(sone);
1890 public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1891 trustedIdentities.get(ownIdentity).remove(identity);
1895 // INTERFACE UpdateListener
1902 public void updateFound(Version version, long releaseTime, long latestEdition) {
1903 coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition);
1907 * Convenience interface for external classes that want to access the core’s
1910 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1912 public static class Preferences {
1914 /** The wrapped options. */
1915 private final Options options;
1918 * Creates a new preferences object wrapped around the given options.
1921 * The options to wrap
1923 public Preferences(Options options) {
1924 this.options = options;
1928 * Returns the insertion delay.
1930 * @return The insertion delay
1932 public int getInsertionDelay() {
1933 return options.getIntegerOption("InsertionDelay").get();
1937 * Sets the insertion delay
1939 * @param insertionDelay
1940 * The new insertion delay, or {@code null} to restore it to
1942 * @return This preferences
1944 public Preferences setInsertionDelay(Integer insertionDelay) {
1945 options.getIntegerOption("InsertionDelay").set(insertionDelay);
1950 * Returns the positive trust.
1952 * @return The positive trust
1954 public int getPositiveTrust() {
1955 return options.getIntegerOption("PositiveTrust").get();
1959 * Sets the positive trust.
1961 * @param positiveTrust
1962 * The new positive trust, or {@code null} to restore it to
1964 * @return This preferences
1966 public Preferences setPositiveTrust(Integer positiveTrust) {
1967 options.getIntegerOption("PositiveTrust").set(positiveTrust);
1972 * Returns the negative trust.
1974 * @return The negative trust
1976 public int getNegativeTrust() {
1977 return options.getIntegerOption("NegativeTrust").get();
1981 * Sets the negative trust.
1983 * @param negativeTrust
1984 * The negative trust, or {@code null} to restore it to the
1986 * @return The preferences
1988 public Preferences setNegativeTrust(Integer negativeTrust) {
1989 options.getIntegerOption("NegativeTrust").set(negativeTrust);
1994 * Returns the trust comment. This is the comment that is set in the web
1995 * of trust when a trust value is assigned to an identity.
1997 * @return The trust comment
1999 public String getTrustComment() {
2000 return options.getStringOption("TrustComment").get();
2004 * Sets the trust comment.
2006 * @param trustComment
2007 * The trust comment, or {@code null} to restore it to the
2009 * @return This preferences
2011 public Preferences setTrustComment(String trustComment) {
2012 options.getStringOption("TrustComment").set(trustComment);
2017 * Returns whether the rescue mode is active.
2019 * @return {@code true} if the rescue mode is active, {@code false}
2022 public boolean isSoneRescueMode() {
2023 return options.getBooleanOption("SoneRescueMode").get();
2027 * Sets whether the rescue mode is active.
2029 * @param soneRescueMode
2030 * {@code true} if the rescue mode is active, {@code false}
2032 * @return This preferences
2034 public Preferences setSoneRescueMode(Boolean soneRescueMode) {
2035 options.getBooleanOption("SoneRescueMode").set(soneRescueMode);
2040 * Returns whether Sone should clear its settings on the next restart.
2041 * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
2042 * to return {@code true} as well!
2044 * @return {@code true} if Sone should clear its settings on the next
2045 * restart, {@code false} otherwise
2047 public boolean isClearOnNextRestart() {
2048 return options.getBooleanOption("ClearOnNextRestart").get();
2052 * Sets whether Sone will clear its settings on the next restart.
2054 * @param clearOnNextRestart
2055 * {@code true} if Sone should clear its settings on the next
2056 * restart, {@code false} otherwise
2057 * @return This preferences
2059 public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
2060 options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
2065 * Returns whether Sone should really clear its settings on next
2066 * restart. This is a confirmation option that needs to be set in
2067 * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
2068 * settings on the next restart.
2070 * @return {@code true} if Sone should really clear its settings on the
2071 * next restart, {@code false} otherwise
2073 public boolean isReallyClearOnNextRestart() {
2074 return options.getBooleanOption("ReallyClearOnNextRestart").get();
2078 * Sets whether Sone should really clear its settings on the next
2081 * @param reallyClearOnNextRestart
2082 * {@code true} if Sone should really clear its settings on
2083 * the next restart, {@code false} otherwise
2084 * @return This preferences
2086 public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
2087 options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);