source: java/installer2/src/net/oni2/aeinstaller/backend/packages/download/ModDownload.java@ 790

Last change on this file since 790 was 741, checked in by alloc, 12 years ago

HTTP File Downloader lib - import

File size: 4.0 KB
Line 
1package net.oni2.aeinstaller.backend.packages.download;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.io.PrintWriter;
7
8import net.oni2.aeinstaller.backend.Paths;
9import net.oni2.aeinstaller.backend.packages.Package;
10import net.oni2.httpfiledownloader.FileDownloadListener;
11import net.oni2.httpfiledownloader.FileDownloader;
12import net.oni2.httpfiledownloader.FileDownloader.EState;
13import net.oni2.aeinstaller.backend.packages.unpack.UnpackListener;
14import net.oni2.aeinstaller.backend.packages.unpack.Unpacker;
15
16/**
17 * @author Christian Illy
18 */
19public class ModDownload implements FileDownloadListener, UnpackListener {
20 /**
21 * @author Christian Illy
22 */
23 public enum ModDownloadState {
24 /**
25 * Downloader initialized but not started
26 */
27 INIT,
28 /**
29 * Download running
30 */
31 RUNNING,
32 /**
33 * Aborted because of an error
34 */
35 ERROR,
36 /**
37 * Download interrupted
38 */
39 INTERRUPTED,
40 /**
41 * Download finished successfully
42 */
43 DOWNLOADED,
44 /**
45 * Package unzipped successfully
46 */
47 UNPACKED
48 };
49
50 private Package mod;
51 private FileDownloader downloader;
52 private Unpacker unpacker;
53 private File zipFile;
54 private File targetFolder;
55 private ModDownloadListener listener;
56 private int size;
57
58 private ModDownloadState state = ModDownloadState.INIT;
59
60 /**
61 * Create a mod download
62 *
63 * @param mod
64 * Mod to download
65 * @param listener
66 * Listener for progress
67 */
68 public ModDownload(Package mod, ModDownloadListener listener) {
69 this.mod = mod;
70 this.listener = listener;
71
72 zipFile = new File(Paths.getDownloadPath(),
73 mod.getPackageNumberString() + ".zip");
74 targetFolder = mod.getLocalPath();
75 try {
76 downloader = new FileDownloader(mod.getFile().getUri_full(),
77 zipFile);
78 downloader.addListener(this);
79 unpacker = new Unpacker(zipFile, targetFolder, this);
80 } catch (IOException e) {
81 e.printStackTrace();
82 }
83 }
84
85 /**
86 * @return Size of this download
87 */
88 public int getSize() {
89 return mod.getZipSize();
90 }
91
92 /**
93 * Start this download
94 */
95 public void start() {
96 state = ModDownloadState.RUNNING;
97 downloader.start();
98 }
99
100 /**
101 * Abort this download
102 */
103 public void abort() {
104 switch (state) {
105 case UNPACKED:
106 case INIT:
107 case ERROR:
108 case INTERRUPTED:
109 break;
110 case RUNNING:
111 downloader.stop();
112 break;
113 case DOWNLOADED:
114 unpacker.stop();
115 break;
116 }
117 state = ModDownloadState.INTERRUPTED;
118 }
119
120 /**
121 * @return the mod object handled by this download
122 */
123 public Package getMod() {
124 return mod;
125 }
126
127 private void writeTimestamp() {
128 File logFile = new File(targetFolder, "aei.cfg");
129 PrintWriter log = null;
130 try {
131 log = new PrintWriter(logFile);
132 log.println("Timestamp -> " + mod.getFile().getTimestamp());
133 } catch (FileNotFoundException e) {
134 e.printStackTrace();
135 }
136 if (log != null)
137 log.close();
138 }
139
140 @Override
141 public void statusUpdate(FileDownloader source, EState state, int done,
142 int total) {
143 switch (state) {
144 case INIT:
145 break;
146 case RUNNING:
147 listener.modDownloadStatusUpdate(this, this.state, done, total);
148 break;
149 case PAUSED:
150 break;
151 case INTERRUPTED:
152 break;
153 case ERROR:
154 this.state = ModDownloadState.ERROR;
155 listener.modDownloadStatusUpdate(this, this.state, done, total);
156 break;
157 case FINISHED:
158 this.state = ModDownloadState.DOWNLOADED;
159 listener.modDownloadStatusUpdate(this, this.state, done, total);
160 this.size = done;
161 unpacker.start();
162 break;
163 }
164 }
165
166 @Override
167 public void statusUpdate(Unpacker source,
168 net.oni2.aeinstaller.backend.packages.unpack.Unpacker.EState state) {
169 switch (state) {
170 case INIT:
171 break;
172 case RUNNING:
173 break;
174 case INTERRUPTED:
175 this.state = ModDownloadState.INTERRUPTED;
176 listener.modDownloadStatusUpdate(this, this.state, size, size);
177 break;
178 case FINISHED:
179 this.state = ModDownloadState.UNPACKED;
180 writeTimestamp();
181 zipFile.delete();
182 listener.modDownloadStatusUpdate(this, this.state, size, size);
183 break;
184 }
185 }
186}
Note: See TracBrowser for help on using the repository browser.