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.Album;
35 import net.pterodactylus.sone.data.Client;
36 import net.pterodactylus.sone.data.Image;
37 import net.pterodactylus.sone.data.Post;
38 import net.pterodactylus.sone.data.Profile;
39 import net.pterodactylus.sone.data.Profile.Field;
40 import net.pterodactylus.sone.data.Reply;
41 import net.pterodactylus.sone.data.Sone;
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 /** Whether the core has been stopped. */
115 private volatile boolean stopped;
117 /** The Sones’ statuses. */
118 /* synchronize access on itself. */
119 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
121 /** Locked local Sones. */
122 /* synchronize on itself. */
123 private final Set<Sone> lockedSones = new HashSet<Sone>();
125 /** Sone inserters. */
126 /* synchronize access on this on localSones. */
127 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
129 /** All local Sones. */
130 /* synchronize access on this on itself. */
131 private Map<String, Sone> localSones = new HashMap<String, Sone>();
133 /** All remote Sones. */
134 /* synchronize access on this on itself. */
135 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
137 /** All new Sones. */
138 private Set<String> newSones = new HashSet<String>();
140 /** All known Sones. */
141 /* synchronize access on {@link #newSones}. */
142 private Set<String> knownSones = new HashSet<String>();
145 private Map<String, Post> posts = new HashMap<String, Post>();
147 /** All new posts. */
148 private Set<String> newPosts = new HashSet<String>();
150 /** All known posts. */
151 /* synchronize access on {@link #newPosts}. */
152 private Set<String> knownPosts = new HashSet<String>();
155 private Map<String, Reply> replies = new HashMap<String, Reply>();
157 /** All new replies. */
158 private Set<String> newReplies = new HashSet<String>();
160 /** All known replies. */
161 private Set<String> knownReplies = new HashSet<String>();
163 /** All bookmarked posts. */
164 /* synchronize access on itself. */
165 private Set<String> bookmarkedPosts = new HashSet<String>();
167 /** Trusted identities, sorted by own identities. */
168 private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
170 /** All known albums. */
171 private Map<String, Album> albums = new HashMap<String, Album>();
173 /** All known images. */
174 private Map<String, Image> images = new HashMap<String, Image>();
177 * Creates a new core.
179 * @param configuration
180 * The configuration of the core
181 * @param freenetInterface
182 * The freenet interface
183 * @param identityManager
184 * The identity manager
186 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
187 this.configuration = configuration;
188 this.freenetInterface = freenetInterface;
189 this.identityManager = identityManager;
190 this.soneDownloader = new SoneDownloader(this, freenetInterface);
191 this.updateChecker = new UpdateChecker(freenetInterface);
195 // LISTENER MANAGEMENT
199 * Adds a new core listener.
201 * @param coreListener
202 * The listener to add
204 public void addCoreListener(CoreListener coreListener) {
205 coreListenerManager.addListener(coreListener);
209 * Removes a core listener.
211 * @param coreListener
212 * The listener to remove
214 public void removeCoreListener(CoreListener coreListener) {
215 coreListenerManager.removeListener(coreListener);
223 * Sets the configuration to use. This will automatically save the current
224 * configuration to the given configuration.
226 * @param configuration
227 * The new configuration to use
229 public void setConfiguration(Configuration configuration) {
230 this.configuration = configuration;
235 * Returns the options used by the core.
237 * @return The options of the core
239 public Preferences getPreferences() {
244 * Returns the identity manager used by the core.
246 * @return The identity manager
248 public IdentityManager getIdentityManager() {
249 return identityManager;
253 * Returns the update checker.
255 * @return The update checker
257 public UpdateChecker getUpdateChecker() {
258 return updateChecker;
262 * Returns the status of the given Sone.
265 * The Sone to get the status for
266 * @return The status of the Sone
268 public SoneStatus getSoneStatus(Sone sone) {
269 synchronized (soneStatuses) {
270 return soneStatuses.get(sone);
275 * Sets the status of the given Sone.
278 * The Sone to set the status of
282 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
283 synchronized (soneStatuses) {
284 soneStatuses.put(sone, soneStatus);
289 * Returns whether the given Sone is currently locked.
293 * @return {@code true} if the Sone is locked, {@code false} if it is not
295 public boolean isLocked(Sone sone) {
296 synchronized (lockedSones) {
297 return lockedSones.contains(sone);
302 * Returns all Sones, remote and local.
306 public Set<Sone> getSones() {
307 Set<Sone> allSones = new HashSet<Sone>();
308 allSones.addAll(getLocalSones());
309 allSones.addAll(getRemoteSones());
314 * Returns the Sone with the given ID, regardless whether it’s local or
318 * The ID of the Sone to get
319 * @return The Sone with the given ID, or {@code null} if there is no such
322 public Sone getSone(String id) {
323 return getSone(id, true);
327 * Returns the Sone with the given ID, regardless whether it’s local or
331 * The ID of the Sone to get
333 * {@code true} to create a new Sone if none exists,
334 * {@code false} to return {@code null} if a Sone with the given
336 * @return The Sone with the given ID, or {@code null} if there is no such
339 public Sone getSone(String id, boolean create) {
340 if (isLocalSone(id)) {
341 return getLocalSone(id);
343 return getRemoteSone(id, create);
347 * Checks whether the core knows a Sone with the given ID.
351 * @return {@code true} if there is a Sone with the given ID, {@code false}
354 public boolean hasSone(String id) {
355 return isLocalSone(id) || isRemoteSone(id);
359 * Returns whether the given Sone is a local Sone.
362 * The Sone to check for its locality
363 * @return {@code true} if the given Sone is local, {@code false} otherwise
365 public boolean isLocalSone(Sone sone) {
366 synchronized (localSones) {
367 return localSones.containsKey(sone.getId());
372 * Returns whether the given ID is the ID of a local Sone.
375 * The Sone ID to check for its locality
376 * @return {@code true} if the given ID is a local Sone, {@code false}
379 public boolean isLocalSone(String id) {
380 synchronized (localSones) {
381 return localSones.containsKey(id);
386 * Returns all local Sones.
388 * @return All local Sones
390 public Set<Sone> getLocalSones() {
391 synchronized (localSones) {
392 return new HashSet<Sone>(localSones.values());
397 * Returns the local Sone with the given ID.
400 * The ID of the Sone to get
401 * @return The Sone with the given ID
403 public Sone getLocalSone(String id) {
404 return getLocalSone(id, true);
408 * Returns the local Sone with the given ID, optionally creating a new Sone.
413 * {@code true} to create a new Sone if none exists,
414 * {@code false} to return null if none exists
415 * @return The Sone with the given ID, or {@code null}
417 public Sone getLocalSone(String id, boolean create) {
418 synchronized (localSones) {
419 Sone sone = localSones.get(id);
420 if ((sone == null) && create) {
422 localSones.put(id, sone);
423 setSoneStatus(sone, SoneStatus.unknown);
430 * Returns all remote Sones.
432 * @return All remote Sones
434 public Set<Sone> getRemoteSones() {
435 synchronized (remoteSones) {
436 return new HashSet<Sone>(remoteSones.values());
441 * Returns the remote Sone with the given ID.
444 * The ID of the remote Sone to get
445 * @return The Sone with the given ID
447 public Sone getRemoteSone(String id) {
448 return getRemoteSone(id, true);
452 * Returns the remote Sone with the given ID.
455 * The ID of the remote Sone to get
457 * {@code true} to always create a Sone, {@code false} to return
458 * {@code null} if no Sone with the given ID exists
459 * @return The Sone with the given ID
461 public Sone getRemoteSone(String id, boolean create) {
462 synchronized (remoteSones) {
463 Sone sone = remoteSones.get(id);
464 if ((sone == null) && create) {
466 remoteSones.put(id, sone);
467 setSoneStatus(sone, SoneStatus.unknown);
474 * Returns whether the given Sone is a remote Sone.
478 * @return {@code true} if the given Sone is a remote Sone, {@code false}
481 public boolean isRemoteSone(Sone sone) {
482 synchronized (remoteSones) {
483 return remoteSones.containsKey(sone.getId());
488 * Returns whether the Sone with the given ID is a remote Sone.
491 * The ID of the Sone to check
492 * @return {@code true} if the Sone with the given ID is a remote Sone,
493 * {@code false} otherwise
495 public boolean isRemoteSone(String id) {
496 synchronized (remoteSones) {
497 return remoteSones.containsKey(id);
502 * Returns whether the Sone with the given ID is a new Sone.
505 * The ID of the sone to check for
506 * @return {@code true} if the given Sone is new, false otherwise
508 public boolean isNewSone(String soneId) {
509 synchronized (newSones) {
510 return !knownSones.contains(soneId) && newSones.contains(soneId);
515 * Returns whether the given Sone has been modified.
518 * The Sone to check for modifications
519 * @return {@code true} if a modification has been detected in the Sone,
520 * {@code false} otherwise
522 public boolean isModifiedSone(Sone sone) {
523 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
527 * Returns whether the target Sone is trusted by the origin Sone.
533 * @return {@code true} if the target Sone is trusted by the origin Sone
535 public boolean isSoneTrusted(Sone origin, Sone target) {
536 return trustedIdentities.containsKey(origin) && trustedIdentities.get(origin.getIdentity()).contains(target);
540 * Returns the post with the given ID.
543 * The ID of the post to get
544 * @return The post, or {@code null} if there is no such post
546 public Post getPost(String postId) {
547 return getPost(postId, true);
551 * Returns the post with the given ID, optionally creating a new post.
554 * The ID of the post to get
556 * {@code true} it create a new post if no post with the given ID
557 * exists, {@code false} to return {@code null}
558 * @return The post, or {@code null} if there is no such post
560 public Post getPost(String postId, boolean create) {
561 synchronized (posts) {
562 Post post = posts.get(postId);
563 if ((post == null) && create) {
564 post = new Post(postId);
565 posts.put(postId, post);
572 * Returns whether the given post ID is new.
576 * @return {@code true} if the post is considered to be new, {@code false}
579 public boolean isNewPost(String postId) {
580 synchronized (newPosts) {
581 return !knownPosts.contains(postId) && newPosts.contains(postId);
586 * Returns the reply with the given ID. If there is no reply with the given
587 * ID yet, a new one is created.
590 * The ID of the reply to get
593 public Reply getReply(String replyId) {
594 return getReply(replyId, true);
598 * Returns the reply with the given ID. If there is no reply with the given
599 * ID yet, a new one is created, unless {@code create} is false in which
600 * case {@code null} is returned.
603 * The ID of the reply to get
605 * {@code true} to always return a {@link Reply}, {@code false}
606 * to return {@code null} if no reply can be found
607 * @return The reply, or {@code null} if there is no such reply
609 public Reply getReply(String replyId, boolean create) {
610 synchronized (replies) {
611 Reply reply = replies.get(replyId);
612 if (create && (reply == null)) {
613 reply = new Reply(replyId);
614 replies.put(replyId, reply);
621 * Returns all replies for the given post, order ascending by time.
624 * The post to get all replies for
625 * @return All replies for the given post
627 public List<Reply> getReplies(Post post) {
628 Set<Sone> sones = getSones();
629 List<Reply> replies = new ArrayList<Reply>();
630 for (Sone sone : sones) {
631 for (Reply reply : sone.getReplies()) {
632 if (reply.getPost().equals(post)) {
637 Collections.sort(replies, Reply.TIME_COMPARATOR);
642 * Returns whether the reply with the given ID is new.
645 * The ID of the reply to check
646 * @return {@code true} if the reply is considered to be new, {@code false}
649 public boolean isNewReply(String replyId) {
650 synchronized (newReplies) {
651 return !knownReplies.contains(replyId) && newReplies.contains(replyId);
656 * Returns all Sones that have liked the given post.
659 * The post to get the liking Sones for
660 * @return The Sones that like the given post
662 public Set<Sone> getLikes(Post post) {
663 Set<Sone> sones = new HashSet<Sone>();
664 for (Sone sone : getSones()) {
665 if (sone.getLikedPostIds().contains(post.getId())) {
673 * Returns all Sones that have liked the given reply.
676 * The reply to get the liking Sones for
677 * @return The Sones that like the given reply
679 public Set<Sone> getLikes(Reply reply) {
680 Set<Sone> sones = new HashSet<Sone>();
681 for (Sone sone : getSones()) {
682 if (sone.getLikedReplyIds().contains(reply.getId())) {
690 * Returns whether the given post is bookmarked.
694 * @return {@code true} if the given post is bookmarked, {@code false}
697 public boolean isBookmarked(Post post) {
698 return isPostBookmarked(post.getId());
702 * Returns whether the post with the given ID is bookmarked.
705 * The ID of the post to check
706 * @return {@code true} if the post with the given ID is bookmarked,
707 * {@code false} otherwise
709 public boolean isPostBookmarked(String id) {
710 synchronized (bookmarkedPosts) {
711 return bookmarkedPosts.contains(id);
716 * Returns all currently known bookmarked posts.
718 * @return All bookmarked posts
720 public Set<Post> getBookmarkedPosts() {
721 Set<Post> posts = new HashSet<Post>();
722 synchronized (bookmarkedPosts) {
723 for (String bookmarkedPostId : bookmarkedPosts) {
724 Post post = getPost(bookmarkedPostId, false);
735 * Returns the album with the given ID, creating a new album if no album
736 * with the given ID can be found.
739 * The ID of the album
740 * @return The album with the given ID
742 public Album getAlbum(String albumId) {
743 return getAlbum(albumId, true);
747 * Returns the album with the given ID, optionally creating a new album if
748 * an album with the given ID can not be found.
751 * The ID of the album
753 * {@code true} to create a new album if none exists for the
755 * @return The album with the given ID, or {@code null} if no album with the
756 * given ID exists and {@code create} is {@code false}
758 public Album getAlbum(String albumId, boolean create) {
759 synchronized (albums) {
760 Album album = albums.get(albumId);
761 if (create && (album == null)) {
762 album = new Album(albumId);
763 albums.put(albumId, album);
770 * Returns the image with the given ID, creating it if necessary.
773 * The ID of the image
774 * @return The image with the given ID
776 public Image getImage(String imageId) {
777 return getImage(imageId, true);
781 * Returns the image with the given ID, optionally creating it if it does
785 * The ID of the image
787 * {@code true} to create an image if none exists with the given
789 * @return The image with the given ID, or {@code null} if none exists and
792 public Image getImage(String imageId, boolean create) {
793 synchronized (images) {
794 Image image = images.get(imageId);
795 if (create && (image == null)) {
796 image = new Image(imageId);
797 images.put(imageId, image);
808 * Locks the given Sone. A locked Sone will not be inserted by
809 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
815 public void lockSone(Sone sone) {
816 synchronized (lockedSones) {
817 if (lockedSones.add(sone)) {
818 coreListenerManager.fireSoneLocked(sone);
824 * Unlocks the given Sone.
826 * @see #lockSone(Sone)
830 public void unlockSone(Sone sone) {
831 synchronized (lockedSones) {
832 if (lockedSones.remove(sone)) {
833 coreListenerManager.fireSoneUnlocked(sone);
839 * Adds a local Sone from the given ID which has to be the ID of an own
843 * The ID of an own identity to add a Sone for
844 * @return The added (or already existing) Sone
846 public Sone addLocalSone(String id) {
847 synchronized (localSones) {
848 if (localSones.containsKey(id)) {
849 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
850 return localSones.get(id);
852 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
853 if (ownIdentity == null) {
854 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
857 return addLocalSone(ownIdentity);
862 * Adds a local Sone from the given own identity.
865 * The own identity to create a Sone from
866 * @return The added (or already existing) Sone
868 public Sone addLocalSone(OwnIdentity ownIdentity) {
869 if (ownIdentity == null) {
870 logger.log(Level.WARNING, "Given OwnIdentity is null!");
873 synchronized (localSones) {
876 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
877 } catch (MalformedURLException mue1) {
878 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
881 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
882 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
883 /* TODO - load posts ’n stuff */
884 localSones.put(ownIdentity.getId(), sone);
885 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
886 soneInserters.put(sone, soneInserter);
887 setSoneStatus(sone, SoneStatus.idle);
889 if (!preferences.isSoneRescueMode()) {
890 soneInserter.start();
892 new Thread(new Runnable() {
895 @SuppressWarnings("synthetic-access")
897 if (!preferences.isSoneRescueMode()) {
898 soneDownloader.fetchSone(sone);
901 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
902 coreListenerManager.fireRescuingSone(sone);
904 long edition = sone.getLatestEdition();
905 while (!stopped && (edition >= 0) && preferences.isSoneRescueMode()) {
906 logger.log(Level.FINE, "Downloading edition " + edition + "…");
907 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
910 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
912 coreListenerManager.fireRescuedSone(sone);
913 soneInserter.start();
916 }, "Sone Downloader").start();
922 * Creates a new Sone for the given own identity.
925 * The own identity to create a Sone for
926 * @return The created Sone
928 public Sone createSone(OwnIdentity ownIdentity) {
930 ownIdentity.addContext("Sone");
931 } catch (WebOfTrustException wote1) {
932 logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
935 Sone sone = addLocalSone(ownIdentity);
940 * Adds the Sone of the given identity.
943 * The identity whose Sone to add
944 * @return The added or already existing Sone
946 public Sone addRemoteSone(Identity identity) {
947 if (identity == null) {
948 logger.log(Level.WARNING, "Given Identity is null!");
951 synchronized (remoteSones) {
952 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
953 boolean newSone = sone.getRequestUri() == null;
954 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
955 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
957 synchronized (newSones) {
958 newSone = !knownSones.contains(sone.getId());
960 newSones.add(sone.getId());
964 coreListenerManager.fireNewSoneFound(sone);
967 remoteSones.put(identity.getId(), sone);
968 soneDownloader.addSone(sone);
969 setSoneStatus(sone, SoneStatus.unknown);
970 new Thread(new Runnable() {
973 @SuppressWarnings("synthetic-access")
975 soneDownloader.fetchSone(sone);
978 }, "Sone Downloader").start();
984 * Retrieves the trust relationship from the origin to the target. If the
985 * trust relationship can not be retrieved, {@code null} is returned.
987 * @see Identity#getTrust(OwnIdentity)
989 * The origin of the trust tree
991 * The target of the trust
992 * @return The trust relationship
994 public Trust getTrust(Sone origin, Sone target) {
995 if (!isLocalSone(origin)) {
996 logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
999 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
1003 * Sets the trust value of the given origin Sone for the target Sone.
1010 * The trust value (from {@code -100} to {@code 100})
1012 public void setTrust(Sone origin, Sone target, int trustValue) {
1013 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();
1015 ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
1016 } catch (WebOfTrustException wote1) {
1017 logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
1022 * Removes any trust assignment for the given target Sone.
1029 public void removeTrust(Sone origin, Sone target) {
1030 Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
1032 ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
1033 } catch (WebOfTrustException wote1) {
1034 logger.log(Level.WARNING, "Could not remove trust for Sone: " + target, wote1);
1039 * Assigns the configured positive trust value for the given target.
1046 public void trustSone(Sone origin, Sone target) {
1047 setTrust(origin, target, preferences.getPositiveTrust());
1051 * Assigns the configured negative trust value for the given target.
1058 public void distrustSone(Sone origin, Sone target) {
1059 setTrust(origin, target, preferences.getNegativeTrust());
1063 * Removes the trust assignment for the given target.
1070 public void untrustSone(Sone origin, Sone target) {
1071 removeTrust(origin, target);
1075 * Updates the stores Sone with the given Sone.
1080 public void updateSone(Sone sone) {
1081 if (hasSone(sone.getId())) {
1082 boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode();
1083 Sone storedSone = getSone(sone.getId());
1084 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
1085 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
1088 synchronized (posts) {
1089 if (!soneRescueMode) {
1090 for (Post post : storedSone.getPosts()) {
1091 posts.remove(post.getId());
1092 if (!sone.getPosts().contains(post)) {
1093 coreListenerManager.firePostRemoved(post);
1097 List<Post> storedPosts = storedSone.getPosts();
1098 synchronized (newPosts) {
1099 for (Post post : sone.getPosts()) {
1100 post.setSone(storedSone);
1101 if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
1102 newPosts.add(post.getId());
1103 coreListenerManager.fireNewPostFound(post);
1105 posts.put(post.getId(), post);
1109 synchronized (replies) {
1110 if (!soneRescueMode) {
1111 for (Reply reply : storedSone.getReplies()) {
1112 replies.remove(reply.getId());
1113 if (!sone.getReplies().contains(reply)) {
1114 coreListenerManager.fireReplyRemoved(reply);
1118 Set<Reply> storedReplies = storedSone.getReplies();
1119 synchronized (newReplies) {
1120 for (Reply reply : sone.getReplies()) {
1121 reply.setSone(storedSone);
1122 if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
1123 newReplies.add(reply.getId());
1124 coreListenerManager.fireNewReplyFound(reply);
1126 replies.put(reply.getId(), reply);
1130 synchronized (storedSone) {
1131 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
1132 storedSone.setTime(sone.getTime());
1134 storedSone.setClient(sone.getClient());
1135 storedSone.setProfile(sone.getProfile());
1136 if (soneRescueMode) {
1137 for (Post post : sone.getPosts()) {
1138 storedSone.addPost(post);
1140 for (Reply reply : sone.getReplies()) {
1141 storedSone.addReply(reply);
1143 for (String likedPostId : sone.getLikedPostIds()) {
1144 storedSone.addLikedPostId(likedPostId);
1146 for (String likedReplyId : sone.getLikedReplyIds()) {
1147 storedSone.addLikedReplyId(likedReplyId);
1150 storedSone.setPosts(sone.getPosts());
1151 storedSone.setReplies(sone.getReplies());
1152 storedSone.setLikePostIds(sone.getLikedPostIds());
1153 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
1155 storedSone.setLatestEdition(sone.getLatestEdition());
1161 * Deletes the given Sone. This will remove the Sone from the
1162 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1163 * and remove the context from its identity.
1166 * The Sone to delete
1168 public void deleteSone(Sone sone) {
1169 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1170 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1173 synchronized (localSones) {
1174 if (!localSones.containsKey(sone.getId())) {
1175 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1178 localSones.remove(sone.getId());
1179 soneInserters.remove(sone).stop();
1182 ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1183 ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1184 } catch (WebOfTrustException wote1) {
1185 logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1188 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1189 } catch (ConfigurationException ce1) {
1190 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1195 * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
1196 * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
1199 * The Sone to mark as known
1201 public void markSoneKnown(Sone sone) {
1202 synchronized (newSones) {
1203 if (newSones.remove(sone.getId())) {
1204 knownSones.add(sone.getId());
1205 coreListenerManager.fireMarkSoneKnown(sone);
1206 saveConfiguration();
1212 * Loads and updates the given Sone from the configuration. If any error is
1213 * encountered, loading is aborted and the given Sone is not changed.
1216 * The Sone to load and update
1218 public void loadSone(Sone sone) {
1219 if (!isLocalSone(sone)) {
1220 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1225 String sonePrefix = "Sone/" + sone.getId();
1226 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1227 if (soneTime == null) {
1228 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1231 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1234 Profile profile = new Profile();
1235 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1236 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1237 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1238 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1239 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1240 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1242 /* load profile fields. */
1244 String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
1245 String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
1246 if (fieldName == null) {
1249 String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
1250 profile.addField(fieldName).setValue(fieldValue);
1254 Set<Post> posts = new HashSet<Post>();
1256 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1257 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1258 if (postId == null) {
1261 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1262 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1263 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1264 if ((postTime == 0) || (postText == null)) {
1265 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1268 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1269 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1270 post.setRecipient(getSone(postRecipientId));
1276 Set<Reply> replies = new HashSet<Reply>();
1278 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1279 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1280 if (replyId == null) {
1283 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1284 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1285 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1286 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1287 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1290 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1293 /* load post likes. */
1294 Set<String> likedPostIds = new HashSet<String>();
1296 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1297 if (likedPostId == null) {
1300 likedPostIds.add(likedPostId);
1303 /* load reply likes. */
1304 Set<String> likedReplyIds = new HashSet<String>();
1306 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1307 if (likedReplyId == null) {
1310 likedReplyIds.add(likedReplyId);
1314 Set<String> friends = new HashSet<String>();
1316 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1317 if (friendId == null) {
1320 friends.add(friendId);
1323 /* if we’re still here, Sone was loaded successfully. */
1324 synchronized (sone) {
1325 sone.setTime(soneTime);
1326 sone.setProfile(profile);
1327 sone.setPosts(posts);
1328 sone.setReplies(replies);
1329 sone.setLikePostIds(likedPostIds);
1330 sone.setLikeReplyIds(likedReplyIds);
1331 sone.setFriends(friends);
1332 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1334 synchronized (newSones) {
1335 for (String friend : friends) {
1336 knownSones.add(friend);
1339 synchronized (newPosts) {
1340 for (Post post : posts) {
1341 knownPosts.add(post.getId());
1344 synchronized (newReplies) {
1345 for (Reply reply : replies) {
1346 knownReplies.add(reply.getId());
1352 * Saves the given Sone. This will persist all local settings for the given
1353 * Sone, such as the friends list and similar, private options.
1358 public synchronized void saveSone(Sone sone) {
1359 if (!isLocalSone(sone)) {
1360 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1363 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1364 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1368 logger.log(Level.INFO, "Saving Sone: %s", sone);
1370 ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1372 /* save Sone into configuration. */
1373 String sonePrefix = "Sone/" + sone.getId();
1374 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1375 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1378 Profile profile = sone.getProfile();
1379 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1380 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1381 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1382 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1383 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1384 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1386 /* save profile fields. */
1387 int fieldCounter = 0;
1388 for (Field profileField : profile.getFields()) {
1389 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1390 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1391 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1393 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1396 int postCounter = 0;
1397 for (Post post : sone.getPosts()) {
1398 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1399 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1400 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1401 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1402 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1404 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1407 int replyCounter = 0;
1408 for (Reply reply : sone.getReplies()) {
1409 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1410 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1411 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1412 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1413 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1415 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1417 /* save post likes. */
1418 int postLikeCounter = 0;
1419 for (String postId : sone.getLikedPostIds()) {
1420 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1422 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1424 /* save reply likes. */
1425 int replyLikeCounter = 0;
1426 for (String replyId : sone.getLikedReplyIds()) {
1427 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1429 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1432 int friendCounter = 0;
1433 for (String friendId : sone.getFriends()) {
1434 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1436 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1438 configuration.save();
1439 logger.log(Level.INFO, "Sone %s saved.", sone);
1440 } catch (ConfigurationException ce1) {
1441 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1442 } catch (WebOfTrustException wote1) {
1443 logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1448 * Creates a new post.
1451 * The Sone that creates the post
1453 * The text of the post
1454 * @return The created post
1456 public Post createPost(Sone sone, String text) {
1457 return createPost(sone, System.currentTimeMillis(), text);
1461 * Creates a new post.
1464 * The Sone that creates the post
1466 * The time of the post
1468 * The text of the post
1469 * @return The created post
1471 public Post createPost(Sone sone, long time, String text) {
1472 return createPost(sone, null, time, text);
1476 * Creates a new post.
1479 * The Sone that creates the post
1481 * The recipient Sone, or {@code null} if this post does not have
1484 * The text of the post
1485 * @return The created post
1487 public Post createPost(Sone sone, Sone recipient, String text) {
1488 return createPost(sone, recipient, System.currentTimeMillis(), text);
1492 * Creates a new post.
1495 * The Sone that creates the post
1497 * The recipient Sone, or {@code null} if this post does not have
1500 * The time of the post
1502 * The text of the post
1503 * @return The created post
1505 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1506 if (!isLocalSone(sone)) {
1507 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1510 Post post = new Post(sone, time, text);
1511 if (recipient != null) {
1512 post.setRecipient(recipient);
1514 synchronized (posts) {
1515 posts.put(post.getId(), post);
1517 synchronized (newPosts) {
1518 knownPosts.add(post.getId());
1526 * Deletes the given post.
1529 * The post to delete
1531 public void deletePost(Post post) {
1532 if (!isLocalSone(post.getSone())) {
1533 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1536 post.getSone().removePost(post);
1537 synchronized (posts) {
1538 posts.remove(post.getId());
1540 saveSone(post.getSone());
1544 * Marks the given post as known, if it is currently a new post (according
1545 * to {@link #isNewPost(String)}).
1548 * The post to mark as known
1550 public void markPostKnown(Post post) {
1551 synchronized (newPosts) {
1552 if (newPosts.remove(post.getId())) {
1553 knownPosts.add(post.getId());
1554 coreListenerManager.fireMarkPostKnown(post);
1555 saveConfiguration();
1561 * Bookmarks the given post.
1564 * The post to bookmark
1566 public void bookmark(Post post) {
1567 bookmarkPost(post.getId());
1571 * Bookmarks the post with the given ID.
1574 * The ID of the post to bookmark
1576 public void bookmarkPost(String id) {
1577 synchronized (bookmarkedPosts) {
1578 bookmarkedPosts.add(id);
1583 * Removes the given post from the bookmarks.
1586 * The post to unbookmark
1588 public void unbookmark(Post post) {
1589 unbookmarkPost(post.getId());
1593 * Removes the post with the given ID from the bookmarks.
1596 * The ID of the post to unbookmark
1598 public void unbookmarkPost(String id) {
1599 synchronized (bookmarkedPosts) {
1600 bookmarkedPosts.remove(id);
1605 * Creates a new reply.
1608 * The Sone that creates the reply
1610 * The post that this reply refers to
1612 * The text of the reply
1613 * @return The created reply
1615 public Reply createReply(Sone sone, Post post, String text) {
1616 return createReply(sone, post, System.currentTimeMillis(), text);
1620 * Creates a new reply.
1623 * The Sone that creates the reply
1625 * The post that this reply refers to
1627 * The time of the reply
1629 * The text of the reply
1630 * @return The created reply
1632 public Reply createReply(Sone sone, Post post, long time, String text) {
1633 if (!isLocalSone(sone)) {
1634 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1637 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1638 synchronized (replies) {
1639 replies.put(reply.getId(), reply);
1641 synchronized (newReplies) {
1642 knownReplies.add(reply.getId());
1644 sone.addReply(reply);
1650 * Deletes the given reply.
1653 * The reply to delete
1655 public void deleteReply(Reply reply) {
1656 Sone sone = reply.getSone();
1657 if (!isLocalSone(sone)) {
1658 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1661 synchronized (replies) {
1662 replies.remove(reply.getId());
1664 sone.removeReply(reply);
1669 * Marks the given reply as known, if it is currently a new reply (according
1670 * to {@link #isNewReply(String)}).
1673 * The reply to mark as known
1675 public void markReplyKnown(Reply reply) {
1676 synchronized (newReplies) {
1677 if (newReplies.remove(reply.getId())) {
1678 knownReplies.add(reply.getId());
1679 coreListenerManager.fireMarkReplyKnown(reply);
1680 saveConfiguration();
1686 * Creates a new top-level album for the given Sone.
1689 * The Sone to create the album for
1690 * @return The new album
1692 public Album createAlbum(Sone sone) {
1693 return createAlbum(sone, null);
1697 * Creates a new album for the given Sone.
1700 * The Sone to create the album for
1702 * The parent of the album (may be {@code null} to create a
1704 * @return The new album
1706 public Album createAlbum(Sone sone, Album parent) {
1707 Album album = new Album();
1708 synchronized (albums) {
1709 albums.put(album.getId(), album);
1711 album.setSone(sone);
1712 if (parent != null) {
1713 parent.addAlbum(album);
1715 sone.addAlbum(album);
1722 public void start() {
1723 loadConfiguration();
1724 updateChecker.addUpdateListener(this);
1725 updateChecker.start();
1731 public void stop() {
1732 synchronized (localSones) {
1733 for (SoneInserter soneInserter : soneInserters.values()) {
1734 soneInserter.stop();
1737 updateChecker.stop();
1738 updateChecker.removeUpdateListener(this);
1739 soneDownloader.stop();
1740 saveConfiguration();
1745 * Saves the current options.
1747 public void saveConfiguration() {
1748 synchronized (configuration) {
1749 if (storingConfiguration) {
1750 logger.log(Level.FINE, "Already storing configuration…");
1753 storingConfiguration = true;
1756 /* store the options first. */
1758 configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
1759 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1760 configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
1761 configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
1762 configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
1763 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1764 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1765 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1767 /* save known Sones. */
1768 int soneCounter = 0;
1769 synchronized (newSones) {
1770 for (String knownSoneId : knownSones) {
1771 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1773 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1776 /* save known posts. */
1777 int postCounter = 0;
1778 synchronized (newPosts) {
1779 for (String knownPostId : knownPosts) {
1780 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1782 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1785 /* save known replies. */
1786 int replyCounter = 0;
1787 synchronized (newReplies) {
1788 for (String knownReplyId : knownReplies) {
1789 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1791 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1794 /* save bookmarked posts. */
1795 int bookmarkedPostCounter = 0;
1796 synchronized (bookmarkedPosts) {
1797 for (String bookmarkedPostId : bookmarkedPosts) {
1798 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
1801 configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
1804 configuration.save();
1806 } catch (ConfigurationException ce1) {
1807 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1809 synchronized (configuration) {
1810 storingConfiguration = false;
1820 * Loads the configuration.
1822 @SuppressWarnings("unchecked")
1823 private void loadConfiguration() {
1824 /* create options. */
1825 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1828 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1829 SoneInserter.setInsertionDelay(newValue);
1833 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75));
1834 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-100));
1835 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
1836 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1837 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1838 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1840 /* read options from configuration. */
1841 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1842 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1843 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1844 options.getBooleanOption("ClearOnNextRestart").set(null);
1845 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1846 if (clearConfiguration) {
1847 /* stop loading the configuration. */
1851 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1852 options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
1853 options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
1854 options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
1855 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1857 /* load known Sones. */
1858 int soneCounter = 0;
1860 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1861 if (knownSoneId == null) {
1864 synchronized (newSones) {
1865 knownSones.add(knownSoneId);
1869 /* load known posts. */
1870 int postCounter = 0;
1872 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1873 if (knownPostId == null) {
1876 synchronized (newPosts) {
1877 knownPosts.add(knownPostId);
1881 /* load known replies. */
1882 int replyCounter = 0;
1884 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1885 if (knownReplyId == null) {
1888 synchronized (newReplies) {
1889 knownReplies.add(knownReplyId);
1893 /* load bookmarked posts. */
1894 int bookmarkedPostCounter = 0;
1896 String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null);
1897 if (bookmarkedPostId == null) {
1900 synchronized (bookmarkedPosts) {
1901 bookmarkedPosts.add(bookmarkedPostId);
1908 * Generate a Sone URI from the given URI and latest edition.
1911 * The URI to derive the Sone URI from
1912 * @return The derived URI
1914 private FreenetURI getSoneUri(String uriString) {
1916 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1918 } catch (MalformedURLException mue1) {
1919 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1925 // INTERFACE IdentityListener
1932 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1933 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1934 if (ownIdentity.hasContext("Sone")) {
1935 trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1936 addLocalSone(ownIdentity);
1944 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1945 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1946 trustedIdentities.remove(ownIdentity);
1953 public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1954 logger.log(Level.FINEST, "Adding Identity: " + identity);
1955 trustedIdentities.get(ownIdentity).add(identity);
1956 addRemoteSone(identity);
1963 public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1964 new Thread(new Runnable() {
1967 @SuppressWarnings("synthetic-access")
1969 Sone sone = getRemoteSone(identity.getId());
1970 sone.setIdentity(identity);
1971 soneDownloader.addSone(sone);
1972 soneDownloader.fetchSone(sone);
1981 public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1982 trustedIdentities.get(ownIdentity).remove(identity);
1986 // INTERFACE UpdateListener
1993 public void updateFound(Version version, long releaseTime, long latestEdition) {
1994 coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition);
1998 * Convenience interface for external classes that want to access the core’s
2001 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
2003 public static class Preferences {
2005 /** The wrapped options. */
2006 private final Options options;
2009 * Creates a new preferences object wrapped around the given options.
2012 * The options to wrap
2014 public Preferences(Options options) {
2015 this.options = options;
2019 * Returns the insertion delay.
2021 * @return The insertion delay
2023 public int getInsertionDelay() {
2024 return options.getIntegerOption("InsertionDelay").get();
2028 * Sets the insertion delay
2030 * @param insertionDelay
2031 * The new insertion delay, or {@code null} to restore it to
2033 * @return This preferences
2035 public Preferences setInsertionDelay(Integer insertionDelay) {
2036 options.getIntegerOption("InsertionDelay").set(insertionDelay);
2041 * Returns the positive trust.
2043 * @return The positive trust
2045 public int getPositiveTrust() {
2046 return options.getIntegerOption("PositiveTrust").get();
2050 * Sets the positive trust.
2052 * @param positiveTrust
2053 * The new positive trust, or {@code null} to restore it to
2055 * @return This preferences
2057 public Preferences setPositiveTrust(Integer positiveTrust) {
2058 options.getIntegerOption("PositiveTrust").set(positiveTrust);
2063 * Returns the negative trust.
2065 * @return The negative trust
2067 public int getNegativeTrust() {
2068 return options.getIntegerOption("NegativeTrust").get();
2072 * Sets the negative trust.
2074 * @param negativeTrust
2075 * The negative trust, or {@code null} to restore it to the
2077 * @return The preferences
2079 public Preferences setNegativeTrust(Integer negativeTrust) {
2080 options.getIntegerOption("NegativeTrust").set(negativeTrust);
2085 * Returns the trust comment. This is the comment that is set in the web
2086 * of trust when a trust value is assigned to an identity.
2088 * @return The trust comment
2090 public String getTrustComment() {
2091 return options.getStringOption("TrustComment").get();
2095 * Sets the trust comment.
2097 * @param trustComment
2098 * The trust comment, or {@code null} to restore it to the
2100 * @return This preferences
2102 public Preferences setTrustComment(String trustComment) {
2103 options.getStringOption("TrustComment").set(trustComment);
2108 * Returns whether the rescue mode is active.
2110 * @return {@code true} if the rescue mode is active, {@code false}
2113 public boolean isSoneRescueMode() {
2114 return options.getBooleanOption("SoneRescueMode").get();
2118 * Sets whether the rescue mode is active.
2120 * @param soneRescueMode
2121 * {@code true} if the rescue mode is active, {@code false}
2123 * @return This preferences
2125 public Preferences setSoneRescueMode(Boolean soneRescueMode) {
2126 options.getBooleanOption("SoneRescueMode").set(soneRescueMode);
2131 * Returns whether Sone should clear its settings on the next restart.
2132 * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
2133 * to return {@code true} as well!
2135 * @return {@code true} if Sone should clear its settings on the next
2136 * restart, {@code false} otherwise
2138 public boolean isClearOnNextRestart() {
2139 return options.getBooleanOption("ClearOnNextRestart").get();
2143 * Sets whether Sone will clear its settings on the next restart.
2145 * @param clearOnNextRestart
2146 * {@code true} if Sone should clear its settings on the next
2147 * restart, {@code false} otherwise
2148 * @return This preferences
2150 public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
2151 options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
2156 * Returns whether Sone should really clear its settings on next
2157 * restart. This is a confirmation option that needs to be set in
2158 * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
2159 * settings on the next restart.
2161 * @return {@code true} if Sone should really clear its settings on the
2162 * next restart, {@code false} otherwise
2164 public boolean isReallyClearOnNextRestart() {
2165 return options.getBooleanOption("ReallyClearOnNextRestart").get();
2169 * Sets whether Sone should really clear its settings on the next
2172 * @param reallyClearOnNextRestart
2173 * {@code true} if Sone should really clear its settings on
2174 * the next restart, {@code false} otherwise
2175 * @return This preferences
2177 public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
2178 options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);