Lot of fixes + first (bad, ugly) working GUI
[fanfix.git] / src / be / nikiroo / fanfix / Library.java
index 304f19f462e1a05a899e4b01de430e6c283abfef..b5f246c6abe8eaa3f9a034da164ee99f290434c8 100644 (file)
@@ -52,6 +52,23 @@ public class Library {
                dir.mkdirs();
        }
 
+       /**
+        * List all the known types of stories.
+        * 
+        * @return the types
+        */
+       public List<String> getTypes() {
+               List<String> list = new ArrayList<String>();
+               for (Entry<MetaData, File> entry : getStories().entrySet()) {
+                       String storyType = entry.getValue().getParentFile().getName();
+                       if (!list.contains(storyType)) {
+                               list.add(storyType);
+                       }
+               }
+
+               return list;
+       }
+
        /**
         * List all the stories of the given source type in the {@link Library}, or
         * all the stories if NULL is passed as a type.
@@ -61,13 +78,11 @@ public class Library {
         * 
         * @return the stories
         */
-       public List<MetaData> getList(SupportType type) {
-               String typeString = type == null ? null : type.getSourceName();
-
+       public List<MetaData> getList(String type) {
                List<MetaData> list = new ArrayList<MetaData>();
                for (Entry<MetaData, File> entry : getStories().entrySet()) {
                        String storyType = entry.getValue().getParentFile().getName();
-                       if (typeString == null || typeString.equalsIgnoreCase(storyType)) {
+                       if (type == null || type.equalsIgnoreCase(storyType)) {
                                list.add(entry.getKey());
                        }
                }
@@ -75,6 +90,26 @@ public class Library {
                return list;
        }
 
+       /**
+        * Retrieve a {@link File} corresponding to the given {@link Story}.
+        * 
+        * @param luid
+        *            the Library UID of the story
+        * 
+        * @return the corresponding {@link Story}
+        */
+       public MetaData getInfo(String luid) {
+               if (luid != null) {
+                       for (Entry<MetaData, File> entry : getStories().entrySet()) {
+                               if (luid.equals(entry.getKey().getLuid())) {
+                                       return entry.getKey();
+                               }
+                       }
+               }
+
+               return null;
+       }
+
        /**
         * Retrieve a {@link File} corresponding to the given {@link Story}.
         * 
@@ -174,6 +209,11 @@ public class Library {
                        throw new IOException("Output type not supported: " + type);
                }
 
+               Story story = getStory(luid);
+               if (story == null) {
+                       throw new IOException("Cannot find story to export: " + luid);
+               }
+
                return out.process(getStory(luid), target);
        }
 
@@ -207,7 +247,9 @@ public class Library {
         *             in case of I/O error
         */
        private Story save(Story story, String luid) throws IOException {
-               MetaData key = story.getMeta();
+               // Do not change the original metadata, but change the original story
+               MetaData key = story.getMeta().clone();
+               story.setMeta(key);
 
                if (luid == null || luid.isEmpty()) {
                        getStories(); // refresh lastId if needed