2 * XdccDownloader - ListDownloadsCommand.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.xdcc.ui.stdin;
20 import static com.google.common.collect.FluentIterable.from;
21 import static com.google.common.collect.Lists.newArrayList;
22 import static java.util.Arrays.asList;
23 import static net.pterodactylus.xdcc.data.Download.BY_NAME;
24 import static net.pterodactylus.xdcc.data.Download.BY_RUNNING;
25 import static net.pterodactylus.xdcc.ui.stdin.CommandReader.f;
27 import java.io.IOException;
28 import java.io.Writer;
29 import java.util.Collection;
30 import java.util.List;
32 import net.pterodactylus.irc.DccReceiver;
33 import net.pterodactylus.xdcc.core.Core;
34 import net.pterodactylus.xdcc.data.Download;
36 import com.google.common.collect.Ordering;
39 * Command that will list all current downloads.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class ListDownloadsCommand implements Command {
45 /** The core to operate on. */
46 private final Core core;
49 * Creates a new list downloads command.
52 * The core to operate on
54 public ListDownloadsCommand(Core core) {
63 public String getName() {
68 public Collection<String> getAliases() {
73 public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
75 List<Download> downloads = newArrayList(from(core.downloads()).toSortedList(Ordering.from(BY_NAME).compound(BY_RUNNING)));
76 for (Download download : downloads) {
77 DccReceiver dccReceiver = download.dccReceiver();
78 if (dccReceiver == null) {
79 /* download has not even started. */
80 outputWriter.write(String.format("[%d] %s requested from %s (not started yet)\n", counter++, download.pack().name(), download.bot().name()));
83 outputWriter.write(String.format("[%d] %s from %s (%s, ", counter++, dccReceiver.filename(), download.bot().name(), f(dccReceiver.size())));
84 if (dccReceiver.isRunning()) {
85 outputWriter.write(String.format("%.1f%%, %s/s, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate()), getTimeLeft(dccReceiver)));
87 if (dccReceiver.progress() >= dccReceiver.size()) {
88 outputWriter.write(String.format("complete, %s/s", f(dccReceiver.overallRate())));
90 outputWriter.write(String.format("aborted at %.1f%%, %s/s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
93 outputWriter.write(")\n");
95 outputWriter.write("End of DCCs.\n");
97 return state.setLastDownloads(downloads);
105 * Returns the estimated time left for the given transfer.
108 * The DCC receiver to get the time left for
109 * @return The time left for the transfer, or “unknown” if the time can not be
112 private static String getTimeLeft(DccReceiver dccReceiver) {
113 if ((dccReceiver.size() == -1) || (dccReceiver.currentRate() == 0)) {
116 long secondsLeft = (dccReceiver.size() - dccReceiver.progress()) / dccReceiver.currentRate();
117 if (secondsLeft > 3600) {
118 return String.format("%02d:%02d:%02d", secondsLeft / 3600, (secondsLeft / 60) % 60, secondsLeft % 60);
120 return String.format("%02d:%02d", (secondsLeft / 60) % 60, secondsLeft % 60);