use of com.biglybt.core.download.DownloadManager in project BiglyBT by BiglySoftware.
the class TagDownloadWithState method getPerformableOperations.
@Override
public boolean[] getPerformableOperations(int[] ops) {
boolean[] result = new boolean[ops.length];
Set<DownloadManager> dms = getTaggedDownloads();
for (DownloadManager dm : dms) {
int dm_state = dm.getState();
for (int i = 0; i < ops.length; i++) {
if (result[i]) {
continue;
}
int op = ops[i];
if ((op & TagFeatureRunState.RSC_START) != 0) {
if (dm_state == DownloadManager.STATE_STOPPED || dm_state == DownloadManager.STATE_ERROR) {
result[i] = true;
}
}
if ((op & TagFeatureRunState.RSC_STOP) != 0) {
if (dm_state != DownloadManager.STATE_STOPPED && dm_state != DownloadManager.STATE_STOPPING && dm_state != DownloadManager.STATE_ERROR) {
result[i] = true;
}
}
if ((op & TagFeatureRunState.RSC_PAUSE) != 0) {
if (dm_state != DownloadManager.STATE_STOPPED && dm_state != DownloadManager.STATE_STOPPING && dm_state != DownloadManager.STATE_ERROR) {
if (!dm.isPaused()) {
result[i] = true;
}
}
}
if ((op & TagFeatureRunState.RSC_RESUME) != 0) {
if (dm.isPaused()) {
result[i] = true;
}
}
}
}
return (result);
}
use of com.biglybt.core.download.DownloadManager in project BiglyBT by BiglySoftware.
the class TagManagerImpl method taggableAdded.
public void taggableAdded(TagType tag_type, Tag tag, Taggable tagged) {
// hack to support initial-save-location logic when a user manually assigns a tag and the download
// hasn't had files allocated yet (most common scenario is user has 'add-torrent-stopped' set up)
int tt = tag_type.getTagType();
try {
if (tt == TagType.TT_DOWNLOAD_MANUAL && tagged instanceof DownloadManager) {
TagFeatureFileLocation fl = (TagFeatureFileLocation) tag;
if (fl.supportsTagInitialSaveFolder()) {
File save_loc = fl.getTagInitialSaveFolder();
if (save_loc != null) {
DownloadManager dm = (DownloadManager) tagged;
if (dm.getState() == DownloadManager.STATE_STOPPED) {
TOTorrent torrent = dm.getTorrent();
if (torrent != null) {
if (dm.getGlobalManager().getDownloadManager(torrent.getHashWrapper()) != null) {
long options = fl.getTagInitialSaveOptions();
boolean set_data = (options & TagFeatureFileLocation.FL_DATA) != 0;
boolean set_torrent = (options & TagFeatureFileLocation.FL_TORRENT) != 0;
if (set_data) {
File existing_save_loc = dm.getSaveLocation();
if (!(existing_save_loc.equals(save_loc) || existing_save_loc.exists())) {
dm.setTorrentSaveDir(save_loc.getAbsolutePath());
}
}
if (set_torrent) {
File old_torrent_file = new File(dm.getTorrentFileName());
if (old_torrent_file.exists()) {
try {
dm.setTorrentFile(save_loc, old_torrent_file.getName());
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
}
}
}
}
}
} catch (Throwable e) {
Debug.out(e);
}
if (tt == TagType.TT_DOWNLOAD_MANUAL) {
synchronized (lifecycle_handlers) {
long type = tagged.getTaggableType();
LifecycleHandlerImpl handler = lifecycle_handlers.get(type);
if (handler == null) {
handler = new LifecycleHandlerImpl();
lifecycle_handlers.put(type, handler);
}
handler.taggableTagged(tag_type, tag, tagged);
}
}
}
use of com.biglybt.core.download.DownloadManager 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();
}
}
});
}
use of com.biglybt.core.download.DownloadManager in project BiglyBT by BiglySoftware.
the class TagPropertyConstraintHandler method tagAdded.
public void tagAdded(Tag tag) {
TagFeatureProperties tfp = (TagFeatureProperties) tag;
TagProperty prop = tfp.getProperty(TagFeatureProperties.PR_CONSTRAINT);
if (prop != null) {
prop.addListener(new TagPropertyListener() {
@Override
public void propertyChanged(TagProperty property) {
handleProperty(property);
}
@Override
public void propertySync(TagProperty property) {
}
});
handleProperty(prop);
}
tag.addTagListener(new TagListener() {
@Override
public void taggableSync(Tag tag) {
}
@Override
public void taggableRemoved(Tag tag, Taggable tagged) {
apply((DownloadManager) tagged, tag, true);
}
@Override
public void taggableAdded(Tag tag, Taggable tagged) {
apply((DownloadManager) tagged, tag, true);
}
}, false);
}
use of com.biglybt.core.download.DownloadManager in project BiglyBT by BiglySoftware.
the class TagPropertyTrackerHandler method handleProperty.
private void handleProperty(TagProperty property, boolean start_of_day) {
String[] trackers = property.getStringList();
Set<String> tag_hosts = new HashSet<>(Arrays.asList(trackers));
Tag tag = property.getTag();
synchronized (tracker_host_map) {
for (Map.Entry<String, List<Tag>> entry : tracker_host_map.entrySet()) {
List<Tag> tags = entry.getValue();
if (tags.contains(tag)) {
if (!tag_hosts.contains(entry.getKey())) {
tags.remove(tag);
}
}
}
for (String host : tag_hosts) {
List<Tag> tags = tracker_host_map.get(host);
if (tags == null) {
tags = new ArrayList<>();
tracker_host_map.put(host, tags);
} else if (tags.contains(tag)) {
continue;
}
tags.add(tag);
}
}
if (start_of_day) {
return;
}
Set<Taggable> tag_dls = tag.getTagged();
for (Taggable tag_dl : tag_dls) {
DownloadManager dm = (DownloadManager) tag_dl;
Set<String> hosts = getAugmentedHosts(dm);
boolean hit = false;
for (String host : hosts) {
if (tag_hosts.contains(host)) {
hit = true;
break;
}
}
if (!hit) {
tag.removeTaggable(tag_dl);
}
}
List<DownloadManager> managers = core.getGlobalManager().getDownloadManagers();
for (DownloadManager dm : managers) {
if (!dm.isPersistent()) {
continue;
}
if (tag.hasTaggable(dm)) {
continue;
}
Set<String> hosts = getAugmentedHosts(dm);
boolean hit = false;
for (String host : hosts) {
if (tag_hosts.contains(host)) {
hit = true;
break;
}
}
if (hit) {
tag.addTaggable(dm);
}
}
}
Aggregations