use of com.biglybt.core.download.impl.DownloadManagerAdapter in project BiglyBT by BiglySoftware.
the class ManagerUtils method asyncPauseForPeriod.
public static void asyncPauseForPeriod(final List<DownloadManager> dms, final int seconds) {
CoreWaiterSWT.waitForCore(TriggerInThread.NEW_THREAD, new CoreRunningListener() {
@Override
public void coreRunning(Core core) {
final List<DownloadManager> paused = new ArrayList<>();
final DownloadManagerListener listener = new DownloadManagerAdapter() {
@Override
public void stateChanged(DownloadManager manager, int state) {
synchronized (paused) {
if (!paused.remove(manager)) {
return;
}
}
manager.removeListener(this);
}
};
long target_time = SystemTime.getOffsetTime(seconds * 1000);
String time_str = new SimpleDateFormat("HH:mm:ss").format(new Date(target_time));
String reason = MessageText.getString("label.resuming.at", new String[] { time_str });
for (DownloadManager dm : dms) {
if (!isPauseable(dm)) {
continue;
}
if (dm.pause(target_time)) {
dm.setStopReason(reason);
synchronized (paused) {
paused.add(dm);
}
dm.addListener(listener, false);
}
}
if (paused.size() > 0) {
SimpleTimer.addEvent("ManagerUtils.resumer", target_time, new TimerEventPerformer() {
@Override
public void perform(TimerEvent event) {
List<DownloadManager> to_resume = new ArrayList<>();
synchronized (paused) {
to_resume.addAll(paused);
paused.clear();
}
for (DownloadManager dm : to_resume) {
dm.removeListener(listener);
try {
dm.resume();
} catch (Throwable e) {
Debug.out(e);
}
}
}
});
}
}
});
}
use of com.biglybt.core.download.impl.DownloadManagerAdapter in project BiglyBT by BiglySoftware.
the class EnhancedDownloadManager method setProgressiveMode.
protected boolean setProgressiveMode(boolean active, boolean switching_progressive_downloads) {
TOTorrent torrent = download_manager.getTorrent();
DiskManagerFileInfo primaryFile = download_manager.getDownloadState().getPrimaryFile();
if (torrent == null || primaryFile == null) {
return (false);
}
EnhancedDownloadManagerFile enhanced_file = enhanced_files[primaryFile.getIndex()];
synchronized (this) {
if (progressive_active == active) {
return (true);
}
if (active && !supportsProgressiveMode()) {
Debug.out("Attempt to set progress mode on non-progressible content - " + getName());
return (false);
}
log("Progressive mode changed to " + active);
final GlobalManager gm = download_manager.getGlobalManager();
if (active) {
if (dmListener == null) {
dmListener = new DownloadManagerAdapter() {
@Override
public void downloadComplete(DownloadManager manager) {
enhancer.resume();
}
};
}
download_manager.addListener(dmListener);
// Check existing downloading torrents and turn off any
// existing progressive/downloading
Object[] dms = gm.getDownloadManagers().toArray();
for (int i = 0; i < dms.length; i++) {
DownloadManager dmCheck = (DownloadManager) dms[i];
if (dmCheck.equals(download_manager)) {
continue;
}
if (!dmCheck.isDownloadComplete(false)) {
int state = dmCheck.getState();
if (state == DownloadManager.STATE_DOWNLOADING || state == DownloadManager.STATE_QUEUED) {
enhancer.pause(dmCheck);
}
EnhancedDownloadManager edmCheck = enhancer.getEnhancedDownload(dmCheck);
if (edmCheck != null && edmCheck.getProgressiveMode()) {
edmCheck.setProgressiveMode(false, true);
}
}
}
if (download_manager.isPaused()) {
enhancer.resume(download_manager);
}
// and putting at top
if (download_manager.getState() == DownloadManager.STATE_STOPPED) {
download_manager.setStateWaiting();
}
if (download_manager.getPosition() != 1) {
download_manager.getGlobalManager().moveTo(download_manager, 1);
}
} else {
download_manager.removeListener(dmListener);
if (!switching_progressive_downloads) {
enhancer.resume();
}
}
progressive_active = active;
if (progressive_active) {
enhancer.progressiveActivated();
}
if (current_piece_pickler != null) {
if (progressive_active) {
buffer_provider.activate(current_piece_pickler);
progressive_stats.update(0);
} else {
buffer_provider.deactivate(current_piece_pickler);
progressive_stats = createProgressiveStats(download_manager, enhanced_file);
}
} else {
progressive_stats = createProgressiveStats(download_manager, enhanced_file);
}
if (!switching_progressive_downloads) {
if (active) {
RealTimeInfo.setProgressiveActive(progressive_stats.getStreamBytesPerSecondMax());
} else {
RealTimeInfo.setProgressiveInactive();
}
}
}
return (true);
}
use of com.biglybt.core.download.impl.DownloadManagerAdapter 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