1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.plugin.gift.net;
21
22 import java.io.File;
23
24 import javax.swing.Action;
25
26 import org.xnap.peer.Peer;
27 import org.xnap.plugin.Plugin;
28 import org.xnap.plugin.gift.GiFTPlugin;
29 import org.xnap.transfer.AbstractTransfer;
30 import org.xnap.transfer.DefaultSegment;
31 import org.xnap.transfer.Download;
32 import org.xnap.transfer.Segment;
33
34 /***
35 * @author Tammo
36 */
37 public class GiFTDownload extends AbstractTransfer implements Download {
38
39 private GiFTDownloadContainer dc;
40 private GiFTUser user;
41 private String url;
42 private String status;
43 private DefaultSegment segment;
44
45 private boolean failed;
46 private boolean done;
47 private boolean paused = true;
48
49 private boolean marked;
50
51 /***
52 *
53 */
54 public GiFTDownload(GiFTDownloadContainer dc, GiFTUser user, String url)
55 {
56 this.dc = dc;
57 this.user = user;
58 this.url = url;
59
60 segment = new DefaultSegment(dc.getFilesize());
61 }
62
63 private void done()
64 {
65 done = true;
66 transferStopped();
67
68 segment.setEnd(segment.getStart() + segment.getTransferred());
69 }
70
71 /***
72 * @see xnap.transfer.Transfer#getActions()
73 */
74 public Action[] getActions() {
75 return dc.getActions(url);
76 }
77
78 /***
79 * @see xnap.transfer.Transfer#getTotalBytesTransferred()
80 */
81 public long getBytesTransferred() {
82 return segment.getTransferred();
83 }
84
85 /***
86 * @see xnap.transfer.Transfer#getFile()
87 */
88 public File getFile() {
89 return null;
90 }
91
92 /***
93 * @see xnap.transfer.Transfer#getPeer()
94 */
95 public Peer getPeer() {
96 return user;
97 }
98
99 public Plugin getPlugin()
100 {
101 return GiFTPlugin.getInstance();
102 }
103
104 public Segment[] getSegments()
105 {
106 return new Segment[] { segment };
107 }
108
109 /***
110 * @see xnap.transfer.Transfer#getTotalBytesTransferred()
111 */
112 public long getTotalBytesTransferred() {
113 return segment.getTransferred();
114 }
115
116 /***
117 * @see xnap.transfer.Transfer#getStatus()
118 */
119 public String getStatus()
120 {
121 return status;
122 }
123
124 /***
125 * @see xnap.transfer.Transfer#isDone()
126 */
127 public boolean isDone()
128 {
129 return done;
130 }
131
132 public boolean isMarked()
133 {
134 return marked;
135 }
136
137 /***
138 * @see xnap.transfer.Transfer#isRunning()
139 */
140 public boolean isRunning() {
141 return !paused && !done;
142 }
143
144 /***
145 * @param l
146 */
147 public void updated(long start, long transmit, long total)
148 {
149 segment.setStart(start);
150 segment.setEnd(start + total);
151 segment.setTransferred(transmit);
152 }
153
154 /***
155 * @see xnap.transfer.Transfer#getFilename()
156 */
157 public String getFilename() {
158 return dc.getFilename();
159 }
160
161 /***
162 * @see xnap.transfer.Transfer#getFilesize()
163 */
164 public long getFilesize() {
165 return segment.getEnd() - segment.getStart();
166 }
167
168 public void setMarked(boolean marked)
169 {
170 this.marked = marked;
171 }
172
173 private void setStatus(String status)
174 {
175 this.status = status;
176 stateChanged();
177 }
178
179 public void setState(String state, String protocolStatus)
180 {
181 if (state.equals("Active")) {
182 if (paused) {
183 paused = false;
184 transferStarted();
185 }
186 }
187 else if (state.equals("Paused")
188 || state.startsWith("Queued")) {
189 if (!paused) {
190 paused = true;
191 transferStopped();
192 }
193 }
194 else if (state.startsWith("Cancelled")
195 || state.equals("Complete")
196 || state.equals("Timed out")
197 || state.equals("Inactive")) {
198 if (!done) {
199 done();
200 }
201 }
202
203 setStatus((protocolStatus == null || protocolStatus.equals(state))
204 ? state
205 : state + ": " + protocolStatus);
206 }
207
208 }