Search in sources :

Example 1 with TrackerTorrentWillBeRemovedListener

use of com.biglybt.pif.tracker.TrackerTorrentWillBeRemovedListener in project BiglyBT by BiglySoftware.

the class ShareHosterPlugin method resourceAdded.

@Override
public void resourceAdded(final ShareResource resource) {
    log.log(LoggerChannel.LT_INFORMATION, "Resource added:".concat(resource.getName()));
    try {
        resource.addDeletionListener(new ShareResourceWillBeDeletedListener() {

            @Override
            public void resourceWillBeDeleted(ShareResource resource) throws ShareResourceDeletionVetoException {
                canResourceBeDeleted(resource);
            }
        });
        Download new_download = null;
        int type = resource.getType();
        if (type == ShareResource.ST_FILE) {
            ShareResourceFile file_resource = (ShareResourceFile) resource;
            ShareItem item = file_resource.getItem();
            Torrent torrent = item.getTorrent();
            Download download = download_manager.getDownload(torrent);
            if (download == null) {
                new_download = addDownload(resource, torrent, item.getTorrentFile(), file_resource.getFile());
            }
        } else if (type == ShareResource.ST_DIR) {
            ShareResourceDir dir_resource = (ShareResourceDir) resource;
            ShareItem item = dir_resource.getItem();
            Torrent torrent = item.getTorrent();
            Download download = download_manager.getDownload(torrent);
            if (download == null) {
                new_download = addDownload(resource, torrent, item.getTorrentFile(), dir_resource.getDir());
            }
        }
        if (new_download != null) {
            final Download f_new_download = new_download;
            resource_dl_map.put(resource, new_download);
            resource.addChangeListener(new ShareResourceListener() {

                @Override
                public void shareResourceChanged(ShareResource resource, ShareResourceEvent event) {
                    if (event.getType() == ShareResourceEvent.ET_ATTRIBUTE_CHANGED) {
                        TorrentAttribute attribute = (TorrentAttribute) event.getData();
                        // System.out.println( "sh: res -> ds: " + attribute.getName() + "/" + resource.getAttribute( attribute ));
                        f_new_download.setAttribute(attribute, resource.getAttribute(attribute));
                    }
                }
            });
            TorrentAttribute[] attributes = resource.getAttributes();
            for (int i = 0; i < attributes.length; i++) {
                TorrentAttribute ta = attributes[i];
                new_download.setAttribute(ta, resource.getAttribute(ta));
            }
            new_download.addAttributeListener(new DownloadAttributeListener() {

                @Override
                public void attributeEventOccurred(Download d, TorrentAttribute attr, int event_type) {
                    resource.setAttribute(attr, d.getAttribute(attr));
                }
            }, plugin_interface.getTorrentManager().getAttribute(TorrentAttribute.TA_CATEGORY), DownloadAttributeListener.WRITTEN);
            boolean persistent = resource.isPersistent();
            Torrent dl_torrent = new_download.getTorrent();
            if (dl_torrent != null) {
                TrackerTorrent tt = tracker.host(dl_torrent, persistent);
                if (!persistent) {
                    tt.addRemovalListener(new TrackerTorrentWillBeRemovedListener() {

                        @Override
                        public void torrentWillBeRemoved(TrackerTorrent tt) throws TrackerTorrentRemovalVetoException {
                            if (tt != torrent_being_removed) {
                                throw (new TrackerTorrentRemovalVetoException(MessageText.getString("plugin.sharing.torrent.remove.veto")));
                            }
                        }
                    });
                }
                resource_tt_map.put(resource, tt);
            }
            if (!persistent) {
                new_download.addDownloadWillBeRemovedListener(new DownloadWillBeRemovedListener() {

                    @Override
                    public void downloadWillBeRemoved(Download dl) throws DownloadRemovalVetoException {
                        if (dl != download_being_removed) {
                            throw (new DownloadRemovalVetoException(MessageText.getString("plugin.sharing.download.remove.veto")));
                        }
                    }
                });
            }
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) TrackerTorrentWillBeRemovedListener(com.biglybt.pif.tracker.TrackerTorrentWillBeRemovedListener) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) TorrentAttribute(com.biglybt.pif.torrent.TorrentAttribute) TrackerTorrentRemovalVetoException(com.biglybt.pif.tracker.TrackerTorrentRemovalVetoException)

Aggregations

Torrent (com.biglybt.pif.torrent.Torrent)1 TorrentAttribute (com.biglybt.pif.torrent.TorrentAttribute)1 TrackerTorrent (com.biglybt.pif.tracker.TrackerTorrent)1 TrackerTorrentRemovalVetoException (com.biglybt.pif.tracker.TrackerTorrentRemovalVetoException)1 TrackerTorrentWillBeRemovedListener (com.biglybt.pif.tracker.TrackerTorrentWillBeRemovedListener)1