Search in sources :

Example 1 with CoreComponent

use of com.biglybt.core.CoreComponent in project BiglyBT by BiglySoftware.

the class TagManagerImpl method init.

private void init() {
    if (!enabled) {
        return;
    }
    Core core = CoreFactory.getSingleton();
    final TagPropertyTrackerHandler auto_tracker = new TagPropertyTrackerHandler(core, this);
    untagged_handler = new TagPropertyUntaggedHandler(core, this);
    new TagPropertyTrackerTemplateHandler(core, this);
    constraint_handler = new TagPropertyConstraintHandler(core, this);
    core.addLifecycleListener(new CoreLifecycleAdapter() {

        @Override
        public void started(Core core) {
            core.getPluginManager().getDefaultPluginInterface().getDownloadManager().getGlobalDownloadEventNotifier().addCompletionListener(TagManagerImpl.this);
        }

        @Override
        public void componentCreated(Core core, CoreComponent component) {
            if (component instanceof GlobalManager) {
                GlobalManager global_manager = (GlobalManager) component;
                global_manager.addDownloadManagerInitialisationAdapter(new DownloadManagerInitialisationAdapter() {

                    @Override
                    public int getActions() {
                        return (ACT_ASSIGNS_TAGS);
                    }

                    @Override
                    public void initialised(DownloadManager manager, boolean for_seeding) {
                        com.biglybt.core.disk.DiskManagerFileInfo[] files = manager.getDiskManagerFileInfoSet().getFiles();
                        for (com.biglybt.core.disk.DiskManagerFileInfo file : files) {
                            if (file.getTorrentFile().getPathComponents().length == 1) {
                                String name = file.getTorrentFile().getRelativePath().toLowerCase(Locale.US);
                                if (name.equals("index.html") || name.equals("index.htm")) {
                                    TagType tt = TagManagerFactory.getTagManager().getTagType(TagType.TT_DOWNLOAD_MANUAL);
                                    String tag_name = "Websites";
                                    Tag tag = tt.getTag(tag_name, true);
                                    try {
                                        if (tag == null) {
                                            tag = tt.createTag(tag_name, true);
                                        }
                                        if (!tag.hasTaggable(manager)) {
                                            tag.addTaggable(manager);
                                            tag.setDescription(MessageText.getString("tag.website.desc"));
                                        }
                                    } catch (Throwable e) {
                                        Debug.out(e);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                });
                global_manager.addDownloadManagerInitialisationAdapter(new DownloadManagerInitialisationAdapter() {

                    @Override
                    public int getActions() {
                        return (ACT_PROCESSES_TAGS);
                    }

                    @Override
                    public void initialised(DownloadManager manager, boolean for_seeding) {
                        if (for_seeding) {
                            return;
                        }
                        // perform any auto-tagging - note that auto-tags aren't applied to the download
                        // yet
                        List<Tag> auto_tags = auto_tracker.getTagsForDownload(manager);
                        Set<Tag> tags = new HashSet<>(getTagsForTaggable(TagType.TT_DOWNLOAD_MANUAL, manager));
                        tags.addAll(auto_tags);
                        if (tags.size() == 0) {
                            // pick up untagged tags here as they haven't been added yet :(
                            tags.addAll(untagged_handler.getUntaggedTags());
                        }
                        if (tags.size() > 0) {
                            List<Tag> sl_tags = new ArrayList<>();
                            for (Tag tag : tags) {
                                TagFeatureFileLocation fl = (TagFeatureFileLocation) tag;
                                if (fl.supportsTagInitialSaveFolder()) {
                                    File save_loc = fl.getTagInitialSaveFolder();
                                    if (save_loc != null) {
                                        sl_tags.add(tag);
                                    }
                                }
                            }
                            if (sl_tags.size() > 0) {
                                if (sl_tags.size() > 1) {
                                    Collections.sort(sl_tags, new Comparator<Tag>() {

                                        @Override
                                        public int compare(Tag o1, Tag o2) {
                                            return (o1.getTagID() - o2.getTagID());
                                        }
                                    });
                                }
                                TagFeatureFileLocation tag = (TagFeatureFileLocation) sl_tags.get(0);
                                long options = tag.getTagInitialSaveOptions();
                                boolean set_data = (options & TagFeatureFileLocation.FL_DATA) != 0;
                                boolean set_torrent = (options & TagFeatureFileLocation.FL_TORRENT) != 0;
                                File new_loc = tag.getTagInitialSaveFolder();
                                if (set_data) {
                                    File old_loc = manager.getSaveLocation();
                                    if (!new_loc.equals(old_loc)) {
                                        manager.setTorrentSaveDir(new_loc.getAbsolutePath());
                                    }
                                }
                                if (set_torrent) {
                                    File old_torrent_file = new File(manager.getTorrentFileName());
                                    if (old_torrent_file.exists()) {
                                        try {
                                            manager.setTorrentFile(new_loc, old_torrent_file.getName());
                                        } catch (Throwable e) {
                                            Debug.out(e);
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }

        @Override
        public void stopped(Core core) {
            destroy();
        }
    });
    SimpleTimer.addPeriodicEvent("TM:Sync", 30 * 1000, new TimerEventPerformer() {

        @Override
        public void perform(TimerEvent event) {
            for (TagType tt : tag_types) {
                ((TagTypeBase) tt).sync();
            }
        }
    });
}
Also used : CoreLifecycleAdapter(com.biglybt.core.CoreLifecycleAdapter) DownloadManager(com.biglybt.core.download.DownloadManager) GlobalManager(com.biglybt.core.global.GlobalManager) CoreComponent(com.biglybt.core.CoreComponent) Core(com.biglybt.core.Core) DiskManagerFileInfo(com.biglybt.pif.disk.DiskManagerFileInfo) DownloadManagerInitialisationAdapter(com.biglybt.core.download.DownloadManagerInitialisationAdapter) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File)

Example 2 with CoreComponent

use of com.biglybt.core.CoreComponent in project BiglyBT by BiglySoftware.

the class UI method coreCreated.

@Override
public void coreCreated(Core core) {
    super.coreCreated(core);
    UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
    if (uiFunctions != null) {
        openQueuedTorrents();
    } else {
        core.addLifecycleListener(new CoreLifecycleAdapter() {

            @Override
            public void componentCreated(Core core, CoreComponent component) {
                if (component instanceof UIFunctionsSWT) {
                    openQueuedTorrents();
                    core.removeLifecycleListener(this);
                }
            }
        });
    }
}
Also used : CoreLifecycleAdapter(com.biglybt.core.CoreLifecycleAdapter) UIFunctions(com.biglybt.ui.UIFunctions) CoreComponent(com.biglybt.core.CoreComponent) Core(com.biglybt.core.Core)

Aggregations

Core (com.biglybt.core.Core)2 CoreComponent (com.biglybt.core.CoreComponent)2 CoreLifecycleAdapter (com.biglybt.core.CoreLifecycleAdapter)2 DownloadManager (com.biglybt.core.download.DownloadManager)1 DownloadManagerInitialisationAdapter (com.biglybt.core.download.DownloadManagerInitialisationAdapter)1 GlobalManager (com.biglybt.core.global.GlobalManager)1 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)1 DiskManagerFileInfo (com.biglybt.pif.disk.DiskManagerFileInfo)1 UIFunctions (com.biglybt.ui.UIFunctions)1 File (java.io.File)1