OSDN Git Service

Cleanup of initial sync with ink notes and added the ability to highlight words in...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / xml / NoteFormatter.java
1 package cx.fbn.nevernote.xml;\r
2 \r
3 import java.io.File;\r
4 import java.util.ArrayList;\r
5 import java.util.List;\r
6 \r
7 import com.evernote.edam.type.Note;\r
8 import com.evernote.edam.type.Resource;\r
9 import com.trolltech.qt.core.QByteArray;\r
10 import com.trolltech.qt.core.QDataStream;\r
11 import com.trolltech.qt.core.QFile;\r
12 import com.trolltech.qt.core.QIODevice;\r
13 import com.trolltech.qt.core.QIODevice.OpenModeFlag;\r
14 import com.trolltech.qt.core.QTemporaryFile;\r
15 import com.trolltech.qt.core.QUrl;\r
16 import com.trolltech.qt.core.Qt.BGMode;\r
17 import com.trolltech.qt.gui.QColor;\r
18 import com.trolltech.qt.gui.QPainter;\r
19 import com.trolltech.qt.gui.QPixmap;\r
20 import com.trolltech.qt.xml.QDomAttr;\r
21 import com.trolltech.qt.xml.QDomDocument;\r
22 import com.trolltech.qt.xml.QDomElement;\r
23 import com.trolltech.qt.xml.QDomNodeList;\r
24 \r
25 import cx.fbn.nevernote.Global;\r
26 import cx.fbn.nevernote.config.FileManager;\r
27 import cx.fbn.nevernote.filters.EnSearch;\r
28 import cx.fbn.nevernote.gui.PDFPreview;\r
29 import cx.fbn.nevernote.sql.DatabaseConnection;\r
30 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
31 \r
32 public class NoteFormatter {\r
33 \r
34         private final ApplicationLogger logger;\r
35         private final DatabaseConnection conn;\r
36         public boolean resourceError = false;\r
37         public boolean readOnly = false;\r
38         public boolean addHighlight = true;\r
39         private Note currentNote;\r
40         private String currentNoteGuid;\r
41         private boolean pdfPreview;\r
42         ArrayList<QTemporaryFile> tempFiles;\r
43         private EnSearch enSearch;\r
44         private boolean noteHistory;\r
45         \r
46         public NoteFormatter(ApplicationLogger logger, DatabaseConnection conn, List<QTemporaryFile> tempFiles2) {\r
47                 this.logger = logger;\r
48                 this.conn = conn;\r
49                 noteHistory = false;\r
50         }\r
51         \r
52         \r
53         public void setNote(Note note, boolean pdfPreview) {\r
54                 currentNote = note;\r
55                 this.pdfPreview = pdfPreview;\r
56                 currentNoteGuid = note.getGuid();\r
57                 readOnly = false;\r
58                 resourceError = false;\r
59         }\r
60         \r
61         public void setHighlight(EnSearch search) {\r
62                 if (search==null || search.hilightWords == null ||search.hilightWords.size() == 0) {\r
63                         enSearch = null;\r
64                         addHighlight = false;\r
65                 } else {\r
66                         enSearch = search;\r
67                         addHighlight = true;\r
68                 }\r
69         }\r
70         \r
71         // Set if we are coming here through note histary.  It triggers longer file names to avoid conflicts\r
72         public void setNoteHistory(boolean value) {\r
73                 noteHistory = value;\r
74         }\r
75         \r
76         // Rebuild the note HTML to something usable\r
77         public String rebuildNoteHTML() {\r
78                 logger.log(logger.HIGH, "Entering NeverNote.rebuildNoteHTML");\r
79                 logger.log(logger.EXTREME, "Note guid: " +currentNoteGuid);\r
80                 logger.log(logger.EXTREME, "Note Text:" +currentNote);\r
81                 QDomDocument doc = new QDomDocument();\r
82                 QDomDocument.Result result = doc.setContent(currentNote.getContent());\r
83                 if (!result.success) {\r
84                         logger.log(logger.MEDIUM, "Parse error when rebuilding HTML");\r
85                         logger.log(logger.MEDIUM, "Note guid: " +currentNoteGuid);\r
86                         logger.log(logger.EXTREME, "Start of unmodified note HTML");\r
87                         logger.log(logger.EXTREME, currentNote.getContent());\r
88                         logger.log(logger.EXTREME, "End of unmodified note HTML");\r
89                         return currentNote.getContent();\r
90                 }\r
91 \r
92                 if (tempFiles == null)\r
93                         tempFiles = new ArrayList<QTemporaryFile>();\r
94                 tempFiles.clear();\r
95                 \r
96                 doc = modifyTags(doc);\r
97                 if (addHighlight)\r
98                         doc = addHilight(doc);\r
99                 QDomElement docElem = doc.documentElement();\r
100                 docElem.setTagName("Body");\r
101 //              docElem.setAttribute("bgcolor", "green");\r
102                 logger.log(logger.EXTREME, "Rebuilt HTML:");\r
103                 logger.log(logger.EXTREME, doc.toString());     \r
104                 logger.log(logger.HIGH, "Leaving NeverNote.rebuildNoteHTML");\r
105                 // Fix the stupid problem where inserting an <img> tag after an <a> tag (which is done\r
106                 // to get the <en-media> application tag to work properly) causes spaces to be inserted\r
107                 // between the <a> & <img>.  This messes things up later.  This is an ugly hack.\r
108                 StringBuffer html = new StringBuffer(doc.toString());\r
109                 for (int i=html.indexOf("<a en-tag=\"en-media\" ", 0); i>-1; i=html.indexOf("<a en-tag=\"en-media\" ", i)) {\r
110                         i=html.indexOf(">\n",i+1);\r
111                         int z = html.indexOf("<img",i);\r
112                         for (int j=z-1; j>i; j--) \r
113                                 html.deleteCharAt(j);\r
114                         i=html.indexOf("/>", z+1);\r
115                         z = html.indexOf("</a>",i);\r
116                         for (int j=z-1; j>i+1; j--) \r
117                                 html.deleteCharAt(j);\r
118                 } \r
119                 return html.toString();\r
120         }       \r
121 \r
122         private void addImageHilight(String resGuid, QFile f) {\r
123                 if (enSearch == null || enSearch.hilightWords == null || enSearch.hilightWords.size() == 0)\r
124                         return;\r
125                 \r
126                 // Get the recognition XML that tells where to hilight on the image\r
127                 Resource recoResource = conn.getNoteTable().noteResourceTable.getNoteResourceRecognition(resGuid);\r
128                 QByteArray recoData = new QByteArray(recoResource.getRecognition().getBody());\r
129                 String xml = recoData.toString();\r
130                 \r
131                 // Get a painter for the image.  This is the background (the initial image).\r
132         QPixmap pix = new QPixmap(f.fileName());\r
133         QPixmap hilightedPix = new QPixmap(pix.size());\r
134         QPainter p = new QPainter(hilightedPix);\r
135         p.drawPixmap(0,0, pix);\r
136 \r
137         // Create a transparent pixmap.  The only non-transparent\r
138         // piece is the hilight that will be overlayed to hilight text no the background\r
139         QPixmap overlayPix = new QPixmap(pix.size());\r
140         QPainter p2 = new QPainter(overlayPix);\r
141         p2.setBackgroundMode(BGMode.TransparentMode);\r
142         p2.setBrush(QColor.yellow);\r
143         \r
144                 // Get the recognition data from the note\r
145         QDomDocument doc = new QDomDocument();\r
146         doc.setContent(xml);\r
147         \r
148         // Go through all "item" nodes\r
149                 QDomNodeList anchors = doc.elementsByTagName("item");\r
150                 for (int i=0; i<anchors.length(); i++) {\r
151                         QDomElement element = anchors.at(i).toElement();\r
152                         int x = new Integer(element.attribute("x"));   // x coordinate\r
153                         int y = new Integer(element.attribute("y"));   // y coordinate\r
154                         int w = new Integer(element.attribute("w"));   // width\r
155                         int h = new Integer(element.attribute("h"));   // height\r
156                         QDomNodeList children = element.childNodes();  // all children ("t" nodes).\r
157                         \r
158                         // Go through the children ("t" nodes)\r
159                         for (int j=0; j<children.length(); j++) {\r
160                         QDomElement child = children.at(j).toElement();\r
161                         if (child.nodeName().equalsIgnoreCase("t")) {\r
162                                 String text = child.text();   // recognition text\r
163                                 int weight = new Integer(child.attribute("w"));  // recognition weight\r
164                                 if (weight >= Global.getRecognitionWeight()) {   // Are we above the maximum?\r
165                                         \r
166                                         // Check to see if this word matches something we were searching for.\r
167                                         for (int k=0; k<enSearch.hilightWords.size(); k++) {\r
168                                                 if (enSearch.hilightWords.get(k).equalsIgnoreCase(text))\r
169                                                         p2.drawRect(x,y,w,h);                           \r
170                                         }\r
171                                 }\r
172                         }\r
173                         }\r
174                 }\r
175         p2.end();\r
176         \r
177         // Paint the hilight onto the background.\r
178         p.setOpacity(0.40);\r
179         p.drawPixmap(0,0, overlayPix);\r
180         p.end();\r
181         \r
182         // Save over the initial pixmap.\r
183         hilightedPix.save(f.fileName());\r
184         }\r
185         \r
186     // Modify the en-media tag into an image tag so it can be displayed.\r
187     private void modifyImageTags(QDomElement docElem, QDomElement enmedia, QDomAttr hash) {\r
188         logger.log(logger.HIGH, "Entering NeverNote.modifyImageTags");\r
189         String type = enmedia.attribute("type");\r
190         if (type.startsWith("image/"))\r
191                 type = "."+type.substring(6);\r
192         else\r
193                 type="";\r
194         \r
195         String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNoteGuid, hash.value());\r
196         QFile tfile = new QFile(Global.getFileManager().getResDirPath(resGuid + type));\r
197 //      if (!tfile.exists()) {\r
198                 Resource r = null;\r
199                 if (resGuid != null)\r
200                         r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid,true);\r
201                         if (r==null || r.getData() == null || r.getData().getBody().length == 0)\r
202                                 resourceError = true;;\r
203                         if (r!= null && r.getData() != null && r.getData().getBody().length > 0) {\r
204                                 tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));\r
205                                 QByteArray binData = new QByteArray(r.getData().getBody());\r
206                                 tfile.write(binData);\r
207                                 tfile.close();\r
208                                 \r
209                                 // If we have recognition text, outline it\r
210                                 addImageHilight(r.getGuid(), tfile);\r
211                                 \r
212                                 enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());\r
213                                 enmedia.setAttribute("en-tag", "en-media");\r
214                                 enmedia.setNodeValue("");\r
215                         enmedia.setAttribute("guid", r.getGuid());\r
216                         enmedia.setTagName("img");\r
217                 }\r
218 //      }\r
219         // Technically, we should do a file:// to have a proper url, but for some reason QWebPage hates\r
220         // them and won't generate a thumbnail image properly if we use them.\r
221 //              enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());\r
222                 enmedia.setAttribute("src", tfile.fileName().toString());\r
223                 enmedia.setAttribute("en-tag", "en-media");\r
224                 enmedia.setAttribute("onContextMenu", "window.jambi.imageContextMenu('" +tfile.fileName()  +"');");\r
225                 enmedia.setNodeValue("");\r
226                 enmedia.setAttribute("guid", resGuid);\r
227                 enmedia.setTagName("img");\r
228 \r
229                 logger.log(logger.HIGH, "Leaving NeverNote.modifyImageTags");\r
230     }\r
231     \r
232     \r
233         // Modify tags from Evernote specific things to XHTML tags.\r
234         private QDomDocument modifyTags(QDomDocument doc) {\r
235                 logger.log(logger.HIGH, "Entering NeverNote.modifyTags");\r
236                 if (tempFiles == null)\r
237                         tempFiles = new ArrayList<QTemporaryFile>();\r
238                 tempFiles.clear();\r
239                 QDomElement docElem = doc.documentElement();\r
240                 \r
241                 // Modify en-media tags\r
242                 QDomNodeList anchors = docElem.elementsByTagName("en-media");\r
243                 int enMediaCount = anchors.length();\r
244                 for (int i=enMediaCount-1; i>=0; i--) {\r
245                         QDomElement enmedia = anchors.at(i).toElement();\r
246                         if (enmedia.hasAttribute("type")) {\r
247                                 QDomAttr attr = enmedia.attributeNode("type");\r
248                                 QDomAttr hash = enmedia.attributeNode("hash");\r
249                                 String[] type = attr.nodeValue().split("/");\r
250                                 String appl = type[1];\r
251                                 \r
252                                 if (type[0] != null) {\r
253                                         if (type[0].equals("image")) {\r
254                                                 modifyImageTags(docElem, enmedia, hash);\r
255                                         }\r
256                                         if (!type[0].equals("image")) {\r
257                                                 modifyApplicationTags(doc, docElem, enmedia, hash, appl);\r
258                                         }\r
259                                 }\r
260                         }\r
261                 }\r
262                 \r
263                 // Modify todo tags\r
264                 anchors = docElem.elementsByTagName("en-todo");\r
265                 int enTodoCount = anchors.length();\r
266                 for (int i=enTodoCount-1; i>=0; i--) {\r
267                         QDomElement enmedia = anchors.at(i).toElement();\r
268                         modifyTodoTags(enmedia);\r
269                 }\r
270                 \r
271                 // Modify en-crypt tags\r
272                 anchors = docElem.elementsByTagName("en-crypt");\r
273                 int enCryptLen = anchors.length();\r
274                 for (int i=enCryptLen-1; i>=0; i--) {\r
275                         QDomElement enmedia = anchors.at(i).toElement();\r
276                         enmedia.setAttribute("contentEditable","false");\r
277                         enmedia.setAttribute("src", Global.getFileManager().getImageDirPath("encrypt.png"));\r
278                         enmedia.setAttribute("en-tag","en-crypt");\r
279                         enmedia.setAttribute("alt", enmedia.text());\r
280                         Global.cryptCounter++;\r
281                         enmedia.setAttribute("id", "crypt"+Global.cryptCounter.toString());\r
282                         String encryptedText = enmedia.text();\r
283                         \r
284                         // If the encryption string contains crlf at the end, remove them because they mess up the javascript.\r
285                         if (encryptedText.endsWith("\n"))\r
286                                 encryptedText = encryptedText.substring(0,encryptedText.length()-1);\r
287                         if (encryptedText.endsWith("\r"))\r
288                                 encryptedText = encryptedText.substring(0,encryptedText.length()-1);\r
289                         \r
290                         // Add the commands\r
291                         String hint = enmedia.attribute("hint");\r
292                         hint = hint.replace("'","&apos;");\r
293                         enmedia.setAttribute("onClick", "window.jambi.decryptText('crypt"+Global.cryptCounter.toString()+"', '"+encryptedText+"', '"+hint+"');");\r
294                         enmedia.setAttribute("onMouseOver", "style.cursor='hand'");\r
295                         enmedia.setTagName("img");\r
296                         enmedia.removeChild(enmedia.firstChild());   // Remove the actual encrypted text\r
297                 }\r
298 \r
299                 \r
300                 // Modify link tags\r
301                 anchors = docElem.elementsByTagName("a");\r
302                 enCryptLen = anchors.length();\r
303                 for (int i=0; i<anchors.length(); i++) {\r
304                         QDomElement element = anchors.at(i).toElement();\r
305                         element.setAttribute("title", element.attribute("href"));\r
306                 }\r
307 \r
308                 logger.log(logger.HIGH, "Leaving NeverNote.modifyTags");\r
309                 return doc;\r
310         }\r
311         \r
312 \r
313         // Get an ink note image.  If an image doesn't exist then we fall back \r
314         // to the old ugly icon\r
315     private boolean buildInkNote(QDomDocument doc, QDomElement docElem, QDomElement enmedia, QDomAttr hash, String appl) {\r
316         String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNote.getGuid(), hash.value());\r
317         Resource r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid, false);\r
318  \r
319         // If we can't find the resource, then fall back to the old method.  We'll return & show\r
320         // an error later\r
321         if (r == null || r.getData() == null) \r
322                 return false;\r
323         \r
324         // If there isn't some type of error, continue on.\r
325                 if (!resourceError) {\r
326                         \r
327                         // Get a list of images in the database.  We'll use these to bulid the page.\r
328                         List<QByteArray> data = conn.getInkImagesTable().getImage(r.getGuid());\r
329                         \r
330                         // If no pictures are found, go back to & just show the icon\r
331                         if (data.size() == 0)\r
332                                 return false;\r
333                         \r
334                         // We have pictures, so append them to the page.  This really isn't proper since\r
335                         // we leave the en-media tag in place, but since we can't edit the page it doesn't\r
336                         // hurt anything.\r
337                         for (int i=0; i<data.size(); i++) {\r
338                         QFile f = new QFile(Global.getFileManager().getResDirPath(resGuid + new Integer(i).toString()+".png"));\r
339                                 f.open(OpenModeFlag.WriteOnly);\r
340                                 f.write(data.get(i));\r
341                                 f.close();\r
342                                 QDomElement newImage = doc.createElement("img");\r
343                                 newImage.setAttribute("src", QUrl.fromLocalFile(f.fileName()).toString());\r
344                                 enmedia.appendChild(newImage);\r
345                         }\r
346                         return true;\r
347                 }\r
348         return false;\r
349     }\r
350         \r
351         \r
352     // Modify the en-media tag into an attachment\r
353     private void modifyApplicationTags(QDomDocument doc, QDomElement docElem, QDomElement enmedia, QDomAttr hash, String appl) {\r
354         logger.log(logger.HIGH, "Entering NeverNote.modifyApplicationTags");\r
355         if (appl.equalsIgnoreCase("vnd.evernote.ink")) {\r
356                 readOnly = true;\r
357             if (buildInkNote(doc, docElem, enmedia, hash, appl))\r
358                 return;\r
359         }\r
360         String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNote.getGuid(), hash.value());\r
361         Resource r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid, false);\r
362         if (r == null || r.getData() == null) \r
363                 resourceError = true;\r
364                 if (r!= null) {\r
365                         if (r.getData()!=null) {\r
366                                 // Did we get a generic applicaiton?  Then look at the file name to \r
367                                 // try and find a good application type for the icon\r
368                                 if (appl.equalsIgnoreCase("octet-stream")) {\r
369                                         if (r.getAttributes() != null && r.getAttributes().getFileName() != null) {\r
370                                                 String fn = r.getAttributes().getFileName();\r
371                                                 int pos = fn.lastIndexOf(".");\r
372                                                 if (pos > -1) {\r
373                                                         appl = fn.substring(pos+1);\r
374                                                 }\r
375                                         }\r
376                                 }\r
377                                 \r
378                                 String fileDetails = null;\r
379                                 if (r.getAttributes() != null && r.getAttributes().getFileName() != null && !r.getAttributes().getFileName().equals(""))\r
380                                         fileDetails = r.getAttributes().getFileName();\r
381                                 String contextFileName;\r
382                                 FileManager fileManager = Global.getFileManager();\r
383                 if (fileDetails != null && !fileDetails.equals("")) {\r
384                         if (!noteHistory) {\r
385                                 enmedia.setAttribute("href", "nnres://" +r.getGuid() \r
386                                                 +Global.attachmentNameDelimeter +fileDetails);\r
387                                 contextFileName = fileManager.getResDirPath(r.getGuid() \r
388                                                 +Global.attachmentNameDelimeter + fileDetails);\r
389                         } else {\r
390                                 enmedia.setAttribute("href", "nnres://" +r.getGuid() + currentNote.getUpdateSequenceNum() \r
391                                                 +Global.attachmentNameDelimeter +fileDetails);\r
392                                 contextFileName = fileManager.getResDirPath(r.getGuid() + currentNote.getUpdateSequenceNum() \r
393                                                 +Global.attachmentNameDelimeter + fileDetails);\r
394                         }\r
395                                 } else { \r
396                                         if (!noteHistory) {\r
397                                                 enmedia.setAttribute("href", "nnres://" +r.getGuid() +currentNote.getUpdateSequenceNum()\r
398                                                                 +Global.attachmentNameDelimeter +appl);\r
399                                                 contextFileName = fileManager.getResDirPath(r.getGuid() +currentNote.getUpdateSequenceNum() \r
400                                                                 +Global.attachmentNameDelimeter + appl);\r
401                                         } else {\r
402                                                 enmedia.setAttribute("href", "nnres://" +r.getGuid() \r
403                                                                 +Global.attachmentNameDelimeter +appl);\r
404                                                 contextFileName = fileManager.getResDirPath(r.getGuid() \r
405                                                                 +Global.attachmentNameDelimeter + appl);\r
406                                         }\r
407                                 }\r
408                                 contextFileName = contextFileName.replace("\\", "/");\r
409                                 enmedia.setAttribute("onContextMenu", "window.jambi.resourceContextMenu('" +contextFileName +"');");\r
410                                 if (fileDetails == null || fileDetails.equals(""))\r
411                                         fileDetails = "";\r
412                                 enmedia.setAttribute("en-tag", "en-media");\r
413                                 enmedia.setAttribute("guid", r.getGuid());\r
414                                 enmedia.setTagName("a");\r
415                                 QDomElement newText = doc.createElement("img");\r
416                                 boolean goodPreview = false;\r
417                                 String filePath = "";\r
418                                 if (appl.equalsIgnoreCase("pdf") && pdfPreview) {\r
419                                         String fileName;\r
420                                         Resource res = conn.getNoteTable().noteResourceTable.getNoteResource(r.getGuid(), true);\r
421                                         if (res.getAttributes() != null && \r
422                                                         res.getAttributes().getFileName() != null && \r
423                                                         !res.getAttributes().getFileName().trim().equals(""))\r
424                                                 fileName = res.getGuid()+Global.attachmentNameDelimeter+res.getAttributes().getFileName();\r
425                                         else\r
426                                                 fileName = res.getGuid()+".pdf";\r
427                                         QFile file = new QFile(fileManager.getResDirPath(fileName));\r
428                                 QFile.OpenMode mode = new QFile.OpenMode();\r
429                                 mode.set(QFile.OpenModeFlag.WriteOnly);\r
430                                 file.open(mode);\r
431                                 QDataStream out = new QDataStream(file);\r
432                                 Resource resBinary = conn.getNoteTable().noteResourceTable.getNoteResource(res.getGuid(), true);\r
433                                         QByteArray binData = new QByteArray(resBinary.getData().getBody());\r
434                                         resBinary = null;\r
435                                 out.writeBytes(binData.toByteArray());\r
436                                 file.close();\r
437                                 PDFPreview pdfPreview = new PDFPreview();\r
438                                         goodPreview = pdfPreview.setupPreview(file.fileName(), appl,0);\r
439                                         if (goodPreview) {\r
440                                                 QDomElement span = doc.createElement("span");\r
441                                                 QDomElement table = doc.createElement("table");\r
442                                                 span.setAttribute("pdfNavigationTable", "true");\r
443                                                 QDomElement tr = doc.createElement("tr");\r
444                                                 QDomElement td = doc.createElement("td");\r
445                                                 QDomElement left = doc.createElement("img");\r
446                                                 left.setAttribute("onMouseDown", "window.jambi.nextPage('" +file.fileName() +"')");\r
447                                                 left.setAttribute("onMouseDown", "window.jambi.nextPage('" +file.fileName() +"')");\r
448                                                 left.setAttribute("onMouseOver", "style.cursor='hand'");\r
449                                                 QDomElement right = doc.createElement("img");\r
450                                                 right.setAttribute("onMouseDown", "window.jambi.nextPage('" +file.fileName() +"')");\r
451                                                 left.setAttribute("onMouseDown", "window.jambi.previousPage('" +file.fileName() +"')");\r
452                                                 // NFC TODO: should these be file:// URLs?\r
453                                                 left.setAttribute("src", Global.getFileManager().getImageDirPath("small_left.png"));\r
454                                                 right.setAttribute("src", Global.getFileManager().getImageDirPath("small_right.png"));\r
455                                                 right.setAttribute("onMouseOver", "style.cursor='hand'");\r
456                                                 \r
457                                                 table.appendChild(tr);\r
458                                                 tr.appendChild(td);\r
459                                                 td.appendChild(left);\r
460                                                 td.appendChild(right);\r
461                                                 span.appendChild(table);\r
462                                                 enmedia.parentNode().insertBefore(span, enmedia);\r
463                                         } \r
464                                         filePath = fileName+".png";\r
465                                 }\r
466                                 String icon = findIcon(appl);\r
467                                 if (icon.equals("attachment.png"))\r
468                                         icon = findIcon(fileDetails.substring(fileDetails.indexOf(".")+1));\r
469                                 // NFC TODO: should this be a 'file://' URL?\r
470                                 newText.setAttribute("src", Global.getFileManager().getImageDirPath(icon));\r
471                                 if (goodPreview) {\r
472                                 // NFC TODO: should this be a 'file://' URL?\r
473                                         newText.setAttribute("src", fileManager.getResDirPath(filePath));\r
474                                         newText.setAttribute("style", "border-style:solid; border-color:green; padding:0.5mm 0.5mm 0.5mm 0.5mm;");\r
475                                 }\r
476                                 newText.setAttribute("title", fileDetails);\r
477                                 enmedia.removeChild(enmedia.firstChild());\r
478                                 \r
479                                 enmedia.appendChild(newText);\r
480                         }\r
481                 }\r
482                 logger.log(logger.HIGH, "Leaving NeverNote.modifyApplicationTags");\r
483     }\r
484     // Modify the en-to tag into an input field\r
485     private void modifyTodoTags(QDomElement todo) {\r
486         logger.log(logger.HIGH, "Entering NeverNote.modifyTodoTags");\r
487                 todo.setAttribute("type", "checkbox");\r
488                 String checked = todo.attribute("checked");\r
489                 todo.removeAttribute("checked");\r
490                 if (checked.equalsIgnoreCase("true"))\r
491                         todo.setAttribute("checked", "");\r
492                 else\r
493                         todo.setAttribute("unchecked","");\r
494                 todo.setAttribute("value", checked);\r
495                 todo.setAttribute("onClick", "value=checked;window.jambi.contentChanged(); ");\r
496                 todo.setTagName("input");\r
497                 logger.log(logger.HIGH, "Leaving NeverNote.modifyTodoTags");\r
498     }\r
499     \r
500     \r
501     \r
502     // Modify any cached todo tags that may have changed\r
503     public String modifyCachedTodoTags(String note) {\r
504         logger.log(logger.HIGH, "Entering NeverNote.modifyCachedTodoTags");\r
505         StringBuffer html = new StringBuffer(note);\r
506                 for (int i=html.indexOf("<input", 0); i>-1; i=html.indexOf("<input", i)) {\r
507                         int endPos =html.indexOf(">",i+1);\r
508                         String input = html.substring(i,endPos);\r
509                         if (input.indexOf("value=\"true\"") > 0) \r
510                                 input = input.replace(" unchecked=\"\"", " checked=\"\"");\r
511                         else\r
512                                 input = input.replace(" checked=\"\"", " unchecked=\"\"");\r
513                         html.replace(i, endPos, input);\r
514                         i++;\r
515                 }\r
516                 logger.log(logger.HIGH, "Leaving NeverNote.modifyCachedTodoTags");\r
517                 return html.toString();\r
518     }\r
519     \r
520     \r
521 \r
522 \r
523         // Scan and do hilighting of words\r
524         public QDomDocument addHilight(QDomDocument doc) {\r
525 //              EnSearch e = listManager.getEnSearch();\r
526                 if (enSearch.hilightWords == null || enSearch.hilightWords.size() == 0)\r
527                         return doc;\r
528                 XMLInsertHilight hilight = new XMLInsertHilight(doc, enSearch.hilightWords);\r
529                 return hilight.getDoc();\r
530         }\r
531         \r
532         \r
533     // find the appropriate icon for an attachment\r
534     private String findIcon(String appl) {\r
535         logger.log(logger.HIGH, "Entering NeverNote.findIcon");\r
536         appl = appl.toLowerCase();\r
537         String relativePath = appl + ".png";\r
538         File f = Global.getFileManager().getImageDirFile(relativePath);\r
539         if (f.exists()) {\r
540             return relativePath;\r
541         }\r
542         if (f.exists())\r
543                 return appl+".png";\r
544         logger.log(logger.HIGH, "Leaving NeverNote.findIcon");\r
545         return "attachment.png";\r
546     }\r
547 \r
548 }\r