import be.nikiroo.fanfix.data.Story;
import be.nikiroo.fanfix.output.BasicOutput;
import be.nikiroo.fanfix.output.BasicOutput.OutputType;
+import be.nikiroo.fanfix.output.InfoCover;
import be.nikiroo.fanfix.supported.BasicSupport;
import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
import be.nikiroo.fanfix.supported.InfoReader;
}
/**
- * List all the known types of stories.
+ * List all the known types (sources) of stories.
*
* @return the types
*/
* @return the stories
*/
public synchronized List<MetaData> getListByType(String type) {
+ if (type != null) {
+ // convert the type to dir name
+ type = getDir(type).getName();
+ }
+
List<MetaData> list = new ArrayList<MetaData>();
for (Entry<MetaData, File> entry : getStories(null).entrySet()) {
String storyType = entry.getValue().getParentFile().getName();
key.setLuid(luid);
}
- getDir(key).mkdirs();
- if (!getDir(key).exists()) {
+ getDir(key.getSource()).mkdirs();
+ if (!getDir(key.getSource()).exists()) {
throw new IOException("Cannot create library dir");
}
public synchronized boolean delete(String luid) {
boolean ok = false;
+ List<File> files = getFiles(luid);
+ if (!files.isEmpty()) {
+ for (File file : files) {
+ IOUtils.deltree(file);
+ }
+
+ ok = true;
+
+ // clear cache
+ stories.clear();
+ }
+
+ return ok;
+ }
+
+ /**
+ * Change the type (source) of the given {@link Story}.
+ *
+ * @param luid
+ * the {@link Story} LUID
+ * @param newSourcethe
+ * new source
+ *
+ * @return TRUE if the {@link Story} was found
+ */
+ public synchronized boolean changeType(String luid, String newType) {
+ MetaData meta = getInfo(luid);
+ if (meta != null) {
+ meta.setSource(newType);
+ File newDir = getDir(meta.getSource());
+ if (!newDir.exists()) {
+ newDir.mkdir();
+ }
+
+ List<File> files = getFiles(luid);
+ for (File file : files) {
+ if (file.getName().endsWith(".info")) {
+ try {
+ String name = file.getName().replaceFirst("\\.info$",
+ "");
+ InfoCover.writeInfo(newDir, name, meta);
+ file.delete();
+ } catch (IOException e) {
+ Instance.syserr(e);
+ }
+ } else {
+ file.renameTo(new File(newDir, file.getName()));
+ }
+ }
+
+ // clear cache
+ stories.clear();
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return the list of files/dirs on disk for this {@link Story}.
+ * <p>
+ * If the {@link Story} is not found, and empty list is returned.
+ *
+ * @param luid
+ * the {@link Story} LUID
+ *
+ * @return the list of {@link File}s
+ */
+ private List<File> getFiles(String luid) {
+ List<File> files = new ArrayList<File>();
+
MetaData meta = getInfo(luid);
File file = getStories(null).get(meta);
if (file != null) {
- if (file.delete()) {
- String readerExt = getOutputType(meta)
- .getDefaultExtension(true);
- String fileExt = getOutputType(meta).getDefaultExtension(false);
-
- String path = file.getAbsolutePath();
- if (readerExt != null && !readerExt.equals(fileExt)) {
- path = path
- .substring(0, path.length() - readerExt.length())
- + fileExt;
- file = new File(path);
- IOUtils.deltree(file);
- }
+ files.add(file);
- File infoFile = new File(path + ".info");
- if (!infoFile.exists()) {
- infoFile = new File(path.substring(0, path.length()
- - fileExt.length())
- + ".info");
- }
- infoFile.delete();
-
- String coverExt = "."
- + Instance.getConfig().getString(
- Config.IMAGE_FORMAT_COVER);
- File coverFile = new File(path + coverExt);
- if (!coverFile.exists()) {
- coverFile = new File(path.substring(0, path.length()
- - fileExt.length()));
+ String readerExt = getOutputType(meta).getDefaultExtension(true);
+ String fileExt = getOutputType(meta).getDefaultExtension(false);
+
+ String path = file.getAbsolutePath();
+ if (readerExt != null && !readerExt.equals(fileExt)) {
+ path = path.substring(0, path.length() - readerExt.length())
+ + fileExt;
+ file = new File(path);
+
+ if (file.exists()) {
+ files.add(file);
}
- coverFile.delete();
+ }
- ok = true;
+ File infoFile = new File(path + ".info");
+ if (!infoFile.exists()) {
+ infoFile = new File(path.substring(0,
+ path.length() - fileExt.length())
+ + ".info");
}
- // clear cache
- stories.clear();
+ if (infoFile.exists()) {
+ files.add(infoFile);
+ }
+
+ String coverExt = "."
+ + Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER);
+ File coverFile = new File(path + coverExt);
+ if (!coverFile.exists()) {
+ coverFile = new File(path.substring(0,
+ path.length() - fileExt.length())
+ + coverExt);
+ }
+
+ if (coverFile.exists()) {
+ files.add(coverFile);
+ }
}
- return ok;
+ return files;
}
/**
* The directory (full path) where the {@link Story} related to this
* {@link MetaData} should be located on disk.
*
- * @param key
- * the {@link Story} {@link MetaData}
+ * @param type
+ * the type (source)
*
* @return the target directory
*/
- private File getDir(MetaData key) {
- String source = key.getSource().replaceAll("[^a-zA-Z0-9._+-]", "_");
+ private File getDir(String type) {
+ String source = type.replaceAll("[^a-zA-Z0-9._+-]", "_");
return new File(baseDir, source);
}
title = "";
}
title = title.replaceAll("[^a-zA-Z0-9._+-]", "_");
- return new File(getDir(key), key.getLuid() + "_" + title);
+ return new File(getDir(key.getSource()), key.getLuid() + "_" + title);
}
/**
import java.io.File;
import java.io.IOException;
import java.net.URL;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
* the selected type or author.
*
* @param value
- * the author or the type
+ * the author or the type, or NULL to get all the
+ * authors-or-types
* @param type
* TRUE for type, FALSE for author
*/
popup.add(createMenuItemOpenBook());
popup.addSeparator();
popup.add(createMenuItemExport());
+ popup.add(createMenuItemMove());
popup.add(createMenuItemClearCache());
popup.add(createMenuItemRedownload());
popup.addSeparator();
file.add(createMenuItemOpenBook());
file.add(createMenuItemExport());
+ file.add(createMenuItemMove());
file.addSeparator();
file.add(imprt);
file.add(imprtF);
return refresh;
}
+ /**
+ * Create the delete menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemMove() {
+ JMenu moveTo = new JMenu("Move to...");
+ moveTo.setMnemonic(KeyEvent.VK_M);
+
+ List<String> types = new ArrayList<String>();
+ types.add(null);
+ types.addAll(Instance.getLibrary().getTypes());
+
+ for (String type : types) {
+ JMenuItem item = new JMenuItem(type == null ? "New type..." : type);
+
+ moveTo.add(item);
+ if (type == null) {
+ moveTo.addSeparator();
+ }
+
+ final String ftype = type;
+ item.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if (selectedBook != null) {
+ String type = ftype;
+ if (type == null) {
+ Object rep = JOptionPane.showInputDialog(
+ LocalReaderFrame.this, "Move to:",
+ "Moving story",
+ JOptionPane.QUESTION_MESSAGE, null, null,
+ selectedBook.getMeta().getSource());
+ if (rep == null) {
+ return;
+ } else {
+ type = rep.toString();
+ }
+ }
+
+ final String ftype = type;
+ outOfUi(null, new Runnable() {
+ public void run() {
+ reader.changeType(selectedBook.getMeta()
+ .getLuid(), ftype);
+
+ selectedBook = null;
+
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ setJMenuBar(createMenu());
+ }
+ });
+ }
+ });
+ }
+ }
+ });
+ }
+
+ return moveTo;
+ }
+
/**
* Create the redownload (then delete original) menu item.
*