use of com.biglybt.core.category.CategoryListener in project BiglyBT by BiglySoftware.
the class TranscodeManagerImpl method checkCategories.
void checkCategories() {
Category[] cats = CategoryManager.getCategories();
Map<Category, Object[]> active_map = new HashMap<>();
for (Category cat : cats) {
String target = cat.getStringAttribute(Category.AT_AUTO_TRANSCODE_TARGET);
if (target != null) {
String device_id = null;
if (target.endsWith("/blank")) {
device_id = target.substring(0, target.length() - 6);
}
DeviceMediaRenderer target_dmr = null;
TranscodeProfile target_profile = null;
for (DeviceImpl device : device_manager.getDevices()) {
if (!(device instanceof DeviceMediaRenderer)) {
continue;
}
DeviceMediaRenderer dmr = (DeviceMediaRenderer) device;
if (device_id != null) {
if (device.getID().equals(device_id)) {
target_dmr = dmr;
target_profile = device.getBlankProfile();
break;
}
} else {
TranscodeProfile[] profs = device.getTranscodeProfiles();
for (TranscodeProfile prof : profs) {
if (prof.getUID().equals(target)) {
target_dmr = dmr;
target_profile = prof;
break;
}
}
}
}
if (target_dmr != null) {
active_map.put(cat, new Object[] { target_dmr, target_profile });
}
}
}
Map<Category, Object[]> to_process = new HashMap<>();
synchronized (category_map) {
if (category_listener == null) {
category_listener = new CategoryListener() {
@Override
public void downloadManagerAdded(Category cat, DownloadManager manager) {
Object[] details;
synchronized (category_map) {
details = category_map.get(cat);
}
if (details != null) {
processCategory(cat, details, manager);
}
}
@Override
public void downloadManagerRemoved(Category cat, DownloadManager removed) {
}
};
}
Iterator<Category> it = category_map.keySet().iterator();
while (it.hasNext()) {
Category c = it.next();
if (!active_map.containsKey(c)) {
c.removeCategoryListener(category_listener);
it.remove();
}
}
for (final Category c : active_map.keySet()) {
if (!category_map.containsKey(c)) {
to_process.put(c, active_map.get(c));
c.addCategoryListener(category_listener);
category_map.put(c, active_map.get(c));
if (c.getType() == Category.TYPE_UNCATEGORIZED) {
if (category_dl_listener == null) {
// new downloads don't get a category-change event fired when added
// we also want to delay things a bit to allow other components
// to set an initial category. there's no hurry anyways
category_dl_listener = new GlobalManagerAdapter() {
@Override
public void downloadManagerAdded(final DownloadManager dm) {
new DelayedEvent("TM:cat-check", 10 * 1000, new AERunnable() {
@Override
public void runSupport() {
Category dm_c = dm.getDownloadState().getCategory();
if (dm_c == null || dm_c == c) {
// still uncategorised
Object[] details;
synchronized (category_map) {
details = category_map.get(c);
}
if (details != null) {
processCategory(c, details, dm);
}
}
}
});
}
@Override
public void downloadManagerRemoved(DownloadManager dm) {
}
};
core.getGlobalManager().addListener(category_dl_listener, false);
}
}
}
}
}
if (to_process.size() > 0) {
List<DownloadManager> downloads = core.getGlobalManager().getDownloadManagers();
for (Map.Entry<Category, Object[]> entry : to_process.entrySet()) {
Category c = entry.getKey();
Object[] details = entry.getValue();
List<DownloadManager> list = c.getDownloadManagers(downloads);
for (DownloadManager dm : list) {
processCategory(c, details, dm);
}
}
}
}
use of com.biglybt.core.category.CategoryListener in project BiglyBT by BiglySoftware.
the class SB_Transfers method setupViewTitleWithCore.
protected void setupViewTitleWithCore(Core _core) {
synchronized (SB_Transfers.class) {
if (!first) {
return;
}
first = false;
core = _core;
coreCreateTime = core.getCreateTime();
}
categoryListener = new CategoryListener() {
@Override
public void downloadManagerRemoved(Category cat, DownloadManager removed) {
RefreshCategorySideBar(cat);
}
@Override
public void downloadManagerAdded(Category cat, DownloadManager manager) {
RefreshCategorySideBar(cat);
}
};
paramCatInSidebarListener = new ParameterListener() {
@Override
public void parameterChanged(String parameterName) {
if (Utils.isAZ2UI()) {
return;
}
Category[] categories = CategoryManager.getCategories();
if (categories.length == 0) {
return;
}
boolean catInSidebar = COConfigurationManager.getBooleanParameter("Library.CatInSideBar");
if (catInSidebar) {
if (categoryManagerListener != null) {
return;
}
categoryManagerListener = new CategoryManagerListener() {
@Override
public void categoryRemoved(Category category) {
removeCategory(category);
}
@Override
public void categoryChanged(Category category) {
RefreshCategorySideBar(category);
}
@Override
public void categoryAdded(Category category) {
Category[] categories = CategoryManager.getCategories();
if (categories.length == 3) {
for (Category cat : categories) {
setupCategory(cat);
}
} else {
setupCategory(category);
}
}
};
CategoryManager.addCategoryManagerListener(categoryManagerListener);
if (categories.length > 2) {
for (Category category : categories) {
category.addCategoryListener(categoryListener);
setupCategory(category);
}
}
} else {
if (categoryManagerListener != null) {
CategoryManager.removeCategoryManagerListener(categoryManagerListener);
categoryManagerListener = null;
}
for (Category category : categories) {
category.removeCategoryListener(categoryListener);
removeCategory(category);
}
}
}
};
COConfigurationManager.addAndFireParameterListener("Library.CatInSideBar", paramCatInSidebarListener);
tagListener = new TagListener() {
@Override
public void taggableAdded(Tag tag, Taggable tagged) {
refreshTagSideBar(tag);
}
@Override
public void taggableSync(Tag tag) {
refreshTagSideBar(tag);
}
@Override
public void taggableRemoved(Tag tag, Taggable tagged) {
refreshTagSideBar(tag);
}
};
paramTagsInSidebarListener = new ParameterListener() {
@Override
public void parameterChanged(String parameterName) {
boolean tagInSidebar = COConfigurationManager.getBooleanParameter("Library.TagInSideBar");
if (tagInSidebar) {
addTagManagerListeners();
} else {
removeTagManagerListeners(true);
}
}
};
COConfigurationManager.addAndFireParameterListener("Library.TagInSideBar", paramTagsInSidebarListener);
final GlobalManager gm = core.getGlobalManager();
dmListener = new DownloadManagerAdapter() {
@Override
public void stateChanged(DownloadManager dm, int state) {
stateChanged(dm, state, statsNoLowNoise);
stateChanged(dm, state, statsWithLowNoise);
}
public void stateChanged(DownloadManager dm, int state, stats stats) {
if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
return;
}
synchronized (statsLock) {
updateDMCounts(dm);
boolean complete = dm.getAssumedComplete();
Boolean wasErrorStateB = (Boolean) dm.getUserData("wasErrorState");
boolean wasErrorState = wasErrorStateB != null && wasErrorStateB.booleanValue();
boolean isErrorState = state == DownloadManager.STATE_ERROR;
if (isErrorState != wasErrorState) {
int rel = isErrorState ? 1 : -1;
if (complete) {
stats.numErrorComplete += rel;
} else {
stats.numErrorInComplete += rel;
}
updateErrorTooltip(gm, stats);
dm.setUserData("wasErrorState", Boolean.valueOf(isErrorState));
}
refreshAllLibraries();
}
}
@Override
public void completionChanged(DownloadManager dm, boolean completed) {
completionChanged(dm, completed, statsNoLowNoise);
completionChanged(dm, completed, statsWithLowNoise);
}
public void completionChanged(DownloadManager dm, boolean completed, stats stats) {
if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
return;
}
synchronized (statsLock) {
int dm_state = updateDMCounts(dm);
if (completed) {
stats.numComplete++;
stats.numIncomplete--;
if (dm_state == DownloadManager.STATE_ERROR) {
stats.numErrorComplete++;
stats.numErrorInComplete--;
}
if (dm_state == DownloadManager.STATE_STOPPED) {
statsNoLowNoise.numStoppedIncomplete--;
}
} else {
stats.numComplete--;
stats.numIncomplete++;
if (dm_state == DownloadManager.STATE_ERROR) {
stats.numErrorComplete--;
stats.numErrorInComplete++;
}
if (dm_state == DownloadManager.STATE_STOPPED) {
statsNoLowNoise.numStoppedIncomplete++;
}
}
recountUnopened();
updateErrorTooltip(gm, stats);
refreshAllLibraries();
}
}
};
gmListener = new GlobalManagerAdapter() {
@Override
public void downloadManagerRemoved(DownloadManager dm) {
downloadManagerRemoved(dm, statsNoLowNoise);
downloadManagerRemoved(dm, statsWithLowNoise);
}
public void downloadManagerRemoved(DownloadManager dm, stats stats) {
if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
return;
}
synchronized (statsLock) {
recountUnopened();
if (dm.getAssumedComplete()) {
stats.numComplete--;
Boolean wasDownloadingB = (Boolean) dm.getUserData("wasDownloading");
if (wasDownloadingB != null && wasDownloadingB.booleanValue()) {
stats.numDownloading--;
}
} else {
stats.numIncomplete--;
Boolean wasSeedingB = (Boolean) dm.getUserData("wasSeeding");
if (wasSeedingB != null && wasSeedingB.booleanValue()) {
stats.numSeeding--;
}
}
Boolean wasStoppedB = (Boolean) dm.getUserData("wasStopped");
boolean wasStopped = wasStoppedB != null && wasStoppedB.booleanValue();
if (wasStopped) {
if (!dm.getAssumedComplete()) {
stats.numStoppedIncomplete--;
}
}
Boolean wasQueuedB = (Boolean) dm.getUserData("wasQueued");
boolean wasQueued = wasQueuedB != null && wasQueuedB.booleanValue();
if (wasQueued) {
stats.numQueued--;
}
refreshAllLibraries();
}
dm.removeListener(dmListener);
}
@Override
public void downloadManagerAdded(DownloadManager dm) {
dm.addListener(dmListener, false);
synchronized (statsLock) {
recountUnopened();
downloadManagerAdded(dm, statsNoLowNoise);
downloadManagerAdded(dm, statsWithLowNoise);
refreshAllLibraries();
}
}
public void downloadManagerAdded(DownloadManager dm, stats stats) {
if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
return;
}
boolean assumed_complete = dm.getAssumedComplete();
synchronized (statsLock) {
if (dm.isPersistent() && dm.getTorrent() != null && !assumed_complete) {
// ignore borked torrents as their create time is inaccurate
stats.newestIncompleteDownloadTime = Math.max(stats.newestIncompleteDownloadTime, dm.getCreationTime());
}
int dm_state = dm.getState();
if (assumed_complete) {
stats.numComplete++;
if (dm_state == DownloadManager.STATE_SEEDING) {
stats.numSeeding++;
}
} else {
stats.numIncomplete++;
if (dm_state == DownloadManager.STATE_DOWNLOADING) {
dm.setUserData("wasDownloading", Boolean.TRUE);
stats.numDownloading++;
} else {
dm.setUserData("wasDownloading", Boolean.FALSE);
}
}
}
}
};
gm.addListener(gmListener, false);
resetStats(gm, dmListener, statsWithLowNoise, statsNoLowNoise);
refreshAllLibraries();
timerEventPeriodic = SimpleTimer.addPeriodicEvent("header:refresh", 60 * 1000, new TimerEventPerformer() {
@Override
public void perform(TimerEvent event) {
stats withNoise = new stats();
stats noNoise = new stats();
noNoise.includeLowNoise = false;
withNoise.includeLowNoise = true;
synchronized (statsLock) {
resetStats(gm, null, withNoise, noNoise);
boolean fixed = false;
if (!withNoise.sameAs(statsWithLowNoise)) {
statsWithLowNoise.copyFrom(withNoise);
fixed = true;
}
if (!noNoise.sameAs(statsNoLowNoise)) {
statsNoLowNoise.copyFrom(noNoise);
fixed = true;
}
if (fixed) {
updateErrorTooltip(gm, statsWithLowNoise);
updateErrorTooltip(gm, statsNoLowNoise);
refreshAllLibraries();
}
}
}
});
}
Aggregations