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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.FrameContainer;
  24. import com.dmdirc.ServerState;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.addons.dcc.actions.DCCActions;
  27. import com.dmdirc.addons.dcc.io.DCC;
  28. import com.dmdirc.addons.dcc.io.DCCTransfer;
  29. import com.dmdirc.interfaces.Connection;
  30. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  31. import com.dmdirc.parser.interfaces.Parser;
  32. import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
  33. import com.dmdirc.util.URLBuilder;
  34. import java.awt.Desktop;
  35. import java.io.File;
  36. import java.util.Arrays;
  37. import java.util.Date;
  38. import javax.swing.JOptionPane;
  39. /**
  40. * This class links DCC Send objects to a window.
  41. */
  42. public class TransferContainer extends FrameContainer implements
  43. DCCTransferHandler, SocketCloseListener {
  44. /** The dcc plugin that owns this frame */
  45. protected final DCCManager plugin;
  46. /** Config manager. */
  47. private final AggregateConfigProvider config;
  48. /** The Window we're using. */
  49. private boolean windowClosing = false;
  50. /** The DCCSend object we are a window for */
  51. private final DCCTransfer dcc;
  52. /** Other Nickname */
  53. private final String otherNickname;
  54. /** Total data transfered */
  55. private volatile long transferCount = 0;
  56. /** Time Started */
  57. private long timeStarted = 0;
  58. /** Plugin that this send belongs to. */
  59. private final DCCManager myPlugin;
  60. /** IRC Parser that caused this send */
  61. private Parser parser = null;
  62. /** Connection the send was initiated on. */
  63. private Connection connection = null;
  64. /** Show open button. */
  65. private boolean showOpen = Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(
  66. Desktop.Action.OPEN);
  67. /**
  68. * Creates a new instance of DCCTransferWindow with a given DCCTransfer object.
  69. *
  70. * @param plugin the DCC Plugin responsible for this window
  71. * @param dcc The DCCTransfer object this window wraps around
  72. * @param config Config manager
  73. * @param title The title of this window
  74. * @param targetNick Nickname of target
  75. * @param connection The connection that the send was that initiated on
  76. * @param urlBuilder The URL builder to use when finding icons.
  77. */
  78. public TransferContainer(final DCCManager plugin, final DCCTransfer dcc,
  79. final AggregateConfigProvider config, final String title,
  80. final String targetNick, final Connection connection, final URLBuilder urlBuilder) {
  81. super(dcc.getType() == DCCTransfer.TransferType.SEND
  82. ? "dcc-send-inactive" : "dcc-receive-inactive",
  83. title, title, config, urlBuilder,
  84. Arrays.asList("com.dmdirc.addons.dcc.ui.TransferPanel"));
  85. this.plugin = plugin;
  86. this.dcc = dcc;
  87. this.connection = connection;
  88. this.config = config;
  89. parser = connection == null ? null : connection.getParser();
  90. myPlugin = plugin;
  91. if (parser != null) {
  92. parser.getCallbackManager().addNonCriticalCallback(
  93. SocketCloseListener.class, this);
  94. }
  95. dcc.addHandler(this);
  96. otherNickname = targetNick;
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public void onSocketClosed(final Parser parser, final Date date) {
  101. // Remove our reference to the parser (and its reference to us)
  102. this.parser.getCallbackManager().delAllCallback(this);
  103. this.parser = null;
  104. }
  105. /**
  106. * Get the DCCSend Object associated with this window
  107. *
  108. * @return The DCCSend Object associated with this window
  109. */
  110. public DCCTransfer getDCC() {
  111. return dcc;
  112. }
  113. /**
  114. * Retrieves the nickname of the other party involved in this transfer.
  115. *
  116. * @return The other party's nickname
  117. *
  118. * @since 0.6.4
  119. */
  120. public String getOtherNickname() {
  121. return otherNickname;
  122. }
  123. /**
  124. * Called when data is sent/recieved
  125. *
  126. * @param dcc The DCCSend that this message is from
  127. * @param bytes The number of new bytes that were transfered
  128. */
  129. @Override
  130. public void dataTransfered(final DCCTransfer dcc, final int bytes) {
  131. final double percent;
  132. synchronized (this) {
  133. transferCount += bytes;
  134. percent = getPercent();
  135. }
  136. final boolean percentageInTitle = config.getOptionBool(
  137. plugin.getDomain(), "general.percentageInTitle");
  138. if (percentageInTitle) {
  139. final StringBuilder title = new StringBuilder();
  140. if (dcc.isListenSocket()) {
  141. title.append("*");
  142. }
  143. title.append(dcc.getType() == DCCTransfer.TransferType.SEND
  144. ? "Sending: " : "Recieving: ");
  145. title.append(otherNickname);
  146. title.append(" (")
  147. .append(String.format("%.0f", Math.floor(percent)))
  148. .append("%)");
  149. setName(title.toString());
  150. setTitle(title.toString());
  151. }
  152. ActionManager.getActionManager().triggerEvent(
  153. DCCActions.DCC_SEND_DATATRANSFERED, null, this, bytes);
  154. }
  155. /**
  156. * Retrieves the current percentage progress of this transfer.
  157. *
  158. * @since 0.6.4
  159. * @return The percentage of this transfer that has been completed
  160. */
  161. public double getPercent() {
  162. return (100.00 / dcc.getFileSize()) * (transferCount
  163. + dcc.getFileStart());
  164. }
  165. /**
  166. * Retrieves the current transfer speed of this transfer.
  167. *
  168. * @since 0.6.4
  169. * @return The speed of this transfer in Bytes/Sec
  170. */
  171. public double getBytesPerSecond() {
  172. final long time = getElapsedTime();
  173. synchronized (this) {
  174. return time > 0 ? ((double) transferCount / time) : transferCount;
  175. }
  176. }
  177. /**
  178. * Retrieves the estimated time remaining for this transfer.
  179. *
  180. * @since 0.6.4
  181. * @return The number of seconds estimated for this transfer to complete
  182. */
  183. public double getRemainingTime() {
  184. final double bytesPerSecond = getBytesPerSecond();
  185. final long remaningBytes;
  186. synchronized (this) {
  187. remaningBytes = dcc.getFileSize() - dcc.getFileStart()
  188. - transferCount;
  189. }
  190. return bytesPerSecond > 0 ? (remaningBytes / bytesPerSecond) : 1;
  191. }
  192. /**
  193. * Retrieves the timestamp at which this transfer started.
  194. *
  195. * @since 0.6.4
  196. * @return The timestamp (milliseconds since 01/01/1970) at which this transfer started.
  197. */
  198. public long getStartTime() {
  199. return timeStarted;
  200. }
  201. /**
  202. * Retrieves the number of seconds that this transfer has been running for.
  203. *
  204. * @since 0.6.4
  205. * @return The number of seconds elapsed since this transfer started
  206. */
  207. public long getElapsedTime() {
  208. return (System.currentTimeMillis() - timeStarted) / 1000;
  209. }
  210. /**
  211. * Determines whether this transfer is complete or not.
  212. *
  213. * @since 0.6.4
  214. * @return True if the transfer is complete, false otherwise
  215. */
  216. public boolean isComplete() {
  217. return transferCount == dcc.getFileSize() - dcc.getFileStart();
  218. }
  219. /**
  220. * Determines whether the "Open" button should be displayed for this transfer.
  221. *
  222. * @since 0.6.4
  223. * @return True if the open button should be displayed, false otherwise
  224. */
  225. public boolean shouldShowOpenButton() {
  226. return showOpen && dcc.getType() == DCCTransfer.TransferType.RECEIVE;
  227. }
  228. /**
  229. * Called when the socket is closed
  230. *
  231. * @param dcc The DCCSend that this message is from
  232. */
  233. @Override
  234. public void socketClosed(final DCCTransfer dcc) {
  235. ActionManager.getActionManager().triggerEvent(
  236. DCCActions.DCC_SEND_SOCKETCLOSED, null, this);
  237. if (!windowClosing) {
  238. synchronized (this) {
  239. if (transferCount == dcc.getFileSize() - dcc.getFileStart()) {
  240. setIcon(dcc.getType() == DCCTransfer.TransferType.SEND
  241. ? "dcc-send-done" : "dcc-receive-done");
  242. } else {
  243. setIcon(dcc.getType() == DCCTransfer.TransferType.SEND
  244. ? "dcc-send-failed" : "dcc-receive-failed");
  245. }
  246. }
  247. }
  248. }
  249. /**
  250. * Called when the socket is opened
  251. *
  252. * @param dcc The DCCSend that this message is from
  253. */
  254. @Override
  255. public void socketOpened(final DCCTransfer dcc) {
  256. ActionManager.getActionManager().triggerEvent(
  257. DCCActions.DCC_SEND_SOCKETOPENED, null, this);
  258. timeStarted = System.currentTimeMillis();
  259. setIcon(dcc.getType() == DCCTransfer.TransferType.SEND
  260. ? "dcc-send-active" : "dcc-receive-active");
  261. }
  262. /**
  263. * Attempts to resend the transfer.
  264. *
  265. * @since 0.6.4
  266. * @return True if the transfer could be resent, false otherwise
  267. */
  268. public boolean resend() {
  269. synchronized (this) {
  270. transferCount = 0;
  271. }
  272. dcc.reset();
  273. if (connection != null && connection.getState() == ServerState.CONNECTED) {
  274. final String myNickname = connection.getParser().getLocalClient()
  275. .getNickname();
  276. // Check again incase we have changed nickname to the same nickname
  277. //that this send is for.
  278. if (connection.getParser().getStringConverter().equalsIgnoreCase(
  279. otherNickname, myNickname)) {
  280. final Thread errorThread = new Thread(new Runnable() {
  281. /** {@inheritDoc} */
  282. @Override
  283. public void run() {
  284. JOptionPane.showMessageDialog(null,
  285. "You can't DCC yourself.", "DCC Error",
  286. JOptionPane.ERROR_MESSAGE);
  287. }
  288. }, "DCC-Error-Message");
  289. errorThread.start();
  290. } else {
  291. if (config.getOptionBool(plugin.getDomain(), "send.reverse")) {
  292. parser.sendCTCP(otherNickname, "DCC", "SEND \"" + new File(dcc.getFileName()).
  293. getName() + "\" "
  294. + DCC.ipToLong(myPlugin.getListenIP(parser))
  295. + " 0 " + dcc.getFileSize() + " " + dcc.makeToken()
  296. + ((dcc.isTurbo()) ? " T" : ""));
  297. } else if (plugin.listen(dcc)) {
  298. parser.sendCTCP(otherNickname, "DCC", "SEND \""
  299. + new File(dcc.getFileName()).getName() + "\" "
  300. + DCC.ipToLong(myPlugin.getListenIP(parser)) + " "
  301. + dcc.getPort() + " " + dcc.getFileSize()
  302. + ((dcc.isTurbo()) ? " T" : ""));
  303. }
  304. }
  305. return true;
  306. }
  307. return false;
  308. }
  309. /**
  310. * Closes this container (and it's associated frame).
  311. */
  312. @Override
  313. public void close() {
  314. windowClosing = true;
  315. super.close();
  316. dcc.removeFromTransfers();
  317. }
  318. public void addSocketCloseCallback(final SocketCloseListener listener) {
  319. if (connection != null && connection.getParser() != null) {
  320. connection.getParser().getCallbackManager()
  321. .addNonCriticalCallback(SocketCloseListener.class,
  322. listener);
  323. }
  324. }
  325. /** {@inheritDoc} */
  326. @Override
  327. public Connection getConnection() {
  328. return null;
  329. }
  330. }