- // find the appropriate icon for an attachment
- private String findIcon(String appl) {
- logger.log(logger.HIGH, "Entering NeverNote.findIcon");
- appl = appl.toLowerCase();
- String relativePath = appl + ".png";
- File f = Global.getFileManager().getImageDirFile(relativePath);
- if (f.exists()) {
- return relativePath;
- }
- if (f.exists())
- return appl+".png";
- logger.log(logger.HIGH, "Leaving NeverNote.findIcon");
- return "attachment.png";
- }
- // Modify the en-media tag into an attachment
- private void modifyApplicationTags(QDomDocument doc, QDomElement docElem, QDomElement enmedia, QDomAttr hash, String appl) {
- logger.log(logger.HIGH, "Entering NeverNote.modifyApplicationTags");
- if (appl.equalsIgnoreCase("vnd.evernote.ink"))
- inkNote = true;
- String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNote.getGuid(), hash.value());
- Resource r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid, false);
- if (r == null || r.getData() == null)
- resourceErrorMessage();
- if (r!= null) {
- if (r.getData()!=null) {
- // Did we get a generic applicaiton? Then look at the file name to
- // try and find a good application type for the icon
- if (appl.equalsIgnoreCase("octet-stream")) {
- if (r.getAttributes() != null && r.getAttributes().getFileName() != null) {
- String fn = r.getAttributes().getFileName();
- int pos = fn.lastIndexOf(".");
- if (pos > -1) {
- appl = fn.substring(pos+1);
- }
- }
- }
-
- String fileDetails = null;
- if (r.getAttributes() != null && r.getAttributes().getFileName() != null && !r.getAttributes().getFileName().equals(""))
- fileDetails = r.getAttributes().getFileName();
- String contextFileName;
- FileManager fileManager = Global.getFileManager();
- if (fileDetails != null && !fileDetails.equals("")) {
- enmedia.setAttribute("href", "nnres://" +r.getGuid() +Global.attachmentNameDelimeter +fileDetails);
- contextFileName = fileManager.getResDirPath(r.getGuid() + Global.attachmentNameDelimeter + fileDetails);
- } else {
- enmedia.setAttribute("href", "nnres://" +r.getGuid() +Global.attachmentNameDelimeter +appl);
- contextFileName = fileManager.getResDirPath(r.getGuid() + Global.attachmentNameDelimeter + appl);
- }
- contextFileName = contextFileName.replace("\\", "/");
- enmedia.setAttribute("onContextMenu", "window.jambi.resourceContextMenu('" +contextFileName +"');");
- if (fileDetails == null || fileDetails.equals(""))
- fileDetails = "";
- enmedia.setAttribute("en-tag", "en-media");
- enmedia.setAttribute("guid", r.getGuid());
- enmedia.setTagName("a");
- QDomElement newText = doc.createElement("img");
- boolean goodPreview = false;
- String filePath = "";
- if (appl.equalsIgnoreCase("pdf") && Global.pdfPreview()) {
- String fileName;
- Resource res = conn.getNoteTable().noteResourceTable.getNoteResource(r.getGuid(), true);
- if (res.getAttributes() != null &&
- res.getAttributes().getFileName() != null &&
- !res.getAttributes().getFileName().trim().equals(""))
- fileName = res.getGuid()+Global.attachmentNameDelimeter+res.getAttributes().getFileName();
- else
- fileName = res.getGuid()+".pdf";
- QFile file = new QFile(fileManager.getResDirPath(fileName));
- QFile.OpenMode mode = new QFile.OpenMode();
- mode.set(QFile.OpenModeFlag.WriteOnly);
- file.open(mode);
- QDataStream out = new QDataStream(file);
- Resource resBinary = conn.getNoteTable().noteResourceTable.getNoteResource(res.getGuid(), true);
- QByteArray binData = new QByteArray(resBinary.getData().getBody());
- resBinary = null;
- out.writeBytes(binData.toByteArray());
- file.close();
- PDFPreview pdfPreview = new PDFPreview();
- goodPreview = pdfPreview.setupPreview(file.fileName(), appl,0);
- if (goodPreview) {
- QDomElement span = doc.createElement("span");
- QDomElement table = doc.createElement("table");
- span.setAttribute("pdfNavigationTable", "true");
- QDomElement tr = doc.createElement("tr");
- QDomElement td = doc.createElement("td");
- QDomElement left = doc.createElement("img");
- left.setAttribute("onMouseDown", "window.jambi.nextPage('" +file.fileName() +"')");
- left.setAttribute("onMouseDown", "window.jambi.nextPage('" +file.fileName() +"')");
- left.setAttribute("onMouseOver", "style.cursor='hand'");
- QDomElement right = doc.createElement("img");
- right.setAttribute("onMouseDown", "window.jambi.nextPage('" +file.fileName() +"')");
- left.setAttribute("onMouseDown", "window.jambi.previousPage('" +file.fileName() +"')");
- // NFC TODO: should these be file:// URLs?
- left.setAttribute("src", Global.getFileManager().getImageDirPath("small_left.png"));
- right.setAttribute("src", Global.getFileManager().getImageDirPath("small_right.png"));
- right.setAttribute("onMouseOver", "style.cursor='hand'");
-
- table.appendChild(tr);
- tr.appendChild(td);
- td.appendChild(left);
- td.appendChild(right);
- span.appendChild(table);
- enmedia.parentNode().insertBefore(span, enmedia);
- }
- filePath = fileName+".png";
- }
- String icon = findIcon(appl);
- if (icon.equals("attachment.png"))
- icon = findIcon(fileDetails.substring(fileDetails.indexOf(".")+1));
- // NFC TODO: should this be a 'file://' URL?
- newText.setAttribute("src", Global.getFileManager().getImageDirPath(icon));
- if (goodPreview) {
- // NFC TODO: should this be a 'file://' URL?
- newText.setAttribute("src", fileManager.getResDirPath(filePath));
- newText.setAttribute("style", "border-style:solid; border-color:green; padding:0.5mm 0.5mm 0.5mm 0.5mm;");
- }
- newText.setAttribute("title", fileDetails);
- enmedia.removeChild(enmedia.firstChild());
-
- enmedia.appendChild(newText);
- }
- }
- logger.log(logger.HIGH, "Leaving NeverNote.modifyApplicationTags");
- }
- // Modify the en-to tag into an input field
- private void modifyTodoTags(QDomElement todo) {
- logger.log(logger.HIGH, "Entering NeverNote.modifyTodoTags");
- todo.setAttribute("type", "checkbox");
- String checked = todo.attribute("checked");
- todo.removeAttribute("checked");
- if (checked.equalsIgnoreCase("true"))
- todo.setAttribute("checked", "");
- else
- todo.setAttribute("unchecked","");
- todo.setAttribute("value", checked);
- todo.setAttribute("onClick", "value=checked;window.jambi.contentChanged(); ");
- todo.setTagName("input");
- logger.log(logger.HIGH, "Leaving NeverNote.modifyTodoTags");
- }
- // Modify any cached todo tags that may have changed
- private String modifyCachedTodoTags(String note) {
- logger.log(logger.HIGH, "Entering NeverNote.modifyCachedTodoTags");
- StringBuffer html = new StringBuffer(note);
- for (int i=html.indexOf("<input", 0); i>-1; i=html.indexOf("<input", i)) {
- int endPos =html.indexOf(">",i+1);
- String input = html.substring(i,endPos);
- if (input.indexOf("value=\"true\"") > 0)
- input = input.replace("unchecked=\"\"", "checked=\"\"");
- else
- input = input.replace("checked=\"\"", "unchecked=\"\"");
- html.replace(i, endPos, input);
- i++;
- }
- logger.log(logger.HIGH, "Leaving NeverNote.modifyCachedTodoTags");
- return html.toString();
- }
- // Modify the en-media tag into an image tag so it can be displayed.
- private void modifyImageTags(QDomElement docElem, QDomElement enmedia, QDomAttr hash) {
- logger.log(logger.HIGH, "Entering NeverNote.modifyImageTags");
- String type = enmedia.attribute("type");
- if (type.startsWith("image/"))
- type = "."+type.substring(6);
- else
- type="";
-
- String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNoteGuid, hash.value());
- QFile tfile = new QFile(Global.getFileManager().getResDirPath(resGuid + type));
- if (!tfile.exists()) {
- Resource r = null;
- if (resGuid != null)
- r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid,true);
- if (r==null || r.getData() == null || r.getData().getBody().length == 0)
- resourceErrorMessage();
- if (r!= null && r.getData() != null && r.getData().getBody().length > 0) {
- tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));
- QByteArray binData = new QByteArray(r.getData().getBody());
- tfile.write(binData);
- tfile.close();
- enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());
- enmedia.setAttribute("en-tag", "en-media");
- enmedia.setNodeValue("");
- enmedia.setAttribute("guid", r.getGuid());
- enmedia.setTagName("img");
- }
- }
- enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());
- enmedia.setAttribute("en-tag", "en-media");
- enmedia.setAttribute("onContextMenu", "window.jambi.imageContextMenu('" +tfile.fileName() +"');");
- enmedia.setNodeValue("");
- enmedia.setAttribute("guid", resGuid);
- enmedia.setTagName("img");
-
- logger.log(logger.HIGH, "Leaving NeverNote.modifyImageTags");
- }
- // Modify tags from Evernote specific things to XHTML tags.
- private QDomDocument modifyTags(QDomDocument doc) {
- logger.log(logger.HIGH, "Entering NeverNote.modifyTags");
- if (tempFiles == null)
- tempFiles = new ArrayList<QTemporaryFile>();
- tempFiles.clear();
- QDomElement docElem = doc.documentElement();
-
- // Modify en-media tags
- QDomNodeList anchors = docElem.elementsByTagName("en-media");
- int enMediaCount = anchors.length();
- for (int i=enMediaCount-1; i>=0; i--) {
- QDomElement enmedia = anchors.at(i).toElement();
- if (enmedia.hasAttribute("type")) {
- QDomAttr attr = enmedia.attributeNode("type");
- QDomAttr hash = enmedia.attributeNode("hash");
- String[] type = attr.nodeValue().split("/");
- String appl = type[1];
-
- if (type[0] != null) {
- if (type[0].equals("image")) {
- modifyImageTags(docElem, enmedia, hash);
- }
- if (!type[0].equals("image")) {
- modifyApplicationTags(doc, docElem, enmedia, hash, appl);
- }
- }
- }
- }
-
- // Modify todo tags
- anchors = docElem.elementsByTagName("en-todo");
- int enTodoCount = anchors.length();
- for (int i=enTodoCount-1; i>=0; i--) {
- QDomElement enmedia = anchors.at(i).toElement();
- modifyTodoTags(enmedia);
- }
-
- // Modify en-crypt tags
- anchors = docElem.elementsByTagName("en-crypt");
- int enCryptLen = anchors.length();
- for (int i=enCryptLen-1; i>=0; i--) {
- QDomElement enmedia = anchors.at(i).toElement();
- enmedia.setAttribute("contentEditable","false");
- enmedia.setAttribute("src", Global.getFileManager().getImageDirPath("encrypt.png"));
- enmedia.setAttribute("en-tag","en-crypt");
- enmedia.setAttribute("alt", enmedia.text());
- Global.cryptCounter++;
- enmedia.setAttribute("id", "crypt"+Global.cryptCounter.toString());
- String encryptedText = enmedia.text();
-
- // If the encryption string contains crlf at the end, remove them because they mess up the javascript.
- if (encryptedText.endsWith("\n"))
- encryptedText = encryptedText.substring(0,encryptedText.length()-1);
- if (encryptedText.endsWith("\r"))
- encryptedText = encryptedText.substring(0,encryptedText.length()-1);
-
- // Add the commands
- String hint = enmedia.attribute("hint");
- hint = hint.replace("'","'");
- enmedia.setAttribute("onClick", "window.jambi.decryptText('crypt"+Global.cryptCounter.toString()+"', '"+encryptedText+"', '"+hint+"');");
- enmedia.setAttribute("onMouseOver", "style.cursor='hand'");
- enmedia.setTagName("img");
- enmedia.removeChild(enmedia.firstChild()); // Remove the actual encrypted text
- }
-
- logger.log(logger.HIGH, "Leaving NeverNote.modifyTags");
- return doc;
- }
- // Rebuild the note HTML to something usable
- private String rebuildNoteHTML(String noteGuid, String note) {
- logger.log(logger.HIGH, "Entering NeverNote.rebuildNoteHTML");
- logger.log(logger.EXTREME, "Note guid: " +noteGuid);
- logger.log(logger.EXTREME, "Note Text:" +note);
- QDomDocument doc = new QDomDocument();
- QDomDocument.Result result = doc.setContent(note);
- if (!result.success) {
- logger.log(logger.MEDIUM, "Parse error when rebuilding HTML");
- logger.log(logger.MEDIUM, "Note guid: " +noteGuid);
- logger.log(logger.EXTREME, "Start of unmodified note HTML");
- logger.log(logger.EXTREME, note);
- logger.log(logger.EXTREME, "End of unmodified note HTML");
- return note;
- }
-
- if (tempFiles == null)
- tempFiles = new ArrayList<QTemporaryFile>();
- tempFiles.clear();
-
- doc = modifyTags(doc);
- doc = addHilight(doc);
- QDomElement docElem = doc.documentElement();
- docElem.setTagName("Body");
-// docElem.setAttribute("bgcolor", "green");
- logger.log(logger.EXTREME, "Rebuilt HTML:");
- logger.log(logger.EXTREME, doc.toString());
- logger.log(logger.HIGH, "Leaving NeverNote.rebuildNoteHTML");
- // Fix the stupid problem where inserting an <img> tag after an <a> tag (which is done
- // to get the <en-media> application tag to work properly) causes spaces to be inserted
- // between the <a> & <img>. This messes things up later. This is an ugly hack.
- StringBuffer html = new StringBuffer(doc.toString());
- for (int i=html.indexOf("<a en-tag=\"en-media\" ", 0); i>-1; i=html.indexOf("<a en-tag=\"en-media\" ", i)) {
- i=html.indexOf(">\n",i+1);
- int z = html.indexOf("<img",i);
- for (int j=z-1; j>i; j--)
- html.deleteCharAt(j);
- i=html.indexOf("/>", z+1);
- z = html.indexOf("</a>",i);
- for (int j=z-1; j>i+1; j--)
- html.deleteCharAt(j);
- }
- return html.toString();
- }
- // Scan and do hilighting of words
- private QDomDocument addHilight(QDomDocument doc) {
- EnSearch e = listManager.getEnSearch();
- if (e.hilightWords == null || e.hilightWords.size() == 0)
- return doc;
- XMLInsertHilight hilight = new XMLInsertHilight(doc, listManager.getEnSearch().hilightWords);
- return hilight.getDoc();
- }
-