Add option to require full access FCP connections to change data.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
1 /*
2  * Sone - Core.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
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;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.core.Options.DefaultOption;
32 import net.pterodactylus.sone.core.Options.Option;
33 import net.pterodactylus.sone.core.Options.OptionWatcher;
34 import net.pterodactylus.sone.data.Client;
35 import net.pterodactylus.sone.data.Post;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Profile.Field;
38 import net.pterodactylus.sone.data.Reply;
39 import net.pterodactylus.sone.data.Sone;
40 import net.pterodactylus.sone.fcp.FcpInterface;
41 import net.pterodactylus.sone.freenet.wot.Identity;
42 import net.pterodactylus.sone.freenet.wot.IdentityListener;
43 import net.pterodactylus.sone.freenet.wot.IdentityManager;
44 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
45 import net.pterodactylus.sone.freenet.wot.Trust;
46 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
47 import net.pterodactylus.sone.main.SonePlugin;
48 import net.pterodactylus.util.config.Configuration;
49 import net.pterodactylus.util.config.ConfigurationException;
50 import net.pterodactylus.util.logging.Logging;
51 import net.pterodactylus.util.number.Numbers;
52 import net.pterodactylus.util.validation.Validation;
53 import net.pterodactylus.util.version.Version;
54 import freenet.keys.FreenetURI;
55
56 /**
57  * The Sone core.
58  *
59  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60  */
61 public class Core implements IdentityListener, UpdateListener {
62
63         /**
64          * Enumeration for the possible states of a {@link Sone}.
65          *
66          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
67          */
68         public enum SoneStatus {
69
70                 /** The Sone is unknown, i.e. not yet downloaded. */
71                 unknown,
72
73                 /** The Sone is idle, i.e. not being downloaded or inserted. */
74                 idle,
75
76                 /** The Sone is currently being inserted. */
77                 inserting,
78
79                 /** The Sone is currently being downloaded. */
80                 downloading,
81         }
82
83         /** The logger. */
84         private static final Logger logger = Logging.getLogger(Core.class);
85
86         /** The options. */
87         private final Options options = new Options();
88
89         /** The preferences. */
90         private final Preferences preferences = new Preferences(options);
91
92         /** The core listener manager. */
93         private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
94
95         /** The configuration. */
96         private Configuration configuration;
97
98         /** Whether we’re currently saving the configuration. */
99         private boolean storingConfiguration = false;
100
101         /** The identity manager. */
102         private final IdentityManager identityManager;
103
104         /** Interface to freenet. */
105         private final FreenetInterface freenetInterface;
106
107         /** The Sone downloader. */
108         private final SoneDownloader soneDownloader;
109
110         /** The update checker. */
111         private final UpdateChecker updateChecker;
112
113         /** The FCP interface. */
114         private volatile FcpInterface fcpInterface;
115
116         /** Whether the core has been stopped. */
117         private volatile boolean stopped;
118
119         /** The Sones’ statuses. */
120         /* synchronize access on itself. */
121         private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
122
123         /** Locked local Sones. */
124         /* synchronize on itself. */
125         private final Set<Sone> lockedSones = new HashSet<Sone>();
126
127         /** Sone inserters. */
128         /* synchronize access on this on localSones. */
129         private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
130
131         /** All local Sones. */
132         /* synchronize access on this on itself. */
133         private Map<String, Sone> localSones = new HashMap<String, Sone>();
134
135         /** All remote Sones. */
136         /* synchronize access on this on itself. */
137         private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
138
139         /** All new Sones. */
140         private Set<String> newSones = new HashSet<String>();
141
142         /** All known Sones. */
143         /* synchronize access on {@link #newSones}. */
144         private Set<String> knownSones = new HashSet<String>();
145
146         /** All posts. */
147         private Map<String, Post> posts = new HashMap<String, Post>();
148
149         /** All new posts. */
150         private Set<String> newPosts = new HashSet<String>();
151
152         /** All known posts. */
153         /* synchronize access on {@link #newPosts}. */
154         private Set<String> knownPosts = new HashSet<String>();
155
156         /** All replies. */
157         private Map<String, Reply> replies = new HashMap<String, Reply>();
158
159         /** All new replies. */
160         private Set<String> newReplies = new HashSet<String>();
161
162         /** All known replies. */
163         private Set<String> knownReplies = new HashSet<String>();
164
165         /** All bookmarked posts. */
166         /* synchronize access on itself. */
167         private Set<String> bookmarkedPosts = new HashSet<String>();
168
169         /** Trusted identities, sorted by own identities. */
170         private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
171
172         /**
173          * Creates a new core.
174          *
175          * @param configuration
176          *            The configuration of the core
177          * @param freenetInterface
178          *            The freenet interface
179          * @param identityManager
180          *            The identity manager
181          */
182         public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
183                 this.configuration = configuration;
184                 this.freenetInterface = freenetInterface;
185                 this.identityManager = identityManager;
186                 this.soneDownloader = new SoneDownloader(this, freenetInterface);
187                 this.updateChecker = new UpdateChecker(freenetInterface);
188         }
189
190         //
191         // LISTENER MANAGEMENT
192         //
193
194         /**
195          * Adds a new core listener.
196          *
197          * @param coreListener
198          *            The listener to add
199          */
200         public void addCoreListener(CoreListener coreListener) {
201                 coreListenerManager.addListener(coreListener);
202         }
203
204         /**
205          * Removes a core listener.
206          *
207          * @param coreListener
208          *            The listener to remove
209          */
210         public void removeCoreListener(CoreListener coreListener) {
211                 coreListenerManager.removeListener(coreListener);
212         }
213
214         //
215         // ACCESSORS
216         //
217
218         /**
219          * Sets the configuration to use. This will automatically save the current
220          * configuration to the given configuration.
221          *
222          * @param configuration
223          *            The new configuration to use
224          */
225         public void setConfiguration(Configuration configuration) {
226                 this.configuration = configuration;
227                 saveConfiguration();
228         }
229
230         /**
231          * Returns the options used by the core.
232          *
233          * @return The options of the core
234          */
235         public Preferences getPreferences() {
236                 return preferences;
237         }
238
239         /**
240          * Returns the identity manager used by the core.
241          *
242          * @return The identity manager
243          */
244         public IdentityManager getIdentityManager() {
245                 return identityManager;
246         }
247
248         /**
249          * Returns the update checker.
250          *
251          * @return The update checker
252          */
253         public UpdateChecker getUpdateChecker() {
254                 return updateChecker;
255         }
256
257         /**
258          * Sets the FCP interface to use.
259          *
260          * @param fcpInterface
261          *            The FCP interface to use
262          */
263         public void setFcpInterface(FcpInterface fcpInterface) {
264                 this.fcpInterface = fcpInterface;
265         }
266
267         /**
268          * Returns the status of the given Sone.
269          *
270          * @param sone
271          *            The Sone to get the status for
272          * @return The status of the Sone
273          */
274         public SoneStatus getSoneStatus(Sone sone) {
275                 synchronized (soneStatuses) {
276                         return soneStatuses.get(sone);
277                 }
278         }
279
280         /**
281          * Sets the status of the given Sone.
282          *
283          * @param sone
284          *            The Sone to set the status of
285          * @param soneStatus
286          *            The status to set
287          */
288         public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
289                 synchronized (soneStatuses) {
290                         soneStatuses.put(sone, soneStatus);
291                 }
292         }
293
294         /**
295          * Returns whether the given Sone is currently locked.
296          *
297          * @param sone
298          *            The sone to check
299          * @return {@code true} if the Sone is locked, {@code false} if it is not
300          */
301         public boolean isLocked(Sone sone) {
302                 synchronized (lockedSones) {
303                         return lockedSones.contains(sone);
304                 }
305         }
306
307         /**
308          * Returns all Sones, remote and local.
309          *
310          * @return All Sones
311          */
312         public Set<Sone> getSones() {
313                 Set<Sone> allSones = new HashSet<Sone>();
314                 allSones.addAll(getLocalSones());
315                 allSones.addAll(getRemoteSones());
316                 return allSones;
317         }
318
319         /**
320          * Returns the Sone with the given ID, regardless whether it’s local or
321          * remote.
322          *
323          * @param id
324          *            The ID of the Sone to get
325          * @return The Sone with the given ID, or {@code null} if there is no such
326          *         Sone
327          */
328         public Sone getSone(String id) {
329                 return getSone(id, true);
330         }
331
332         /**
333          * Returns the Sone with the given ID, regardless whether it’s local or
334          * remote.
335          *
336          * @param id
337          *            The ID of the Sone to get
338          * @param create
339          *            {@code true} to create a new Sone if none exists,
340          *            {@code false} to return {@code null} if a Sone with the given
341          *            ID does not exist
342          * @return The Sone with the given ID, or {@code null} if there is no such
343          *         Sone
344          */
345         public Sone getSone(String id, boolean create) {
346                 if (isLocalSone(id)) {
347                         return getLocalSone(id);
348                 }
349                 return getRemoteSone(id, create);
350         }
351
352         /**
353          * Checks whether the core knows a Sone with the given ID.
354          *
355          * @param id
356          *            The ID of the Sone
357          * @return {@code true} if there is a Sone with the given ID, {@code false}
358          *         otherwise
359          */
360         public boolean hasSone(String id) {
361                 return isLocalSone(id) || isRemoteSone(id);
362         }
363
364         /**
365          * Returns whether the given Sone is a local Sone.
366          *
367          * @param sone
368          *            The Sone to check for its locality
369          * @return {@code true} if the given Sone is local, {@code false} otherwise
370          */
371         public boolean isLocalSone(Sone sone) {
372                 synchronized (localSones) {
373                         return localSones.containsKey(sone.getId());
374                 }
375         }
376
377         /**
378          * Returns whether the given ID is the ID of a local Sone.
379          *
380          * @param id
381          *            The Sone ID to check for its locality
382          * @return {@code true} if the given ID is a local Sone, {@code false}
383          *         otherwise
384          */
385         public boolean isLocalSone(String id) {
386                 synchronized (localSones) {
387                         return localSones.containsKey(id);
388                 }
389         }
390
391         /**
392          * Returns all local Sones.
393          *
394          * @return All local Sones
395          */
396         public Set<Sone> getLocalSones() {
397                 synchronized (localSones) {
398                         return new HashSet<Sone>(localSones.values());
399                 }
400         }
401
402         /**
403          * Returns the local Sone with the given ID.
404          *
405          * @param id
406          *            The ID of the Sone to get
407          * @return The Sone with the given ID
408          */
409         public Sone getLocalSone(String id) {
410                 return getLocalSone(id, true);
411         }
412
413         /**
414          * Returns the local Sone with the given ID, optionally creating a new Sone.
415          *
416          * @param id
417          *            The ID of the Sone
418          * @param create
419          *            {@code true} to create a new Sone if none exists,
420          *            {@code false} to return null if none exists
421          * @return The Sone with the given ID, or {@code null}
422          */
423         public Sone getLocalSone(String id, boolean create) {
424                 synchronized (localSones) {
425                         Sone sone = localSones.get(id);
426                         if ((sone == null) && create) {
427                                 sone = new Sone(id);
428                                 localSones.put(id, sone);
429                                 setSoneStatus(sone, SoneStatus.unknown);
430                         }
431                         return sone;
432                 }
433         }
434
435         /**
436          * Returns all remote Sones.
437          *
438          * @return All remote Sones
439          */
440         public Set<Sone> getRemoteSones() {
441                 synchronized (remoteSones) {
442                         return new HashSet<Sone>(remoteSones.values());
443                 }
444         }
445
446         /**
447          * Returns the remote Sone with the given ID.
448          *
449          * @param id
450          *            The ID of the remote Sone to get
451          * @return The Sone with the given ID
452          */
453         public Sone getRemoteSone(String id) {
454                 return getRemoteSone(id, true);
455         }
456
457         /**
458          * Returns the remote Sone with the given ID.
459          *
460          * @param id
461          *            The ID of the remote Sone to get
462          * @param create
463          *            {@code true} to always create a Sone, {@code false} to return
464          *            {@code null} if no Sone with the given ID exists
465          * @return The Sone with the given ID
466          */
467         public Sone getRemoteSone(String id, boolean create) {
468                 synchronized (remoteSones) {
469                         Sone sone = remoteSones.get(id);
470                         if ((sone == null) && create) {
471                                 sone = new Sone(id);
472                                 remoteSones.put(id, sone);
473                                 setSoneStatus(sone, SoneStatus.unknown);
474                         }
475                         return sone;
476                 }
477         }
478
479         /**
480          * Returns whether the given Sone is a remote Sone.
481          *
482          * @param sone
483          *            The Sone to check
484          * @return {@code true} if the given Sone is a remote Sone, {@code false}
485          *         otherwise
486          */
487         public boolean isRemoteSone(Sone sone) {
488                 synchronized (remoteSones) {
489                         return remoteSones.containsKey(sone.getId());
490                 }
491         }
492
493         /**
494          * Returns whether the Sone with the given ID is a remote Sone.
495          *
496          * @param id
497          *            The ID of the Sone to check
498          * @return {@code true} if the Sone with the given ID is a remote Sone,
499          *         {@code false} otherwise
500          */
501         public boolean isRemoteSone(String id) {
502                 synchronized (remoteSones) {
503                         return remoteSones.containsKey(id);
504                 }
505         }
506
507         /**
508          * Returns whether the Sone with the given ID is a new Sone.
509          *
510          * @param soneId
511          *            The ID of the sone to check for
512          * @return {@code true} if the given Sone is new, false otherwise
513          */
514         public boolean isNewSone(String soneId) {
515                 synchronized (newSones) {
516                         return !knownSones.contains(soneId) && newSones.contains(soneId);
517                 }
518         }
519
520         /**
521          * Returns whether the given Sone has been modified.
522          *
523          * @param sone
524          *            The Sone to check for modifications
525          * @return {@code true} if a modification has been detected in the Sone,
526          *         {@code false} otherwise
527          */
528         public boolean isModifiedSone(Sone sone) {
529                 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
530         }
531
532         /**
533          * Returns whether the target Sone is trusted by the origin Sone.
534          *
535          * @param origin
536          *            The origin Sone
537          * @param target
538          *            The target Sone
539          * @return {@code true} if the target Sone is trusted by the origin Sone
540          */
541         public boolean isSoneTrusted(Sone origin, Sone target) {
542                 Validation.begin().isNotNull("Origin", origin).isNotNull("Target", target).check().isInstanceOf("Origin’s OwnIdentity", origin.getIdentity(), OwnIdentity.class).check();
543                 return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity());
544         }
545
546         /**
547          * Returns the post with the given ID.
548          *
549          * @param postId
550          *            The ID of the post to get
551          * @return The post with the given ID, or a new post with the given ID
552          */
553         public Post getPost(String postId) {
554                 return getPost(postId, true);
555         }
556
557         /**
558          * Returns the post with the given ID, optionally creating a new post.
559          *
560          * @param postId
561          *            The ID of the post to get
562          * @param create
563          *            {@code true} it create a new post if no post with the given ID
564          *            exists, {@code false} to return {@code null}
565          * @return The post, or {@code null} if there is no such post
566          */
567         public Post getPost(String postId, boolean create) {
568                 synchronized (posts) {
569                         Post post = posts.get(postId);
570                         if ((post == null) && create) {
571                                 post = new Post(postId);
572                                 posts.put(postId, post);
573                         }
574                         return post;
575                 }
576         }
577
578         /**
579          * Returns whether the given post ID is new.
580          *
581          * @param postId
582          *            The post ID
583          * @return {@code true} if the post is considered to be new, {@code false}
584          *         otherwise
585          */
586         public boolean isNewPost(String postId) {
587                 synchronized (newPosts) {
588                         return !knownPosts.contains(postId) && newPosts.contains(postId);
589                 }
590         }
591
592         /**
593          * Returns all posts that have the given Sone as recipient.
594          *
595          * @see Post#getRecipient()
596          * @param recipient
597          *            The recipient of the posts
598          * @return All posts that have the given Sone as recipient
599          */
600         public Set<Post> getDirectedPosts(Sone recipient) {
601                 Validation.begin().isNotNull("Recipient", recipient).check();
602                 Set<Post> directedPosts = new HashSet<Post>();
603                 synchronized (posts) {
604                         for (Post post : posts.values()) {
605                                 if (recipient.equals(post.getRecipient())) {
606                                         directedPosts.add(post);
607                                 }
608                         }
609                 }
610                 return directedPosts;
611         }
612
613         /**
614          * Returns the reply with the given ID. If there is no reply with the given
615          * ID yet, a new one is created.
616          *
617          * @param replyId
618          *            The ID of the reply to get
619          * @return The reply
620          */
621         public Reply getReply(String replyId) {
622                 return getReply(replyId, true);
623         }
624
625         /**
626          * Returns the reply with the given ID. If there is no reply with the given
627          * ID yet, a new one is created, unless {@code create} is false in which
628          * case {@code null} is returned.
629          *
630          * @param replyId
631          *            The ID of the reply to get
632          * @param create
633          *            {@code true} to always return a {@link Reply}, {@code false}
634          *            to return {@code null} if no reply can be found
635          * @return The reply, or {@code null} if there is no such reply
636          */
637         public Reply getReply(String replyId, boolean create) {
638                 synchronized (replies) {
639                         Reply reply = replies.get(replyId);
640                         if (create && (reply == null)) {
641                                 reply = new Reply(replyId);
642                                 replies.put(replyId, reply);
643                         }
644                         return reply;
645                 }
646         }
647
648         /**
649          * Returns all replies for the given post, order ascending by time.
650          *
651          * @param post
652          *            The post to get all replies for
653          * @return All replies for the given post
654          */
655         public List<Reply> getReplies(Post post) {
656                 Set<Sone> sones = getSones();
657                 List<Reply> replies = new ArrayList<Reply>();
658                 for (Sone sone : sones) {
659                         for (Reply reply : sone.getReplies()) {
660                                 if (reply.getPost().equals(post)) {
661                                         replies.add(reply);
662                                 }
663                         }
664                 }
665                 Collections.sort(replies, Reply.TIME_COMPARATOR);
666                 return replies;
667         }
668
669         /**
670          * Returns whether the reply with the given ID is new.
671          *
672          * @param replyId
673          *            The ID of the reply to check
674          * @return {@code true} if the reply is considered to be new, {@code false}
675          *         otherwise
676          */
677         public boolean isNewReply(String replyId) {
678                 synchronized (newReplies) {
679                         return !knownReplies.contains(replyId) && newReplies.contains(replyId);
680                 }
681         }
682
683         /**
684          * Returns all Sones that have liked the given post.
685          *
686          * @param post
687          *            The post to get the liking Sones for
688          * @return The Sones that like the given post
689          */
690         public Set<Sone> getLikes(Post post) {
691                 Set<Sone> sones = new HashSet<Sone>();
692                 for (Sone sone : getSones()) {
693                         if (sone.getLikedPostIds().contains(post.getId())) {
694                                 sones.add(sone);
695                         }
696                 }
697                 return sones;
698         }
699
700         /**
701          * Returns all Sones that have liked the given reply.
702          *
703          * @param reply
704          *            The reply to get the liking Sones for
705          * @return The Sones that like the given reply
706          */
707         public Set<Sone> getLikes(Reply reply) {
708                 Set<Sone> sones = new HashSet<Sone>();
709                 for (Sone sone : getSones()) {
710                         if (sone.getLikedReplyIds().contains(reply.getId())) {
711                                 sones.add(sone);
712                         }
713                 }
714                 return sones;
715         }
716
717         /**
718          * Returns whether the given post is bookmarked.
719          *
720          * @param post
721          *            The post to check
722          * @return {@code true} if the given post is bookmarked, {@code false}
723          *         otherwise
724          */
725         public boolean isBookmarked(Post post) {
726                 return isPostBookmarked(post.getId());
727         }
728
729         /**
730          * Returns whether the post with the given ID is bookmarked.
731          *
732          * @param id
733          *            The ID of the post to check
734          * @return {@code true} if the post with the given ID is bookmarked,
735          *         {@code false} otherwise
736          */
737         public boolean isPostBookmarked(String id) {
738                 synchronized (bookmarkedPosts) {
739                         return bookmarkedPosts.contains(id);
740                 }
741         }
742
743         /**
744          * Returns all currently known bookmarked posts.
745          *
746          * @return All bookmarked posts
747          */
748         public Set<Post> getBookmarkedPosts() {
749                 Set<Post> posts = new HashSet<Post>();
750                 synchronized (bookmarkedPosts) {
751                         for (String bookmarkedPostId : bookmarkedPosts) {
752                                 Post post = getPost(bookmarkedPostId, false);
753                                 if (post != null) {
754                                         posts.add(post);
755                                 }
756                         }
757                 }
758                 return posts;
759         }
760
761         //
762         // ACTIONS
763         //
764
765         /**
766          * Locks the given Sone. A locked Sone will not be inserted by
767          * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
768          * again.
769          *
770          * @param sone
771          *            The sone to lock
772          */
773         public void lockSone(Sone sone) {
774                 synchronized (lockedSones) {
775                         if (lockedSones.add(sone)) {
776                                 coreListenerManager.fireSoneLocked(sone);
777                         }
778                 }
779         }
780
781         /**
782          * Unlocks the given Sone.
783          *
784          * @see #lockSone(Sone)
785          * @param sone
786          *            The sone to unlock
787          */
788         public void unlockSone(Sone sone) {
789                 synchronized (lockedSones) {
790                         if (lockedSones.remove(sone)) {
791                                 coreListenerManager.fireSoneUnlocked(sone);
792                         }
793                 }
794         }
795
796         /**
797          * Adds a local Sone from the given ID which has to be the ID of an own
798          * identity.
799          *
800          * @param id
801          *            The ID of an own identity to add a Sone for
802          * @return The added (or already existing) Sone
803          */
804         public Sone addLocalSone(String id) {
805                 synchronized (localSones) {
806                         if (localSones.containsKey(id)) {
807                                 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
808                                 return localSones.get(id);
809                         }
810                         OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
811                         if (ownIdentity == null) {
812                                 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
813                                 return null;
814                         }
815                         return addLocalSone(ownIdentity);
816                 }
817         }
818
819         /**
820          * Adds a local Sone from the given own identity.
821          *
822          * @param ownIdentity
823          *            The own identity to create a Sone from
824          * @return The added (or already existing) Sone
825          */
826         public Sone addLocalSone(OwnIdentity ownIdentity) {
827                 if (ownIdentity == null) {
828                         logger.log(Level.WARNING, "Given OwnIdentity is null!");
829                         return null;
830                 }
831                 synchronized (localSones) {
832                         final Sone sone;
833                         try {
834                                 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
835                         } catch (MalformedURLException mue1) {
836                                 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
837                                 return null;
838                         }
839                         sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
840                         sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
841                         /* TODO - load posts ’n stuff */
842                         localSones.put(ownIdentity.getId(), sone);
843                         final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
844                         soneInserters.put(sone, soneInserter);
845                         setSoneStatus(sone, SoneStatus.idle);
846                         loadSone(sone);
847                         if (!preferences.isSoneRescueMode()) {
848                                 soneInserter.start();
849                         }
850                         new Thread(new Runnable() {
851
852                                 @Override
853                                 @SuppressWarnings("synthetic-access")
854                                 public void run() {
855                                         if (!preferences.isSoneRescueMode()) {
856                                                 return;
857                                         }
858                                         logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
859                                         coreListenerManager.fireRescuingSone(sone);
860                                         lockSone(sone);
861                                         long edition = sone.getLatestEdition();
862                                         while (!stopped && (edition >= 0) && preferences.isSoneRescueMode()) {
863                                                 logger.log(Level.FINE, "Downloading edition " + edition + "…");
864                                                 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
865                                                 --edition;
866                                         }
867                                         logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
868                                         saveSone(sone);
869                                         coreListenerManager.fireRescuedSone(sone);
870                                         soneInserter.start();
871                                 }
872
873                         }, "Sone Downloader").start();
874                         return sone;
875                 }
876         }
877
878         /**
879          * Creates a new Sone for the given own identity.
880          *
881          * @param ownIdentity
882          *            The own identity to create a Sone for
883          * @return The created Sone
884          */
885         public Sone createSone(OwnIdentity ownIdentity) {
886                 try {
887                         ownIdentity.addContext("Sone");
888                 } catch (WebOfTrustException wote1) {
889                         logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
890                         return null;
891                 }
892                 Sone sone = addLocalSone(ownIdentity);
893                 return sone;
894         }
895
896         /**
897          * Adds the Sone of the given identity.
898          *
899          * @param identity
900          *            The identity whose Sone to add
901          * @return The added or already existing Sone
902          */
903         public Sone addRemoteSone(Identity identity) {
904                 if (identity == null) {
905                         logger.log(Level.WARNING, "Given Identity is null!");
906                         return null;
907                 }
908                 synchronized (remoteSones) {
909                         final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
910                         boolean newSone = sone.getRequestUri() == null;
911                         sone.setRequestUri(getSoneUri(identity.getRequestUri()));
912                         sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
913                         if (newSone) {
914                                 synchronized (newSones) {
915                                         newSone = !knownSones.contains(sone.getId());
916                                         if (newSone) {
917                                                 newSones.add(sone.getId());
918                                         }
919                                 }
920                                 if (newSone) {
921                                         coreListenerManager.fireNewSoneFound(sone);
922                                         for (Sone localSone : getLocalSones()) {
923                                                 if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
924                                                         localSone.addFriend(sone.getId());
925                                                 }
926                                         }
927                                 }
928                         }
929                         remoteSones.put(identity.getId(), sone);
930                         soneDownloader.addSone(sone);
931                         setSoneStatus(sone, SoneStatus.unknown);
932                         new Thread(new Runnable() {
933
934                                 @Override
935                                 @SuppressWarnings("synthetic-access")
936                                 public void run() {
937                                         soneDownloader.fetchSone(sone);
938                                 }
939
940                         }, "Sone Downloader").start();
941                         return sone;
942                 }
943         }
944
945         /**
946          * Retrieves the trust relationship from the origin to the target. If the
947          * trust relationship can not be retrieved, {@code null} is returned.
948          *
949          * @see Identity#getTrust(OwnIdentity)
950          * @param origin
951          *            The origin of the trust tree
952          * @param target
953          *            The target of the trust
954          * @return The trust relationship
955          */
956         public Trust getTrust(Sone origin, Sone target) {
957                 if (!isLocalSone(origin)) {
958                         logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
959                         return null;
960                 }
961                 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
962         }
963
964         /**
965          * Sets the trust value of the given origin Sone for the target Sone.
966          *
967          * @param origin
968          *            The origin Sone
969          * @param target
970          *            The target Sone
971          * @param trustValue
972          *            The trust value (from {@code -100} to {@code 100})
973          */
974         public void setTrust(Sone origin, Sone target, int trustValue) {
975                 Validation.begin().isNotNull("Trust Origin", origin).check().isInstanceOf("Trust Origin", origin.getIdentity(), OwnIdentity.class).isNotNull("Trust Target", target).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check();
976                 try {
977                         ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
978                 } catch (WebOfTrustException wote1) {
979                         logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
980                 }
981         }
982
983         /**
984          * Removes any trust assignment for the given target Sone.
985          *
986          * @param origin
987          *            The trust origin
988          * @param target
989          *            The trust target
990          */
991         public void removeTrust(Sone origin, Sone target) {
992                 Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
993                 try {
994                         ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
995                 } catch (WebOfTrustException wote1) {
996                         logger.log(Level.WARNING, "Could not remove trust for Sone: " + target, wote1);
997                 }
998         }
999
1000         /**
1001          * Assigns the configured positive trust value for the given target.
1002          *
1003          * @param origin
1004          *            The trust origin
1005          * @param target
1006          *            The trust target
1007          */
1008         public void trustSone(Sone origin, Sone target) {
1009                 setTrust(origin, target, preferences.getPositiveTrust());
1010         }
1011
1012         /**
1013          * Assigns the configured negative trust value for the given target.
1014          *
1015          * @param origin
1016          *            The trust origin
1017          * @param target
1018          *            The trust target
1019          */
1020         public void distrustSone(Sone origin, Sone target) {
1021                 setTrust(origin, target, preferences.getNegativeTrust());
1022         }
1023
1024         /**
1025          * Removes the trust assignment for the given target.
1026          *
1027          * @param origin
1028          *            The trust origin
1029          * @param target
1030          *            The trust target
1031          */
1032         public void untrustSone(Sone origin, Sone target) {
1033                 removeTrust(origin, target);
1034         }
1035
1036         /**
1037          * Updates the stores Sone with the given Sone.
1038          *
1039          * @param sone
1040          *            The updated Sone
1041          */
1042         public void updateSone(Sone sone) {
1043                 if (hasSone(sone.getId())) {
1044                         boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode();
1045                         Sone storedSone = getSone(sone.getId());
1046                         if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
1047                                 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
1048                                 return;
1049                         }
1050                         synchronized (posts) {
1051                                 if (!soneRescueMode) {
1052                                         for (Post post : storedSone.getPosts()) {
1053                                                 posts.remove(post.getId());
1054                                                 if (!sone.getPosts().contains(post)) {
1055                                                         coreListenerManager.firePostRemoved(post);
1056                                                 }
1057                                         }
1058                                 }
1059                                 List<Post> storedPosts = storedSone.getPosts();
1060                                 synchronized (newPosts) {
1061                                         for (Post post : sone.getPosts()) {
1062                                                 post.setSone(storedSone);
1063                                                 if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
1064                                                         newPosts.add(post.getId());
1065                                                         coreListenerManager.fireNewPostFound(post);
1066                                                 }
1067                                                 posts.put(post.getId(), post);
1068                                         }
1069                                 }
1070                         }
1071                         synchronized (replies) {
1072                                 if (!soneRescueMode) {
1073                                         for (Reply reply : storedSone.getReplies()) {
1074                                                 replies.remove(reply.getId());
1075                                                 if (!sone.getReplies().contains(reply)) {
1076                                                         coreListenerManager.fireReplyRemoved(reply);
1077                                                 }
1078                                         }
1079                                 }
1080                                 Set<Reply> storedReplies = storedSone.getReplies();
1081                                 synchronized (newReplies) {
1082                                         for (Reply reply : sone.getReplies()) {
1083                                                 reply.setSone(storedSone);
1084                                                 if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
1085                                                         newReplies.add(reply.getId());
1086                                                         coreListenerManager.fireNewReplyFound(reply);
1087                                                 }
1088                                                 replies.put(reply.getId(), reply);
1089                                         }
1090                                 }
1091                         }
1092                         synchronized (storedSone) {
1093                                 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
1094                                         storedSone.setTime(sone.getTime());
1095                                 }
1096                                 storedSone.setClient(sone.getClient());
1097                                 storedSone.setProfile(sone.getProfile());
1098                                 if (soneRescueMode) {
1099                                         for (Post post : sone.getPosts()) {
1100                                                 storedSone.addPost(post);
1101                                         }
1102                                         for (Reply reply : sone.getReplies()) {
1103                                                 storedSone.addReply(reply);
1104                                         }
1105                                         for (String likedPostId : sone.getLikedPostIds()) {
1106                                                 storedSone.addLikedPostId(likedPostId);
1107                                         }
1108                                         for (String likedReplyId : sone.getLikedReplyIds()) {
1109                                                 storedSone.addLikedReplyId(likedReplyId);
1110                                         }
1111                                 } else {
1112                                         storedSone.setPosts(sone.getPosts());
1113                                         storedSone.setReplies(sone.getReplies());
1114                                         storedSone.setLikePostIds(sone.getLikedPostIds());
1115                                         storedSone.setLikeReplyIds(sone.getLikedReplyIds());
1116                                 }
1117                                 storedSone.setLatestEdition(sone.getLatestEdition());
1118                         }
1119                 }
1120         }
1121
1122         /**
1123          * Deletes the given Sone. This will remove the Sone from the
1124          * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1125          * and remove the context from its identity.
1126          *
1127          * @param sone
1128          *            The Sone to delete
1129          */
1130         public void deleteSone(Sone sone) {
1131                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1132                         logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1133                         return;
1134                 }
1135                 synchronized (localSones) {
1136                         if (!localSones.containsKey(sone.getId())) {
1137                                 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1138                                 return;
1139                         }
1140                         localSones.remove(sone.getId());
1141                         soneInserters.remove(sone).stop();
1142                 }
1143                 try {
1144                         ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1145                         ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1146                 } catch (WebOfTrustException wote1) {
1147                         logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1148                 }
1149                 try {
1150                         configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1151                 } catch (ConfigurationException ce1) {
1152                         logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1153                 }
1154         }
1155
1156         /**
1157          * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
1158          * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
1159          *
1160          * @param sone
1161          *            The Sone to mark as known
1162          */
1163         public void markSoneKnown(Sone sone) {
1164                 synchronized (newSones) {
1165                         if (newSones.remove(sone.getId())) {
1166                                 knownSones.add(sone.getId());
1167                                 coreListenerManager.fireMarkSoneKnown(sone);
1168                                 saveConfiguration();
1169                         }
1170                 }
1171         }
1172
1173         /**
1174          * Loads and updates the given Sone from the configuration. If any error is
1175          * encountered, loading is aborted and the given Sone is not changed.
1176          *
1177          * @param sone
1178          *            The Sone to load and update
1179          */
1180         public void loadSone(Sone sone) {
1181                 if (!isLocalSone(sone)) {
1182                         logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1183                         return;
1184                 }
1185
1186                 /* load Sone. */
1187                 String sonePrefix = "Sone/" + sone.getId();
1188                 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1189                 if (soneTime == null) {
1190                         logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1191                         return;
1192                 }
1193                 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1194
1195                 /* load profile. */
1196                 Profile profile = new Profile();
1197                 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1198                 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1199                 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1200                 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1201                 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1202                 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1203
1204                 /* load profile fields. */
1205                 while (true) {
1206                         String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
1207                         String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
1208                         if (fieldName == null) {
1209                                 break;
1210                         }
1211                         String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
1212                         profile.addField(fieldName).setValue(fieldValue);
1213                 }
1214
1215                 /* load posts. */
1216                 Set<Post> posts = new HashSet<Post>();
1217                 while (true) {
1218                         String postPrefix = sonePrefix + "/Posts/" + posts.size();
1219                         String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1220                         if (postId == null) {
1221                                 break;
1222                         }
1223                         String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1224                         long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1225                         String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1226                         if ((postTime == 0) || (postText == null)) {
1227                                 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1228                                 return;
1229                         }
1230                         Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1231                         if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1232                                 post.setRecipient(getSone(postRecipientId));
1233                         }
1234                         posts.add(post);
1235                 }
1236
1237                 /* load replies. */
1238                 Set<Reply> replies = new HashSet<Reply>();
1239                 while (true) {
1240                         String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1241                         String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1242                         if (replyId == null) {
1243                                 break;
1244                         }
1245                         String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1246                         long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1247                         String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1248                         if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1249                                 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1250                                 return;
1251                         }
1252                         replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1253                 }
1254
1255                 /* load post likes. */
1256                 Set<String> likedPostIds = new HashSet<String>();
1257                 while (true) {
1258                         String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1259                         if (likedPostId == null) {
1260                                 break;
1261                         }
1262                         likedPostIds.add(likedPostId);
1263                 }
1264
1265                 /* load reply likes. */
1266                 Set<String> likedReplyIds = new HashSet<String>();
1267                 while (true) {
1268                         String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1269                         if (likedReplyId == null) {
1270                                 break;
1271                         }
1272                         likedReplyIds.add(likedReplyId);
1273                 }
1274
1275                 /* load friends. */
1276                 Set<String> friends = new HashSet<String>();
1277                 while (true) {
1278                         String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1279                         if (friendId == null) {
1280                                 break;
1281                         }
1282                         friends.add(friendId);
1283                 }
1284
1285                 /* load options. */
1286                 sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
1287                 sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
1288
1289                 /* if we’re still here, Sone was loaded successfully. */
1290                 synchronized (sone) {
1291                         sone.setTime(soneTime);
1292                         sone.setProfile(profile);
1293                         sone.setPosts(posts);
1294                         sone.setReplies(replies);
1295                         sone.setLikePostIds(likedPostIds);
1296                         sone.setLikeReplyIds(likedReplyIds);
1297                         sone.setFriends(friends);
1298                         soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1299                 }
1300                 synchronized (newSones) {
1301                         for (String friend : friends) {
1302                                 knownSones.add(friend);
1303                         }
1304                 }
1305                 synchronized (newPosts) {
1306                         for (Post post : posts) {
1307                                 knownPosts.add(post.getId());
1308                         }
1309                 }
1310                 synchronized (newReplies) {
1311                         for (Reply reply : replies) {
1312                                 knownReplies.add(reply.getId());
1313                         }
1314                 }
1315         }
1316
1317         /**
1318          * Saves the given Sone. This will persist all local settings for the given
1319          * Sone, such as the friends list and similar, private options.
1320          *
1321          * @param sone
1322          *            The Sone to save
1323          */
1324         public synchronized void saveSone(Sone sone) {
1325                 if (!isLocalSone(sone)) {
1326                         logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1327                         return;
1328                 }
1329                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1330                         logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1331                         return;
1332                 }
1333
1334                 logger.log(Level.INFO, "Saving Sone: %s", sone);
1335                 try {
1336                         ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1337
1338                         /* save Sone into configuration. */
1339                         String sonePrefix = "Sone/" + sone.getId();
1340                         configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1341                         configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1342
1343                         /* save profile. */
1344                         Profile profile = sone.getProfile();
1345                         configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1346                         configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1347                         configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1348                         configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1349                         configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1350                         configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1351
1352                         /* save profile fields. */
1353                         int fieldCounter = 0;
1354                         for (Field profileField : profile.getFields()) {
1355                                 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1356                                 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1357                                 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1358                         }
1359                         configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1360
1361                         /* save posts. */
1362                         int postCounter = 0;
1363                         for (Post post : sone.getPosts()) {
1364                                 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1365                                 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1366                                 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1367                                 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1368                                 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1369                         }
1370                         configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1371
1372                         /* save replies. */
1373                         int replyCounter = 0;
1374                         for (Reply reply : sone.getReplies()) {
1375                                 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1376                                 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1377                                 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1378                                 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1379                                 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1380                         }
1381                         configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1382
1383                         /* save post likes. */
1384                         int postLikeCounter = 0;
1385                         for (String postId : sone.getLikedPostIds()) {
1386                                 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1387                         }
1388                         configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1389
1390                         /* save reply likes. */
1391                         int replyLikeCounter = 0;
1392                         for (String replyId : sone.getLikedReplyIds()) {
1393                                 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1394                         }
1395                         configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1396
1397                         /* save friends. */
1398                         int friendCounter = 0;
1399                         for (String friendId : sone.getFriends()) {
1400                                 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1401                         }
1402                         configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1403
1404                         /* save options. */
1405                         configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
1406
1407                         configuration.save();
1408                         logger.log(Level.INFO, "Sone %s saved.", sone);
1409                 } catch (ConfigurationException ce1) {
1410                         logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1411                 } catch (WebOfTrustException wote1) {
1412                         logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1413                 }
1414         }
1415
1416         /**
1417          * Creates a new post.
1418          *
1419          * @param sone
1420          *            The Sone that creates the post
1421          * @param text
1422          *            The text of the post
1423          * @return The created post
1424          */
1425         public Post createPost(Sone sone, String text) {
1426                 return createPost(sone, System.currentTimeMillis(), text);
1427         }
1428
1429         /**
1430          * Creates a new post.
1431          *
1432          * @param sone
1433          *            The Sone that creates the post
1434          * @param time
1435          *            The time of the post
1436          * @param text
1437          *            The text of the post
1438          * @return The created post
1439          */
1440         public Post createPost(Sone sone, long time, String text) {
1441                 return createPost(sone, null, time, text);
1442         }
1443
1444         /**
1445          * Creates a new post.
1446          *
1447          * @param sone
1448          *            The Sone that creates the post
1449          * @param recipient
1450          *            The recipient Sone, or {@code null} if this post does not have
1451          *            a recipient
1452          * @param text
1453          *            The text of the post
1454          * @return The created post
1455          */
1456         public Post createPost(Sone sone, Sone recipient, String text) {
1457                 return createPost(sone, recipient, System.currentTimeMillis(), text);
1458         }
1459
1460         /**
1461          * Creates a new post.
1462          *
1463          * @param sone
1464          *            The Sone that creates the post
1465          * @param recipient
1466          *            The recipient Sone, or {@code null} if this post does not have
1467          *            a recipient
1468          * @param time
1469          *            The time of the post
1470          * @param text
1471          *            The text of the post
1472          * @return The created post
1473          */
1474         public Post createPost(Sone sone, Sone recipient, long time, String text) {
1475                 if (!isLocalSone(sone)) {
1476                         logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1477                         return null;
1478                 }
1479                 Post post = new Post(sone, time, text);
1480                 if (recipient != null) {
1481                         post.setRecipient(recipient);
1482                 }
1483                 synchronized (posts) {
1484                         posts.put(post.getId(), post);
1485                 }
1486                 synchronized (newPosts) {
1487                         newPosts.add(post.getId());
1488                         coreListenerManager.fireNewPostFound(post);
1489                 }
1490                 sone.addPost(post);
1491                 saveSone(sone);
1492                 return post;
1493         }
1494
1495         /**
1496          * Deletes the given post.
1497          *
1498          * @param post
1499          *            The post to delete
1500          */
1501         public void deletePost(Post post) {
1502                 if (!isLocalSone(post.getSone())) {
1503                         logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1504                         return;
1505                 }
1506                 post.getSone().removePost(post);
1507                 synchronized (posts) {
1508                         posts.remove(post.getId());
1509                 }
1510                 synchronized (newPosts) {
1511                         markPostKnown(post);
1512                         knownPosts.remove(post.getId());
1513                 }
1514                 saveSone(post.getSone());
1515         }
1516
1517         /**
1518          * Marks the given post as known, if it is currently a new post (according
1519          * to {@link #isNewPost(String)}).
1520          *
1521          * @param post
1522          *            The post to mark as known
1523          */
1524         public void markPostKnown(Post post) {
1525                 synchronized (newPosts) {
1526                         if (newPosts.remove(post.getId())) {
1527                                 knownPosts.add(post.getId());
1528                                 coreListenerManager.fireMarkPostKnown(post);
1529                                 saveConfiguration();
1530                         }
1531                 }
1532         }
1533
1534         /**
1535          * Bookmarks the given post.
1536          *
1537          * @param post
1538          *            The post to bookmark
1539          */
1540         public void bookmark(Post post) {
1541                 bookmarkPost(post.getId());
1542         }
1543
1544         /**
1545          * Bookmarks the post with the given ID.
1546          *
1547          * @param id
1548          *            The ID of the post to bookmark
1549          */
1550         public void bookmarkPost(String id) {
1551                 synchronized (bookmarkedPosts) {
1552                         bookmarkedPosts.add(id);
1553                 }
1554         }
1555
1556         /**
1557          * Removes the given post from the bookmarks.
1558          *
1559          * @param post
1560          *            The post to unbookmark
1561          */
1562         public void unbookmark(Post post) {
1563                 unbookmarkPost(post.getId());
1564         }
1565
1566         /**
1567          * Removes the post with the given ID from the bookmarks.
1568          *
1569          * @param id
1570          *            The ID of the post to unbookmark
1571          */
1572         public void unbookmarkPost(String id) {
1573                 synchronized (bookmarkedPosts) {
1574                         bookmarkedPosts.remove(id);
1575                 }
1576         }
1577
1578         /**
1579          * Creates a new reply.
1580          *
1581          * @param sone
1582          *            The Sone that creates the reply
1583          * @param post
1584          *            The post that this reply refers to
1585          * @param text
1586          *            The text of the reply
1587          * @return The created reply
1588          */
1589         public Reply createReply(Sone sone, Post post, String text) {
1590                 return createReply(sone, post, System.currentTimeMillis(), text);
1591         }
1592
1593         /**
1594          * Creates a new reply.
1595          *
1596          * @param sone
1597          *            The Sone that creates the reply
1598          * @param post
1599          *            The post that this reply refers to
1600          * @param time
1601          *            The time of the reply
1602          * @param text
1603          *            The text of the reply
1604          * @return The created reply
1605          */
1606         public Reply createReply(Sone sone, Post post, long time, String text) {
1607                 if (!isLocalSone(sone)) {
1608                         logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1609                         return null;
1610                 }
1611                 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1612                 synchronized (replies) {
1613                         replies.put(reply.getId(), reply);
1614                 }
1615                 synchronized (newReplies) {
1616                         newReplies.add(reply.getId());
1617                         coreListenerManager.fireNewReplyFound(reply);
1618                 }
1619                 sone.addReply(reply);
1620                 saveSone(sone);
1621                 return reply;
1622         }
1623
1624         /**
1625          * Deletes the given reply.
1626          *
1627          * @param reply
1628          *            The reply to delete
1629          */
1630         public void deleteReply(Reply reply) {
1631                 Sone sone = reply.getSone();
1632                 if (!isLocalSone(sone)) {
1633                         logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1634                         return;
1635                 }
1636                 synchronized (replies) {
1637                         replies.remove(reply.getId());
1638                 }
1639                 synchronized (newReplies) {
1640                         markReplyKnown(reply);
1641                         knownReplies.remove(reply.getId());
1642                 }
1643                 sone.removeReply(reply);
1644                 saveSone(sone);
1645         }
1646
1647         /**
1648          * Marks the given reply as known, if it is currently a new reply (according
1649          * to {@link #isNewReply(String)}).
1650          *
1651          * @param reply
1652          *            The reply to mark as known
1653          */
1654         public void markReplyKnown(Reply reply) {
1655                 synchronized (newReplies) {
1656                         if (newReplies.remove(reply.getId())) {
1657                                 knownReplies.add(reply.getId());
1658                                 coreListenerManager.fireMarkReplyKnown(reply);
1659                                 saveConfiguration();
1660                         }
1661                 }
1662         }
1663
1664         /**
1665          * Starts the core.
1666          */
1667         public void start() {
1668                 loadConfiguration();
1669                 updateChecker.addUpdateListener(this);
1670                 updateChecker.start();
1671         }
1672
1673         /**
1674          * Stops the core.
1675          */
1676         public void stop() {
1677                 synchronized (localSones) {
1678                         for (SoneInserter soneInserter : soneInserters.values()) {
1679                                 soneInserter.stop();
1680                         }
1681                 }
1682                 updateChecker.stop();
1683                 updateChecker.removeUpdateListener(this);
1684                 soneDownloader.stop();
1685                 saveConfiguration();
1686                 stopped = true;
1687         }
1688
1689         /**
1690          * Saves the current options.
1691          */
1692         public void saveConfiguration() {
1693                 synchronized (configuration) {
1694                         if (storingConfiguration) {
1695                                 logger.log(Level.FINE, "Already storing configuration…");
1696                                 return;
1697                         }
1698                         storingConfiguration = true;
1699                 }
1700
1701                 /* store the options first. */
1702                 try {
1703                         configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
1704                         configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1705                         configuration.getIntValue("Option/PostsPerPage").setValue(options.getIntegerOption("PostsPerPage").getReal());
1706                         configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
1707                         configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
1708                         configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
1709                         configuration.getBooleanValue("Option/ActivateFcpInterface").setValue(options.getBooleanOption("ActivateFcpInterface").getReal());
1710                         configuration.getBooleanValue("Option/FcpWriteFromFullAccessOnly").setValue(options.getBooleanOption("FcpWriteFromFullAccessOnly").getReal());
1711                         configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1712                         configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1713                         configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1714
1715                         /* save known Sones. */
1716                         int soneCounter = 0;
1717                         synchronized (newSones) {
1718                                 for (String knownSoneId : knownSones) {
1719                                         configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1720                                 }
1721                                 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1722                         }
1723
1724                         /* save known posts. */
1725                         int postCounter = 0;
1726                         synchronized (newPosts) {
1727                                 for (String knownPostId : knownPosts) {
1728                                         configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1729                                 }
1730                                 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1731                         }
1732
1733                         /* save known replies. */
1734                         int replyCounter = 0;
1735                         synchronized (newReplies) {
1736                                 for (String knownReplyId : knownReplies) {
1737                                         configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1738                                 }
1739                                 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1740                         }
1741
1742                         /* save bookmarked posts. */
1743                         int bookmarkedPostCounter = 0;
1744                         synchronized (bookmarkedPosts) {
1745                                 for (String bookmarkedPostId : bookmarkedPosts) {
1746                                         configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
1747                                 }
1748                         }
1749                         configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
1750
1751                         /* now save it. */
1752                         configuration.save();
1753
1754                 } catch (ConfigurationException ce1) {
1755                         logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1756                 } finally {
1757                         synchronized (configuration) {
1758                                 storingConfiguration = false;
1759                         }
1760                 }
1761         }
1762
1763         //
1764         // PRIVATE METHODS
1765         //
1766
1767         /**
1768          * Loads the configuration.
1769          */
1770         @SuppressWarnings("unchecked")
1771         private void loadConfiguration() {
1772                 /* create options. */
1773                 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1774
1775                         @Override
1776                         public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1777                                 SoneInserter.setInsertionDelay(newValue);
1778                         }
1779
1780                 }));
1781                 options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(25));
1782                 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75));
1783                 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25));
1784                 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
1785                 options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, new OptionWatcher<Boolean>() {
1786
1787                         @Override
1788                         @SuppressWarnings("synthetic-access")
1789                         public void optionChanged(Option<Boolean> option, Boolean oldValue, Boolean newValue) {
1790                                 fcpInterface.setActive(newValue);
1791                         }
1792                 }));
1793                 options.addBooleanOption("FcpWriteFromFullAccessOnly", new DefaultOption<Boolean>(true, new OptionWatcher<Boolean>() {
1794
1795                         @Override
1796                         @SuppressWarnings("synthetic-access")
1797                         public void optionChanged(Option<Boolean> option, Boolean oldValue, Boolean newValue) {
1798                                 fcpInterface.setAllowWriteFromFullAccessOnly(newValue);
1799                         }
1800
1801                 }));
1802                 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1803                 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1804                 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1805
1806                 /* read options from configuration. */
1807                 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1808                 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1809                 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1810                 options.getBooleanOption("ClearOnNextRestart").set(null);
1811                 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1812                 if (clearConfiguration) {
1813                         /* stop loading the configuration. */
1814                         return;
1815                 }
1816
1817                 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1818                 options.getIntegerOption("PostsPerPage").set(configuration.getIntValue("Option/PostsPerPage").getValue(null));
1819                 options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
1820                 options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
1821                 options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
1822                 options.getBooleanOption("ActivateFcpInterface").set(configuration.getBooleanValue("Option/ActivateFcpInterface").getValue(null));
1823                 options.getBooleanOption("FcpWriteFromFullAccessOnly").set(configuration.getBooleanValue("Option/FcpWriteFromFullAccessOnly").getValue(null));
1824                 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1825
1826                 /* load known Sones. */
1827                 int soneCounter = 0;
1828                 while (true) {
1829                         String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1830                         if (knownSoneId == null) {
1831                                 break;
1832                         }
1833                         synchronized (newSones) {
1834                                 knownSones.add(knownSoneId);
1835                         }
1836                 }
1837
1838                 /* load known posts. */
1839                 int postCounter = 0;
1840                 while (true) {
1841                         String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1842                         if (knownPostId == null) {
1843                                 break;
1844                         }
1845                         synchronized (newPosts) {
1846                                 knownPosts.add(knownPostId);
1847                         }
1848                 }
1849
1850                 /* load known replies. */
1851                 int replyCounter = 0;
1852                 while (true) {
1853                         String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1854                         if (knownReplyId == null) {
1855                                 break;
1856                         }
1857                         synchronized (newReplies) {
1858                                 knownReplies.add(knownReplyId);
1859                         }
1860                 }
1861
1862                 /* load bookmarked posts. */
1863                 int bookmarkedPostCounter = 0;
1864                 while (true) {
1865                         String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null);
1866                         if (bookmarkedPostId == null) {
1867                                 break;
1868                         }
1869                         synchronized (bookmarkedPosts) {
1870                                 bookmarkedPosts.add(bookmarkedPostId);
1871                         }
1872                 }
1873
1874         }
1875
1876         /**
1877          * Generate a Sone URI from the given URI and latest edition.
1878          *
1879          * @param uriString
1880          *            The URI to derive the Sone URI from
1881          * @return The derived URI
1882          */
1883         private FreenetURI getSoneUri(String uriString) {
1884                 try {
1885                         FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1886                         return uri;
1887                 } catch (MalformedURLException mue1) {
1888                         logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1889                         return null;
1890                 }
1891         }
1892
1893         //
1894         // INTERFACE IdentityListener
1895         //
1896
1897         /**
1898          * {@inheritDoc}
1899          */
1900         @Override
1901         public void ownIdentityAdded(OwnIdentity ownIdentity) {
1902                 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1903                 if (ownIdentity.hasContext("Sone")) {
1904                         trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1905                         addLocalSone(ownIdentity);
1906                 }
1907         }
1908
1909         /**
1910          * {@inheritDoc}
1911          */
1912         @Override
1913         public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1914                 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1915                 trustedIdentities.remove(ownIdentity);
1916         }
1917
1918         /**
1919          * {@inheritDoc}
1920          */
1921         @Override
1922         public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1923                 logger.log(Level.FINEST, "Adding Identity: " + identity);
1924                 trustedIdentities.get(ownIdentity).add(identity);
1925                 addRemoteSone(identity);
1926         }
1927
1928         /**
1929          * {@inheritDoc}
1930          */
1931         @Override
1932         public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1933                 new Thread(new Runnable() {
1934
1935                         @Override
1936                         @SuppressWarnings("synthetic-access")
1937                         public void run() {
1938                                 Sone sone = getRemoteSone(identity.getId());
1939                                 sone.setIdentity(identity);
1940                                 soneDownloader.addSone(sone);
1941                                 soneDownloader.fetchSone(sone);
1942                         }
1943                 }).start();
1944         }
1945
1946         /**
1947          * {@inheritDoc}
1948          */
1949         @Override
1950         public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1951                 trustedIdentities.get(ownIdentity).remove(identity);
1952         }
1953
1954         //
1955         // INTERFACE UpdateListener
1956         //
1957
1958         /**
1959          * {@inheritDoc}
1960          */
1961         @Override
1962         public void updateFound(Version version, long releaseTime, long latestEdition) {
1963                 coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition);
1964         }
1965
1966         /**
1967          * Convenience interface for external classes that want to access the core’s
1968          * configuration.
1969          *
1970          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1971          */
1972         public static class Preferences {
1973
1974                 /** The wrapped options. */
1975                 private final Options options;
1976
1977                 /**
1978                  * Creates a new preferences object wrapped around the given options.
1979                  *
1980                  * @param options
1981                  *            The options to wrap
1982                  */
1983                 public Preferences(Options options) {
1984                         this.options = options;
1985                 }
1986
1987                 /**
1988                  * Returns the insertion delay.
1989                  *
1990                  * @return The insertion delay
1991                  */
1992                 public int getInsertionDelay() {
1993                         return options.getIntegerOption("InsertionDelay").get();
1994                 }
1995
1996                 /**
1997                  * Sets the insertion delay
1998                  *
1999                  * @param insertionDelay
2000                  *            The new insertion delay, or {@code null} to restore it to
2001                  *            the default value
2002                  * @return This preferences
2003                  */
2004                 public Preferences setInsertionDelay(Integer insertionDelay) {
2005                         options.getIntegerOption("InsertionDelay").set(insertionDelay);
2006                         return this;
2007                 }
2008
2009                 /**
2010                  * Returns the number of posts to show per page.
2011                  *
2012                  * @return The number of posts to show per page
2013                  */
2014                 public int getPostsPerPage() {
2015                         return options.getIntegerOption("PostsPerPage").get();
2016                 }
2017
2018                 /**
2019                  * Sets the number of posts to show per page.
2020                  *
2021                  * @param postsPerPage
2022                  *            The number of posts to show per page
2023                  * @return This preferences object
2024                  */
2025                 public Preferences setPostsPerPage(Integer postsPerPage) {
2026                         options.getIntegerOption("PostsPerPage").set(postsPerPage);
2027                         return this;
2028                 }
2029
2030                 /**
2031                  * Returns the positive trust.
2032                  *
2033                  * @return The positive trust
2034                  */
2035                 public int getPositiveTrust() {
2036                         return options.getIntegerOption("PositiveTrust").get();
2037                 }
2038
2039                 /**
2040                  * Sets the positive trust.
2041                  *
2042                  * @param positiveTrust
2043                  *            The new positive trust, or {@code null} to restore it to
2044                  *            the default vlaue
2045                  * @return This preferences
2046                  */
2047                 public Preferences setPositiveTrust(Integer positiveTrust) {
2048                         options.getIntegerOption("PositiveTrust").set(positiveTrust);
2049                         return this;
2050                 }
2051
2052                 /**
2053                  * Returns the negative trust.
2054                  *
2055                  * @return The negative trust
2056                  */
2057                 public int getNegativeTrust() {
2058                         return options.getIntegerOption("NegativeTrust").get();
2059                 }
2060
2061                 /**
2062                  * Sets the negative trust.
2063                  *
2064                  * @param negativeTrust
2065                  *            The negative trust, or {@code null} to restore it to the
2066                  *            default value
2067                  * @return The preferences
2068                  */
2069                 public Preferences setNegativeTrust(Integer negativeTrust) {
2070                         options.getIntegerOption("NegativeTrust").set(negativeTrust);
2071                         return this;
2072                 }
2073
2074                 /**
2075                  * Returns the trust comment. This is the comment that is set in the web
2076                  * of trust when a trust value is assigned to an identity.
2077                  *
2078                  * @return The trust comment
2079                  */
2080                 public String getTrustComment() {
2081                         return options.getStringOption("TrustComment").get();
2082                 }
2083
2084                 /**
2085                  * Sets the trust comment.
2086                  *
2087                  * @param trustComment
2088                  *            The trust comment, or {@code null} to restore it to the
2089                  *            default value
2090                  * @return This preferences
2091                  */
2092                 public Preferences setTrustComment(String trustComment) {
2093                         options.getStringOption("TrustComment").set(trustComment);
2094                         return this;
2095                 }
2096
2097                 /**
2098                  * Returns whether the {@link FcpInterface FCP interface} is currently
2099                  * active.
2100                  *
2101                  * @see FcpInterface#setActive(boolean)
2102                  * @return {@code true} if the FCP interface is currently active,
2103                  *         {@code false} otherwise
2104                  */
2105                 public boolean isFcpInterfaceActive() {
2106                         return options.getBooleanOption("ActivateFcpInterface").get();
2107                 }
2108
2109                 /**
2110                  * Sets whether the {@link FcpInterface FCP interface} is currently
2111                  * active.
2112                  *
2113                  * @see FcpInterface#setActive(boolean)
2114                  * @param fcpInterfaceActive
2115                  *            {@code true} to activate the FCP interface, {@code false}
2116                  *            to deactivate the FCP interface
2117                  * @return This preferences object
2118                  */
2119                 public Preferences setFcpInterfaceActive(boolean fcpInterfaceActive) {
2120                         options.getBooleanOption("ActivateFcpInterface").set(fcpInterfaceActive);
2121                         return this;
2122                 }
2123
2124                 /**
2125                  * Returns whether write access to the FCP interface is only allowed
2126                  * from the allowed FCP hosts of the node configuration.
2127                  *
2128                  * @return {@code true} if only allowed hosts are allowed to change data
2129                  *         using the FCP interface, {@code false} if everybody can
2130                  *         change data using the FCP interface
2131                  */
2132                 public boolean isFcpWriteFromFullAccessOnly() {
2133                         return options.getBooleanOption("FcpWriteFromFullAccessOnly").get();
2134                 }
2135
2136                 /**
2137                  * Sets whether write access to the FCP interface is only allowed from
2138                  * the allowed FCP hosts of the node configuration.
2139                  *
2140                  * @param fcpWriteFromFullAccessOnly
2141                  *            {@code true} if only allowed hosts should be allowed to
2142                  *            change data using the FCP interface, {@code false} if
2143                  *            everybody can change data using the FCP interface
2144                  * @return This preferences object
2145                  */
2146                 public Preferences setFcpWriteFromFullAccessOnly(boolean fcpWriteFromFullAccessOnly) {
2147                         options.getBooleanOption("FcpWriteFromFullAccessOnly").set(fcpWriteFromFullAccessOnly);
2148                         return this;
2149                 }
2150
2151                 /**
2152                  * Returns whether the rescue mode is active.
2153                  *
2154                  * @return {@code true} if the rescue mode is active, {@code false}
2155                  *         otherwise
2156                  */
2157                 public boolean isSoneRescueMode() {
2158                         return options.getBooleanOption("SoneRescueMode").get();
2159                 }
2160
2161                 /**
2162                  * Sets whether the rescue mode is active.
2163                  *
2164                  * @param soneRescueMode
2165                  *            {@code true} if the rescue mode is active, {@code false}
2166                  *            otherwise
2167                  * @return This preferences
2168                  */
2169                 public Preferences setSoneRescueMode(Boolean soneRescueMode) {
2170                         options.getBooleanOption("SoneRescueMode").set(soneRescueMode);
2171                         return this;
2172                 }
2173
2174                 /**
2175                  * Returns whether Sone should clear its settings on the next restart.
2176                  * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
2177                  * to return {@code true} as well!
2178                  *
2179                  * @return {@code true} if Sone should clear its settings on the next
2180                  *         restart, {@code false} otherwise
2181                  */
2182                 public boolean isClearOnNextRestart() {
2183                         return options.getBooleanOption("ClearOnNextRestart").get();
2184                 }
2185
2186                 /**
2187                  * Sets whether Sone will clear its settings on the next restart.
2188                  *
2189                  * @param clearOnNextRestart
2190                  *            {@code true} if Sone should clear its settings on the next
2191                  *            restart, {@code false} otherwise
2192                  * @return This preferences
2193                  */
2194                 public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
2195                         options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
2196                         return this;
2197                 }
2198
2199                 /**
2200                  * Returns whether Sone should really clear its settings on next
2201                  * restart. This is a confirmation option that needs to be set in
2202                  * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
2203                  * settings on the next restart.
2204                  *
2205                  * @return {@code true} if Sone should really clear its settings on the
2206                  *         next restart, {@code false} otherwise
2207                  */
2208                 public boolean isReallyClearOnNextRestart() {
2209                         return options.getBooleanOption("ReallyClearOnNextRestart").get();
2210                 }
2211
2212                 /**
2213                  * Sets whether Sone should really clear its settings on the next
2214                  * restart.
2215                  *
2216                  * @param reallyClearOnNextRestart
2217                  *            {@code true} if Sone should really clear its settings on
2218                  *            the next restart, {@code false} otherwise
2219                  * @return This preferences
2220                  */
2221                 public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
2222                         options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);
2223                         return this;
2224                 }
2225
2226         }
2227
2228 }