Search in sources :

Example 1 with DownloadAttributeListener

use of com.biglybt.pif.download.DownloadAttributeListener in project BiglyBT by BiglySoftware.

the class PlatformContentDirectory method lookupContentFile.

@Override
public ContentFile lookupContentFile(Map attributes) {
    byte[] hash = (byte[]) attributes.get(AT_BTIH);
    int index = ((Integer) attributes.get(AT_FILE_INDEX)).intValue();
    try {
        Download download = PluginInitializer.getDefaultInterface().getDownloadManager().getDownload(hash);
        if (download == null) {
            return (null);
        }
        Torrent t_torrent = download.getTorrent();
        if (t_torrent == null) {
            return (null);
        }
        String ud_key = "PlatformContentDirectory" + ":" + index;
        ContentFile acf = (ContentFile) download.getUserData(ud_key);
        if (acf != null) {
            return (acf);
        }
        final DiskManagerFileInfo file = download.getDiskManagerFileInfo(index);
        acf = new ContentFile() {

            @Override
            public DiskManagerFileInfo getFile() {
                return (file);
            }

            @Override
            public Object getProperty(String name) {
                try {
                    if (name.equals(PT_DATE)) {
                        return (new Long(file.getDownload().getCreationTime()));
                    } else if (name.equals(PT_CATEGORIES)) {
                        try {
                            String cat = file.getDownload().getCategoryName();
                            if (cat != null && cat.length() > 0) {
                                if (!cat.equalsIgnoreCase("Categories.uncategorized")) {
                                    return (new String[] { cat });
                                }
                            }
                        } catch (Throwable e) {
                        }
                        return (new String[0]);
                    } else if (name.equals(PT_TAGS)) {
                        List<Tag> tags = TagManagerFactory.getTagManager().getTagsForTaggable(PluginCoreUtils.unwrap(file.getDownload()));
                        List<String> tag_names = new ArrayList<>();
                        for (Tag tag : tags) {
                            if (tag.getTagType().getTagType() == TagType.TT_DOWNLOAD_MANUAL) {
                                tag_names.add(tag.getTagName(true));
                            }
                        }
                        return (tag_names.toArray(new String[tag_names.size()]));
                    } else if (name.equals(PT_PERCENT_DONE)) {
                        long size = file.getLength();
                        return (new Long(size == 0 ? 100 : (1000 * file.getDownloaded() / size)));
                    } else if (name.equals(PT_ETA)) {
                        return (getETA(file));
                    }
                } catch (Throwable e) {
                }
                return (null);
            }
        };
        download.setUserData(ud_key, acf);
        final ContentFile f_acf = acf;
        download.addAttributeListener(new DownloadAttributeListener() {

            @Override
            public void attributeEventOccurred(Download download, TorrentAttribute attribute, int eventType) {
                fireCatsChanged(f_acf);
            }
        }, ta_category, DownloadAttributeListener.WRITTEN);
        TagManagerFactory.getTagManager().getTagType(TagType.TT_DOWNLOAD_MANUAL).addTagListener(PluginCoreUtils.unwrap(download), new TagListener() {

            @Override
            public void taggableSync(Tag tag) {
            }

            @Override
            public void taggableRemoved(Tag tag, Taggable tagged) {
                update(tagged);
            }

            @Override
            public void taggableAdded(Tag tag, Taggable tagged) {
                update(tagged);
            }

            private void update(Taggable tagged) {
                fireTagsChanged(f_acf);
            }
        });
        return (acf);
    } catch (Throwable e) {
        return (null);
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) DiskManagerFileInfo(com.biglybt.pif.disk.DiskManagerFileInfo) TorrentAttribute(com.biglybt.pif.torrent.TorrentAttribute) DownloadAttributeListener(com.biglybt.pif.download.DownloadAttributeListener) List(java.util.List) ArrayList(java.util.ArrayList) CopyOnWriteList(com.biglybt.core.util.CopyOnWriteList) Download(com.biglybt.pif.download.Download)

Example 2 with DownloadAttributeListener

use of com.biglybt.pif.download.DownloadAttributeListener in project BiglyBT by BiglySoftware.

the class DeviceUPnPImpl method syncCategoriesAndTags.

protected void syncCategoriesAndTags(TranscodeFileImpl tf, boolean inherit_from_download) {
    try {
        final Download dl = tf.getSourceFile().getDownload();
        if (dl != null) {
            if (inherit_from_download) {
                setCategories(tf, dl);
                setTags(tf, dl);
            }
            final String tf_key = tf.getKey();
            dl.addAttributeListener(new DownloadAttributeListener() {

                @Override
                public void attributeEventOccurred(Download download, TorrentAttribute attribute, int eventType) {
                    TranscodeFileImpl tf = getTranscodeFile(tf_key);
                    if (tf != null) {
                        setCategories(tf, download);
                    } else {
                        dl.removeAttributeListener(this, upnp_manager.getCategoryAttibute(), DownloadAttributeListener.WRITTEN);
                    }
                }
            }, upnp_manager.getCategoryAttibute(), DownloadAttributeListener.WRITTEN);
            TagManagerFactory.getTagManager().getTagType(TagType.TT_DOWNLOAD_MANUAL).addTagListener(PluginCoreUtils.unwrap(dl), new TagListener() {

                @Override
                public void taggableSync(Tag tag) {
                }

                @Override
                public void taggableRemoved(Tag tag, Taggable tagged) {
                    update(tagged);
                }

                @Override
                public void taggableAdded(Tag tag, Taggable tagged) {
                    update(tagged);
                }

                private void update(Taggable tagged) {
                    TranscodeFileImpl tf = getTranscodeFile(tf_key);
                    if (tf != null) {
                        setTags(tf, dl);
                    } else {
                        TagManagerFactory.getTagManager().getTagType(TagType.TT_DOWNLOAD_MANUAL).removeTagListener(tagged, this);
                    }
                }
            });
        }
    } catch (Throwable e) {
    }
}
Also used : TorrentAttribute(com.biglybt.pif.torrent.TorrentAttribute) DownloadAttributeListener(com.biglybt.pif.download.DownloadAttributeListener) Download(com.biglybt.pif.download.Download) ContentDownload(com.biglybt.core.content.ContentDownload)

Aggregations

Download (com.biglybt.pif.download.Download)2 DownloadAttributeListener (com.biglybt.pif.download.DownloadAttributeListener)2 TorrentAttribute (com.biglybt.pif.torrent.TorrentAttribute)2 ContentDownload (com.biglybt.core.content.ContentDownload)1 CopyOnWriteList (com.biglybt.core.util.CopyOnWriteList)1 DiskManagerFileInfo (com.biglybt.pif.disk.DiskManagerFileInfo)1 Torrent (com.biglybt.pif.torrent.Torrent)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1