code cleanup
[fanfix.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
index e040a210cb3aaf5e4dbc7ef46721e86b38c5a86b..03cb22738b37b1f25cb4ff9c5c73d2d4bcb5ff82 100644 (file)
@@ -1,6 +1,7 @@
 package be.nikiroo.fanfix.reader.cli;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
 import be.nikiroo.fanfix.Instance;
@@ -9,8 +10,11 @@ import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.fanfix.reader.FanfixReader;
-import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
+import be.nikiroo.fanfix.reader.BasicReader;
+import be.nikiroo.fanfix.searchable.BasicSearchable;
+import be.nikiroo.fanfix.searchable.SearchableTag;
+import be.nikiroo.fanfix.supported.SupportType;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * Command line {@link Story} reader.
@@ -19,33 +23,26 @@ import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
  * 
  * @author niki
  */
-public class CliReader extends FanfixReader {
-       /**
-        * Start the {@link Story} Reading.
-        * 
-        * @throws IOException
-        *             in case of I/O error or if the {@link Story} was not
-        *             previously set
-        */
-       public void read() throws IOException {
-               if (getStory() == null) {
+class CliReader extends BasicReader {
+       @Override
+       public void read(boolean sync) throws IOException {
+               MetaData meta = getMeta();
+
+               if (meta == null) {
                        throw new IOException("No story to read");
                }
 
                String title = "";
                String author = "";
 
-               MetaData meta = getStory().getMeta();
-               if (meta != null) {
-                       if (meta.getTitle() != null) {
-                               title = meta.getTitle();
-                       }
+               if (meta.getTitle() != null) {
+                       title = meta.getTitle();
+               }
 
-                       if (meta.getAuthor() != null) {
-                               author = "©" + meta.getAuthor();
-                               if (meta.getDate() != null && !meta.getDate().isEmpty()) {
-                                       author = author + " (" + meta.getDate() + ")";
-                               }
+               if (meta.getAuthor() != null) {
+                       author = "©" + meta.getAuthor();
+                       if (meta.getDate() != null && !meta.getDate().isEmpty()) {
+                               author = author + " (" + meta.getDate() + ")";
                        }
                }
 
@@ -53,7 +50,8 @@ public class CliReader extends FanfixReader {
                System.out.println(author);
                System.out.println("");
 
-               for (Chapter chap : getStory()) {
+               // TODO: progress?
+               for (Chapter chap : getStory(null)) {
                        if (chap.getName() != null && !chap.getName().isEmpty()) {
                                System.out.println(Instance.getTrans().getString(
                                                StringId.CHAPTER_NAMED, chap.getNumber(),
@@ -65,17 +63,18 @@ public class CliReader extends FanfixReader {
                }
        }
 
-       /**
-        * Read the selected chapter (starting at 1).
-        * 
-        * @param chapter
-        *            the chapter
-        */
-       public void read(int chapter) {
-               if (chapter > getStory().getChapters().size()) {
+       public void read(int chapter) throws IOException {
+               MetaData meta = getMeta();
+
+               if (meta == null) {
+                       throw new IOException("No story to read");
+               }
+
+               // TODO: progress?
+               if (chapter > getStory(null).getChapters().size()) {
                        System.err.println("Chapter " + chapter + ": no such chapter");
                } else {
-                       Chapter chap = getStory().getChapters().get(chapter - 1);
+                       Chapter chap = getStory(null).getChapters().get(chapter - 1);
                        System.out.println("Chapter " + chap.getNumber() + ": "
                                        + chap.getName());
 
@@ -87,9 +86,9 @@ public class CliReader extends FanfixReader {
        }
 
        @Override
-       public void start(SupportType type) {
+       public void browse(String source) {
                List<MetaData> stories;
-               stories = Instance.getLibrary().getList(type);
+               stories = getLibrary().getListBySource(source);
 
                for (MetaData story : stories) {
                        String author = "";
@@ -101,4 +100,144 @@ public class CliReader extends FanfixReader {
                                        + author);
                }
        }
+
+       @Override
+       public void search(SupportType searchOn, String keywords, int page, int item)
+                       throws IOException {
+
+       }
+
+       @Override
+       public void searchTag(SupportType searchOn, int page, int item,
+                       String... tags) throws IOException {
+               BasicSearchable search = BasicSearchable.getSearchable(searchOn);
+               List<SearchableTag> stags = search.getTags();
+
+               SearchableTag stag = null;
+               for (String tag : tags) {
+                       stag = null;
+                       for (int i = 0; i < stags.size(); i++) {
+                               if (stags.get(i).getName().equalsIgnoreCase(tag)) {
+                                       stag = stags.get(i);
+                                       break;
+                               }
+                       }
+
+                       if (stag != null) {
+                               search.fillTag(stag);
+                               stags = stag.getChildren();
+                       } else {
+                               stags = new ArrayList<SearchableTag>();
+                               break;
+                       }
+               }
+
+               if (stag != null) {
+                       if (page <= 0) {
+                               if (stag.isLeaf()) {
+                                       search.search(stag, 1);
+                                       System.out.println(stag.getPages());
+                               } else {
+                                       System.out.println(stag.getCount());
+                               }
+                       } else {
+                               List<MetaData> metas = null;
+                               List<SearchableTag> subtags = null;
+                               int count;
+
+                               if (stag.isLeaf()) {
+                                       metas = search.search(stag, page);
+                                       count = metas.size();
+                               } else {
+                                       subtags = stag.getChildren();
+                                       count = subtags.size();
+                               }
+
+                               if (item > 0) {
+                                       if (item <= count) {
+                                               if (metas != null) {
+                                                       MetaData meta = metas.get(item - 1);
+                                                       System.out.println(page + "/" + item + ": "
+                                                                       + meta.getTitle());
+                                                       System.out.println();
+                                                       System.out.println(meta.getUrl());
+                                                       System.out.println();
+                                                       System.out.println("Tags: " + meta.getTags());
+                                                       System.out.println();
+                                                       for (Paragraph para : meta.getResume()) {
+                                                               System.out.println(para.getContent());
+                                                               System.out.println("");
+                                                       }
+                                               } else {
+                                                       SearchableTag subtag = subtags.get(item - 1);
+
+                                                       String sp = "";
+                                                       if (subtag.getParent() != null) {
+                                                               List<String> parents = new ArrayList<String>();
+                                                               for (SearchableTag parent = subtag.getParent(); parent != null; parent = parent
+                                                                               .getParent()) {
+                                                                       parents.add(parent.getName());
+                                                               }
+                                                               for (String parent : parents) {
+                                                                       if (!sp.isEmpty()) {
+                                                                               sp += " / ";
+                                                                       }
+                                                                       sp += parent;
+                                                               }
+                                                       }
+
+                                                       // TODO: i18n
+                                                       String stories = "stories";
+                                                       String num = StringUtils.formatNumber(subtag
+                                                                       .getCount());
+                                                       if (sp.isEmpty()) {
+                                                               System.out.println(String.format(
+                                                                               "%d/%d: %s, %s %s", page, item,
+                                                                               subtag.getName(), num, stories));
+                                                       } else {
+                                                               System.out.println(String.format(
+                                                                               "%d/%d: %s (%s), %s %s", page, item,
+                                                                               subtag.getName(), sp, num, stories));
+                                                       }
+                                               }
+                                       } else {
+                                               System.out.println("Invalid item: only " + count
+                                                               + " items found");
+                                       }
+                               } else {
+                                       if (metas != null) {
+                                               int i = 0;
+                                               for (MetaData meta : metas) {
+                                                       System.out
+                                                                       .println((i + 1) + ": " + meta.getTitle());
+                                                       i++;
+                                               }
+                                       } else {
+                                               int i = 1;
+                                               for (SearchableTag subtag : subtags) {
+                                                       String total = "";
+                                                       if (subtag.getCount() > 0) {
+                                                               total = StringUtils.formatNumber(subtag
+                                                                               .getCount());
+                                                       }
+
+                                                       if (total.isEmpty()) {
+                                                               System.out.println(String.format("%d: %s", i,
+                                                                               subtag.getName()));
+                                                       } else {
+                                                               System.out.println(String.format("%d: %s (%s)",
+                                                                               i, subtag.getName(), total));
+                                                       }
+
+                                                       i++;
+                                               }
+                                       }
+                               }
+                       }
+               } else {
+                       for (SearchableTag s : stags) {
+                               System.out.println(s.getName());
+                       }
+               }
+       }
 }