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