Notify all listeners when a download failes.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 18:28:19 +0000 (20:28 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 18:28:19 +0000 (20:28 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java
src/main/java/net/pterodactylus/xdcc/core/event/DownloadFailed.java [new file with mode: 0644]

index 13e63f2..4314933 100644 (file)
@@ -36,12 +36,14 @@ import net.pterodactylus.irc.DccReceiver;
 import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelMessageReceived;
 import net.pterodactylus.irc.event.ConnectionEstablished;
+import net.pterodactylus.irc.event.DccDownloadFailed;
 import net.pterodactylus.irc.event.DccDownloadFinished;
 import net.pterodactylus.irc.event.DccSendReceived;
 import net.pterodactylus.irc.util.MessageCleaner;
 import net.pterodactylus.irc.util.RandomNickname;
 import net.pterodactylus.xdcc.core.event.BotAdded;
 import net.pterodactylus.xdcc.core.event.CoreStarted;
+import net.pterodactylus.xdcc.core.event.DownloadFailed;
 import net.pterodactylus.xdcc.core.event.DownloadFinished;
 import net.pterodactylus.xdcc.core.event.DownloadStarted;
 import net.pterodactylus.xdcc.data.Bot;
@@ -60,6 +62,7 @@ import com.google.common.collect.Sets;
 import com.google.common.collect.Table;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
+import com.google.common.io.Closeables;
 import com.google.common.util.concurrent.AbstractIdleService;
 import com.google.inject.Inject;
 
@@ -398,6 +401,28 @@ public class Core extends AbstractIdleService {
                }
        }
 
+       /**
+        * Closes the output stream and notifies all listeners of the failure.
+        *
+        * @param dccDownloadFailed
+        *              The DCC download failed event
+        */
+       @Subscribe
+       public void dccDownloadFailed(DccDownloadFailed dccDownloadFailed) {
+               Download download = downloads.get(dccDownloadFailed.dccReceiver().filename());
+               if (download == null) {
+                       /* probably shouldn’t happen. */
+                       return;
+               }
+
+               try {
+                       Closeables.close(download.outputStream(), true);
+                       eventBus.post(new DownloadFailed(download));
+               } catch (IOException ioe1) {
+                       /* swallow silently. */
+               }
+       }
+
        //
        // PRIVATE METHODS
        //
diff --git a/src/main/java/net/pterodactylus/xdcc/core/event/DownloadFailed.java b/src/main/java/net/pterodactylus/xdcc/core/event/DownloadFailed.java
new file mode 100644 (file)
index 0000000..b2352f5
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * XdccDownloader - DownloadFailed.java - Copyright © 2013 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.xdcc.core.event;
+
+import net.pterodactylus.xdcc.data.Download;
+
+/**
+ * Notifies a listener that a {@link Download} has failed.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DownloadFailed extends AbstractDownloadEvent {
+
+       /**
+        * Creates a new download finished event.
+        *
+        * @param download
+        *              The download that finished
+        */
+       public DownloadFailed(Download download) {
+               super(download);
+       }
+
+}