Split command reader into separate commands.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / ListDownloadsCommand.java
1 /*
2  * XdccDownloader - ListDownloadsCommand.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.xdcc.ui.stdin;
19
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;
26
27 import java.io.IOException;
28 import java.io.Writer;
29 import java.util.Collection;
30 import java.util.List;
31
32 import net.pterodactylus.irc.DccReceiver;
33 import net.pterodactylus.xdcc.core.Core;
34 import net.pterodactylus.xdcc.data.Download;
35
36 import com.google.common.collect.Ordering;
37
38 /**
39  * Command that will list all current downloads.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class ListDownloadsCommand implements Command {
44
45         /** The core to operate on. */
46         private final Core core;
47
48         /**
49          * Creates a new list downloads command.
50          *
51          * @param core
52          *              The core to operate on
53          */
54         public ListDownloadsCommand(Core core) {
55                 this.core = core;
56         }
57
58         //
59         // COMMAND METHODS
60         //
61
62         @Override
63         public String getName() {
64                 return "list";
65         }
66
67         @Override
68         public Collection<String> getAliases() {
69                 return asList("dcc");
70         }
71
72         @Override
73         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
74                 int counter = 0;
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()));
81                                 continue;
82                         }
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)));
86                         } else {
87                                 if (dccReceiver.progress() >= dccReceiver.size()) {
88                                         outputWriter.write(String.format("complete, %s/s", f(dccReceiver.overallRate())));
89                                 } else {
90                                         outputWriter.write(String.format("aborted at %.1f%%, %s/s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
91                                 }
92                         }
93                         outputWriter.write(")\n");
94                 }
95                 outputWriter.write("End of DCCs.\n");
96                 outputWriter.flush();
97                 return state.setLastDownloads(downloads);
98         }
99
100         //
101         // PRIVATE METHODS
102         //
103
104         /**
105          * Returns the estimated time left for the given transfer.
106          *
107          * @param dccReceiver
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
110          *         estimated
111          */
112         private static String getTimeLeft(DccReceiver dccReceiver) {
113                 if ((dccReceiver.size() == -1) || (dccReceiver.currentRate() == 0)) {
114                         return "unknown";
115                 }
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);
119                 }
120                 return String.format("%02d:%02d", (secondsLeft / 60) % 60, secondsLeft % 60);
121         }
122
123 }