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.fcp.FcpInterface;
41 import net.pterodactylus.sone.freenet.wot.Identity;
42 import net.pterodactylus.sone.freenet.wot.IdentityListener;
43 import net.pterodactylus.sone.freenet.wot.IdentityManager;
44 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
45 import net.pterodactylus.sone.freenet.wot.Trust;
46 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
47 import net.pterodactylus.sone.main.SonePlugin;
48 import net.pterodactylus.util.config.Configuration;
49 import net.pterodactylus.util.config.ConfigurationException;
50 import net.pterodactylus.util.logging.Logging;
51 import net.pterodactylus.util.number.Numbers;
52 import net.pterodactylus.util.validation.Validation;
53 import net.pterodactylus.util.version.Version;
54 import freenet.keys.FreenetURI;
59 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61 public class Core implements IdentityListener, UpdateListener {
64 * Enumeration for the possible states of a {@link Sone}.
66 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
68 public enum SoneStatus {
70 /** The Sone is unknown, i.e. not yet downloaded. */
73 /** The Sone is idle, i.e. not being downloaded or inserted. */
76 /** The Sone is currently being inserted. */
79 /** The Sone is currently being downloaded. */
84 private static final Logger logger = Logging.getLogger(Core.class);
87 private final Options options = new Options();
89 /** The preferences. */
90 private final Preferences preferences = new Preferences(options);
92 /** The core listener manager. */
93 private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
95 /** The configuration. */
96 private Configuration configuration;
98 /** Whether we’re currently saving the configuration. */
99 private boolean storingConfiguration = false;
101 /** The identity manager. */
102 private final IdentityManager identityManager;
104 /** Interface to freenet. */
105 private final FreenetInterface freenetInterface;
107 /** The Sone downloader. */
108 private final SoneDownloader soneDownloader;
110 /** The update checker. */
111 private final UpdateChecker updateChecker;
113 /** The FCP interface. */
114 private volatile FcpInterface fcpInterface;
116 /** Whether the core has been stopped. */
117 private volatile boolean stopped;
119 /** The Sones’ statuses. */
120 /* synchronize access on itself. */
121 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
123 /** Locked local Sones. */
124 /* synchronize on itself. */
125 private final Set<Sone> lockedSones = new HashSet<Sone>();
127 /** Sone inserters. */
128 /* synchronize access on this on localSones. */
129 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
131 /** All local Sones. */
132 /* synchronize access on this on itself. */
133 private Map<String, Sone> localSones = new HashMap<String, Sone>();
135 /** All remote Sones. */
136 /* synchronize access on this on itself. */
137 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
139 /** All new Sones. */
140 private Set<String> newSones = new HashSet<String>();
142 /** All known Sones. */
143 /* synchronize access on {@link #newSones}. */
144 private Set<String> knownSones = new HashSet<String>();
147 private Map<String, Post> posts = new HashMap<String, Post>();
149 /** All new posts. */
150 private Set<String> newPosts = new HashSet<String>();
152 /** All known posts. */
153 /* synchronize access on {@link #newPosts}. */
154 private Set<String> knownPosts = new HashSet<String>();
157 private Map<String, Reply> replies = new HashMap<String, Reply>();
159 /** All new replies. */
160 private Set<String> newReplies = new HashSet<String>();
162 /** All known replies. */
163 private Set<String> knownReplies = new HashSet<String>();
165 /** All bookmarked posts. */
166 /* synchronize access on itself. */
167 private Set<String> bookmarkedPosts = new HashSet<String>();
169 /** Trusted identities, sorted by own identities. */
170 private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
173 * Creates a new core.
175 * @param configuration
176 * The configuration of the core
177 * @param freenetInterface
178 * The freenet interface
179 * @param identityManager
180 * The identity manager
182 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
183 this.configuration = configuration;
184 this.freenetInterface = freenetInterface;
185 this.identityManager = identityManager;
186 this.soneDownloader = new SoneDownloader(this, freenetInterface);
187 this.updateChecker = new UpdateChecker(freenetInterface);
191 // LISTENER MANAGEMENT
195 * Adds a new core listener.
197 * @param coreListener
198 * The listener to add
200 public void addCoreListener(CoreListener coreListener) {
201 coreListenerManager.addListener(coreListener);
205 * Removes a core listener.
207 * @param coreListener
208 * The listener to remove
210 public void removeCoreListener(CoreListener coreListener) {
211 coreListenerManager.removeListener(coreListener);
219 * Sets the configuration to use. This will automatically save the current
220 * configuration to the given configuration.
222 * @param configuration
223 * The new configuration to use
225 public void setConfiguration(Configuration configuration) {
226 this.configuration = configuration;
231 * Returns the options used by the core.
233 * @return The options of the core
235 public Preferences getPreferences() {
240 * Returns the identity manager used by the core.
242 * @return The identity manager
244 public IdentityManager getIdentityManager() {
245 return identityManager;
249 * Returns the update checker.
251 * @return The update checker
253 public UpdateChecker getUpdateChecker() {
254 return updateChecker;
258 * Sets the FCP interface to use.
260 * @param fcpInterface
261 * The FCP interface to use
263 public void setFcpInterface(FcpInterface fcpInterface) {
264 this.fcpInterface = fcpInterface;
268 * Returns the status of the given Sone.
271 * The Sone to get the status for
272 * @return The status of the Sone
274 public SoneStatus getSoneStatus(Sone sone) {
275 synchronized (soneStatuses) {
276 return soneStatuses.get(sone);
281 * Sets the status of the given Sone.
284 * The Sone to set the status of
288 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
289 synchronized (soneStatuses) {
290 soneStatuses.put(sone, soneStatus);
295 * Returns whether the given Sone is currently locked.
299 * @return {@code true} if the Sone is locked, {@code false} if it is not
301 public boolean isLocked(Sone sone) {
302 synchronized (lockedSones) {
303 return lockedSones.contains(sone);
308 * Returns all Sones, remote and local.
312 public Set<Sone> getSones() {
313 Set<Sone> allSones = new HashSet<Sone>();
314 allSones.addAll(getLocalSones());
315 allSones.addAll(getRemoteSones());
320 * Returns the Sone with the given ID, regardless whether it’s local or
324 * The ID of the Sone to get
325 * @return The Sone with the given ID, or {@code null} if there is no such
328 public Sone getSone(String id) {
329 return getSone(id, true);
333 * Returns the Sone with the given ID, regardless whether it’s local or
337 * The ID of the Sone to get
339 * {@code true} to create a new Sone if none exists,
340 * {@code false} to return {@code null} if a Sone with the given
342 * @return The Sone with the given ID, or {@code null} if there is no such
345 public Sone getSone(String id, boolean create) {
346 if (isLocalSone(id)) {
347 return getLocalSone(id);
349 return getRemoteSone(id, create);
353 * Checks whether the core knows a Sone with the given ID.
357 * @return {@code true} if there is a Sone with the given ID, {@code false}
360 public boolean hasSone(String id) {
361 return isLocalSone(id) || isRemoteSone(id);
365 * Returns whether the given Sone is a local Sone.
368 * The Sone to check for its locality
369 * @return {@code true} if the given Sone is local, {@code false} otherwise
371 public boolean isLocalSone(Sone sone) {
372 synchronized (localSones) {
373 return localSones.containsKey(sone.getId());
378 * Returns whether the given ID is the ID of a local Sone.
381 * The Sone ID to check for its locality
382 * @return {@code true} if the given ID is a local Sone, {@code false}
385 public boolean isLocalSone(String id) {
386 synchronized (localSones) {
387 return localSones.containsKey(id);
392 * Returns all local Sones.
394 * @return All local Sones
396 public Set<Sone> getLocalSones() {
397 synchronized (localSones) {
398 return new HashSet<Sone>(localSones.values());
403 * Returns the local Sone with the given ID.
406 * The ID of the Sone to get
407 * @return The Sone with the given ID
409 public Sone getLocalSone(String id) {
410 return getLocalSone(id, true);
414 * Returns the local Sone with the given ID, optionally creating a new Sone.
419 * {@code true} to create a new Sone if none exists,
420 * {@code false} to return null if none exists
421 * @return The Sone with the given ID, or {@code null}
423 public Sone getLocalSone(String id, boolean create) {
424 synchronized (localSones) {
425 Sone sone = localSones.get(id);
426 if ((sone == null) && create) {
428 localSones.put(id, sone);
429 setSoneStatus(sone, SoneStatus.unknown);
436 * Returns all remote Sones.
438 * @return All remote Sones
440 public Set<Sone> getRemoteSones() {
441 synchronized (remoteSones) {
442 return new HashSet<Sone>(remoteSones.values());
447 * Returns the remote Sone with the given ID.
450 * The ID of the remote Sone to get
451 * @return The Sone with the given ID
453 public Sone getRemoteSone(String id) {
454 return getRemoteSone(id, true);
458 * Returns the remote Sone with the given ID.
461 * The ID of the remote Sone to get
463 * {@code true} to always create a Sone, {@code false} to return
464 * {@code null} if no Sone with the given ID exists
465 * @return The Sone with the given ID
467 public Sone getRemoteSone(String id, boolean create) {
468 synchronized (remoteSones) {
469 Sone sone = remoteSones.get(id);
470 if ((sone == null) && create) {
472 remoteSones.put(id, sone);
473 setSoneStatus(sone, SoneStatus.unknown);
480 * Returns whether the given Sone is a remote Sone.
484 * @return {@code true} if the given Sone is a remote Sone, {@code false}
487 public boolean isRemoteSone(Sone sone) {
488 synchronized (remoteSones) {
489 return remoteSones.containsKey(sone.getId());
494 * Returns whether the Sone with the given ID is a remote Sone.
497 * The ID of the Sone to check
498 * @return {@code true} if the Sone with the given ID is a remote Sone,
499 * {@code false} otherwise
501 public boolean isRemoteSone(String id) {
502 synchronized (remoteSones) {
503 return remoteSones.containsKey(id);
508 * Returns whether the Sone with the given ID is a new Sone.
511 * The ID of the sone to check for
512 * @return {@code true} if the given Sone is new, false otherwise
514 public boolean isNewSone(String soneId) {
515 synchronized (newSones) {
516 return !knownSones.contains(soneId) && newSones.contains(soneId);
521 * Returns whether the given Sone has been modified.
524 * The Sone to check for modifications
525 * @return {@code true} if a modification has been detected in the Sone,
526 * {@code false} otherwise
528 public boolean isModifiedSone(Sone sone) {
529 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
533 * Returns whether the target Sone is trusted by the origin Sone.
539 * @return {@code true} if the target Sone is trusted by the origin Sone
541 public boolean isSoneTrusted(Sone origin, Sone target) {
542 Validation.begin().isNotNull("Origin", origin).isNotNull("Target", target).check().isInstanceOf("Origin’s OwnIdentity", origin.getIdentity(), OwnIdentity.class).check();
543 return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity());
547 * Returns the post with the given ID.
550 * The ID of the post to get
551 * @return The post with the given ID, or a new post with the given ID
553 public Post getPost(String postId) {
554 return getPost(postId, true);
558 * Returns the post with the given ID, optionally creating a new post.
561 * The ID of the post to get
563 * {@code true} it create a new post if no post with the given ID
564 * exists, {@code false} to return {@code null}
565 * @return The post, or {@code null} if there is no such post
567 public Post getPost(String postId, boolean create) {
568 synchronized (posts) {
569 Post post = posts.get(postId);
570 if ((post == null) && create) {
571 post = new Post(postId);
572 posts.put(postId, post);
579 * Returns whether the given post ID is new.
583 * @return {@code true} if the post is considered to be new, {@code false}
586 public boolean isNewPost(String postId) {
587 synchronized (newPosts) {
588 return !knownPosts.contains(postId) && newPosts.contains(postId);
593 * Returns all posts that have the given Sone as recipient.
595 * @see Post#getRecipient()
597 * The recipient of the posts
598 * @return All posts that have the given Sone as recipient
600 public Set<Post> getDirectedPosts(Sone recipient) {
601 Validation.begin().isNotNull("Recipient", recipient).check();
602 Set<Post> directedPosts = new HashSet<Post>();
603 synchronized (posts) {
604 for (Post post : posts.values()) {
605 if (recipient.equals(post.getRecipient())) {
606 directedPosts.add(post);
610 return directedPosts;
614 * Returns the reply with the given ID. If there is no reply with the given
615 * ID yet, a new one is created.
618 * The ID of the reply to get
621 public Reply getReply(String replyId) {
622 return getReply(replyId, true);
626 * Returns the reply with the given ID. If there is no reply with the given
627 * ID yet, a new one is created, unless {@code create} is false in which
628 * case {@code null} is returned.
631 * The ID of the reply to get
633 * {@code true} to always return a {@link Reply}, {@code false}
634 * to return {@code null} if no reply can be found
635 * @return The reply, or {@code null} if there is no such reply
637 public Reply getReply(String replyId, boolean create) {
638 synchronized (replies) {
639 Reply reply = replies.get(replyId);
640 if (create && (reply == null)) {
641 reply = new Reply(replyId);
642 replies.put(replyId, reply);
649 * Returns all replies for the given post, order ascending by time.
652 * The post to get all replies for
653 * @return All replies for the given post
655 public List<Reply> getReplies(Post post) {
656 Set<Sone> sones = getSones();
657 List<Reply> replies = new ArrayList<Reply>();
658 for (Sone sone : sones) {
659 for (Reply reply : sone.getReplies()) {
660 if (reply.getPost().equals(post)) {
665 Collections.sort(replies, Reply.TIME_COMPARATOR);
670 * Returns whether the reply with the given ID is new.
673 * The ID of the reply to check
674 * @return {@code true} if the reply is considered to be new, {@code false}
677 public boolean isNewReply(String replyId) {
678 synchronized (newReplies) {
679 return !knownReplies.contains(replyId) && newReplies.contains(replyId);
684 * Returns all Sones that have liked the given post.
687 * The post to get the liking Sones for
688 * @return The Sones that like the given post
690 public Set<Sone> getLikes(Post post) {
691 Set<Sone> sones = new HashSet<Sone>();
692 for (Sone sone : getSones()) {
693 if (sone.getLikedPostIds().contains(post.getId())) {
701 * Returns all Sones that have liked the given reply.
704 * The reply to get the liking Sones for
705 * @return The Sones that like the given reply
707 public Set<Sone> getLikes(Reply reply) {
708 Set<Sone> sones = new HashSet<Sone>();
709 for (Sone sone : getSones()) {
710 if (sone.getLikedReplyIds().contains(reply.getId())) {
718 * Returns whether the given post is bookmarked.
722 * @return {@code true} if the given post is bookmarked, {@code false}
725 public boolean isBookmarked(Post post) {
726 return isPostBookmarked(post.getId());
730 * Returns whether the post with the given ID is bookmarked.
733 * The ID of the post to check
734 * @return {@code true} if the post with the given ID is bookmarked,
735 * {@code false} otherwise
737 public boolean isPostBookmarked(String id) {
738 synchronized (bookmarkedPosts) {
739 return bookmarkedPosts.contains(id);
744 * Returns all currently known bookmarked posts.
746 * @return All bookmarked posts
748 public Set<Post> getBookmarkedPosts() {
749 Set<Post> posts = new HashSet<Post>();
750 synchronized (bookmarkedPosts) {
751 for (String bookmarkedPostId : bookmarkedPosts) {
752 Post post = getPost(bookmarkedPostId, false);
766 * Locks the given Sone. A locked Sone will not be inserted by
767 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
773 public void lockSone(Sone sone) {
774 synchronized (lockedSones) {
775 if (lockedSones.add(sone)) {
776 coreListenerManager.fireSoneLocked(sone);
782 * Unlocks the given Sone.
784 * @see #lockSone(Sone)
788 public void unlockSone(Sone sone) {
789 synchronized (lockedSones) {
790 if (lockedSones.remove(sone)) {
791 coreListenerManager.fireSoneUnlocked(sone);
797 * Adds a local Sone from the given ID which has to be the ID of an own
801 * The ID of an own identity to add a Sone for
802 * @return The added (or already existing) Sone
804 public Sone addLocalSone(String id) {
805 synchronized (localSones) {
806 if (localSones.containsKey(id)) {
807 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
808 return localSones.get(id);
810 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
811 if (ownIdentity == null) {
812 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
815 return addLocalSone(ownIdentity);
820 * Adds a local Sone from the given own identity.
823 * The own identity to create a Sone from
824 * @return The added (or already existing) Sone
826 public Sone addLocalSone(OwnIdentity ownIdentity) {
827 if (ownIdentity == null) {
828 logger.log(Level.WARNING, "Given OwnIdentity is null!");
831 synchronized (localSones) {
834 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
835 } catch (MalformedURLException mue1) {
836 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
839 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
840 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
841 /* TODO - load posts ’n stuff */
842 localSones.put(ownIdentity.getId(), sone);
843 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
844 soneInserters.put(sone, soneInserter);
845 setSoneStatus(sone, SoneStatus.idle);
847 if (!preferences.isSoneRescueMode()) {
848 soneInserter.start();
850 new Thread(new Runnable() {
853 @SuppressWarnings("synthetic-access")
855 if (!preferences.isSoneRescueMode()) {
858 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
859 coreListenerManager.fireRescuingSone(sone);
861 long edition = sone.getLatestEdition();
862 while (!stopped && (edition >= 0) && preferences.isSoneRescueMode()) {
863 logger.log(Level.FINE, "Downloading edition " + edition + "…");
864 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
867 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
869 coreListenerManager.fireRescuedSone(sone);
870 soneInserter.start();
873 }, "Sone Downloader").start();
879 * Creates a new Sone for the given own identity.
882 * The own identity to create a Sone for
883 * @return The created Sone
885 public Sone createSone(OwnIdentity ownIdentity) {
887 ownIdentity.addContext("Sone");
888 } catch (WebOfTrustException wote1) {
889 logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
892 Sone sone = addLocalSone(ownIdentity);
897 * Adds the Sone of the given identity.
900 * The identity whose Sone to add
901 * @return The added or already existing Sone
903 public Sone addRemoteSone(Identity identity) {
904 if (identity == null) {
905 logger.log(Level.WARNING, "Given Identity is null!");
908 synchronized (remoteSones) {
909 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
910 boolean newSone = sone.getRequestUri() == null;
911 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
912 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
914 synchronized (newSones) {
915 newSone = !knownSones.contains(sone.getId());
917 newSones.add(sone.getId());
921 coreListenerManager.fireNewSoneFound(sone);
922 for (Sone localSone : getLocalSones()) {
923 if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
924 localSone.addFriend(sone.getId());
929 remoteSones.put(identity.getId(), sone);
930 soneDownloader.addSone(sone);
931 setSoneStatus(sone, SoneStatus.unknown);
932 new Thread(new Runnable() {
935 @SuppressWarnings("synthetic-access")
937 soneDownloader.fetchSone(sone);
940 }, "Sone Downloader").start();
946 * Retrieves the trust relationship from the origin to the target. If the
947 * trust relationship can not be retrieved, {@code null} is returned.
949 * @see Identity#getTrust(OwnIdentity)
951 * The origin of the trust tree
953 * The target of the trust
954 * @return The trust relationship
956 public Trust getTrust(Sone origin, Sone target) {
957 if (!isLocalSone(origin)) {
958 logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
961 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
965 * Sets the trust value of the given origin Sone for the target Sone.
972 * The trust value (from {@code -100} to {@code 100})
974 public void setTrust(Sone origin, Sone target, int trustValue) {
975 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();
977 ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
978 } catch (WebOfTrustException wote1) {
979 logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
984 * Removes any trust assignment for the given target Sone.
991 public void removeTrust(Sone origin, Sone target) {
992 Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
994 ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
995 } catch (WebOfTrustException wote1) {
996 logger.log(Level.WARNING, "Could not remove trust for Sone: " + target, wote1);
1001 * Assigns the configured positive trust value for the given target.
1008 public void trustSone(Sone origin, Sone target) {
1009 setTrust(origin, target, preferences.getPositiveTrust());
1013 * Assigns the configured negative trust value for the given target.
1020 public void distrustSone(Sone origin, Sone target) {
1021 setTrust(origin, target, preferences.getNegativeTrust());
1025 * Removes the trust assignment for the given target.
1032 public void untrustSone(Sone origin, Sone target) {
1033 removeTrust(origin, target);
1037 * Updates the stores Sone with the given Sone.
1042 public void updateSone(Sone sone) {
1043 if (hasSone(sone.getId())) {
1044 boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode();
1045 Sone storedSone = getSone(sone.getId());
1046 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
1047 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
1050 synchronized (posts) {
1051 if (!soneRescueMode) {
1052 for (Post post : storedSone.getPosts()) {
1053 posts.remove(post.getId());
1054 if (!sone.getPosts().contains(post)) {
1055 coreListenerManager.firePostRemoved(post);
1059 List<Post> storedPosts = storedSone.getPosts();
1060 synchronized (newPosts) {
1061 for (Post post : sone.getPosts()) {
1062 post.setSone(storedSone);
1063 if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
1064 newPosts.add(post.getId());
1065 coreListenerManager.fireNewPostFound(post);
1067 posts.put(post.getId(), post);
1071 synchronized (replies) {
1072 if (!soneRescueMode) {
1073 for (Reply reply : storedSone.getReplies()) {
1074 replies.remove(reply.getId());
1075 if (!sone.getReplies().contains(reply)) {
1076 coreListenerManager.fireReplyRemoved(reply);
1080 Set<Reply> storedReplies = storedSone.getReplies();
1081 synchronized (newReplies) {
1082 for (Reply reply : sone.getReplies()) {
1083 reply.setSone(storedSone);
1084 if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
1085 newReplies.add(reply.getId());
1086 coreListenerManager.fireNewReplyFound(reply);
1088 replies.put(reply.getId(), reply);
1092 synchronized (storedSone) {
1093 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
1094 storedSone.setTime(sone.getTime());
1096 storedSone.setClient(sone.getClient());
1097 storedSone.setProfile(sone.getProfile());
1098 if (soneRescueMode) {
1099 for (Post post : sone.getPosts()) {
1100 storedSone.addPost(post);
1102 for (Reply reply : sone.getReplies()) {
1103 storedSone.addReply(reply);
1105 for (String likedPostId : sone.getLikedPostIds()) {
1106 storedSone.addLikedPostId(likedPostId);
1108 for (String likedReplyId : sone.getLikedReplyIds()) {
1109 storedSone.addLikedReplyId(likedReplyId);
1112 storedSone.setPosts(sone.getPosts());
1113 storedSone.setReplies(sone.getReplies());
1114 storedSone.setLikePostIds(sone.getLikedPostIds());
1115 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
1117 storedSone.setLatestEdition(sone.getLatestEdition());
1123 * Deletes the given Sone. This will remove the Sone from the
1124 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1125 * and remove the context from its identity.
1128 * The Sone to delete
1130 public void deleteSone(Sone sone) {
1131 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1132 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1135 synchronized (localSones) {
1136 if (!localSones.containsKey(sone.getId())) {
1137 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1140 localSones.remove(sone.getId());
1141 soneInserters.remove(sone).stop();
1144 ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1145 ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1146 } catch (WebOfTrustException wote1) {
1147 logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1150 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1151 } catch (ConfigurationException ce1) {
1152 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1157 * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
1158 * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
1161 * The Sone to mark as known
1163 public void markSoneKnown(Sone sone) {
1164 synchronized (newSones) {
1165 if (newSones.remove(sone.getId())) {
1166 knownSones.add(sone.getId());
1167 coreListenerManager.fireMarkSoneKnown(sone);
1168 saveConfiguration();
1174 * Loads and updates the given Sone from the configuration. If any error is
1175 * encountered, loading is aborted and the given Sone is not changed.
1178 * The Sone to load and update
1180 public void loadSone(Sone sone) {
1181 if (!isLocalSone(sone)) {
1182 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1187 String sonePrefix = "Sone/" + sone.getId();
1188 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1189 if (soneTime == null) {
1190 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1193 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1196 Profile profile = new Profile();
1197 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1198 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1199 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1200 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1201 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1202 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1204 /* load profile fields. */
1206 String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
1207 String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
1208 if (fieldName == null) {
1211 String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
1212 profile.addField(fieldName).setValue(fieldValue);
1216 Set<Post> posts = new HashSet<Post>();
1218 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1219 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1220 if (postId == null) {
1223 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1224 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1225 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1226 if ((postTime == 0) || (postText == null)) {
1227 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1230 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1231 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1232 post.setRecipient(getSone(postRecipientId));
1238 Set<Reply> replies = new HashSet<Reply>();
1240 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1241 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1242 if (replyId == null) {
1245 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1246 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1247 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1248 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1249 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1252 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1255 /* load post likes. */
1256 Set<String> likedPostIds = new HashSet<String>();
1258 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1259 if (likedPostId == null) {
1262 likedPostIds.add(likedPostId);
1265 /* load reply likes. */
1266 Set<String> likedReplyIds = new HashSet<String>();
1268 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1269 if (likedReplyId == null) {
1272 likedReplyIds.add(likedReplyId);
1276 Set<String> friends = new HashSet<String>();
1278 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1279 if (friendId == null) {
1282 friends.add(friendId);
1286 sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
1287 sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
1289 /* if we’re still here, Sone was loaded successfully. */
1290 synchronized (sone) {
1291 sone.setTime(soneTime);
1292 sone.setProfile(profile);
1293 sone.setPosts(posts);
1294 sone.setReplies(replies);
1295 sone.setLikePostIds(likedPostIds);
1296 sone.setLikeReplyIds(likedReplyIds);
1297 sone.setFriends(friends);
1298 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1300 synchronized (newSones) {
1301 for (String friend : friends) {
1302 knownSones.add(friend);
1305 synchronized (newPosts) {
1306 for (Post post : posts) {
1307 knownPosts.add(post.getId());
1310 synchronized (newReplies) {
1311 for (Reply reply : replies) {
1312 knownReplies.add(reply.getId());
1318 * Saves the given Sone. This will persist all local settings for the given
1319 * Sone, such as the friends list and similar, private options.
1324 public synchronized void saveSone(Sone sone) {
1325 if (!isLocalSone(sone)) {
1326 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1329 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1330 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1334 logger.log(Level.INFO, "Saving Sone: %s", sone);
1336 ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1338 /* save Sone into configuration. */
1339 String sonePrefix = "Sone/" + sone.getId();
1340 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1341 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1344 Profile profile = sone.getProfile();
1345 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1346 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1347 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1348 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1349 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1350 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1352 /* save profile fields. */
1353 int fieldCounter = 0;
1354 for (Field profileField : profile.getFields()) {
1355 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1356 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1357 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1359 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1362 int postCounter = 0;
1363 for (Post post : sone.getPosts()) {
1364 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1365 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1366 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1367 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1368 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1370 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1373 int replyCounter = 0;
1374 for (Reply reply : sone.getReplies()) {
1375 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1376 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1377 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1378 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1379 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1381 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1383 /* save post likes. */
1384 int postLikeCounter = 0;
1385 for (String postId : sone.getLikedPostIds()) {
1386 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1388 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1390 /* save reply likes. */
1391 int replyLikeCounter = 0;
1392 for (String replyId : sone.getLikedReplyIds()) {
1393 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1395 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1398 int friendCounter = 0;
1399 for (String friendId : sone.getFriends()) {
1400 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1402 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1405 configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
1407 configuration.save();
1408 logger.log(Level.INFO, "Sone %s saved.", sone);
1409 } catch (ConfigurationException ce1) {
1410 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1411 } catch (WebOfTrustException wote1) {
1412 logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1417 * Creates a new post.
1420 * The Sone that creates the post
1422 * The text of the post
1423 * @return The created post
1425 public Post createPost(Sone sone, String text) {
1426 return createPost(sone, System.currentTimeMillis(), text);
1430 * Creates a new post.
1433 * The Sone that creates the post
1435 * The time of the post
1437 * The text of the post
1438 * @return The created post
1440 public Post createPost(Sone sone, long time, String text) {
1441 return createPost(sone, null, time, text);
1445 * Creates a new post.
1448 * The Sone that creates the post
1450 * The recipient Sone, or {@code null} if this post does not have
1453 * The text of the post
1454 * @return The created post
1456 public Post createPost(Sone sone, Sone recipient, String text) {
1457 return createPost(sone, recipient, System.currentTimeMillis(), text);
1461 * Creates a new post.
1464 * The Sone that creates the post
1466 * The recipient Sone, or {@code null} if this post does not have
1469 * The time of the post
1471 * The text of the post
1472 * @return The created post
1474 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1475 if (!isLocalSone(sone)) {
1476 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1479 Post post = new Post(sone, time, text);
1480 if (recipient != null) {
1481 post.setRecipient(recipient);
1483 synchronized (posts) {
1484 posts.put(post.getId(), post);
1486 synchronized (newPosts) {
1487 newPosts.add(post.getId());
1488 coreListenerManager.fireNewPostFound(post);
1496 * Deletes the given post.
1499 * The post to delete
1501 public void deletePost(Post post) {
1502 if (!isLocalSone(post.getSone())) {
1503 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1506 post.getSone().removePost(post);
1507 synchronized (posts) {
1508 posts.remove(post.getId());
1510 synchronized (newPosts) {
1511 markPostKnown(post);
1512 knownPosts.remove(post.getId());
1514 saveSone(post.getSone());
1518 * Marks the given post as known, if it is currently a new post (according
1519 * to {@link #isNewPost(String)}).
1522 * The post to mark as known
1524 public void markPostKnown(Post post) {
1525 synchronized (newPosts) {
1526 if (newPosts.remove(post.getId())) {
1527 knownPosts.add(post.getId());
1528 coreListenerManager.fireMarkPostKnown(post);
1529 saveConfiguration();
1535 * Bookmarks the given post.
1538 * The post to bookmark
1540 public void bookmark(Post post) {
1541 bookmarkPost(post.getId());
1545 * Bookmarks the post with the given ID.
1548 * The ID of the post to bookmark
1550 public void bookmarkPost(String id) {
1551 synchronized (bookmarkedPosts) {
1552 bookmarkedPosts.add(id);
1557 * Removes the given post from the bookmarks.
1560 * The post to unbookmark
1562 public void unbookmark(Post post) {
1563 unbookmarkPost(post.getId());
1567 * Removes the post with the given ID from the bookmarks.
1570 * The ID of the post to unbookmark
1572 public void unbookmarkPost(String id) {
1573 synchronized (bookmarkedPosts) {
1574 bookmarkedPosts.remove(id);
1579 * Creates a new reply.
1582 * The Sone that creates the reply
1584 * The post that this reply refers to
1586 * The text of the reply
1587 * @return The created reply
1589 public Reply createReply(Sone sone, Post post, String text) {
1590 return createReply(sone, post, System.currentTimeMillis(), text);
1594 * Creates a new reply.
1597 * The Sone that creates the reply
1599 * The post that this reply refers to
1601 * The time of the reply
1603 * The text of the reply
1604 * @return The created reply
1606 public Reply createReply(Sone sone, Post post, long time, String text) {
1607 if (!isLocalSone(sone)) {
1608 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1611 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1612 synchronized (replies) {
1613 replies.put(reply.getId(), reply);
1615 synchronized (newReplies) {
1616 newReplies.add(reply.getId());
1617 coreListenerManager.fireNewReplyFound(reply);
1619 sone.addReply(reply);
1625 * Deletes the given reply.
1628 * The reply to delete
1630 public void deleteReply(Reply reply) {
1631 Sone sone = reply.getSone();
1632 if (!isLocalSone(sone)) {
1633 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1636 synchronized (replies) {
1637 replies.remove(reply.getId());
1639 synchronized (newReplies) {
1640 markReplyKnown(reply);
1641 knownReplies.remove(reply.getId());
1643 sone.removeReply(reply);
1648 * Marks the given reply as known, if it is currently a new reply (according
1649 * to {@link #isNewReply(String)}).
1652 * The reply to mark as known
1654 public void markReplyKnown(Reply reply) {
1655 synchronized (newReplies) {
1656 if (newReplies.remove(reply.getId())) {
1657 knownReplies.add(reply.getId());
1658 coreListenerManager.fireMarkReplyKnown(reply);
1659 saveConfiguration();
1667 public void start() {
1668 loadConfiguration();
1669 updateChecker.addUpdateListener(this);
1670 updateChecker.start();
1676 public void stop() {
1677 synchronized (localSones) {
1678 for (SoneInserter soneInserter : soneInserters.values()) {
1679 soneInserter.stop();
1682 updateChecker.stop();
1683 updateChecker.removeUpdateListener(this);
1684 soneDownloader.stop();
1685 saveConfiguration();
1690 * Saves the current options.
1692 public void saveConfiguration() {
1693 synchronized (configuration) {
1694 if (storingConfiguration) {
1695 logger.log(Level.FINE, "Already storing configuration…");
1698 storingConfiguration = true;
1701 /* store the options first. */
1703 configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
1704 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1705 configuration.getIntValue("Option/PostsPerPage").setValue(options.getIntegerOption("PostsPerPage").getReal());
1706 configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
1707 configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
1708 configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
1709 configuration.getBooleanValue("Option/ActivateFcpInterface").setValue(options.getBooleanOption("ActivateFcpInterface").getReal());
1710 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1711 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1712 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1714 /* save known Sones. */
1715 int soneCounter = 0;
1716 synchronized (newSones) {
1717 for (String knownSoneId : knownSones) {
1718 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1720 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1723 /* save known posts. */
1724 int postCounter = 0;
1725 synchronized (newPosts) {
1726 for (String knownPostId : knownPosts) {
1727 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1729 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1732 /* save known replies. */
1733 int replyCounter = 0;
1734 synchronized (newReplies) {
1735 for (String knownReplyId : knownReplies) {
1736 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1738 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1741 /* save bookmarked posts. */
1742 int bookmarkedPostCounter = 0;
1743 synchronized (bookmarkedPosts) {
1744 for (String bookmarkedPostId : bookmarkedPosts) {
1745 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
1748 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
1751 configuration.save();
1753 } catch (ConfigurationException ce1) {
1754 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1756 synchronized (configuration) {
1757 storingConfiguration = false;
1767 * Loads the configuration.
1769 @SuppressWarnings("unchecked")
1770 private void loadConfiguration() {
1771 /* create options. */
1772 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1775 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1776 SoneInserter.setInsertionDelay(newValue);
1780 options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(25));
1781 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75));
1782 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25));
1783 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
1784 options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, new OptionWatcher<Boolean>() {
1787 @SuppressWarnings("synthetic-access")
1788 public void optionChanged(Option<Boolean> option, Boolean oldValue, Boolean newValue) {
1789 fcpInterface.setActive(newValue);
1792 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1793 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1794 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1796 /* read options from configuration. */
1797 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1798 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1799 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1800 options.getBooleanOption("ClearOnNextRestart").set(null);
1801 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1802 if (clearConfiguration) {
1803 /* stop loading the configuration. */
1807 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1808 options.getIntegerOption("PostsPerPage").set(configuration.getIntValue("Option/PostsPerPage").getValue(null));
1809 options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
1810 options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
1811 options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
1812 options.getBooleanOption("ActivateFcpInterface").set(configuration.getBooleanValue("Option/ActivateFcpInterface").getValue(null));
1813 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1815 /* load known Sones. */
1816 int soneCounter = 0;
1818 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1819 if (knownSoneId == null) {
1822 synchronized (newSones) {
1823 knownSones.add(knownSoneId);
1827 /* load known posts. */
1828 int postCounter = 0;
1830 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1831 if (knownPostId == null) {
1834 synchronized (newPosts) {
1835 knownPosts.add(knownPostId);
1839 /* load known replies. */
1840 int replyCounter = 0;
1842 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1843 if (knownReplyId == null) {
1846 synchronized (newReplies) {
1847 knownReplies.add(knownReplyId);
1851 /* load bookmarked posts. */
1852 int bookmarkedPostCounter = 0;
1854 String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null);
1855 if (bookmarkedPostId == null) {
1858 synchronized (bookmarkedPosts) {
1859 bookmarkedPosts.add(bookmarkedPostId);
1866 * Generate a Sone URI from the given URI and latest edition.
1869 * The URI to derive the Sone URI from
1870 * @return The derived URI
1872 private FreenetURI getSoneUri(String uriString) {
1874 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1876 } catch (MalformedURLException mue1) {
1877 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1883 // INTERFACE IdentityListener
1890 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1891 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1892 if (ownIdentity.hasContext("Sone")) {
1893 trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1894 addLocalSone(ownIdentity);
1902 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1903 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1904 trustedIdentities.remove(ownIdentity);
1911 public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1912 logger.log(Level.FINEST, "Adding Identity: " + identity);
1913 trustedIdentities.get(ownIdentity).add(identity);
1914 addRemoteSone(identity);
1921 public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1922 new Thread(new Runnable() {
1925 @SuppressWarnings("synthetic-access")
1927 Sone sone = getRemoteSone(identity.getId());
1928 sone.setIdentity(identity);
1929 soneDownloader.addSone(sone);
1930 soneDownloader.fetchSone(sone);
1939 public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1940 trustedIdentities.get(ownIdentity).remove(identity);
1944 // INTERFACE UpdateListener
1951 public void updateFound(Version version, long releaseTime, long latestEdition) {
1952 coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition);
1956 * Convenience interface for external classes that want to access the core’s
1959 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1961 public static class Preferences {
1963 /** The wrapped options. */
1964 private final Options options;
1967 * Creates a new preferences object wrapped around the given options.
1970 * The options to wrap
1972 public Preferences(Options options) {
1973 this.options = options;
1977 * Returns the insertion delay.
1979 * @return The insertion delay
1981 public int getInsertionDelay() {
1982 return options.getIntegerOption("InsertionDelay").get();
1986 * Sets the insertion delay
1988 * @param insertionDelay
1989 * The new insertion delay, or {@code null} to restore it to
1991 * @return This preferences
1993 public Preferences setInsertionDelay(Integer insertionDelay) {
1994 options.getIntegerOption("InsertionDelay").set(insertionDelay);
1999 * Returns the number of posts to show per page.
2001 * @return The number of posts to show per page
2003 public int getPostsPerPage() {
2004 return options.getIntegerOption("PostsPerPage").get();
2008 * Sets the number of posts to show per page.
2010 * @param postsPerPage
2011 * The number of posts to show per page
2012 * @return This preferences object
2014 public Preferences setPostsPerPage(Integer postsPerPage) {
2015 options.getIntegerOption("PostsPerPage").set(postsPerPage);
2020 * Returns the positive trust.
2022 * @return The positive trust
2024 public int getPositiveTrust() {
2025 return options.getIntegerOption("PositiveTrust").get();
2029 * Sets the positive trust.
2031 * @param positiveTrust
2032 * The new positive trust, or {@code null} to restore it to
2034 * @return This preferences
2036 public Preferences setPositiveTrust(Integer positiveTrust) {
2037 options.getIntegerOption("PositiveTrust").set(positiveTrust);
2042 * Returns the negative trust.
2044 * @return The negative trust
2046 public int getNegativeTrust() {
2047 return options.getIntegerOption("NegativeTrust").get();
2051 * Sets the negative trust.
2053 * @param negativeTrust
2054 * The negative trust, or {@code null} to restore it to the
2056 * @return The preferences
2058 public Preferences setNegativeTrust(Integer negativeTrust) {
2059 options.getIntegerOption("NegativeTrust").set(negativeTrust);
2064 * Returns the trust comment. This is the comment that is set in the web
2065 * of trust when a trust value is assigned to an identity.
2067 * @return The trust comment
2069 public String getTrustComment() {
2070 return options.getStringOption("TrustComment").get();
2074 * Sets the trust comment.
2076 * @param trustComment
2077 * The trust comment, or {@code null} to restore it to the
2079 * @return This preferences
2081 public Preferences setTrustComment(String trustComment) {
2082 options.getStringOption("TrustComment").set(trustComment);
2087 * Returns whether the {@link FcpInterface FCP interface} is currently
2090 * @see FcpInterface#setActive(boolean)
2091 * @return {@code true} if the FCP interface is currently active,
2092 * {@code false} otherwise
2094 public boolean isFcpInterfaceActive() {
2095 return options.getBooleanOption("ActivateFcpInterface").get();
2099 * Sets whether the {@link FcpInterface FCP interface} is currently
2102 * @see FcpInterface#setActive(boolean)
2103 * @param fcpInterfaceActive
2104 * {@code true} to activate the FCP interface, {@code false}
2105 * to deactivate the FCP interface
2106 * @return This preferences object
2108 public Preferences setFcpInterfaceActive(boolean fcpInterfaceActive) {
2109 options.getBooleanOption("ActivateFcpInterface").set(fcpInterfaceActive);
2114 * Returns whether the rescue mode is active.
2116 * @return {@code true} if the rescue mode is active, {@code false}
2119 public boolean isSoneRescueMode() {
2120 return options.getBooleanOption("SoneRescueMode").get();
2124 * Sets whether the rescue mode is active.
2126 * @param soneRescueMode
2127 * {@code true} if the rescue mode is active, {@code false}
2129 * @return This preferences
2131 public Preferences setSoneRescueMode(Boolean soneRescueMode) {
2132 options.getBooleanOption("SoneRescueMode").set(soneRescueMode);
2137 * Returns whether Sone should clear its settings on the next restart.
2138 * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
2139 * to return {@code true} as well!
2141 * @return {@code true} if Sone should clear its settings on the next
2142 * restart, {@code false} otherwise
2144 public boolean isClearOnNextRestart() {
2145 return options.getBooleanOption("ClearOnNextRestart").get();
2149 * Sets whether Sone will clear its settings on the next restart.
2151 * @param clearOnNextRestart
2152 * {@code true} if Sone should clear its settings on the next
2153 * restart, {@code false} otherwise
2154 * @return This preferences
2156 public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
2157 options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
2162 * Returns whether Sone should really clear its settings on next
2163 * restart. This is a confirmation option that needs to be set in
2164 * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
2165 * settings on the next restart.
2167 * @return {@code true} if Sone should really clear its settings on the
2168 * next restart, {@code false} otherwise
2170 public boolean isReallyClearOnNextRestart() {
2171 return options.getBooleanOption("ReallyClearOnNextRestart").get();
2175 * Sets whether Sone should really clear its settings on the next
2178 * @param reallyClearOnNextRestart
2179 * {@code true} if Sone should really clear its settings on
2180 * the next restart, {@code false} otherwise
2181 * @return This preferences
2183 public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
2184 options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);