8a19dffda7480d576bff87462f8cb7337ea6fc92
[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.data.Download.SECONDS_LEFT;
26 import static net.pterodactylus.xdcc.ui.stdin.CommandReader.f;
27 import static net.pterodactylus.xdcc.ui.stdin.CommandReader.t;
28
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.util.Collection;
32 import java.util.List;
33
34 import net.pterodactylus.irc.DccReceiver;
35 import net.pterodactylus.xdcc.core.Core;
36 import net.pterodactylus.xdcc.data.Download;
37
38 import com.google.common.collect.Ordering;
39
40 /**
41  * Command that will list all current downloads.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class ListDownloadsCommand implements Command {
46
47         /** The core to operate on. */
48         private final Core core;
49
50         /**
51          * Creates a new list downloads command.
52          *
53          * @param core
54          *              The core to operate on
55          */
56         public ListDownloadsCommand(Core core) {
57                 this.core = core;
58         }
59
60         //
61         // COMMAND METHODS
62         //
63
64         @Override
65         public String getName() {
66                 return "list";
67         }
68
69         @Override
70         public Collection<String> getAliases() {
71                 return asList("dcc");
72         }
73
74         @Override
75         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
76                 int counter = 0;
77                 List<Download> downloads = newArrayList(from(core.downloads()).toSortedList(Ordering.from(BY_NAME).compound(BY_RUNNING)));
78                 for (Download download : downloads) {
79                         DccReceiver dccReceiver = download.dccReceiver();
80                         if (dccReceiver == null) {
81                                                 /* download has not even started. */
82                                 outputWriter.write(String.format("[%d] %s requested from %s (not started yet)\n", counter++, download.pack().name(), download.bot().name()));
83                                 continue;
84                         }
85                         outputWriter.write(String.format("[%d] %s from %s (%s, ", counter++, dccReceiver.filename(), download.bot().name(), f(dccReceiver.size())));
86                         if (dccReceiver.isRunning()) {
87                                 outputWriter.write(String.format("%.1f%%, %s/s, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate()), t(SECONDS_LEFT.apply(download))));
88                         } else {
89                                 if (dccReceiver.progress() >= dccReceiver.size()) {
90                                         outputWriter.write(String.format("complete, %s/s", f(dccReceiver.overallRate())));
91                                 } else {
92                                         outputWriter.write(String.format("aborted at %.1f%%, %s/s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
93                                 }
94                         }
95                         outputWriter.write(")\n");
96                 }
97                 outputWriter.write("End of DCCs.\n");
98                 outputWriter.flush();
99                 return state.setLastDownloads(downloads);
100         }
101
102 }