Add possibility to cancel a running download.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 8 Aug 2013 00:22:20 +0000 (02:22 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 8 Aug 2013 00:22:20 +0000 (02:22 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/CommandReader.java

index d02e679..e2710e4 100644 (file)
@@ -261,6 +261,43 @@ public class Core extends AbstractExecutionThreadService {
        }
 
        /**
+        * Cancels the download of the given pack from the given bot.
+        *
+        * @param bot
+        *              The bot the pack is being downloaded from
+        * @param pack
+        *              The pack being downloaded
+        */
+       public void cancelDownload(Bot bot, Pack pack) {
+               Optional<Download> download = getDownload(pack, bot);
+               if (!download.isPresent()) {
+                       return;
+               }
+
+               /* get connection. */
+               Connection connection = networkConnections.get(bot.network());
+               if (connection == null) {
+                       /* request for unknown network? */
+                       return;
+               }
+
+               /* remove download. */
+               downloads.remove(pack.name(), download.get());
+
+               /* stop the DCC receiver. */
+               if (download.get().dccReceiver() != null) {
+                       download.get().dccReceiver().stop();
+               }
+
+               /* remove the request from the bot, too. */
+               try {
+                       connection.sendMessage(bot.name(), String.format("XDCC %s", (download.get().dccReceiver() != null) ? "CANCEL" : "REMOVE"));
+               } catch (IOException ioe1) {
+                       logger.log(Level.WARNING, String.format("Could not cancel DCC from %s (%s)!", bot.name(), bot.network().name()), ioe1);
+               }
+       }
+
+       /**
         * Closes the given connection.
         *
         * @param connection
@@ -829,6 +866,28 @@ public class Core extends AbstractExecutionThreadService {
        //
 
        /**
+        * Returns the download of the given pack from the given bot.
+        *
+        * @param pack
+        *              The pack being downloaded
+        * @param bot
+        *              The bot the pack is being downloaded from
+        * @return The download, or {@link Optional#absent()} if the download could not
+        *         be found
+        */
+       private Optional<Download> getDownload(Pack pack, Bot bot) {
+               if (!downloads.containsKey(pack.name())) {
+                       return Optional.absent();
+               }
+               for (Download download : Lists.newArrayList(downloads.get(pack.name()))) {
+                       if (download.bot().equals(bot)) {
+                               return Optional.of(download);
+                       }
+               }
+               return Optional.absent();
+       }
+
+       /**
         * Searches all current connections for the given connection, returning the
         * associated network.
         *
index 26696f9..3714956 100644 (file)
@@ -155,6 +155,11 @@ public class CommandReader extends AbstractExecutionThreadService {
                                if ((index != null) && (index < lastResult.size())) {
                                        core.fetch(lastResult.get(index).bot(), lastResult.get(index).pack());
                                }
+                       } else if (words[0].equalsIgnoreCase("cancel")) {
+                               Integer index = Ints.tryParse(words[1]);
+                               if ((index != null) && (index < lastResult.size())) {
+                                       core.cancelDownload(lastResult.get(index).bot(), lastResult.get(index).pack());
+                               }
                        } else if (words[0].equalsIgnoreCase("stats")) {
                                int configuredChannelsCount = core.channels().size();
                                int joinedChannelsCount = core.joinedChannels().size();