🎨 Declare return value for ticker explicitely
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
1 /*
2  * Sone - Core.java - Copyright © 2010–2019 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 static com.google.common.base.Optional.fromNullable;
21 import static com.google.common.base.Preconditions.checkArgument;
22 import static com.google.common.base.Preconditions.checkNotNull;
23 import static com.google.common.primitives.Longs.tryParse;
24 import static java.lang.String.format;
25 import static java.util.logging.Level.WARNING;
26 import static java.util.logging.Logger.getLogger;
27 import static net.pterodactylus.sone.data.AlbumsKt.getAllImages;
28
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Set;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Executors;
39 import java.util.concurrent.ScheduledExecutorService;
40 import java.util.concurrent.TimeUnit;
41 import java.util.concurrent.atomic.*;
42 import java.util.logging.Level;
43 import java.util.logging.Logger;
44
45 import javax.annotation.Nonnull;
46 import javax.annotation.Nullable;
47
48 import com.codahale.metrics.*;
49 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
50 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
51 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
52 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
53 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
54 import net.pterodactylus.sone.core.event.*;
55 import net.pterodactylus.sone.data.Album;
56 import net.pterodactylus.sone.data.Client;
57 import net.pterodactylus.sone.data.Image;
58 import net.pterodactylus.sone.data.Post;
59 import net.pterodactylus.sone.data.PostReply;
60 import net.pterodactylus.sone.data.Profile;
61 import net.pterodactylus.sone.data.Profile.Field;
62 import net.pterodactylus.sone.data.Reply;
63 import net.pterodactylus.sone.data.Sone;
64 import net.pterodactylus.sone.data.Sone.SoneStatus;
65 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent;
66 import net.pterodactylus.sone.data.TemporaryImage;
67 import net.pterodactylus.sone.database.AlbumBuilder;
68 import net.pterodactylus.sone.database.Database;
69 import net.pterodactylus.sone.database.DatabaseException;
70 import net.pterodactylus.sone.database.ImageBuilder;
71 import net.pterodactylus.sone.database.PostBuilder;
72 import net.pterodactylus.sone.database.PostProvider;
73 import net.pterodactylus.sone.database.PostReplyBuilder;
74 import net.pterodactylus.sone.database.PostReplyProvider;
75 import net.pterodactylus.sone.database.SoneBuilder;
76 import net.pterodactylus.sone.database.SoneProvider;
77 import net.pterodactylus.sone.freenet.wot.Identity;
78 import net.pterodactylus.sone.freenet.wot.IdentityManager;
79 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
80 import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
81 import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
82 import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
83 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
84 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
85 import net.pterodactylus.sone.main.SonePlugin;
86 import net.pterodactylus.util.config.Configuration;
87 import net.pterodactylus.util.config.ConfigurationException;
88 import net.pterodactylus.util.service.AbstractService;
89 import net.pterodactylus.util.thread.NamedThreadFactory;
90
91 import com.google.common.annotations.VisibleForTesting;
92 import com.google.common.base.Stopwatch;
93 import com.google.common.collect.FluentIterable;
94 import com.google.common.collect.HashMultimap;
95 import com.google.common.collect.Multimap;
96 import com.google.common.collect.Multimaps;
97 import com.google.common.eventbus.EventBus;
98 import com.google.common.eventbus.Subscribe;
99 import com.google.inject.Inject;
100 import com.google.inject.Singleton;
101 import kotlin.jvm.functions.Function1;
102
103 /**
104  * The Sone core.
105  */
106 @Singleton
107 public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
108
109         /** The logger. */
110         private static final Logger logger = getLogger(Core.class.getName());
111
112         /** The start time. */
113         private final long startupTime = System.currentTimeMillis();
114
115         private final AtomicBoolean debug = new AtomicBoolean(false);
116
117         /** The preferences. */
118         private final Preferences preferences;
119
120         /** The event bus. */
121         private final EventBus eventBus;
122
123         /** The configuration. */
124         private final Configuration configuration;
125
126         /** Whether we’re currently saving the configuration. */
127         private boolean storingConfiguration = false;
128
129         /** The identity manager. */
130         private final IdentityManager identityManager;
131
132         /** Interface to freenet. */
133         private final FreenetInterface freenetInterface;
134
135         /** The Sone downloader. */
136         private final SoneDownloader soneDownloader;
137
138         /** The image inserter. */
139         private final ImageInserter imageInserter;
140
141         /** Sone downloader thread-pool. */
142         private final ExecutorService soneDownloaders = Executors.newFixedThreadPool(10, new NamedThreadFactory("Sone Downloader %2$d"));
143
144         /** The update checker. */
145         private final UpdateChecker updateChecker;
146
147         /** The trust updater. */
148         private final WebOfTrustUpdater webOfTrustUpdater;
149
150         /** Locked local Sones. */
151         /* synchronize on itself. */
152         private final Set<Sone> lockedSones = new HashSet<>();
153
154         /** Sone inserters. */
155         /* synchronize access on this on sones. */
156         private final Map<Sone, SoneInserter> soneInserters = new HashMap<>();
157
158         /** Sone rescuers. */
159         /* synchronize access on this on sones. */
160         private final Map<Sone, SoneRescuer> soneRescuers = new HashMap<>();
161
162         /** All known Sones. */
163         private final Set<String> knownSones = new HashSet<>();
164
165         /** The post database. */
166         private final Database database;
167
168         /** Trusted identities, sorted by own identities. */
169         private final Multimap<OwnIdentity, Identity> trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.<OwnIdentity, Identity>create());
170
171         /** All temporary images. */
172         private final Map<String, TemporaryImage> temporaryImages = new HashMap<>();
173
174         /** Ticker for threads that mark own elements as known. */
175         private final ScheduledExecutorService localElementTicker = Executors.newScheduledThreadPool(1);
176
177         /** The time the configuration was last touched. */
178         private volatile long lastConfigurationUpdate;
179
180         private final MetricRegistry metricRegistry;
181         private final Histogram configurationSaveTimeHistogram;
182
183         /**
184          * Creates a new core.
185          *
186          * @param configuration
187          *            The configuration of the core
188          * @param freenetInterface
189          *            The freenet interface
190          * @param identityManager
191          *            The identity manager
192          * @param webOfTrustUpdater
193          *            The WebOfTrust updater
194          * @param eventBus
195          *            The event bus
196          * @param database
197          *            The database
198          */
199         @Inject
200         public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database, MetricRegistry metricRegistry) {
201                 super("Sone Core");
202                 this.configuration = configuration;
203                 this.freenetInterface = freenetInterface;
204                 this.identityManager = identityManager;
205                 this.soneDownloader = soneDownloader;
206                 this.imageInserter = imageInserter;
207                 this.updateChecker = updateChecker;
208                 this.webOfTrustUpdater = webOfTrustUpdater;
209                 this.eventBus = eventBus;
210                 this.database = database;
211                 this.metricRegistry = metricRegistry;
212                 preferences = new Preferences(eventBus);
213                 this.configurationSaveTimeHistogram = metricRegistry.histogram("configuration.save.duration", () -> new Histogram(new ExponentiallyDecayingReservoir(3000, 0)));
214         }
215
216         //
217         // ACCESSORS
218         //
219
220         /**
221          * Returns the time Sone was started.
222          *
223          * @return The startup time (in milliseconds since Jan 1, 1970 UTC)
224          */
225         public long getStartupTime() {
226                 return startupTime;
227         }
228
229         @Nonnull
230         public boolean getDebug() {
231                 return debug.get();
232         }
233
234         public void setDebug() {
235                 debug.set(true);
236                 eventBus.post(new DebugActivatedEvent());
237         }
238
239         /**
240          * Returns the options used by the core.
241          *
242          * @return The options of the core
243          */
244         public Preferences getPreferences() {
245                 return preferences;
246         }
247
248         /**
249          * Returns the identity manager used by the core.
250          *
251          * @return The identity manager
252          */
253         public IdentityManager getIdentityManager() {
254                 return identityManager;
255         }
256
257         /**
258          * Returns the update checker.
259          *
260          * @return The update checker
261          */
262         public UpdateChecker getUpdateChecker() {
263                 return updateChecker;
264         }
265
266         /**
267          * Returns the Sone rescuer for the given local Sone.
268          *
269          * @param sone
270          *            The local Sone to get the rescuer for
271          * @return The Sone rescuer for the given Sone
272          */
273         public SoneRescuer getSoneRescuer(Sone sone) {
274                 checkNotNull(sone, "sone must not be null");
275                 checkArgument(sone.isLocal(), "sone must be local");
276                 synchronized (soneRescuers) {
277                         SoneRescuer soneRescuer = soneRescuers.get(sone);
278                         if (soneRescuer == null) {
279                                 soneRescuer = new SoneRescuer(this, soneDownloader, sone);
280                                 soneRescuers.put(sone, soneRescuer);
281                                 soneRescuer.start();
282                         }
283                         return soneRescuer;
284                 }
285         }
286
287         /**
288          * Returns whether the given Sone is currently locked.
289          *
290          * @param sone
291          *            The sone to check
292          * @return {@code true} if the Sone is locked, {@code false} if it is not
293          */
294         public boolean isLocked(Sone sone) {
295                 synchronized (lockedSones) {
296                         return lockedSones.contains(sone);
297                 }
298         }
299
300         public SoneBuilder soneBuilder() {
301                 return database.newSoneBuilder();
302         }
303
304         /**
305          * {@inheritDocs}
306          */
307         @Nonnull
308         @Override
309         public Collection<Sone> getSones() {
310                 return database.getSones();
311         }
312
313         @Nonnull
314         @Override
315         public Function1<String, Sone> getSoneLoader() {
316                 return database.getSoneLoader();
317         }
318
319         /**
320          * Returns the Sone with the given ID, regardless whether it’s local or
321          * remote.
322          *
323          * @param id
324          *            The ID of the Sone to get
325          * @return The Sone with the given ID, or {@code null} if there is no such
326          *         Sone
327          */
328         @Override
329         @Nullable
330         public Sone getSone(@Nonnull String id) {
331                 return database.getSone(id);
332         }
333
334         /**
335          * {@inheritDocs}
336          */
337         @Override
338         public Collection<Sone> getLocalSones() {
339                 return database.getLocalSones();
340         }
341
342         /**
343          * Returns the local Sone with the given ID, optionally creating a new Sone.
344          *
345          * @param id
346          *            The ID of the Sone
347          * @return The Sone with the given ID, or {@code null}
348          */
349         public Sone getLocalSone(String id) {
350                 Sone sone = database.getSone(id);
351                 if ((sone != null) && sone.isLocal()) {
352                         return sone;
353                 }
354                 return null;
355         }
356
357         /**
358          * {@inheritDocs}
359          */
360         @Override
361         public Collection<Sone> getRemoteSones() {
362                 return database.getRemoteSones();
363         }
364
365         /**
366          * Returns the remote Sone with the given ID.
367          *
368          *
369          * @param id
370          *            The ID of the remote Sone to get
371          * @return The Sone with the given ID
372          */
373         public Sone getRemoteSone(String id) {
374                 return database.getSone(id);
375         }
376
377         /**
378          * Returns whether the given Sone has been modified.
379          *
380          * @param sone
381          *            The Sone to check for modifications
382          * @return {@code true} if a modification has been detected in the Sone,
383          *         {@code false} otherwise
384          */
385         public boolean isModifiedSone(Sone sone) {
386                 return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
387         }
388
389         /**
390          * Returns a post builder.
391          *
392          * @return A new post builder
393          */
394         public PostBuilder postBuilder() {
395                 return database.newPostBuilder();
396         }
397
398         @Nullable
399         @Override
400         public Post getPost(@Nonnull String postId) {
401                 return database.getPost(postId);
402         }
403
404         /**
405          * {@inheritDocs}
406          */
407         @Override
408         public Collection<Post> getPosts(String soneId) {
409                 return database.getPosts(soneId);
410         }
411
412         /**
413          * {@inheritDoc}
414          */
415         @Override
416         public Collection<Post> getDirectedPosts(final String recipientId) {
417                 checkNotNull(recipientId, "recipient must not be null");
418                 return database.getDirectedPosts(recipientId);
419         }
420
421         /**
422          * Returns a post reply builder.
423          *
424          * @return A new post reply builder
425          */
426         public PostReplyBuilder postReplyBuilder() {
427                 return database.newPostReplyBuilder();
428         }
429
430         /**
431          * {@inheritDoc}
432          */
433         @Nullable
434         @Override
435         public PostReply getPostReply(String replyId) {
436                 return database.getPostReply(replyId);
437         }
438
439         /**
440          * {@inheritDoc}
441          */
442         @Override
443         public List<PostReply> getReplies(final String postId) {
444                 return database.getReplies(postId);
445         }
446
447         /**
448          * Returns all Sones that have liked the given post.
449          *
450          * @param post
451          *            The post to get the liking Sones for
452          * @return The Sones that like the given post
453          */
454         public Set<Sone> getLikes(Post post) {
455                 Set<Sone> sones = new HashSet<>();
456                 for (Sone sone : getSones()) {
457                         if (sone.getLikedPostIds().contains(post.getId())) {
458                                 sones.add(sone);
459                         }
460                 }
461                 return sones;
462         }
463
464         /**
465          * Returns all Sones that have liked the given reply.
466          *
467          * @param reply
468          *            The reply to get the liking Sones for
469          * @return The Sones that like the given reply
470          */
471         public Set<Sone> getLikes(PostReply reply) {
472                 Set<Sone> sones = new HashSet<>();
473                 for (Sone sone : getSones()) {
474                         if (sone.getLikedReplyIds().contains(reply.getId())) {
475                                 sones.add(sone);
476                         }
477                 }
478                 return sones;
479         }
480
481         /**
482          * Returns whether the given post is bookmarked.
483          *
484          * @param post
485          *            The post to check
486          * @return {@code true} if the given post is bookmarked, {@code false}
487          *         otherwise
488          */
489         public boolean isBookmarked(Post post) {
490                 return database.isPostBookmarked(post);
491         }
492
493         /**
494          * Returns all currently known bookmarked posts.
495          *
496          * @return All bookmarked posts
497          */
498         public Set<Post> getBookmarkedPosts() {
499                 return database.getBookmarkedPosts();
500         }
501
502         public AlbumBuilder albumBuilder() {
503                 return database.newAlbumBuilder();
504         }
505
506         /**
507          * Returns the album with the given ID, optionally creating a new album if
508          * an album with the given ID can not be found.
509          *
510          * @param albumId
511          *            The ID of the album
512          * @return The album with the given ID, or {@code null} if no album with the
513          *         given ID exists
514          */
515         @Nullable
516         public Album getAlbum(@Nonnull String albumId) {
517                 return database.getAlbum(albumId);
518         }
519
520         public ImageBuilder imageBuilder() {
521                 return database.newImageBuilder();
522         }
523
524         /**
525          * Returns the image with the given ID, creating it if necessary.
526          *
527          * @param imageId
528          *            The ID of the image
529          * @return The image with the given ID
530          */
531         @Nullable
532         public Image getImage(String imageId) {
533                 return getImage(imageId, true);
534         }
535
536         /**
537          * Returns the image with the given ID, optionally creating it if it does
538          * not exist.
539          *
540          * @param imageId
541          *            The ID of the image
542          * @param create
543          *            {@code true} to create an image if none exists with the given
544          *            ID
545          * @return The image with the given ID, or {@code null} if none exists and
546          *         none was created
547          */
548         @Nullable
549         public Image getImage(String imageId, boolean create) {
550                 Image image = database.getImage(imageId);
551                 if (image != null) {
552                         return image;
553                 }
554                 if (!create) {
555                         return null;
556                 }
557                 Image newImage = database.newImageBuilder().withId(imageId).build();
558                 database.storeImage(newImage);
559                 return newImage;
560         }
561
562         /**
563          * Returns the temporary image with the given ID.
564          *
565          * @param imageId
566          *            The ID of the temporary image
567          * @return The temporary image, or {@code null} if there is no temporary
568          *         image with the given ID
569          */
570         public TemporaryImage getTemporaryImage(String imageId) {
571                 synchronized (temporaryImages) {
572                         return temporaryImages.get(imageId);
573                 }
574         }
575
576         //
577         // ACTIONS
578         //
579
580         /**
581          * Locks the given Sone. A locked Sone will not be inserted by
582          * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
583          * again.
584          *
585          * @param sone
586          *            The sone to lock
587          */
588         public void lockSone(Sone sone) {
589                 synchronized (lockedSones) {
590                         if (lockedSones.add(sone)) {
591                                 eventBus.post(new SoneLockedEvent(sone));
592                         }
593                 }
594         }
595
596         /**
597          * Unlocks the given Sone.
598          *
599          * @see #lockSone(Sone)
600          * @param sone
601          *            The sone to unlock
602          */
603         public void unlockSone(Sone sone) {
604                 synchronized (lockedSones) {
605                         if (lockedSones.remove(sone)) {
606                                 eventBus.post(new SoneUnlockedEvent(sone));
607                         }
608                 }
609         }
610
611         /**
612          * Adds a local Sone from the given own identity.
613          *
614          * @param ownIdentity
615          *            The own identity to create a Sone from
616          * @return The added (or already existing) Sone
617          */
618         public Sone addLocalSone(OwnIdentity ownIdentity) {
619                 if (ownIdentity == null) {
620                         logger.log(Level.WARNING, "Given OwnIdentity is null!");
621                         return null;
622                 }
623                 logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
624                 Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
625                 String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0");
626                 sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
627                 sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
628                 sone.setKnown(true);
629                 SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, metricRegistry, ownIdentity.getId());
630                 soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
631                 eventBus.register(soneInserter);
632                 synchronized (soneInserters) {
633                         soneInserters.put(sone, soneInserter);
634                 }
635                 loadSone(sone);
636                 database.storeSone(sone);
637                 sone.setStatus(SoneStatus.idle);
638                 if (sone.getPosts().isEmpty() && sone.getReplies().isEmpty() && getAllImages(sone.getRootAlbum()).isEmpty()) {
639                         // dirty hack
640                         lockSone(sone);
641                         eventBus.post(new SoneLockedOnStartup(sone));
642                 }
643                 soneInserter.start();
644                 return sone;
645         }
646
647         /**
648          * Creates a new Sone for the given own identity.
649          *
650          * @param ownIdentity
651          *            The own identity to create a Sone for
652          * @return The created Sone
653          */
654         public Sone createSone(OwnIdentity ownIdentity) {
655                 if (!webOfTrustUpdater.addContextWait(ownIdentity, "Sone")) {
656                         logger.log(Level.SEVERE, String.format("Could not add “Sone” context to own identity: %s", ownIdentity));
657                         return null;
658                 }
659                 Sone sone = addLocalSone(ownIdentity);
660
661                 followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
662                 touchConfiguration();
663                 return sone;
664         }
665
666         /**
667          * Adds the Sone of the given identity.
668          *
669          * @param identity
670          *            The identity whose Sone to add
671          * @return The added or already existing Sone
672          */
673         public Sone addRemoteSone(Identity identity) {
674                 if (identity == null) {
675                         logger.log(Level.WARNING, "Given Identity is null!");
676                         return null;
677                 }
678                 String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
679                 long latestEdition = fromNullable(tryParse(property)).or(0L);
680                 Sone existingSone = getSone(identity.getId());
681                 if ((existingSone != null )&& existingSone.isLocal()) {
682                         return existingSone;
683                 }
684                 boolean newSone = existingSone == null;
685                 Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build();
686                 sone.setLatestEdition(latestEdition);
687                 if (newSone) {
688                         synchronized (knownSones) {
689                                 newSone = !knownSones.contains(sone.getId());
690                         }
691                         sone.setKnown(!newSone);
692                         if (newSone) {
693                                 eventBus.post(new NewSoneFoundEvent(sone));
694                                 for (Sone localSone : getLocalSones()) {
695                                         if (localSone.getOptions().isAutoFollow()) {
696                                                 followSone(localSone, sone.getId());
697                                         }
698                                 }
699                         }
700                 }
701                 database.storeSone(sone);
702                 soneDownloader.addSone(sone);
703                 soneDownloaders.execute(soneDownloader.fetchSoneAsUskAction(sone));
704                 return sone;
705         }
706
707         /**
708          * Lets the given local Sone follow the Sone with the given ID.
709          *
710          * @param sone
711          *            The local Sone that should follow another Sone
712          * @param soneId
713          *            The ID of the Sone to follow
714          */
715         public void followSone(Sone sone, String soneId) {
716                 checkNotNull(sone, "sone must not be null");
717                 checkNotNull(soneId, "soneId must not be null");
718                 database.addFriend(sone, soneId);
719                 @SuppressWarnings("ConstantConditions") // we just followed, this can’t be null.
720                 long now = database.getFollowingTime(soneId);
721                 Sone followedSone = getSone(soneId);
722                 if (followedSone == null) {
723                         return;
724                 }
725                 for (Post post : followedSone.getPosts()) {
726                         if (post.getTime() < now) {
727                                 markPostKnown(post);
728                         }
729                 }
730                 for (PostReply reply : followedSone.getReplies()) {
731                         if (reply.getTime() < now) {
732                                 markReplyKnown(reply);
733                         }
734                 }
735                 touchConfiguration();
736         }
737
738         /**
739          * Lets the given local Sone unfollow the Sone with the given ID.
740          *
741          * @param sone
742          *            The local Sone that should unfollow another Sone
743          * @param soneId
744          *            The ID of the Sone being unfollowed
745          */
746         public void unfollowSone(Sone sone, String soneId) {
747                 checkNotNull(sone, "sone must not be null");
748                 checkNotNull(soneId, "soneId must not be null");
749                 database.removeFriend(sone, soneId);
750                 touchConfiguration();
751         }
752
753         /**
754          * Updates the stored Sone with the given Sone.
755          *
756          * @param sone
757          *            The updated Sone
758          */
759         public void updateSone(Sone sone) {
760                 updateSone(sone, false);
761         }
762
763         /**
764          * Updates the stored Sone with the given Sone. If {@code soneRescueMode} is
765          * {@code true}, an older Sone than the current Sone can be given to restore
766          * an old state.
767          *
768          * @param sone
769          *            The Sone to update
770          * @param soneRescueMode
771          *            {@code true} if the stored Sone should be updated regardless
772          *            of the age of the given Sone
773          */
774         public void updateSone(final Sone sone, boolean soneRescueMode) {
775                 Sone storedSone = getSone(sone.getId());
776                 if (storedSone != null) {
777                         if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
778                                 logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
779                                 return;
780                         }
781                         List<Object> events =
782                                         collectEventsForChangesInSone(storedSone, sone);
783                         database.storeSone(sone);
784                         for (Object event : events) {
785                                 eventBus.post(event);
786                         }
787                         sone.setOptions(storedSone.getOptions());
788                         sone.setKnown(storedSone.isKnown());
789                         sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
790                         if (sone.isLocal()) {
791                                 touchConfiguration();
792                         }
793                 }
794         }
795
796         private List<Object> collectEventsForChangesInSone(Sone oldSone, Sone newSone) {
797                 List<Object> events = new ArrayList<>();
798                 SoneComparison soneComparison = new SoneComparison(oldSone, newSone);
799                 for (Post newPost : soneComparison.getNewPosts()) {
800                         if (newPost.getSone().equals(newSone)) {
801                                 newPost.setKnown(true);
802                         } else if (newPost.getTime() < database.getFollowingTime(newSone.getId())) {
803                                 newPost.setKnown(true);
804                         } else if (!newPost.isKnown()) {
805                                 events.add(new NewPostFoundEvent(newPost));
806                         }
807                 }
808                 for (Post post : soneComparison.getRemovedPosts()) {
809                         events.add(new PostRemovedEvent(post));
810                 }
811                 for (PostReply postReply : soneComparison.getNewPostReplies()) {
812                         if (postReply.getSone().equals(newSone)) {
813                                 postReply.setKnown(true);
814                         } else if (postReply.getTime() < database.getFollowingTime(newSone.getId())) {
815                                 postReply.setKnown(true);
816                         } else if (!postReply.isKnown()) {
817                                 events.add(new NewPostReplyFoundEvent(postReply));
818                         }
819                 }
820                 for (PostReply postReply : soneComparison.getRemovedPostReplies()) {
821                         events.add(new PostReplyRemovedEvent(postReply));
822                 }
823                 return events;
824         }
825
826         /**
827          * Deletes the given Sone. This will remove the Sone from the
828          * {@link #getLocalSones() local Sones}, stop its {@link SoneInserter} and
829          * remove the context from its identity.
830          *
831          * @param sone
832          *            The Sone to delete
833          */
834         public void deleteSone(Sone sone) {
835                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
836                         logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
837                         return;
838                 }
839                 if (!getLocalSones().contains(sone)) {
840                         logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
841                         return;
842                 }
843                 SoneInserter soneInserter = soneInserters.remove(sone);
844                 soneInserter.stop();
845                 database.removeSone(sone);
846                 webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
847                 webOfTrustUpdater.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
848                 try {
849                         configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
850                 } catch (ConfigurationException ce1) {
851                         logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
852                 }
853         }
854
855         /**
856          * Marks the given Sone as known. If the Sone was not {@link Post#isKnown()
857          * known} before, a {@link MarkSoneKnownEvent} is fired.
858          *
859          * @param sone
860          *            The Sone to mark as known
861          */
862         public void markSoneKnown(Sone sone) {
863                 if (!sone.isKnown()) {
864                         sone.setKnown(true);
865                         synchronized (knownSones) {
866                                 knownSones.add(sone.getId());
867                         }
868                         eventBus.post(new MarkSoneKnownEvent(sone));
869                         touchConfiguration();
870                 }
871         }
872
873         /**
874          * Loads and updates the given Sone from the configuration. If any error is
875          * encountered, loading is aborted and the given Sone is not changed.
876          *
877          * @param sone
878          *            The Sone to load and update
879          */
880         public void loadSone(Sone sone) {
881                 if (!sone.isLocal()) {
882                         logger.log(Level.FINE, String.format("Tried to load non-local Sone: %s", sone));
883                         return;
884                 }
885                 logger.info(String.format("Loading local Sone: %s", sone));
886
887                 /* load Sone. */
888                 String sonePrefix = "Sone/" + sone.getId();
889                 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
890                 if (soneTime == null) {
891                         logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
892                         return;
893                 }
894                 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
895
896                 /* load profile. */
897                 ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
898                 Profile profile = configurationSoneParser.parseProfile();
899
900                 /* load posts. */
901                 Collection<Post> posts;
902                 try {
903                         posts = configurationSoneParser.parsePosts(database);
904                 } catch (InvalidPostFound ipf) {
905                         logger.log(Level.WARNING, "Invalid post found, aborting load!");
906                         return;
907                 }
908
909                 /* load replies. */
910                 Collection<PostReply> replies;
911                 try {
912                         replies = configurationSoneParser.parsePostReplies(database);
913                 } catch (InvalidPostReplyFound iprf) {
914                         logger.log(Level.WARNING, "Invalid reply found, aborting load!");
915                         return;
916                 }
917
918                 /* load post likes. */
919                 Set<String> likedPostIds =
920                                 configurationSoneParser.parseLikedPostIds();
921
922                 /* load reply likes. */
923                 Set<String> likedReplyIds =
924                                 configurationSoneParser.parseLikedPostReplyIds();
925
926                 /* load albums. */
927                 List<Album> topLevelAlbums;
928                 try {
929                         topLevelAlbums =
930                                         configurationSoneParser.parseTopLevelAlbums(database);
931                 } catch (InvalidAlbumFound iaf) {
932                         logger.log(Level.WARNING, "Invalid album found, aborting load!");
933                         return;
934                 } catch (InvalidParentAlbumFound ipaf) {
935                         logger.log(Level.WARNING, format("Invalid parent album ID: %s",
936                                         ipaf.getAlbumParentId()));
937                         return;
938                 }
939
940                 /* load images. */
941                 try {
942                         configurationSoneParser.parseImages(database);
943                 } catch (InvalidImageFound iif) {
944                         logger.log(WARNING, "Invalid image found, aborting load!");
945                         return;
946                 } catch (InvalidParentAlbumFound ipaf) {
947                         logger.log(Level.WARNING,
948                                         format("Invalid album image (%s) encountered, aborting load!",
949                                                         ipaf.getAlbumParentId()));
950                         return;
951                 }
952
953                 /* load avatar. */
954                 String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
955                 if (avatarId != null) {
956                         final Map<String, Image> images =
957                                         configurationSoneParser.getImages();
958                         profile.setAvatar(images.get(avatarId));
959                 }
960
961                 /* load options. */
962                 sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
963                 sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
964                 sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
965                 sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
966                 sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
967                 sone.getOptions().setShowCustomAvatars(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(LoadExternalContent.NEVER.name())));
968                 sone.getOptions().setLoadLinkedImages(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").getValue(LoadExternalContent.NEVER.name())));
969
970                 /* if we’re still here, Sone was loaded successfully. */
971                 synchronized (sone) {
972                         sone.setTime(soneTime);
973                         sone.setProfile(profile);
974                         sone.setPosts(posts);
975                         sone.setReplies(replies);
976                         sone.setLikePostIds(likedPostIds);
977                         sone.setLikeReplyIds(likedReplyIds);
978                         for (Album album : sone.getRootAlbum().getAlbums()) {
979                                 sone.getRootAlbum().removeAlbum(album);
980                         }
981                         for (Album album : topLevelAlbums) {
982                                 sone.getRootAlbum().addAlbum(album);
983                         }
984                         synchronized (soneInserters) {
985                                 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
986                         }
987                 }
988                 for (Post post : posts) {
989                         post.setKnown(true);
990                 }
991                 for (PostReply reply : replies) {
992                         reply.setKnown(true);
993                 }
994
995                 logger.info(String.format("Sone loaded successfully: %s", sone));
996         }
997
998         /**
999          * Creates a new post.
1000          *
1001          * @param sone
1002          *            The Sone that creates the post
1003          * @param recipient
1004          *            The recipient Sone, or {@code null} if this post does not have
1005          *            a recipient
1006          * @param text
1007          *            The text of the post
1008          * @return The created post
1009          */
1010         public Post createPost(Sone sone, @Nullable Sone recipient, String text) {
1011                 checkNotNull(text, "text must not be null");
1012                 checkArgument(text.trim().length() > 0, "text must not be empty");
1013                 if (!sone.isLocal()) {
1014                         logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
1015                         return null;
1016                 }
1017                 PostBuilder postBuilder = database.newPostBuilder();
1018                 postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim());
1019                 if (recipient != null) {
1020                         postBuilder.to(recipient.getId());
1021                 }
1022                 final Post post = postBuilder.build();
1023                 database.storePost(post);
1024                 eventBus.post(new NewPostFoundEvent(post));
1025                 sone.addPost(post);
1026                 touchConfiguration();
1027                 localElementTicker.schedule(new MarkPostKnown(post), 10, TimeUnit.SECONDS);
1028                 return post;
1029         }
1030
1031         /**
1032          * Deletes the given post.
1033          *
1034          * @param post
1035          *            The post to delete
1036          */
1037         public void deletePost(Post post) {
1038                 if (!post.getSone().isLocal()) {
1039                         logger.log(Level.WARNING, String.format("Tried to delete post of non-local Sone: %s", post.getSone()));
1040                         return;
1041                 }
1042                 database.removePost(post);
1043                 eventBus.post(new PostRemovedEvent(post));
1044                 markPostKnown(post);
1045                 touchConfiguration();
1046         }
1047
1048         /**
1049          * Marks the given post as known, if it is currently not a known post
1050          * (according to {@link Post#isKnown()}).
1051          *
1052          * @param post
1053          *            The post to mark as known
1054          */
1055         public void markPostKnown(Post post) {
1056                 post.setKnown(true);
1057                 eventBus.post(new MarkPostKnownEvent(post));
1058                 touchConfiguration();
1059                 for (PostReply reply : getReplies(post.getId())) {
1060                         markReplyKnown(reply);
1061                 }
1062         }
1063
1064         public void bookmarkPost(Post post) {
1065                 database.bookmarkPost(post);
1066         }
1067
1068         /**
1069          * Removes the given post from the bookmarks.
1070          *
1071          * @param post
1072          *            The post to unbookmark
1073          */
1074         public void unbookmarkPost(Post post) {
1075                 database.unbookmarkPost(post);
1076         }
1077
1078         /**
1079          * Creates a new reply.
1080          *
1081          * @param sone
1082          *            The Sone that creates the reply
1083          * @param post
1084          *            The post that this reply refers to
1085          * @param text
1086          *            The text of the reply
1087          * @return The created reply
1088          */
1089         public PostReply createReply(Sone sone, Post post, String text) {
1090                 checkNotNull(text, "text must not be null");
1091                 checkArgument(text.trim().length() > 0, "text must not be empty");
1092                 if (!sone.isLocal()) {
1093                         logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone));
1094                         return null;
1095                 }
1096                 PostReplyBuilder postReplyBuilder = postReplyBuilder();
1097                 postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
1098                 final PostReply reply = postReplyBuilder.build();
1099                 database.storePostReply(reply);
1100                 eventBus.post(new NewPostReplyFoundEvent(reply));
1101                 sone.addReply(reply);
1102                 touchConfiguration();
1103                 localElementTicker.schedule(new MarkReplyKnown(reply), 10, TimeUnit.SECONDS);
1104                 return reply;
1105         }
1106
1107         /**
1108          * Deletes the given reply.
1109          *
1110          * @param reply
1111          *            The reply to delete
1112          */
1113         public void deleteReply(PostReply reply) {
1114                 Sone sone = reply.getSone();
1115                 if (!sone.isLocal()) {
1116                         logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
1117                         return;
1118                 }
1119                 database.removePostReply(reply);
1120                 markReplyKnown(reply);
1121                 sone.removeReply(reply);
1122                 touchConfiguration();
1123         }
1124
1125         /**
1126          * Marks the given reply as known, if it is currently not a known reply
1127          * (according to {@link Reply#isKnown()}).
1128          *
1129          * @param reply
1130          *            The reply to mark as known
1131          */
1132         public void markReplyKnown(PostReply reply) {
1133                 boolean previouslyKnown = reply.isKnown();
1134                 reply.setKnown(true);
1135                 eventBus.post(new MarkPostReplyKnownEvent(reply));
1136                 if (!previouslyKnown) {
1137                         touchConfiguration();
1138                 }
1139         }
1140
1141         /**
1142          * Creates a new album for the given Sone.
1143          *
1144          * @param sone
1145          *            The Sone to create the album for
1146          * @param parent
1147          *            The parent of the album (may be {@code null} to create a
1148          *            top-level album)
1149          * @return The new album
1150          */
1151         public Album createAlbum(Sone sone, Album parent) {
1152                 Album album = database.newAlbumBuilder().randomId().by(sone).build();
1153                 database.storeAlbum(album);
1154                 parent.addAlbum(album);
1155                 return album;
1156         }
1157
1158         /**
1159          * Deletes the given album. The owner of the album has to be a local Sone,
1160          * and the album has to be {@link Album#isEmpty() empty} to be deleted.
1161          *
1162          * @param album
1163          *            The album to remove
1164          */
1165         public void deleteAlbum(Album album) {
1166                 checkNotNull(album, "album must not be null");
1167                 checkArgument(album.getSone().isLocal(), "album’s Sone must be a local Sone");
1168                 if (!album.isEmpty()) {
1169                         return;
1170                 }
1171                 album.getParent().removeAlbum(album);
1172                 database.removeAlbum(album);
1173                 touchConfiguration();
1174         }
1175
1176         /**
1177          * Creates a new image.
1178          *
1179          * @param sone
1180          *            The Sone creating the image
1181          * @param album
1182          *            The album the image will be inserted into
1183          * @param temporaryImage
1184          *            The temporary image to create the image from
1185          * @return The newly created image
1186          */
1187         public Image createImage(Sone sone, Album album, TemporaryImage temporaryImage) {
1188                 checkNotNull(sone, "sone must not be null");
1189                 checkNotNull(album, "album must not be null");
1190                 checkNotNull(temporaryImage, "temporaryImage must not be null");
1191                 checkArgument(sone.isLocal(), "sone must be a local Sone");
1192                 checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
1193                 Image image = database.newImageBuilder().withId(temporaryImage.getId()).build().modify().setSone(sone).setCreationTime(System.currentTimeMillis()).update();
1194                 album.addImage(image);
1195                 database.storeImage(image);
1196                 imageInserter.insertImage(temporaryImage, image);
1197                 return image;
1198         }
1199
1200         /**
1201          * Deletes the given image. This method will also delete a matching
1202          * temporary image.
1203          *
1204          * @see #deleteTemporaryImage(String)
1205          * @param image
1206          *            The image to delete
1207          */
1208         public void deleteImage(Image image) {
1209                 checkNotNull(image, "image must not be null");
1210                 checkArgument(image.getSone().isLocal(), "image must belong to a local Sone");
1211                 deleteTemporaryImage(image.getId());
1212                 image.getAlbum().removeImage(image);
1213                 database.removeImage(image);
1214                 touchConfiguration();
1215         }
1216
1217         /**
1218          * Creates a new temporary image.
1219          *
1220          * @param mimeType
1221          *            The MIME type of the temporary image
1222          * @param imageData
1223          *            The encoded data of the image
1224          * @return The temporary image
1225          */
1226         public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData) {
1227                 TemporaryImage temporaryImage = new TemporaryImage();
1228                 temporaryImage.setMimeType(mimeType).setImageData(imageData);
1229                 synchronized (temporaryImages) {
1230                         temporaryImages.put(temporaryImage.getId(), temporaryImage);
1231                 }
1232                 return temporaryImage;
1233         }
1234
1235         /**
1236          * Deletes the temporary image with the given ID.
1237          *
1238          * @param imageId
1239          *            The ID of the temporary image to delete
1240          */
1241         public void deleteTemporaryImage(String imageId) {
1242                 checkNotNull(imageId, "imageId must not be null");
1243                 synchronized (temporaryImages) {
1244                         temporaryImages.remove(imageId);
1245                 }
1246                 Image image = getImage(imageId, false);
1247                 if (image != null) {
1248                         imageInserter.cancelImageInsert(image);
1249                 }
1250         }
1251
1252         /**
1253          * Notifies the core that the configuration, either of the core or of a
1254          * single local Sone, has changed, and that the configuration should be
1255          * saved.
1256          */
1257         public void touchConfiguration() {
1258                 lastConfigurationUpdate = System.currentTimeMillis();
1259         }
1260
1261         //
1262         // SERVICE METHODS
1263         //
1264
1265         /**
1266          * Starts the core.
1267          */
1268         @Override
1269         public void serviceStart() {
1270                 loadConfiguration();
1271                 updateChecker.start();
1272                 identityManager.start();
1273                 webOfTrustUpdater.init();
1274                 webOfTrustUpdater.start();
1275                 database.startAsync();
1276         }
1277
1278         /**
1279          * {@inheritDoc}
1280          */
1281         @Override
1282         public void serviceRun() {
1283                 long lastSaved = System.currentTimeMillis();
1284                 while (!shouldStop()) {
1285                         sleep(1000);
1286                         long now = System.currentTimeMillis();
1287                         if (shouldStop() || ((lastConfigurationUpdate > lastSaved) && ((now - lastConfigurationUpdate) > 5000))) {
1288                                 for (Sone localSone : getLocalSones()) {
1289                                         saveSone(localSone);
1290                                 }
1291                                 saveConfiguration();
1292                                 lastSaved = now;
1293                         }
1294                 }
1295         }
1296
1297         /**
1298          * Stops the core.
1299          */
1300         @Override
1301         public void serviceStop() {
1302                 localElementTicker.shutdownNow();
1303                 synchronized (soneInserters) {
1304                         for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
1305                                 soneInserter.getValue().stop();
1306                                 Sone latestSone = getLocalSone(soneInserter.getKey().getId());
1307                                 saveSone(latestSone);
1308                         }
1309                 }
1310                 synchronized (soneRescuers) {
1311                         for (SoneRescuer soneRescuer : soneRescuers.values()) {
1312                                 soneRescuer.stop();
1313                         }
1314                 }
1315                 saveConfiguration();
1316                 database.stopAsync();
1317                 webOfTrustUpdater.stop();
1318                 updateChecker.stop();
1319                 soneDownloader.stop();
1320                 soneDownloaders.shutdown();
1321                 identityManager.stop();
1322         }
1323
1324         //
1325         // PRIVATE METHODS
1326         //
1327
1328         /**
1329          * Saves the given Sone. This will persist all local settings for the given
1330          * Sone, such as the friends list and similar, private options.
1331          *
1332          * @param sone
1333          *            The Sone to save
1334          */
1335         private synchronized void saveSone(Sone sone) {
1336                 if (!sone.isLocal()) {
1337                         logger.log(Level.FINE, String.format("Tried to save non-local Sone: %s", sone));
1338                         return;
1339                 }
1340                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1341                         logger.log(Level.WARNING, String.format("Local Sone without OwnIdentity found, refusing to save: %s", sone));
1342                         return;
1343                 }
1344
1345                 logger.log(Level.INFO, String.format("Saving Sone: %s", sone));
1346                 try {
1347                         /* save Sone into configuration. */
1348                         String sonePrefix = "Sone/" + sone.getId();
1349                         configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1350                         configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1351
1352                         /* save profile. */
1353                         Profile profile = sone.getProfile();
1354                         configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1355                         configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1356                         configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1357                         configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1358                         configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1359                         configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1360                         configuration.getStringValue(sonePrefix + "/Profile/Avatar").setValue(profile.getAvatar());
1361
1362                         /* save profile fields. */
1363                         int fieldCounter = 0;
1364                         for (Field profileField : profile.getFields()) {
1365                                 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1366                                 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1367                                 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1368                         }
1369                         configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1370
1371                         /* save posts. */
1372                         int postCounter = 0;
1373                         for (Post post : sone.getPosts()) {
1374                                 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1375                                 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1376                                 configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
1377                                 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1378                                 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1379                         }
1380                         configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1381
1382                         /* save replies. */
1383                         int replyCounter = 0;
1384                         for (PostReply reply : sone.getReplies()) {
1385                                 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1386                                 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1387                                 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
1388                                 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1389                                 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1390                         }
1391                         configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1392
1393                         /* save post likes. */
1394                         int postLikeCounter = 0;
1395                         for (String postId : sone.getLikedPostIds()) {
1396                                 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1397                         }
1398                         configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1399
1400                         /* save reply likes. */
1401                         int replyLikeCounter = 0;
1402                         for (String replyId : sone.getLikedReplyIds()) {
1403                                 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1404                         }
1405                         configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1406
1407                         /* save albums. first, collect in a flat structure, top-level first. */
1408                         List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
1409
1410                         int albumCounter = 0;
1411                         for (Album album : albums) {
1412                                 String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
1413                                 configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
1414                                 configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
1415                                 configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
1416                                 configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
1417                         }
1418                         configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
1419
1420                         /* save images. */
1421                         int imageCounter = 0;
1422                         for (Album album : albums) {
1423                                 for (Image image : album.getImages()) {
1424                                         if (!image.isInserted()) {
1425                                                 continue;
1426                                         }
1427                                         String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
1428                                         configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
1429                                         configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
1430                                         configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
1431                                         configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
1432                                         configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());
1433                                         configuration.getLongValue(imagePrefix + "/CreationTime").setValue(image.getCreationTime());
1434                                         configuration.getIntValue(imagePrefix + "/Width").setValue(image.getWidth());
1435                                         configuration.getIntValue(imagePrefix + "/Height").setValue(image.getHeight());
1436                                 }
1437                         }
1438                         configuration.getStringValue(sonePrefix + "/Images/" + imageCounter + "/ID").setValue(null);
1439
1440                         /* save options. */
1441                         configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().isAutoFollow());
1442                         configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().isSoneInsertNotificationEnabled());
1443                         configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications());
1444                         configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
1445                         configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
1446                         configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
1447                         configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").setValue(sone.getOptions().getLoadLinkedImages().name());
1448
1449                         webOfTrustUpdater.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1450
1451                         logger.log(Level.INFO, String.format("Sone %s saved.", sone));
1452                 } catch (ConfigurationException ce1) {
1453                         logger.log(Level.WARNING, String.format("Could not save Sone: %s", sone), ce1);
1454                 }
1455         }
1456
1457         /**
1458          * Saves the current options.
1459          */
1460         private void saveConfiguration() {
1461                 synchronized (configuration) {
1462                         if (storingConfiguration) {
1463                                 logger.log(Level.FINE, "Already storing configuration…");
1464                                 return;
1465                         }
1466                         storingConfiguration = true;
1467                 }
1468
1469                 /* store the options first. */
1470                 try {
1471                         preferences.saveTo(configuration);
1472
1473                         /* save known Sones. */
1474                         int soneCounter = 0;
1475                         synchronized (knownSones) {
1476                                 for (String knownSoneId : knownSones) {
1477                                         configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1478                                 }
1479                                 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1480                         }
1481
1482                         /* save known posts. */
1483                         database.save();
1484
1485                         /* now save it. */
1486                         Stopwatch stopwatch = Stopwatch.createStarted();
1487                         configuration.save();
1488                         configurationSaveTimeHistogram.update(stopwatch.elapsed(TimeUnit.MICROSECONDS));
1489
1490                 } catch (ConfigurationException ce1) {
1491                         logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1492                 } catch (DatabaseException de1) {
1493                         logger.log(Level.SEVERE, "Could not save database!", de1);
1494                 } finally {
1495                         synchronized (configuration) {
1496                                 storingConfiguration = false;
1497                         }
1498                 }
1499         }
1500
1501         /**
1502          * Loads the configuration.
1503          */
1504         private void loadConfiguration() {
1505                 new PreferencesLoader(preferences).loadFrom(configuration);
1506
1507                 /* load known Sones. */
1508                 int soneCounter = 0;
1509                 while (true) {
1510                         String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1511                         if (knownSoneId == null) {
1512                                 break;
1513                         }
1514                         synchronized (knownSones) {
1515                                 knownSones.add(knownSoneId);
1516                         }
1517                 }
1518         }
1519
1520         /**
1521          * Notifies the core that a new {@link OwnIdentity} was added.
1522          *
1523          * @param ownIdentityAddedEvent
1524          *            The event
1525          */
1526         @Subscribe
1527         public void ownIdentityAdded(OwnIdentityAddedEvent ownIdentityAddedEvent) {
1528                 OwnIdentity ownIdentity = ownIdentityAddedEvent.getOwnIdentity();
1529                 logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity));
1530                 if (ownIdentity.hasContext("Sone")) {
1531                         addLocalSone(ownIdentity);
1532                 }
1533         }
1534
1535         /**
1536          * Notifies the core that an {@link OwnIdentity} was removed.
1537          *
1538          * @param ownIdentityRemovedEvent
1539          *            The event
1540          */
1541         @Subscribe
1542         public void ownIdentityRemoved(OwnIdentityRemovedEvent ownIdentityRemovedEvent) {
1543                 OwnIdentity ownIdentity = ownIdentityRemovedEvent.getOwnIdentity();
1544                 logger.log(Level.FINEST, String.format("Removing OwnIdentity: %s", ownIdentity));
1545                 trustedIdentities.removeAll(ownIdentity);
1546         }
1547
1548         /**
1549          * Notifies the core that a new {@link Identity} was added.
1550          *
1551          * @param identityAddedEvent
1552          *            The event
1553          */
1554         @Subscribe
1555         public void identityAdded(IdentityAddedEvent identityAddedEvent) {
1556                 Identity identity = identityAddedEvent.getIdentity();
1557                 logger.log(Level.FINEST, String.format("Adding Identity: %s", identity));
1558                 trustedIdentities.put(identityAddedEvent.getOwnIdentity(), identity);
1559                 addRemoteSone(identity);
1560         }
1561
1562         /**
1563          * Notifies the core that an {@link Identity} was updated.
1564          *
1565          * @param identityUpdatedEvent
1566          *            The event
1567          */
1568         @Subscribe
1569         public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
1570                 Identity identity = identityUpdatedEvent.getIdentity();
1571                 final Sone sone = getRemoteSone(identity.getId());
1572                 if (sone.isLocal()) {
1573                         return;
1574                 }
1575                 String newLatestEdition = identity.getProperty("Sone.LatestEdition");
1576                 if (newLatestEdition != null) {
1577                         Long parsedNewLatestEdition = tryParse(newLatestEdition);
1578                         if (parsedNewLatestEdition != null) {
1579                                 sone.setLatestEdition(parsedNewLatestEdition);
1580                         }
1581                 }
1582                 soneDownloader.addSone(sone);
1583                 soneDownloaders.execute(soneDownloader.fetchSoneAsSskAction(sone));
1584         }
1585
1586         /**
1587          * Notifies the core that an {@link Identity} was removed.
1588          *
1589          * @param identityRemovedEvent
1590          *            The event
1591          */
1592         @Subscribe
1593         public void identityRemoved(IdentityRemovedEvent identityRemovedEvent) {
1594                 OwnIdentity ownIdentity = identityRemovedEvent.getOwnIdentity();
1595                 Identity identity = identityRemovedEvent.getIdentity();
1596                 trustedIdentities.remove(ownIdentity, identity);
1597                 for (Entry<OwnIdentity, Collection<Identity>> trustedIdentity : trustedIdentities.asMap().entrySet()) {
1598                         if (trustedIdentity.getKey().equals(ownIdentity)) {
1599                                 continue;
1600                         }
1601                         if (trustedIdentity.getValue().contains(identity)) {
1602                                 return;
1603                         }
1604                 }
1605                 Sone sone = getSone(identity.getId());
1606                 if (sone == null) {
1607                         /* TODO - we don’t have the Sone anymore. should this happen? */
1608                         return;
1609                 }
1610                 for (PostReply postReply : sone.getReplies()) {
1611                         eventBus.post(new PostReplyRemovedEvent(postReply));
1612                 }
1613                 for (Post post : sone.getPosts()) {
1614                         eventBus.post(new PostRemovedEvent(post));
1615                 }
1616                 eventBus.post(new SoneRemovedEvent(sone));
1617                 database.removeSone(sone);
1618         }
1619
1620         /**
1621          * Deletes the temporary image.
1622          *
1623          * @param imageInsertFinishedEvent
1624          *            The event
1625          */
1626         @Subscribe
1627         public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1628                 logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.getImage(), imageInsertFinishedEvent.getResultingUri()));
1629                 imageInsertFinishedEvent.getImage().modify().setKey(imageInsertFinishedEvent.getResultingUri().toString()).update();
1630                 deleteTemporaryImage(imageInsertFinishedEvent.getImage().getId());
1631                 touchConfiguration();
1632         }
1633
1634         @VisibleForTesting
1635         class MarkPostKnown implements Runnable {
1636
1637                 private final Post post;
1638
1639                 public MarkPostKnown(Post post) {
1640                         this.post = post;
1641                 }
1642
1643                 @Override
1644                 public void run() {
1645                         markPostKnown(post);
1646                 }
1647
1648         }
1649
1650         @VisibleForTesting
1651         class MarkReplyKnown implements Runnable {
1652
1653                 private final PostReply postReply;
1654
1655                 public MarkReplyKnown(PostReply postReply) {
1656                         this.postReply = postReply;
1657                 }
1658
1659                 @Override
1660                 public void run() {
1661                         markReplyKnown(postReply);
1662                 }
1663
1664         }
1665
1666 }