* i2psnark: Display base name, not torrent file name (ticket #985)

This commit is contained in:
zzz
2013-10-26 20:42:16 +00:00
parent e04646bd37
commit 7316c82ef3

View File

@ -1117,55 +1117,32 @@ public class I2PSnarkServlet extends BasicServlet {
/** /**
* Sort alphabetically in current locale, ignore case, ignore leading "the " * Sort alphabetically in current locale, ignore case, ignore leading "the "
* (I guess this is worth it, a lot of torrents start with "The " * (I guess this is worth it, a lot of torrents start with "The "
* These are full path names which makes it harder
* @since 0.7.14 * @since 0.7.14
*/ */
private class TorrentNameComparator implements Comparator<String> { private static class TorrentNameComparator implements Comparator<Snark> {
private final Comparator collator = Collator.getInstance(); private final Comparator collator = Collator.getInstance();
private final String skip;
public TorrentNameComparator() { public int compare(Snark l, Snark r) {
String s; // put downloads and magnets first
try { if (l.getStorage() == null && r.getStorage() != null)
s = _manager.getDataDir().getCanonicalPath(); return -1;
} catch (IOException ioe) { if (l.getStorage() != null && r.getStorage() == null)
s = _manager.getDataDir().getAbsolutePath(); return 1;
} String ls = l.getBaseName();
skip = s + File.separator; String llc = ls.toLowerCase(Locale.US);
}
public int compare(String l, String r) {
if (l.startsWith(skip))
l = l.substring(skip.length());
if (r.startsWith(skip))
r = r.substring(skip.length());
String llc = l.toLowerCase(Locale.US);
if (llc.startsWith("the ") || llc.startsWith("the.") || llc.startsWith("the_")) if (llc.startsWith("the ") || llc.startsWith("the.") || llc.startsWith("the_"))
l = l.substring(4); ls = ls.substring(4);
String rlc = r.toLowerCase(Locale.US); String rs = r.getBaseName();
String rlc = rs.toLowerCase(Locale.US);
if (rlc.startsWith("the ") || rlc.startsWith("the.") || rlc.startsWith("the_")) if (rlc.startsWith("the ") || rlc.startsWith("the.") || rlc.startsWith("the_"))
r = r.substring(4); rs = rs.substring(4);
return collator.compare(l, r); return collator.compare(ls, rs);
} }
} }
private List<Snark> getSortedSnarks(HttpServletRequest req) { private List<Snark> getSortedSnarks(HttpServletRequest req) {
Set<String> files = _manager.listTorrentFiles(); ArrayList<Snark> rv = new ArrayList(_manager.getTorrents());
TreeSet<String> fileNames = new TreeSet(new TorrentNameComparator()); Collections.sort(rv, new TorrentNameComparator());
fileNames.addAll(files);
ArrayList<Snark> rv = new ArrayList(fileNames.size());
int magnet = 0;
for (Iterator iter = fileNames.iterator(); iter.hasNext(); ) {
String name = (String)iter.next();
Snark snark = _manager.getTorrent(name);
if (snark != null) {
// put downloads and magnets first
if (snark.getStorage() == null)
rv.add(magnet++, snark);
else
rv.add(snark);
}
}
return rv; return rv;
} }
@ -1201,23 +1178,12 @@ public class I2PSnarkServlet extends BasicServlet {
if (statsOnly) if (statsOnly)
return; return;
String filename = snark.getName(); String basename = snark.getBaseName();
if (snark.getMetaInfo() != null) { if (basename.length() > MAX_DISPLAYED_FILENAME_LENGTH) {
// Only do this if not a magnet or torrent download String start = basename.substring(0, MAX_DISPLAYED_FILENAME_LENGTH);
// Strip full path down to the local name
File f = new File(filename);
filename = f.getName();
}
int i = filename.lastIndexOf(".torrent");
if (i > 0)
filename = filename.substring(0, i);
String fullFilename = filename;
if (filename.length() > MAX_DISPLAYED_FILENAME_LENGTH) {
String start = filename.substring(0, MAX_DISPLAYED_FILENAME_LENGTH);
if (start.indexOf(" ") < 0 && start.indexOf("-") < 0) { if (start.indexOf(" ") < 0 && start.indexOf("-") < 0) {
// browser has nowhere to break it // browser has nowhere to break it
fullFilename = filename; basename = start + "&hellip;";
filename = start + "&hellip;";
} }
} }
// includes skipped files, -1 for magnet mode // includes skipped files, -1 for magnet mode
@ -1360,7 +1326,7 @@ public class I2PSnarkServlet extends BasicServlet {
out.write(trackerLink); out.write(trackerLink);
} }
String encodedBaseName = urlEncode(snark.getBaseName()); String encodedBaseName = urlEncode(basename);
// File type icon column // File type icon column
out.write("</td>\n<td>"); out.write("</td>\n<td>");
if (isValid) { if (isValid) {
@ -1403,7 +1369,7 @@ public class I2PSnarkServlet extends BasicServlet {
buf.append("\">"); buf.append("\">");
out.write(buf.toString()); out.write(buf.toString());
} }
out.write(filename); out.write(basename);
if (remaining == 0 || isMultiFile) if (remaining == 0 || isMultiFile)
out.write("</a>"); out.write("</a>");
@ -1474,7 +1440,7 @@ public class I2PSnarkServlet extends BasicServlet {
// Can't figure out how to escape double quotes inside the onclick string. // Can't figure out how to escape double quotes inside the onclick string.
// Single quotes in translate strings with parameters must be doubled. // Single quotes in translate strings with parameters must be doubled.
// Then the remaining single quote must be escaped // Then the remaining single quote must be escaped
out.write(_("Are you sure you want to delete the file \\''{0}.torrent\\'' (downloaded data will not be deleted) ?", fullFilename)); out.write(_("Are you sure you want to delete the file \\''{0}.torrent\\'' (downloaded data will not be deleted) ?", basename));
out.write("')) { return false; }\""); out.write("')) { return false; }\"");
out.write(" src=\"" + _imgPath + "remove.png\" alt=\""); out.write(" src=\"" + _imgPath + "remove.png\" alt=\"");
out.write(_("Remove")); out.write(_("Remove"));
@ -1494,7 +1460,7 @@ public class I2PSnarkServlet extends BasicServlet {
// Can't figure out how to escape double quotes inside the onclick string. // Can't figure out how to escape double quotes inside the onclick string.
// Single quotes in translate strings with parameters must be doubled. // Single quotes in translate strings with parameters must be doubled.
// Then the remaining single quote must be escaped // Then the remaining single quote must be escaped
out.write(_("Are you sure you want to delete the torrent \\''{0}\\'' and all downloaded data?", fullFilename)); out.write(_("Are you sure you want to delete the torrent \\''{0}\\'' and all downloaded data?", basename));
out.write("')) { return false; }\""); out.write("')) { return false; }\"");
out.write(" src=\"" + _imgPath + "delete.png\" alt=\""); out.write(" src=\"" + _imgPath + "delete.png\" alt=\"");
out.write(_("Delete")); out.write(_("Delete"));