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.

TransferPanel.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.dcc.ui;
  18. import com.dmdirc.addons.dcc.DCCTransferHandler;
  19. import com.dmdirc.addons.dcc.TransferContainer;
  20. import com.dmdirc.addons.dcc.io.DCCTransfer;
  21. import com.dmdirc.addons.ui_swing.UIUtilities;
  22. import com.dmdirc.addons.ui_swing.components.frames.SwingFrameComponent;
  23. import com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.interfaces.EventBus;
  25. import com.dmdirc.interfaces.WindowModel;
  26. import com.dmdirc.parser.events.SocketCloseEvent;
  27. import com.dmdirc.parser.interfaces.Parser;
  28. import com.dmdirc.util.DateUtils;
  29. import java.awt.Desktop;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import javax.swing.JButton;
  35. import javax.swing.JLabel;
  36. import javax.swing.JPanel;
  37. import javax.swing.JProgressBar;
  38. import net.miginfocom.swing.MigLayout;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import net.engio.mbassy.listener.Handler;
  42. import static com.dmdirc.util.LogUtils.USER_ERROR;
  43. /**
  44. * A panel for displaying the progress of DCC transfers.
  45. *
  46. * @since 0.6.6
  47. */
  48. public class TransferPanel extends JPanel implements ActionListener,
  49. DCCTransferHandler, SwingFrameComponent {
  50. private static final Logger LOG = LoggerFactory.getLogger(TransferPanel.class);
  51. /** A version number for this class. */
  52. private static final long serialVersionUID = 1L;
  53. /** Parent container. */
  54. private final TransferContainer transferContainer;
  55. /** Progress Bar */
  56. private final JProgressBar progress = new JProgressBar();
  57. /** Status Label */
  58. private final JLabel status = new JLabel("Status: Waiting");
  59. /** Speed Label */
  60. private final JLabel speed = new JLabel("Speed: Unknown");
  61. /** Time Label */
  62. private final JLabel remaining = new JLabel("Time Remaining: Unknown");
  63. /** Time Taken */
  64. private final JLabel taken = new JLabel("Time Taken: 00:00");
  65. /** Button */
  66. private final JButton button = new JButton("Cancel");
  67. /** Open Button */
  68. private final JButton openButton = new JButton("Open");
  69. /** The transfer that this window is showing. */
  70. private final DCCTransfer dcc;
  71. /** The event bus to post errors. */
  72. private final EventBus errorBus;
  73. /**
  74. * Creates a new transfer window for the specified UI controller and owner.
  75. *
  76. * @param owner The frame container that owns this frame
  77. * @param errorBus The event bus to post errors to
  78. */
  79. public TransferPanel(final WindowModel owner, final EventBus errorBus) {
  80. this.transferContainer = (TransferContainer) owner;
  81. this.errorBus = errorBus;
  82. dcc = transferContainer.getDCC();
  83. dcc.addHandler(this);
  84. transferContainer.getConnection()
  85. .flatMap(Connection::getParser)
  86. .map(Parser::getCallbackManager)
  87. .ifPresent(cm -> cm.subscribe(this));
  88. setLayout(new MigLayout("hidemode 0"));
  89. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  90. add(new JLabel("Sending: " + dcc.getShortFileName()), "wrap");
  91. add(new JLabel("To: " + transferContainer
  92. .getOtherNickname()), "wrap");
  93. } else {
  94. add(new JLabel("Receiving: " + dcc.getShortFileName()), "wrap");
  95. add(new JLabel("From: " + transferContainer
  96. .getOtherNickname()), "wrap");
  97. }
  98. add(status, "wrap");
  99. add(speed, "wrap");
  100. add(remaining, "wrap");
  101. add(taken, "wrap");
  102. add(progress, "growx, wrap");
  103. button.addActionListener(this);
  104. openButton.addActionListener(this);
  105. openButton.setVisible(false);
  106. add(openButton, "split 2, align right");
  107. add(button, "align right");
  108. }
  109. @Override
  110. public void actionPerformed(final ActionEvent e) {
  111. if (e.getActionCommand().equals("Cancel")) {
  112. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  113. button.setText("Resend");
  114. } else {
  115. button.setText("Close Window");
  116. }
  117. status.setText("Status: Cancelled");
  118. dcc.close();
  119. } else if (e.getActionCommand().equals("Resend")) {
  120. button.setText("Cancel");
  121. status.setText("Status: Resending...");
  122. if (!(transferContainer.resend())) {
  123. status.setText("Status: Resend failed.");
  124. button.setText("Close Window");
  125. }
  126. } else if (e.getActionCommand().equals("Close Window")) {
  127. transferContainer.close();
  128. } else if (e.getSource() == openButton) {
  129. final File file = new File(dcc.getFileName());
  130. try {
  131. Desktop.getDesktop().open(file);
  132. } catch (IllegalArgumentException ex) {
  133. LOG.info(USER_ERROR, "Unable to open file {}", file.getAbsolutePath(), ex);
  134. openButton.setEnabled(false);
  135. } catch (IOException ex) {
  136. try {
  137. Desktop.getDesktop().open(file.getParentFile());
  138. } catch (IllegalArgumentException ex1) {
  139. LOG.info(USER_ERROR, "Unable to open folder: {}",
  140. file.getParentFile().getAbsolutePath(), ex1);
  141. openButton.setEnabled(false);
  142. } catch (IOException ex1) {
  143. LOG.info(USER_ERROR, "No associated handler to open file or directory.", ex1);
  144. openButton.setEnabled(false);
  145. }
  146. }
  147. }
  148. }
  149. @Handler
  150. public void onSocketClosed(final SocketCloseEvent event) {
  151. if ("Resend".equals(button.getText())) {
  152. button.setText("Close Window");
  153. }
  154. }
  155. @Override
  156. public void socketClosed(final DCCTransfer dcc) {
  157. UIUtilities.invokeLater(() -> {
  158. if (transferContainer.isComplete()) {
  159. status.setText("Status: Transfer Complete.");
  160. if (transferContainer.shouldShowOpenButton()) {
  161. openButton.setVisible(true);
  162. }
  163. progress.setValue(100);
  164. button.setText("Close Window");
  165. } else {
  166. status.setText("Status: Transfer Failed.");
  167. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  168. button.setText("Resend");
  169. } else {
  170. button.setText("Close Window");
  171. }
  172. }
  173. });
  174. }
  175. @Override
  176. public void socketOpened(final DCCTransfer dcc) {
  177. UIUtilities.invokeLater(() -> status.setText("Status: Socket Opened"));
  178. }
  179. @Override
  180. public void dataTransferred(final DCCTransfer dcc, final int bytes) {
  181. UIUtilities.invokeLater(() -> {
  182. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  183. status.setText("Status: Sending");
  184. } else {
  185. status.setText("Status: Receiving");
  186. }
  187. progress.setValue((int) transferContainer.getPercent());
  188. final double bytesPerSecond = transferContainer
  189. .getBytesPerSecond();
  190. if (bytesPerSecond > 1048576) {
  191. speed.setText(String.format("Speed: %.2f MiB/s",
  192. bytesPerSecond / 1048576));
  193. } else if (bytesPerSecond > 1024) {
  194. speed.setText(String.format("Speed: %.2f KiB/s",
  195. bytesPerSecond / 1024));
  196. } else {
  197. speed.setText(String.format("Speed: %.2f B/s",
  198. bytesPerSecond));
  199. }
  200. remaining.setText(String.format("Time Remaining: %s",
  201. DateUtils.formatDurationAsTime((int) transferContainer.getRemainingTime())));
  202. taken.setText(String.format("Time Taken: %s", transferContainer
  203. .getStartTime() == 0 ? "N/A" : DateUtils.formatDurationAsTime(
  204. (int) transferContainer.getElapsedTime())));
  205. });
  206. }
  207. }