use of com.biglybt.core.global.GlobalManager in project BiglyBT by BiglySoftware.
the class TorrentFolderWatcher method importAddedFiles.
void importAddedFiles() {
Core core = CoreFactory.getSingleton();
try {
this_mon.enter();
if (!running) {
return;
}
GlobalManager global_manager = _global_manager;
if (global_manager == null || !core.isStarted()) {
return;
}
com.biglybt.pif.download.DownloadManager plugin_dm = core.getPluginManager().getDefaultPluginInterface().getDownloadManager();
boolean save_torrents_default = COConfigurationManager.getBooleanParameter("Save Torrent Files");
String torrent_save_path = COConfigurationManager.getStringParameter("General_sDefaultTorrent_Directory");
int start_state = COConfigurationManager.getBooleanParameter("Start Watched Torrents Stopped") ? DownloadManager.STATE_STOPPED : DownloadManager.STATE_QUEUED;
int num_folders = COConfigurationManager.getIntParameter("Watch Torrent Folder Path Count", 1);
List<File> folders = new ArrayList<>();
List<String> tags = new ArrayList<>();
for (int i = 0; i < num_folders; i++) {
String folder_path = COConfigurationManager.getStringParameter("Watch Torrent Folder Path" + (i == 0 ? "" : (" " + i)));
File folder = null;
if (folder_path != null && folder_path.length() > 0) {
folder = new File(folder_path);
if (!folder.isDirectory()) {
if (!folder.exists()) {
FileUtil.mkdirs(folder);
}
if (!folder.isDirectory()) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "[Watch Torrent Folder Path] " + "does not exist or " + "is not a dir"));
folder = null;
}
}
}
if (folder != null) {
folders.add(folder);
String tag = COConfigurationManager.getStringParameter("Watch Torrent Folder Tag" + (i == 0 ? "" : (" " + i)), null);
if (tag != null && tag.trim().length() == 0) {
tag = null;
}
tags.add(tag);
}
}
if (folders.isEmpty()) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "[Watch Torrent Folder Path] not configured"));
return;
}
String data_save_path = COConfigurationManager.getStringParameter("Default save path");
File f = null;
if (data_save_path != null && data_save_path.length() > 0) {
f = new File(data_save_path);
// Path is not an existing directory.
if (!f.isDirectory()) {
if (!f.exists()) {
FileUtil.mkdirs(f);
}
// If path is still not a directory, abort.
if (!f.isDirectory()) {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "[Default save path] does not exist or is not a dir"));
}
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, "[Default save path] does not exist or is not a dir"));
return;
}
}
}
// If we get here, and this is true, then data_save_path isn't valid.
if (f == null) {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "[Default save path] needs to be set for auto-.torrent-import to work"));
}
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, "[Default save path] needs to be set for auto-.torrent-import to work"));
}
for (int i = 0; i < to_delete.size(); i++) {
TOTorrent torrent = (TOTorrent) to_delete.get(i);
try {
TorrentUtils.delete(torrent);
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
to_delete.clear();
for (int folder_index = 0; folder_index < folders.size(); folder_index++) {
File folder = folders.get(folder_index);
final String tag_name = tags.get(folder_index);
// if we are saving torrents to the same location as we import them from
// then we can't assume that its safe to delete the torrent after import!
boolean save_torrents = save_torrents_default;
if (torrent_save_path.length() == 0 || new File(torrent_save_path).getAbsolutePath().equals(folder.getAbsolutePath()) || !new File(torrent_save_path).isDirectory()) {
save_torrents = false;
}
String[] currentFileList = folder.list(filename_filter);
if (currentFileList == null) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "There was a problem trying to get a listing of torrents from " + folder));
} else {
for (int i = 0; i < currentFileList.length; i++) {
if (!running) {
return;
}
File file = new File(folder, currentFileList[i]);
if (file.getName().toLowerCase(Locale.US).endsWith(".magnet")) {
handleMagnet(file);
} else {
try {
TOTorrent torrent = TorrentUtils.readFromFile(file, false);
if (global_manager.getDownloadManager(torrent) != null) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, file.getAbsolutePath() + " is already being downloaded"));
// we can't touch the torrent file as it is (probably)
// being used for the download
} else if (plugin_dm.lookupDownloadStub(torrent.getHash()) != null) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, file.getAbsolutePath() + " is an archived download"));
if (!save_torrents) {
File imported = new File(folder, file.getName() + ".imported");
TorrentUtils.move(file, imported);
} else {
to_delete.add(torrent);
}
} else {
final DownloadManagerInitialisationAdapter dmia = new DownloadManagerInitialisationAdapter() {
@Override
public int getActions() {
return (ACT_ASSIGNS_TAGS);
}
@Override
public void initialised(DownloadManager dm, boolean for_seeding) {
if (tag_name != null) {
TagManager tm = TagManagerFactory.getTagManager();
TagType tt = tm.getTagType(TagType.TT_DOWNLOAD_MANUAL);
Tag tag = tt.getTag(tag_name, true);
try {
if (tag == null) {
tag = tt.createTag(tag_name, true);
}
tag.addTaggable(dm);
} catch (Throwable e) {
Debug.out(e);
}
}
}
};
byte[] hash = null;
try {
hash = torrent.getHash();
} catch (Exception e) {
}
if (!save_torrents) {
File imported = new File(folder, file.getName() + ".imported");
TorrentUtils.move(file, imported);
global_manager.addDownloadManager(imported.getAbsolutePath(), hash, data_save_path, start_state, true, false, dmia);
} else {
global_manager.addDownloadManager(file.getAbsolutePath(), hash, data_save_path, start_state, true, false, dmia);
// add torrent for deletion, since there will be a
// saved copy elsewhere
to_delete.add(torrent);
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Auto-imported " + file.getAbsolutePath()));
}
} catch (Throwable e) {
Debug.out("Failed to auto-import torrent file '" + file.getAbsolutePath() + "' - " + Debug.getNestedExceptionMessage(e));
Debug.printStackTrace(e);
}
}
}
}
}
} finally {
this_mon.exit();
}
}
use of com.biglybt.core.global.GlobalManager 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.global.GlobalManager in project BiglyBT by BiglySoftware.
the class DataSourceUtils method getTorrent.
public static TOTorrent getTorrent(Object ds) {
if (ds instanceof TOTorrent) {
return (TOTorrent) ds;
}
if (ds instanceof DownloadManager) {
TOTorrent torrent = ((DownloadManager) ds).getTorrent();
if (torrent != null) {
return torrent;
}
}
if (ds instanceof ActivitiesEntry) {
TOTorrent torrent = ((ActivitiesEntry) ds).getTorrent();
if (torrent == null) {
// getDM will check hash as well
DownloadManager dm = getDM(ds);
if (dm != null) {
torrent = dm.getTorrent();
}
}
return torrent;
}
if (ds instanceof TranscodeFile) {
TranscodeFile tf = (TranscodeFile) ds;
try {
DiskManagerFileInfo file = tf.getSourceFile();
if (file != null) {
Download download = file.getDownload();
if (download != null) {
Torrent torrent = download.getTorrent();
if (torrent != null) {
return PluginCoreUtils.unwrap(torrent);
}
}
}
} catch (Throwable e) {
}
}
if (ds instanceof TranscodeJob) {
TranscodeJob tj = (TranscodeJob) ds;
try {
DiskManagerFileInfo file = tj.getFile();
if (file != null) {
Download download = tj.getFile().getDownload();
if (download != null) {
Torrent torrent = download.getTorrent();
if (torrent != null) {
return PluginCoreUtils.unwrap(torrent);
}
}
}
} catch (DownloadException e) {
}
}
if (ds instanceof ISelectedContent) {
return ((ISelectedContent) ds).getTorrent();
}
if (ds instanceof String) {
String hash = (String) ds;
try {
GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
DownloadManager dm = gm.getDownloadManager(new HashWrapper(Base32.decode(hash)));
if (dm != null) {
return dm.getTorrent();
}
} catch (Exception e) {
// ignore
}
}
DownloadManager dm = getDM(ds);
if (dm != null) {
return dm.getTorrent();
}
return null;
}
use of com.biglybt.core.global.GlobalManager in project BiglyBT by BiglySoftware.
the class TorrentLog method performCommand.
@Override
protected boolean performCommand(ConsoleInput ci, DownloadManager dm, List args) {
try {
dms_mon.enter();
if (!gm_listener_added) {
gm_listener_added = true;
GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
gm.addListener(new GlobalManagerAdapter() {
@Override
public void downloadManagerRemoved(DownloadManager dm) {
dms.remove(dm);
}
}, false);
}
boolean turnOn;
if (mode == MODE_FLIP) {
turnOn = !dms.contains(dm);
} else {
turnOn = mode == MODE_ON;
}
if (turnOn) {
ci.out.print("->on] ");
if (dms.contains(dm)) {
return true;
}
dms.add(dm);
if (dms.size() == 1) {
Logger.addListener(this);
}
} else {
ci.out.print("->off] ");
dms.remove(dm);
if (dms.size() == 0) {
Logger.removeListener(this);
}
}
} catch (Exception e) {
e.printStackTrace(ci.out);
return false;
} finally {
dms_mon.exit();
}
return true;
}
use of com.biglybt.core.global.GlobalManager in project BiglyBT by BiglySoftware.
the class Move method execute.
@Override
public void execute(String commandName, ConsoleInput ci, List args) {
if (args.isEmpty()) {
ci.out.println("> Missing subcommand for 'move'\r\n> move syntax: move <#from> [<#to>]");
return;
}
if (ci.torrents.isEmpty()) {
ci.out.println("> Command 'move': No torrents in list.");
return;
}
int ncommand;
int nmoveto = -1;
boolean moveto = false;
try {
ncommand = Integer.parseInt((String) args.get(0));
if (args.size() > 1) {
nmoveto = Integer.parseInt((String) args.get(1));
moveto = true;
}
} catch (NumberFormatException e) {
ci.out.println("> Command 'move': Subcommand '" + args.get(0) + "' unknown.");
return;
}
int number = Math.abs(ncommand);
if (number == 0 || number > ci.torrents.size()) {
ci.out.println("> Command 'move': Torrent #" + Integer.toString(number) + " unknown.");
return;
}
DownloadManager dm = (DownloadManager) ci.torrents.get(number - 1);
String name = dm.getDisplayName();
if (name == null)
name = "?";
GlobalManager gm = dm.getGlobalManager();
if (moveto) {
gm.moveTo(dm, nmoveto - 1);
gm.fixUpDownloadManagerPositions();
ci.out.println("> Torrent #" + Integer.toString(number) + " (" + name + ") moved to #" + Integer.toString(nmoveto) + ".");
} else if (ncommand > 0) {
if (gm.isMoveableUp(dm)) {
while (gm.isMoveableUp(dm)) gm.moveUp(dm);
gm.fixUpDownloadManagerPositions();
ci.out.println("> Torrent #" + Integer.toString(number) + " (" + name + ") moved to top.");
} else {
ci.out.println("> Torrent #" + Integer.toString(number) + " (" + name + ") already at top.");
}
} else {
if (gm.isMoveableDown(dm)) {
while (gm.isMoveableDown(dm)) gm.moveDown(dm);
gm.fixUpDownloadManagerPositions();
ci.out.println("> Torrent #" + Integer.toString(number) + " (" + name + ") moved to bottom.");
} else {
ci.out.println("> Torrent #" + Integer.toString(number) + " (" + name + ") already at bottom.");
}
}
}
Aggregations