You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransferContainer.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.dcc;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.ServerState;
  26. import com.dmdirc.addons.dcc.events.DccSendDatatransferedEvent;
  27. import com.dmdirc.addons.dcc.events.DccSendSocketclosedEvent;
  28. import com.dmdirc.addons.dcc.events.DccSendSocketopenedEvent;
  29. import com.dmdirc.addons.dcc.io.DCC;
  30. import com.dmdirc.addons.dcc.io.DCCTransfer;
  31. import com.dmdirc.interfaces.Connection;
  32. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  33. import com.dmdirc.parser.interfaces.Parser;
  34. import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
  35. import com.dmdirc.util.URLBuilder;
  36. import java.awt.Desktop;
  37. import java.io.File;
  38. import java.util.Arrays;
  39. import java.util.Date;
  40. import javax.swing.JOptionPane;
  41. /**
  42. * This class links DCC Send objects to a window.
  43. */
  44. public class TransferContainer extends FrameContainer implements
  45. DCCTransferHandler, SocketCloseListener {
  46. /** The dcc plugin that owns this frame */
  47. protected final DCCManager plugin;
  48. /** Config manager. */
  49. private final AggregateConfigProvider config;
  50. /** The Window we're using. */
  51. private boolean windowClosing = false;
  52. /** The DCCSend object we are a window for */
  53. private final DCCTransfer dcc;
  54. /** Other Nickname */
  55. private final String otherNickname;
  56. /** Total data transferred */
  57. private volatile long transferCount = 0;
  58. /** Time Started */
  59. private long timeStarted = 0;
  60. /** Plugin that this send belongs to. */
  61. private final DCCManager myPlugin;
  62. /** IRC Parser that caused this send */
  63. private Parser parser = null;
  64. /** Connection the send was initiated on. */
  65. private Connection connection = null;
  66. /** Show open button. */
  67. private final boolean showOpen = Desktop.isDesktopSupported()
  68. && Desktop.getDesktop().isSupported(Desktop.Action.OPEN);
  69. /** Event bus to post events on. */
  70. private final DMDircMBassador eventBus;
  71. /**
  72. * Creates a new instance of DCCTransferWindow with a given DCCTransfer object.
  73. *
  74. * @param plugin the DCC Plugin responsible for this window
  75. * @param dcc The DCCTransfer object this window wraps around
  76. * @param config Config manager
  77. * @param title The title of this window
  78. * @param targetNick Nickname of target
  79. * @param connection The connection that the send was that initiated on
  80. * @param urlBuilder The URL builder to use when finding icons.
  81. * @param eventBus The bus to dispatch events on.
  82. */
  83. public TransferContainer(final DCCManager plugin, final DCCTransfer dcc,
  84. final AggregateConfigProvider config, final String title,
  85. final String targetNick, final Connection connection,
  86. final URLBuilder urlBuilder, final DMDircMBassador eventBus) {
  87. super(plugin.getContainer(), dcc.getType() == DCCTransfer.TransferType.SEND
  88. ? "dcc-send-inactive" : "dcc-receive-inactive",
  89. title, title, config, urlBuilder, eventBus,
  90. Arrays.asList("com.dmdirc.addons.dcc.ui.TransferPanel"));
  91. this.plugin = plugin;
  92. this.dcc = dcc;
  93. this.connection = connection;
  94. this.config = config;
  95. parser = connection == null ? null : connection.getParser();
  96. myPlugin = plugin;
  97. if (parser != null) {
  98. parser.getCallbackManager().addNonCriticalCallback(
  99. SocketCloseListener.class, this);
  100. }
  101. dcc.addHandler(this);
  102. otherNickname = targetNick;
  103. this.eventBus = eventBus;
  104. }
  105. @Override
  106. public void onSocketClosed(final Parser parser, final Date date) {
  107. // Remove our reference to the parser (and its reference to us)
  108. this.parser.getCallbackManager().delAllCallback(this);
  109. this.parser = null;
  110. }
  111. /**
  112. * Get the DCCSend Object associated with this window
  113. *
  114. * @return The DCCSend Object associated with this window
  115. */
  116. public DCCTransfer getDCC() {
  117. return dcc;
  118. }
  119. /**
  120. * Retrieves the nickname of the other party involved in this transfer.
  121. *
  122. * @return The other party's nickname
  123. *
  124. * @since 0.6.4
  125. */
  126. public String getOtherNickname() {
  127. return otherNickname;
  128. }
  129. /**
  130. * Called when data is sent/received
  131. *
  132. * @param dcc The DCCSend that this message is from
  133. * @param bytes The number of new bytes that were transferred
  134. */
  135. @Override
  136. public void dataTransferred(final DCCTransfer dcc, final int bytes) {
  137. final double percent;
  138. synchronized (this) {
  139. transferCount += bytes;
  140. percent = getPercent();
  141. }
  142. final boolean percentageInTitle = config.getOptionBool(
  143. plugin.getDomain(), "general.percentageInTitle");
  144. if (percentageInTitle) {
  145. final StringBuilder title = new StringBuilder();
  146. if (dcc.isListenSocket()) {
  147. title.append("*");
  148. }
  149. title.append(dcc.getType() == DCCTransfer.TransferType.SEND
  150. ? "Sending: " : "Receiving: ");
  151. title.append(otherNickname);
  152. title.append(" (")
  153. .append(String.format("%.0f", Math.floor(percent)))
  154. .append("%)");
  155. setName(title.toString());
  156. setTitle(title.toString());
  157. }
  158. eventBus.publish(new DccSendDatatransferedEvent(this, bytes));
  159. }
  160. /**
  161. * Retrieves the current percentage progress of this transfer.
  162. *
  163. * @since 0.6.4
  164. * @return The percentage of this transfer that has been completed
  165. */
  166. public double getPercent() {
  167. return (100.00 / dcc.getFileSize()) * (transferCount
  168. + dcc.getFileStart());
  169. }
  170. /**
  171. * Retrieves the current transfer speed of this transfer.
  172. *
  173. * @since 0.6.4
  174. * @return The speed of this transfer in Bytes/Sec
  175. */
  176. public double getBytesPerSecond() {
  177. final long time = getElapsedTime();
  178. synchronized (this) {
  179. return time > 0 ? ((double) transferCount / time) : transferCount;
  180. }
  181. }
  182. /**
  183. * Retrieves the estimated time remaining for this transfer.
  184. *
  185. * @since 0.6.4
  186. * @return The number of seconds estimated for this transfer to complete
  187. */
  188. public double getRemainingTime() {
  189. final double bytesPerSecond = getBytesPerSecond();
  190. final long remainingBytes;
  191. synchronized (this) {
  192. remainingBytes = dcc.getFileSize() - dcc.getFileStart()
  193. - transferCount;
  194. }
  195. return bytesPerSecond > 0 ? (remainingBytes / bytesPerSecond) : 1;
  196. }
  197. /**
  198. * Retrieves the timestamp at which this transfer started.
  199. *
  200. * @since 0.6.4
  201. * @return The timestamp (milliseconds since 01/01/1970) at which this transfer started.
  202. */
  203. public long getStartTime() {
  204. return timeStarted;
  205. }
  206. /**
  207. * Retrieves the number of seconds that this transfer has been running for.
  208. *
  209. * @since 0.6.4
  210. * @return The number of seconds elapsed since this transfer started
  211. */
  212. public long getElapsedTime() {
  213. return (System.currentTimeMillis() - timeStarted) / 1000;
  214. }
  215. /**
  216. * Determines whether this transfer is complete or not.
  217. *
  218. * @since 0.6.4
  219. * @return True if the transfer is complete, false otherwise
  220. */
  221. public boolean isComplete() {
  222. return transferCount == dcc.getFileSize() - dcc.getFileStart();
  223. }
  224. /**
  225. * Determines whether the "Open" button should be displayed for this transfer.
  226. *
  227. * @since 0.6.4
  228. * @return True if the open button should be displayed, false otherwise
  229. */
  230. public boolean shouldShowOpenButton() {
  231. return showOpen && dcc.getType() == DCCTransfer.TransferType.RECEIVE;
  232. }
  233. /**
  234. * Called when the socket is closed
  235. *
  236. * @param dcc The DCCSend that this message is from
  237. */
  238. @Override
  239. public void socketClosed(final DCCTransfer dcc) {
  240. eventBus.publish(new DccSendSocketclosedEvent(this));
  241. if (!windowClosing) {
  242. synchronized (this) {
  243. if (transferCount == dcc.getFileSize() - dcc.getFileStart()) {
  244. setIcon(dcc.getType() == DCCTransfer.TransferType.SEND
  245. ? "dcc-send-done" : "dcc-receive-done");
  246. } else {
  247. setIcon(dcc.getType() == DCCTransfer.TransferType.SEND
  248. ? "dcc-send-failed" : "dcc-receive-failed");
  249. }
  250. }
  251. }
  252. }
  253. /**
  254. * Called when the socket is opened
  255. *
  256. * @param dcc The DCCSend that this message is from
  257. */
  258. @Override
  259. public void socketOpened(final DCCTransfer dcc) {
  260. eventBus.publish(new DccSendSocketopenedEvent(this));
  261. timeStarted = System.currentTimeMillis();
  262. setIcon(dcc.getType() == DCCTransfer.TransferType.SEND
  263. ? "dcc-send-active" : "dcc-receive-active");
  264. }
  265. /**
  266. * Attempts to resend the transfer.
  267. *
  268. * @since 0.6.4
  269. * @return True if the transfer could be resent, false otherwise
  270. */
  271. public boolean resend() {
  272. synchronized (this) {
  273. transferCount = 0;
  274. }
  275. dcc.reset();
  276. if (connection != null && connection.getState() == ServerState.CONNECTED) {
  277. final String myNickname = connection.getParser().getLocalClient()
  278. .getNickname();
  279. // Check again in case we have changed nickname to the same nickname
  280. //that this send is for.
  281. if (connection.getParser().getStringConverter().equalsIgnoreCase(
  282. otherNickname, myNickname)) {
  283. final Thread errorThread = new Thread(new Runnable() {
  284. @Override
  285. public void run() {
  286. JOptionPane.showMessageDialog(null,
  287. "You can't DCC yourself.", "DCC Error",
  288. JOptionPane.ERROR_MESSAGE);
  289. }
  290. }, "DCC-Error-Message");
  291. errorThread.start();
  292. } else {
  293. if (config.getOptionBool(plugin.getDomain(), "send.reverse")) {
  294. parser.sendCTCP(otherNickname, "DCC", "SEND \"" + new File(dcc.getFileName()).
  295. getName() + "\" "
  296. + DCC.ipToLong(myPlugin.getListenIP(parser))
  297. + " 0 " + dcc.getFileSize() + " " + dcc.makeToken()
  298. + ((dcc.isTurbo()) ? " T" : ""));
  299. } else if (plugin.listen(dcc)) {
  300. parser.sendCTCP(otherNickname, "DCC", "SEND \""
  301. + new File(dcc.getFileName()).getName() + "\" "
  302. + DCC.ipToLong(myPlugin.getListenIP(parser)) + " "
  303. + dcc.getPort() + " " + dcc.getFileSize()
  304. + ((dcc.isTurbo()) ? " T" : ""));
  305. }
  306. }
  307. return true;
  308. }
  309. return false;
  310. }
  311. /**
  312. * Closes this container (and it's associated frame).
  313. */
  314. @Override
  315. public void close() {
  316. windowClosing = true;
  317. super.close();
  318. dcc.removeFromTransfers();
  319. }
  320. public void addSocketCloseCallback(final SocketCloseListener listener) {
  321. if (connection != null && connection.getParser() != null) {
  322. connection.getParser().getCallbackManager()
  323. .addNonCriticalCallback(SocketCloseListener.class,
  324. listener);
  325. }
  326. }
  327. @Override
  328. public Connection getConnection() {
  329. return null;
  330. }
  331. }