🚸 Only lock Sone if it doesn’t have images, either
[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                 }
642                 soneInserter.start();
643                 return sone;
644         }
645
646         /**
647          * Creates a new Sone for the given own identity.
648          *
649          * @param ownIdentity
650          *            The own identity to create a Sone for
651          * @return The created Sone
652          */
653         public Sone createSone(OwnIdentity ownIdentity) {
654                 if (!webOfTrustUpdater.addContextWait(ownIdentity, "Sone")) {
655                         logger.log(Level.SEVERE, String.format("Could not add “Sone” context to own identity: %s", ownIdentity));
656                         return null;
657                 }
658                 Sone sone = addLocalSone(ownIdentity);
659
660                 followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
661                 touchConfiguration();
662                 return sone;
663         }
664
665         /**
666          * Adds the Sone of the given identity.
667          *
668          * @param identity
669          *            The identity whose Sone to add
670          * @return The added or already existing Sone
671          */
672         public Sone addRemoteSone(Identity identity) {
673                 if (identity == null) {
674                         logger.log(Level.WARNING, "Given Identity is null!");
675                         return null;
676                 }
677                 String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
678                 long latestEdition = fromNullable(tryParse(property)).or(0L);
679                 Sone existingSone = getSone(identity.getId());
680                 if ((existingSone != null )&& existingSone.isLocal()) {
681                         return existingSone;
682                 }
683                 boolean newSone = existingSone == null;
684                 Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build();
685                 sone.setLatestEdition(latestEdition);
686                 if (newSone) {
687                         synchronized (knownSones) {
688                                 newSone = !knownSones.contains(sone.getId());
689                         }
690                         sone.setKnown(!newSone);
691                         if (newSone) {
692                                 eventBus.post(new NewSoneFoundEvent(sone));
693                                 for (Sone localSone : getLocalSones()) {
694                                         if (localSone.getOptions().isAutoFollow()) {
695                                                 followSone(localSone, sone.getId());
696                                         }
697                                 }
698                         }
699                 }
700                 database.storeSone(sone);
701                 soneDownloader.addSone(sone);
702                 soneDownloaders.execute(soneDownloader.fetchSoneAsUskAction(sone));
703                 return sone;
704         }
705
706         /**
707          * Lets the given local Sone follow the Sone with the given ID.
708          *
709          * @param sone
710          *            The local Sone that should follow another Sone
711          * @param soneId
712          *            The ID of the Sone to follow
713          */
714         public void followSone(Sone sone, String soneId) {
715                 checkNotNull(sone, "sone must not be null");
716                 checkNotNull(soneId, "soneId must not be null");
717                 database.addFriend(sone, soneId);
718                 @SuppressWarnings("ConstantConditions") // we just followed, this can’t be null.
719                 long now = database.getFollowingTime(soneId);
720                 Sone followedSone = getSone(soneId);
721                 if (followedSone == null) {
722                         return;
723                 }
724                 for (Post post : followedSone.getPosts()) {
725                         if (post.getTime() < now) {
726                                 markPostKnown(post);
727                         }
728                 }
729                 for (PostReply reply : followedSone.getReplies()) {
730                         if (reply.getTime() < now) {
731                                 markReplyKnown(reply);
732                         }
733                 }
734                 touchConfiguration();
735         }
736
737         /**
738          * Lets the given local Sone unfollow the Sone with the given ID.
739          *
740          * @param sone
741          *            The local Sone that should unfollow another Sone
742          * @param soneId
743          *            The ID of the Sone being unfollowed
744          */
745         public void unfollowSone(Sone sone, String soneId) {
746                 checkNotNull(sone, "sone must not be null");
747                 checkNotNull(soneId, "soneId must not be null");
748                 database.removeFriend(sone, soneId);
749                 touchConfiguration();
750         }
751
752         /**
753          * Updates the stored Sone with the given Sone.
754          *
755          * @param sone
756          *            The updated Sone
757          */
758         public void updateSone(Sone sone) {
759                 updateSone(sone, false);
760         }
761
762         /**
763          * Updates the stored Sone with the given Sone. If {@code soneRescueMode} is
764          * {@code true}, an older Sone than the current Sone can be given to restore
765          * an old state.
766          *
767          * @param sone
768          *            The Sone to update
769          * @param soneRescueMode
770          *            {@code true} if the stored Sone should be updated regardless
771          *            of the age of the given Sone
772          */
773         public void updateSone(final Sone sone, boolean soneRescueMode) {
774                 Sone storedSone = getSone(sone.getId());
775                 if (storedSone != null) {
776                         if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
777                                 logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
778                                 return;
779                         }
780                         List<Object> events =
781                                         collectEventsForChangesInSone(storedSone, sone);
782                         database.storeSone(sone);
783                         for (Object event : events) {
784                                 eventBus.post(event);
785                         }
786                         sone.setOptions(storedSone.getOptions());
787                         sone.setKnown(storedSone.isKnown());
788                         sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
789                         if (sone.isLocal()) {
790                                 touchConfiguration();
791                         }
792                 }
793         }
794
795         private List<Object> collectEventsForChangesInSone(Sone oldSone, Sone newSone) {
796                 List<Object> events = new ArrayList<>();
797                 SoneComparison soneComparison = new SoneComparison(oldSone, newSone);
798                 for (Post newPost : soneComparison.getNewPosts()) {
799                         if (newPost.getSone().equals(newSone)) {
800                                 newPost.setKnown(true);
801                         } else if (newPost.getTime() < database.getFollowingTime(newSone.getId())) {
802                                 newPost.setKnown(true);
803                         } else if (!newPost.isKnown()) {
804                                 events.add(new NewPostFoundEvent(newPost));
805                         }
806                 }
807                 for (Post post : soneComparison.getRemovedPosts()) {
808                         events.add(new PostRemovedEvent(post));
809                 }
810                 for (PostReply postReply : soneComparison.getNewPostReplies()) {
811                         if (postReply.getSone().equals(newSone)) {
812                                 postReply.setKnown(true);
813                         } else if (postReply.getTime() < database.getFollowingTime(newSone.getId())) {
814                                 postReply.setKnown(true);
815                         } else if (!postReply.isKnown()) {
816                                 events.add(new NewPostReplyFoundEvent(postReply));
817                         }
818                 }
819                 for (PostReply postReply : soneComparison.getRemovedPostReplies()) {
820                         events.add(new PostReplyRemovedEvent(postReply));
821                 }
822                 return events;
823         }
824
825         /**
826          * Deletes the given Sone. This will remove the Sone from the
827          * {@link #getLocalSones() local Sones}, stop its {@link SoneInserter} and
828          * remove the context from its identity.
829          *
830          * @param sone
831          *            The Sone to delete
832          */
833         public void deleteSone(Sone sone) {
834                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
835                         logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
836                         return;
837                 }
838                 if (!getLocalSones().contains(sone)) {
839                         logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
840                         return;
841                 }
842                 SoneInserter soneInserter = soneInserters.remove(sone);
843                 soneInserter.stop();
844                 database.removeSone(sone);
845                 webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
846                 webOfTrustUpdater.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
847                 try {
848                         configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
849                 } catch (ConfigurationException ce1) {
850                         logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
851                 }
852         }
853
854         /**
855          * Marks the given Sone as known. If the Sone was not {@link Post#isKnown()
856          * known} before, a {@link MarkSoneKnownEvent} is fired.
857          *
858          * @param sone
859          *            The Sone to mark as known
860          */
861         public void markSoneKnown(Sone sone) {
862                 if (!sone.isKnown()) {
863                         sone.setKnown(true);
864                         synchronized (knownSones) {
865                                 knownSones.add(sone.getId());
866                         }
867                         eventBus.post(new MarkSoneKnownEvent(sone));
868                         touchConfiguration();
869                 }
870         }
871
872         /**
873          * Loads and updates the given Sone from the configuration. If any error is
874          * encountered, loading is aborted and the given Sone is not changed.
875          *
876          * @param sone
877          *            The Sone to load and update
878          */
879         public void loadSone(Sone sone) {
880                 if (!sone.isLocal()) {
881                         logger.log(Level.FINE, String.format("Tried to load non-local Sone: %s", sone));
882                         return;
883                 }
884                 logger.info(String.format("Loading local Sone: %s", sone));
885
886                 /* load Sone. */
887                 String sonePrefix = "Sone/" + sone.getId();
888                 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
889                 if (soneTime == null) {
890                         logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
891                         return;
892                 }
893                 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
894
895                 /* load profile. */
896                 ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
897                 Profile profile = configurationSoneParser.parseProfile();
898
899                 /* load posts. */
900                 Collection<Post> posts;
901                 try {
902                         posts = configurationSoneParser.parsePosts(database);
903                 } catch (InvalidPostFound ipf) {
904                         logger.log(Level.WARNING, "Invalid post found, aborting load!");
905                         return;
906                 }
907
908                 /* load replies. */
909                 Collection<PostReply> replies;
910                 try {
911                         replies = configurationSoneParser.parsePostReplies(database);
912                 } catch (InvalidPostReplyFound iprf) {
913                         logger.log(Level.WARNING, "Invalid reply found, aborting load!");
914                         return;
915                 }
916
917                 /* load post likes. */
918                 Set<String> likedPostIds =
919                                 configurationSoneParser.parseLikedPostIds();
920
921                 /* load reply likes. */
922                 Set<String> likedReplyIds =
923                                 configurationSoneParser.parseLikedPostReplyIds();
924
925                 /* load albums. */
926                 List<Album> topLevelAlbums;
927                 try {
928                         topLevelAlbums =
929                                         configurationSoneParser.parseTopLevelAlbums(database);
930                 } catch (InvalidAlbumFound iaf) {
931                         logger.log(Level.WARNING, "Invalid album found, aborting load!");
932                         return;
933                 } catch (InvalidParentAlbumFound ipaf) {
934                         logger.log(Level.WARNING, format("Invalid parent album ID: %s",
935                                         ipaf.getAlbumParentId()));
936                         return;
937                 }
938
939                 /* load images. */
940                 try {
941                         configurationSoneParser.parseImages(database);
942                 } catch (InvalidImageFound iif) {
943                         logger.log(WARNING, "Invalid image found, aborting load!");
944                         return;
945                 } catch (InvalidParentAlbumFound ipaf) {
946                         logger.log(Level.WARNING,
947                                         format("Invalid album image (%s) encountered, aborting load!",
948                                                         ipaf.getAlbumParentId()));
949                         return;
950                 }
951
952                 /* load avatar. */
953                 String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
954                 if (avatarId != null) {
955                         final Map<String, Image> images =
956                                         configurationSoneParser.getImages();
957                         profile.setAvatar(images.get(avatarId));
958                 }
959
960                 /* load options. */
961                 sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
962                 sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
963                 sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
964                 sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
965                 sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
966                 sone.getOptions().setShowCustomAvatars(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(LoadExternalContent.NEVER.name())));
967                 sone.getOptions().setLoadLinkedImages(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").getValue(LoadExternalContent.NEVER.name())));
968
969                 /* if we’re still here, Sone was loaded successfully. */
970                 synchronized (sone) {
971                         sone.setTime(soneTime);
972                         sone.setProfile(profile);
973                         sone.setPosts(posts);
974                         sone.setReplies(replies);
975                         sone.setLikePostIds(likedPostIds);
976                         sone.setLikeReplyIds(likedReplyIds);
977                         for (Album album : sone.getRootAlbum().getAlbums()) {
978                                 sone.getRootAlbum().removeAlbum(album);
979                         }
980                         for (Album album : topLevelAlbums) {
981                                 sone.getRootAlbum().addAlbum(album);
982                         }
983                         synchronized (soneInserters) {
984                                 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
985                         }
986                 }
987                 for (Post post : posts) {
988                         post.setKnown(true);
989                 }
990                 for (PostReply reply : replies) {
991                         reply.setKnown(true);
992                 }
993
994                 logger.info(String.format("Sone loaded successfully: %s", sone));
995         }
996
997         /**
998          * Creates a new post.
999          *
1000          * @param sone
1001          *            The Sone that creates the post
1002          * @param recipient
1003          *            The recipient Sone, or {@code null} if this post does not have
1004          *            a recipient
1005          * @param text
1006          *            The text of the post
1007          * @return The created post
1008          */
1009         public Post createPost(Sone sone, @Nullable Sone recipient, String text) {
1010                 checkNotNull(text, "text must not be null");
1011                 checkArgument(text.trim().length() > 0, "text must not be empty");
1012                 if (!sone.isLocal()) {
1013                         logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
1014                         return null;
1015                 }
1016                 PostBuilder postBuilder = database.newPostBuilder();
1017                 postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim());
1018                 if (recipient != null) {
1019                         postBuilder.to(recipient.getId());
1020                 }
1021                 final Post post = postBuilder.build();
1022                 database.storePost(post);
1023                 eventBus.post(new NewPostFoundEvent(post));
1024                 sone.addPost(post);
1025                 touchConfiguration();
1026                 localElementTicker.schedule(new MarkPostKnown(post), 10, TimeUnit.SECONDS);
1027                 return post;
1028         }
1029
1030         /**
1031          * Deletes the given post.
1032          *
1033          * @param post
1034          *            The post to delete
1035          */
1036         public void deletePost(Post post) {
1037                 if (!post.getSone().isLocal()) {
1038                         logger.log(Level.WARNING, String.format("Tried to delete post of non-local Sone: %s", post.getSone()));
1039                         return;
1040                 }
1041                 database.removePost(post);
1042                 eventBus.post(new PostRemovedEvent(post));
1043                 markPostKnown(post);
1044                 touchConfiguration();
1045         }
1046
1047         /**
1048          * Marks the given post as known, if it is currently not a known post
1049          * (according to {@link Post#isKnown()}).
1050          *
1051          * @param post
1052          *            The post to mark as known
1053          */
1054         public void markPostKnown(Post post) {
1055                 post.setKnown(true);
1056                 eventBus.post(new MarkPostKnownEvent(post));
1057                 touchConfiguration();
1058                 for (PostReply reply : getReplies(post.getId())) {
1059                         markReplyKnown(reply);
1060                 }
1061         }
1062
1063         public void bookmarkPost(Post post) {
1064                 database.bookmarkPost(post);
1065         }
1066
1067         /**
1068          * Removes the given post from the bookmarks.
1069          *
1070          * @param post
1071          *            The post to unbookmark
1072          */
1073         public void unbookmarkPost(Post post) {
1074                 database.unbookmarkPost(post);
1075         }
1076
1077         /**
1078          * Creates a new reply.
1079          *
1080          * @param sone
1081          *            The Sone that creates the reply
1082          * @param post
1083          *            The post that this reply refers to
1084          * @param text
1085          *            The text of the reply
1086          * @return The created reply
1087          */
1088         public PostReply createReply(Sone sone, Post post, String text) {
1089                 checkNotNull(text, "text must not be null");
1090                 checkArgument(text.trim().length() > 0, "text must not be empty");
1091                 if (!sone.isLocal()) {
1092                         logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone));
1093                         return null;
1094                 }
1095                 PostReplyBuilder postReplyBuilder = postReplyBuilder();
1096                 postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
1097                 final PostReply reply = postReplyBuilder.build();
1098                 database.storePostReply(reply);
1099                 eventBus.post(new NewPostReplyFoundEvent(reply));
1100                 sone.addReply(reply);
1101                 touchConfiguration();
1102                 localElementTicker.schedule(new MarkReplyKnown(reply), 10, TimeUnit.SECONDS);
1103                 return reply;
1104         }
1105
1106         /**
1107          * Deletes the given reply.
1108          *
1109          * @param reply
1110          *            The reply to delete
1111          */
1112         public void deleteReply(PostReply reply) {
1113                 Sone sone = reply.getSone();
1114                 if (!sone.isLocal()) {
1115                         logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
1116                         return;
1117                 }
1118                 database.removePostReply(reply);
1119                 markReplyKnown(reply);
1120                 sone.removeReply(reply);
1121                 touchConfiguration();
1122         }
1123
1124         /**
1125          * Marks the given reply as known, if it is currently not a known reply
1126          * (according to {@link Reply#isKnown()}).
1127          *
1128          * @param reply
1129          *            The reply to mark as known
1130          */
1131         public void markReplyKnown(PostReply reply) {
1132                 boolean previouslyKnown = reply.isKnown();
1133                 reply.setKnown(true);
1134                 eventBus.post(new MarkPostReplyKnownEvent(reply));
1135                 if (!previouslyKnown) {
1136                         touchConfiguration();
1137                 }
1138         }
1139
1140         /**
1141          * Creates a new album for the given Sone.
1142          *
1143          * @param sone
1144          *            The Sone to create the album for
1145          * @param parent
1146          *            The parent of the album (may be {@code null} to create a
1147          *            top-level album)
1148          * @return The new album
1149          */
1150         public Album createAlbum(Sone sone, Album parent) {
1151                 Album album = database.newAlbumBuilder().randomId().by(sone).build();
1152                 database.storeAlbum(album);
1153                 parent.addAlbum(album);
1154                 return album;
1155         }
1156
1157         /**
1158          * Deletes the given album. The owner of the album has to be a local Sone,
1159          * and the album has to be {@link Album#isEmpty() empty} to be deleted.
1160          *
1161          * @param album
1162          *            The album to remove
1163          */
1164         public void deleteAlbum(Album album) {
1165                 checkNotNull(album, "album must not be null");
1166                 checkArgument(album.getSone().isLocal(), "album’s Sone must be a local Sone");
1167                 if (!album.isEmpty()) {
1168                         return;
1169                 }
1170                 album.getParent().removeAlbum(album);
1171                 database.removeAlbum(album);
1172                 touchConfiguration();
1173         }
1174
1175         /**
1176          * Creates a new image.
1177          *
1178          * @param sone
1179          *            The Sone creating the image
1180          * @param album
1181          *            The album the image will be inserted into
1182          * @param temporaryImage
1183          *            The temporary image to create the image from
1184          * @return The newly created image
1185          */
1186         public Image createImage(Sone sone, Album album, TemporaryImage temporaryImage) {
1187                 checkNotNull(sone, "sone must not be null");
1188                 checkNotNull(album, "album must not be null");
1189                 checkNotNull(temporaryImage, "temporaryImage must not be null");
1190                 checkArgument(sone.isLocal(), "sone must be a local Sone");
1191                 checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
1192                 Image image = database.newImageBuilder().withId(temporaryImage.getId()).build().modify().setSone(sone).setCreationTime(System.currentTimeMillis()).update();
1193                 album.addImage(image);
1194                 database.storeImage(image);
1195                 imageInserter.insertImage(temporaryImage, image);
1196                 return image;
1197         }
1198
1199         /**
1200          * Deletes the given image. This method will also delete a matching
1201          * temporary image.
1202          *
1203          * @see #deleteTemporaryImage(String)
1204          * @param image
1205          *            The image to delete
1206          */
1207         public void deleteImage(Image image) {
1208                 checkNotNull(image, "image must not be null");
1209                 checkArgument(image.getSone().isLocal(), "image must belong to a local Sone");
1210                 deleteTemporaryImage(image.getId());
1211                 image.getAlbum().removeImage(image);
1212                 database.removeImage(image);
1213                 touchConfiguration();
1214         }
1215
1216         /**
1217          * Creates a new temporary image.
1218          *
1219          * @param mimeType
1220          *            The MIME type of the temporary image
1221          * @param imageData
1222          *            The encoded data of the image
1223          * @return The temporary image
1224          */
1225         public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData) {
1226                 TemporaryImage temporaryImage = new TemporaryImage();
1227                 temporaryImage.setMimeType(mimeType).setImageData(imageData);
1228                 synchronized (temporaryImages) {
1229                         temporaryImages.put(temporaryImage.getId(), temporaryImage);
1230                 }
1231                 return temporaryImage;
1232         }
1233
1234         /**
1235          * Deletes the temporary image with the given ID.
1236          *
1237          * @param imageId
1238          *            The ID of the temporary image to delete
1239          */
1240         public void deleteTemporaryImage(String imageId) {
1241                 checkNotNull(imageId, "imageId must not be null");
1242                 synchronized (temporaryImages) {
1243                         temporaryImages.remove(imageId);
1244                 }
1245                 Image image = getImage(imageId, false);
1246                 if (image != null) {
1247                         imageInserter.cancelImageInsert(image);
1248                 }
1249         }
1250
1251         /**
1252          * Notifies the core that the configuration, either of the core or of a
1253          * single local Sone, has changed, and that the configuration should be
1254          * saved.
1255          */
1256         public void touchConfiguration() {
1257                 lastConfigurationUpdate = System.currentTimeMillis();
1258         }
1259
1260         //
1261         // SERVICE METHODS
1262         //
1263
1264         /**
1265          * Starts the core.
1266          */
1267         @Override
1268         public void serviceStart() {
1269                 loadConfiguration();
1270                 updateChecker.start();
1271                 identityManager.start();
1272                 webOfTrustUpdater.init();
1273                 webOfTrustUpdater.start();
1274                 database.startAsync();
1275         }
1276
1277         /**
1278          * {@inheritDoc}
1279          */
1280         @Override
1281         public void serviceRun() {
1282                 long lastSaved = System.currentTimeMillis();
1283                 while (!shouldStop()) {
1284                         sleep(1000);
1285                         long now = System.currentTimeMillis();
1286                         if (shouldStop() || ((lastConfigurationUpdate > lastSaved) && ((now - lastConfigurationUpdate) > 5000))) {
1287                                 for (Sone localSone : getLocalSones()) {
1288                                         saveSone(localSone);
1289                                 }
1290                                 saveConfiguration();
1291                                 lastSaved = now;
1292                         }
1293                 }
1294         }
1295
1296         /**
1297          * Stops the core.
1298          */
1299         @Override
1300         public void serviceStop() {
1301                 localElementTicker.shutdownNow();
1302                 synchronized (soneInserters) {
1303                         for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
1304                                 soneInserter.getValue().stop();
1305                                 Sone latestSone = getLocalSone(soneInserter.getKey().getId());
1306                                 saveSone(latestSone);
1307                         }
1308                 }
1309                 synchronized (soneRescuers) {
1310                         for (SoneRescuer soneRescuer : soneRescuers.values()) {
1311                                 soneRescuer.stop();
1312                         }
1313                 }
1314                 saveConfiguration();
1315                 database.stopAsync();
1316                 webOfTrustUpdater.stop();
1317                 updateChecker.stop();
1318                 soneDownloader.stop();
1319                 soneDownloaders.shutdown();
1320                 identityManager.stop();
1321         }
1322
1323         //
1324         // PRIVATE METHODS
1325         //
1326
1327         /**
1328          * Saves the given Sone. This will persist all local settings for the given
1329          * Sone, such as the friends list and similar, private options.
1330          *
1331          * @param sone
1332          *            The Sone to save
1333          */
1334         private synchronized void saveSone(Sone sone) {
1335                 if (!sone.isLocal()) {
1336                         logger.log(Level.FINE, String.format("Tried to save non-local Sone: %s", sone));
1337                         return;
1338                 }
1339                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1340                         logger.log(Level.WARNING, String.format("Local Sone without OwnIdentity found, refusing to save: %s", sone));
1341                         return;
1342                 }
1343
1344                 logger.log(Level.INFO, String.format("Saving Sone: %s", sone));
1345                 try {
1346                         /* save Sone into configuration. */
1347                         String sonePrefix = "Sone/" + sone.getId();
1348                         configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1349                         configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1350
1351                         /* save profile. */
1352                         Profile profile = sone.getProfile();
1353                         configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1354                         configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1355                         configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1356                         configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1357                         configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1358                         configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1359                         configuration.getStringValue(sonePrefix + "/Profile/Avatar").setValue(profile.getAvatar());
1360
1361                         /* save profile fields. */
1362                         int fieldCounter = 0;
1363                         for (Field profileField : profile.getFields()) {
1364                                 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1365                                 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1366                                 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1367                         }
1368                         configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1369
1370                         /* save posts. */
1371                         int postCounter = 0;
1372                         for (Post post : sone.getPosts()) {
1373                                 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1374                                 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1375                                 configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
1376                                 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1377                                 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1378                         }
1379                         configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1380
1381                         /* save replies. */
1382                         int replyCounter = 0;
1383                         for (PostReply reply : sone.getReplies()) {
1384                                 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1385                                 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1386                                 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
1387                                 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1388                                 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1389                         }
1390                         configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1391
1392                         /* save post likes. */
1393                         int postLikeCounter = 0;
1394                         for (String postId : sone.getLikedPostIds()) {
1395                                 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1396                         }
1397                         configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1398
1399                         /* save reply likes. */
1400                         int replyLikeCounter = 0;
1401                         for (String replyId : sone.getLikedReplyIds()) {
1402                                 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1403                         }
1404                         configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1405
1406                         /* save albums. first, collect in a flat structure, top-level first. */
1407                         List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
1408
1409                         int albumCounter = 0;
1410                         for (Album album : albums) {
1411                                 String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
1412                                 configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
1413                                 configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
1414                                 configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
1415                                 configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
1416                         }
1417                         configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
1418
1419                         /* save images. */
1420                         int imageCounter = 0;
1421                         for (Album album : albums) {
1422                                 for (Image image : album.getImages()) {
1423                                         if (!image.isInserted()) {
1424                                                 continue;
1425                                         }
1426                                         String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
1427                                         configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
1428                                         configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
1429                                         configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
1430                                         configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
1431                                         configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());
1432                                         configuration.getLongValue(imagePrefix + "/CreationTime").setValue(image.getCreationTime());
1433                                         configuration.getIntValue(imagePrefix + "/Width").setValue(image.getWidth());
1434                                         configuration.getIntValue(imagePrefix + "/Height").setValue(image.getHeight());
1435                                 }
1436                         }
1437                         configuration.getStringValue(sonePrefix + "/Images/" + imageCounter + "/ID").setValue(null);
1438
1439                         /* save options. */
1440                         configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().isAutoFollow());
1441                         configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().isSoneInsertNotificationEnabled());
1442                         configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications());
1443                         configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
1444                         configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
1445                         configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
1446                         configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").setValue(sone.getOptions().getLoadLinkedImages().name());
1447
1448                         webOfTrustUpdater.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1449
1450                         logger.log(Level.INFO, String.format("Sone %s saved.", sone));
1451                 } catch (ConfigurationException ce1) {
1452                         logger.log(Level.WARNING, String.format("Could not save Sone: %s", sone), ce1);
1453                 }
1454         }
1455
1456         /**
1457          * Saves the current options.
1458          */
1459         private void saveConfiguration() {
1460                 synchronized (configuration) {
1461                         if (storingConfiguration) {
1462                                 logger.log(Level.FINE, "Already storing configuration…");
1463                                 return;
1464                         }
1465                         storingConfiguration = true;
1466                 }
1467
1468                 /* store the options first. */
1469                 try {
1470                         preferences.saveTo(configuration);
1471
1472                         /* save known Sones. */
1473                         int soneCounter = 0;
1474                         synchronized (knownSones) {
1475                                 for (String knownSoneId : knownSones) {
1476                                         configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1477                                 }
1478                                 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1479                         }
1480
1481                         /* save known posts. */
1482                         database.save();
1483
1484                         /* now save it. */
1485                         Stopwatch stopwatch = Stopwatch.createStarted();
1486                         configuration.save();
1487                         configurationSaveTimeHistogram.update(stopwatch.elapsed(TimeUnit.MICROSECONDS));
1488
1489                 } catch (ConfigurationException ce1) {
1490                         logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1491                 } catch (DatabaseException de1) {
1492                         logger.log(Level.SEVERE, "Could not save database!", de1);
1493                 } finally {
1494                         synchronized (configuration) {
1495                                 storingConfiguration = false;
1496                         }
1497                 }
1498         }
1499
1500         /**
1501          * Loads the configuration.
1502          */
1503         private void loadConfiguration() {
1504                 new PreferencesLoader(preferences).loadFrom(configuration);
1505
1506                 /* load known Sones. */
1507                 int soneCounter = 0;
1508                 while (true) {
1509                         String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1510                         if (knownSoneId == null) {
1511                                 break;
1512                         }
1513                         synchronized (knownSones) {
1514                                 knownSones.add(knownSoneId);
1515                         }
1516                 }
1517         }
1518
1519         /**
1520          * Notifies the core that a new {@link OwnIdentity} was added.
1521          *
1522          * @param ownIdentityAddedEvent
1523          *            The event
1524          */
1525         @Subscribe
1526         public void ownIdentityAdded(OwnIdentityAddedEvent ownIdentityAddedEvent) {
1527                 OwnIdentity ownIdentity = ownIdentityAddedEvent.getOwnIdentity();
1528                 logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity));
1529                 if (ownIdentity.hasContext("Sone")) {
1530                         addLocalSone(ownIdentity);
1531                 }
1532         }
1533
1534         /**
1535          * Notifies the core that an {@link OwnIdentity} was removed.
1536          *
1537          * @param ownIdentityRemovedEvent
1538          *            The event
1539          */
1540         @Subscribe
1541         public void ownIdentityRemoved(OwnIdentityRemovedEvent ownIdentityRemovedEvent) {
1542                 OwnIdentity ownIdentity = ownIdentityRemovedEvent.getOwnIdentity();
1543                 logger.log(Level.FINEST, String.format("Removing OwnIdentity: %s", ownIdentity));
1544                 trustedIdentities.removeAll(ownIdentity);
1545         }
1546
1547         /**
1548          * Notifies the core that a new {@link Identity} was added.
1549          *
1550          * @param identityAddedEvent
1551          *            The event
1552          */
1553         @Subscribe
1554         public void identityAdded(IdentityAddedEvent identityAddedEvent) {
1555                 Identity identity = identityAddedEvent.getIdentity();
1556                 logger.log(Level.FINEST, String.format("Adding Identity: %s", identity));
1557                 trustedIdentities.put(identityAddedEvent.getOwnIdentity(), identity);
1558                 addRemoteSone(identity);
1559         }
1560
1561         /**
1562          * Notifies the core that an {@link Identity} was updated.
1563          *
1564          * @param identityUpdatedEvent
1565          *            The event
1566          */
1567         @Subscribe
1568         public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
1569                 Identity identity = identityUpdatedEvent.getIdentity();
1570                 final Sone sone = getRemoteSone(identity.getId());
1571                 if (sone.isLocal()) {
1572                         return;
1573                 }
1574                 String newLatestEdition = identity.getProperty("Sone.LatestEdition");
1575                 if (newLatestEdition != null) {
1576                         Long parsedNewLatestEdition = tryParse(newLatestEdition);
1577                         if (parsedNewLatestEdition != null) {
1578                                 sone.setLatestEdition(parsedNewLatestEdition);
1579                         }
1580                 }
1581                 soneDownloader.addSone(sone);
1582                 soneDownloaders.execute(soneDownloader.fetchSoneAsSskAction(sone));
1583         }
1584
1585         /**
1586          * Notifies the core that an {@link Identity} was removed.
1587          *
1588          * @param identityRemovedEvent
1589          *            The event
1590          */
1591         @Subscribe
1592         public void identityRemoved(IdentityRemovedEvent identityRemovedEvent) {
1593                 OwnIdentity ownIdentity = identityRemovedEvent.getOwnIdentity();
1594                 Identity identity = identityRemovedEvent.getIdentity();
1595                 trustedIdentities.remove(ownIdentity, identity);
1596                 for (Entry<OwnIdentity, Collection<Identity>> trustedIdentity : trustedIdentities.asMap().entrySet()) {
1597                         if (trustedIdentity.getKey().equals(ownIdentity)) {
1598                                 continue;
1599                         }
1600                         if (trustedIdentity.getValue().contains(identity)) {
1601                                 return;
1602                         }
1603                 }
1604                 Sone sone = getSone(identity.getId());
1605                 if (sone == null) {
1606                         /* TODO - we don’t have the Sone anymore. should this happen? */
1607                         return;
1608                 }
1609                 for (PostReply postReply : sone.getReplies()) {
1610                         eventBus.post(new PostReplyRemovedEvent(postReply));
1611                 }
1612                 for (Post post : sone.getPosts()) {
1613                         eventBus.post(new PostRemovedEvent(post));
1614                 }
1615                 eventBus.post(new SoneRemovedEvent(sone));
1616                 database.removeSone(sone);
1617         }
1618
1619         /**
1620          * Deletes the temporary image.
1621          *
1622          * @param imageInsertFinishedEvent
1623          *            The event
1624          */
1625         @Subscribe
1626         public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1627                 logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.getImage(), imageInsertFinishedEvent.getResultingUri()));
1628                 imageInsertFinishedEvent.getImage().modify().setKey(imageInsertFinishedEvent.getResultingUri().toString()).update();
1629                 deleteTemporaryImage(imageInsertFinishedEvent.getImage().getId());
1630                 touchConfiguration();
1631         }
1632
1633         @VisibleForTesting
1634         class MarkPostKnown implements Runnable {
1635
1636                 private final Post post;
1637
1638                 public MarkPostKnown(Post post) {
1639                         this.post = post;
1640                 }
1641
1642                 @Override
1643                 public void run() {
1644                         markPostKnown(post);
1645                 }
1646
1647         }
1648
1649         @VisibleForTesting
1650         class MarkReplyKnown implements Runnable {
1651
1652                 private final PostReply postReply;
1653
1654                 public MarkReplyKnown(PostReply postReply) {
1655                         this.postReply = postReply;
1656                 }
1657
1658                 @Override
1659                 public void run() {
1660                         markReplyKnown(postReply);
1661                 }
1662
1663         }
1664
1665 }