i2psnark:

- Add file type sorter
 - Cycle through name/type sorters
This commit is contained in:
zzz
2014-09-11 16:36:14 +00:00
parent 9ab766375d
commit 0817b58b9d
2 changed files with 60 additions and 6 deletions

View File

@ -464,14 +464,26 @@ public class I2PSnarkServlet extends BasicServlet {
out.write("</a><br>\n");
}
out.write("</th>\n<th colspan=\"2\" align=\"left\">");
// cycle through sort by name or type
boolean isTypeSort = false;
if (showSort) {
sort = (currentSort == null || "0".equals(currentSort) || "1".equals(currentSort)) ? "-1" : "";
if (currentSort == null || "0".equals(currentSort) || "1".equals(currentSort)) {
sort = "-1";
} else if ("-1".equals(currentSort)) {
sort = "12";
isTypeSort = true;
} else if ("12".equals(currentSort)) {
sort = "-12";
isTypeSort = true;
} else {
sort = "";
}
out.write("<a href=\"" + _contextPath + '/' + getQueryString(req, null, null, sort));
out.write("\">");
}
out.write("<img border=\"0\" src=\"" + _imgPath + "torrent.png\" title=\"");
if (showSort)
out.write(_("Sort by {0}", _("Torrent")));
out.write(_("Sort by {0}", (isTypeSort ? _("File type") : _("Torrent"))));
else
out.write(_("Torrent"));
out.write("\" alt=\"");
@ -1365,7 +1377,7 @@ public class I2PSnarkServlet extends BasicServlet {
} catch (NumberFormatException nfe) {}
}
try {
Collections.sort(rv, Sorters.getComparator(sort));
Collections.sort(rv, Sorters.getComparator(sort, this));
} catch (IllegalArgumentException iae) {
// Java 7 TimSort - may be unstable
}
@ -2977,7 +2989,12 @@ public class I2PSnarkServlet extends BasicServlet {
return buf.toString();
}
/** @since 0.7.14 */
/**
* Pick an icon; try to catch the common types in an i2p environment.
*
* @return file name not including ".png"
* @since 0.7.14
*/
private String toIcon(File item) {
if (item.isDirectory())
return "folder";
@ -2986,10 +3003,12 @@ public class I2PSnarkServlet extends BasicServlet {
/**
* Pick an icon; try to catch the common types in an i2p environment
* Pkg private for FileTypeSorter.
*
* @return file name not including ".png"
* @since 0.7.14
*/
private String toIcon(String path) {
String toIcon(String path) {
String icon;
// Note that for this to work well, our custom mime.properties file must be loaded.
String plc = path.toLowerCase(Locale.US);

View File

@ -6,6 +6,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;
import org.klomp.snark.MetaInfo;
import org.klomp.snark.Snark;
/**
@ -30,9 +31,12 @@ class Sorters {
*<li>9: Up rate
*<li>10: Remaining (needed)
*<li>11: Upload ratio
*<li>11: File type
*</ul>
*
* @param servlet for file type callback only
*/
public static Comparator<Snark> getComparator(int type) {
public static Comparator<Snark> getComparator(int type, I2PSnarkServlet servlet) {
boolean rev = type < 0;
Comparator<Snark> rv;
switch (type) {
@ -96,6 +100,11 @@ class Sorters {
rv = new RatioComparator(rev);
break;
case -12:
case 12:
rv = new FileTypeComparator(rev, servlet);
break;
}
return rv;
}
@ -303,4 +312,30 @@ class Sorters {
return compLong(ld, rd);
}
}
private static class FileTypeComparator extends Sort {
private final I2PSnarkServlet servlet;
public FileTypeComparator(boolean rev, I2PSnarkServlet servlet) {
super(rev);
this.servlet = servlet;
}
public int compareIt(Snark l, Snark r) {
String ls = toName(l);
String rs = toName(r);
return ls.compareTo(rs);
}
private String toName(Snark snark) {
MetaInfo meta = snark.getMetaInfo();
if (meta == null)
return "0";
if (meta.getFiles() != null)
return "1";
// arbitrary sort based on icon name
return servlet.toIcon(meta.getName());
}
}
}