+ /**
+ * Starts a DCC download.
+ *
+ * @param dccSendReceived
+ * The DCC SEND event
+ */
+ @Subscribe
+ public void dccSendReceived(DccSendReceived dccSendReceived) {
+ Optional<Network> network = getNetwork(dccSendReceived.connection());
+ if (!network.isPresent()) {
+ return;
+ }
+
+ Download download = downloads.get(dccSendReceived.filename());
+ if (download == null) {
+ /* unknown download, ignore. */
+ return;
+ }
+
+ logger.info(String.format("Starting download of %s.", dccSendReceived.filename()));
+ try {
+ File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
+ OutputStream fileOutputStream = new FileOutputStream(outputFile);
+ DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
+ download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
+ dccReceivers.add(dccReceiver);
+ dccReceiver.start();
+ eventBus.post(new DownloadStarted(download));
+ } catch (FileNotFoundException fnfe1) {
+ logger.log(Level.WARNING, "Could not open file for download!", fnfe1);
+ }
+ }
+
+ /**
+ * Closes the output stream of the download and moves the file to the final
+ * location.
+ *
+ * @param dccDownloadFinished
+ * The DCC download finished event
+ */
+ @Subscribe
+ public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
+ Download download = downloads.get(dccDownloadFinished.dccReceiver().filename());
+ if (download == null) {
+ /* probably shouldn’t happen. */
+ return;
+ }
+
+ try {
+ download.outputStream().close();
+ File file = new File(download.filename());
+ file.renameTo(new File(finalDirectory, download.filename()));
+ eventBus.post(new DownloadFinished(download));
+ } catch (IOException ioe1) {
+ /* TODO - handle all the errors. */
+ logger.log(Level.WARNING, String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
+ }
+ }
+