From bb8e361377837b7785ab7f175341f87c46bceb6a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 11 Apr 2013 07:34:58 +0200 Subject: [PATCH] Send events when downloads start and finish. --- .../java/net/pterodactylus/xdcc/core/Core.java | 4 ++ .../xdcc/core/event/AbstractDownloadEvent.java | 50 ++++++++++++++++++++++ .../xdcc/core/event/DownloadFinished.java | 39 +++++++++++++++++ .../xdcc/core/event/DownloadStarted.java | 39 +++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 src/main/java/net/pterodactylus/xdcc/core/event/AbstractDownloadEvent.java create mode 100644 src/main/java/net/pterodactylus/xdcc/core/event/DownloadFinished.java create mode 100644 src/main/java/net/pterodactylus/xdcc/core/event/DownloadStarted.java diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index bd60a97..13e63f2 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -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 index 0000000..1532aa9 --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/core/event/AbstractDownloadEvent.java @@ -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 . + */ + +package net.pterodactylus.xdcc.core.event; + +import net.pterodactylus.xdcc.data.Download; + +/** + * Abstract base class for all {@link Download}-related events. + * + * @author David ‘Bombe’ Roden + */ +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 index 0000000..8fd9014 --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/core/event/DownloadFinished.java @@ -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 . + */ + +package net.pterodactylus.xdcc.core.event; + +import net.pterodactylus.xdcc.data.Download; + +/** + * Notifies a listener that a {@link Download} has finished successfully. + * + * @author David ‘Bombe’ Roden + */ +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 index 0000000..415a3cd --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/core/event/DownloadStarted.java @@ -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 . + */ + +package net.pterodactylus.xdcc.core.event; + +import net.pterodactylus.xdcc.data.Download; + +/** + * Notifies a listener that a {@link Download} was started. + * + * @author David ‘Bombe’ Roden + */ +public abstract class DownloadStarted extends AbstractDownloadEvent { + + /** + * Creates a new download started event. + * + * @param download + * The download that started + */ + protected DownloadStarted(Download download) { + super(download); + } + +} -- 2.7.4