Allow cancelling a download even if network is gone
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index bfad867..24727d9 100644 (file)
@@ -30,6 +30,8 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.time.Duration;
+import java.time.Instant;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
@@ -110,6 +112,7 @@ public class Core extends AbstractExecutionThreadService {
        /** The logger. */
        private static final Logger logger = Logger.getLogger(Core.class.getName());
 
+       private final Instant startup = Instant.now();
        private final Object syncObject = new Object();
        /** The event bus. */
        private final EventBus eventBus;
@@ -196,8 +199,11 @@ public class Core extends AbstractExecutionThreadService {
                        Network network = entry.getKey();
                        Collection<Bot> bots = networkBots.row(network).values();
                        int packCount = bots.stream().mapToInt((bot) -> bot.packs().size()).reduce((a, b) -> a + b).orElse(0);
-                       return new ConnectedNetwork(network, entry.getValue().hostname(),
-                                       entry.getValue().port(), entry.getValue().nickname(),
+                       Connection connection = entry.getValue();
+                       return new ConnectedNetwork(network, connection.hostname(),
+                                       connection.port(),
+                                       connection.getUptime().orElse(Duration.ofSeconds(0)),
+                                       connection.nickname(),
                                        channels.stream()
                                                        .filter((channel) -> channel.network()
                                                                        .equals(network))
@@ -258,6 +264,10 @@ public class Core extends AbstractExecutionThreadService {
                return downloads.values();
        }
 
+       public Duration getUptime() {
+               return Duration.between(startup, Instant.now());
+       }
+
        //
        // ACTIONS
        //
@@ -329,13 +339,6 @@ public class Core extends AbstractExecutionThreadService {
                        return;
                }
 
-               /* get connection. */
-               Connection connection = networkConnections.get(bot.network());
-               if (connection == null) {
-                       /* request for unknown network? */
-                       return;
-               }
-
                /* stop the DCC receiver. */
                if (download.get().dccReceiver() != null) {
                        download.get().dccReceiver().stop();
@@ -344,6 +347,13 @@ public class Core extends AbstractExecutionThreadService {
                        downloads.remove(pack.name(), download.get());
                }
 
+               /* get connection. */
+               Connection connection = networkConnections.get(bot.network());
+               if (connection == null) {
+                       /* request for unknown network? */
+                       return;
+               }
+
                /* remove the request from the bot, too. */
                try {
                        connection.sendMessage(bot.name(), String.format("XDCC %s", (download.get().dccReceiver() != null) ? "CANCEL" : "REMOVE"));