Add method to return all connected networks.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index 240beb1..d13b486 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.xdcc.core;
 
+import static net.pterodactylus.xdcc.data.Channel.TO_NETWORK;
 import static net.pterodactylus.xdcc.data.Download.FILTER_RUNNING;
 
 import java.io.File;
@@ -69,6 +70,7 @@ import net.pterodactylus.xdcc.data.Network;
 import net.pterodactylus.xdcc.data.Pack;
 import net.pterodactylus.xdcc.data.Server;
 
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 import com.google.common.collect.FluentIterable;
@@ -158,6 +160,29 @@ public class Core extends AbstractExecutionThreadService {
        }
 
        /**
+        * Returns all defined networks.
+        *
+        * @return All defined networks
+        */
+       public Collection<Network> networks() {
+               return FluentIterable.from(channels).transform(TO_NETWORK).toSet();
+       }
+
+       /**
+        * Returns all connected networks.
+        *
+        * @return All connected networks
+        */
+       public Collection<Network> connectedNetworks() {
+               return Lists.newArrayList(Optional.presentInstances(FluentIterable.from(networkConnections.values()).transform(new Function<Connection, Optional<Network>>() {
+                       @Override
+                       public Optional<Network> apply(Connection connection) {
+                               return getNetwork(connection);
+                       }
+               })));
+       }
+
+       /**
         * Returns all configured channels. Due to various circumstances, configured
         * channels might not actually be joined.
         *
@@ -281,12 +306,12 @@ public class Core extends AbstractExecutionThreadService {
                        return;
                }
 
-               /* remove download. */
-               downloads.remove(pack.name(), download.get());
-
                /* stop the DCC receiver. */
                if (download.get().dccReceiver() != null) {
                        download.get().dccReceiver().stop();
+               } else {
+                       /* remove download if it hasn’t started yet. */
+                       downloads.remove(pack.name(), download.get());
                }
 
                /* remove the request from the bot, too. */
@@ -443,13 +468,17 @@ public class Core extends AbstractExecutionThreadService {
 
                /* found network? */
                if (!network.isPresent()) {
+                       eventBus.post(new GenericMessage(String.format("Connected to unknown network: %s", connectionEstablished.connection().hostname())));
                        return;
                }
 
+               eventBus.post(new GenericMessage(String.format("Connected to network %s.", network.get().name())));
+
                /* join all channels on this network. */
                for (Channel channel : channels) {
                        if (channel.network().equals(network.get())) {
                                try {
+                                       eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", channel.name(), network.get().name())));
                                        connectionEstablished.connection().joinChannel(channel.name());
                                } catch (IOException ioe1) {
                                        logger.log(Level.WARNING, String.format("Could not join %s on %s!", channel.name(), network.get().name()), ioe1);
@@ -543,10 +572,7 @@ public class Core extends AbstractExecutionThreadService {
                        return;
                }
 
-               Bot removedBot = networkBots.remove(network.get(), channelLeft.client().nick().get());
-               if (removedBot != null) {
-                       eventBus.post(new GenericMessage(String.format("Bot %s (%s) was removed, %d packs removed.", removedBot.name(), removedBot.network().name(), removedBot.packs().size())));
-               }
+               networkBots.remove(network.get(), channelLeft.client().nick().get());
        }
 
        /**
@@ -562,10 +588,7 @@ public class Core extends AbstractExecutionThreadService {
                        return;
                }
 
-               Bot removedBot = networkBots.remove(network.get(), clientQuit.client().nick().get());
-               if (removedBot != null) {
-                       eventBus.post(new GenericMessage(String.format("Bot %s (%s) was removed, %d packs removed.", removedBot.name(), removedBot.network().name(), removedBot.packs().size())));
-               }
+               networkBots.remove(network.get(), clientQuit.client().nick().get());
        }
 
        /**