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<Long, PlatformMetaSearchMessenger.templateInfo>();
Map<Long,PlatformMetaSearchMessenger.templateInfo> vuze_preload_ids = new HashMap<Long,PlatformMetaSearchMessenger.templateInfo>();
Set<Long> featured_ids = new HashSet<Long>();
Set<Long> popular_ids = new HashSet<Long>();
Set<Long> manual_vuze_ids = new HashSet<Long>();
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<Long,Engine>();
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 );
}
}
}
}
// deselect any not in use
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 );
}
}
// finally pick up any unreported selection changes and re-affirm positive selections
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;