package com.github.bhaak;

import java.io.File;

import com.yifanlu.Kindle.LauncherAction;
import com.yifanlu.Kindle.LauncherMenu;
import com.yifanlu.Kindle.Menuable;

public class HackedUpReaderExtension implements Menuable {

	@Override
	public void addItemsToMenu(LauncherMenu menu)
	{
		// sub menu in GUI launcher's  
		LauncherMenu topMenu = new LauncherMenu("HackedUpReader", 0);
		menu.addMenuItem(topMenu);

		// HackedUpReader location
		File cr3 = new File("/mnt/us/cr3xcb/bin/cr3");

		// e-books location
		File folder = new File("/mnt/us/documents/");
		addEBooksToMenu(topMenu, folder, cr3);
	}

	private void addEBooksToMenu(LauncherMenu topMenu, File folder, File cr3) {
		File[] files = folder.listFiles(new BookFilter());
		for (int i=0; i<files.length; i++) {
			if (files[i].isFile()) {
				// add all e-books under folder
				LauncherAction launcherScript = new LauncherExecutable(
						files[i].getName(),
						i,
						cr3,
						files[i].getAbsolutePath());
				topMenu.addMenuItem(launcherScript);
			} else if (files[i].isDirectory()) {
				// add sub directories as sub menus
				LauncherMenu subMenu = new LauncherMenu(files[i].getName(), i);
				topMenu.addMenuItem(subMenu);
				addEBooksToMenu(subMenu, files[i], cr3);
			}
		}
	}

	class BookFilter implements java.io.FileFilter {
		@Override
		public boolean accept(File file) {
			String filename = file.getName().toLowerCase();
			if (filename.endsWith(".epub") ||
				filename.endsWith(".fb2") ||
				filename.endsWith(".txt") ||
				filename.endsWith(".rtf") ||
				filename.endsWith(".html") ||
				filename.endsWith(".htm") ||
				filename.endsWith(".pdb")) {
				return true;
			} else if (file.isDirectory()) {
				// ignore dictionaries directory
				if (filename.equals("dictionaries")) { return false; }

				// ignore sdr directories generated by Amazon's
				// standard reader
				return !filename.endsWith(".sdr");
			}
			return false;
		}
	}
}
package com.github.bhaak;

import java.io.File;

import com.yifanlu.Kindle.LauncherAction;
import com.yifanlu.Kindle.LauncherMenu;
import com.yifanlu.Kindle.Menuable;

public class HackedUpReaderExtension implements Menuable {

	@Override
	public void addItemsToMenu(LauncherMenu menu)
	{
		// sub menu in GUI launcher's  
		LauncherMenu topMenu = new LauncherMenu("HackedUpReader", 0);
		menu.addMenuItem(topMenu);

		// HackedUpReader location
		File cr3 = new File("/mnt/us/cr3xcb/bin/cr3");

		// launch HackedUpReader with last read e-book
		LauncherAction launcherScript = new LauncherExecutable(
				"Open last read e-book",
				0,
				cr3,
				"--last-book");
		topMenu.addMenuItem(launcherScript);
		
		// e-books location
		File folder = new File("/mnt/us/documents/");
		addEBooksToMenu(topMenu, folder, cr3);
	}

	private void addEBooksToMenu(LauncherMenu topMenu, File folder, File cr3) {
		File[] files = folder.listFiles(new BookFilter());
		for (int i=0; i<files.length; i++) {
			if (files[i].isFile()) {
				// add all e-books under folder
				LauncherAction launcherScript = new LauncherExecutable(
						files[i].getName(),
						i,
						cr3,
						files[i].getAbsolutePath());
				topMenu.addMenuItem(launcherScript);
			} else if (files[i].isDirectory()) {
				// add sub directories as sub menus
				LauncherMenu subMenu = new LauncherMenu(files[i].getName(), i);
				topMenu.addMenuItem(subMenu);
				addEBooksToMenu(subMenu, files[i], cr3);
			}
		}
	}

	class BookFilter implements java.io.FileFilter {
		@Override
		public boolean accept(File file) {
			String filename = file.getName().toLowerCase();
			if (filename.endsWith(".epub") ||
				filename.endsWith(".fb2") ||
				filename.endsWith(".txt") ||
				filename.endsWith(".rtf") ||
				filename.endsWith(".html") ||
				filename.endsWith(".htm") ||
				filename.endsWith(".pdb")) {
				return true;
			} else if (file.isDirectory()) {
				// ignore dictionaries directory
				if (filename.equals("dictionaries")) { return false; }

				// ignore sdr directories generated by Amazon's
				// standard reader
				return !filename.endsWith(".sdr");
			}
			return false;
		}
	}
}
add "Open last read e-book" entry to top menu