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.fcp.FcpInterface.FullAccessRequired;
42 import net.pterodactylus.sone.freenet.wot.Identity;
43 import net.pterodactylus.sone.freenet.wot.IdentityListener;
44 import net.pterodactylus.sone.freenet.wot.IdentityManager;
45 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
46 import net.pterodactylus.sone.freenet.wot.Trust;
47 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
48 import net.pterodactylus.sone.main.SonePlugin;
49 import net.pterodactylus.util.config.Configuration;
50 import net.pterodactylus.util.config.ConfigurationException;
51 import net.pterodactylus.util.logging.Logging;
52 import net.pterodactylus.util.number.Numbers;
53 import net.pterodactylus.util.validation.Validation;
54 import net.pterodactylus.util.version.Version;
55 import freenet.keys.FreenetURI;
60 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
62 public class Core implements IdentityListener, UpdateListener {
65 * Enumeration for the possible states of a {@link Sone}.
67 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
69 public enum SoneStatus {
71 /** The Sone is unknown, i.e. not yet downloaded. */
74 /** The Sone is idle, i.e. not being downloaded or inserted. */
77 /** The Sone is currently being inserted. */
80 /** The Sone is currently being downloaded. */
85 private static final Logger logger = Logging.getLogger(Core.class);
88 private final Options options = new Options();
90 /** The preferences. */
91 private final Preferences preferences = new Preferences(options);
93 /** The core listener manager. */
94 private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
96 /** The configuration. */
97 private Configuration configuration;
99 /** Whether we’re currently saving the configuration. */
100 private boolean storingConfiguration = false;
102 /** The identity manager. */
103 private final IdentityManager identityManager;
105 /** Interface to freenet. */
106 private final FreenetInterface freenetInterface;
108 /** The Sone downloader. */
109 private final SoneDownloader soneDownloader;
111 /** The update checker. */
112 private final UpdateChecker updateChecker;
114 /** The FCP interface. */
115 private volatile FcpInterface fcpInterface;
117 /** Whether the core has been stopped. */
118 private volatile boolean stopped;
120 /** The Sones’ statuses. */
121 /* synchronize access on itself. */
122 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
124 /** Locked local Sones. */
125 /* synchronize on itself. */
126 private final Set<Sone> lockedSones = new HashSet<Sone>();
128 /** Sone inserters. */
129 /* synchronize access on this on localSones. */
130 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
132 /** All local Sones. */
133 /* synchronize access on this on itself. */
134 private Map<String, Sone> localSones = new HashMap<String, Sone>();
136 /** All remote Sones. */
137 /* synchronize access on this on itself. */
138 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
140 /** All new Sones. */
141 private Set<String> newSones = new HashSet<String>();
143 /** All known Sones. */
144 /* synchronize access on {@link #newSones}. */
145 private Set<String> knownSones = new HashSet<String>();
148 private Map<String, Post> posts = new HashMap<String, Post>();
150 /** All new posts. */
151 private Set<String> newPosts = new HashSet<String>();
153 /** All known posts. */
154 /* synchronize access on {@link #newPosts}. */
155 private Set<String> knownPosts = new HashSet<String>();
158 private Map<String, Reply> replies = new HashMap<String, Reply>();
160 /** All new replies. */
161 private Set<String> newReplies = new HashSet<String>();
163 /** All known replies. */
164 private Set<String> knownReplies = new HashSet<String>();
166 /** All bookmarked posts. */
167 /* synchronize access on itself. */
168 private Set<String> bookmarkedPosts = new HashSet<String>();
170 /** Trusted identities, sorted by own identities. */
171 private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
174 * Creates a new core.
176 * @param configuration
177 * The configuration of the core
178 * @param freenetInterface
179 * The freenet interface
180 * @param identityManager
181 * The identity manager
183 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
184 this.configuration = configuration;
185 this.freenetInterface = freenetInterface;
186 this.identityManager = identityManager;
187 this.soneDownloader = new SoneDownloader(this, freenetInterface);
188 this.updateChecker = new UpdateChecker(freenetInterface);
192 // LISTENER MANAGEMENT
196 * Adds a new core listener.
198 * @param coreListener
199 * The listener to add
201 public void addCoreListener(CoreListener coreListener) {
202 coreListenerManager.addListener(coreListener);
206 * Removes a core listener.
208 * @param coreListener
209 * The listener to remove
211 public void removeCoreListener(CoreListener coreListener) {
212 coreListenerManager.removeListener(coreListener);
220 * Sets the configuration to use. This will automatically save the current
221 * configuration to the given configuration.
223 * @param configuration
224 * The new configuration to use
226 public void setConfiguration(Configuration configuration) {
227 this.configuration = configuration;
232 * Returns the options used by the core.
234 * @return The options of the core
236 public Preferences getPreferences() {
241 * Returns the identity manager used by the core.
243 * @return The identity manager
245 public IdentityManager getIdentityManager() {
246 return identityManager;
250 * Returns the update checker.
252 * @return The update checker
254 public UpdateChecker getUpdateChecker() {
255 return updateChecker;
259 * Sets the FCP interface to use.
261 * @param fcpInterface
262 * The FCP interface to use
264 public void setFcpInterface(FcpInterface fcpInterface) {
265 this.fcpInterface = fcpInterface;
269 * Returns the status of the given Sone.
272 * The Sone to get the status for
273 * @return The status of the Sone
275 public SoneStatus getSoneStatus(Sone sone) {
276 synchronized (soneStatuses) {
277 return soneStatuses.get(sone);
282 * Sets the status of the given Sone.
285 * The Sone to set the status of
289 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
290 synchronized (soneStatuses) {
291 soneStatuses.put(sone, soneStatus);
296 * Returns whether the given Sone is currently locked.
300 * @return {@code true} if the Sone is locked, {@code false} if it is not
302 public boolean isLocked(Sone sone) {
303 synchronized (lockedSones) {
304 return lockedSones.contains(sone);
309 * Returns all Sones, remote and local.
313 public Set<Sone> getSones() {
314 Set<Sone> allSones = new HashSet<Sone>();
315 allSones.addAll(getLocalSones());
316 allSones.addAll(getRemoteSones());
321 * Returns the Sone with the given ID, regardless whether it’s local or
325 * The ID of the Sone to get
326 * @return The Sone with the given ID, or {@code null} if there is no such
329 public Sone getSone(String id) {
330 return getSone(id, true);
334 * Returns the Sone with the given ID, regardless whether it’s local or
338 * The ID of the Sone to get
340 * {@code true} to create a new Sone if none exists,
341 * {@code false} to return {@code null} if a Sone with the given
343 * @return The Sone with the given ID, or {@code null} if there is no such
346 public Sone getSone(String id, boolean create) {
347 if (isLocalSone(id)) {
348 return getLocalSone(id);
350 return getRemoteSone(id, create);
354 * Checks whether the core knows a Sone with the given ID.
358 * @return {@code true} if there is a Sone with the given ID, {@code false}
361 public boolean hasSone(String id) {
362 return isLocalSone(id) || isRemoteSone(id);
366 * Returns whether the given Sone is a local Sone.
369 * The Sone to check for its locality
370 * @return {@code true} if the given Sone is local, {@code false} otherwise
372 public boolean isLocalSone(Sone sone) {
373 synchronized (localSones) {
374 return localSones.containsKey(sone.getId());
379 * Returns whether the given ID is the ID of a local Sone.
382 * The Sone ID to check for its locality
383 * @return {@code true} if the given ID is a local Sone, {@code false}
386 public boolean isLocalSone(String id) {
387 synchronized (localSones) {
388 return localSones.containsKey(id);
393 * Returns all local Sones.
395 * @return All local Sones
397 public Set<Sone> getLocalSones() {
398 synchronized (localSones) {
399 return new HashSet<Sone>(localSones.values());
404 * Returns the local Sone with the given ID.
407 * The ID of the Sone to get
408 * @return The Sone with the given ID
410 public Sone getLocalSone(String id) {
411 return getLocalSone(id, true);
415 * Returns the local Sone with the given ID, optionally creating a new Sone.
420 * {@code true} to create a new Sone if none exists,
421 * {@code false} to return null if none exists
422 * @return The Sone with the given ID, or {@code null}
424 public Sone getLocalSone(String id, boolean create) {
425 synchronized (localSones) {
426 Sone sone = localSones.get(id);
427 if ((sone == null) && create) {
429 localSones.put(id, sone);
430 setSoneStatus(sone, SoneStatus.unknown);
437 * Returns all remote Sones.
439 * @return All remote Sones
441 public Set<Sone> getRemoteSones() {
442 synchronized (remoteSones) {
443 return new HashSet<Sone>(remoteSones.values());
448 * Returns the remote Sone with the given ID.
451 * The ID of the remote Sone to get
452 * @return The Sone with the given ID
454 public Sone getRemoteSone(String id) {
455 return getRemoteSone(id, true);
459 * Returns the remote Sone with the given ID.
462 * The ID of the remote Sone to get
464 * {@code true} to always create a Sone, {@code false} to return
465 * {@code null} if no Sone with the given ID exists
466 * @return The Sone with the given ID
468 public Sone getRemoteSone(String id, boolean create) {
469 synchronized (remoteSones) {
470 Sone sone = remoteSones.get(id);
471 if ((sone == null) && create) {
473 remoteSones.put(id, sone);
474 setSoneStatus(sone, SoneStatus.unknown);
481 * Returns whether the given Sone is a remote Sone.
485 * @return {@code true} if the given Sone is a remote Sone, {@code false}
488 public boolean isRemoteSone(Sone sone) {
489 synchronized (remoteSones) {
490 return remoteSones.containsKey(sone.getId());
495 * Returns whether the Sone with the given ID is a remote Sone.
498 * The ID of the Sone to check
499 * @return {@code true} if the Sone with the given ID is a remote Sone,
500 * {@code false} otherwise
502 public boolean isRemoteSone(String id) {
503 synchronized (remoteSones) {
504 return remoteSones.containsKey(id);
509 * Returns whether the Sone with the given ID is a new Sone.
512 * The ID of the sone to check for
513 * @return {@code true} if the given Sone is new, false otherwise
515 public boolean isNewSone(String soneId) {
516 synchronized (newSones) {
517 return !knownSones.contains(soneId) && newSones.contains(soneId);
522 * Returns whether the given Sone has been modified.
525 * The Sone to check for modifications
526 * @return {@code true} if a modification has been detected in the Sone,
527 * {@code false} otherwise
529 public boolean isModifiedSone(Sone sone) {
530 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
534 * Returns whether the target Sone is trusted by the origin Sone.
540 * @return {@code true} if the target Sone is trusted by the origin Sone
542 public boolean isSoneTrusted(Sone origin, Sone target) {
543 Validation.begin().isNotNull("Origin", origin).isNotNull("Target", target).check().isInstanceOf("Origin’s OwnIdentity", origin.getIdentity(), OwnIdentity.class).check();
544 return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity());
548 * Returns the post with the given ID.
551 * The ID of the post to get
552 * @return The post with the given ID, or a new post with the given ID
554 public Post getPost(String postId) {
555 return getPost(postId, true);
559 * Returns the post with the given ID, optionally creating a new post.
562 * The ID of the post to get
564 * {@code true} it create a new post if no post with the given ID
565 * exists, {@code false} to return {@code null}
566 * @return The post, or {@code null} if there is no such post
568 public Post getPost(String postId, boolean create) {
569 synchronized (posts) {
570 Post post = posts.get(postId);
571 if ((post == null) && create) {
572 post = new Post(postId);
573 posts.put(postId, post);
580 * Returns whether the given post ID is new.
584 * @return {@code true} if the post is considered to be new, {@code false}
587 public boolean isNewPost(String postId) {
588 synchronized (newPosts) {
589 return !knownPosts.contains(postId) && newPosts.contains(postId);
594 * Returns all posts that have the given Sone as recipient.
596 * @see Post#getRecipient()
598 * The recipient of the posts
599 * @return All posts that have the given Sone as recipient
601 public Set<Post> getDirectedPosts(Sone recipient) {
602 Validation.begin().isNotNull("Recipient", recipient).check();
603 Set<Post> directedPosts = new HashSet<Post>();
604 synchronized (posts) {
605 for (Post post : posts.values()) {
606 if (recipient.equals(post.getRecipient())) {
607 directedPosts.add(post);
611 return directedPosts;
615 * Returns the reply with the given ID. If there is no reply with the given
616 * ID yet, a new one is created.
619 * The ID of the reply to get
622 public Reply getReply(String replyId) {
623 return getReply(replyId, true);
627 * Returns the reply with the given ID. If there is no reply with the given
628 * ID yet, a new one is created, unless {@code create} is false in which
629 * case {@code null} is returned.
632 * The ID of the reply to get
634 * {@code true} to always return a {@link Reply}, {@code false}
635 * to return {@code null} if no reply can be found
636 * @return The reply, or {@code null} if there is no such reply
638 public Reply getReply(String replyId, boolean create) {
639 synchronized (replies) {
640 Reply reply = replies.get(replyId);
641 if (create && (reply == null)) {
642 reply = new Reply(replyId);
643 replies.put(replyId, reply);
650 * Returns all replies for the given post, order ascending by time.
653 * The post to get all replies for
654 * @return All replies for the given post
656 public List<Reply> getReplies(Post post) {
657 Set<Sone> sones = getSones();
658 List<Reply> replies = new ArrayList<Reply>();
659 for (Sone sone : sones) {
660 for (Reply reply : sone.getReplies()) {
661 if (reply.getPost().equals(post)) {
666 Collections.sort(replies, Reply.TIME_COMPARATOR);
671 * Returns whether the reply with the given ID is new.
674 * The ID of the reply to check
675 * @return {@code true} if the reply is considered to be new, {@code false}
678 public boolean isNewReply(String replyId) {
679 synchronized (newReplies) {
680 return !knownReplies.contains(replyId) && newReplies.contains(replyId);
685 * Returns all Sones that have liked the given post.
688 * The post to get the liking Sones for
689 * @return The Sones that like the given post
691 public Set<Sone> getLikes(Post post) {
692 Set<Sone> sones = new HashSet<Sone>();
693 for (Sone sone : getSones()) {
694 if (sone.getLikedPostIds().contains(post.getId())) {
702 * Returns all Sones that have liked the given reply.
705 * The reply to get the liking Sones for
706 * @return The Sones that like the given reply
708 public Set<Sone> getLikes(Reply reply) {
709 Set<Sone> sones = new HashSet<Sone>();
710 for (Sone sone : getSones()) {
711 if (sone.getLikedReplyIds().contains(reply.getId())) {
719 * Returns whether the given post is bookmarked.
723 * @return {@code true} if the given post is bookmarked, {@code false}
726 public boolean isBookmarked(Post post) {
727 return isPostBookmarked(post.getId());
731 * Returns whether the post with the given ID is bookmarked.
734 * The ID of the post to check
735 * @return {@code true} if the post with the given ID is bookmarked,
736 * {@code false} otherwise
738 public boolean isPostBookmarked(String id) {
739 synchronized (bookmarkedPosts) {
740 return bookmarkedPosts.contains(id);
745 * Returns all currently known bookmarked posts.
747 * @return All bookmarked posts
749 public Set<Post> getBookmarkedPosts() {
750 Set<Post> posts = new HashSet<Post>();
751 synchronized (bookmarkedPosts) {
752 for (String bookmarkedPostId : bookmarkedPosts) {
753 Post post = getPost(bookmarkedPostId, false);
767 * Locks the given Sone. A locked Sone will not be inserted by
768 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
774 public void lockSone(Sone sone) {
775 synchronized (lockedSones) {
776 if (lockedSones.add(sone)) {
777 coreListenerManager.fireSoneLocked(sone);
783 * Unlocks the given Sone.
785 * @see #lockSone(Sone)
789 public void unlockSone(Sone sone) {
790 synchronized (lockedSones) {
791 if (lockedSones.remove(sone)) {
792 coreListenerManager.fireSoneUnlocked(sone);
798 * Adds a local Sone from the given ID which has to be the ID of an own
802 * The ID of an own identity to add a Sone for
803 * @return The added (or already existing) Sone
805 public Sone addLocalSone(String id) {
806 synchronized (localSones) {
807 if (localSones.containsKey(id)) {
808 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
809 return localSones.get(id);
811 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
812 if (ownIdentity == null) {
813 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
816 return addLocalSone(ownIdentity);
821 * Adds a local Sone from the given own identity.
824 * The own identity to create a Sone from
825 * @return The added (or already existing) Sone
827 public Sone addLocalSone(OwnIdentity ownIdentity) {
828 if (ownIdentity == null) {
829 logger.log(Level.WARNING, "Given OwnIdentity is null!");
832 synchronized (localSones) {
835 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
836 } catch (MalformedURLException mue1) {
837 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
840 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
841 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
842 /* TODO - load posts ’n stuff */
843 localSones.put(ownIdentity.getId(), sone);
844 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
845 soneInserters.put(sone, soneInserter);
846 setSoneStatus(sone, SoneStatus.idle);
848 if (!preferences.isSoneRescueMode()) {
849 soneInserter.start();
851 new Thread(new Runnable() {
854 @SuppressWarnings("synthetic-access")
856 if (!preferences.isSoneRescueMode()) {
859 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
860 coreListenerManager.fireRescuingSone(sone);
862 long edition = sone.getLatestEdition();
863 while (!stopped && (edition >= 0) && preferences.isSoneRescueMode()) {
864 logger.log(Level.FINE, "Downloading edition " + edition + "…");
865 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
868 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
870 coreListenerManager.fireRescuedSone(sone);
871 soneInserter.start();
874 }, "Sone Downloader").start();
880 * Creates a new Sone for the given own identity.
883 * The own identity to create a Sone for
884 * @return The created Sone
886 public Sone createSone(OwnIdentity ownIdentity) {
888 ownIdentity.addContext("Sone");
889 } catch (WebOfTrustException wote1) {
890 logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
893 Sone sone = addLocalSone(ownIdentity);
898 * Adds the Sone of the given identity.
901 * The identity whose Sone to add
902 * @return The added or already existing Sone
904 public Sone addRemoteSone(Identity identity) {
905 if (identity == null) {
906 logger.log(Level.WARNING, "Given Identity is null!");
909 synchronized (remoteSones) {
910 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
911 boolean newSone = sone.getRequestUri() == null;
912 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
913 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
915 synchronized (newSones) {
916 newSone = !knownSones.contains(sone.getId());
918 newSones.add(sone.getId());
922 coreListenerManager.fireNewSoneFound(sone);
923 for (Sone localSone : getLocalSones()) {
924 if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
925 localSone.addFriend(sone.getId());
930 remoteSones.put(identity.getId(), sone);
931 soneDownloader.addSone(sone);
932 setSoneStatus(sone, SoneStatus.unknown);
933 new Thread(new Runnable() {
936 @SuppressWarnings("synthetic-access")
938 soneDownloader.fetchSone(sone);
941 }, "Sone Downloader").start();
947 * Retrieves the trust relationship from the origin to the target. If the
948 * trust relationship can not be retrieved, {@code null} is returned.
950 * @see Identity#getTrust(OwnIdentity)
952 * The origin of the trust tree
954 * The target of the trust
955 * @return The trust relationship
957 public Trust getTrust(Sone origin, Sone target) {
958 if (!isLocalSone(origin)) {
959 logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
962 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
966 * Sets the trust value of the given origin Sone for the target Sone.
973 * The trust value (from {@code -100} to {@code 100})
975 public void setTrust(Sone origin, Sone target, int trustValue) {
976 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();
978 ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
979 } catch (WebOfTrustException wote1) {
980 logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
985 * Removes any trust assignment for the given target Sone.
992 public void removeTrust(Sone origin, Sone target) {
993 Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
995 ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
996 } catch (WebOfTrustException wote1) {
997 logger.log(Level.WARNING, "Could not remove trust for Sone: " + target, wote1);
1002 * Assigns the configured positive trust value for the given target.
1009 public void trustSone(Sone origin, Sone target) {
1010 setTrust(origin, target, preferences.getPositiveTrust());
1014 * Assigns the configured negative trust value for the given target.
1021 public void distrustSone(Sone origin, Sone target) {
1022 setTrust(origin, target, preferences.getNegativeTrust());
1026 * Removes the trust assignment for the given target.
1033 public void untrustSone(Sone origin, Sone target) {
1034 removeTrust(origin, target);
1038 * Updates the stores Sone with the given Sone.
1043 public void updateSone(Sone sone) {
1044 if (hasSone(sone.getId())) {
1045 boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode();
1046 Sone storedSone = getSone(sone.getId());
1047 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
1048 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
1051 synchronized (posts) {
1052 if (!soneRescueMode) {
1053 for (Post post : storedSone.getPosts()) {
1054 posts.remove(post.getId());
1055 if (!sone.getPosts().contains(post)) {
1056 coreListenerManager.firePostRemoved(post);
1060 List<Post> storedPosts = storedSone.getPosts();
1061 synchronized (newPosts) {
1062 for (Post post : sone.getPosts()) {
1063 post.setSone(storedSone);
1064 if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
1065 newPosts.add(post.getId());
1066 coreListenerManager.fireNewPostFound(post);
1068 posts.put(post.getId(), post);
1072 synchronized (replies) {
1073 if (!soneRescueMode) {
1074 for (Reply reply : storedSone.getReplies()) {
1075 replies.remove(reply.getId());
1076 if (!sone.getReplies().contains(reply)) {
1077 coreListenerManager.fireReplyRemoved(reply);
1081 Set<Reply> storedReplies = storedSone.getReplies();
1082 synchronized (newReplies) {
1083 for (Reply reply : sone.getReplies()) {
1084 reply.setSone(storedSone);
1085 if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
1086 newReplies.add(reply.getId());
1087 coreListenerManager.fireNewReplyFound(reply);
1089 replies.put(reply.getId(), reply);
1093 synchronized (storedSone) {
1094 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
1095 storedSone.setTime(sone.getTime());
1097 storedSone.setClient(sone.getClient());
1098 storedSone.setProfile(sone.getProfile());
1099 if (soneRescueMode) {
1100 for (Post post : sone.getPosts()) {
1101 storedSone.addPost(post);
1103 for (Reply reply : sone.getReplies()) {
1104 storedSone.addReply(reply);
1106 for (String likedPostId : sone.getLikedPostIds()) {
1107 storedSone.addLikedPostId(likedPostId);
1109 for (String likedReplyId : sone.getLikedReplyIds()) {
1110 storedSone.addLikedReplyId(likedReplyId);
1113 storedSone.setPosts(sone.getPosts());
1114 storedSone.setReplies(sone.getReplies());
1115 storedSone.setLikePostIds(sone.getLikedPostIds());
1116 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
1118 storedSone.setLatestEdition(sone.getLatestEdition());
1124 * Deletes the given Sone. This will remove the Sone from the
1125 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1126 * and remove the context from its identity.
1129 * The Sone to delete
1131 public void deleteSone(Sone sone) {
1132 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1133 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1136 synchronized (localSones) {
1137 if (!localSones.containsKey(sone.getId())) {
1138 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1141 localSones.remove(sone.getId());
1142 soneInserters.remove(sone).stop();
1145 ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1146 ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1147 } catch (WebOfTrustException wote1) {
1148 logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1151 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1152 } catch (ConfigurationException ce1) {
1153 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1158 * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
1159 * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
1162 * The Sone to mark as known
1164 public void markSoneKnown(Sone sone) {
1165 synchronized (newSones) {
1166 if (newSones.remove(sone.getId())) {
1167 knownSones.add(sone.getId());
1168 coreListenerManager.fireMarkSoneKnown(sone);
1169 saveConfiguration();
1175 * Loads and updates the given Sone from the configuration. If any error is
1176 * encountered, loading is aborted and the given Sone is not changed.
1179 * The Sone to load and update
1181 public void loadSone(Sone sone) {
1182 if (!isLocalSone(sone)) {
1183 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1188 String sonePrefix = "Sone/" + sone.getId();
1189 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1190 if (soneTime == null) {
1191 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1194 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1197 Profile profile = new Profile();
1198 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1199 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1200 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1201 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1202 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1203 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1205 /* load profile fields. */
1207 String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
1208 String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
1209 if (fieldName == null) {
1212 String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
1213 profile.addField(fieldName).setValue(fieldValue);
1217 Set<Post> posts = new HashSet<Post>();
1219 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1220 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1221 if (postId == null) {
1224 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1225 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1226 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1227 if ((postTime == 0) || (postText == null)) {
1228 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1231 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1232 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1233 post.setRecipient(getSone(postRecipientId));
1239 Set<Reply> replies = new HashSet<Reply>();
1241 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1242 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1243 if (replyId == null) {
1246 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1247 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1248 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1249 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1250 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1253 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1256 /* load post likes. */
1257 Set<String> likedPostIds = new HashSet<String>();
1259 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1260 if (likedPostId == null) {
1263 likedPostIds.add(likedPostId);
1266 /* load reply likes. */
1267 Set<String> likedReplyIds = new HashSet<String>();
1269 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1270 if (likedReplyId == null) {
1273 likedReplyIds.add(likedReplyId);
1277 Set<String> friends = new HashSet<String>();
1279 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1280 if (friendId == null) {
1283 friends.add(friendId);
1287 sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
1288 sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
1290 /* if we’re still here, Sone was loaded successfully. */
1291 synchronized (sone) {
1292 sone.setTime(soneTime);
1293 sone.setProfile(profile);
1294 sone.setPosts(posts);
1295 sone.setReplies(replies);
1296 sone.setLikePostIds(likedPostIds);
1297 sone.setLikeReplyIds(likedReplyIds);
1298 sone.setFriends(friends);
1299 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1301 synchronized (newSones) {
1302 for (String friend : friends) {
1303 knownSones.add(friend);
1306 synchronized (newPosts) {
1307 for (Post post : posts) {
1308 knownPosts.add(post.getId());
1311 synchronized (newReplies) {
1312 for (Reply reply : replies) {
1313 knownReplies.add(reply.getId());
1319 * Saves the given Sone. This will persist all local settings for the given
1320 * Sone, such as the friends list and similar, private options.
1325 public synchronized void saveSone(Sone sone) {
1326 if (!isLocalSone(sone)) {
1327 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1330 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1331 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1335 logger.log(Level.INFO, "Saving Sone: %s", sone);
1337 ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1339 /* save Sone into configuration. */
1340 String sonePrefix = "Sone/" + sone.getId();
1341 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1342 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1345 Profile profile = sone.getProfile();
1346 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1347 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1348 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1349 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1350 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1351 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1353 /* save profile fields. */
1354 int fieldCounter = 0;
1355 for (Field profileField : profile.getFields()) {
1356 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1357 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1358 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1360 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1363 int postCounter = 0;
1364 for (Post post : sone.getPosts()) {
1365 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1366 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1367 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1368 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1369 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1371 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1374 int replyCounter = 0;
1375 for (Reply reply : sone.getReplies()) {
1376 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1377 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1378 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1379 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1380 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1382 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1384 /* save post likes. */
1385 int postLikeCounter = 0;
1386 for (String postId : sone.getLikedPostIds()) {
1387 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1389 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1391 /* save reply likes. */
1392 int replyLikeCounter = 0;
1393 for (String replyId : sone.getLikedReplyIds()) {
1394 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1396 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1399 int friendCounter = 0;
1400 for (String friendId : sone.getFriends()) {
1401 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1403 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1406 configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
1408 configuration.save();
1409 logger.log(Level.INFO, "Sone %s saved.", sone);
1410 } catch (ConfigurationException ce1) {
1411 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1412 } catch (WebOfTrustException wote1) {
1413 logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1418 * Creates a new post.
1421 * The Sone that creates the post
1423 * The text of the post
1424 * @return The created post
1426 public Post createPost(Sone sone, String text) {
1427 return createPost(sone, System.currentTimeMillis(), text);
1431 * Creates a new post.
1434 * The Sone that creates the post
1436 * The time of the post
1438 * The text of the post
1439 * @return The created post
1441 public Post createPost(Sone sone, long time, String text) {
1442 return createPost(sone, null, time, text);
1446 * Creates a new post.
1449 * The Sone that creates the post
1451 * The recipient Sone, or {@code null} if this post does not have
1454 * The text of the post
1455 * @return The created post
1457 public Post createPost(Sone sone, Sone recipient, String text) {
1458 return createPost(sone, recipient, System.currentTimeMillis(), text);
1462 * Creates a new post.
1465 * The Sone that creates the post
1467 * The recipient Sone, or {@code null} if this post does not have
1470 * The time of the post
1472 * The text of the post
1473 * @return The created post
1475 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1476 if (!isLocalSone(sone)) {
1477 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1480 Post post = new Post(sone, time, text);
1481 if (recipient != null) {
1482 post.setRecipient(recipient);
1484 synchronized (posts) {
1485 posts.put(post.getId(), post);
1487 synchronized (newPosts) {
1488 newPosts.add(post.getId());
1489 coreListenerManager.fireNewPostFound(post);
1497 * Deletes the given post.
1500 * The post to delete
1502 public void deletePost(Post post) {
1503 if (!isLocalSone(post.getSone())) {
1504 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1507 post.getSone().removePost(post);
1508 synchronized (posts) {
1509 posts.remove(post.getId());
1511 synchronized (newPosts) {
1512 markPostKnown(post);
1513 knownPosts.remove(post.getId());
1515 saveSone(post.getSone());
1519 * Marks the given post as known, if it is currently a new post (according
1520 * to {@link #isNewPost(String)}).
1523 * The post to mark as known
1525 public void markPostKnown(Post post) {
1526 synchronized (newPosts) {
1527 if (newPosts.remove(post.getId())) {
1528 knownPosts.add(post.getId());
1529 coreListenerManager.fireMarkPostKnown(post);
1530 saveConfiguration();
1536 * Bookmarks the given post.
1539 * The post to bookmark
1541 public void bookmark(Post post) {
1542 bookmarkPost(post.getId());
1546 * Bookmarks the post with the given ID.
1549 * The ID of the post to bookmark
1551 public void bookmarkPost(String id) {
1552 synchronized (bookmarkedPosts) {
1553 bookmarkedPosts.add(id);
1558 * Removes the given post from the bookmarks.
1561 * The post to unbookmark
1563 public void unbookmark(Post post) {
1564 unbookmarkPost(post.getId());
1568 * Removes the post with the given ID from the bookmarks.
1571 * The ID of the post to unbookmark
1573 public void unbookmarkPost(String id) {
1574 synchronized (bookmarkedPosts) {
1575 bookmarkedPosts.remove(id);
1580 * Creates a new reply.
1583 * The Sone that creates the reply
1585 * The post that this reply refers to
1587 * The text of the reply
1588 * @return The created reply
1590 public Reply createReply(Sone sone, Post post, String text) {
1591 return createReply(sone, post, System.currentTimeMillis(), text);
1595 * Creates a new reply.
1598 * The Sone that creates the reply
1600 * The post that this reply refers to
1602 * The time of the reply
1604 * The text of the reply
1605 * @return The created reply
1607 public Reply createReply(Sone sone, Post post, long time, String text) {
1608 if (!isLocalSone(sone)) {
1609 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1612 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1613 synchronized (replies) {
1614 replies.put(reply.getId(), reply);
1616 synchronized (newReplies) {
1617 newReplies.add(reply.getId());
1618 coreListenerManager.fireNewReplyFound(reply);
1620 sone.addReply(reply);
1626 * Deletes the given reply.
1629 * The reply to delete
1631 public void deleteReply(Reply reply) {
1632 Sone sone = reply.getSone();
1633 if (!isLocalSone(sone)) {
1634 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1637 synchronized (replies) {
1638 replies.remove(reply.getId());
1640 synchronized (newReplies) {
1641 markReplyKnown(reply);
1642 knownReplies.remove(reply.getId());
1644 sone.removeReply(reply);
1649 * Marks the given reply as known, if it is currently a new reply (according
1650 * to {@link #isNewReply(String)}).
1653 * The reply to mark as known
1655 public void markReplyKnown(Reply reply) {
1656 synchronized (newReplies) {
1657 if (newReplies.remove(reply.getId())) {
1658 knownReplies.add(reply.getId());
1659 coreListenerManager.fireMarkReplyKnown(reply);
1660 saveConfiguration();
1668 public void start() {
1669 loadConfiguration();
1670 updateChecker.addUpdateListener(this);
1671 updateChecker.start();
1677 public void stop() {
1678 synchronized (localSones) {
1679 for (SoneInserter soneInserter : soneInserters.values()) {
1680 soneInserter.stop();
1683 updateChecker.stop();
1684 updateChecker.removeUpdateListener(this);
1685 soneDownloader.stop();
1686 saveConfiguration();
1691 * Saves the current options.
1693 public void saveConfiguration() {
1694 synchronized (configuration) {
1695 if (storingConfiguration) {
1696 logger.log(Level.FINE, "Already storing configuration…");
1699 storingConfiguration = true;
1702 /* store the options first. */
1704 configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
1705 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1706 configuration.getIntValue("Option/PostsPerPage").setValue(options.getIntegerOption("PostsPerPage").getReal());
1707 configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
1708 configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
1709 configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
1710 configuration.getBooleanValue("Option/ActivateFcpInterface").setValue(options.getBooleanOption("ActivateFcpInterface").getReal());
1711 configuration.getIntValue("Option/FcpFullAccessRequired").setValue(options.getIntegerOption("FcpFullAccessRequired").getReal());
1712 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1713 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1714 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1716 /* save known Sones. */
1717 int soneCounter = 0;
1718 synchronized (newSones) {
1719 for (String knownSoneId : knownSones) {
1720 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1722 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1725 /* save known posts. */
1726 int postCounter = 0;
1727 synchronized (newPosts) {
1728 for (String knownPostId : knownPosts) {
1729 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1731 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1734 /* save known replies. */
1735 int replyCounter = 0;
1736 synchronized (newReplies) {
1737 for (String knownReplyId : knownReplies) {
1738 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1740 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1743 /* save bookmarked posts. */
1744 int bookmarkedPostCounter = 0;
1745 synchronized (bookmarkedPosts) {
1746 for (String bookmarkedPostId : bookmarkedPosts) {
1747 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
1750 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
1753 configuration.save();
1755 } catch (ConfigurationException ce1) {
1756 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1758 synchronized (configuration) {
1759 storingConfiguration = false;
1769 * Loads the configuration.
1771 @SuppressWarnings("unchecked")
1772 private void loadConfiguration() {
1773 /* create options. */
1774 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1777 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1778 SoneInserter.setInsertionDelay(newValue);
1782 options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(25));
1783 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75));
1784 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25));
1785 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
1786 options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, new OptionWatcher<Boolean>() {
1789 @SuppressWarnings("synthetic-access")
1790 public void optionChanged(Option<Boolean> option, Boolean oldValue, Boolean newValue) {
1791 fcpInterface.setActive(newValue);
1794 options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2, new OptionWatcher<Integer>() {
1797 @SuppressWarnings("synthetic-access")
1798 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1799 fcpInterface.setFullAccessRequired(FullAccessRequired.values()[newValue]);
1803 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1804 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1805 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1807 /* read options from configuration. */
1808 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1809 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1810 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1811 options.getBooleanOption("ClearOnNextRestart").set(null);
1812 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1813 if (clearConfiguration) {
1814 /* stop loading the configuration. */
1818 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1819 options.getIntegerOption("PostsPerPage").set(configuration.getIntValue("Option/PostsPerPage").getValue(null));
1820 options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
1821 options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
1822 options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
1823 options.getBooleanOption("ActivateFcpInterface").set(configuration.getBooleanValue("Option/ActivateFcpInterface").getValue(null));
1824 options.getIntegerOption("FcpFullAccessRequired").set(configuration.getIntValue("Option/FcpFullAccessRequired").getValue(null));
1825 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1827 /* load known Sones. */
1828 int soneCounter = 0;
1830 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1831 if (knownSoneId == null) {
1834 synchronized (newSones) {
1835 knownSones.add(knownSoneId);
1839 /* load known posts. */
1840 int postCounter = 0;
1842 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1843 if (knownPostId == null) {
1846 synchronized (newPosts) {
1847 knownPosts.add(knownPostId);
1851 /* load known replies. */
1852 int replyCounter = 0;
1854 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1855 if (knownReplyId == null) {
1858 synchronized (newReplies) {
1859 knownReplies.add(knownReplyId);
1863 /* load bookmarked posts. */
1864 int bookmarkedPostCounter = 0;
1866 String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null);
1867 if (bookmarkedPostId == null) {
1870 synchronized (bookmarkedPosts) {
1871 bookmarkedPosts.add(bookmarkedPostId);
1878 * Generate a Sone URI from the given URI and latest edition.
1881 * The URI to derive the Sone URI from
1882 * @return The derived URI
1884 private FreenetURI getSoneUri(String uriString) {
1886 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1888 } catch (MalformedURLException mue1) {
1889 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1895 // INTERFACE IdentityListener
1902 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1903 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1904 if (ownIdentity.hasContext("Sone")) {
1905 trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1906 addLocalSone(ownIdentity);
1914 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1915 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1916 trustedIdentities.remove(ownIdentity);
1923 public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1924 logger.log(Level.FINEST, "Adding Identity: " + identity);
1925 trustedIdentities.get(ownIdentity).add(identity);
1926 addRemoteSone(identity);
1933 public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1934 new Thread(new Runnable() {
1937 @SuppressWarnings("synthetic-access")
1939 Sone sone = getRemoteSone(identity.getId());
1940 sone.setIdentity(identity);
1941 soneDownloader.addSone(sone);
1942 soneDownloader.fetchSone(sone);
1951 public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1952 trustedIdentities.get(ownIdentity).remove(identity);
1956 // INTERFACE UpdateListener
1963 public void updateFound(Version version, long releaseTime, long latestEdition) {
1964 coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition);
1968 * Convenience interface for external classes that want to access the core’s
1971 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1973 public static class Preferences {
1975 /** The wrapped options. */
1976 private final Options options;
1979 * Creates a new preferences object wrapped around the given options.
1982 * The options to wrap
1984 public Preferences(Options options) {
1985 this.options = options;
1989 * Returns the insertion delay.
1991 * @return The insertion delay
1993 public int getInsertionDelay() {
1994 return options.getIntegerOption("InsertionDelay").get();
1998 * Sets the insertion delay
2000 * @param insertionDelay
2001 * The new insertion delay, or {@code null} to restore it to
2003 * @return This preferences
2005 public Preferences setInsertionDelay(Integer insertionDelay) {
2006 options.getIntegerOption("InsertionDelay").set(insertionDelay);
2011 * Returns the number of posts to show per page.
2013 * @return The number of posts to show per page
2015 public int getPostsPerPage() {
2016 return options.getIntegerOption("PostsPerPage").get();
2020 * Sets the number of posts to show per page.
2022 * @param postsPerPage
2023 * The number of posts to show per page
2024 * @return This preferences object
2026 public Preferences setPostsPerPage(Integer postsPerPage) {
2027 options.getIntegerOption("PostsPerPage").set(postsPerPage);
2032 * Returns the positive trust.
2034 * @return The positive trust
2036 public int getPositiveTrust() {
2037 return options.getIntegerOption("PositiveTrust").get();
2041 * Sets the positive trust.
2043 * @param positiveTrust
2044 * The new positive trust, or {@code null} to restore it to
2046 * @return This preferences
2048 public Preferences setPositiveTrust(Integer positiveTrust) {
2049 options.getIntegerOption("PositiveTrust").set(positiveTrust);
2054 * Returns the negative trust.
2056 * @return The negative trust
2058 public int getNegativeTrust() {
2059 return options.getIntegerOption("NegativeTrust").get();
2063 * Sets the negative trust.
2065 * @param negativeTrust
2066 * The negative trust, or {@code null} to restore it to the
2068 * @return The preferences
2070 public Preferences setNegativeTrust(Integer negativeTrust) {
2071 options.getIntegerOption("NegativeTrust").set(negativeTrust);
2076 * Returns the trust comment. This is the comment that is set in the web
2077 * of trust when a trust value is assigned to an identity.
2079 * @return The trust comment
2081 public String getTrustComment() {
2082 return options.getStringOption("TrustComment").get();
2086 * Sets the trust comment.
2088 * @param trustComment
2089 * The trust comment, or {@code null} to restore it to the
2091 * @return This preferences
2093 public Preferences setTrustComment(String trustComment) {
2094 options.getStringOption("TrustComment").set(trustComment);
2099 * Returns whether the {@link FcpInterface FCP interface} is currently
2102 * @see FcpInterface#setActive(boolean)
2103 * @return {@code true} if the FCP interface is currently active,
2104 * {@code false} otherwise
2106 public boolean isFcpInterfaceActive() {
2107 return options.getBooleanOption("ActivateFcpInterface").get();
2111 * Sets whether the {@link FcpInterface FCP interface} is currently
2114 * @see FcpInterface#setActive(boolean)
2115 * @param fcpInterfaceActive
2116 * {@code true} to activate the FCP interface, {@code false}
2117 * to deactivate the FCP interface
2118 * @return This preferences object
2120 public Preferences setFcpInterfaceActive(boolean fcpInterfaceActive) {
2121 options.getBooleanOption("ActivateFcpInterface").set(fcpInterfaceActive);
2126 * Returns the action level for which full access to the FCP interface
2129 * @return The action level for which full access to the FCP interface
2132 public FullAccessRequired getFcpFullAccessRequired() {
2133 return FullAccessRequired.values()[options.getIntegerOption("FcpFullAccessRequired").get()];
2137 * Sets the action level for which full access to the FCP interface is
2140 * @param fcpFullAccessRequired
2142 * @return This preferences
2144 public Preferences setFcpFullAccessRequired(FullAccessRequired fcpFullAccessRequired) {
2145 options.getIntegerOption("FcpFullAccessRequired").set((fcpFullAccessRequired != null) ? fcpFullAccessRequired.ordinal() : null);
2150 * Returns whether the rescue mode is active.
2152 * @return {@code true} if the rescue mode is active, {@code false}
2155 public boolean isSoneRescueMode() {
2156 return options.getBooleanOption("SoneRescueMode").get();
2160 * Sets whether the rescue mode is active.
2162 * @param soneRescueMode
2163 * {@code true} if the rescue mode is active, {@code false}
2165 * @return This preferences
2167 public Preferences setSoneRescueMode(Boolean soneRescueMode) {
2168 options.getBooleanOption("SoneRescueMode").set(soneRescueMode);
2173 * Returns whether Sone should clear its settings on the next restart.
2174 * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
2175 * to return {@code true} as well!
2177 * @return {@code true} if Sone should clear its settings on the next
2178 * restart, {@code false} otherwise
2180 public boolean isClearOnNextRestart() {
2181 return options.getBooleanOption("ClearOnNextRestart").get();
2185 * Sets whether Sone will clear its settings on the next restart.
2187 * @param clearOnNextRestart
2188 * {@code true} if Sone should clear its settings on the next
2189 * restart, {@code false} otherwise
2190 * @return This preferences
2192 public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
2193 options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
2198 * Returns whether Sone should really clear its settings on next
2199 * restart. This is a confirmation option that needs to be set in
2200 * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
2201 * settings on the next restart.
2203 * @return {@code true} if Sone should really clear its settings on the
2204 * next restart, {@code false} otherwise
2206 public boolean isReallyClearOnNextRestart() {
2207 return options.getBooleanOption("ReallyClearOnNextRestart").get();
2211 * Sets whether Sone should really clear its settings on the next
2214 * @param reallyClearOnNextRestart
2215 * {@code true} if Sone should really clear its settings on
2216 * the next restart, {@code false} otherwise
2217 * @return This preferences
2219 public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
2220 options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);