use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.
the class SideBar method popoutEntry.
public boolean popoutEntry(MdiEntry entry, boolean onTop) {
SideBarEntrySWT sbe = (SideBarEntrySWT) entry;
SkinnedDialog skinnedDialog = new SkinnedDialog("skin3_dlg_sidebar_popout", "shell", onTop ? UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell() : null, SWT.RESIZE | SWT.MAX | SWT.DIALOG_TRIM);
SWTSkin skin = skinnedDialog.getSkin();
SWTSkinObjectContainer cont = sbe.buildStandAlone((SWTSkinObjectContainer) skin.getSkinObject("content-area"));
if (cont != null) {
skinnedDialog.setTitle(sbe.getTitle());
String metrics_id;
if (sbe.getDatasource() instanceof Download) {
metrics_id = MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_DETAILS;
} else {
metrics_id = sbe.getId();
}
skinnedDialog.open("mdi.popout:" + metrics_id, true);
return (true);
} else {
skinnedDialog.close();
return (false);
}
}
use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.
the class SubscriptionSchedulerImpl method download.
@Override
public void download(final Subscription subs, final SubscriptionResult original_result) {
String download_link = original_result.getDownloadLink();
if (download_link == null) {
log(subs.getName() + ": can't download " + original_result.getID() + " as no direct download link available");
return;
}
final String key = subs.getID() + ":" + original_result.getID();
final String dl = download_link;
synchronized (active_result_downloaders) {
if (active_result_downloaders.contains(key)) {
return;
}
log(subs.getName() + ": queued result for download - " + original_result.getID() + "/" + download_link);
active_result_downloaders.add(key);
result_downloader.run(new AERunnable() {
@Override
public void runSupport() {
boolean success = false;
SubscriptionResult result = null;
// need to fix up to the latest history due to the lazy nature of things :(
try {
result = subs.getHistory().getResult(original_result.getID());
if (result == null) {
log(subs.getName() + ": result has been deleted - " + original_result.getID());
success = true;
} else if (result.getRead()) {
log(subs.getName() + ": result already marked as read, skipping - " + result.getID());
success = true;
} else {
boolean retry = true;
boolean use_ref = subs.getHistory().getDownloadWithReferer();
boolean tried_ref_switch = false;
while (retry) {
retry = false;
try {
TorrentUtils.setTLSDescription("Subscription: " + subs.getName());
URL original_url = new URL(dl);
PluginProxy plugin_proxy = null;
if (dl.startsWith("tor:")) {
String target_resource = dl.substring(4);
original_url = new URL(target_resource);
Map<String, Object> options = new HashMap<>();
options.put(AEProxyFactory.PO_PEER_NETWORKS, new String[] { AENetworkClassifier.AT_TOR });
plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download of '" + target_resource + "'", original_url, options, true);
if (plugin_proxy == null) {
throw (new Exception("No Tor plugin proxy available for '" + dl + "'"));
}
}
URL current_url = plugin_proxy == null ? original_url : plugin_proxy.getURL();
Torrent torrent = null;
try {
while (true) {
try {
ResourceDownloaderFactory rdf = StaticUtilities.getResourceDownloaderFactory();
ResourceDownloader url_rd = rdf.create(current_url, plugin_proxy == null ? null : plugin_proxy.getProxy());
if (plugin_proxy != null) {
url_rd.setProperty("URL_HOST", plugin_proxy.getURLHostRewrite() + (current_url.getPort() == -1 ? "" : (":" + current_url.getPort())));
}
String referer = use_ref ? subs.getReferer() : null;
UrlUtils.setBrowserHeaders(url_rd, referer);
Engine engine = subs.getEngine();
if (engine instanceof WebEngine) {
WebEngine we = (WebEngine) engine;
if (we.isNeedsAuth()) {
String cookies = we.getCookies();
if (cookies != null && cookies.length() > 0) {
url_rd.setProperty("URL_Cookie", cookies);
}
}
}
ResourceDownloader mr_rd = rdf.getMetaRefreshDownloader(url_rd);
InputStream is = mr_rd.download();
torrent = new TorrentImpl(TOTorrentFactory.deserialiseFromBEncodedInputStream(is));
break;
} catch (Throwable e) {
if (plugin_proxy == null) {
plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download", original_url);
if (plugin_proxy != null) {
current_url = plugin_proxy.getURL();
continue;
}
}
throw (e);
}
}
} finally {
if (plugin_proxy != null) {
plugin_proxy.setOK(torrent != null);
}
}
byte[] hash = torrent.getHash();
// PlatformTorrentUtils.setContentTitle(torrent, torr );
DownloadManager dm = PluginInitializer.getDefaultInterface().getDownloadManager();
Download download;
// if we're assigning a tag/networks then we need to add it stopped in case the tag has any pre-start actions (e.g. set initial save location)
// this is because the assignments are done in SubscriptionManagerImpl on the download(willbe)added event
boolean stop_override = subs.getTagID() >= 0 || subs.getHistory().getDownloadNetworks() != null;
boolean auto_start = manager.shouldAutoStart(torrent);
manager.addPrepareTrigger(hash, new Subscription[] { subs }, new SubscriptionResult[] { result });
try {
File data_location = null;
File torrent_location = null;
if (manager.getAddHashDirs()) {
String torrent_name = FileUtil.convertOSSpecificChars(torrent.getName(), false);
String hash_str = ByteFormatter.encodeString(hash).substring(0, 8);
String data_dir = COConfigurationManager.getStringParameter("Default save path");
if (data_dir != null && !data_dir.isEmpty()) {
data_location = FileUtil.newFile(data_dir, torrent_name + "_" + hash_str);
}
if (COConfigurationManager.getBooleanParameter("Save Torrent Files")) {
String torrent_dir = COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory");
if (torrent_dir != null && !torrent_dir.isEmpty()) {
torrent_location = FileUtil.newFile(torrent_dir, torrent_name + "_" + hash_str + ".torrent");
try {
torrent.writeToFile(torrent_location);
} catch (Throwable e) {
Debug.out(e);
torrent_location = null;
}
}
}
}
if (auto_start && !stop_override) {
download = dm.addDownload(torrent, torrent_location, data_location);
} else {
download = dm.addDownloadStopped(torrent, torrent_location, data_location);
}
} finally {
manager.removePrepareTrigger(hash);
}
log(subs.getName() + ": added download " + download.getName() + ": auto-start=" + auto_start);
// maybe remove this as should be actioned in the trigger?
manager.prepareDownload(download, new Subscription[] { subs }, new SubscriptionResult[] { result });
subs.addAssociation(hash);
if (auto_start && stop_override) {
download.restart();
}
result.setRead(true);
success = true;
if (tried_ref_switch) {
subs.getHistory().setDownloadWithReferer(use_ref);
}
} catch (Throwable e) {
log(subs.getName() + ": Failed to download result " + dl, e);
if (e instanceof TOTorrentException && !tried_ref_switch) {
use_ref = !use_ref;
tried_ref_switch = true;
retry = true;
log(subs.getName() + ": Retrying " + (use_ref ? "with referer" : "without referer"));
}
} finally {
TorrentUtils.setTLSDescription(null);
}
}
}
} finally {
try {
if (result != null && !success) {
if (dl.startsWith("azplug:") || dl.startsWith("chat:")) {
// whatever the outcome these have been handled async
result.setRead(true);
} else {
int rad = manager.getAutoDownloadMarkReadAfterDays();
if (rad > 0) {
long rad_millis = rad * 24 * 60 * 60 * 1000L;
long time_found = result.getTimeFound();
if (time_found > 0 && time_found + rad_millis < SystemTime.getCurrentTime()) {
log(subs.getName() + ": result expired, marking as read - " + result.getID());
result.setRead(true);
}
}
}
}
} catch (Throwable e) {
Debug.out(e);
} finally {
synchronized (active_result_downloaders) {
active_result_downloaders.remove(key);
}
calculateSchedule();
}
}
}
});
}
}
use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.
the class TagDownloadWithState method checkMaximumTaggables.
@Override
protected void checkMaximumTaggables() {
if (getTagType().getTagType() != TagType.TT_DOWNLOAD_MANUAL) {
return;
}
int max = getMaximumTaggables();
if (max <= 0) {
return;
}
if (max == 999999) {
max = 0;
}
int removal_strategy = getRemovalStrategy();
if (removal_strategy == RS_NONE) {
return;
}
if (getTaggedCount() > max) {
Set<DownloadManager> dms = getTaggedDownloads();
List<DownloadManager> sorted_dms = new ArrayList<>(dms);
final int order = getOrdering();
Collections.sort(sorted_dms, new Comparator<DownloadManager>() {
@Override
public int compare(DownloadManager dm1, DownloadManager dm2) {
if (order == OP_ADDED_TO_VUZE) {
long t1 = dm1.getDownloadState().getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME);
long t2 = dm2.getDownloadState().getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME);
if (t1 < t2) {
return (-1);
} else if (t1 > t2) {
return (1);
} else {
return (dm1.getInternalName().compareTo(dm2.getInternalName()));
}
} else {
long t1 = getTaggableAddedTime(dm1);
long t2 = getTaggableAddedTime(dm2);
if (t1 < t2) {
return (-1);
} else if (t1 > t2) {
return (1);
} else {
return (dm1.getInternalName().compareTo(dm2.getInternalName()));
}
}
}
});
Iterator<DownloadManager> it = sorted_dms.iterator();
while (it.hasNext() && sorted_dms.size() > max) {
DownloadManager dm = it.next();
if (dm.isPersistent()) {
it.remove();
try {
if (removal_strategy == RS_ARCHIVE) {
Download download = PluginCoreUtils.wrap(dm);
if (download.canStubbify()) {
// have to remove from tag otherwise when it is restored it will no doubt get re-archived!
removeTaggable(dm);
download.stubbify();
}
} else if (removal_strategy == RS_REMOVE_FROM_LIBRARY) {
dm.getGlobalManager().removeDownloadManager(dm, false, false);
} else if (removal_strategy == RS_DELETE_FROM_COMPUTER) {
boolean reallyDeleteData = !dm.getDownloadState().getFlag(Download.FLAG_DO_NOT_DELETE_DATA_ON_REMOVE);
dm.getGlobalManager().removeDownloadManager(dm, true, reallyDeleteData);
} else if (removal_strategy == RS_MOVE_TO_OLD_TAG) {
String old_tag = getTagName(true) + "_";
if (Character.isUpperCase(old_tag.charAt(0))) {
old_tag += "Old";
} else {
old_tag += "old";
}
Tag ot = getTagType().getTag(old_tag, true);
if (ot == null) {
ot = getTagType().createTag(old_tag, true);
}
ot.addTaggable(dm);
removeTaggable(dm);
}
} catch (Throwable e) {
Debug.out(e);
}
} else {
// can't remove/archive non-persistent downloads here so just ignore them
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, "Non-persistent downloads (e.g. shares) can't be automatically deleted or archived. Maximum entries not enforced for Tag '" + getTagName(true) + "'"));
}
}
}
}
use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.
the class TagManagerImpl method evalScript.
protected Object evalScript(Tag tag, String script, List<DownloadManager> dms, String intent_key) {
String script_type = "";
script = script.trim();
if (script.length() >= 10) {
String start = script.substring(0, 10).toLowerCase(Locale.US);
if (start.startsWith("javascript") || start.startsWith("plugin")) {
int p1 = script.indexOf('(');
int p2 = script.lastIndexOf(')');
if (p1 != -1 && p2 != -1) {
script = script.substring(p1 + 1, p2).trim();
if (script.startsWith("\"") && script.endsWith("\"")) {
script = script.substring(1, script.length() - 1);
}
// allow people to escape " if it makes them feel better
script = script.replaceAll("\\\\\"", "\"");
script_type = start.startsWith("javascript") ? ScriptProvider.ST_JAVASCRIPT : ScriptProvider.ST_PLUGIN;
}
}
}
if (script_type == "") {
String error = "Unrecognised script type: " + script;
Debug.out(error);
return (new Exception(error));
}
boolean provider_found = false;
List<ScriptProvider> providers = CoreFactory.getSingleton().getPluginManager().getDefaultPluginInterface().getUtilities().getScriptProviders();
for (ScriptProvider p : providers) {
if (p.getScriptType() == script_type) {
provider_found = true;
if (dms.size() > 1 && p.canEvalBatch(script)) {
Map<String, Object> bindings = new HashMap<>();
List<String> intents = new ArrayList<>();
List<Download> plugin_dms = new ArrayList<>();
for (DownloadManager dm : dms) {
Download plugin_dm = PluginCoreUtils.wrap(dm);
if (plugin_dm == null) {
continue;
}
String dm_name = dm.getDisplayName();
if (dm_name.length() > 32) {
dm_name = dm_name.substring(0, 29) + "...";
}
String intent = intent_key + "(\"" + tag.getTagName() + "\",\"" + dm_name + "\")";
intents.add(intent);
plugin_dms.add(plugin_dm);
}
if (intents.isEmpty()) {
return (null);
}
bindings.put("intents", intents);
bindings.put("downloads", plugin_dms);
bindings.put("tag", tag);
try {
Object result = p.eval(script, bindings);
return (result);
} catch (Throwable e) {
Debug.out(e);
return (e);
}
} else {
List<Object> results = new ArrayList<>();
for (DownloadManager dm : dms) {
Download plugin_dm = PluginCoreUtils.wrap(dm);
if (plugin_dm == null) {
// deleted in the meantime
continue;
}
Map<String, Object> bindings = new HashMap<>();
String dm_name = dm.getDisplayName();
if (dm_name.length() > 32) {
dm_name = dm_name.substring(0, 29) + "...";
}
String intent = intent_key + "(\"" + tag.getTagName() + "\",\"" + dm_name + "\")";
bindings.put("intent", intent);
bindings.put("download", plugin_dm);
bindings.put("tag", tag);
try {
Object result = p.eval(script, bindings);
results.add(result);
} catch (Throwable e) {
Debug.out(e);
results.add(e);
}
}
if (results.size() == 1 && dms.size() == 1) {
return (results.get(0));
} else {
return (results);
}
}
}
}
if (script_type == ScriptProvider.ST_JAVASCRIPT && !provider_found) {
if (!js_plugin_install_tried) {
js_plugin_install_tried = true;
PluginUtils.installJavaScriptPlugin();
}
}
return (null);
}
use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.
the class TagManagerImpl method tagGroupCreated.
protected void tagGroupCreated(TagTypeBase tag_type, TagGroupImpl group) {
Map<String, Object> conf = getConf(tag_type, false);
if (conf != null) {
Map<String, Object> tg_conf = (Map<String, Object>) conf.get(group.getGroupID());
if (tg_conf != null) {
group.importState(tg_conf);
}
}
PluginInterface pi = CoreFactory.getSingleton().getPluginManager().getDefaultPluginInterface();
UIManager ui_manager = pi.getUIManager();
TableManager tm = ui_manager.getTableManager();
Properties props = new Properties();
String col_id_text = "tag.group.col." + group.getGroupID();
String col_id_icons = "tag.group.col.icons." + group.getGroupID();
props.put("TableColumn.header." + col_id_text, group.getName());
props.put("TableColumn.header." + col_id_text + ".info", MessageText.getString("label.tag.names"));
props.put("TableColumn.header." + col_id_icons, group.getName());
props.put("TableColumn.header." + col_id_icons + ".info", MessageText.getString("TableColumn.header.tag_icons"));
pi.getUtilities().getLocaleUtilities().integrateLocalisedMessageBundle(props);
int[] interesting_tts = { TagType.TT_DOWNLOAD_MANUAL, TagType.TT_DOWNLOAD_CATEGORY };
tm.registerColumn(Download.class, col_id_text, new TableColumnCreationListener() {
@Override
public void tableColumnCreated(TableColumn column) {
column.setAlignment(TableColumn.ALIGN_CENTER);
column.setPosition(TableColumn.POSITION_INVISIBLE);
column.setWidth(70);
column.setRefreshInterval(TableColumn.INTERVAL_LIVE);
column.setIconReference("image.tag.column", false);
column.addCellRefreshListener((cell) -> {
Download dl = (Download) cell.getDataSource();
if (dl == null) {
return;
}
List<Tag> tags = TagManagerImpl.this.getTagsForTaggable(interesting_tts, PluginCoreUtils.unwrap(dl));
String sTags = null;
if (tags.size() > 0) {
tags = TagUtils.sortTags(tags);
for (Tag t : tags) {
if (t.getGroupContainer() == group) {
String str = t.getTagName(true);
if (sTags == null) {
sTags = str;
} else {
sTags += ", " + str;
}
}
}
}
cell.setText((sTags == null) ? "" : sTags);
});
}
});
tm.registerColumn(Download.class, col_id_icons, new TableColumnCreationListener() {
@Override
public void tableColumnCreated(TableColumn column) {
try {
Class cla = Class.forName("com.biglybt.ui.swt.columns.tag.ColumnTagGroupIcons");
cla.getConstructor(TableColumn.class, TagGroup.class).newInstance(column, group);
} catch (Throwable e) {
Debug.out(e);
;
}
}
});
}
Aggregations