Show new files in green and bold.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / triggers / NewEpisodeTrigger.java
index 745a1f8..075c6f6 100644 (file)
@@ -59,6 +59,8 @@ public class NewEpisodeTrigger implements Trigger {
        /** All changed episodes. */
        private final Collection<Episode> changedEpisodes = Sets.newHashSet();
 
+       /** All new torrent files. */
+       private final Collection<TorrentFile> newTorrentFiles = Sets.newHashSet();
 
        //
        // TRIGGER METHODS
@@ -74,6 +76,7 @@ public class NewEpisodeTrigger implements Trigger {
                newEpisodes.clear();
                changedEpisodes.clear();
                this.allEpisodes.clear();
+               newTorrentFiles.clear();
                Map<Episode, Episode> allEpisodes = Maps.newHashMap(FluentIterable.from(((EpisodeState) previousState).episodes()).toMap(new Function<Episode, Episode>() {
 
                        @Override
@@ -86,12 +89,16 @@ public class NewEpisodeTrigger implements Trigger {
                                allEpisodes.put(episode, episode);
                                newEpisodes.add(episode);
                        }
+                       Episode existingEpisode = allEpisodes.get(episode);
                        for (TorrentFile torrentFile : Lists.newArrayList(episode.torrentFiles())) {
-                               int oldSize = allEpisodes.get(episode).torrentFiles().size();
-                               allEpisodes.get(episode).addTorrentFile(torrentFile);
-                               int newSize = allEpisodes.get(episode).torrentFiles().size();
-                               if (!newEpisodes.contains(episode) && (oldSize != newSize)) {
-                                       changedEpisodes.add(episode);
+                               int oldSize = existingEpisode.torrentFiles().size();
+                               existingEpisode.addTorrentFile(torrentFile);
+                               int newSize = existingEpisode.torrentFiles().size();
+                               if (oldSize != newSize) {
+                                       newTorrentFiles.add(torrentFile);
+                               }
+                               if (!newEpisodes.contains(existingEpisode) && (oldSize != newSize)) {
+                                       changedEpisodes.add(existingEpisode);
                                }
                        }
                }
@@ -123,8 +130,8 @@ public class NewEpisodeTrigger implements Trigger {
                        summary = String.format("%d changed Torrent(s) for “%s!”", changedEpisodes.size(), reaction.name());
                }
                DefaultOutput output = new DefaultOutput(summary);
-               output.addText("text/plain", generatePlainText(reaction, newEpisodes, changedEpisodes, allEpisodes));
-               output.addText("text/html", generateHtmlText(reaction, newEpisodes, changedEpisodes, allEpisodes));
+               output.addText("text/plain", generatePlainText(reaction));
+               output.addText("text/html", generateHtmlText(reaction));
                return output;
        }
 
@@ -137,15 +144,9 @@ public class NewEpisodeTrigger implements Trigger {
         *
         * @param reaction
         *            The reaction that was triggered
-        * @param newEpisodes
-        *            The new episodes
-        * @param changedEpisodes
-        *            The changed episodes
-        * @param allEpisodes
-        *            All episodes
         * @return The plain text output
         */
-       private static String generatePlainText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes, Collection<Episode> allEpisodes) {
+       private String generatePlainText(Reaction reaction) {
                StringBuilder stringBuilder = new StringBuilder();
                if (!newEpisodes.isEmpty()) {
                        stringBuilder.append(reaction.name()).append(" - New Episodes\n\n");
@@ -206,70 +207,57 @@ public class NewEpisodeTrigger implements Trigger {
         *
         * @param reaction
         *            The reaction that was triggered
-        * @param newEpisodes
-        *            The new episodes
-        * @param changedEpisodes
-        *            The changed episodes
-        * @param allEpisodes
-        *            All episodes
         * @return The HTML output
         */
-       private static String generateHtmlText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes, Collection<Episode> allEpisodes) {
+       private String generateHtmlText(Reaction reaction) {
                StringBuilder htmlBuilder = new StringBuilder();
                htmlBuilder.append("<html><body>\n");
-               htmlBuilder.append("<h1>").append(StringEscapeUtils.escapeHtml4(reaction.name())).append("</h1>\n");
-               if (!newEpisodes.isEmpty()) {
-                       htmlBuilder.append("<h2>New Episodes</h2>\n");
-                       htmlBuilder.append("<ul>\n");
-                       for (Episode episode : newEpisodes) {
-                               htmlBuilder.append("<li>Season ").append(episode.season()).append(", Episode ").append(episode.episode()).append("</li>\n");
-                               htmlBuilder.append("<ul>\n");
+               /* show all known episodes. */
+               htmlBuilder.append("<table>\n<caption>All Known Episodes</caption>\n");
+               htmlBuilder.append("<thead>\n");
+               htmlBuilder.append("<tr>");
+               htmlBuilder.append("<th>Season</th>");
+               htmlBuilder.append("<th>Episode</th>");
+               htmlBuilder.append("<th>Filename</th>");
+               htmlBuilder.append("<th>Size</th>");
+               htmlBuilder.append("<th>File(s)</th>");
+               htmlBuilder.append("<th>Seeds</th>");
+               htmlBuilder.append("<th>Leechers</th>");
+               htmlBuilder.append("<th>Magnet</th>");
+               htmlBuilder.append("<th>Download</th>");
+               htmlBuilder.append("</tr>\n");
+               htmlBuilder.append("</thead>\n");
+               htmlBuilder.append("<tbody>\n");
+               Episode lastEpisode = null;
+               for (Entry<Integer, Collection<Episode>> seasonEntry : FluentIterable.from(Ordering.natural().reverse().sortedCopy(allEpisodes)).index(Episode.BY_SEASON).asMap().entrySet()) {
+                       for (Episode episode : seasonEntry.getValue()) {
                                for (TorrentFile torrentFile : episode) {
-                                       htmlBuilder.append("<li>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</li>\n");
-                                       htmlBuilder.append("<div>");
-                                       htmlBuilder.append("<strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong>, ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.fileCount()).append("</strong> file(s), ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.seedCount()).append("</strong> seed(s), ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>\n");
-                                       htmlBuilder.append("<div>");
-                                       if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
-                                               htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Magnet</a> ");
+                                       if (newEpisodes.contains(episode)) {
+                                               htmlBuilder.append("<tr style=\"color: #008000; font-weight: bold;\">");
+                                       } else if (newTorrentFiles.contains(torrentFile)) {
+                                               htmlBuilder.append("<tr style=\"color: #008000;\">");
+                                       } else {
+                                               htmlBuilder.append("<tr>");
                                        }
-                                       if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
-                                               htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Download</a>");
-                                       }
-                                       htmlBuilder.append("</div>\n");
-                               }
-                               htmlBuilder.append("</ul>\n");
-                       }
-                       htmlBuilder.append("</ul>\n");
-               }
-               if (!changedEpisodes.isEmpty()) {
-                       htmlBuilder.append("<h2>Changed Episodes</h2>\n");
-                       htmlBuilder.append("<ul>\n");
-                       for (Episode episode : changedEpisodes) {
-                               htmlBuilder.append("<li>Season ").append(episode.season()).append(", Episode ").append(episode.episode()).append("</li>\n");
-                               htmlBuilder.append("<ul>\n");
-                               for (TorrentFile torrentFile : episode) {
-                                       htmlBuilder.append("<li>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</li>\n");
-                                       htmlBuilder.append("<div>");
-                                       htmlBuilder.append("<strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong>, ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.fileCount()).append("</strong> file(s), ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.seedCount()).append("</strong> seed(s), ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>\n");
-                                       htmlBuilder.append("<div>");
-                                       if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
-                                               htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Magnet</a> ");
-                                       }
-                                       if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
-                                               htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Download</a>");
+                                       if ((lastEpisode == null) || !lastEpisode.equals(episode)) {
+                                               htmlBuilder.append("<td>").append(episode.season()).append("</td><td>").append(episode.episode()).append("</td>");
+                                       } else {
+                                               htmlBuilder.append("<td colspan=\"2\"></td>");
                                        }
-                                       htmlBuilder.append("</div>\n");
+                                       htmlBuilder.append("<td>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</td>");
+                                       htmlBuilder.append("<td>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</td>");
+                                       htmlBuilder.append("<td>").append(torrentFile.fileCount()).append("</td>");
+                                       htmlBuilder.append("<td>").append(torrentFile.seedCount()).append("</td>");
+                                       htmlBuilder.append("<td>").append(torrentFile.leechCount()).append("</td>");
+                                       htmlBuilder.append("<td><a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Link</a></td>");
+                                       htmlBuilder.append("<td><a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Link</a></td>");
+                                       htmlBuilder.append("</tr>\n");
+                                       lastEpisode = episode;
                                }
-                               htmlBuilder.append("</ul>\n");
                        }
-                       htmlBuilder.append("</ul>\n");
                }
+               htmlBuilder.append("</tbody>\n");
+               htmlBuilder.append("</table>\n");
                htmlBuilder.append("</body></html>\n");
                return htmlBuilder.toString();
        }