All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.e2u.PreviewController Maven / Gradle / Ivy

package com.googlecode.e2u;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.daisy.braille.utils.api.table.BrailleConstants;
import org.daisy.braille.utils.api.table.BrailleConverter;
import org.daisy.braille.utils.api.table.Table;
import org.daisy.braille.utils.api.table.TableCatalog;
import org.daisy.braille.utils.impl.tools.table.DefaultTableProvider;

import com.googlecode.e2u.BookReader.BookReaderResult;
import com.googlecode.e2u.l10n.L10nKeys;
import com.googlecode.e2u.l10n.Messages;

import shared.Settings;
import shared.Settings.Keys;

public class PreviewController {
	private static final Logger logger = Logger.getLogger(PreviewController.class.getCanonicalName());
	private final BookReader r;
	private Map done;
	private final Settings settings;
	private boolean saxonNotAvailable;
	private String brailleFont, textFont, charset;
	private long lastUpdated;

	/**
	 * Creates a new preview controller.
	 * 
	 * @param r the book reader
	 * @param settings the settings
	 */
	public PreviewController(final BookReader r, Settings settings) {
		this.settings = settings;
		this.r = r;
		done = Collections.synchronizedMap(new HashMap());
		update(false);
		brailleFont = settings.getString(Keys.brailleFont);
		textFont = settings.getString(Keys.textFont);
		charset = settings.getString(Keys.charset);
	}
	
	private void update(boolean force) {
		saxonNotAvailable = false;
		if (!force && lastUpdated+10000>System.currentTimeMillis()) {
			return;
		}
		lastUpdated = System.currentTimeMillis();
		try {
			BookReaderResult brr = r.getResult();
			Map params = buildParams(settings, "view.html", settings.getString(Keys.charset), "book.xml", null);
			for (int i=1; i<=r.getResult().getBook().getVolumes(); i++) {
				PreviewRenderer pr = done.remove(i);
				if (pr!=null) {
					pr.abort();
					if (pr.getFile()!=null) {
						logger.fine("Removing old renderer");
						pr.getFile().delete();
					}
				}
		        done.put(i, new PreviewRenderer(brr.getURI(), i, this, params));
			}
		} catch (IllegalArgumentException iae) { 
			saxonNotAvailable = true;
		}
	}
	
	public synchronized boolean myTurn(int vol) {
		for (Integer i : done.keySet()) {
			if (!done.get(i).isDone()) {
				return vol == i;
			}
		}
		return true;
	}
	
	private boolean settingsChanged() {
		String brailleFont = settings.getString(Keys.brailleFont);
		String textFont = settings.getString(Keys.textFont);
		String charset = settings.getString(Keys.charset);
		boolean changed = 
			(this.brailleFont!=null && !this.brailleFont.equals(brailleFont)) ||
			(this.textFont!=null && !this.textFont.equals(textFont)) ||
			(this.charset!=null && !this.charset.equals(charset));
		this.brailleFont = brailleFont;
		this.textFont = textFont;
		this.charset = charset;
		return changed;
	}
	
	private boolean fileChanged() {
		if (r.getResult().getBookFile()==null) {
			return false;
		} else {
			return lastUpdated buildParams(Settings settings, String target, String charset, String file, String volume) {
		HashMap params = new HashMap<>();
		if (file!=null) {
			Table table = null;
			if (charset!=null) { 
				table = TableCatalog.newInstance().get(charset);
			}
			if (table==null) {
				table = TableCatalog.newInstance().get(DefaultTableProvider.TableType.EN_US.getIdentifier());
				settings.getSetPref(Keys.charset, table.getIdentifier());
			}
			params.put("uri", file);
			if (volume!=null && !"".equals(volume)) {
				params.put("volume", volume);
			} else {
				params.put("volume", "1");
			}
			BrailleConverter as = table.newBrailleConverter();
			if (as.supportsEightDot()) {
				params.put("code", as.toText(BrailleConstants.BRAILLE_PATTERNS_256));
			} else {
				params.put("code", as.toText(BrailleConstants.BRAILLE_PATTERNS_64));
			}
			params.put("toggle-view-label", Messages.getString(L10nKeys.XSLT_TOGGLE_VIEW));
			params.put("return-label", Messages.getString(L10nKeys.XSLT_RETURN_LABEL));
			params.put("emboss-view-label", Messages.getString(L10nKeys.EMBOSS_VIEW));
			params.put("preview-view-label", Messages.getString(L10nKeys.PREVIEW_VIEW));
			params.put("find-view-label", Messages.getString(L10nKeys.MENU_OPEN));
			params.put("setup-view-label", Messages.getString(L10nKeys.MENU_SETUP));
			params.put("about-software-label", Messages.getString(L10nKeys.MENU_ABOUT_SOFTWARE));
			params.put("unknown-author-label", Messages.getString(L10nKeys.UNKNOWN_AUTHOR));
			params.put("unknown-title-label", Messages.getString(L10nKeys.UNKNOWN_TITLE));
			params.put("showing-pages-label", Messages.getString(L10nKeys.XSLT_SHOWING_PAGES));
			params.put("about-label", Messages.getString(L10nKeys.XSLT_ABOUT_LABEL));
			params.put("show-source", Messages.getString(L10nKeys.XSLT_VIEW_SOURCE));
			params.put("volume-label", Messages.getString(L10nKeys.XSLT_VOLUME_LABEL));
			params.put("section-label", Messages.getString(L10nKeys.XSLT_SECTION_LABEL));
			params.put("page-label", Messages.getString(L10nKeys.XSLT_PAGE_LABEL));
			params.put("sheets-label", Messages.getString(L10nKeys.XSLT_SHEETS_LABEL));
			params.put("viewing-label", Messages.getString(L10nKeys.XSLT_VIEWING_LABEL));
			params.put("go-to-page-label", Messages.getString(L10nKeys.XSLT_GO_TO_PAGE_LABEL));
			params.put("uri-string", target + "?file=" + file + "&charset=" + charset);
			try {
				params.put("brailleFont", URLDecoder.decode(settings.getString(Keys.brailleFont, ""), MainPage.ENCODING));
				params.put("textFont", URLDecoder.decode(settings.getString(Keys.textFont, ""), MainPage.ENCODING));
			} catch (UnsupportedEncodingException e) {
				// should never happen if encoding is UTF-8
			}
		}
		return params;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy