Search in sources :

Example 1 with CustomizationManager

use of com.biglybt.core.custom.CustomizationManager in project BiglyBT by BiglySoftware.

the class MetaSearchManagerImpl method syncRefresh.

protected void syncRefresh() throws MetaSearchException {
    boolean refresh_completed = false;
    boolean first_run = false;
    try {
        refresh_sem.reserve();
        first_run = COConfigurationManager.getBooleanParameter("metasearch.refresh.first_run", true);
        if (!checked_customization) {
            checked_customization = true;
            CustomizationManager cust_man = CustomizationManagerFactory.getSingleton();
            Customization cust = cust_man.getActiveCustomization();
            if (cust != null) {
                String cust_name = COConfigurationManager.getStringParameter("metasearch.custom.name", "");
                String cust_version = COConfigurationManager.getStringParameter("metasearch.custom.version", "0");
                boolean new_name = !cust_name.equals(cust.getName());
                boolean new_version = com.biglybt.core.util.Constants.compareVersions(cust_version, cust.getVersion()) < 0;
                if (new_name || new_version) {
                    log("Customization: checking templates for " + cust.getName() + "/" + cust.getVersion());
                    try {
                        InputStream[] streams = cust.getResources(Customization.RT_META_SEARCH_TEMPLATES);
                        if (streams.length > 0 && new_name) {
                            // reset engines
                            log("    setting auto-mode to false");
                            setAutoMode(false);
                        /*
								Engine[]	engines = meta_search.getEngines( false, false );

								for (int i=0;i<engines.length;i++){

									Engine engine = engines[i];

									if ( engine.getSelectionState()) == Engine.SEL_STATE_MANUAL_SELECTED ){

									}
								}
								*/
                        }
                        for (int i = 0; i < streams.length; i++) {
                            InputStream is = streams[i];
                            try {
                                VuzeFile vf = VuzeFileHandler.getSingleton().loadVuzeFile(is);
                                if (vf != null) {
                                    VuzeFileComponent[] comps = vf.getComponents();
                                    for (int j = 0; j < comps.length; j++) {
                                        VuzeFileComponent comp = comps[j];
                                        if (comp.getType() == VuzeFileComponent.COMP_TYPE_METASEARCH_TEMPLATE) {
                                            try {
                                                Engine e = getSingleton().importEngine(comp.getContent(), false);
                                                log("    updated " + e.getName());
                                                e.setSelectionState(Engine.SEL_STATE_MANUAL_SELECTED);
                                            } catch (Throwable e) {
                                                Debug.printStackTrace(e);
                                            }
                                        }
                                    }
                                }
                            } finally {
                                try {
                                    is.close();
                                } catch (Throwable e) {
                                }
                            }
                        }
                    } finally {
                        COConfigurationManager.setParameter("metasearch.custom.name", cust.getName());
                        COConfigurationManager.setParameter("metasearch.custom.version", cust.getVersion());
                    }
                }
            }
        }
        log("Refreshing engines");
        // featured templates are always shown - can't be deselected
        // popular ones are selected if in 'auto' mode
        // manually selected ones are, well, manually selected
        Map<Long, PlatformMetaSearchMessenger.templateInfo> vuze_selected_ids = new HashMap<>();
        Map<Long, PlatformMetaSearchMessenger.templateInfo> vuze_preload_ids = new HashMap<>();
        Set<Long> featured_ids = new HashSet<>();
        Set<Long> popular_ids = new HashSet<>();
        Set<Long> manual_vuze_ids = new HashSet<>();
        boolean auto_mode = isAutoMode();
        Engine[] engines = meta_search.getEngines(false, false);
        String fud = meta_search.getFUD();
        try {
            PlatformMetaSearchMessenger.templateInfo[] featured = PlatformMetaSearchMessenger.listFeaturedTemplates(extension_key, fud);
            String featured_str = "";
            for (int i = 0; i < featured.length; i++) {
                PlatformMetaSearchMessenger.templateInfo template = featured[i];
                if (!template.isVisible()) {
                    continue;
                }
                Long key = new Long(template.getId());
                vuze_selected_ids.put(key, template);
                featured_ids.add(key);
                featured_str += (featured_str.length() == 0 ? "" : ",") + key;
            }
            log("Featured templates: " + featured_str);
            if (auto_mode || first_run) {
                PlatformMetaSearchMessenger.templateInfo[] popular = PlatformMetaSearchMessenger.listTopPopularTemplates(extension_key, fud);
                String popular_str = "";
                String preload_str = "";
                for (int i = 0; i < popular.length; i++) {
                    PlatformMetaSearchMessenger.templateInfo template = popular[i];
                    if (!template.isVisible()) {
                        continue;
                    }
                    Long key = new Long(template.getId());
                    if (auto_mode) {
                        if (!vuze_selected_ids.containsKey(key)) {
                            vuze_selected_ids.put(key, template);
                            popular_ids.add(key);
                            popular_str += (popular_str.length() == 0 ? "" : ",") + key;
                        }
                    } else {
                        if (!vuze_preload_ids.containsKey(key)) {
                            vuze_preload_ids.put(key, template);
                            preload_str += (preload_str.length() == 0 ? "" : ",") + key;
                        }
                    }
                }
                log("Popular templates: " + popular_str);
                if (preload_str.length() > 0) {
                    log("Pre-load templates: " + popular_str);
                }
            }
            // pick up explicitly selected vuze ones
            String manual_str = "";
            for (int i = 0; i < engines.length; i++) {
                Engine engine = engines[i];
                Long key = new Long(engine.getId());
                if (engine.getSource() == Engine.ENGINE_SOURCE_VUZE && engine.getSelectionState() == Engine.SEL_STATE_MANUAL_SELECTED && !vuze_selected_ids.containsKey(key)) {
                    manual_vuze_ids.add(key);
                }
            }
            if (manual_vuze_ids.size() > 0) {
                long[] manual_ids = new long[manual_vuze_ids.size()];
                Iterator<Long> it = manual_vuze_ids.iterator();
                int pos = 0;
                while (it.hasNext()) {
                    manual_ids[pos++] = it.next().longValue();
                }
                PlatformMetaSearchMessenger.templateInfo[] manual = PlatformMetaSearchMessenger.getTemplateDetails(extension_key, manual_ids);
                for (int i = 0; i < manual.length; i++) {
                    PlatformMetaSearchMessenger.templateInfo template = manual[i];
                    if (!template.isVisible()) {
                        continue;
                    }
                    Long key = new Long(template.getId());
                    vuze_selected_ids.put(key, template);
                    manual_str += (manual_str.length() == 0 ? "" : ",") + key;
                }
            }
            log("Manual templates: " + manual_str);
            Map<Long, Engine> existing_engine_map = new HashMap<>();
            String existing_str = "";
            for (int i = 0; i < engines.length; i++) {
                Engine engine = engines[i];
                Long key = new Long(engine.getId());
                existing_engine_map.put(key, engine);
                existing_str += (existing_str.length() == 0 ? "" : ",") + key + "[source=" + Engine.ENGINE_SOURCE_STRS[engine.getSource()] + ",type=" + engine.getType() + ",selected=" + Engine.SEL_STATE_STRINGS[engine.getSelectionState()] + "]";
            }
            log("Existing templates: " + existing_str);
            // we've compiled a list of the engines we should have and their latest dates
            // update any that are out of date
            Iterator<Map.Entry<Long, PlatformMetaSearchMessenger.templateInfo>> it = vuze_selected_ids.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Long, PlatformMetaSearchMessenger.templateInfo> entry = it.next();
                vuze_preload_ids.remove(entry.getKey());
                long id = entry.getKey().longValue();
                PlatformMetaSearchMessenger.templateInfo template = entry.getValue();
                long modified = template.getModifiedDate();
                Engine this_engine = (Engine) existing_engine_map.get(new Long(id));
                boolean update = this_engine == null || this_engine.getLastUpdated() < modified;
                if (update) {
                    PlatformMetaSearchMessenger.templateDetails details = PlatformMetaSearchMessenger.getTemplate(extension_key, id);
                    log("Downloading definition of template " + id);
                    log(details.getValue());
                    if (details.isVisible()) {
                        try {
                            this_engine = meta_search.importFromJSONString(details.getType() == PlatformMetaSearchMessenger.templateDetails.ENGINE_TYPE_JSON ? Engine.ENGINE_TYPE_JSON : Engine.ENGINE_TYPE_REGEX, details.getId(), details.getModifiedDate(), details.getRankBias(), details.getName(), details.getValue());
                            this_engine.setSource(Engine.ENGINE_SOURCE_VUZE);
                            meta_search.addEngine(this_engine);
                        } catch (Throwable e) {
                            log("Failed to import engine '" + details.getValue() + "'", e);
                        }
                    }
                } else if (this_engine.getRankBias() != template.getRankBias()) {
                    this_engine.setRankBias(template.getRankBias());
                    log("Updating rank bias for " + this_engine.getString() + " to " + template.getRankBias());
                } else {
                    log("Not updating " + this_engine.getString() + " as unchanged");
                }
                if (this_engine != null) {
                    int sel_state = this_engine.getSelectionState();
                    if (sel_state == Engine.SEL_STATE_DESELECTED) {
                        log("Auto-selecting " + this_engine.getString());
                        this_engine.setSelectionState(Engine.SEL_STATE_AUTO_SELECTED);
                    } else if (auto_mode && sel_state == Engine.SEL_STATE_MANUAL_SELECTED) {
                        log("Switching Manual to Auto select for " + this_engine.getString());
                        this_engine.setSelectionState(Engine.SEL_STATE_AUTO_SELECTED);
                    }
                }
            }
            // do any pre-loads
            it = vuze_preload_ids.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Long, PlatformMetaSearchMessenger.templateInfo> entry = it.next();
                long id = ((Long) entry.getKey()).longValue();
                Engine this_engine = (Engine) existing_engine_map.get(new Long(id));
                if (this_engine == null) {
                    PlatformMetaSearchMessenger.templateDetails details = PlatformMetaSearchMessenger.getTemplate(extension_key, id);
                    log("Downloading pre-load definition of template " + id);
                    log(details.getValue());
                    if (details.isVisible()) {
                        try {
                            this_engine = meta_search.importFromJSONString(details.getType() == PlatformMetaSearchMessenger.templateDetails.ENGINE_TYPE_JSON ? Engine.ENGINE_TYPE_JSON : Engine.ENGINE_TYPE_REGEX, details.getId(), details.getModifiedDate(), details.getRankBias(), details.getName(), details.getValue());
                            this_engine.setSource(Engine.ENGINE_SOURCE_VUZE);
                            this_engine.setSelectionState(Engine.SEL_STATE_DESELECTED);
                            meta_search.addEngine(this_engine);
                        } catch (Throwable e) {
                            log("Failed to import engine '" + details.getValue() + "'", e);
                        }
                    }
                }
            }
            for (int i = 0; i < engines.length; i++) {
                Engine engine = engines[i];
                if (engine.getSource() == Engine.ENGINE_SOURCE_VUZE && engine.getSelectionState() == Engine.SEL_STATE_AUTO_SELECTED && !vuze_selected_ids.containsKey(new Long(engine.getId()))) {
                    log("Deselecting " + engine.getString() + " as no longer visible on Vuze");
                    engine.setSelectionState(Engine.SEL_STATE_DESELECTED);
                }
            }
            for (int i = 0; i < engines.length; i++) {
                Engine engine = engines[i];
                if (engine.getSource() == Engine.ENGINE_SOURCE_VUZE && engine.getSelectionState() == Engine.SEL_STATE_MANUAL_SELECTED) {
                    engine.recordSelectionState();
                } else {
                    engine.checkSelectionStateRecorded();
                }
            }
            refresh_completed = true;
        } catch (Throwable e) {
            log("Refresh failed", e);
            throw (new MetaSearchException("Refresh failed", e));
        }
    } finally {
        if (first_run && refresh_completed) {
            COConfigurationManager.setParameter("metasearch.refresh.first_run", false);
        }
        refresh_sem.release();
        initial_refresh_sem.releaseForever();
    }
}
Also used : VuzeFileComponent(com.biglybt.core.vuzefile.VuzeFileComponent) Customization(com.biglybt.core.custom.Customization) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) PluginEngine(com.biglybt.core.metasearch.impl.plugin.PluginEngine) InputStream(java.io.InputStream) PlatformMetaSearchMessenger(com.biglybt.core.messenger.config.PlatformMetaSearchMessenger) CustomizationManager(com.biglybt.core.custom.CustomizationManager)

Aggregations

Customization (com.biglybt.core.custom.Customization)1 CustomizationManager (com.biglybt.core.custom.CustomizationManager)1 PlatformMetaSearchMessenger (com.biglybt.core.messenger.config.PlatformMetaSearchMessenger)1 PluginEngine (com.biglybt.core.metasearch.impl.plugin.PluginEngine)1 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)1 VuzeFileComponent (com.biglybt.core.vuzefile.VuzeFileComponent)1 InputStream (java.io.InputStream)1