84a7d2ec57de7971a0f1143a5cae9d9902584e95
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / RestartCommand.java
1 /*
2  * XdccDownloader - RestartCommand.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 java.util.Arrays.asList;
21
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.util.Collection;
25 import java.util.List;
26
27 import net.pterodactylus.xdcc.core.Core;
28 import net.pterodactylus.xdcc.data.Download;
29
30 import com.google.common.primitives.Ints;
31
32 /**
33  * TODO
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class RestartCommand implements Command {
38
39         private final Core core;
40         private final DownloadFailures downloadFailures;
41
42         public RestartCommand(Core core, DownloadFailures downloadFailures) {
43                 this.core = core;
44                 this.downloadFailures = downloadFailures;
45         }
46
47         @Override
48         public String getName() {
49                 return "restart";
50         }
51
52         @Override
53         public Collection<String> getAliases() {
54                 return asList("sa");
55         }
56
57         @Override
58         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
59                 Integer index = Ints.tryParse(parameters.get(0));
60                 List<Download> lastFailedDownloads = state.getLastFailedDownloads();
61                 if ((index != null) && (index < lastFailedDownloads.size())) {
62                         Download failedDownload = lastFailedDownloads.get(index);
63                         core.fetch(failedDownload.bot(), failedDownload.pack());
64                         downloadFailures.removeFailedDownload(failedDownload);
65                 }
66                 return state;
67         }
68
69 }