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.Map.Entry;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
34 import net.pterodactylus.sone.core.Options.DefaultOption;
35 import net.pterodactylus.sone.core.Options.Option;
36 import net.pterodactylus.sone.core.Options.OptionWatcher;
37 import net.pterodactylus.sone.data.Client;
38 import net.pterodactylus.sone.data.Post;
39 import net.pterodactylus.sone.data.Profile;
40 import net.pterodactylus.sone.data.Profile.Field;
41 import net.pterodactylus.sone.data.Reply;
42 import net.pterodactylus.sone.data.Sone;
43 import net.pterodactylus.sone.freenet.wot.Identity;
44 import net.pterodactylus.sone.freenet.wot.IdentityListener;
45 import net.pterodactylus.sone.freenet.wot.IdentityManager;
46 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
47 import net.pterodactylus.sone.freenet.wot.Trust;
48 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
49 import net.pterodactylus.sone.main.SonePlugin;
50 import net.pterodactylus.util.config.Configuration;
51 import net.pterodactylus.util.config.ConfigurationException;
52 import net.pterodactylus.util.logging.Logging;
53 import net.pterodactylus.util.number.Numbers;
54 import net.pterodactylus.util.validation.Validation;
55 import net.pterodactylus.util.version.Version;
56 import freenet.keys.FreenetURI;
61 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
63 public class Core implements IdentityListener, UpdateListener {
66 * Enumeration for the possible states of a {@link Sone}.
68 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
70 public enum SoneStatus {
72 /** The Sone is unknown, i.e. not yet downloaded. */
75 /** The Sone is idle, i.e. not being downloaded or inserted. */
78 /** The Sone is currently being inserted. */
81 /** The Sone is currently being downloaded. */
86 private static final Logger logger = Logging.getLogger(Core.class);
89 private final Options options = new Options();
91 /** The preferences. */
92 private final Preferences preferences = new Preferences(options);
94 /** The core listener manager. */
95 private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
97 /** The configuration. */
98 private Configuration configuration;
100 /** Whether we’re currently saving the configuration. */
101 private boolean storingConfiguration = false;
103 /** The identity manager. */
104 private final IdentityManager identityManager;
106 /** Interface to freenet. */
107 private final FreenetInterface freenetInterface;
109 /** The Sone downloader. */
110 private final SoneDownloader soneDownloader;
112 /** Sone downloader thread-pool. */
113 private final ExecutorService soneDownloaders = Executors.newFixedThreadPool(10);
115 /** The update checker. */
116 private final UpdateChecker updateChecker;
118 /** Whether the core has been stopped. */
119 private volatile boolean stopped;
121 /** The Sones’ statuses. */
122 /* synchronize access on itself. */
123 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
125 /** Locked local Sones. */
126 /* synchronize on itself. */
127 private final Set<Sone> lockedSones = new HashSet<Sone>();
129 /** Sone inserters. */
130 /* synchronize access on this on localSones. */
131 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
133 /** All local Sones. */
134 /* synchronize access on this on itself. */
135 private Map<String, Sone> localSones = new HashMap<String, Sone>();
137 /** All remote Sones. */
138 /* synchronize access on this on itself. */
139 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
141 /** All new Sones. */
142 private Set<String> newSones = new HashSet<String>();
144 /** All known Sones. */
145 /* synchronize access on {@link #newSones}. */
146 private Set<String> knownSones = new HashSet<String>();
149 private Map<String, Post> posts = new HashMap<String, Post>();
151 /** All new posts. */
152 private Set<String> newPosts = new HashSet<String>();
154 /** All known posts. */
155 /* synchronize access on {@link #newPosts}. */
156 private Set<String> knownPosts = new HashSet<String>();
159 private Map<String, Reply> replies = new HashMap<String, Reply>();
161 /** All new replies. */
162 private Set<String> newReplies = new HashSet<String>();
164 /** All known replies. */
165 private Set<String> knownReplies = new HashSet<String>();
167 /** All bookmarked posts. */
168 /* synchronize access on itself. */
169 private Set<String> bookmarkedPosts = new HashSet<String>();
171 /** Trusted identities, sorted by own identities. */
172 private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
175 * Creates a new core.
177 * @param configuration
178 * The configuration of the core
179 * @param freenetInterface
180 * The freenet interface
181 * @param identityManager
182 * The identity manager
184 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
185 this.configuration = configuration;
186 this.freenetInterface = freenetInterface;
187 this.identityManager = identityManager;
188 this.soneDownloader = new SoneDownloader(this, freenetInterface);
189 this.updateChecker = new UpdateChecker(freenetInterface);
193 // LISTENER MANAGEMENT
197 * Adds a new core listener.
199 * @param coreListener
200 * The listener to add
202 public void addCoreListener(CoreListener coreListener) {
203 coreListenerManager.addListener(coreListener);
207 * Removes a core listener.
209 * @param coreListener
210 * The listener to remove
212 public void removeCoreListener(CoreListener coreListener) {
213 coreListenerManager.removeListener(coreListener);
221 * Sets the configuration to use. This will automatically save the current
222 * configuration to the given configuration.
224 * @param configuration
225 * The new configuration to use
227 public void setConfiguration(Configuration configuration) {
228 this.configuration = configuration;
233 * Returns the options used by the core.
235 * @return The options of the core
237 public Preferences getPreferences() {
242 * Returns the identity manager used by the core.
244 * @return The identity manager
246 public IdentityManager getIdentityManager() {
247 return identityManager;
251 * Returns the update checker.
253 * @return The update checker
255 public UpdateChecker getUpdateChecker() {
256 return updateChecker;
260 * Returns the status of the given Sone.
263 * The Sone to get the status for
264 * @return The status of the Sone
266 public SoneStatus getSoneStatus(Sone sone) {
267 synchronized (soneStatuses) {
268 return soneStatuses.get(sone);
273 * Sets the status of the given Sone.
276 * The Sone to set the status of
280 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
281 synchronized (soneStatuses) {
282 soneStatuses.put(sone, soneStatus);
287 * Returns whether the given Sone is currently locked.
291 * @return {@code true} if the Sone is locked, {@code false} if it is not
293 public boolean isLocked(Sone sone) {
294 synchronized (lockedSones) {
295 return lockedSones.contains(sone);
300 * Returns all Sones, remote and local.
304 public Set<Sone> getSones() {
305 Set<Sone> allSones = new HashSet<Sone>();
306 allSones.addAll(getLocalSones());
307 allSones.addAll(getRemoteSones());
312 * Returns the Sone with the given ID, regardless whether it’s local or
316 * The ID of the Sone to get
317 * @return The Sone with the given ID, or {@code null} if there is no such
320 public Sone getSone(String id) {
321 return getSone(id, true);
325 * Returns the Sone with the given ID, regardless whether it’s local or
329 * The ID of the Sone to get
331 * {@code true} to create a new Sone if none exists,
332 * {@code false} to return {@code null} if a Sone with the given
334 * @return The Sone with the given ID, or {@code null} if there is no such
337 public Sone getSone(String id, boolean create) {
338 if (isLocalSone(id)) {
339 return getLocalSone(id);
341 return getRemoteSone(id, create);
345 * Checks whether the core knows a Sone with the given ID.
349 * @return {@code true} if there is a Sone with the given ID, {@code false}
352 public boolean hasSone(String id) {
353 return isLocalSone(id) || isRemoteSone(id);
357 * Returns whether the given Sone is a local Sone.
360 * The Sone to check for its locality
361 * @return {@code true} if the given Sone is local, {@code false} otherwise
363 public boolean isLocalSone(Sone sone) {
364 synchronized (localSones) {
365 return localSones.containsKey(sone.getId());
370 * Returns whether the given ID is the ID of a local Sone.
373 * The Sone ID to check for its locality
374 * @return {@code true} if the given ID is a local Sone, {@code false}
377 public boolean isLocalSone(String id) {
378 synchronized (localSones) {
379 return localSones.containsKey(id);
384 * Returns all local Sones.
386 * @return All local Sones
388 public Set<Sone> getLocalSones() {
389 synchronized (localSones) {
390 return new HashSet<Sone>(localSones.values());
395 * Returns the local Sone with the given ID.
398 * The ID of the Sone to get
399 * @return The Sone with the given ID
401 public Sone getLocalSone(String id) {
402 return getLocalSone(id, true);
406 * Returns the local Sone with the given ID, optionally creating a new Sone.
411 * {@code true} to create a new Sone if none exists,
412 * {@code false} to return null if none exists
413 * @return The Sone with the given ID, or {@code null}
415 public Sone getLocalSone(String id, boolean create) {
416 synchronized (localSones) {
417 Sone sone = localSones.get(id);
418 if ((sone == null) && create) {
420 localSones.put(id, sone);
421 setSoneStatus(sone, SoneStatus.unknown);
428 * Returns all remote Sones.
430 * @return All remote Sones
432 public Set<Sone> getRemoteSones() {
433 synchronized (remoteSones) {
434 return new HashSet<Sone>(remoteSones.values());
439 * Returns the remote Sone with the given ID.
442 * The ID of the remote Sone to get
443 * @return The Sone with the given ID
445 public Sone getRemoteSone(String id) {
446 return getRemoteSone(id, true);
450 * Returns the remote Sone with the given ID.
453 * The ID of the remote Sone to get
455 * {@code true} to always create a Sone, {@code false} to return
456 * {@code null} if no Sone with the given ID exists
457 * @return The Sone with the given ID
459 public Sone getRemoteSone(String id, boolean create) {
460 synchronized (remoteSones) {
461 Sone sone = remoteSones.get(id);
462 if ((sone == null) && create) {
464 remoteSones.put(id, sone);
465 setSoneStatus(sone, SoneStatus.unknown);
472 * Returns whether the given Sone is a remote Sone.
476 * @return {@code true} if the given Sone is a remote Sone, {@code false}
479 public boolean isRemoteSone(Sone sone) {
480 synchronized (remoteSones) {
481 return remoteSones.containsKey(sone.getId());
486 * Returns whether the Sone with the given ID is a remote Sone.
489 * The ID of the Sone to check
490 * @return {@code true} if the Sone with the given ID is a remote Sone,
491 * {@code false} otherwise
493 public boolean isRemoteSone(String id) {
494 synchronized (remoteSones) {
495 return remoteSones.containsKey(id);
500 * Returns whether the Sone with the given ID is a new Sone.
503 * The ID of the sone to check for
504 * @return {@code true} if the given Sone is new, false otherwise
506 public boolean isNewSone(String soneId) {
507 synchronized (newSones) {
508 return !knownSones.contains(soneId) && newSones.contains(soneId);
513 * Returns whether the given Sone has been modified.
516 * The Sone to check for modifications
517 * @return {@code true} if a modification has been detected in the Sone,
518 * {@code false} otherwise
520 public boolean isModifiedSone(Sone sone) {
521 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
525 * Returns whether the target Sone is trusted by the origin Sone.
531 * @return {@code true} if the target Sone is trusted by the origin Sone
533 public boolean isSoneTrusted(Sone origin, Sone target) {
534 Validation.begin().isNotNull("Origin", origin).isNotNull("Target", target).check().isInstanceOf("Origin’s OwnIdentity", origin.getIdentity(), OwnIdentity.class).check();
535 return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity());
539 * Returns the post with the given ID.
542 * The ID of the post to get
543 * @return The post with the given ID, or a new post with the given ID
545 public Post getPost(String postId) {
546 return getPost(postId, true);
550 * Returns the post with the given ID, optionally creating a new post.
553 * The ID of the post to get
555 * {@code true} it create a new post if no post with the given ID
556 * exists, {@code false} to return {@code null}
557 * @return The post, or {@code null} if there is no such post
559 public Post getPost(String postId, boolean create) {
560 synchronized (posts) {
561 Post post = posts.get(postId);
562 if ((post == null) && create) {
563 post = new Post(postId);
564 posts.put(postId, post);
571 * Returns whether the given post ID is new.
575 * @return {@code true} if the post is considered to be new, {@code false}
578 public boolean isNewPost(String postId) {
579 synchronized (newPosts) {
580 return !knownPosts.contains(postId) && newPosts.contains(postId);
585 * Returns all posts that have the given Sone as recipient.
587 * @see Post#getRecipient()
589 * The recipient of the posts
590 * @return All posts that have the given Sone as recipient
592 public Set<Post> getDirectedPosts(Sone recipient) {
593 Validation.begin().isNotNull("Recipient", recipient).check();
594 Set<Post> directedPosts = new HashSet<Post>();
595 synchronized (posts) {
596 for (Post post : posts.values()) {
597 if (recipient.equals(post.getRecipient())) {
598 directedPosts.add(post);
602 return directedPosts;
606 * Returns the reply with the given ID. If there is no reply with the given
607 * ID yet, a new one is created.
610 * The ID of the reply to get
613 public Reply getReply(String replyId) {
614 return getReply(replyId, true);
618 * Returns the reply with the given ID. If there is no reply with the given
619 * ID yet, a new one is created, unless {@code create} is false in which
620 * case {@code null} is returned.
623 * The ID of the reply to get
625 * {@code true} to always return a {@link Reply}, {@code false}
626 * to return {@code null} if no reply can be found
627 * @return The reply, or {@code null} if there is no such reply
629 public Reply getReply(String replyId, boolean create) {
630 synchronized (replies) {
631 Reply reply = replies.get(replyId);
632 if (create && (reply == null)) {
633 reply = new Reply(replyId);
634 replies.put(replyId, reply);
641 * Returns all replies for the given post, order ascending by time.
644 * The post to get all replies for
645 * @return All replies for the given post
647 public List<Reply> getReplies(Post post) {
648 Set<Sone> sones = getSones();
649 List<Reply> replies = new ArrayList<Reply>();
650 for (Sone sone : sones) {
651 for (Reply reply : sone.getReplies()) {
652 if (reply.getPost().equals(post)) {
657 Collections.sort(replies, Reply.TIME_COMPARATOR);
662 * Returns whether the reply with the given ID is new.
665 * The ID of the reply to check
666 * @return {@code true} if the reply is considered to be new, {@code false}
669 public boolean isNewReply(String replyId) {
670 synchronized (newReplies) {
671 return !knownReplies.contains(replyId) && newReplies.contains(replyId);
676 * Returns all Sones that have liked the given post.
679 * The post to get the liking Sones for
680 * @return The Sones that like the given post
682 public Set<Sone> getLikes(Post post) {
683 Set<Sone> sones = new HashSet<Sone>();
684 for (Sone sone : getSones()) {
685 if (sone.getLikedPostIds().contains(post.getId())) {
693 * Returns all Sones that have liked the given reply.
696 * The reply to get the liking Sones for
697 * @return The Sones that like the given reply
699 public Set<Sone> getLikes(Reply reply) {
700 Set<Sone> sones = new HashSet<Sone>();
701 for (Sone sone : getSones()) {
702 if (sone.getLikedReplyIds().contains(reply.getId())) {
710 * Returns whether the given post is bookmarked.
714 * @return {@code true} if the given post is bookmarked, {@code false}
717 public boolean isBookmarked(Post post) {
718 return isPostBookmarked(post.getId());
722 * Returns whether the post with the given ID is bookmarked.
725 * The ID of the post to check
726 * @return {@code true} if the post with the given ID is bookmarked,
727 * {@code false} otherwise
729 public boolean isPostBookmarked(String id) {
730 synchronized (bookmarkedPosts) {
731 return bookmarkedPosts.contains(id);
736 * Returns all currently known bookmarked posts.
738 * @return All bookmarked posts
740 public Set<Post> getBookmarkedPosts() {
741 Set<Post> posts = new HashSet<Post>();
742 synchronized (bookmarkedPosts) {
743 for (String bookmarkedPostId : bookmarkedPosts) {
744 Post post = getPost(bookmarkedPostId, false);
758 * Locks the given Sone. A locked Sone will not be inserted by
759 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
765 public void lockSone(Sone sone) {
766 synchronized (lockedSones) {
767 if (lockedSones.add(sone)) {
768 coreListenerManager.fireSoneLocked(sone);
774 * Unlocks the given Sone.
776 * @see #lockSone(Sone)
780 public void unlockSone(Sone sone) {
781 synchronized (lockedSones) {
782 if (lockedSones.remove(sone)) {
783 coreListenerManager.fireSoneUnlocked(sone);
789 * Adds a local Sone from the given ID which has to be the ID of an own
793 * The ID of an own identity to add a Sone for
794 * @return The added (or already existing) Sone
796 public Sone addLocalSone(String id) {
797 synchronized (localSones) {
798 if (localSones.containsKey(id)) {
799 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
800 return localSones.get(id);
802 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
803 if (ownIdentity == null) {
804 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
807 return addLocalSone(ownIdentity);
812 * Adds a local Sone from the given own identity.
815 * The own identity to create a Sone from
816 * @return The added (or already existing) Sone
818 public Sone addLocalSone(OwnIdentity ownIdentity) {
819 if (ownIdentity == null) {
820 logger.log(Level.WARNING, "Given OwnIdentity is null!");
823 synchronized (localSones) {
826 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
827 } catch (MalformedURLException mue1) {
828 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
831 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
832 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
833 /* TODO - load posts ’n stuff */
834 localSones.put(ownIdentity.getId(), sone);
835 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
836 soneInserters.put(sone, soneInserter);
837 setSoneStatus(sone, SoneStatus.idle);
839 if (!preferences.isSoneRescueMode()) {
840 soneInserter.start();
842 new Thread(new Runnable() {
845 @SuppressWarnings("synthetic-access")
847 if (!preferences.isSoneRescueMode()) {
850 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
851 coreListenerManager.fireRescuingSone(sone);
853 long edition = sone.getLatestEdition();
854 while (!stopped && (edition >= 0) && preferences.isSoneRescueMode()) {
855 logger.log(Level.FINE, "Downloading edition " + edition + "…");
856 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
859 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
861 coreListenerManager.fireRescuedSone(sone);
862 soneInserter.start();
865 }, "Sone Downloader").start();
871 * Creates a new Sone for the given own identity.
874 * The own identity to create a Sone for
875 * @return The created Sone
877 public Sone createSone(OwnIdentity ownIdentity) {
879 ownIdentity.addContext("Sone");
880 } catch (WebOfTrustException wote1) {
881 logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
884 Sone sone = addLocalSone(ownIdentity);
885 sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
886 sone.addFriend("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
892 * Adds the Sone of the given identity.
895 * The identity whose Sone to add
896 * @return The added or already existing Sone
898 public Sone addRemoteSone(Identity identity) {
899 if (identity == null) {
900 logger.log(Level.WARNING, "Given Identity is null!");
903 synchronized (remoteSones) {
904 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
905 boolean newSone = sone.getRequestUri() == null;
906 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
907 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
909 synchronized (newSones) {
910 newSone = !knownSones.contains(sone.getId());
912 newSones.add(sone.getId());
916 coreListenerManager.fireNewSoneFound(sone);
917 for (Sone localSone : getLocalSones()) {
918 if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
919 localSone.addFriend(sone.getId());
924 remoteSones.put(identity.getId(), sone);
925 soneDownloader.addSone(sone);
926 setSoneStatus(sone, SoneStatus.unknown);
927 soneDownloaders.execute(new Runnable() {
930 @SuppressWarnings("synthetic-access")
932 soneDownloader.fetchSone(sone, sone.getRequestUri());
941 * Retrieves the trust relationship from the origin to the target. If the
942 * trust relationship can not be retrieved, {@code null} is returned.
944 * @see Identity#getTrust(OwnIdentity)
946 * The origin of the trust tree
948 * The target of the trust
949 * @return The trust relationship
951 public Trust getTrust(Sone origin, Sone target) {
952 if (!isLocalSone(origin)) {
953 logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
956 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
960 * Sets the trust value of the given origin Sone for the target Sone.
967 * The trust value (from {@code -100} to {@code 100})
969 public void setTrust(Sone origin, Sone target, int trustValue) {
970 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();
972 ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
973 } catch (WebOfTrustException wote1) {
974 logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
979 * Removes any trust assignment for the given target Sone.
986 public void removeTrust(Sone origin, Sone target) {
987 Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
989 ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
990 } catch (WebOfTrustException wote1) {
991 logger.log(Level.WARNING, "Could not remove trust for Sone: " + target, wote1);
996 * Assigns the configured positive trust value for the given target.
1003 public void trustSone(Sone origin, Sone target) {
1004 setTrust(origin, target, preferences.getPositiveTrust());
1008 * Assigns the configured negative trust value for the given target.
1015 public void distrustSone(Sone origin, Sone target) {
1016 setTrust(origin, target, preferences.getNegativeTrust());
1020 * Removes the trust assignment for the given target.
1027 public void untrustSone(Sone origin, Sone target) {
1028 removeTrust(origin, target);
1032 * Updates the stores Sone with the given Sone.
1037 public void updateSone(Sone sone) {
1038 if (hasSone(sone.getId())) {
1039 boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode();
1040 Sone storedSone = getSone(sone.getId());
1041 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
1042 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
1045 synchronized (posts) {
1046 if (!soneRescueMode) {
1047 for (Post post : storedSone.getPosts()) {
1048 posts.remove(post.getId());
1049 if (!sone.getPosts().contains(post)) {
1050 coreListenerManager.firePostRemoved(post);
1054 List<Post> storedPosts = storedSone.getPosts();
1055 synchronized (newPosts) {
1056 for (Post post : sone.getPosts()) {
1057 post.setSone(storedSone);
1058 if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
1059 newPosts.add(post.getId());
1060 coreListenerManager.fireNewPostFound(post);
1062 posts.put(post.getId(), post);
1066 synchronized (replies) {
1067 if (!soneRescueMode) {
1068 for (Reply reply : storedSone.getReplies()) {
1069 replies.remove(reply.getId());
1070 if (!sone.getReplies().contains(reply)) {
1071 coreListenerManager.fireReplyRemoved(reply);
1075 Set<Reply> storedReplies = storedSone.getReplies();
1076 synchronized (newReplies) {
1077 for (Reply reply : sone.getReplies()) {
1078 reply.setSone(storedSone);
1079 if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
1080 newReplies.add(reply.getId());
1081 coreListenerManager.fireNewReplyFound(reply);
1083 replies.put(reply.getId(), reply);
1087 synchronized (storedSone) {
1088 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
1089 storedSone.setTime(sone.getTime());
1091 storedSone.setClient(sone.getClient());
1092 storedSone.setProfile(sone.getProfile());
1093 if (soneRescueMode) {
1094 for (Post post : sone.getPosts()) {
1095 storedSone.addPost(post);
1097 for (Reply reply : sone.getReplies()) {
1098 storedSone.addReply(reply);
1100 for (String likedPostId : sone.getLikedPostIds()) {
1101 storedSone.addLikedPostId(likedPostId);
1103 for (String likedReplyId : sone.getLikedReplyIds()) {
1104 storedSone.addLikedReplyId(likedReplyId);
1107 storedSone.setPosts(sone.getPosts());
1108 storedSone.setReplies(sone.getReplies());
1109 storedSone.setLikePostIds(sone.getLikedPostIds());
1110 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
1112 storedSone.setLatestEdition(sone.getLatestEdition());
1118 * Deletes the given Sone. This will remove the Sone from the
1119 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1120 * and remove the context from its identity.
1123 * The Sone to delete
1125 public void deleteSone(Sone sone) {
1126 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1127 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1130 synchronized (localSones) {
1131 if (!localSones.containsKey(sone.getId())) {
1132 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1135 localSones.remove(sone.getId());
1136 soneInserters.remove(sone).stop();
1139 ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1140 ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1141 } catch (WebOfTrustException wote1) {
1142 logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1145 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1146 } catch (ConfigurationException ce1) {
1147 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1152 * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
1153 * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
1156 * The Sone to mark as known
1158 public void markSoneKnown(Sone sone) {
1159 synchronized (newSones) {
1160 if (newSones.remove(sone.getId())) {
1161 knownSones.add(sone.getId());
1162 coreListenerManager.fireMarkSoneKnown(sone);
1163 saveConfiguration();
1169 * Loads and updates the given Sone from the configuration. If any error is
1170 * encountered, loading is aborted and the given Sone is not changed.
1173 * The Sone to load and update
1175 public void loadSone(Sone sone) {
1176 if (!isLocalSone(sone)) {
1177 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1181 /* initialize options. */
1182 sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
1185 String sonePrefix = "Sone/" + sone.getId();
1186 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1187 if (soneTime == null) {
1188 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1191 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1194 Profile profile = new Profile();
1195 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1196 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1197 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1198 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1199 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1200 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1202 /* load profile fields. */
1204 String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
1205 String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
1206 if (fieldName == null) {
1209 String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
1210 profile.addField(fieldName).setValue(fieldValue);
1214 Set<Post> posts = new HashSet<Post>();
1216 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1217 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1218 if (postId == null) {
1221 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1222 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1223 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1224 if ((postTime == 0) || (postText == null)) {
1225 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1228 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1229 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1230 post.setRecipient(getSone(postRecipientId));
1236 Set<Reply> replies = new HashSet<Reply>();
1238 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1239 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1240 if (replyId == null) {
1243 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1244 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1245 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1246 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1247 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1250 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1253 /* load post likes. */
1254 Set<String> likedPostIds = new HashSet<String>();
1256 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1257 if (likedPostId == null) {
1260 likedPostIds.add(likedPostId);
1263 /* load reply likes. */
1264 Set<String> likedReplyIds = new HashSet<String>();
1266 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1267 if (likedReplyId == null) {
1270 likedReplyIds.add(likedReplyId);
1274 Set<String> friends = new HashSet<String>();
1276 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1277 if (friendId == null) {
1280 friends.add(friendId);
1284 sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
1286 /* if we’re still here, Sone was loaded successfully. */
1287 synchronized (sone) {
1288 sone.setTime(soneTime);
1289 sone.setProfile(profile);
1290 sone.setPosts(posts);
1291 sone.setReplies(replies);
1292 sone.setLikePostIds(likedPostIds);
1293 sone.setLikeReplyIds(likedReplyIds);
1294 sone.setFriends(friends);
1295 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1297 synchronized (newSones) {
1298 for (String friend : friends) {
1299 knownSones.add(friend);
1302 synchronized (newPosts) {
1303 for (Post post : posts) {
1304 knownPosts.add(post.getId());
1307 synchronized (newReplies) {
1308 for (Reply reply : replies) {
1309 knownReplies.add(reply.getId());
1315 * Saves the given Sone. This will persist all local settings for the given
1316 * Sone, such as the friends list and similar, private options.
1321 public synchronized void saveSone(Sone sone) {
1322 if (!isLocalSone(sone)) {
1323 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1326 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1327 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1331 logger.log(Level.INFO, "Saving Sone: %s", sone);
1333 ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1335 /* save Sone into configuration. */
1336 String sonePrefix = "Sone/" + sone.getId();
1337 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1338 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1341 Profile profile = sone.getProfile();
1342 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1343 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1344 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1345 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1346 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1347 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1349 /* save profile fields. */
1350 int fieldCounter = 0;
1351 for (Field profileField : profile.getFields()) {
1352 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1353 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1354 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1356 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1359 int postCounter = 0;
1360 for (Post post : sone.getPosts()) {
1361 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1362 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1363 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1364 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1365 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1367 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1370 int replyCounter = 0;
1371 for (Reply reply : sone.getReplies()) {
1372 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1373 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1374 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1375 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1376 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1378 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1380 /* save post likes. */
1381 int postLikeCounter = 0;
1382 for (String postId : sone.getLikedPostIds()) {
1383 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1385 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1387 /* save reply likes. */
1388 int replyLikeCounter = 0;
1389 for (String replyId : sone.getLikedReplyIds()) {
1390 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1392 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1395 int friendCounter = 0;
1396 for (String friendId : sone.getFriends()) {
1397 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1399 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1402 configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
1404 configuration.save();
1405 logger.log(Level.INFO, "Sone %s saved.", sone);
1406 } catch (ConfigurationException ce1) {
1407 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1408 } catch (WebOfTrustException wote1) {
1409 logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1414 * Creates a new post.
1417 * The Sone that creates the post
1419 * The text of the post
1420 * @return The created post
1422 public Post createPost(Sone sone, String text) {
1423 return createPost(sone, System.currentTimeMillis(), text);
1427 * Creates a new post.
1430 * The Sone that creates the post
1432 * The time of the post
1434 * The text of the post
1435 * @return The created post
1437 public Post createPost(Sone sone, long time, String text) {
1438 return createPost(sone, null, time, text);
1442 * Creates a new post.
1445 * The Sone that creates the post
1447 * The recipient Sone, or {@code null} if this post does not have
1450 * The text of the post
1451 * @return The created post
1453 public Post createPost(Sone sone, Sone recipient, String text) {
1454 return createPost(sone, recipient, System.currentTimeMillis(), text);
1458 * Creates a new post.
1461 * The Sone that creates the post
1463 * The recipient Sone, or {@code null} if this post does not have
1466 * The time of the post
1468 * The text of the post
1469 * @return The created post
1471 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1472 if (!isLocalSone(sone)) {
1473 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1476 Post post = new Post(sone, time, text);
1477 if (recipient != null) {
1478 post.setRecipient(recipient);
1480 synchronized (posts) {
1481 posts.put(post.getId(), post);
1483 synchronized (newPosts) {
1484 newPosts.add(post.getId());
1485 coreListenerManager.fireNewPostFound(post);
1493 * Deletes the given post.
1496 * The post to delete
1498 public void deletePost(Post post) {
1499 if (!isLocalSone(post.getSone())) {
1500 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1503 post.getSone().removePost(post);
1504 synchronized (posts) {
1505 posts.remove(post.getId());
1507 coreListenerManager.firePostRemoved(post);
1508 synchronized (newPosts) {
1509 markPostKnown(post);
1510 knownPosts.remove(post.getId());
1512 saveSone(post.getSone());
1516 * Marks the given post as known, if it is currently a new post (according
1517 * to {@link #isNewPost(String)}).
1520 * The post to mark as known
1522 public void markPostKnown(Post post) {
1523 synchronized (newPosts) {
1524 if (newPosts.remove(post.getId())) {
1525 knownPosts.add(post.getId());
1526 coreListenerManager.fireMarkPostKnown(post);
1527 saveConfiguration();
1533 * Bookmarks the given post.
1536 * The post to bookmark
1538 public void bookmark(Post post) {
1539 bookmarkPost(post.getId());
1543 * Bookmarks the post with the given ID.
1546 * The ID of the post to bookmark
1548 public void bookmarkPost(String id) {
1549 synchronized (bookmarkedPosts) {
1550 bookmarkedPosts.add(id);
1555 * Removes the given post from the bookmarks.
1558 * The post to unbookmark
1560 public void unbookmark(Post post) {
1561 unbookmarkPost(post.getId());
1565 * Removes the post with the given ID from the bookmarks.
1568 * The ID of the post to unbookmark
1570 public void unbookmarkPost(String id) {
1571 synchronized (bookmarkedPosts) {
1572 bookmarkedPosts.remove(id);
1577 * Creates a new reply.
1580 * The Sone that creates the reply
1582 * The post that this reply refers to
1584 * The text of the reply
1585 * @return The created reply
1587 public Reply createReply(Sone sone, Post post, String text) {
1588 return createReply(sone, post, System.currentTimeMillis(), text);
1592 * Creates a new reply.
1595 * The Sone that creates the reply
1597 * The post that this reply refers to
1599 * The time of the reply
1601 * The text of the reply
1602 * @return The created reply
1604 public Reply createReply(Sone sone, Post post, long time, String text) {
1605 if (!isLocalSone(sone)) {
1606 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1609 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1610 synchronized (replies) {
1611 replies.put(reply.getId(), reply);
1613 synchronized (newReplies) {
1614 newReplies.add(reply.getId());
1615 coreListenerManager.fireNewReplyFound(reply);
1617 sone.addReply(reply);
1623 * Deletes the given reply.
1626 * The reply to delete
1628 public void deleteReply(Reply reply) {
1629 Sone sone = reply.getSone();
1630 if (!isLocalSone(sone)) {
1631 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1634 synchronized (replies) {
1635 replies.remove(reply.getId());
1637 synchronized (newReplies) {
1638 markReplyKnown(reply);
1639 knownReplies.remove(reply.getId());
1641 sone.removeReply(reply);
1646 * Marks the given reply as known, if it is currently a new reply (according
1647 * to {@link #isNewReply(String)}).
1650 * The reply to mark as known
1652 public void markReplyKnown(Reply reply) {
1653 synchronized (newReplies) {
1654 if (newReplies.remove(reply.getId())) {
1655 knownReplies.add(reply.getId());
1656 coreListenerManager.fireMarkReplyKnown(reply);
1657 saveConfiguration();
1665 public void start() {
1666 loadConfiguration();
1667 updateChecker.addUpdateListener(this);
1668 updateChecker.start();
1674 public void stop() {
1675 synchronized (localSones) {
1676 for (SoneInserter soneInserter : soneInserters.values()) {
1677 soneInserter.stop();
1680 updateChecker.stop();
1681 updateChecker.removeUpdateListener(this);
1682 soneDownloader.stop();
1683 saveConfiguration();
1688 * Saves the current options.
1690 public void saveConfiguration() {
1691 synchronized (configuration) {
1692 if (storingConfiguration) {
1693 logger.log(Level.FINE, "Already storing configuration…");
1696 storingConfiguration = true;
1699 /* store the options first. */
1701 configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
1702 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1703 configuration.getIntValue("Option/PostsPerPage").setValue(options.getIntegerOption("PostsPerPage").getReal());
1704 configuration.getBooleanValue("Option/RequireFullAccess").setValue(options.getBooleanOption("RequireFullAccess").getReal());
1705 configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
1706 configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
1707 configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
1708 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1709 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1710 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1712 /* save known Sones. */
1713 int soneCounter = 0;
1714 synchronized (newSones) {
1715 for (String knownSoneId : knownSones) {
1716 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1718 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1721 /* save known posts. */
1722 int postCounter = 0;
1723 synchronized (newPosts) {
1724 for (String knownPostId : knownPosts) {
1725 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1727 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1730 /* save known replies. */
1731 int replyCounter = 0;
1732 synchronized (newReplies) {
1733 for (String knownReplyId : knownReplies) {
1734 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1736 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1739 /* save bookmarked posts. */
1740 int bookmarkedPostCounter = 0;
1741 synchronized (bookmarkedPosts) {
1742 for (String bookmarkedPostId : bookmarkedPosts) {
1743 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
1746 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
1749 configuration.save();
1751 } catch (ConfigurationException ce1) {
1752 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1754 synchronized (configuration) {
1755 storingConfiguration = false;
1765 * Loads the configuration.
1767 @SuppressWarnings("unchecked")
1768 private void loadConfiguration() {
1769 /* create options. */
1770 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1773 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1774 SoneInserter.setInsertionDelay(newValue);
1778 options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10));
1779 options.addBooleanOption("RequireFullAccess", new DefaultOption<Boolean>(false));
1780 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75));
1781 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25));
1782 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
1783 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1784 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1785 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1787 /* read options from configuration. */
1788 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1789 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1790 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1791 options.getBooleanOption("ClearOnNextRestart").set(null);
1792 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1793 if (clearConfiguration) {
1794 /* stop loading the configuration. */
1798 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1799 options.getIntegerOption("PostsPerPage").set(configuration.getIntValue("Option/PostsPerPage").getValue(null));
1800 options.getBooleanOption("RequireFullAccess").set(configuration.getBooleanValue("Option/RequireFullAccess").getValue(null));
1801 options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
1802 options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
1803 options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
1804 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1806 /* load known Sones. */
1807 int soneCounter = 0;
1809 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1810 if (knownSoneId == null) {
1813 synchronized (newSones) {
1814 knownSones.add(knownSoneId);
1818 /* load known posts. */
1819 int postCounter = 0;
1821 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1822 if (knownPostId == null) {
1825 synchronized (newPosts) {
1826 knownPosts.add(knownPostId);
1830 /* load known replies. */
1831 int replyCounter = 0;
1833 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1834 if (knownReplyId == null) {
1837 synchronized (newReplies) {
1838 knownReplies.add(knownReplyId);
1842 /* load bookmarked posts. */
1843 int bookmarkedPostCounter = 0;
1845 String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null);
1846 if (bookmarkedPostId == null) {
1849 synchronized (bookmarkedPosts) {
1850 bookmarkedPosts.add(bookmarkedPostId);
1857 * Generate a Sone URI from the given URI and latest edition.
1860 * The URI to derive the Sone URI from
1861 * @return The derived URI
1863 private FreenetURI getSoneUri(String uriString) {
1865 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1867 } catch (MalformedURLException mue1) {
1868 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1874 // INTERFACE IdentityListener
1881 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1882 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1883 if (ownIdentity.hasContext("Sone")) {
1884 trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1885 addLocalSone(ownIdentity);
1893 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1894 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1895 trustedIdentities.remove(ownIdentity);
1902 public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1903 logger.log(Level.FINEST, "Adding Identity: " + identity);
1904 trustedIdentities.get(ownIdentity).add(identity);
1905 addRemoteSone(identity);
1912 public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1913 new Thread(new Runnable() {
1916 @SuppressWarnings("synthetic-access")
1918 Sone sone = getRemoteSone(identity.getId());
1919 sone.setIdentity(identity);
1920 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition()));
1921 soneDownloader.addSone(sone);
1922 soneDownloader.fetchSone(sone);
1931 public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1932 trustedIdentities.get(ownIdentity).remove(identity);
1933 boolean foundIdentity = false;
1934 for (Entry<OwnIdentity, Set<Identity>> trustedIdentity : trustedIdentities.entrySet()) {
1935 if (trustedIdentity.getKey().equals(ownIdentity)) {
1938 if (trustedIdentity.getValue().contains(identity)) {
1939 foundIdentity = true;
1942 if (foundIdentity) {
1943 /* some local identity still trusts this identity, don’t remove. */
1946 Sone sone = getSone(identity.getId(), false);
1948 /* TODO - we don’t have the Sone anymore. should this happen? */
1951 synchronized (posts) {
1952 synchronized (newPosts) {
1953 for (Post post : sone.getPosts()) {
1954 posts.remove(post.getId());
1955 newPosts.remove(post.getId());
1956 coreListenerManager.firePostRemoved(post);
1960 synchronized (replies) {
1961 synchronized (newReplies) {
1962 for (Reply reply : sone.getReplies()) {
1963 replies.remove(reply.getId());
1964 newReplies.remove(reply.getId());
1965 coreListenerManager.fireReplyRemoved(reply);
1969 synchronized (remoteSones) {
1970 remoteSones.remove(identity.getId());
1972 synchronized (newSones) {
1973 newSones.remove(identity.getId());
1978 // INTERFACE UpdateListener
1985 public void updateFound(Version version, long releaseTime, long latestEdition) {
1986 coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition);
1990 * Convenience interface for external classes that want to access the core’s
1993 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1995 public static class Preferences {
1997 /** The wrapped options. */
1998 private final Options options;
2001 * Creates a new preferences object wrapped around the given options.
2004 * The options to wrap
2006 public Preferences(Options options) {
2007 this.options = options;
2011 * Returns the insertion delay.
2013 * @return The insertion delay
2015 public int getInsertionDelay() {
2016 return options.getIntegerOption("InsertionDelay").get();
2020 * Sets the insertion delay
2022 * @param insertionDelay
2023 * The new insertion delay, or {@code null} to restore it to
2025 * @return This preferences
2027 public Preferences setInsertionDelay(Integer insertionDelay) {
2028 options.getIntegerOption("InsertionDelay").set(insertionDelay);
2033 * Returns the number of posts to show per page.
2035 * @return The number of posts to show per page
2037 public int getPostsPerPage() {
2038 return options.getIntegerOption("PostsPerPage").get();
2042 * Sets the number of posts to show per page.
2044 * @param postsPerPage
2045 * The number of posts to show per page
2046 * @return This preferences object
2048 public Preferences setPostsPerPage(Integer postsPerPage) {
2049 options.getIntegerOption("PostsPerPage").set(postsPerPage);
2054 * Returns whether Sone requires full access to be even visible.
2056 * @return {@code true} if Sone requires full access, {@code false}
2059 public boolean isRequireFullAccess() {
2060 return options.getBooleanOption("RequireFullAccess").get();
2064 * Sets whether Sone requires full access to be even visible.
2066 * @param requireFullAccess
2067 * {@code true} if Sone requires full access, {@code false}
2070 public void setRequireFullAccess(Boolean requireFullAccess) {
2071 options.getBooleanOption("RequireFullAccess").set(requireFullAccess);
2075 * Returns the positive trust.
2077 * @return The positive trust
2079 public int getPositiveTrust() {
2080 return options.getIntegerOption("PositiveTrust").get();
2084 * Sets the positive trust.
2086 * @param positiveTrust
2087 * The new positive trust, or {@code null} to restore it to
2089 * @return This preferences
2091 public Preferences setPositiveTrust(Integer positiveTrust) {
2092 options.getIntegerOption("PositiveTrust").set(positiveTrust);
2097 * Returns the negative trust.
2099 * @return The negative trust
2101 public int getNegativeTrust() {
2102 return options.getIntegerOption("NegativeTrust").get();
2106 * Sets the negative trust.
2108 * @param negativeTrust
2109 * The negative trust, or {@code null} to restore it to the
2111 * @return The preferences
2113 public Preferences setNegativeTrust(Integer negativeTrust) {
2114 options.getIntegerOption("NegativeTrust").set(negativeTrust);
2119 * Returns the trust comment. This is the comment that is set in the web
2120 * of trust when a trust value is assigned to an identity.
2122 * @return The trust comment
2124 public String getTrustComment() {
2125 return options.getStringOption("TrustComment").get();
2129 * Sets the trust comment.
2131 * @param trustComment
2132 * The trust comment, or {@code null} to restore it to the
2134 * @return This preferences
2136 public Preferences setTrustComment(String trustComment) {
2137 options.getStringOption("TrustComment").set(trustComment);
2142 * Returns whether the rescue mode is active.
2144 * @return {@code true} if the rescue mode is active, {@code false}
2147 public boolean isSoneRescueMode() {
2148 return options.getBooleanOption("SoneRescueMode").get();
2152 * Sets whether the rescue mode is active.
2154 * @param soneRescueMode
2155 * {@code true} if the rescue mode is active, {@code false}
2157 * @return This preferences
2159 public Preferences setSoneRescueMode(Boolean soneRescueMode) {
2160 options.getBooleanOption("SoneRescueMode").set(soneRescueMode);
2165 * Returns whether Sone should clear its settings on the next restart.
2166 * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
2167 * to return {@code true} as well!
2169 * @return {@code true} if Sone should clear its settings on the next
2170 * restart, {@code false} otherwise
2172 public boolean isClearOnNextRestart() {
2173 return options.getBooleanOption("ClearOnNextRestart").get();
2177 * Sets whether Sone will clear its settings on the next restart.
2179 * @param clearOnNextRestart
2180 * {@code true} if Sone should clear its settings on the next
2181 * restart, {@code false} otherwise
2182 * @return This preferences
2184 public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
2185 options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
2190 * Returns whether Sone should really clear its settings on next
2191 * restart. This is a confirmation option that needs to be set in
2192 * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
2193 * settings on the next restart.
2195 * @return {@code true} if Sone should really clear its settings on the
2196 * next restart, {@code false} otherwise
2198 public boolean isReallyClearOnNextRestart() {
2199 return options.getBooleanOption("ReallyClearOnNextRestart").get();
2203 * Sets whether Sone should really clear its settings on the next
2206 * @param reallyClearOnNextRestart
2207 * {@code true} if Sone should really clear its settings on
2208 * the next restart, {@code false} otherwise
2209 * @return This preferences
2211 public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
2212 options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);