Send events when downloads start and finish.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 05:34:58 +0000 (07:34 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 05:34:58 +0000 (07:34 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java
src/main/java/net/pterodactylus/xdcc/core/event/AbstractDownloadEvent.java [new file with mode: 0644]
src/main/java/net/pterodactylus/xdcc/core/event/DownloadFinished.java [new file with mode: 0644]
src/main/java/net/pterodactylus/xdcc/core/event/DownloadStarted.java [new file with mode: 0644]

index bd60a97..13e63f2 100644 (file)
@@ -42,6 +42,8 @@ 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.DownloadFinished;
+import net.pterodactylus.xdcc.core.event.DownloadStarted;
 import net.pterodactylus.xdcc.data.Bot;
 import net.pterodactylus.xdcc.data.Channel;
 import net.pterodactylus.xdcc.data.Download;
@@ -364,6 +366,7 @@ public class Core extends AbstractIdleService {
                        download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
                        dccReceivers.add(dccReceiver);
                        dccReceiver.start();
+                       eventBus.post(new DownloadStarted(download));
                } catch (FileNotFoundException fnfe1) {
                        logger.log(Level.WARNING, "Could not open file for download!", fnfe1);
                }
@@ -388,6 +391,7 @@ public class Core extends AbstractIdleService {
                        download.outputStream().close();
                        File file = new File(download.filename());
                        file.renameTo(new File(finalDirectory, download.filename()));
+                       eventBus.post(new DownloadFinished(download));
                } catch (IOException ioe1) {
                        /* TODO - handle all the errors. */
                        logger.log(Level.WARNING, String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
diff --git a/src/main/java/net/pterodactylus/xdcc/core/event/AbstractDownloadEvent.java b/src/main/java/net/pterodactylus/xdcc/core/event/AbstractDownloadEvent.java
new file mode 100644 (file)
index 0000000..1532aa9
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * XdccDownloader - DownloadEvent.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;
+
+/**
+ * Abstract base class for all {@link Download}-related events.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class AbstractDownloadEvent {
+
+       /** The download this event is about. */
+       protected final Download download;
+
+       /**
+        * Creates a new download event.
+        *
+        * @param download
+        *              The download the event is about
+        */
+       public AbstractDownloadEvent(Download download) {
+               this.download = download;
+       }
+
+       /**
+        * Returns the download this event is about.
+        *
+        * @return The download
+        */
+       public Download download() {
+               return download;
+       }
+}
diff --git a/src/main/java/net/pterodactylus/xdcc/core/event/DownloadFinished.java b/src/main/java/net/pterodactylus/xdcc/core/event/DownloadFinished.java
new file mode 100644 (file)
index 0000000..8fd9014
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * XdccDownloader - DownloadStarted.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 finished successfully.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DownloadFinished extends AbstractDownloadEvent {
+
+       /**
+        * Creates a new download finished event.
+        *
+        * @param download
+        *              The download that finished
+        */
+       public DownloadFinished(Download download) {
+               super(download);
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/xdcc/core/event/DownloadStarted.java b/src/main/java/net/pterodactylus/xdcc/core/event/DownloadStarted.java
new file mode 100644 (file)
index 0000000..415a3cd
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * XdccDownloader - DownloadStarted.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} was started.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public abstract class DownloadStarted extends AbstractDownloadEvent {
+
+       /**
+        * Creates a new download started event.
+        *
+        * @param download
+        *              The download that started
+        */
+       protected DownloadStarted(Download download) {
+               super(download);
+       }
+
+}