Implement auto-following of new Sones.
[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                 return sone;
880         }
881
882         /**
883          * Adds the Sone of the given identity.
884          *
885          * @param identity
886          *            The identity whose Sone to add
887          * @return The added or already existing Sone
888          */
889         public Sone addRemoteSone(Identity identity) {
890                 if (identity == null) {
891                         logger.log(Level.WARNING, "Given Identity is null!");
892                         return null;
893                 }
894                 synchronized (remoteSones) {
895                         final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
896                         boolean newSone = sone.getRequestUri() == null;
897                         sone.setRequestUri(getSoneUri(identity.getRequestUri()));
898                         sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
899                         if (newSone) {
900                                 synchronized (newSones) {
901                                         newSone = !knownSones.contains(sone.getId());
902                                         if (newSone) {
903                                                 newSones.add(sone.getId());
904                                         }
905                                 }
906                                 if (newSone) {
907                                         coreListenerManager.fireNewSoneFound(sone);
908                                         for (Sone localSone : getLocalSones()) {
909                                                 if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
910                                                         localSone.addFriend(sone.getId());
911                                                 }
912                                         }
913                                 }
914                         }
915                         remoteSones.put(identity.getId(), sone);
916                         soneDownloader.addSone(sone);
917                         setSoneStatus(sone, SoneStatus.unknown);
918                         new Thread(new Runnable() {
919
920                                 @Override
921                                 @SuppressWarnings("synthetic-access")
922                                 public void run() {
923                                         soneDownloader.fetchSone(sone);
924                                 }
925
926                         }, "Sone Downloader").start();
927                         return sone;
928                 }
929         }
930
931         /**
932          * Retrieves the trust relationship from the origin to the target. If the
933          * trust relationship can not be retrieved, {@code null} is returned.
934          *
935          * @see Identity#getTrust(OwnIdentity)
936          * @param origin
937          *            The origin of the trust tree
938          * @param target
939          *            The target of the trust
940          * @return The trust relationship
941          */
942         public Trust getTrust(Sone origin, Sone target) {
943                 if (!isLocalSone(origin)) {
944                         logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
945                         return null;
946                 }
947                 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
948         }
949
950         /**
951          * Sets the trust value of the given origin Sone for the target Sone.
952          *
953          * @param origin
954          *            The origin Sone
955          * @param target
956          *            The target Sone
957          * @param trustValue
958          *            The trust value (from {@code -100} to {@code 100})
959          */
960         public void setTrust(Sone origin, Sone target, int trustValue) {
961                 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();
962                 try {
963                         ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment());
964                 } catch (WebOfTrustException wote1) {
965                         logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1);
966                 }
967         }
968
969         /**
970          * Removes any trust assignment for the given target Sone.
971          *
972          * @param origin
973          *            The trust origin
974          * @param target
975          *            The trust target
976          */
977         public void removeTrust(Sone origin, Sone target) {
978                 Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
979                 try {
980                         ((OwnIdentity) origin.getIdentity()).removeTrust(target.getIdentity());
981                 } catch (WebOfTrustException wote1) {
982                         logger.log(Level.WARNING, "Could not remove trust for Sone: " + target, wote1);
983                 }
984         }
985
986         /**
987          * Assigns the configured positive trust value for the given target.
988          *
989          * @param origin
990          *            The trust origin
991          * @param target
992          *            The trust target
993          */
994         public void trustSone(Sone origin, Sone target) {
995                 setTrust(origin, target, preferences.getPositiveTrust());
996         }
997
998         /**
999          * Assigns the configured negative trust value for the given target.
1000          *
1001          * @param origin
1002          *            The trust origin
1003          * @param target
1004          *            The trust target
1005          */
1006         public void distrustSone(Sone origin, Sone target) {
1007                 setTrust(origin, target, preferences.getNegativeTrust());
1008         }
1009
1010         /**
1011          * Removes the trust assignment for the given target.
1012          *
1013          * @param origin
1014          *            The trust origin
1015          * @param target
1016          *            The trust target
1017          */
1018         public void untrustSone(Sone origin, Sone target) {
1019                 removeTrust(origin, target);
1020         }
1021
1022         /**
1023          * Updates the stores Sone with the given Sone.
1024          *
1025          * @param sone
1026          *            The updated Sone
1027          */
1028         public void updateSone(Sone sone) {
1029                 if (hasSone(sone.getId())) {
1030                         boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode();
1031                         Sone storedSone = getSone(sone.getId());
1032                         if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
1033                                 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
1034                                 return;
1035                         }
1036                         synchronized (posts) {
1037                                 if (!soneRescueMode) {
1038                                         for (Post post : storedSone.getPosts()) {
1039                                                 posts.remove(post.getId());
1040                                                 if (!sone.getPosts().contains(post)) {
1041                                                         coreListenerManager.firePostRemoved(post);
1042                                                 }
1043                                         }
1044                                 }
1045                                 List<Post> storedPosts = storedSone.getPosts();
1046                                 synchronized (newPosts) {
1047                                         for (Post post : sone.getPosts()) {
1048                                                 post.setSone(storedSone);
1049                                                 if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
1050                                                         newPosts.add(post.getId());
1051                                                         coreListenerManager.fireNewPostFound(post);
1052                                                 }
1053                                                 posts.put(post.getId(), post);
1054                                         }
1055                                 }
1056                         }
1057                         synchronized (replies) {
1058                                 if (!soneRescueMode) {
1059                                         for (Reply reply : storedSone.getReplies()) {
1060                                                 replies.remove(reply.getId());
1061                                                 if (!sone.getReplies().contains(reply)) {
1062                                                         coreListenerManager.fireReplyRemoved(reply);
1063                                                 }
1064                                         }
1065                                 }
1066                                 Set<Reply> storedReplies = storedSone.getReplies();
1067                                 synchronized (newReplies) {
1068                                         for (Reply reply : sone.getReplies()) {
1069                                                 reply.setSone(storedSone);
1070                                                 if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
1071                                                         newReplies.add(reply.getId());
1072                                                         coreListenerManager.fireNewReplyFound(reply);
1073                                                 }
1074                                                 replies.put(reply.getId(), reply);
1075                                         }
1076                                 }
1077                         }
1078                         synchronized (storedSone) {
1079                                 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
1080                                         storedSone.setTime(sone.getTime());
1081                                 }
1082                                 storedSone.setClient(sone.getClient());
1083                                 storedSone.setProfile(sone.getProfile());
1084                                 if (soneRescueMode) {
1085                                         for (Post post : sone.getPosts()) {
1086                                                 storedSone.addPost(post);
1087                                         }
1088                                         for (Reply reply : sone.getReplies()) {
1089                                                 storedSone.addReply(reply);
1090                                         }
1091                                         for (String likedPostId : sone.getLikedPostIds()) {
1092                                                 storedSone.addLikedPostId(likedPostId);
1093                                         }
1094                                         for (String likedReplyId : sone.getLikedReplyIds()) {
1095                                                 storedSone.addLikedReplyId(likedReplyId);
1096                                         }
1097                                 } else {
1098                                         storedSone.setPosts(sone.getPosts());
1099                                         storedSone.setReplies(sone.getReplies());
1100                                         storedSone.setLikePostIds(sone.getLikedPostIds());
1101                                         storedSone.setLikeReplyIds(sone.getLikedReplyIds());
1102                                 }
1103                                 storedSone.setLatestEdition(sone.getLatestEdition());
1104                         }
1105                 }
1106         }
1107
1108         /**
1109          * Deletes the given Sone. This will remove the Sone from the
1110          * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1111          * and remove the context from its identity.
1112          *
1113          * @param sone
1114          *            The Sone to delete
1115          */
1116         public void deleteSone(Sone sone) {
1117                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1118                         logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1119                         return;
1120                 }
1121                 synchronized (localSones) {
1122                         if (!localSones.containsKey(sone.getId())) {
1123                                 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1124                                 return;
1125                         }
1126                         localSones.remove(sone.getId());
1127                         soneInserters.remove(sone).stop();
1128                 }
1129                 try {
1130                         ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1131                         ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1132                 } catch (WebOfTrustException wote1) {
1133                         logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1134                 }
1135                 try {
1136                         configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1137                 } catch (ConfigurationException ce1) {
1138                         logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1139                 }
1140         }
1141
1142         /**
1143          * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
1144          * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
1145          *
1146          * @param sone
1147          *            The Sone to mark as known
1148          */
1149         public void markSoneKnown(Sone sone) {
1150                 synchronized (newSones) {
1151                         if (newSones.remove(sone.getId())) {
1152                                 knownSones.add(sone.getId());
1153                                 coreListenerManager.fireMarkSoneKnown(sone);
1154                                 saveConfiguration();
1155                         }
1156                 }
1157         }
1158
1159         /**
1160          * Loads and updates the given Sone from the configuration. If any error is
1161          * encountered, loading is aborted and the given Sone is not changed.
1162          *
1163          * @param sone
1164          *            The Sone to load and update
1165          */
1166         public void loadSone(Sone sone) {
1167                 if (!isLocalSone(sone)) {
1168                         logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1169                         return;
1170                 }
1171
1172                 /* load Sone. */
1173                 String sonePrefix = "Sone/" + sone.getId();
1174                 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1175                 if (soneTime == null) {
1176                         logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1177                         return;
1178                 }
1179                 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1180
1181                 /* load profile. */
1182                 Profile profile = new Profile();
1183                 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1184                 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1185                 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1186                 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1187                 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1188                 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1189
1190                 /* load profile fields. */
1191                 while (true) {
1192                         String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
1193                         String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
1194                         if (fieldName == null) {
1195                                 break;
1196                         }
1197                         String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
1198                         profile.addField(fieldName).setValue(fieldValue);
1199                 }
1200
1201                 /* load posts. */
1202                 Set<Post> posts = new HashSet<Post>();
1203                 while (true) {
1204                         String postPrefix = sonePrefix + "/Posts/" + posts.size();
1205                         String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1206                         if (postId == null) {
1207                                 break;
1208                         }
1209                         String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1210                         long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1211                         String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1212                         if ((postTime == 0) || (postText == null)) {
1213                                 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1214                                 return;
1215                         }
1216                         Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1217                         if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1218                                 post.setRecipient(getSone(postRecipientId));
1219                         }
1220                         posts.add(post);
1221                 }
1222
1223                 /* load replies. */
1224                 Set<Reply> replies = new HashSet<Reply>();
1225                 while (true) {
1226                         String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1227                         String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1228                         if (replyId == null) {
1229                                 break;
1230                         }
1231                         String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1232                         long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1233                         String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1234                         if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1235                                 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1236                                 return;
1237                         }
1238                         replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1239                 }
1240
1241                 /* load post likes. */
1242                 Set<String> likedPostIds = new HashSet<String>();
1243                 while (true) {
1244                         String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1245                         if (likedPostId == null) {
1246                                 break;
1247                         }
1248                         likedPostIds.add(likedPostId);
1249                 }
1250
1251                 /* load reply likes. */
1252                 Set<String> likedReplyIds = new HashSet<String>();
1253                 while (true) {
1254                         String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1255                         if (likedReplyId == null) {
1256                                 break;
1257                         }
1258                         likedReplyIds.add(likedReplyId);
1259                 }
1260
1261                 /* load friends. */
1262                 Set<String> friends = new HashSet<String>();
1263                 while (true) {
1264                         String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1265                         if (friendId == null) {
1266                                 break;
1267                         }
1268                         friends.add(friendId);
1269                 }
1270
1271                 /* load options. */
1272                 sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
1273                 sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
1274
1275                 /* if we’re still here, Sone was loaded successfully. */
1276                 synchronized (sone) {
1277                         sone.setTime(soneTime);
1278                         sone.setProfile(profile);
1279                         sone.setPosts(posts);
1280                         sone.setReplies(replies);
1281                         sone.setLikePostIds(likedPostIds);
1282                         sone.setLikeReplyIds(likedReplyIds);
1283                         sone.setFriends(friends);
1284                         soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1285                 }
1286                 synchronized (newSones) {
1287                         for (String friend : friends) {
1288                                 knownSones.add(friend);
1289                         }
1290                 }
1291                 synchronized (newPosts) {
1292                         for (Post post : posts) {
1293                                 knownPosts.add(post.getId());
1294                         }
1295                 }
1296                 synchronized (newReplies) {
1297                         for (Reply reply : replies) {
1298                                 knownReplies.add(reply.getId());
1299                         }
1300                 }
1301         }
1302
1303         /**
1304          * Saves the given Sone. This will persist all local settings for the given
1305          * Sone, such as the friends list and similar, private options.
1306          *
1307          * @param sone
1308          *            The Sone to save
1309          */
1310         public synchronized void saveSone(Sone sone) {
1311                 if (!isLocalSone(sone)) {
1312                         logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1313                         return;
1314                 }
1315                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1316                         logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1317                         return;
1318                 }
1319
1320                 logger.log(Level.INFO, "Saving Sone: %s", sone);
1321                 try {
1322                         ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1323
1324                         /* save Sone into configuration. */
1325                         String sonePrefix = "Sone/" + sone.getId();
1326                         configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1327                         configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1328
1329                         /* save profile. */
1330                         Profile profile = sone.getProfile();
1331                         configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1332                         configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1333                         configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1334                         configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1335                         configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1336                         configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1337
1338                         /* save profile fields. */
1339                         int fieldCounter = 0;
1340                         for (Field profileField : profile.getFields()) {
1341                                 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1342                                 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1343                                 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1344                         }
1345                         configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1346
1347                         /* save posts. */
1348                         int postCounter = 0;
1349                         for (Post post : sone.getPosts()) {
1350                                 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1351                                 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1352                                 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1353                                 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1354                                 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1355                         }
1356                         configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1357
1358                         /* save replies. */
1359                         int replyCounter = 0;
1360                         for (Reply reply : sone.getReplies()) {
1361                                 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1362                                 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1363                                 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1364                                 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1365                                 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1366                         }
1367                         configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1368
1369                         /* save post likes. */
1370                         int postLikeCounter = 0;
1371                         for (String postId : sone.getLikedPostIds()) {
1372                                 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1373                         }
1374                         configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1375
1376                         /* save reply likes. */
1377                         int replyLikeCounter = 0;
1378                         for (String replyId : sone.getLikedReplyIds()) {
1379                                 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1380                         }
1381                         configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1382
1383                         /* save friends. */
1384                         int friendCounter = 0;
1385                         for (String friendId : sone.getFriends()) {
1386                                 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1387                         }
1388                         configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1389
1390                         /* save options. */
1391                         configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
1392
1393                         configuration.save();
1394                         logger.log(Level.INFO, "Sone %s saved.", sone);
1395                 } catch (ConfigurationException ce1) {
1396                         logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1397                 } catch (WebOfTrustException wote1) {
1398                         logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1399                 }
1400         }
1401
1402         /**
1403          * Creates a new post.
1404          *
1405          * @param sone
1406          *            The Sone that creates the post
1407          * @param text
1408          *            The text of the post
1409          * @return The created post
1410          */
1411         public Post createPost(Sone sone, String text) {
1412                 return createPost(sone, System.currentTimeMillis(), text);
1413         }
1414
1415         /**
1416          * Creates a new post.
1417          *
1418          * @param sone
1419          *            The Sone that creates the post
1420          * @param time
1421          *            The time of the post
1422          * @param text
1423          *            The text of the post
1424          * @return The created post
1425          */
1426         public Post createPost(Sone sone, long time, String text) {
1427                 return createPost(sone, null, time, text);
1428         }
1429
1430         /**
1431          * Creates a new post.
1432          *
1433          * @param sone
1434          *            The Sone that creates the post
1435          * @param recipient
1436          *            The recipient Sone, or {@code null} if this post does not have
1437          *            a recipient
1438          * @param text
1439          *            The text of the post
1440          * @return The created post
1441          */
1442         public Post createPost(Sone sone, Sone recipient, String text) {
1443                 return createPost(sone, recipient, System.currentTimeMillis(), text);
1444         }
1445
1446         /**
1447          * Creates a new post.
1448          *
1449          * @param sone
1450          *            The Sone that creates the post
1451          * @param recipient
1452          *            The recipient Sone, or {@code null} if this post does not have
1453          *            a recipient
1454          * @param time
1455          *            The time of the post
1456          * @param text
1457          *            The text of the post
1458          * @return The created post
1459          */
1460         public Post createPost(Sone sone, Sone recipient, long time, String text) {
1461                 if (!isLocalSone(sone)) {
1462                         logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1463                         return null;
1464                 }
1465                 Post post = new Post(sone, time, text);
1466                 if (recipient != null) {
1467                         post.setRecipient(recipient);
1468                 }
1469                 synchronized (posts) {
1470                         posts.put(post.getId(), post);
1471                 }
1472                 synchronized (newPosts) {
1473                         knownPosts.add(post.getId());
1474                 }
1475                 sone.addPost(post);
1476                 saveSone(sone);
1477                 return post;
1478         }
1479
1480         /**
1481          * Deletes the given post.
1482          *
1483          * @param post
1484          *            The post to delete
1485          */
1486         public void deletePost(Post post) {
1487                 if (!isLocalSone(post.getSone())) {
1488                         logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1489                         return;
1490                 }
1491                 post.getSone().removePost(post);
1492                 synchronized (posts) {
1493                         posts.remove(post.getId());
1494                 }
1495                 saveSone(post.getSone());
1496         }
1497
1498         /**
1499          * Marks the given post as known, if it is currently a new post (according
1500          * to {@link #isNewPost(String)}).
1501          *
1502          * @param post
1503          *            The post to mark as known
1504          */
1505         public void markPostKnown(Post post) {
1506                 synchronized (newPosts) {
1507                         if (newPosts.remove(post.getId())) {
1508                                 knownPosts.add(post.getId());
1509                                 coreListenerManager.fireMarkPostKnown(post);
1510                                 saveConfiguration();
1511                         }
1512                 }
1513         }
1514
1515         /**
1516          * Bookmarks the given post.
1517          *
1518          * @param post
1519          *            The post to bookmark
1520          */
1521         public void bookmark(Post post) {
1522                 bookmarkPost(post.getId());
1523         }
1524
1525         /**
1526          * Bookmarks the post with the given ID.
1527          *
1528          * @param id
1529          *            The ID of the post to bookmark
1530          */
1531         public void bookmarkPost(String id) {
1532                 synchronized (bookmarkedPosts) {
1533                         bookmarkedPosts.add(id);
1534                 }
1535         }
1536
1537         /**
1538          * Removes the given post from the bookmarks.
1539          *
1540          * @param post
1541          *            The post to unbookmark
1542          */
1543         public void unbookmark(Post post) {
1544                 unbookmarkPost(post.getId());
1545         }
1546
1547         /**
1548          * Removes the post with the given ID from the bookmarks.
1549          *
1550          * @param id
1551          *            The ID of the post to unbookmark
1552          */
1553         public void unbookmarkPost(String id) {
1554                 synchronized (bookmarkedPosts) {
1555                         bookmarkedPosts.remove(id);
1556                 }
1557         }
1558
1559         /**
1560          * Creates a new reply.
1561          *
1562          * @param sone
1563          *            The Sone that creates the reply
1564          * @param post
1565          *            The post that this reply refers to
1566          * @param text
1567          *            The text of the reply
1568          * @return The created reply
1569          */
1570         public Reply createReply(Sone sone, Post post, String text) {
1571                 return createReply(sone, post, System.currentTimeMillis(), text);
1572         }
1573
1574         /**
1575          * Creates a new reply.
1576          *
1577          * @param sone
1578          *            The Sone that creates the reply
1579          * @param post
1580          *            The post that this reply refers to
1581          * @param time
1582          *            The time of the reply
1583          * @param text
1584          *            The text of the reply
1585          * @return The created reply
1586          */
1587         public Reply createReply(Sone sone, Post post, long time, String text) {
1588                 if (!isLocalSone(sone)) {
1589                         logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1590                         return null;
1591                 }
1592                 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1593                 synchronized (replies) {
1594                         replies.put(reply.getId(), reply);
1595                 }
1596                 synchronized (newReplies) {
1597                         knownReplies.add(reply.getId());
1598                 }
1599                 sone.addReply(reply);
1600                 saveSone(sone);
1601                 return reply;
1602         }
1603
1604         /**
1605          * Deletes the given reply.
1606          *
1607          * @param reply
1608          *            The reply to delete
1609          */
1610         public void deleteReply(Reply reply) {
1611                 Sone sone = reply.getSone();
1612                 if (!isLocalSone(sone)) {
1613                         logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1614                         return;
1615                 }
1616                 synchronized (replies) {
1617                         replies.remove(reply.getId());
1618                 }
1619                 sone.removeReply(reply);
1620                 saveSone(sone);
1621         }
1622
1623         /**
1624          * Marks the given reply as known, if it is currently a new reply (according
1625          * to {@link #isNewReply(String)}).
1626          *
1627          * @param reply
1628          *            The reply to mark as known
1629          */
1630         public void markReplyKnown(Reply reply) {
1631                 synchronized (newReplies) {
1632                         if (newReplies.remove(reply.getId())) {
1633                                 knownReplies.add(reply.getId());
1634                                 coreListenerManager.fireMarkReplyKnown(reply);
1635                                 saveConfiguration();
1636                         }
1637                 }
1638         }
1639
1640         /**
1641          * Starts the core.
1642          */
1643         public void start() {
1644                 loadConfiguration();
1645                 updateChecker.addUpdateListener(this);
1646                 updateChecker.start();
1647         }
1648
1649         /**
1650          * Stops the core.
1651          */
1652         public void stop() {
1653                 synchronized (localSones) {
1654                         for (SoneInserter soneInserter : soneInserters.values()) {
1655                                 soneInserter.stop();
1656                         }
1657                 }
1658                 updateChecker.stop();
1659                 updateChecker.removeUpdateListener(this);
1660                 soneDownloader.stop();
1661                 saveConfiguration();
1662                 stopped = true;
1663         }
1664
1665         /**
1666          * Saves the current options.
1667          */
1668         public void saveConfiguration() {
1669                 synchronized (configuration) {
1670                         if (storingConfiguration) {
1671                                 logger.log(Level.FINE, "Already storing configuration…");
1672                                 return;
1673                         }
1674                         storingConfiguration = true;
1675                 }
1676
1677                 /* store the options first. */
1678                 try {
1679                         configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
1680                         configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1681                         configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
1682                         configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
1683                         configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
1684                         configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1685                         configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1686                         configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1687
1688                         /* save known Sones. */
1689                         int soneCounter = 0;
1690                         synchronized (newSones) {
1691                                 for (String knownSoneId : knownSones) {
1692                                         configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1693                                 }
1694                                 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1695                         }
1696
1697                         /* save known posts. */
1698                         int postCounter = 0;
1699                         synchronized (newPosts) {
1700                                 for (String knownPostId : knownPosts) {
1701                                         configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1702                                 }
1703                                 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1704                         }
1705
1706                         /* save known replies. */
1707                         int replyCounter = 0;
1708                         synchronized (newReplies) {
1709                                 for (String knownReplyId : knownReplies) {
1710                                         configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1711                                 }
1712                                 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1713                         }
1714
1715                         /* save bookmarked posts. */
1716                         int bookmarkedPostCounter = 0;
1717                         synchronized (bookmarkedPosts) {
1718                                 for (String bookmarkedPostId : bookmarkedPosts) {
1719                                         configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
1720                                 }
1721                         }
1722                         configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
1723
1724                         /* now save it. */
1725                         configuration.save();
1726
1727                 } catch (ConfigurationException ce1) {
1728                         logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1729                 } finally {
1730                         synchronized (configuration) {
1731                                 storingConfiguration = false;
1732                         }
1733                 }
1734         }
1735
1736         //
1737         // PRIVATE METHODS
1738         //
1739
1740         /**
1741          * Loads the configuration.
1742          */
1743         @SuppressWarnings("unchecked")
1744         private void loadConfiguration() {
1745                 /* create options. */
1746                 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1747
1748                         @Override
1749                         public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1750                                 SoneInserter.setInsertionDelay(newValue);
1751                         }
1752
1753                 }));
1754                 options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75));
1755                 options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25));
1756                 options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
1757                 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1758                 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1759                 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1760
1761                 /* read options from configuration. */
1762                 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1763                 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1764                 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1765                 options.getBooleanOption("ClearOnNextRestart").set(null);
1766                 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1767                 if (clearConfiguration) {
1768                         /* stop loading the configuration. */
1769                         return;
1770                 }
1771
1772                 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1773                 options.getIntegerOption("PositiveTrust").set(configuration.getIntValue("Option/PositiveTrust").getValue(null));
1774                 options.getIntegerOption("NegativeTrust").set(configuration.getIntValue("Option/NegativeTrust").getValue(null));
1775                 options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
1776                 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1777
1778                 /* load known Sones. */
1779                 int soneCounter = 0;
1780                 while (true) {
1781                         String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1782                         if (knownSoneId == null) {
1783                                 break;
1784                         }
1785                         synchronized (newSones) {
1786                                 knownSones.add(knownSoneId);
1787                         }
1788                 }
1789
1790                 /* load known posts. */
1791                 int postCounter = 0;
1792                 while (true) {
1793                         String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1794                         if (knownPostId == null) {
1795                                 break;
1796                         }
1797                         synchronized (newPosts) {
1798                                 knownPosts.add(knownPostId);
1799                         }
1800                 }
1801
1802                 /* load known replies. */
1803                 int replyCounter = 0;
1804                 while (true) {
1805                         String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1806                         if (knownReplyId == null) {
1807                                 break;
1808                         }
1809                         synchronized (newReplies) {
1810                                 knownReplies.add(knownReplyId);
1811                         }
1812                 }
1813
1814                 /* load bookmarked posts. */
1815                 int bookmarkedPostCounter = 0;
1816                 while (true) {
1817                         String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null);
1818                         if (bookmarkedPostId == null) {
1819                                 break;
1820                         }
1821                         synchronized (bookmarkedPosts) {
1822                                 bookmarkedPosts.add(bookmarkedPostId);
1823                         }
1824                 }
1825
1826         }
1827
1828         /**
1829          * Generate a Sone URI from the given URI and latest edition.
1830          *
1831          * @param uriString
1832          *            The URI to derive the Sone URI from
1833          * @return The derived URI
1834          */
1835         private FreenetURI getSoneUri(String uriString) {
1836                 try {
1837                         FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1838                         return uri;
1839                 } catch (MalformedURLException mue1) {
1840                         logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1841                         return null;
1842                 }
1843         }
1844
1845         //
1846         // INTERFACE IdentityListener
1847         //
1848
1849         /**
1850          * {@inheritDoc}
1851          */
1852         @Override
1853         public void ownIdentityAdded(OwnIdentity ownIdentity) {
1854                 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1855                 if (ownIdentity.hasContext("Sone")) {
1856                         trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1857                         addLocalSone(ownIdentity);
1858                 }
1859         }
1860
1861         /**
1862          * {@inheritDoc}
1863          */
1864         @Override
1865         public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1866                 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1867                 trustedIdentities.remove(ownIdentity);
1868         }
1869
1870         /**
1871          * {@inheritDoc}
1872          */
1873         @Override
1874         public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1875                 logger.log(Level.FINEST, "Adding Identity: " + identity);
1876                 trustedIdentities.get(ownIdentity).add(identity);
1877                 addRemoteSone(identity);
1878         }
1879
1880         /**
1881          * {@inheritDoc}
1882          */
1883         @Override
1884         public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1885                 new Thread(new Runnable() {
1886
1887                         @Override
1888                         @SuppressWarnings("synthetic-access")
1889                         public void run() {
1890                                 Sone sone = getRemoteSone(identity.getId());
1891                                 sone.setIdentity(identity);
1892                                 soneDownloader.addSone(sone);
1893                                 soneDownloader.fetchSone(sone);
1894                         }
1895                 }).start();
1896         }
1897
1898         /**
1899          * {@inheritDoc}
1900          */
1901         @Override
1902         public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1903                 trustedIdentities.get(ownIdentity).remove(identity);
1904         }
1905
1906         //
1907         // INTERFACE UpdateListener
1908         //
1909
1910         /**
1911          * {@inheritDoc}
1912          */
1913         @Override
1914         public void updateFound(Version version, long releaseTime, long latestEdition) {
1915                 coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition);
1916         }
1917
1918         /**
1919          * Convenience interface for external classes that want to access the core’s
1920          * configuration.
1921          *
1922          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1923          */
1924         public static class Preferences {
1925
1926                 /** The wrapped options. */
1927                 private final Options options;
1928
1929                 /**
1930                  * Creates a new preferences object wrapped around the given options.
1931                  *
1932                  * @param options
1933                  *            The options to wrap
1934                  */
1935                 public Preferences(Options options) {
1936                         this.options = options;
1937                 }
1938
1939                 /**
1940                  * Returns the insertion delay.
1941                  *
1942                  * @return The insertion delay
1943                  */
1944                 public int getInsertionDelay() {
1945                         return options.getIntegerOption("InsertionDelay").get();
1946                 }
1947
1948                 /**
1949                  * Sets the insertion delay
1950                  *
1951                  * @param insertionDelay
1952                  *            The new insertion delay, or {@code null} to restore it to
1953                  *            the default value
1954                  * @return This preferences
1955                  */
1956                 public Preferences setInsertionDelay(Integer insertionDelay) {
1957                         options.getIntegerOption("InsertionDelay").set(insertionDelay);
1958                         return this;
1959                 }
1960
1961                 /**
1962                  * Returns the positive trust.
1963                  *
1964                  * @return The positive trust
1965                  */
1966                 public int getPositiveTrust() {
1967                         return options.getIntegerOption("PositiveTrust").get();
1968                 }
1969
1970                 /**
1971                  * Sets the positive trust.
1972                  *
1973                  * @param positiveTrust
1974                  *            The new positive trust, or {@code null} to restore it to
1975                  *            the default vlaue
1976                  * @return This preferences
1977                  */
1978                 public Preferences setPositiveTrust(Integer positiveTrust) {
1979                         options.getIntegerOption("PositiveTrust").set(positiveTrust);
1980                         return this;
1981                 }
1982
1983                 /**
1984                  * Returns the negative trust.
1985                  *
1986                  * @return The negative trust
1987                  */
1988                 public int getNegativeTrust() {
1989                         return options.getIntegerOption("NegativeTrust").get();
1990                 }
1991
1992                 /**
1993                  * Sets the negative trust.
1994                  *
1995                  * @param negativeTrust
1996                  *            The negative trust, or {@code null} to restore it to the
1997                  *            default value
1998                  * @return The preferences
1999                  */
2000                 public Preferences setNegativeTrust(Integer negativeTrust) {
2001                         options.getIntegerOption("NegativeTrust").set(negativeTrust);
2002                         return this;
2003                 }
2004
2005                 /**
2006                  * Returns the trust comment. This is the comment that is set in the web
2007                  * of trust when a trust value is assigned to an identity.
2008                  *
2009                  * @return The trust comment
2010                  */
2011                 public String getTrustComment() {
2012                         return options.getStringOption("TrustComment").get();
2013                 }
2014
2015                 /**
2016                  * Sets the trust comment.
2017                  *
2018                  * @param trustComment
2019                  *            The trust comment, or {@code null} to restore it to the
2020                  *            default value
2021                  * @return This preferences
2022                  */
2023                 public Preferences setTrustComment(String trustComment) {
2024                         options.getStringOption("TrustComment").set(trustComment);
2025                         return this;
2026                 }
2027
2028                 /**
2029                  * Returns whether the rescue mode is active.
2030                  *
2031                  * @return {@code true} if the rescue mode is active, {@code false}
2032                  *         otherwise
2033                  */
2034                 public boolean isSoneRescueMode() {
2035                         return options.getBooleanOption("SoneRescueMode").get();
2036                 }
2037
2038                 /**
2039                  * Sets whether the rescue mode is active.
2040                  *
2041                  * @param soneRescueMode
2042                  *            {@code true} if the rescue mode is active, {@code false}
2043                  *            otherwise
2044                  * @return This preferences
2045                  */
2046                 public Preferences setSoneRescueMode(Boolean soneRescueMode) {
2047                         options.getBooleanOption("SoneRescueMode").set(soneRescueMode);
2048                         return this;
2049                 }
2050
2051                 /**
2052                  * Returns whether Sone should clear its settings on the next restart.
2053                  * In order to be effective, {@link #isReallyClearOnNextRestart()} needs
2054                  * to return {@code true} as well!
2055                  *
2056                  * @return {@code true} if Sone should clear its settings on the next
2057                  *         restart, {@code false} otherwise
2058                  */
2059                 public boolean isClearOnNextRestart() {
2060                         return options.getBooleanOption("ClearOnNextRestart").get();
2061                 }
2062
2063                 /**
2064                  * Sets whether Sone will clear its settings on the next restart.
2065                  *
2066                  * @param clearOnNextRestart
2067                  *            {@code true} if Sone should clear its settings on the next
2068                  *            restart, {@code false} otherwise
2069                  * @return This preferences
2070                  */
2071                 public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) {
2072                         options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart);
2073                         return this;
2074                 }
2075
2076                 /**
2077                  * Returns whether Sone should really clear its settings on next
2078                  * restart. This is a confirmation option that needs to be set in
2079                  * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s
2080                  * settings on the next restart.
2081                  *
2082                  * @return {@code true} if Sone should really clear its settings on the
2083                  *         next restart, {@code false} otherwise
2084                  */
2085                 public boolean isReallyClearOnNextRestart() {
2086                         return options.getBooleanOption("ReallyClearOnNextRestart").get();
2087                 }
2088
2089                 /**
2090                  * Sets whether Sone should really clear its settings on the next
2091                  * restart.
2092                  *
2093                  * @param reallyClearOnNextRestart
2094                  *            {@code true} if Sone should really clear its settings on
2095                  *            the next restart, {@code false} otherwise
2096                  * @return This preferences
2097                  */
2098                 public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) {
2099                         options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart);
2100                         return this;
2101                 }
2102
2103         }
2104
2105 }