Changeset 0817b58


Ignore:
Timestamp:
Sep 11, 2014 4:36:14 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
42eb43f
Parents:
9ab7663
Message:

i2psnark:

  • Add file type sorter
  • Cycle through name/type sorters
Location:
apps/i2psnark/java/src/org/klomp/snark/web
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java

    r9ab7663 r0817b58  
    465465        }
    466466        out.write("</th>\n<th colspan=\"2\" align=\"left\">");
     467        // cycle through sort by name or type
     468        boolean isTypeSort = false;
    467469        if (showSort) {
    468             sort = (currentSort == null || "0".equals(currentSort) || "1".equals(currentSort)) ? "-1" : "";
     470            if (currentSort == null || "0".equals(currentSort) || "1".equals(currentSort)) {
     471                sort = "-1";
     472            } else if ("-1".equals(currentSort)) {
     473                sort = "12";
     474                isTypeSort = true;
     475            } else if ("12".equals(currentSort)) {
     476                sort = "-12";
     477                isTypeSort = true;
     478            } else {
     479                sort = "";
     480            }
    469481            out.write("<a href=\"" + _contextPath + '/' + getQueryString(req, null, null, sort));
    470482            out.write("\">");
     
    472484        out.write("<img border=\"0\" src=\"" + _imgPath + "torrent.png\" title=\"");
    473485        if (showSort)
    474             out.write(_("Sort by {0}", _("Torrent")));
     486            out.write(_("Sort by {0}", (isTypeSort ? _("File type") : _("Torrent"))));
    475487        else
    476488            out.write(_("Torrent"));
     
    13661378            }
    13671379            try {
    1368                 Collections.sort(rv, Sorters.getComparator(sort));
     1380                Collections.sort(rv, Sorters.getComparator(sort, this));
    13691381            } catch (IllegalArgumentException iae) {
    13701382                // Java 7 TimSort - may be unstable
     
    29782990    }
    29792991
    2980     /** @since 0.7.14 */
     2992    /**
     2993     *  Pick an icon; try to catch the common types in an i2p environment.
     2994     *
     2995     *  @return file name not including ".png"
     2996     *  @since 0.7.14
     2997     */
    29812998    private String toIcon(File item) {
    29822999        if (item.isDirectory())
     
    29873004    /**
    29883005     *  Pick an icon; try to catch the common types in an i2p environment
     3006     *  Pkg private for FileTypeSorter.
     3007     *
    29893008     *  @return file name not including ".png"
    29903009     *  @since 0.7.14
    29913010     */
    2992     private String toIcon(String path) {
     3011    String toIcon(String path) {
    29933012        String icon;
    29943013        // Note that for this to work well, our custom mime.properties file must be loaded.
  • apps/i2psnark/java/src/org/klomp/snark/web/Sorters.java

    r9ab7663 r0817b58  
    77import java.util.Locale;
    88
     9import org.klomp.snark.MetaInfo;
    910import org.klomp.snark.Snark;
    1011
     
    3132     *<li>10: Remaining (needed)
    3233     *<li>11: Upload ratio
     34     *<li>11: File type
    3335     *</ul>
     36     *
     37     *  @param servlet for file type callback only
    3438     */
    35     public static Comparator<Snark> getComparator(int type) {
     39    public static Comparator<Snark> getComparator(int type, I2PSnarkServlet servlet) {
    3640        boolean rev = type < 0;
    3741        Comparator<Snark> rv;
     
    9599          case 11:
    96100              rv = new RatioComparator(rev);
     101              break;
     102
     103          case -12:
     104          case 12:
     105              rv = new FileTypeComparator(rev, servlet);
    97106              break;
    98107
     
    304313        }
    305314    }
     315
     316    private static class FileTypeComparator extends Sort {
     317
     318        private final I2PSnarkServlet servlet;
     319
     320        public FileTypeComparator(boolean rev, I2PSnarkServlet servlet) {
     321            super(rev);
     322            this.servlet = servlet;
     323        }
     324
     325        public int compareIt(Snark l, Snark r) {
     326            String ls = toName(l);
     327            String rs = toName(r);
     328            return ls.compareTo(rs);
     329        }
     330
     331        private String toName(Snark snark) {
     332            MetaInfo meta = snark.getMetaInfo();
     333            if (meta == null)
     334                return "0";
     335            if (meta.getFiles() != null)
     336                return "1";
     337            // arbitrary sort based on icon name
     338            return servlet.toIcon(meta.getName());
     339        }
     340    }
    306341}
Note: See TracChangeset for help on using the changeset viewer.