Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TransferContainer.java 12KB

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