OSDN Git Service

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