- }
- }
-
- /**
- * Bookmarks the given post.
- *
- * @param post
- * The post to bookmark
- */
- public void bookmark(Post post) {
- bookmarkPost(post.getId());
- }
-
- /**
- * Bookmarks the post with the given ID.
- *
- * @param id
- * The ID of the post to bookmark
- */
- public void bookmarkPost(String id) {
- synchronized (bookmarkedPosts) {
- bookmarkedPosts.add(id);
- }
- }
-
- /**
- * Removes the given post from the bookmarks.
- *
- * @param post
- * The post to unbookmark
- */
- public void unbookmark(Post post) {
- unbookmarkPost(post.getId());
- }
-
- /**
- * Removes the post with the given ID from the bookmarks.
- *
- * @param id
- * The ID of the post to unbookmark
- */
- public void unbookmarkPost(String id) {
- synchronized (bookmarkedPosts) {
- bookmarkedPosts.remove(id);
- }
- }
-
- /**
- * Creates a new reply.
- *
- * @param sone
- * The Sone that creates the reply
- * @param post
- * The post that this reply refers to
- * @param text
- * The text of the reply
- * @return The created reply
- */
- public Reply createReply(Sone sone, Post post, String text) {
- return createReply(sone, post, System.currentTimeMillis(), text);
- }
-
- /**
- * Creates a new reply.
- *
- * @param sone
- * The Sone that creates the reply
- * @param post
- * The post that this reply refers to
- * @param time
- * The time of the reply
- * @param text
- * The text of the reply
- * @return The created reply
- */
- public Reply createReply(Sone sone, Post post, long time, String text) {
- if (!isLocalSone(sone)) {
- logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
- return null;
- }
- Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
- synchronized (replies) {
- replies.put(reply.getId(), reply);
- }
- synchronized (newReplies) {
- newReplies.add(reply.getId());
- coreListenerManager.fireNewReplyFound(reply);
- }
- sone.addReply(reply);
- saveSone(sone);
- return reply;
- }
-
- /**
- * Deletes the given reply.
- *
- * @param reply
- * The reply to delete
- */
- public void deleteReply(Reply reply) {
- Sone sone = reply.getSone();
- if (!isLocalSone(sone)) {
- logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
- return;
- }
- synchronized (replies) {
- replies.remove(reply.getId());
- }
- synchronized (newReplies) {
- markReplyKnown(reply);
- knownReplies.remove(reply.getId());
- }
- sone.removeReply(reply);
- saveSone(sone);
- }
-
- /**
- * Marks the given reply as known, if it is currently a new reply (according
- * to {@link #isNewReply(String)}).
- *
- * @param reply
- * The reply to mark as known
- */
- public void markReplyKnown(Reply reply) {
- synchronized (newReplies) {
- if (newReplies.remove(reply.getId())) {
- knownReplies.add(reply.getId());
- coreListenerManager.fireMarkReplyKnown(reply);
- saveConfiguration();
- }
- }
- }
-
- /**
- * Creates a new top-level album for the given Sone.
- *
- * @param sone
- * The Sone to create the album for
- * @return The new album
- */
- public Album createAlbum(Sone sone) {
- return createAlbum(sone, null);
- }
-
- /**
- * Creates a new album for the given Sone.
- *
- * @param sone
- * The Sone to create the album for
- * @param parent
- * The parent of the album (may be {@code null} to create a
- * top-level album)
- * @return The new album
- */
- public Album createAlbum(Sone sone, Album parent) {
- Album album = new Album();
- synchronized (albums) {
- albums.put(album.getId(), album);
- }
- album.setSone(sone);
- if (parent != null) {
- parent.addAlbum(album);
- } else {
- sone.addAlbum(album);
- }
- return album;
- }
-
- /**
- * Deletes the given album. The owner of the album has to be a local Sone,
- * and the album has to be {@link Album#isEmpty() empty} to be deleted.
- *
- * @param album
- * The album to remove
- */
- public void deleteAlbum(Album album) {
- Validation.begin().isNotNull("Album", album).check().is("Local Sone", isLocalSone(album.getSone())).check();
- if (!album.isEmpty()) {
- return;
- }
- if (album.getParent() == null) {
- album.getSone().removeAlbum(album);
- } else {
- album.getParent().removeAlbum(album);
- }
- synchronized (albums) {
- albums.remove(album.getId());
- }
- saveSone(album.getSone());
- }
-
- /**
- * Creates a new image.
- *
- * @param sone
- * The Sone creating the image
- * @param album
- * The album the image will be inserted into
- * @param temporaryImage
- * The temporary image to create the image from
- * @return The newly created image
- */
- public Image createImage(Sone sone, Album album, TemporaryImage temporaryImage) {
- Validation.begin().isNotNull("Sone", sone).isNotNull("Album", album).isNotNull("Temporary Image", temporaryImage).check().is("Local Sone", isLocalSone(sone)).check().isEqual("Owner and Album Owner", sone, album.getSone()).check();
- Image image = new Image(temporaryImage.getId()).setSone(sone).setCreationTime(System.currentTimeMillis());
- album.addImage(image);
- synchronized (images) {
- images.put(image.getId(), image);
- }
- imageInserter.insertImage(temporaryImage, image);
- return image;
- }
-
- /**
- * Deletes the given image. This method will also delete a matching
- * temporary image.
- *
- * @see #deleteTemporaryImage(TemporaryImage)
- * @param image
- * The image to delete
- */
- public void deleteImage(Image image) {
- Validation.begin().isNotNull("Image", image).check().is("Local Sone", isLocalSone(image.getSone())).check();
- deleteTemporaryImage(image.getId());
- image.getAlbum().removeImage(image);
- synchronized (images) {
- images.remove(image.getId());
- }
- saveSone(image.getSone());
- }
-
- /**
- * Creates a new temporary image.
- *
- * @param mimeType
- * The MIME type of the temporary image
- * @param imageData
- * The encoded data of the image
- * @return The temporary image
- */
- public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData) {
- TemporaryImage temporaryImage = new TemporaryImage();
- temporaryImage.setMimeType(mimeType).setImageData(imageData);
- synchronized (temporaryImages) {
- temporaryImages.put(temporaryImage.getId(), temporaryImage);
- }
- return temporaryImage;
- }
-
- /**
- * Deletes the given temporary image.
- *
- * @param temporaryImage
- * The temporary image to delete
- */
- public void deleteTemporaryImage(TemporaryImage temporaryImage) {
- Validation.begin().isNotNull("Temporary Image", temporaryImage).check();
- deleteTemporaryImage(temporaryImage.getId());
- }
-
- /**
- * Deletes the temporary image with the given ID.
- *
- * @param imageId
- * The ID of the temporary image to delete
- */
- public void deleteTemporaryImage(String imageId) {
- Validation.begin().isNotNull("Temporary Image ID", imageId).check();
- synchronized (temporaryImages) {
- temporaryImages.remove(imageId);
- }
- Image image = getImage(imageId, false);
- if (image != null) {
- imageInserter.cancelImageInsert(image);
- }
- }
-
- /**
- * Starts the core.
- */
- public void start() {
- loadConfiguration();
- updateChecker.addUpdateListener(this);
- updateChecker.start();
- }
-
- /**
- * Stops the core.
- */
- public void stop() {
- synchronized (localSones) {
- for (SoneInserter soneInserter : soneInserters.values()) {
- soneInserter.stop();
- }
- }
- updateChecker.stop();
- updateChecker.removeUpdateListener(this);
- soneDownloader.stop();
- saveConfiguration();
- stopped = true;
- }
-
- /**
- * Saves the current options.
- */
- public void saveConfiguration() {
- synchronized (configuration) {
- if (storingConfiguration) {
- logger.log(Level.FINE, "Already storing configuration…");
- return;
- }
- storingConfiguration = true;