Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TransferContainer.java 12KB

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