import net.pterodactylus.sone.core.Options.DefaultOption;
import net.pterodactylus.sone.core.Options.Option;
import net.pterodactylus.sone.core.Options.OptionWatcher;
-import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
-import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
-import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import freenet.keys.FreenetURI;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener, ImageInsertListener {
+public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener {
/** The logger. */
private static final Logger logger = Logging.getLogger(Core.class);
this.freenetInterface = freenetInterface;
this.identityManager = identityManager;
this.soneDownloader = new SoneDownloader(this, freenetInterface);
- this.imageInserter = new ImageInserter(this, freenetInterface);
+ this.imageInserter = new ImageInserter(freenetInterface);
this.updateChecker = new UpdateChecker(freenetInterface);
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
eventBus.post(new SoneInsertAbortedEvent(sone, cause));
}
- //
- // SONEINSERTLISTENER METHODS
- //
-
/**
- * {@inheritDoc}
- */
- @Override
- public void imageInsertStarted(Image image) {
- logger.log(Level.WARNING, String.format("Image insert started for %s...", image));
- eventBus.post(new ImageInsertStartedEvent(image));
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void imageInsertAborted(Image image) {
- logger.log(Level.WARNING, String.format("Image insert aborted for %s.", image));
- eventBus.post(new ImageInsertAbortedEvent(image));
- }
-
- /**
- * {@inheritDoc}
+ * Deletes the temporary image.
+ *
+ * @param imageInsertFinishedEvent
+ * The event
*/
- @Override
- public void imageInsertFinished(Image image, FreenetURI key) {
- logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", image, key));
- image.setKey(key.toString());
- deleteTemporaryImage(image.getId());
+ @Subscribe
+ public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
+ logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.image(), imageInsertFinishedEvent.resultingUri()));
+ imageInsertFinishedEvent.image().setKey(imageInsertFinishedEvent.resultingUri().toString());
+ deleteTemporaryImage(imageInsertFinishedEvent.image().getId());
touchConfiguration();
- eventBus.post(new ImageInsertFinishedEvent(image));
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void imageInsertFailed(Image image, Throwable cause) {
- logger.log(Level.WARNING, String.format("Image insert failed for %s." + image), cause);
- eventBus.post(new ImageInsertFailedEvent(image, cause));
}
/**
package net.pterodactylus.sone.core;
import java.net.MalformedURLException;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
+import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
+import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
+import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
+import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.util.logging.Logging;
import com.db4o.ObjectContainer;
+import com.google.common.eventbus.EventBus;
import com.google.inject.Inject;
import freenet.client.ClientMetadata;
/** The logger. */
private static final Logger logger = Logging.getLogger(FreenetInterface.class);
+ /** The event bus. */
+ private final EventBus eventBus;
+
/** The node to interact with. */
private final Node node;
/**
* Creates a new Freenet interface.
*
+ * @param eventBus
+ * The event bus
* @param node
* The node to interact with
*/
@Inject
- public FreenetInterface(Node node) {
+ public FreenetInterface(EventBus eventBus, Node node) {
+ this.eventBus = eventBus;
this.node = node;
this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
}
}
/**
- * Insert token that can be used to add {@link ImageInsertListener}s and
- * cancel a running insert.
+ * Insert token that can cancel a running insert and sends events.
*
+ * @see ImageInsertAbortedEvent
+ * @see ImageInsertStartedEvent
+ * @see ImageInsertFailedEvent
+ * @see ImageInsertFinishedEvent
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class InsertToken implements ClientPutCallback {
/** The image being inserted. */
private final Image image;
- /** The list of registered image insert listeners. */
- private final List<ImageInsertListener> imageInsertListeners = Collections.synchronizedList(new ArrayList<ImageInsertListener>());
-
/** The client putter. */
private ClientPutter clientPutter;
}
//
- // LISTENER MANAGEMENT
- //
-
- /**
- * Adds the given listener to the list of registered listener.
- *
- * @param imageInsertListener
- * The listener to add
- */
- public void addImageInsertListener(ImageInsertListener imageInsertListener) {
- imageInsertListeners.add(imageInsertListener);
- }
-
- /**
- * Removes the given listener from the list of registered listener.
- *
- * @param imageInsertListener
- * The listener to remove
- */
- public void removeImageInsertListener(ImageInsertListener imageInsertListener) {
- imageInsertListeners.remove(imageInsertListener);
- }
-
- //
// ACCESSORS
//
* Sets the client putter that is inserting the image. This will also
* signal all registered listeners that the image has started.
*
- * @see ImageInsertListener#imageInsertStarted(Image)
* @param clientPutter
* The client putter
*/
+ @SuppressWarnings("synthetic-access")
public void setClientPutter(ClientPutter clientPutter) {
this.clientPutter = clientPutter;
- for (ImageInsertListener imageInsertListener : imageInsertListeners) {
- imageInsertListener.imageInsertStarted(image);
- }
+ eventBus.post(new ImageInsertStartedEvent(image));
}
//
/**
* Cancels the running insert.
- *
- * @see ImageInsertListener#imageInsertAborted(Image)
*/
@SuppressWarnings("synthetic-access")
public void cancel() {
clientPutter.cancel(null, node.clientCore.clientContext);
- for (ImageInsertListener imageInsertListener : imageInsertListeners) {
- imageInsertListener.imageInsertAborted(image);
- }
+ eventBus.post(new ImageInsertAbortedEvent(image));
}
//
* {@inheritDoc}
*/
@Override
+ @SuppressWarnings("synthetic-access")
public void onFailure(InsertException insertException, BaseClientPutter clientPutter, ObjectContainer objectContainer) {
- for (ImageInsertListener imageInsertListener : imageInsertListeners) {
- if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
- imageInsertListener.imageInsertAborted(image);
- } else {
- imageInsertListener.imageInsertFailed(image, insertException);
- }
+ if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
+ eventBus.post(new ImageInsertAbortedEvent(image));
+ } else {
+ eventBus.post(new ImageInsertFailedEvent(image, insertException));
}
}
* {@inheritDoc}
*/
@Override
+ @SuppressWarnings("synthetic-access")
public void onSuccess(BaseClientPutter clientPutter, ObjectContainer objectContainer) {
- for (ImageInsertListener imageInsertListener : imageInsertListeners) {
- imageInsertListener.imageInsertFinished(image, resultingUri);
- }
+ eventBus.post(new ImageInsertFinishedEvent(image, resultingUri));
}
}
+++ /dev/null
-/*
- * Sone - ImageInsertListener.java - Copyright © 2011–2012 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.core;
-
-import java.util.EventListener;
-
-import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
-import net.pterodactylus.sone.data.Image;
-import freenet.keys.FreenetURI;
-
-/**
- * Listener interface for objects that want to be notified about the status of
- * an image insert.
- *
- * @see ImageInserter#insertImage(net.pterodactylus.sone.data.TemporaryImage,
- * Image)
- * @see FreenetInterface#insertImage(net.pterodactylus.sone.data.TemporaryImage,
- * Image, net.pterodactylus.sone.core.FreenetInterface.InsertToken)
- * @see InsertToken
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public interface ImageInsertListener extends EventListener {
-
- /**
- * Notifies a listener that the insert of the given image started.
- *
- * @param image
- * The image that is being inserted
- */
- public void imageInsertStarted(Image image);
-
- /**
- * Notifies a listener that the insert of the given image was aborted by the
- * user.
- *
- * @param image
- * The image that is no longer being inserted
- */
- public void imageInsertAborted(Image image);
-
- /**
- * Notifies a listener that the given image was inserted successfully.
- *
- * @param image
- * The image that was inserted
- * @param key
- * The final key of the image
- */
- public void imageInsertFinished(Image image, FreenetURI key);
-
- /**
- * Notifies a listener that the given image could not be inserted.
- *
- * @param image
- * The image that could not be inserted
- * @param cause
- * The cause of the insertion failure
- */
- public void imageInsertFailed(Image image, Throwable cause);
-
-}
/** The logger. */
private static final Logger logger = Logging.getLogger(ImageInserter.class);
- /** The core. */
- private final Core core;
-
/** The freenet interface. */
private final FreenetInterface freenetInterface;
/**
* Creates a new image inserter.
*
- * @param core
- * The Sone core
* @param freenetInterface
* The freenet interface
*/
- public ImageInserter(Core core, FreenetInterface freenetInterface) {
- this.core = core;
+ public ImageInserter(FreenetInterface freenetInterface) {
this.freenetInterface = freenetInterface;
}
/**
- * Inserts the given image. The {@link #core} will automatically added as
- * {@link ImageInsertListener} to the created {@link InsertToken}.
+ * Inserts the given image.
*
* @param temporaryImage
* The temporary image data
try {
InsertToken insertToken = freenetInterface.new InsertToken(image);
insertTokens.put(image.getId(), insertToken);
- insertToken.addImageInsertListener(core);
freenetInterface.insertImage(temporaryImage, image, insertToken);
} catch (SoneException se1) {
logger.log(Level.WARNING, "Could not insert image!", se1);
return;
}
insertToken.cancel();
- insertToken.removeImageInsertListener(core);
}
}
package net.pterodactylus.sone.core.event;
import net.pterodactylus.sone.data.Image;
+import freenet.keys.FreenetURI;
/**
* Event that signals that an {@link Image} insert is finished.
*/
public class ImageInsertFinishedEvent extends ImageEvent {
+ /** The URI of the image. */
+ private final FreenetURI resultingUri;
+
/**
* Creates a new “image insert finished” event.
*
* @param image
* The image whose insert finished
+ * @param resultingUri
+ * The resulting URI of the image
*/
- public ImageInsertFinishedEvent(Image image) {
+ public ImageInsertFinishedEvent(Image image, FreenetURI resultingUri) {
super(image);
+ this.resultingUri = resultingUri;
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Returns the URI of the image.
+ *
+ * @return The URI of the image
+ */
+ public FreenetURI resultingUri() {
+ return resultingUri;
}
}