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.

DCCSendWindow.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Server;
  24. import com.dmdirc.ServerState;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.addons.dcc.actions.DCCActions;
  27. import com.dmdirc.config.IdentityManager;
  28. import com.dmdirc.parser.interfaces.Parser;
  29. import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.io.File;
  33. import javax.swing.JButton;
  34. import javax.swing.JLabel;
  35. import javax.swing.JOptionPane;
  36. import javax.swing.JProgressBar;
  37. import net.miginfocom.swing.MigLayout;
  38. /**
  39. * This class links DCC Send objects to a window.
  40. *
  41. * @author Shane 'Dataforce' McCormack
  42. */
  43. public class DCCSendWindow extends DCCFrame implements DCCSendInterface, ActionListener, SocketCloseListener {
  44. /** The DCCSend object we are a window for */
  45. private final DCCSend dcc;
  46. /** Other Nickname */
  47. private final String otherNickname;
  48. /** Total data transfered */
  49. private volatile long transferCount = 0;
  50. /** Time Started */
  51. private long timeStarted = 0;
  52. /** Progress Bar */
  53. private final JProgressBar progress = new JProgressBar();
  54. /** Status Label */
  55. private final JLabel status = new JLabel("Status: Waiting");
  56. /** Speed Label */
  57. private final JLabel speed = new JLabel("Speed: Unknown");
  58. /** Time Label */
  59. private final JLabel remaining = new JLabel("Time Remaining: Unknown");
  60. /** Time Taken */
  61. private final JLabel taken = new JLabel("Time Taken: 00:00");
  62. /** Button */
  63. private final JButton button = new JButton("Cancel");
  64. /** Plugin that this send belongs to. */
  65. private final DCCPlugin myPlugin;
  66. /** IRC Parser that caused this send */
  67. private Parser parser = null;
  68. /** Server that caused this send */
  69. private Server server = null;
  70. /**
  71. * Creates a new instance of DCCSendWindow with a given DCCSend object.
  72. *
  73. * @param plugin the DCC Plugin responsible for this window
  74. * @param dcc The DCCSend object this window wraps around
  75. * @param title The title of this window
  76. * @param targetNick Nickname of target
  77. * @param server The server that initiated this send
  78. */
  79. public DCCSendWindow(final DCCPlugin plugin, final DCCSend dcc, final String title, final String targetNick, final Server server) {
  80. super(plugin, title, dcc.getType() == DCCSend.TransferType.SEND ? "dcc-send-inactive" : "dcc-receive-inactive");
  81. this.dcc = dcc;
  82. this.server = server;
  83. this.parser = server == null ? null : server.getParser();
  84. this.myPlugin = plugin;
  85. if (parser != null) {
  86. parser.getCallbackManager().addNonCriticalCallback(SocketCloseListener.class, this);
  87. }
  88. dcc.setHandler(this);
  89. otherNickname = targetNick;
  90. getContentPane().setLayout(new MigLayout());
  91. progress.setMinimum(0);
  92. progress.setMaximum(100);
  93. progress.setStringPainted(true);
  94. progress.setValue(0);
  95. if (dcc.getType() == DCCSend.TransferType.SEND) {
  96. getContentPane().add(new JLabel("Sending: " + dcc.getShortFileName()), "wrap");
  97. getContentPane().add(new JLabel("To: " + targetNick), "wrap");
  98. } else {
  99. getContentPane().add(new JLabel("Recieving: " + dcc.getShortFileName()), "wrap");
  100. getContentPane().add(new JLabel("From: " + targetNick), "wrap");
  101. }
  102. getContentPane().add(status, "wrap");
  103. getContentPane().add(speed, "wrap");
  104. getContentPane().add(remaining, "wrap");
  105. getContentPane().add(taken, "wrap");
  106. getContentPane().add(progress, "growx, wrap");
  107. button.addActionListener(this);
  108. getContentPane().add(button, "wrap, align right");
  109. plugin.addWindow(this);
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public void onSocketClosed(final Parser tParser) {
  114. // Remove our reference to the parser (and its reference to us)
  115. parser.getCallbackManager().delAllCallback(this);
  116. parser = null;
  117. // Can't resend without the parser.
  118. if ("Resend".equals(button.getText())) {
  119. button.setText("Close Window");
  120. }
  121. }
  122. /**
  123. * Get the DCCSend Object associated with this window
  124. *
  125. * @return The DCCSend Object associated with this window
  126. */
  127. public DCCSend getDCC() {
  128. return dcc;
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public void actionPerformed(final ActionEvent e) {
  133. if (e.getActionCommand().equals("Cancel")) {
  134. if (dcc.getType() == DCCSend.TransferType.SEND) {
  135. button.setText("Resend");
  136. } else {
  137. button.setText("Close Window");
  138. }
  139. status.setText("Status: Cancelled");
  140. dcc.close();
  141. } else if (e.getActionCommand().equals("Resend")) {
  142. button.setText("Cancel");
  143. status.setText("Status: Resending...");
  144. synchronized (this) {
  145. transferCount = 0;
  146. }
  147. dcc.reset();
  148. if (parser != null && server.getState() == ServerState.CONNECTED) {
  149. final String myNickname = parser.getLocalClient().getNickname();
  150. // Check again incase we have changed nickname to the same nickname that
  151. // this send is for.
  152. if (parser.getStringConverter().equalsIgnoreCase(otherNickname, myNickname)) {
  153. final Thread errorThread = new Thread(new Runnable() {
  154. /** {@inheritDoc} */
  155. @Override
  156. public void run() {
  157. JOptionPane.showMessageDialog(null, "You can't DCC yourself.", "DCC Error", JOptionPane.ERROR_MESSAGE);
  158. }
  159. });
  160. errorThread.start();
  161. return;
  162. } else {
  163. if (IdentityManager.getGlobalConfig().getOptionBool(plugin.getDomain(), "send.reverse")) {
  164. parser.sendCTCP(otherNickname, "DCC", "SEND \"" + (new File(dcc.getFileName())).getName() + "\" " + DCC.ipToLong(myPlugin.getListenIP(parser)) + " 0 " + dcc.getFileSize() + " " + dcc.makeToken() + ((dcc.isTurbo()) ? " T" : ""));
  165. return;
  166. } else if (plugin.listen(dcc)) {
  167. parser.sendCTCP(otherNickname, "DCC", "SEND \"" + (new File(dcc.getFileName())).getName() + "\" " + DCC.ipToLong(myPlugin.getListenIP(parser)) + " " + dcc.getPort() + " " + dcc.getFileSize() + ((dcc.isTurbo()) ? " T" : ""));
  168. return;
  169. }
  170. }
  171. } else {
  172. status.setText("Status: Resend failed.");
  173. button.setText("Close Window");
  174. }
  175. } else if (e.getActionCommand().equals("Close Window")) {
  176. close();
  177. }
  178. }
  179. /**
  180. * Called when data is sent/recieved
  181. *
  182. * @param dcc The DCCSend that this message is from
  183. * @param bytes The number of new bytes that were transfered
  184. */
  185. @Override
  186. public void dataTransfered(final DCCSend dcc, final int bytes) {
  187. final double percent;
  188. synchronized (this) {
  189. transferCount += bytes;
  190. percent = (100.00 / dcc.getFileSize()) * (transferCount + dcc.getFileStart());
  191. }
  192. if (dcc.getType() == DCCSend.TransferType.SEND) {
  193. status.setText("Status: Sending");
  194. } else {
  195. status.setText("Status: Recieving");
  196. }
  197. updateSpeedAndTime();
  198. progress.setValue((int) Math.floor(percent));
  199. ActionManager.processEvent(DCCActions.DCC_SEND_DATATRANSFERED, null, this, bytes);
  200. }
  201. /**
  202. * Update the transfer speed, time remaining and time taken labels.
  203. */
  204. public void updateSpeedAndTime() {
  205. final long time = (System.currentTimeMillis() - timeStarted) / 1000;
  206. final double bytesPerSecond;
  207. synchronized (this) {
  208. bytesPerSecond = (time > 0) ? (transferCount / time) : transferCount;
  209. }
  210. if (bytesPerSecond > 1048576) {
  211. speed.setText(String.format("Speed: %.2f MB/s", (bytesPerSecond / 1048576)));
  212. } else if (bytesPerSecond > 1024) {
  213. speed.setText(String.format("Speed: %.2f KB/s", (bytesPerSecond / 1024)));
  214. } else {
  215. speed.setText(String.format("Speed: %f B/s", bytesPerSecond));
  216. }
  217. final long remaningBytes;
  218. synchronized (this) {
  219. remaningBytes = dcc.getFileSize() - dcc.getFileStart() - transferCount;
  220. }
  221. final double remainingSeconds = (bytesPerSecond > 0) ? (remaningBytes / bytesPerSecond) : 1;
  222. remaining.setText(String.format("Time Remaining: %s", duration((int) Math.floor(remainingSeconds))));
  223. taken.setText(String.format("Time Taken: %s", timeStarted == 0 ? "N/A" : duration(time)));
  224. }
  225. /**
  226. * Get the duration in seconds as a string.
  227. *
  228. * @param secondsInput to get duration for
  229. * @return Duration as a string
  230. */
  231. private String duration(final long secondsInput) {
  232. final StringBuilder result = new StringBuilder();
  233. final long hours = (secondsInput / 3600);
  234. final long minutes = (secondsInput / 60 % 60);
  235. final long seconds = (secondsInput % 60);
  236. if (hours > 0) {
  237. result.append(hours + ":");
  238. }
  239. result.append(String.format("%0,2d:%0,2d", minutes, seconds));
  240. return result.toString();
  241. }
  242. /**
  243. * Called when the socket is closed
  244. *
  245. * @param dcc The DCCSend that this message is from
  246. */
  247. @Override
  248. public void socketClosed(final DCCSend dcc) {
  249. ActionManager.processEvent(DCCActions.DCC_SEND_SOCKETCLOSED, null, this);
  250. if (!isWindowClosing()) {
  251. synchronized (this) {
  252. if (transferCount == dcc.getFileSize()) {
  253. status.setText("Status: Transfer Compelete.");
  254. progress.setValue(100);
  255. setIcon(dcc.getType() == DCCSend.TransferType.SEND ? "dcc-send-done" : "dcc-receive-done");
  256. button.setText("Close Window");
  257. } else {
  258. status.setText("Status: Transfer Failed.");
  259. setIcon(dcc.getType() == DCCSend.TransferType.SEND ? "dcc-send-failed" : "dcc-receive-failed");
  260. if (dcc.getType() == DCCSend.TransferType.SEND) {
  261. button.setText("Resend");
  262. } else {
  263. button.setText("Close Window");
  264. }
  265. }
  266. }
  267. updateSpeedAndTime();
  268. }
  269. }
  270. /**
  271. * Called when the socket is opened
  272. *
  273. * @param dcc The DCCSend that this message is from
  274. */
  275. @Override
  276. public void socketOpened(final DCCSend dcc) {
  277. ActionManager.processEvent(DCCActions.DCC_SEND_SOCKETOPENED, null, this);
  278. status.setText("Status: Socket Opened");
  279. timeStarted = System.currentTimeMillis();
  280. setIcon(dcc.getType() == DCCSend.TransferType.SEND ? "dcc-send-active" : "dcc-receive-active");
  281. }
  282. /**
  283. * Closes this container (and it's associated frame).
  284. */
  285. @Override
  286. public void windowClosing() {
  287. super.windowClosing();
  288. dcc.removeFromSends();
  289. dcc.close();
  290. }
  291. }