use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class MyTorrentsView method fillMenu.
// @see com.biglybt.ui.swt.views.table.TableViewSWTMenuFillListener#fillMenu(java.lang.String, org.eclipse.swt.widgets.Menu)
@Override
public void fillMenu(String sColumnName, final Menu menu) {
Object[] dataSources = tv.getSelectedDataSources(true);
DownloadManager[] dms = getSelectedDownloads();
if (dms.length == 0 && dataSources.length > 0) {
List<DiskManagerFileInfo> listFileInfos = new ArrayList<>();
DownloadManager firstFileDM = null;
for (Object ds : dataSources) {
if (ds instanceof DiskManagerFileInfo) {
DiskManagerFileInfo info = (DiskManagerFileInfo) ds;
// for now, FilesViewMenuUtil.fillmenu can only handle one DM
if (firstFileDM != null && !firstFileDM.equals(info.getDownloadManager())) {
break;
}
firstFileDM = info.getDownloadManager();
listFileInfos.add(info);
}
}
if (listFileInfos.size() > 0) {
FilesViewMenuUtil.fillMenu(tv, sColumnName, menu, new DownloadManager[] { firstFileDM }, new DiskManagerFileInfo[][] { listFileInfos.toArray(new DiskManagerFileInfo[0]) });
return;
}
}
boolean hasSelection = (dms.length > 0);
if (hasSelection) {
boolean isSeedingView = Download.class.equals(forDataSourceType) || DownloadTypeComplete.class.equals(forDataSourceType);
TorrentUtil.fillTorrentMenu(menu, dms, core, true, (isSeedingView) ? 2 : 1, tv);
// ---
new MenuItem(menu, SWT.SEPARATOR);
}
}
use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class MyTorrentsView method filterCheck.
@Override
public boolean filterCheck(DownloadManager dm, String sLastSearch, boolean bRegexSearch) {
if (dm == null) {
return (false);
}
boolean bOurs;
if (sLastSearch.length() > 0) {
try {
String comment = dm.getDownloadState().getUserComment();
if (comment == null) {
comment = "";
}
String[][] name_mapping = { { "", dm.getDisplayName() }, { "t:", // defer (index = 1)this as costly dm.getTorrent().getAnnounceURL().getHost()
"" }, { "st:", "" + dm.getState() }, { "c:", comment }, { "f:", // defer (index = 4)
"" }, { "tag:", "" } };
Object o_name = name_mapping[0][1];
String tmpSearch = sLastSearch;
for (int i = 1; i < name_mapping.length; i++) {
if (tmpSearch.startsWith(name_mapping[i][0])) {
tmpSearch = tmpSearch.substring(name_mapping[i][0].length());
if (i == 1) {
List<String> names = new ArrayList<>();
o_name = names;
TOTorrent t = dm.getTorrent();
if (t != null) {
names.add(t.getAnnounceURL().getHost());
TOTorrentAnnounceURLSet[] sets = t.getAnnounceURLGroup().getAnnounceURLSets();
for (TOTorrentAnnounceURLSet set : sets) {
URL[] urls = set.getAnnounceURLs();
for (URL u : urls) {
names.add(u.getHost());
}
}
try {
byte[] hash = t.getHash();
names.add(ByteFormatter.encodeString(hash));
names.add(Base32.encode(hash));
} catch (Throwable e) {
}
}
} else if (i == 4) {
List<String> names = new ArrayList<>();
o_name = names;
DiskManagerFileInfoSet file_set = dm.getDiskManagerFileInfoSet();
DiskManagerFileInfo[] files = file_set.getFiles();
for (DiskManagerFileInfo f : files) {
File file = f.getFile(true);
String name = tmpSearch.contains(File.separator) ? file.getAbsolutePath() : file.getName();
names.add(name);
}
} else if (i == 5) {
List<String> names = new ArrayList<String>();
o_name = names;
TagManager tagManager = TagManagerFactory.getTagManager();
List<Tag> tags = tagManager.getTagsForTaggable(TagType.TT_DOWNLOAD_MANUAL, dm);
if (tags.size() > 0) {
tags = TagUIUtils.sortTags(tags);
for (Tag t : tags) {
String str = t.getTagName(true);
names.add(str);
}
}
} else {
o_name = name_mapping[i][1];
}
}
}
String s = bRegexSearch ? tmpSearch : "\\Q" + tmpSearch.replaceAll("[|;]", "\\\\E|\\\\Q") + "\\E";
boolean match_result = true;
if (bRegexSearch && s.startsWith("!")) {
s = s.substring(1);
match_result = false;
}
Pattern pattern = RegExUtil.getCachedPattern("tv:search", s, Pattern.CASE_INSENSITIVE);
if (o_name instanceof String) {
bOurs = pattern.matcher((String) o_name).find() == match_result;
} else {
List<String> names = (List<String>) o_name;
// match_result: true -> at least one match; false -> any fail
bOurs = !match_result;
for (String name : names) {
if (pattern.matcher(name).find()) {
bOurs = match_result;
break;
}
}
}
} catch (Exception e) {
// Future: report PatternSyntaxException message to user.
bOurs = true;
}
} else {
bOurs = true;
}
return bOurs;
}
use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class TorrentUIUtilsV3 method getContentImage.
/**
* Retrieves the thumbnail for the content, pulling it from the web if
* it can
*
* @param datasource
* @param l When the thumbnail is available, this listener is triggered
* @return If the image is immediately available, the image will be returned
* as well as the trigger being fired. If the image isn't available
* null will be returned and the listener will trigger when avail
*
* @since 4.0.0.5
*/
public static Image[] getContentImage(Object datasource, boolean big, final ContentImageLoadedListener l) {
if (l == null) {
return null;
}
TOTorrent torrent = DataSourceUtils.getTorrent(datasource);
if (torrent == null) {
l.contentImageLoaded(null, true);
return null;
}
if (imageLoaderThumb == null) {
imageLoaderThumb = new ImageLoader(null, null);
}
String thumbnailUrl = PlatformTorrentUtils.getContentThumbnailUrl(torrent);
// System.out.println("thumburl= " + thumbnailUrl);
if (thumbnailUrl != null && imageLoaderThumb.imageExists(thumbnailUrl)) {
// System.out.println("return thumburl");
Image image = imageLoaderThumb.getImage(thumbnailUrl);
l.contentImageLoaded(image, true);
return new Image[] { image };
}
String hash = null;
try {
hash = torrent.getHashWrapper().toBase32String();
} catch (TOTorrentException e) {
}
if (hash == null) {
l.contentImageLoaded(null, true);
return null;
}
int thumbnailVersion = PlatformTorrentUtils.getContentVersion(torrent);
// add torrent size here to differentiate meta-data downloads from actuals
final String id = "Thumbnail." + hash + "." + torrent.getSize() + "." + thumbnailVersion;
Image image = imageLoaderThumb.imageAdded(id) ? imageLoaderThumb.getImage(id) : null;
// System.out.println("image = " + image);
if (image != null && !image.isDisposed()) {
l.contentImageLoaded(image, true);
return new Image[] { image };
}
final byte[] imageBytes = PlatformTorrentUtils.getContentThumbnail(torrent);
// System.out.println("imageBytes = " + imageBytes);
if (imageBytes != null) {
image = (Image) Utils.execSWTThreadWithObject("thumbcreator", new AERunnableObject() {
@Override
public Object runSupport() {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
Image image = new Image(Display.getDefault(), bis);
return image;
} catch (Throwable e) {
return (null);
}
}
}, 500);
}
/**
* if ((image == null || image.isDisposed()) && thumbnailUrl != null) {
* //System.out.println("get image from " + thumbnailUrl);
* image = imageLoader.getUrlImage(thumbnailUrl,
* new ImageDownloaderListener() {
* public void imageDownloaded(Image image, boolean returnedImmediately) {
* l.contentImageLoaded(image, returnedImmediately);
* //System.out.println("got image from thumburl");
* }
* });
* //System.out.println("returning " + image + " (url loading)");
* return image == null ? null : new Image[] { image };
* }
*/
if (image == null || image.isDisposed()) {
// System.out.println("build image from files");
DownloadManager dm = DataSourceUtils.getDM(datasource);
/*
* Try to get an image from the OS
*/
String path = null;
if (dm == null) {
TOTorrentFile[] files = torrent.getFiles();
if (files.length > 0) {
path = files[0].getRelativePath();
}
} else {
DiskManagerFileInfo primaryFile = dm.getDownloadState().getPrimaryFile();
path = primaryFile == null ? null : primaryFile.getFile(true).getName();
}
if (path != null) {
image = ImageRepository.getPathIcon(path, big, false);
if (image != null && !torrent.isSimpleTorrent()) {
Image[] images = new Image[] { image, ImageRepository.getPathIcon(new File(path).getParent(), false, false) };
return images;
}
}
if (image == null) {
imageLoaderThumb.addImageNoDipose(id, ImageLoader.noImage);
} else {
imageLoaderThumb.addImageNoDipose(id, image);
}
} else {
// System.out.println("has mystery image");
imageLoaderThumb.addImage(id, image);
}
l.contentImageLoaded(image, true);
return new Image[] { image };
}
use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class DownloadManagerImpl method deletePartialDataFiles.
protected void deletePartialDataFiles() {
DiskManagerFileInfo[] files = getDiskManagerFileInfoSet().getFiles();
String abs_root = torrent_save_location.getAbsolutePath();
for (DiskManagerFileInfo file : files) {
if (!file.isSkipped()) {
continue;
}
if (file.getDownloaded() == file.getLength()) {
continue;
}
// user may have switched a partially completed file to DND for some reason - be safe
// and only delete compact files
int storage_type = file.getStorageType();
if (storage_type == DiskManagerFileInfo.ST_COMPACT || storage_type == DiskManagerFileInfo.ST_REORDER_COMPACT) {
File f = file.getFile(true);
if (f.exists()) {
if (f.delete()) {
File parent = f.getParentFile();
while (parent != null) {
if (parent.isDirectory() && parent.listFiles().length == 0) {
if (parent.getAbsolutePath().startsWith(abs_root)) {
if (!parent.delete()) {
Debug.outNoStack("Failed to remove empty directory: " + parent);
break;
} else {
parent = parent.getParentFile();
}
} else {
break;
}
} else {
break;
}
}
} else {
Debug.outNoStack("Failed to remove partial: " + f);
}
}
}
}
}
use of com.biglybt.core.disk.DiskManagerFileInfo in project BiglyBT by BiglySoftware.
the class GlobalManagerImpl method fixLongFileName.
void fixLongFileName(DownloadManager manager) {
// "File name too long" test
// Note: This only addresses the case where the filename is too
// long, not the parent directory
DiskManagerFileInfo[] fileInfos = manager.getDiskManagerFileInfo();
DownloadManagerState state = manager.getDownloadState();
try {
state.suppressStateSave(true);
for (int i = 0; i < fileInfos.length; i++) {
DiskManagerFileInfo fileInfo = fileInfos[i];
File base_file = fileInfo.getFile(false);
File existing_link = state.getFileLink(i, base_file);
if (existing_link == null && !base_file.exists()) {
String name = base_file.getName();
String ext = FileUtil.getExtension(name);
int extLength = ext.length();
name = name.substring(0, name.length() - extLength);
// Java appears to be pretending
// each unicode character is 3 bytes long. If the limit
// on a name is 256 two byte characters, then in theory,
// the limit is 170 unicode characters. Instead of assuming
// that's the case, let's just walk back until the name
// is accepted.
// Bail at 50 just for the fun of it (plus most filenames
// are short, so we can skip the Canonical call)
int origLength = name.length();
if (origLength > 50) {
File parentFile = base_file.getParentFile();
// We don't get "File name too long" on getCanonicalPath
// unless the dir is there
// I Wonder if we should remove the dirs after using them
// FMFileImpl will create dirs again
parentFile.mkdirs();
File newFile = null;
boolean first = true;
while (name.length() > 50) {
try {
newFile = new File(parentFile, name + ext);
newFile.getCanonicalPath();
if (first) {
break;
}
// it worked, but the new name might already exist
// always 3 digits :)
int fixNameID = 0xFF;
boolean redo;
do {
redo = false;
for (int j = 0; j < i; j++) {
DiskManagerFileInfo convertedFileInfo = fileInfos[j];
if (newFile.equals(convertedFileInfo.getFile(true))) {
do {
fixNameID++;
if (fixNameID >= 0xFFF) {
// exit, will fail later :(
break;
}
name = name.substring(0, name.length() - 3) + Integer.toHexString(fixNameID);
newFile = new File(parentFile, name + ext);
} while (newFile.equals(convertedFileInfo.getFile(true)));
redo = fixNameID <= 0xFFF;
break;
}
}
} while (redo);
if (fixNameID <= 0xFFF) {
state.setFileLink(i, base_file, newFile);
}
break;
} catch (IOException e) {
first = false;
name = name.substring(0, name.length() - 1);
} catch (Throwable t) {
Debug.out(t);
}
}
}
}
}
} finally {
state.suppressStateSave(false);
}
}
Aggregations